Lines Matching full:vi
391 /* Find end of list, sew whole thing into vi->rq.pages. */ in give_pages()
410 static void virtnet_rq_free_buf(struct virtnet_info *vi, in virtnet_rq_free_buf() argument
413 if (vi->mergeable_rx_bufs) in virtnet_rq_free_buf()
415 else if (vi->big_packets) in virtnet_rq_free_buf()
421 static void enable_delayed_refill(struct virtnet_info *vi) in enable_delayed_refill() argument
423 spin_lock_bh(&vi->refill_lock); in enable_delayed_refill()
424 vi->refill_enabled = true; in enable_delayed_refill()
425 spin_unlock_bh(&vi->refill_lock); in enable_delayed_refill()
428 static void disable_delayed_refill(struct virtnet_info *vi) in disable_delayed_refill() argument
430 spin_lock_bh(&vi->refill_lock); in disable_delayed_refill()
431 vi->refill_enabled = false; in disable_delayed_refill()
432 spin_unlock_bh(&vi->refill_lock); in disable_delayed_refill()
460 struct virtnet_info *vi = vq->vdev->priv; in skb_xmit_done() local
461 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi; in skb_xmit_done()
470 netif_wake_subqueue(vi->dev, vq2txq(vq)); in skb_xmit_done()
527 static struct sk_buff *page_to_skb(struct virtnet_info *vi, in page_to_skb() argument
543 hdr_len = vi->hdr_len; in page_to_skb()
544 if (vi->mergeable_rx_bufs) in page_to_skb()
586 if (vi->mergeable_rx_bufs) { in page_to_skb()
747 static void virtnet_rq_set_premapped(struct virtnet_info *vi) in virtnet_rq_set_premapped() argument
752 if (!vi->mergeable_rx_bufs && vi->big_packets) in virtnet_rq_set_premapped()
755 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_rq_set_premapped()
756 if (virtqueue_set_dma_premapped(vi->rq[i].vq)) in virtnet_rq_set_premapped()
759 vi->rq[i].do_dma = true; in virtnet_rq_set_premapped()
765 struct virtnet_info *vi = vq->vdev->priv; in virtnet_rq_unmap_free_buf() local
769 rq = &vi->rq[i]; in virtnet_rq_unmap_free_buf()
774 virtnet_rq_free_buf(vi, rq, buf); in virtnet_rq_unmap_free_buf()
813 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) in is_xdp_raw_buffer_queue() argument
815 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) in is_xdp_raw_buffer_queue()
817 else if (q < vi->curr_queue_pairs) in is_xdp_raw_buffer_queue()
823 static void check_sq_full_and_disable(struct virtnet_info *vi, in check_sq_full_and_disable() argument
830 qnum = sq - vi->sq; in check_sq_full_and_disable()
858 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, in __virtnet_xdp_xmit_one() argument
867 if (unlikely(xdpf->headroom < vi->hdr_len)) in __virtnet_xdp_xmit_one()
882 xdpf->headroom -= vi->hdr_len; in __virtnet_xdp_xmit_one()
883 xdpf->data -= vi->hdr_len; in __virtnet_xdp_xmit_one()
886 memset(hdr, 0, vi->hdr_len); in __virtnet_xdp_xmit_one()
887 xdpf->len += vi->hdr_len; in __virtnet_xdp_xmit_one()
906 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
914 #define virtnet_xdp_get_sq(vi) ({ \ argument
917 typeof(vi) v = (vi); \
933 #define virtnet_xdp_put_sq(vi, q) { \ argument
935 typeof(vi) v = (vi); \
947 struct virtnet_info *vi = netdev_priv(dev); in virtnet_xdp_xmit() local
948 struct receive_queue *rq = vi->rq; in virtnet_xdp_xmit()
967 sq = virtnet_xdp_get_sq(vi); in virtnet_xdp_xmit()
993 if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) in virtnet_xdp_xmit()
999 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq)) in virtnet_xdp_xmit()
1000 check_sq_full_and_disable(vi, dev, sq); in virtnet_xdp_xmit()
1015 virtnet_xdp_put_sq(vi, sq); in virtnet_xdp_xmit()
1088 static unsigned int virtnet_get_headroom(struct virtnet_info *vi) in virtnet_get_headroom() argument
1090 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; in virtnet_get_headroom()
1171 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi, in receive_small_build_skb() argument
1182 headroom = vi->hdr_len + header_offset; in receive_small_build_skb()
1191 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len); in receive_small_build_skb()
1197 struct virtnet_info *vi, in receive_small_xdp() argument
1207 unsigned int headroom = vi->hdr_len + header_offset; in receive_small_xdp()
1227 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { in receive_small_xdp()
1229 unsigned int tlen = len + vi->hdr_len; in receive_small_xdp()
1232 xdp_headroom = virtnet_get_headroom(vi); in receive_small_xdp()
1234 headroom = vi->hdr_len + header_offset; in receive_small_xdp()
1249 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, in receive_small_xdp()
1288 struct virtnet_info *vi, in receive_small() argument
1304 len -= vi->hdr_len; in receive_small()
1314 if (unlikely(vi->xdp_enabled)) { in receive_small()
1320 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf, in receive_small()
1329 skb = receive_small_build_skb(vi, xdp_headroom, buf, len); in receive_small()
1340 struct virtnet_info *vi, in receive_big() argument
1348 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0); in receive_big()
1350 u64_stats_add(&stats->bytes, len - vi->hdr_len); in receive_big()
1392 struct virtnet_info *vi, in build_skb_from_xdp_buff() argument
1435 struct virtnet_info *vi, in virtnet_build_xdp_buff_mrg() argument
1457 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true); in virtnet_build_xdp_buff_mrg()
1483 virtio16_to_cpu(vi->vdev, hdr->num_buffers)); in virtnet_build_xdp_buff_mrg()
1523 static void *mergeable_xdp_get_buf(struct virtnet_info *vi, in mergeable_xdp_get_buf() argument
1557 if (likely(headroom >= virtnet_get_headroom(vi) && in mergeable_xdp_get_buf()
1572 xdp_page = xdp_linearize_page(vi->dev, rq, num_buf, in mergeable_xdp_get_buf()
1602 struct virtnet_info *vi, in receive_mergeable_xdp() argument
1612 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); in receive_mergeable_xdp()
1623 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page, in receive_mergeable_xdp()
1628 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz, in receive_mergeable_xdp()
1637 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz); in receive_mergeable_xdp()
1662 struct virtnet_info *vi, in receive_mergeable() argument
1671 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); in receive_mergeable()
1681 u64_stats_add(&stats->bytes, len - vi->hdr_len); in receive_mergeable()
1690 if (unlikely(vi->xdp_enabled)) { in receive_mergeable()
1696 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx, in receive_mergeable()
1704 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom); in receive_mergeable()
1716 virtio16_to_cpu(vi->vdev, in receive_mergeable()
1808 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, in receive_buf() argument
1813 struct net_device *dev = vi->dev; in receive_buf()
1818 if (unlikely(len < vi->hdr_len + ETH_HLEN)) { in receive_buf()
1821 virtnet_rq_free_buf(vi, rq, buf); in receive_buf()
1834 if (vi->mergeable_rx_bufs) in receive_buf()
1835 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, in receive_buf()
1837 else if (vi->big_packets) in receive_buf()
1838 skb = receive_big(dev, vi, rq, buf, len, stats); in receive_buf()
1840 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); in receive_buf()
1846 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report) in receive_buf()
1853 virtio_is_little_endian(vi->vdev))) { in receive_buf()
1878 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, in add_recvbuf_small() argument
1882 unsigned int xdp_headroom = virtnet_get_headroom(vi); in add_recvbuf_small()
1884 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; in add_recvbuf_small()
1899 virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN); in add_recvbuf_small()
1911 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, in add_recvbuf_big() argument
1918 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2); in add_recvbuf_big()
1920 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */ in add_recvbuf_big()
1921 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) { in add_recvbuf_big()
1944 sg_set_buf(&rq->sg[0], p, vi->hdr_len); in add_recvbuf_big()
1952 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2, in add_recvbuf_big()
1964 struct virtnet_info *vi = rq->vq->vdev->priv; in get_mergeable_buf_len() local
1965 const size_t hdr_len = vi->hdr_len; in get_mergeable_buf_len()
1977 static int add_recvbuf_mergeable(struct virtnet_info *vi, in add_recvbuf_mergeable() argument
1981 unsigned int headroom = virtnet_get_headroom(vi); in add_recvbuf_mergeable()
2039 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, in try_fill_recv() argument
2046 if (vi->mergeable_rx_bufs) in try_fill_recv()
2047 err = add_recvbuf_mergeable(vi, rq, gfp); in try_fill_recv()
2048 else if (vi->big_packets) in try_fill_recv()
2049 err = add_recvbuf_big(vi, rq, gfp); in try_fill_recv()
2051 err = add_recvbuf_small(vi, rq, gfp); in try_fill_recv()
2070 struct virtnet_info *vi = rvq->vdev->priv; in skb_recv_done() local
2071 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; in skb_recv_done()
2089 static void virtnet_napi_tx_enable(struct virtnet_info *vi, in virtnet_napi_tx_enable() argument
2099 if (!vi->affinity_hint_set) { in virtnet_napi_tx_enable()
2115 struct virtnet_info *vi = in refill_work() local
2120 for (i = 0; i < vi->curr_queue_pairs; i++) { in refill_work()
2121 struct receive_queue *rq = &vi->rq[i]; in refill_work()
2124 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); in refill_work()
2131 schedule_delayed_work(&vi->refill, HZ/2); in refill_work()
2138 struct virtnet_info *vi = rq->vq->vdev->priv; in virtnet_receive() local
2145 if (!vi->big_packets || vi->mergeable_rx_bufs) { in virtnet_receive()
2150 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); in virtnet_receive()
2156 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); in virtnet_receive()
2162 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { in virtnet_receive()
2163 spin_lock(&vi->refill_lock); in virtnet_receive()
2164 if (vi->refill_enabled) in virtnet_receive()
2165 schedule_delayed_work(&vi->refill, 0); in virtnet_receive()
2166 spin_unlock(&vi->refill_lock); in virtnet_receive()
2187 struct virtnet_info *vi = rq->vq->vdev->priv; in virtnet_poll_cleantx() local
2189 struct send_queue *sq = &vi->sq[index]; in virtnet_poll_cleantx()
2190 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); in virtnet_poll_cleantx()
2192 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) in virtnet_poll_cleantx()
2217 struct virtnet_info *vi = rq->vq->vdev->priv; in virtnet_poll() local
2234 sq = virtnet_xdp_get_sq(vi); in virtnet_poll()
2240 virtnet_xdp_put_sq(vi, sq); in virtnet_poll()
2246 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index) in virtnet_disable_queue_pair() argument
2248 virtnet_napi_tx_disable(&vi->sq[qp_index].napi); in virtnet_disable_queue_pair()
2249 napi_disable(&vi->rq[qp_index].napi); in virtnet_disable_queue_pair()
2250 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); in virtnet_disable_queue_pair()
2253 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index) in virtnet_enable_queue_pair() argument
2255 struct net_device *dev = vi->dev; in virtnet_enable_queue_pair()
2258 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index, in virtnet_enable_queue_pair()
2259 vi->rq[qp_index].napi.napi_id); in virtnet_enable_queue_pair()
2263 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq, in virtnet_enable_queue_pair()
2268 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi); in virtnet_enable_queue_pair()
2269 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); in virtnet_enable_queue_pair()
2274 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); in virtnet_enable_queue_pair()
2280 struct virtnet_info *vi = netdev_priv(dev); in virtnet_open() local
2283 enable_delayed_refill(vi); in virtnet_open()
2285 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_open()
2286 if (i < vi->curr_queue_pairs) in virtnet_open()
2288 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) in virtnet_open()
2289 schedule_delayed_work(&vi->refill, 0); in virtnet_open()
2291 err = virtnet_enable_queue_pair(vi, i); in virtnet_open()
2299 disable_delayed_refill(vi); in virtnet_open()
2300 cancel_delayed_work_sync(&vi->refill); in virtnet_open()
2303 virtnet_disable_queue_pair(vi, i); in virtnet_open()
2310 struct virtnet_info *vi = sq->vq->vdev->priv; in virtnet_poll_tx() local
2316 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { in virtnet_poll_tx()
2322 txq = netdev_get_tx_queue(vi->dev, index); in virtnet_poll_tx()
2357 struct virtnet_info *vi = sq->vq->vdev->priv; in xmit_skb() local
2359 unsigned hdr_len = vi->hdr_len; in xmit_skb()
2362 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); in xmit_skb()
2364 can_push = vi->any_header_sg && in xmit_skb()
2375 virtio_is_little_endian(vi->vdev), false, in xmit_skb()
2379 if (vi->mergeable_rx_bufs) in xmit_skb()
2402 struct virtnet_info *vi = netdev_priv(dev); in start_xmit() local
2404 struct send_queue *sq = &vi->sq[qnum]; in start_xmit()
2444 check_sq_full_and_disable(vi, dev, sq); in start_xmit()
2457 static int virtnet_rx_resize(struct virtnet_info *vi, in virtnet_rx_resize() argument
2460 bool running = netif_running(vi->dev); in virtnet_rx_resize()
2463 qindex = rq - vi->rq; in virtnet_rx_resize()
2470 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err); in virtnet_rx_resize()
2472 if (!try_fill_recv(vi, rq, GFP_KERNEL)) in virtnet_rx_resize()
2473 schedule_delayed_work(&vi->refill, 0); in virtnet_rx_resize()
2480 static int virtnet_tx_resize(struct virtnet_info *vi, in virtnet_tx_resize() argument
2483 bool running = netif_running(vi->dev); in virtnet_tx_resize()
2487 qindex = sq - vi->sq; in virtnet_tx_resize()
2492 txq = netdev_get_tx_queue(vi->dev, qindex); in virtnet_tx_resize()
2503 netif_stop_subqueue(vi->dev, qindex); in virtnet_tx_resize()
2509 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err); in virtnet_tx_resize()
2517 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi); in virtnet_tx_resize()
2526 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, in virtnet_send_command() argument
2534 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); in virtnet_send_command()
2536 vi->ctrl->status = ~0; in virtnet_send_command()
2537 vi->ctrl->hdr.class = class; in virtnet_send_command()
2538 vi->ctrl->hdr.cmd = cmd; in virtnet_send_command()
2540 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); in virtnet_send_command()
2547 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); in virtnet_send_command()
2551 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); in virtnet_send_command()
2553 dev_warn(&vi->vdev->dev, in virtnet_send_command()
2558 if (unlikely(!virtqueue_kick(vi->cvq))) in virtnet_send_command()
2559 return vi->ctrl->status == VIRTIO_NET_OK; in virtnet_send_command()
2564 while (!virtqueue_get_buf(vi->cvq, &tmp) && in virtnet_send_command()
2565 !virtqueue_is_broken(vi->cvq)) in virtnet_send_command()
2568 return vi->ctrl->status == VIRTIO_NET_OK; in virtnet_send_command()
2573 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_mac_address() local
2574 struct virtio_device *vdev = vi->vdev; in virtnet_set_mac_address()
2579 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) in virtnet_set_mac_address()
2592 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, in virtnet_set_mac_address()
2621 struct virtnet_info *vi = netdev_priv(dev); in virtnet_stats() local
2625 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_stats()
2627 struct receive_queue *rq = &vi->rq[i]; in virtnet_stats()
2628 struct send_queue *sq = &vi->sq[i]; in virtnet_stats()
2658 static void virtnet_ack_link_announce(struct virtnet_info *vi) in virtnet_ack_link_announce() argument
2661 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, in virtnet_ack_link_announce()
2663 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); in virtnet_ack_link_announce()
2667 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) in _virtnet_set_queues() argument
2670 struct net_device *dev = vi->dev; in _virtnet_set_queues()
2672 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) in _virtnet_set_queues()
2675 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); in _virtnet_set_queues()
2676 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); in _virtnet_set_queues()
2678 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, in _virtnet_set_queues()
2684 vi->curr_queue_pairs = queue_pairs; in _virtnet_set_queues()
2687 schedule_delayed_work(&vi->refill, 0); in _virtnet_set_queues()
2693 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) in virtnet_set_queues() argument
2698 err = _virtnet_set_queues(vi, queue_pairs); in virtnet_set_queues()
2705 struct virtnet_info *vi = netdev_priv(dev); in virtnet_close() local
2709 disable_delayed_refill(vi); in virtnet_close()
2711 cancel_delayed_work_sync(&vi->refill); in virtnet_close()
2713 for (i = 0; i < vi->max_queue_pairs; i++) in virtnet_close()
2714 virtnet_disable_queue_pair(vi, i); in virtnet_close()
2721 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_rx_mode() local
2731 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) in virtnet_set_rx_mode()
2734 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); in virtnet_set_rx_mode()
2735 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); in virtnet_set_rx_mode()
2737 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); in virtnet_set_rx_mode()
2739 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, in virtnet_set_rx_mode()
2742 vi->ctrl->promisc ? "en" : "dis"); in virtnet_set_rx_mode()
2744 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); in virtnet_set_rx_mode()
2746 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, in virtnet_set_rx_mode()
2749 vi->ctrl->allmulti ? "en" : "dis"); in virtnet_set_rx_mode()
2763 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); in virtnet_set_rx_mode()
2774 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); in virtnet_set_rx_mode()
2782 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, in virtnet_set_rx_mode()
2792 struct virtnet_info *vi = netdev_priv(dev); in virtnet_vlan_rx_add_vid() local
2795 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); in virtnet_vlan_rx_add_vid()
2796 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); in virtnet_vlan_rx_add_vid()
2798 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, in virtnet_vlan_rx_add_vid()
2807 struct virtnet_info *vi = netdev_priv(dev); in virtnet_vlan_rx_kill_vid() local
2810 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); in virtnet_vlan_rx_kill_vid()
2811 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); in virtnet_vlan_rx_kill_vid()
2813 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, in virtnet_vlan_rx_kill_vid()
2819 static void virtnet_clean_affinity(struct virtnet_info *vi) in virtnet_clean_affinity() argument
2823 if (vi->affinity_hint_set) { in virtnet_clean_affinity()
2824 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_clean_affinity()
2825 virtqueue_set_affinity(vi->rq[i].vq, NULL); in virtnet_clean_affinity()
2826 virtqueue_set_affinity(vi->sq[i].vq, NULL); in virtnet_clean_affinity()
2829 vi->affinity_hint_set = false; in virtnet_clean_affinity()
2833 static void virtnet_set_affinity(struct virtnet_info *vi) in virtnet_set_affinity() argument
2843 virtnet_clean_affinity(vi); in virtnet_set_affinity()
2848 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); in virtnet_set_affinity()
2849 stragglers = num_cpu >= vi->curr_queue_pairs ? in virtnet_set_affinity()
2850 num_cpu % vi->curr_queue_pairs : in virtnet_set_affinity()
2854 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_set_affinity()
2862 virtqueue_set_affinity(vi->rq[i].vq, mask); in virtnet_set_affinity()
2863 virtqueue_set_affinity(vi->sq[i].vq, mask); in virtnet_set_affinity()
2864 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); in virtnet_set_affinity()
2868 vi->affinity_hint_set = true; in virtnet_set_affinity()
2874 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, in virtnet_cpu_online() local
2876 virtnet_set_affinity(vi); in virtnet_cpu_online()
2882 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, in virtnet_cpu_dead() local
2884 virtnet_set_affinity(vi); in virtnet_cpu_dead()
2890 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, in virtnet_cpu_down_prep() local
2893 virtnet_clean_affinity(vi); in virtnet_cpu_down_prep()
2899 static int virtnet_cpu_notif_add(struct virtnet_info *vi) in virtnet_cpu_notif_add() argument
2903 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); in virtnet_cpu_notif_add()
2907 &vi->node_dead); in virtnet_cpu_notif_add()
2910 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); in virtnet_cpu_notif_add()
2914 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) in virtnet_cpu_notif_remove() argument
2916 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); in virtnet_cpu_notif_remove()
2918 &vi->node_dead); in virtnet_cpu_notif_remove()
2926 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_ringparam() local
2928 ring->rx_max_pending = vi->rq[0].vq->num_max; in virtnet_get_ringparam()
2929 ring->tx_max_pending = vi->sq[0].vq->num_max; in virtnet_get_ringparam()
2930 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); in virtnet_get_ringparam()
2931 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); in virtnet_get_ringparam()
2934 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
2942 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_ringparam() local
2951 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); in virtnet_set_ringparam()
2952 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); in virtnet_set_ringparam()
2958 if (ring->rx_pending > vi->rq[0].vq->num_max) in virtnet_set_ringparam()
2961 if (ring->tx_pending > vi->sq[0].vq->num_max) in virtnet_set_ringparam()
2964 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_set_ringparam()
2965 rq = vi->rq + i; in virtnet_set_ringparam()
2966 sq = vi->sq + i; in virtnet_set_ringparam()
2969 err = virtnet_tx_resize(vi, sq, ring->tx_pending); in virtnet_set_ringparam()
2978 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(i), in virtnet_set_ringparam()
2979 vi->intr_coal_tx.max_usecs, in virtnet_set_ringparam()
2980 vi->intr_coal_tx.max_packets); in virtnet_set_ringparam()
2984 vi->sq[i].intr_coal.max_usecs = vi->intr_coal_tx.max_usecs; in virtnet_set_ringparam()
2985 vi->sq[i].intr_coal.max_packets = vi->intr_coal_tx.max_packets; in virtnet_set_ringparam()
2989 err = virtnet_rx_resize(vi, rq, ring->rx_pending); in virtnet_set_ringparam()
2994 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(i), in virtnet_set_ringparam()
2995 vi->intr_coal_rx.max_usecs, in virtnet_set_ringparam()
2996 vi->intr_coal_rx.max_packets); in virtnet_set_ringparam()
3000 vi->rq[i].intr_coal.max_usecs = vi->intr_coal_rx.max_usecs; in virtnet_set_ringparam()
3001 vi->rq[i].intr_coal.max_packets = vi->intr_coal_rx.max_packets; in virtnet_set_ringparam()
3008 static bool virtnet_commit_rss_command(struct virtnet_info *vi) in virtnet_commit_rss_command() argument
3010 struct net_device *dev = vi->dev; in virtnet_commit_rss_command()
3018 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size); in virtnet_commit_rss_command()
3020 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1); in virtnet_commit_rss_command()
3021 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size); in virtnet_commit_rss_command()
3025 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size); in virtnet_commit_rss_command()
3027 sg_buf_size = vi->rss_key_size; in virtnet_commit_rss_command()
3028 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size); in virtnet_commit_rss_command()
3030 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, in virtnet_commit_rss_command()
3031 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG in virtnet_commit_rss_command()
3039 static void virtnet_init_default_rss(struct virtnet_info *vi) in virtnet_init_default_rss() argument
3044 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported; in virtnet_init_default_rss()
3045 vi->rss_hash_types_saved = vi->rss_hash_types_supported; in virtnet_init_default_rss()
3046 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size in virtnet_init_default_rss()
3047 ? vi->rss_indir_table_size - 1 : 0; in virtnet_init_default_rss()
3048 vi->ctrl->rss.unclassified_queue = 0; in virtnet_init_default_rss()
3050 for (; i < vi->rss_indir_table_size; ++i) { in virtnet_init_default_rss()
3051 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs); in virtnet_init_default_rss()
3052 vi->ctrl->rss.indirection_table[i] = indir_val; in virtnet_init_default_rss()
3055 vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0; in virtnet_init_default_rss()
3056 vi->ctrl->rss.hash_key_length = vi->rss_key_size; in virtnet_init_default_rss()
3058 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size); in virtnet_init_default_rss()
3061 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info) in virtnet_get_hashflow() argument
3066 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { in virtnet_get_hashflow()
3069 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { in virtnet_get_hashflow()
3074 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { in virtnet_get_hashflow()
3077 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { in virtnet_get_hashflow()
3082 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { in virtnet_get_hashflow()
3085 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { in virtnet_get_hashflow()
3090 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { in virtnet_get_hashflow()
3093 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { in virtnet_get_hashflow()
3098 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) in virtnet_get_hashflow()
3103 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) in virtnet_get_hashflow()
3113 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info) in virtnet_set_hashflow() argument
3115 u32 new_hashtypes = vi->rss_hash_types_saved; in virtnet_set_hashflow()
3164 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported)) in virtnet_set_hashflow()
3167 if (new_hashtypes != vi->rss_hash_types_saved) { in virtnet_set_hashflow()
3168 vi->rss_hash_types_saved = new_hashtypes; in virtnet_set_hashflow()
3169 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; in virtnet_set_hashflow()
3170 if (vi->dev->features & NETIF_F_RXHASH) in virtnet_set_hashflow()
3171 return virtnet_commit_rss_command(vi); in virtnet_set_hashflow()
3180 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_drvinfo() local
3181 struct virtio_device *vdev = vi->vdev; in virtnet_get_drvinfo()
3193 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_channels() local
3203 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) in virtnet_set_channels()
3210 if (vi->rq[0].xdp_prog) in virtnet_set_channels()
3214 err = _virtnet_set_queues(vi, queue_pairs); in virtnet_set_channels()
3219 virtnet_set_affinity(vi); in virtnet_set_channels()
3230 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_strings() local
3236 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_strings()
3242 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_strings()
3253 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_sset_count() local
3257 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + in virtnet_get_sset_count()
3267 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_ethtool_stats() local
3273 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_ethtool_stats()
3274 struct receive_queue *rq = &vi->rq[i]; in virtnet_get_ethtool_stats()
3288 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_ethtool_stats()
3289 struct send_queue *sq = &vi->sq[i]; in virtnet_get_ethtool_stats()
3307 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_channels() local
3309 channels->combined_count = vi->curr_queue_pairs; in virtnet_get_channels()
3310 channels->max_combined = vi->max_queue_pairs; in virtnet_get_channels()
3320 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_link_ksettings() local
3323 &vi->speed, &vi->duplex); in virtnet_set_link_ksettings()
3329 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_link_ksettings() local
3331 cmd->base.speed = vi->speed; in virtnet_get_link_ksettings()
3332 cmd->base.duplex = vi->duplex; in virtnet_get_link_ksettings()
3338 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi, in virtnet_send_notf_coal_cmds() argument
3344 vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs); in virtnet_send_notf_coal_cmds()
3345 vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames); in virtnet_send_notf_coal_cmds()
3346 sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx)); in virtnet_send_notf_coal_cmds()
3348 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, in virtnet_send_notf_coal_cmds()
3354 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3355 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3356 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_send_notf_coal_cmds()
3357 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3358 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3361 vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); in virtnet_send_notf_coal_cmds()
3362 vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); in virtnet_send_notf_coal_cmds()
3363 sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx)); in virtnet_send_notf_coal_cmds()
3365 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, in virtnet_send_notf_coal_cmds()
3371 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3372 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3373 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_send_notf_coal_cmds()
3374 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3375 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3381 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi, in virtnet_send_ctrl_coal_vq_cmd() argument
3386 vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn); in virtnet_send_ctrl_coal_vq_cmd()
3387 vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs); in virtnet_send_ctrl_coal_vq_cmd()
3388 vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets); in virtnet_send_ctrl_coal_vq_cmd()
3389 sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq)); in virtnet_send_ctrl_coal_vq_cmd()
3391 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, in virtnet_send_ctrl_coal_vq_cmd()
3399 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, in virtnet_send_notf_coal_vq_cmds() argument
3405 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue), in virtnet_send_notf_coal_vq_cmds()
3411 vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs; in virtnet_send_notf_coal_vq_cmds()
3412 vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames; in virtnet_send_notf_coal_vq_cmds()
3414 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue), in virtnet_send_notf_coal_vq_cmds()
3420 vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs; in virtnet_send_notf_coal_vq_cmds()
3421 vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames; in virtnet_send_notf_coal_vq_cmds()
3458 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_coalesce() local
3464 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) { in virtnet_set_coalesce()
3466 vi->sq[queue_number].napi.weight, in virtnet_set_coalesce()
3472 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be in virtnet_set_coalesce()
3479 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) in virtnet_set_coalesce()
3480 ret = virtnet_send_notf_coal_cmds(vi, ec); in virtnet_set_coalesce()
3488 for (; queue_number < vi->max_queue_pairs; queue_number++) in virtnet_set_coalesce()
3489 vi->sq[queue_number].napi.weight = napi_weight; in virtnet_set_coalesce()
3500 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_coalesce() local
3502 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { in virtnet_get_coalesce()
3503 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs; in virtnet_get_coalesce()
3504 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs; in virtnet_get_coalesce()
3505 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets; in virtnet_get_coalesce()
3506 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets; in virtnet_get_coalesce()
3510 if (vi->sq[0].napi.weight) in virtnet_get_coalesce()
3521 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_per_queue_coalesce() local
3525 if (queue >= vi->max_queue_pairs) in virtnet_set_per_queue_coalesce()
3531 vi->sq[queue].napi.weight, in virtnet_set_per_queue_coalesce()
3536 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) in virtnet_set_per_queue_coalesce()
3537 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue); in virtnet_set_per_queue_coalesce()
3545 vi->sq[queue].napi.weight = napi_weight; in virtnet_set_per_queue_coalesce()
3554 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_per_queue_coalesce() local
3556 if (queue >= vi->max_queue_pairs) in virtnet_get_per_queue_coalesce()
3559 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { in virtnet_get_per_queue_coalesce()
3560 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs; in virtnet_get_per_queue_coalesce()
3561 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs; in virtnet_get_per_queue_coalesce()
3562 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets; in virtnet_get_per_queue_coalesce()
3563 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets; in virtnet_get_per_queue_coalesce()
3567 if (vi->sq[queue].napi.weight) in virtnet_get_per_queue_coalesce()
3576 struct virtnet_info *vi = netdev_priv(dev); in virtnet_init_settings() local
3578 vi->speed = SPEED_UNKNOWN; in virtnet_init_settings()
3579 vi->duplex = DUPLEX_UNKNOWN; in virtnet_init_settings()
3582 static void virtnet_update_settings(struct virtnet_info *vi) in virtnet_update_settings() argument
3587 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) in virtnet_update_settings()
3590 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); in virtnet_update_settings()
3593 vi->speed = speed; in virtnet_update_settings()
3595 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); in virtnet_update_settings()
3598 vi->duplex = duplex; in virtnet_update_settings()
3613 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_rxfh() local
3617 for (i = 0; i < vi->rss_indir_table_size; ++i) in virtnet_get_rxfh()
3618 indir[i] = vi->ctrl->rss.indirection_table[i]; in virtnet_get_rxfh()
3622 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size); in virtnet_get_rxfh()
3632 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_rxfh() local
3640 if (!vi->has_rss) in virtnet_set_rxfh()
3643 for (i = 0; i < vi->rss_indir_table_size; ++i) in virtnet_set_rxfh()
3644 vi->ctrl->rss.indirection_table[i] = indir[i]; in virtnet_set_rxfh()
3652 if (!vi->has_rss && !vi->has_rss_hash_report) in virtnet_set_rxfh()
3655 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size); in virtnet_set_rxfh()
3660 virtnet_commit_rss_command(vi); in virtnet_set_rxfh()
3667 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_rxnfc() local
3672 info->data = vi->curr_queue_pairs; in virtnet_get_rxnfc()
3675 virtnet_get_hashflow(vi, info); in virtnet_get_rxnfc()
3686 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_rxnfc() local
3691 if (!virtnet_set_hashflow(vi, info)) in virtnet_set_rxnfc()
3731 struct virtnet_info *vi = vdev->priv; in virtnet_freeze_down() local
3734 flush_work(&vi->config_work); in virtnet_freeze_down()
3736 netif_tx_lock_bh(vi->dev); in virtnet_freeze_down()
3737 netif_device_detach(vi->dev); in virtnet_freeze_down()
3738 netif_tx_unlock_bh(vi->dev); in virtnet_freeze_down()
3739 if (netif_running(vi->dev)) in virtnet_freeze_down()
3740 virtnet_close(vi->dev); in virtnet_freeze_down()
3743 static int init_vqs(struct virtnet_info *vi);
3747 struct virtnet_info *vi = vdev->priv; in virtnet_restore_up() local
3750 err = init_vqs(vi); in virtnet_restore_up()
3756 enable_delayed_refill(vi); in virtnet_restore_up()
3758 if (netif_running(vi->dev)) { in virtnet_restore_up()
3759 err = virtnet_open(vi->dev); in virtnet_restore_up()
3764 netif_tx_lock_bh(vi->dev); in virtnet_restore_up()
3765 netif_device_attach(vi->dev); in virtnet_restore_up()
3766 netif_tx_unlock_bh(vi->dev); in virtnet_restore_up()
3770 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) in virtnet_set_guest_offloads() argument
3773 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); in virtnet_set_guest_offloads()
3775 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); in virtnet_set_guest_offloads()
3777 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, in virtnet_set_guest_offloads()
3779 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); in virtnet_set_guest_offloads()
3786 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) in virtnet_clear_guest_offloads() argument
3790 if (!vi->guest_offloads) in virtnet_clear_guest_offloads()
3793 return virtnet_set_guest_offloads(vi, offloads); in virtnet_clear_guest_offloads()
3796 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) in virtnet_restore_guest_offloads() argument
3798 u64 offloads = vi->guest_offloads; in virtnet_restore_guest_offloads()
3800 if (!vi->guest_offloads) in virtnet_restore_guest_offloads()
3803 return virtnet_set_guest_offloads(vi, offloads); in virtnet_restore_guest_offloads()
3812 struct virtnet_info *vi = netdev_priv(dev); in virtnet_xdp_set() local
3817 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) in virtnet_xdp_set()
3818 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || in virtnet_xdp_set()
3819 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || in virtnet_xdp_set()
3820 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || in virtnet_xdp_set()
3821 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || in virtnet_xdp_set()
3822 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) || in virtnet_xdp_set()
3823 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) || in virtnet_xdp_set()
3824 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) { in virtnet_xdp_set()
3829 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { in virtnet_xdp_set()
3840 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; in virtnet_xdp_set()
3845 if (curr_qp + xdp_qp > vi->max_queue_pairs) { in virtnet_xdp_set()
3847 curr_qp + xdp_qp, vi->max_queue_pairs); in virtnet_xdp_set()
3851 old_prog = rtnl_dereference(vi->rq[0].xdp_prog); in virtnet_xdp_set()
3856 bpf_prog_add(prog, vi->max_queue_pairs - 1); in virtnet_xdp_set()
3860 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3861 napi_disable(&vi->rq[i].napi); in virtnet_xdp_set()
3862 virtnet_napi_tx_disable(&vi->sq[i].napi); in virtnet_xdp_set()
3867 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3868 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); in virtnet_xdp_set()
3870 virtnet_restore_guest_offloads(vi); in virtnet_xdp_set()
3875 err = _virtnet_set_queues(vi, curr_qp + xdp_qp); in virtnet_xdp_set()
3879 vi->xdp_queue_pairs = xdp_qp; in virtnet_xdp_set()
3882 vi->xdp_enabled = true; in virtnet_xdp_set()
3883 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3884 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); in virtnet_xdp_set()
3886 virtnet_clear_guest_offloads(vi); in virtnet_xdp_set()
3892 vi->xdp_enabled = false; in virtnet_xdp_set()
3895 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3899 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); in virtnet_xdp_set()
3900 virtnet_napi_tx_enable(vi, vi->sq[i].vq, in virtnet_xdp_set()
3901 &vi->sq[i].napi); in virtnet_xdp_set()
3909 virtnet_clear_guest_offloads(vi); in virtnet_xdp_set()
3910 for (i = 0; i < vi->max_queue_pairs; i++) in virtnet_xdp_set()
3911 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); in virtnet_xdp_set()
3915 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3916 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); in virtnet_xdp_set()
3917 virtnet_napi_tx_enable(vi, vi->sq[i].vq, in virtnet_xdp_set()
3918 &vi->sq[i].napi); in virtnet_xdp_set()
3922 bpf_prog_sub(prog, vi->max_queue_pairs - 1); in virtnet_xdp_set()
3939 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_phys_port_name() local
3942 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) in virtnet_get_phys_port_name()
3955 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_features() local
3960 if (vi->xdp_enabled) in virtnet_set_features()
3964 offloads = vi->guest_offloads_capable; in virtnet_set_features()
3966 offloads = vi->guest_offloads_capable & in virtnet_set_features()
3969 err = virtnet_set_guest_offloads(vi, offloads); in virtnet_set_features()
3972 vi->guest_offloads = offloads; in virtnet_set_features()
3977 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; in virtnet_set_features()
3979 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE; in virtnet_set_features()
3981 if (!virtnet_commit_rss_command(vi)) in virtnet_set_features()
4023 struct virtnet_info *vi = in virtnet_config_changed_work() local
4027 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, in virtnet_config_changed_work()
4032 netdev_notify_peers(vi->dev); in virtnet_config_changed_work()
4033 virtnet_ack_link_announce(vi); in virtnet_config_changed_work()
4039 if (vi->status == v) in virtnet_config_changed_work()
4042 vi->status = v; in virtnet_config_changed_work()
4044 if (vi->status & VIRTIO_NET_S_LINK_UP) { in virtnet_config_changed_work()
4045 virtnet_update_settings(vi); in virtnet_config_changed_work()
4046 netif_carrier_on(vi->dev); in virtnet_config_changed_work()
4047 netif_tx_wake_all_queues(vi->dev); in virtnet_config_changed_work()
4049 netif_carrier_off(vi->dev); in virtnet_config_changed_work()
4050 netif_tx_stop_all_queues(vi->dev); in virtnet_config_changed_work()
4056 struct virtnet_info *vi = vdev->priv; in virtnet_config_changed() local
4058 schedule_work(&vi->config_work); in virtnet_config_changed()
4061 static void virtnet_free_queues(struct virtnet_info *vi) in virtnet_free_queues() argument
4065 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_free_queues()
4066 __netif_napi_del(&vi->rq[i].napi); in virtnet_free_queues()
4067 __netif_napi_del(&vi->sq[i].napi); in virtnet_free_queues()
4071 * we need to respect an RCU grace period before freeing vi->rq in virtnet_free_queues()
4075 kfree(vi->rq); in virtnet_free_queues()
4076 kfree(vi->sq); in virtnet_free_queues()
4077 kfree(vi->ctrl); in virtnet_free_queues()
4080 static void _free_receive_bufs(struct virtnet_info *vi) in _free_receive_bufs() argument
4085 for (i = 0; i < vi->max_queue_pairs; i++) { in _free_receive_bufs()
4086 while (vi->rq[i].pages) in _free_receive_bufs()
4087 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); in _free_receive_bufs()
4089 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); in _free_receive_bufs()
4090 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); in _free_receive_bufs()
4096 static void free_receive_bufs(struct virtnet_info *vi) in free_receive_bufs() argument
4099 _free_receive_bufs(vi); in free_receive_bufs()
4103 static void free_receive_page_frags(struct virtnet_info *vi) in free_receive_page_frags() argument
4106 for (i = 0; i < vi->max_queue_pairs; i++) in free_receive_page_frags()
4107 if (vi->rq[i].alloc_frag.page) { in free_receive_page_frags()
4108 if (vi->rq[i].do_dma && vi->rq[i].last_dma) in free_receive_page_frags()
4109 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0); in free_receive_page_frags()
4110 put_page(vi->rq[i].alloc_frag.page); in free_receive_page_frags()
4122 static void free_unused_bufs(struct virtnet_info *vi) in free_unused_bufs() argument
4127 for (i = 0; i < vi->max_queue_pairs; i++) { in free_unused_bufs()
4128 struct virtqueue *vq = vi->sq[i].vq; in free_unused_bufs()
4134 for (i = 0; i < vi->max_queue_pairs; i++) { in free_unused_bufs()
4135 struct virtqueue *vq = vi->rq[i].vq; in free_unused_bufs()
4143 static void virtnet_del_vqs(struct virtnet_info *vi) in virtnet_del_vqs() argument
4145 struct virtio_device *vdev = vi->vdev; in virtnet_del_vqs()
4147 virtnet_clean_affinity(vi); in virtnet_del_vqs()
4151 virtnet_free_queues(vi); in virtnet_del_vqs()
4158 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) in mergeable_min_buf_len() argument
4160 const unsigned int hdr_len = vi->hdr_len; in mergeable_min_buf_len()
4162 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; in mergeable_min_buf_len()
4170 static int virtnet_find_vqs(struct virtnet_info *vi) in virtnet_find_vqs() argument
4184 total_vqs = vi->max_queue_pairs * 2 + in virtnet_find_vqs()
4185 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); in virtnet_find_vqs()
4197 if (!vi->big_packets || vi->mergeable_rx_bufs) { in virtnet_find_vqs()
4206 if (vi->has_cvq) { in virtnet_find_vqs()
4212 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_find_vqs()
4215 sprintf(vi->rq[i].name, "input.%u", i); in virtnet_find_vqs()
4216 sprintf(vi->sq[i].name, "output.%u", i); in virtnet_find_vqs()
4217 names[rxq2vq(i)] = vi->rq[i].name; in virtnet_find_vqs()
4218 names[txq2vq(i)] = vi->sq[i].name; in virtnet_find_vqs()
4223 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, in virtnet_find_vqs()
4228 if (vi->has_cvq) { in virtnet_find_vqs()
4229 vi->cvq = vqs[total_vqs - 1]; in virtnet_find_vqs()
4230 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) in virtnet_find_vqs()
4231 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; in virtnet_find_vqs()
4234 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_find_vqs()
4235 vi->rq[i].vq = vqs[rxq2vq(i)]; in virtnet_find_vqs()
4236 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); in virtnet_find_vqs()
4237 vi->sq[i].vq = vqs[txq2vq(i)]; in virtnet_find_vqs()
4255 static int virtnet_alloc_queues(struct virtnet_info *vi) in virtnet_alloc_queues() argument
4259 if (vi->has_cvq) { in virtnet_alloc_queues()
4260 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); in virtnet_alloc_queues()
4261 if (!vi->ctrl) in virtnet_alloc_queues()
4264 vi->ctrl = NULL; in virtnet_alloc_queues()
4266 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); in virtnet_alloc_queues()
4267 if (!vi->sq) in virtnet_alloc_queues()
4269 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); in virtnet_alloc_queues()
4270 if (!vi->rq) in virtnet_alloc_queues()
4273 INIT_DELAYED_WORK(&vi->refill, refill_work); in virtnet_alloc_queues()
4274 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_alloc_queues()
4275 vi->rq[i].pages = NULL; in virtnet_alloc_queues()
4276 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll, in virtnet_alloc_queues()
4278 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi, in virtnet_alloc_queues()
4282 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); in virtnet_alloc_queues()
4283 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); in virtnet_alloc_queues()
4284 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); in virtnet_alloc_queues()
4286 u64_stats_init(&vi->rq[i].stats.syncp); in virtnet_alloc_queues()
4287 u64_stats_init(&vi->sq[i].stats.syncp); in virtnet_alloc_queues()
4293 kfree(vi->sq); in virtnet_alloc_queues()
4295 kfree(vi->ctrl); in virtnet_alloc_queues()
4300 static int init_vqs(struct virtnet_info *vi) in init_vqs() argument
4305 ret = virtnet_alloc_queues(vi); in init_vqs()
4309 ret = virtnet_find_vqs(vi); in init_vqs()
4313 virtnet_rq_set_premapped(vi); in init_vqs()
4316 virtnet_set_affinity(vi); in init_vqs()
4322 virtnet_free_queues(vi); in init_vqs()
4331 struct virtnet_info *vi = netdev_priv(queue->dev); in mergeable_rx_buffer_size_show() local
4333 unsigned int headroom = virtnet_get_headroom(vi); in mergeable_rx_buffer_size_show()
4337 BUG_ON(queue_index >= vi->max_queue_pairs); in mergeable_rx_buffer_size_show()
4338 avg = &vi->rq[queue_index].mrg_avg_pkt_len; in mergeable_rx_buffer_size_show()
4340 get_mergeable_buf_len(&vi->rq[queue_index], avg, in mergeable_rx_buffer_size_show()
4431 static bool virtnet_check_guest_gso(const struct virtnet_info *vi) in virtnet_check_guest_gso() argument
4433 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || in virtnet_check_guest_gso()
4434 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || in virtnet_check_guest_gso()
4435 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || in virtnet_check_guest_gso()
4436 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || in virtnet_check_guest_gso()
4437 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) && in virtnet_check_guest_gso()
4438 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6)); in virtnet_check_guest_gso()
4441 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) in virtnet_set_big_packets() argument
4443 bool guest_gso = virtnet_check_guest_gso(vi); in virtnet_set_big_packets()
4450 vi->big_packets = true; in virtnet_set_big_packets()
4451 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE); in virtnet_set_big_packets()
4459 struct virtnet_info *vi; in virtnet_probe() local
4554 vi = netdev_priv(dev); in virtnet_probe()
4555 vi->dev = dev; in virtnet_probe()
4556 vi->vdev = vdev; in virtnet_probe()
4557 vdev->priv = vi; in virtnet_probe()
4559 INIT_WORK(&vi->config_work, virtnet_config_changed_work); in virtnet_probe()
4560 spin_lock_init(&vi->refill_lock); in virtnet_probe()
4563 vi->mergeable_rx_bufs = true; in virtnet_probe()
4567 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { in virtnet_probe()
4568 vi->intr_coal_rx.max_usecs = 0; in virtnet_probe()
4569 vi->intr_coal_tx.max_usecs = 0; in virtnet_probe()
4570 vi->intr_coal_tx.max_packets = 0; in virtnet_probe()
4571 vi->intr_coal_rx.max_packets = 0; in virtnet_probe()
4575 vi->has_rss_hash_report = true; in virtnet_probe()
4578 vi->has_rss = true; in virtnet_probe()
4580 vi->rss_indir_table_size = in virtnet_probe()
4585 if (vi->has_rss || vi->has_rss_hash_report) { in virtnet_probe()
4586 vi->rss_key_size = in virtnet_probe()
4588 if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) { in virtnet_probe()
4590 vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE); in virtnet_probe()
4595 vi->rss_hash_types_supported = in virtnet_probe()
4597 vi->rss_hash_types_supported &= in virtnet_probe()
4605 if (vi->has_rss_hash_report) in virtnet_probe()
4606 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); in virtnet_probe()
4609 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); in virtnet_probe()
4611 vi->hdr_len = sizeof(struct virtio_net_hdr); in virtnet_probe()
4615 vi->any_header_sg = true; in virtnet_probe()
4618 vi->has_cvq = true; in virtnet_probe()
4639 virtnet_set_big_packets(vi, mtu); in virtnet_probe()
4641 if (vi->any_header_sg) in virtnet_probe()
4642 dev->needed_headroom = vi->hdr_len; in virtnet_probe()
4646 vi->curr_queue_pairs = max_queue_pairs; in virtnet_probe()
4648 vi->curr_queue_pairs = num_online_cpus(); in virtnet_probe()
4649 vi->max_queue_pairs = max_queue_pairs; in virtnet_probe()
4652 err = init_vqs(vi); in virtnet_probe()
4657 if (vi->mergeable_rx_bufs) in virtnet_probe()
4660 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); in virtnet_probe()
4661 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); in virtnet_probe()
4666 vi->failover = net_failover_create(vi->dev); in virtnet_probe()
4667 if (IS_ERR(vi->failover)) { in virtnet_probe()
4668 err = PTR_ERR(vi->failover); in virtnet_probe()
4673 if (vi->has_rss || vi->has_rss_hash_report) in virtnet_probe()
4674 virtnet_init_default_rss(vi); in virtnet_probe()
4688 _virtnet_set_queues(vi, vi->curr_queue_pairs); in virtnet_probe()
4695 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { in virtnet_probe()
4699 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, in virtnet_probe()
4710 err = virtnet_cpu_notif_add(vi); in virtnet_probe()
4719 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { in virtnet_probe()
4720 schedule_work(&vi->config_work); in virtnet_probe()
4722 vi->status = VIRTIO_NET_S_LINK_UP; in virtnet_probe()
4723 virtnet_update_settings(vi); in virtnet_probe()
4728 if (virtio_has_feature(vi->vdev, guest_offloads[i])) in virtnet_probe()
4729 set_bit(guest_offloads[i], &vi->guest_offloads); in virtnet_probe()
4730 vi->guest_offloads_capable = vi->guest_offloads; in virtnet_probe()
4740 net_failover_destroy(vi->failover); in virtnet_probe()
4743 cancel_delayed_work_sync(&vi->refill); in virtnet_probe()
4744 free_receive_page_frags(vi); in virtnet_probe()
4745 virtnet_del_vqs(vi); in virtnet_probe()
4751 static void remove_vq_common(struct virtnet_info *vi) in remove_vq_common() argument
4753 virtio_reset_device(vi->vdev); in remove_vq_common()
4756 free_unused_bufs(vi); in remove_vq_common()
4758 free_receive_bufs(vi); in remove_vq_common()
4760 free_receive_page_frags(vi); in remove_vq_common()
4762 virtnet_del_vqs(vi); in remove_vq_common()
4767 struct virtnet_info *vi = vdev->priv; in virtnet_remove() local
4769 virtnet_cpu_notif_remove(vi); in virtnet_remove()
4772 flush_work(&vi->config_work); in virtnet_remove()
4774 unregister_netdev(vi->dev); in virtnet_remove()
4776 net_failover_destroy(vi->failover); in virtnet_remove()
4778 remove_vq_common(vi); in virtnet_remove()
4780 free_netdev(vi->dev); in virtnet_remove()
4785 struct virtnet_info *vi = vdev->priv; in virtnet_freeze() local
4787 virtnet_cpu_notif_remove(vi); in virtnet_freeze()
4789 remove_vq_common(vi); in virtnet_freeze()
4796 struct virtnet_info *vi = vdev->priv; in virtnet_restore() local
4802 virtnet_set_queues(vi, vi->curr_queue_pairs); in virtnet_restore()
4804 err = virtnet_cpu_notif_add(vi); in virtnet_restore()
4807 remove_vq_common(vi); in virtnet_restore()