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()
507 static struct sk_buff *page_to_skb(struct virtnet_info *vi, in page_to_skb() argument
523 hdr_len = vi->hdr_len; in page_to_skb()
524 if (vi->mergeable_rx_bufs) in page_to_skb()
566 if (vi->mergeable_rx_bufs) { in page_to_skb()
727 static void virtnet_rq_set_premapped(struct virtnet_info *vi) in virtnet_rq_set_premapped() argument
732 if (!vi->mergeable_rx_bufs && vi->big_packets) in virtnet_rq_set_premapped()
735 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_rq_set_premapped()
736 if (virtqueue_set_dma_premapped(vi->rq[i].vq)) in virtnet_rq_set_premapped()
739 vi->rq[i].do_dma = true; in virtnet_rq_set_premapped()
745 struct virtnet_info *vi = vq->vdev->priv; in virtnet_rq_unmap_free_buf() local
749 rq = &vi->rq[i]; in virtnet_rq_unmap_free_buf()
754 virtnet_rq_free_buf(vi, rq, buf); in virtnet_rq_unmap_free_buf()
793 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q) in is_xdp_raw_buffer_queue() argument
795 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs)) in is_xdp_raw_buffer_queue()
797 else if (q < vi->curr_queue_pairs) in is_xdp_raw_buffer_queue()
803 static void check_sq_full_and_disable(struct virtnet_info *vi, in check_sq_full_and_disable() argument
810 qnum = sq - vi->sq; in check_sq_full_and_disable()
838 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, in __virtnet_xdp_xmit_one() argument
847 if (unlikely(xdpf->headroom < vi->hdr_len)) in __virtnet_xdp_xmit_one()
862 xdpf->headroom -= vi->hdr_len; in __virtnet_xdp_xmit_one()
863 xdpf->data -= vi->hdr_len; in __virtnet_xdp_xmit_one()
866 memset(hdr, 0, vi->hdr_len); in __virtnet_xdp_xmit_one()
867 xdpf->len += vi->hdr_len; in __virtnet_xdp_xmit_one()
886 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
894 #define virtnet_xdp_get_sq(vi) ({ \ argument
897 typeof(vi) v = (vi); \
913 #define virtnet_xdp_put_sq(vi, q) { \ argument
915 typeof(vi) v = (vi); \
927 struct virtnet_info *vi = netdev_priv(dev); in virtnet_xdp_xmit() local
928 struct receive_queue *rq = vi->rq; in virtnet_xdp_xmit()
947 sq = virtnet_xdp_get_sq(vi); in virtnet_xdp_xmit()
973 if (__virtnet_xdp_xmit_one(vi, sq, xdpf)) in virtnet_xdp_xmit()
979 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq)) in virtnet_xdp_xmit()
980 check_sq_full_and_disable(vi, dev, sq); in virtnet_xdp_xmit()
995 virtnet_xdp_put_sq(vi, sq); in virtnet_xdp_xmit()
1068 static unsigned int virtnet_get_headroom(struct virtnet_info *vi) in virtnet_get_headroom() argument
1070 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; in virtnet_get_headroom()
1141 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi, in receive_small_build_skb() argument
1152 headroom = vi->hdr_len + header_offset; in receive_small_build_skb()
1161 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len); in receive_small_build_skb()
1167 struct virtnet_info *vi, in receive_small_xdp() argument
1177 unsigned int headroom = vi->hdr_len + header_offset; in receive_small_xdp()
1197 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) { in receive_small_xdp()
1199 unsigned int tlen = len + vi->hdr_len; in receive_small_xdp()
1202 xdp_headroom = virtnet_get_headroom(vi); in receive_small_xdp()
1204 headroom = vi->hdr_len + header_offset; in receive_small_xdp()
1219 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len, in receive_small_xdp()
1258 struct virtnet_info *vi, in receive_small() argument
1274 len -= vi->hdr_len; in receive_small()
1284 if (unlikely(vi->xdp_enabled)) { in receive_small()
1290 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf, in receive_small()
1299 skb = receive_small_build_skb(vi, xdp_headroom, buf, len); in receive_small()
1310 struct virtnet_info *vi, in receive_big() argument
1318 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0); in receive_big()
1320 u64_stats_add(&stats->bytes, len - vi->hdr_len); in receive_big()
1362 struct virtnet_info *vi, in build_skb_from_xdp_buff() argument
1405 struct virtnet_info *vi, in virtnet_build_xdp_buff_mrg() argument
1427 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true); in virtnet_build_xdp_buff_mrg()
1453 virtio16_to_cpu(vi->vdev, hdr->num_buffers)); in virtnet_build_xdp_buff_mrg()
1493 static void *mergeable_xdp_get_buf(struct virtnet_info *vi, in mergeable_xdp_get_buf() argument
1527 if (likely(headroom >= virtnet_get_headroom(vi) && in mergeable_xdp_get_buf()
1572 struct virtnet_info *vi, in receive_mergeable_xdp() argument
1582 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); in receive_mergeable_xdp()
1593 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page, in receive_mergeable_xdp()
1598 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz, in receive_mergeable_xdp()
1607 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz); in receive_mergeable_xdp()
1632 struct virtnet_info *vi, in receive_mergeable() argument
1641 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); in receive_mergeable()
1651 u64_stats_add(&stats->bytes, len - vi->hdr_len); in receive_mergeable()
1660 if (unlikely(vi->xdp_enabled)) { in receive_mergeable()
1666 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx, in receive_mergeable()
1674 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom); in receive_mergeable()
1686 virtio16_to_cpu(vi->vdev, in receive_mergeable()
1778 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, in receive_buf() argument
1783 struct net_device *dev = vi->dev; in receive_buf()
1788 if (unlikely(len < vi->hdr_len + ETH_HLEN)) { in receive_buf()
1791 virtnet_rq_free_buf(vi, rq, buf); in receive_buf()
1804 if (vi->mergeable_rx_bufs) in receive_buf()
1805 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit, in receive_buf()
1807 else if (vi->big_packets) in receive_buf()
1808 skb = receive_big(dev, vi, rq, buf, len, stats); in receive_buf()
1810 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats); in receive_buf()
1816 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report) in receive_buf()
1823 virtio_is_little_endian(vi->vdev))) { in receive_buf()
1848 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, in add_recvbuf_small() argument
1852 unsigned int xdp_headroom = virtnet_get_headroom(vi); in add_recvbuf_small()
1854 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom; in add_recvbuf_small()
1869 virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN); in add_recvbuf_small()
1881 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, in add_recvbuf_big() argument
1888 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2); in add_recvbuf_big()
1890 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */ in add_recvbuf_big()
1891 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) { in add_recvbuf_big()
1914 sg_set_buf(&rq->sg[0], p, vi->hdr_len); in add_recvbuf_big()
1922 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2, in add_recvbuf_big()
1934 struct virtnet_info *vi = rq->vq->vdev->priv; in get_mergeable_buf_len() local
1935 const size_t hdr_len = vi->hdr_len; in get_mergeable_buf_len()
1947 static int add_recvbuf_mergeable(struct virtnet_info *vi, in add_recvbuf_mergeable() argument
1951 unsigned int headroom = virtnet_get_headroom(vi); in add_recvbuf_mergeable()
2009 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, in try_fill_recv() argument
2016 if (vi->mergeable_rx_bufs) in try_fill_recv()
2017 err = add_recvbuf_mergeable(vi, rq, gfp); in try_fill_recv()
2018 else if (vi->big_packets) in try_fill_recv()
2019 err = add_recvbuf_big(vi, rq, gfp); in try_fill_recv()
2021 err = add_recvbuf_small(vi, rq, gfp); in try_fill_recv()
2040 struct virtnet_info *vi = rvq->vdev->priv; in skb_recv_done() local
2041 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; in skb_recv_done()
2059 static void virtnet_napi_tx_enable(struct virtnet_info *vi, in virtnet_napi_tx_enable() argument
2069 if (!vi->affinity_hint_set) { in virtnet_napi_tx_enable()
2085 struct virtnet_info *vi = in refill_work() local
2090 for (i = 0; i < vi->curr_queue_pairs; i++) { in refill_work()
2091 struct receive_queue *rq = &vi->rq[i]; in refill_work()
2094 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); in refill_work()
2101 schedule_delayed_work(&vi->refill, HZ/2); in refill_work()
2108 struct virtnet_info *vi = rq->vq->vdev->priv; in virtnet_receive() local
2115 if (!vi->big_packets || vi->mergeable_rx_bufs) { in virtnet_receive()
2120 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats); in virtnet_receive()
2126 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats); in virtnet_receive()
2132 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { in virtnet_receive()
2133 spin_lock(&vi->refill_lock); in virtnet_receive()
2134 if (vi->refill_enabled) in virtnet_receive()
2135 schedule_delayed_work(&vi->refill, 0); in virtnet_receive()
2136 spin_unlock(&vi->refill_lock); in virtnet_receive()
2157 struct virtnet_info *vi = rq->vq->vdev->priv; in virtnet_poll_cleantx() local
2159 struct send_queue *sq = &vi->sq[index]; in virtnet_poll_cleantx()
2160 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index); in virtnet_poll_cleantx()
2162 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index)) in virtnet_poll_cleantx()
2187 struct virtnet_info *vi = rq->vq->vdev->priv; in virtnet_poll() local
2204 sq = virtnet_xdp_get_sq(vi); in virtnet_poll()
2210 virtnet_xdp_put_sq(vi, sq); in virtnet_poll()
2216 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index) in virtnet_disable_queue_pair() argument
2218 virtnet_napi_tx_disable(&vi->sq[qp_index].napi); in virtnet_disable_queue_pair()
2219 napi_disable(&vi->rq[qp_index].napi); in virtnet_disable_queue_pair()
2220 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); in virtnet_disable_queue_pair()
2223 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index) in virtnet_enable_queue_pair() argument
2225 struct net_device *dev = vi->dev; in virtnet_enable_queue_pair()
2228 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index, in virtnet_enable_queue_pair()
2229 vi->rq[qp_index].napi.napi_id); in virtnet_enable_queue_pair()
2233 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq, in virtnet_enable_queue_pair()
2238 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi); in virtnet_enable_queue_pair()
2239 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); in virtnet_enable_queue_pair()
2244 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); in virtnet_enable_queue_pair()
2250 struct virtnet_info *vi = netdev_priv(dev); in virtnet_open() local
2253 enable_delayed_refill(vi); in virtnet_open()
2255 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_open()
2256 if (i < vi->curr_queue_pairs) in virtnet_open()
2258 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) in virtnet_open()
2259 schedule_delayed_work(&vi->refill, 0); in virtnet_open()
2261 err = virtnet_enable_queue_pair(vi, i); in virtnet_open()
2269 disable_delayed_refill(vi); in virtnet_open()
2270 cancel_delayed_work_sync(&vi->refill); in virtnet_open()
2273 virtnet_disable_queue_pair(vi, i); in virtnet_open()
2280 struct virtnet_info *vi = sq->vq->vdev->priv; in virtnet_poll_tx() local
2286 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) { in virtnet_poll_tx()
2292 txq = netdev_get_tx_queue(vi->dev, index); in virtnet_poll_tx()
2327 struct virtnet_info *vi = sq->vq->vdev->priv; in xmit_skb() local
2329 unsigned hdr_len = vi->hdr_len; in xmit_skb()
2332 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); in xmit_skb()
2334 can_push = vi->any_header_sg && in xmit_skb()
2345 virtio_is_little_endian(vi->vdev), false, in xmit_skb()
2349 if (vi->mergeable_rx_bufs) in xmit_skb()
2372 struct virtnet_info *vi = netdev_priv(dev); in start_xmit() local
2374 struct send_queue *sq = &vi->sq[qnum]; in start_xmit()
2414 check_sq_full_and_disable(vi, dev, sq); in start_xmit()
2427 static int virtnet_rx_resize(struct virtnet_info *vi, in virtnet_rx_resize() argument
2430 bool running = netif_running(vi->dev); in virtnet_rx_resize()
2433 qindex = rq - vi->rq; in virtnet_rx_resize()
2440 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err); in virtnet_rx_resize()
2442 if (!try_fill_recv(vi, rq, GFP_KERNEL)) in virtnet_rx_resize()
2443 schedule_delayed_work(&vi->refill, 0); in virtnet_rx_resize()
2450 static int virtnet_tx_resize(struct virtnet_info *vi, in virtnet_tx_resize() argument
2453 bool running = netif_running(vi->dev); in virtnet_tx_resize()
2457 qindex = sq - vi->sq; in virtnet_tx_resize()
2462 txq = netdev_get_tx_queue(vi->dev, qindex); in virtnet_tx_resize()
2473 netif_stop_subqueue(vi->dev, qindex); in virtnet_tx_resize()
2479 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err); in virtnet_tx_resize()
2487 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi); in virtnet_tx_resize()
2496 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, in virtnet_send_command() argument
2504 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); in virtnet_send_command()
2506 vi->ctrl->status = ~0; in virtnet_send_command()
2507 vi->ctrl->hdr.class = class; in virtnet_send_command()
2508 vi->ctrl->hdr.cmd = cmd; in virtnet_send_command()
2510 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); in virtnet_send_command()
2517 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); in virtnet_send_command()
2521 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); in virtnet_send_command()
2523 dev_warn(&vi->vdev->dev, in virtnet_send_command()
2528 if (unlikely(!virtqueue_kick(vi->cvq))) in virtnet_send_command()
2529 return vi->ctrl->status == VIRTIO_NET_OK; in virtnet_send_command()
2534 while (!virtqueue_get_buf(vi->cvq, &tmp) && in virtnet_send_command()
2535 !virtqueue_is_broken(vi->cvq)) in virtnet_send_command()
2538 return vi->ctrl->status == VIRTIO_NET_OK; in virtnet_send_command()
2543 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_mac_address() local
2544 struct virtio_device *vdev = vi->vdev; in virtnet_set_mac_address()
2549 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) in virtnet_set_mac_address()
2562 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, in virtnet_set_mac_address()
2591 struct virtnet_info *vi = netdev_priv(dev); in virtnet_stats() local
2595 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_stats()
2597 struct receive_queue *rq = &vi->rq[i]; in virtnet_stats()
2598 struct send_queue *sq = &vi->sq[i]; in virtnet_stats()
2628 static void virtnet_ack_link_announce(struct virtnet_info *vi) in virtnet_ack_link_announce() argument
2631 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, in virtnet_ack_link_announce()
2633 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); in virtnet_ack_link_announce()
2637 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) in _virtnet_set_queues() argument
2640 struct net_device *dev = vi->dev; in _virtnet_set_queues()
2642 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) in _virtnet_set_queues()
2645 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); in _virtnet_set_queues()
2646 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq)); in _virtnet_set_queues()
2648 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, in _virtnet_set_queues()
2654 vi->curr_queue_pairs = queue_pairs; in _virtnet_set_queues()
2657 schedule_delayed_work(&vi->refill, 0); in _virtnet_set_queues()
2663 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) in virtnet_set_queues() argument
2668 err = _virtnet_set_queues(vi, queue_pairs); in virtnet_set_queues()
2675 struct virtnet_info *vi = netdev_priv(dev); in virtnet_close() local
2679 disable_delayed_refill(vi); in virtnet_close()
2681 cancel_delayed_work_sync(&vi->refill); in virtnet_close()
2683 for (i = 0; i < vi->max_queue_pairs; i++) in virtnet_close()
2684 virtnet_disable_queue_pair(vi, i); in virtnet_close()
2691 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_rx_mode() local
2701 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) in virtnet_set_rx_mode()
2704 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0); in virtnet_set_rx_mode()
2705 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0); in virtnet_set_rx_mode()
2707 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc)); in virtnet_set_rx_mode()
2709 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, in virtnet_set_rx_mode()
2712 vi->ctrl->promisc ? "en" : "dis"); in virtnet_set_rx_mode()
2714 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti)); in virtnet_set_rx_mode()
2716 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, in virtnet_set_rx_mode()
2719 vi->ctrl->allmulti ? "en" : "dis"); in virtnet_set_rx_mode()
2733 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); in virtnet_set_rx_mode()
2744 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); in virtnet_set_rx_mode()
2752 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, in virtnet_set_rx_mode()
2762 struct virtnet_info *vi = netdev_priv(dev); in virtnet_vlan_rx_add_vid() local
2765 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); in virtnet_vlan_rx_add_vid()
2766 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); in virtnet_vlan_rx_add_vid()
2768 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, in virtnet_vlan_rx_add_vid()
2777 struct virtnet_info *vi = netdev_priv(dev); in virtnet_vlan_rx_kill_vid() local
2780 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid); in virtnet_vlan_rx_kill_vid()
2781 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid)); in virtnet_vlan_rx_kill_vid()
2783 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, in virtnet_vlan_rx_kill_vid()
2789 static void virtnet_clean_affinity(struct virtnet_info *vi) in virtnet_clean_affinity() argument
2793 if (vi->affinity_hint_set) { in virtnet_clean_affinity()
2794 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_clean_affinity()
2795 virtqueue_set_affinity(vi->rq[i].vq, NULL); in virtnet_clean_affinity()
2796 virtqueue_set_affinity(vi->sq[i].vq, NULL); in virtnet_clean_affinity()
2799 vi->affinity_hint_set = false; in virtnet_clean_affinity()
2803 static void virtnet_set_affinity(struct virtnet_info *vi) in virtnet_set_affinity() argument
2813 virtnet_clean_affinity(vi); in virtnet_set_affinity()
2818 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1); in virtnet_set_affinity()
2819 stragglers = num_cpu >= vi->curr_queue_pairs ? in virtnet_set_affinity()
2820 num_cpu % vi->curr_queue_pairs : in virtnet_set_affinity()
2824 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_set_affinity()
2832 virtqueue_set_affinity(vi->rq[i].vq, mask); in virtnet_set_affinity()
2833 virtqueue_set_affinity(vi->sq[i].vq, mask); in virtnet_set_affinity()
2834 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS); in virtnet_set_affinity()
2838 vi->affinity_hint_set = true; in virtnet_set_affinity()
2844 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, in virtnet_cpu_online() local
2846 virtnet_set_affinity(vi); in virtnet_cpu_online()
2852 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, in virtnet_cpu_dead() local
2854 virtnet_set_affinity(vi); in virtnet_cpu_dead()
2860 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, in virtnet_cpu_down_prep() local
2863 virtnet_clean_affinity(vi); in virtnet_cpu_down_prep()
2869 static int virtnet_cpu_notif_add(struct virtnet_info *vi) in virtnet_cpu_notif_add() argument
2873 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); in virtnet_cpu_notif_add()
2877 &vi->node_dead); in virtnet_cpu_notif_add()
2880 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); in virtnet_cpu_notif_add()
2884 static void virtnet_cpu_notif_remove(struct virtnet_info *vi) in virtnet_cpu_notif_remove() argument
2886 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); in virtnet_cpu_notif_remove()
2888 &vi->node_dead); in virtnet_cpu_notif_remove()
2896 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_ringparam() local
2898 ring->rx_max_pending = vi->rq[0].vq->num_max; in virtnet_get_ringparam()
2899 ring->tx_max_pending = vi->sq[0].vq->num_max; in virtnet_get_ringparam()
2900 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); in virtnet_get_ringparam()
2901 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); in virtnet_get_ringparam()
2904 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
2912 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_ringparam() local
2921 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq); in virtnet_set_ringparam()
2922 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq); in virtnet_set_ringparam()
2928 if (ring->rx_pending > vi->rq[0].vq->num_max) in virtnet_set_ringparam()
2931 if (ring->tx_pending > vi->sq[0].vq->num_max) in virtnet_set_ringparam()
2934 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_set_ringparam()
2935 rq = vi->rq + i; in virtnet_set_ringparam()
2936 sq = vi->sq + i; in virtnet_set_ringparam()
2939 err = virtnet_tx_resize(vi, sq, ring->tx_pending); in virtnet_set_ringparam()
2948 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(i), in virtnet_set_ringparam()
2949 vi->intr_coal_tx.max_usecs, in virtnet_set_ringparam()
2950 vi->intr_coal_tx.max_packets); in virtnet_set_ringparam()
2954 vi->sq[i].intr_coal.max_usecs = vi->intr_coal_tx.max_usecs; in virtnet_set_ringparam()
2955 vi->sq[i].intr_coal.max_packets = vi->intr_coal_tx.max_packets; in virtnet_set_ringparam()
2959 err = virtnet_rx_resize(vi, rq, ring->rx_pending); in virtnet_set_ringparam()
2964 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(i), in virtnet_set_ringparam()
2965 vi->intr_coal_rx.max_usecs, in virtnet_set_ringparam()
2966 vi->intr_coal_rx.max_packets); in virtnet_set_ringparam()
2970 vi->rq[i].intr_coal.max_usecs = vi->intr_coal_rx.max_usecs; in virtnet_set_ringparam()
2971 vi->rq[i].intr_coal.max_packets = vi->intr_coal_rx.max_packets; in virtnet_set_ringparam()
2978 static bool virtnet_commit_rss_command(struct virtnet_info *vi) in virtnet_commit_rss_command() argument
2980 struct net_device *dev = vi->dev; in virtnet_commit_rss_command()
2988 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size); in virtnet_commit_rss_command()
2990 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1); in virtnet_commit_rss_command()
2991 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size); in virtnet_commit_rss_command()
2995 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size); in virtnet_commit_rss_command()
2997 sg_buf_size = vi->rss_key_size; in virtnet_commit_rss_command()
2998 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size); in virtnet_commit_rss_command()
3000 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, in virtnet_commit_rss_command()
3001 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG in virtnet_commit_rss_command()
3009 static void virtnet_init_default_rss(struct virtnet_info *vi) in virtnet_init_default_rss() argument
3014 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported; in virtnet_init_default_rss()
3015 vi->rss_hash_types_saved = vi->rss_hash_types_supported; in virtnet_init_default_rss()
3016 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size in virtnet_init_default_rss()
3017 ? vi->rss_indir_table_size - 1 : 0; in virtnet_init_default_rss()
3018 vi->ctrl->rss.unclassified_queue = 0; in virtnet_init_default_rss()
3020 for (; i < vi->rss_indir_table_size; ++i) { in virtnet_init_default_rss()
3021 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs); in virtnet_init_default_rss()
3022 vi->ctrl->rss.indirection_table[i] = indir_val; in virtnet_init_default_rss()
3025 vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0; in virtnet_init_default_rss()
3026 vi->ctrl->rss.hash_key_length = vi->rss_key_size; in virtnet_init_default_rss()
3028 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size); in virtnet_init_default_rss()
3031 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info) in virtnet_get_hashflow() argument
3036 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) { in virtnet_get_hashflow()
3039 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { in virtnet_get_hashflow()
3044 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) { in virtnet_get_hashflow()
3047 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { in virtnet_get_hashflow()
3052 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) { in virtnet_get_hashflow()
3055 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) { in virtnet_get_hashflow()
3060 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) { in virtnet_get_hashflow()
3063 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) { in virtnet_get_hashflow()
3068 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) in virtnet_get_hashflow()
3073 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) in virtnet_get_hashflow()
3083 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info) in virtnet_set_hashflow() argument
3085 u32 new_hashtypes = vi->rss_hash_types_saved; in virtnet_set_hashflow()
3134 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported)) in virtnet_set_hashflow()
3137 if (new_hashtypes != vi->rss_hash_types_saved) { in virtnet_set_hashflow()
3138 vi->rss_hash_types_saved = new_hashtypes; in virtnet_set_hashflow()
3139 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; in virtnet_set_hashflow()
3140 if (vi->dev->features & NETIF_F_RXHASH) in virtnet_set_hashflow()
3141 return virtnet_commit_rss_command(vi); in virtnet_set_hashflow()
3150 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_drvinfo() local
3151 struct virtio_device *vdev = vi->vdev; in virtnet_get_drvinfo()
3163 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_channels() local
3173 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) in virtnet_set_channels()
3180 if (vi->rq[0].xdp_prog) in virtnet_set_channels()
3184 err = _virtnet_set_queues(vi, queue_pairs); in virtnet_set_channels()
3189 virtnet_set_affinity(vi); in virtnet_set_channels()
3200 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_strings() local
3206 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_strings()
3212 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_strings()
3223 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_sset_count() local
3227 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN + in virtnet_get_sset_count()
3237 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_ethtool_stats() local
3243 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_ethtool_stats()
3244 struct receive_queue *rq = &vi->rq[i]; in virtnet_get_ethtool_stats()
3258 for (i = 0; i < vi->curr_queue_pairs; i++) { in virtnet_get_ethtool_stats()
3259 struct send_queue *sq = &vi->sq[i]; in virtnet_get_ethtool_stats()
3277 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_channels() local
3279 channels->combined_count = vi->curr_queue_pairs; in virtnet_get_channels()
3280 channels->max_combined = vi->max_queue_pairs; in virtnet_get_channels()
3290 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_link_ksettings() local
3293 &vi->speed, &vi->duplex); in virtnet_set_link_ksettings()
3299 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_link_ksettings() local
3301 cmd->base.speed = vi->speed; in virtnet_get_link_ksettings()
3302 cmd->base.duplex = vi->duplex; in virtnet_get_link_ksettings()
3308 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi, in virtnet_send_notf_coal_cmds() argument
3314 vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs); in virtnet_send_notf_coal_cmds()
3315 vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames); in virtnet_send_notf_coal_cmds()
3316 sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx)); in virtnet_send_notf_coal_cmds()
3318 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, in virtnet_send_notf_coal_cmds()
3324 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3325 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3326 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_send_notf_coal_cmds()
3327 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3328 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3331 vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); in virtnet_send_notf_coal_cmds()
3332 vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); in virtnet_send_notf_coal_cmds()
3333 sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx)); in virtnet_send_notf_coal_cmds()
3335 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, in virtnet_send_notf_coal_cmds()
3341 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3342 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3343 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_send_notf_coal_cmds()
3344 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs; in virtnet_send_notf_coal_cmds()
3345 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames; in virtnet_send_notf_coal_cmds()
3351 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi, in virtnet_send_ctrl_coal_vq_cmd() argument
3356 vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn); in virtnet_send_ctrl_coal_vq_cmd()
3357 vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs); in virtnet_send_ctrl_coal_vq_cmd()
3358 vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets); in virtnet_send_ctrl_coal_vq_cmd()
3359 sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq)); in virtnet_send_ctrl_coal_vq_cmd()
3361 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, in virtnet_send_ctrl_coal_vq_cmd()
3369 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, in virtnet_send_notf_coal_vq_cmds() argument
3375 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue), in virtnet_send_notf_coal_vq_cmds()
3381 vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs; in virtnet_send_notf_coal_vq_cmds()
3382 vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames; in virtnet_send_notf_coal_vq_cmds()
3384 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue), in virtnet_send_notf_coal_vq_cmds()
3390 vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs; in virtnet_send_notf_coal_vq_cmds()
3391 vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames; in virtnet_send_notf_coal_vq_cmds()
3428 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_coalesce() local
3434 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) { in virtnet_set_coalesce()
3436 vi->sq[queue_number].napi.weight, in virtnet_set_coalesce()
3442 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be in virtnet_set_coalesce()
3449 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) in virtnet_set_coalesce()
3450 ret = virtnet_send_notf_coal_cmds(vi, ec); in virtnet_set_coalesce()
3458 for (; queue_number < vi->max_queue_pairs; queue_number++) in virtnet_set_coalesce()
3459 vi->sq[queue_number].napi.weight = napi_weight; in virtnet_set_coalesce()
3470 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_coalesce() local
3472 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { in virtnet_get_coalesce()
3473 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs; in virtnet_get_coalesce()
3474 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs; in virtnet_get_coalesce()
3475 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets; in virtnet_get_coalesce()
3476 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets; in virtnet_get_coalesce()
3480 if (vi->sq[0].napi.weight) in virtnet_get_coalesce()
3491 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_per_queue_coalesce() local
3495 if (queue >= vi->max_queue_pairs) in virtnet_set_per_queue_coalesce()
3501 vi->sq[queue].napi.weight, in virtnet_set_per_queue_coalesce()
3506 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) in virtnet_set_per_queue_coalesce()
3507 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue); in virtnet_set_per_queue_coalesce()
3515 vi->sq[queue].napi.weight = napi_weight; in virtnet_set_per_queue_coalesce()
3524 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_per_queue_coalesce() local
3526 if (queue >= vi->max_queue_pairs) in virtnet_get_per_queue_coalesce()
3529 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) { in virtnet_get_per_queue_coalesce()
3530 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs; in virtnet_get_per_queue_coalesce()
3531 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs; in virtnet_get_per_queue_coalesce()
3532 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets; in virtnet_get_per_queue_coalesce()
3533 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets; in virtnet_get_per_queue_coalesce()
3537 if (vi->sq[queue].napi.weight) in virtnet_get_per_queue_coalesce()
3546 struct virtnet_info *vi = netdev_priv(dev); in virtnet_init_settings() local
3548 vi->speed = SPEED_UNKNOWN; in virtnet_init_settings()
3549 vi->duplex = DUPLEX_UNKNOWN; in virtnet_init_settings()
3552 static void virtnet_update_settings(struct virtnet_info *vi) in virtnet_update_settings() argument
3557 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX)) in virtnet_update_settings()
3560 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed); in virtnet_update_settings()
3563 vi->speed = speed; in virtnet_update_settings()
3565 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex); in virtnet_update_settings()
3568 vi->duplex = duplex; in virtnet_update_settings()
3583 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_rxfh() local
3587 for (i = 0; i < vi->rss_indir_table_size; ++i) in virtnet_get_rxfh()
3588 indir[i] = vi->ctrl->rss.indirection_table[i]; in virtnet_get_rxfh()
3592 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size); in virtnet_get_rxfh()
3602 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_rxfh() local
3610 if (!vi->has_rss) in virtnet_set_rxfh()
3613 for (i = 0; i < vi->rss_indir_table_size; ++i) in virtnet_set_rxfh()
3614 vi->ctrl->rss.indirection_table[i] = indir[i]; in virtnet_set_rxfh()
3622 if (!vi->has_rss && !vi->has_rss_hash_report) in virtnet_set_rxfh()
3625 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size); in virtnet_set_rxfh()
3630 virtnet_commit_rss_command(vi); in virtnet_set_rxfh()
3637 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_rxnfc() local
3642 info->data = vi->curr_queue_pairs; in virtnet_get_rxnfc()
3645 virtnet_get_hashflow(vi, info); in virtnet_get_rxnfc()
3656 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_rxnfc() local
3661 if (!virtnet_set_hashflow(vi, info)) in virtnet_set_rxnfc()
3701 struct virtnet_info *vi = vdev->priv; in virtnet_freeze_down() local
3704 flush_work(&vi->config_work); in virtnet_freeze_down()
3706 netif_tx_lock_bh(vi->dev); in virtnet_freeze_down()
3707 netif_device_detach(vi->dev); in virtnet_freeze_down()
3708 netif_tx_unlock_bh(vi->dev); in virtnet_freeze_down()
3709 if (netif_running(vi->dev)) in virtnet_freeze_down()
3710 virtnet_close(vi->dev); in virtnet_freeze_down()
3713 static int init_vqs(struct virtnet_info *vi);
3717 struct virtnet_info *vi = vdev->priv; in virtnet_restore_up() local
3720 err = init_vqs(vi); in virtnet_restore_up()
3726 enable_delayed_refill(vi); in virtnet_restore_up()
3728 if (netif_running(vi->dev)) { in virtnet_restore_up()
3729 err = virtnet_open(vi->dev); in virtnet_restore_up()
3734 netif_tx_lock_bh(vi->dev); in virtnet_restore_up()
3735 netif_device_attach(vi->dev); in virtnet_restore_up()
3736 netif_tx_unlock_bh(vi->dev); in virtnet_restore_up()
3740 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) in virtnet_set_guest_offloads() argument
3743 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads); in virtnet_set_guest_offloads()
3745 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads)); in virtnet_set_guest_offloads()
3747 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, in virtnet_set_guest_offloads()
3749 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n"); in virtnet_set_guest_offloads()
3756 static int virtnet_clear_guest_offloads(struct virtnet_info *vi) in virtnet_clear_guest_offloads() argument
3760 if (!vi->guest_offloads) in virtnet_clear_guest_offloads()
3763 return virtnet_set_guest_offloads(vi, offloads); in virtnet_clear_guest_offloads()
3766 static int virtnet_restore_guest_offloads(struct virtnet_info *vi) in virtnet_restore_guest_offloads() argument
3768 u64 offloads = vi->guest_offloads; in virtnet_restore_guest_offloads()
3770 if (!vi->guest_offloads) in virtnet_restore_guest_offloads()
3773 return virtnet_set_guest_offloads(vi, offloads); in virtnet_restore_guest_offloads()
3782 struct virtnet_info *vi = netdev_priv(dev); in virtnet_xdp_set() local
3787 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) in virtnet_xdp_set()
3788 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || in virtnet_xdp_set()
3789 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || in virtnet_xdp_set()
3790 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || in virtnet_xdp_set()
3791 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || in virtnet_xdp_set()
3792 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) || in virtnet_xdp_set()
3793 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) || in virtnet_xdp_set()
3794 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) { in virtnet_xdp_set()
3799 if (vi->mergeable_rx_bufs && !vi->any_header_sg) { in virtnet_xdp_set()
3810 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs; in virtnet_xdp_set()
3815 if (curr_qp + xdp_qp > vi->max_queue_pairs) { in virtnet_xdp_set()
3817 curr_qp + xdp_qp, vi->max_queue_pairs); in virtnet_xdp_set()
3821 old_prog = rtnl_dereference(vi->rq[0].xdp_prog); in virtnet_xdp_set()
3826 bpf_prog_add(prog, vi->max_queue_pairs - 1); in virtnet_xdp_set()
3830 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3831 napi_disable(&vi->rq[i].napi); in virtnet_xdp_set()
3832 virtnet_napi_tx_disable(&vi->sq[i].napi); in virtnet_xdp_set()
3837 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3838 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); in virtnet_xdp_set()
3840 virtnet_restore_guest_offloads(vi); in virtnet_xdp_set()
3845 err = _virtnet_set_queues(vi, curr_qp + xdp_qp); in virtnet_xdp_set()
3849 vi->xdp_queue_pairs = xdp_qp; in virtnet_xdp_set()
3852 vi->xdp_enabled = true; in virtnet_xdp_set()
3853 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3854 rcu_assign_pointer(vi->rq[i].xdp_prog, prog); in virtnet_xdp_set()
3856 virtnet_clear_guest_offloads(vi); in virtnet_xdp_set()
3862 vi->xdp_enabled = false; in virtnet_xdp_set()
3865 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3869 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); in virtnet_xdp_set()
3870 virtnet_napi_tx_enable(vi, vi->sq[i].vq, in virtnet_xdp_set()
3871 &vi->sq[i].napi); in virtnet_xdp_set()
3879 virtnet_clear_guest_offloads(vi); in virtnet_xdp_set()
3880 for (i = 0; i < vi->max_queue_pairs; i++) in virtnet_xdp_set()
3881 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog); in virtnet_xdp_set()
3885 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_xdp_set()
3886 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi); in virtnet_xdp_set()
3887 virtnet_napi_tx_enable(vi, vi->sq[i].vq, in virtnet_xdp_set()
3888 &vi->sq[i].napi); in virtnet_xdp_set()
3892 bpf_prog_sub(prog, vi->max_queue_pairs - 1); in virtnet_xdp_set()
3909 struct virtnet_info *vi = netdev_priv(dev); in virtnet_get_phys_port_name() local
3912 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY)) in virtnet_get_phys_port_name()
3925 struct virtnet_info *vi = netdev_priv(dev); in virtnet_set_features() local
3930 if (vi->xdp_enabled) in virtnet_set_features()
3934 offloads = vi->guest_offloads_capable; in virtnet_set_features()
3936 offloads = vi->guest_offloads_capable & in virtnet_set_features()
3939 err = virtnet_set_guest_offloads(vi, offloads); in virtnet_set_features()
3942 vi->guest_offloads = offloads; in virtnet_set_features()
3947 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved; in virtnet_set_features()
3949 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE; in virtnet_set_features()
3951 if (!virtnet_commit_rss_command(vi)) in virtnet_set_features()
3993 struct virtnet_info *vi = in virtnet_config_changed_work() local
3997 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, in virtnet_config_changed_work()
4002 netdev_notify_peers(vi->dev); in virtnet_config_changed_work()
4003 virtnet_ack_link_announce(vi); in virtnet_config_changed_work()
4009 if (vi->status == v) in virtnet_config_changed_work()
4012 vi->status = v; in virtnet_config_changed_work()
4014 if (vi->status & VIRTIO_NET_S_LINK_UP) { in virtnet_config_changed_work()
4015 virtnet_update_settings(vi); in virtnet_config_changed_work()
4016 netif_carrier_on(vi->dev); in virtnet_config_changed_work()
4017 netif_tx_wake_all_queues(vi->dev); in virtnet_config_changed_work()
4019 netif_carrier_off(vi->dev); in virtnet_config_changed_work()
4020 netif_tx_stop_all_queues(vi->dev); in virtnet_config_changed_work()
4026 struct virtnet_info *vi = vdev->priv; in virtnet_config_changed() local
4028 schedule_work(&vi->config_work); in virtnet_config_changed()
4031 static void virtnet_free_queues(struct virtnet_info *vi) in virtnet_free_queues() argument
4035 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_free_queues()
4036 __netif_napi_del(&vi->rq[i].napi); in virtnet_free_queues()
4037 __netif_napi_del(&vi->sq[i].napi); in virtnet_free_queues()
4041 * we need to respect an RCU grace period before freeing vi->rq in virtnet_free_queues()
4045 kfree(vi->rq); in virtnet_free_queues()
4046 kfree(vi->sq); in virtnet_free_queues()
4047 kfree(vi->ctrl); in virtnet_free_queues()
4050 static void _free_receive_bufs(struct virtnet_info *vi) in _free_receive_bufs() argument
4055 for (i = 0; i < vi->max_queue_pairs; i++) { in _free_receive_bufs()
4056 while (vi->rq[i].pages) in _free_receive_bufs()
4057 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); in _free_receive_bufs()
4059 old_prog = rtnl_dereference(vi->rq[i].xdp_prog); in _free_receive_bufs()
4060 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL); in _free_receive_bufs()
4066 static void free_receive_bufs(struct virtnet_info *vi) in free_receive_bufs() argument
4069 _free_receive_bufs(vi); in free_receive_bufs()
4073 static void free_receive_page_frags(struct virtnet_info *vi) in free_receive_page_frags() argument
4076 for (i = 0; i < vi->max_queue_pairs; i++) in free_receive_page_frags()
4077 if (vi->rq[i].alloc_frag.page) { in free_receive_page_frags()
4078 if (vi->rq[i].do_dma && vi->rq[i].last_dma) in free_receive_page_frags()
4079 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0); in free_receive_page_frags()
4080 put_page(vi->rq[i].alloc_frag.page); in free_receive_page_frags()
4092 static void free_unused_bufs(struct virtnet_info *vi) in free_unused_bufs() argument
4097 for (i = 0; i < vi->max_queue_pairs; i++) { in free_unused_bufs()
4098 struct virtqueue *vq = vi->sq[i].vq; in free_unused_bufs()
4104 for (i = 0; i < vi->max_queue_pairs; i++) { in free_unused_bufs()
4105 struct virtqueue *vq = vi->rq[i].vq; in free_unused_bufs()
4113 static void virtnet_del_vqs(struct virtnet_info *vi) in virtnet_del_vqs() argument
4115 struct virtio_device *vdev = vi->vdev; in virtnet_del_vqs()
4117 virtnet_clean_affinity(vi); in virtnet_del_vqs()
4121 virtnet_free_queues(vi); in virtnet_del_vqs()
4128 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq) in mergeable_min_buf_len() argument
4130 const unsigned int hdr_len = vi->hdr_len; in mergeable_min_buf_len()
4132 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu; in mergeable_min_buf_len()
4140 static int virtnet_find_vqs(struct virtnet_info *vi) in virtnet_find_vqs() argument
4154 total_vqs = vi->max_queue_pairs * 2 + in virtnet_find_vqs()
4155 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); in virtnet_find_vqs()
4167 if (!vi->big_packets || vi->mergeable_rx_bufs) { in virtnet_find_vqs()
4176 if (vi->has_cvq) { in virtnet_find_vqs()
4182 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_find_vqs()
4185 sprintf(vi->rq[i].name, "input.%u", i); in virtnet_find_vqs()
4186 sprintf(vi->sq[i].name, "output.%u", i); in virtnet_find_vqs()
4187 names[rxq2vq(i)] = vi->rq[i].name; in virtnet_find_vqs()
4188 names[txq2vq(i)] = vi->sq[i].name; in virtnet_find_vqs()
4193 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks, in virtnet_find_vqs()
4198 if (vi->has_cvq) { in virtnet_find_vqs()
4199 vi->cvq = vqs[total_vqs - 1]; in virtnet_find_vqs()
4200 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) in virtnet_find_vqs()
4201 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; in virtnet_find_vqs()
4204 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_find_vqs()
4205 vi->rq[i].vq = vqs[rxq2vq(i)]; in virtnet_find_vqs()
4206 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq); in virtnet_find_vqs()
4207 vi->sq[i].vq = vqs[txq2vq(i)]; in virtnet_find_vqs()
4225 static int virtnet_alloc_queues(struct virtnet_info *vi) in virtnet_alloc_queues() argument
4229 if (vi->has_cvq) { in virtnet_alloc_queues()
4230 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL); in virtnet_alloc_queues()
4231 if (!vi->ctrl) in virtnet_alloc_queues()
4234 vi->ctrl = NULL; in virtnet_alloc_queues()
4236 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL); in virtnet_alloc_queues()
4237 if (!vi->sq) in virtnet_alloc_queues()
4239 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL); in virtnet_alloc_queues()
4240 if (!vi->rq) in virtnet_alloc_queues()
4243 INIT_DELAYED_WORK(&vi->refill, refill_work); in virtnet_alloc_queues()
4244 for (i = 0; i < vi->max_queue_pairs; i++) { in virtnet_alloc_queues()
4245 vi->rq[i].pages = NULL; in virtnet_alloc_queues()
4246 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll, in virtnet_alloc_queues()
4248 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi, in virtnet_alloc_queues()
4252 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); in virtnet_alloc_queues()
4253 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); in virtnet_alloc_queues()
4254 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); in virtnet_alloc_queues()
4256 u64_stats_init(&vi->rq[i].stats.syncp); in virtnet_alloc_queues()
4257 u64_stats_init(&vi->sq[i].stats.syncp); in virtnet_alloc_queues()
4263 kfree(vi->sq); in virtnet_alloc_queues()
4265 kfree(vi->ctrl); in virtnet_alloc_queues()
4270 static int init_vqs(struct virtnet_info *vi) in init_vqs() argument
4275 ret = virtnet_alloc_queues(vi); in init_vqs()
4279 ret = virtnet_find_vqs(vi); in init_vqs()
4283 virtnet_rq_set_premapped(vi); in init_vqs()
4286 virtnet_set_affinity(vi); in init_vqs()
4292 virtnet_free_queues(vi); in init_vqs()
4301 struct virtnet_info *vi = netdev_priv(queue->dev); in mergeable_rx_buffer_size_show() local
4303 unsigned int headroom = virtnet_get_headroom(vi); in mergeable_rx_buffer_size_show()
4307 BUG_ON(queue_index >= vi->max_queue_pairs); in mergeable_rx_buffer_size_show()
4308 avg = &vi->rq[queue_index].mrg_avg_pkt_len; in mergeable_rx_buffer_size_show()
4310 get_mergeable_buf_len(&vi->rq[queue_index], avg, in mergeable_rx_buffer_size_show()
4401 static bool virtnet_check_guest_gso(const struct virtnet_info *vi) in virtnet_check_guest_gso() argument
4403 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) || in virtnet_check_guest_gso()
4404 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) || in virtnet_check_guest_gso()
4405 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) || in virtnet_check_guest_gso()
4406 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) || in virtnet_check_guest_gso()
4407 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) && in virtnet_check_guest_gso()
4408 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6)); in virtnet_check_guest_gso()
4411 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) in virtnet_set_big_packets() argument
4413 bool guest_gso = virtnet_check_guest_gso(vi); in virtnet_set_big_packets()
4420 vi->big_packets = true; in virtnet_set_big_packets()
4421 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE); in virtnet_set_big_packets()
4429 struct virtnet_info *vi; in virtnet_probe() local
4524 vi = netdev_priv(dev); in virtnet_probe()
4525 vi->dev = dev; in virtnet_probe()
4526 vi->vdev = vdev; in virtnet_probe()
4527 vdev->priv = vi; in virtnet_probe()
4529 INIT_WORK(&vi->config_work, virtnet_config_changed_work); in virtnet_probe()
4530 spin_lock_init(&vi->refill_lock); in virtnet_probe()
4533 vi->mergeable_rx_bufs = true; in virtnet_probe()
4537 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { in virtnet_probe()
4538 vi->intr_coal_rx.max_usecs = 0; in virtnet_probe()
4539 vi->intr_coal_tx.max_usecs = 0; in virtnet_probe()
4540 vi->intr_coal_tx.max_packets = 0; in virtnet_probe()
4541 vi->intr_coal_rx.max_packets = 0; in virtnet_probe()
4545 vi->has_rss_hash_report = true; in virtnet_probe()
4548 vi->has_rss = true; in virtnet_probe()
4550 vi->rss_indir_table_size = in virtnet_probe()
4555 if (vi->has_rss || vi->has_rss_hash_report) { in virtnet_probe()
4556 vi->rss_key_size = in virtnet_probe()
4558 if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) { in virtnet_probe()
4560 vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE); in virtnet_probe()
4565 vi->rss_hash_types_supported = in virtnet_probe()
4567 vi->rss_hash_types_supported &= in virtnet_probe()
4575 if (vi->has_rss_hash_report) in virtnet_probe()
4576 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash); in virtnet_probe()
4579 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); in virtnet_probe()
4581 vi->hdr_len = sizeof(struct virtio_net_hdr); in virtnet_probe()
4585 vi->any_header_sg = true; in virtnet_probe()
4588 vi->has_cvq = true; in virtnet_probe()
4609 virtnet_set_big_packets(vi, mtu); in virtnet_probe()
4611 if (vi->any_header_sg) in virtnet_probe()
4612 dev->needed_headroom = vi->hdr_len; in virtnet_probe()
4616 vi->curr_queue_pairs = max_queue_pairs; in virtnet_probe()
4618 vi->curr_queue_pairs = num_online_cpus(); in virtnet_probe()
4619 vi->max_queue_pairs = max_queue_pairs; in virtnet_probe()
4622 err = init_vqs(vi); in virtnet_probe()
4627 if (vi->mergeable_rx_bufs) in virtnet_probe()
4630 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); in virtnet_probe()
4631 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); in virtnet_probe()
4636 vi->failover = net_failover_create(vi->dev); in virtnet_probe()
4637 if (IS_ERR(vi->failover)) { in virtnet_probe()
4638 err = PTR_ERR(vi->failover); in virtnet_probe()
4643 if (vi->has_rss || vi->has_rss_hash_report) in virtnet_probe()
4644 virtnet_init_default_rss(vi); in virtnet_probe()
4658 _virtnet_set_queues(vi, vi->curr_queue_pairs); in virtnet_probe()
4665 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { in virtnet_probe()
4669 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, in virtnet_probe()
4680 err = virtnet_cpu_notif_add(vi); in virtnet_probe()
4689 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { in virtnet_probe()
4690 schedule_work(&vi->config_work); in virtnet_probe()
4692 vi->status = VIRTIO_NET_S_LINK_UP; in virtnet_probe()
4693 virtnet_update_settings(vi); in virtnet_probe()
4698 if (virtio_has_feature(vi->vdev, guest_offloads[i])) in virtnet_probe()
4699 set_bit(guest_offloads[i], &vi->guest_offloads); in virtnet_probe()
4700 vi->guest_offloads_capable = vi->guest_offloads; in virtnet_probe()
4710 net_failover_destroy(vi->failover); in virtnet_probe()
4713 cancel_delayed_work_sync(&vi->refill); in virtnet_probe()
4714 free_receive_page_frags(vi); in virtnet_probe()
4715 virtnet_del_vqs(vi); in virtnet_probe()
4721 static void remove_vq_common(struct virtnet_info *vi) in remove_vq_common() argument
4723 virtio_reset_device(vi->vdev); in remove_vq_common()
4726 free_unused_bufs(vi); in remove_vq_common()
4728 free_receive_bufs(vi); in remove_vq_common()
4730 free_receive_page_frags(vi); in remove_vq_common()
4732 virtnet_del_vqs(vi); in remove_vq_common()
4737 struct virtnet_info *vi = vdev->priv; in virtnet_remove() local
4739 virtnet_cpu_notif_remove(vi); in virtnet_remove()
4742 flush_work(&vi->config_work); in virtnet_remove()
4744 unregister_netdev(vi->dev); in virtnet_remove()
4746 net_failover_destroy(vi->failover); in virtnet_remove()
4748 remove_vq_common(vi); in virtnet_remove()
4750 free_netdev(vi->dev); in virtnet_remove()
4755 struct virtnet_info *vi = vdev->priv; in virtnet_freeze() local
4757 virtnet_cpu_notif_remove(vi); in virtnet_freeze()
4759 remove_vq_common(vi); in virtnet_freeze()
4766 struct virtnet_info *vi = vdev->priv; in virtnet_restore() local
4772 virtnet_set_queues(vi, vi->curr_queue_pairs); in virtnet_restore()
4774 err = virtnet_cpu_notif_add(vi); in virtnet_restore()
4777 remove_vq_common(vi); in virtnet_restore()