Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1 | /* A simple network driver using virtio. |
| 2 | * |
| 3 | * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | //#define DEBUG |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/etherdevice.h> |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 22 | #include <linux/ethtool.h> |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/virtio.h> |
| 25 | #include <linux/virtio_net.h> |
| 26 | #include <linux/scatterlist.h> |
Alex Williamson | e918085a | 2009-01-25 18:06:26 -0800 | [diff] [blame] | 27 | #include <linux/if_vlan.h> |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 28 | |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 29 | static int napi_weight = 128; |
| 30 | module_param(napi_weight, int, 0444); |
| 31 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 32 | static int csum = 1, gso = 1; |
| 33 | module_param(csum, bool, 0444); |
| 34 | module_param(gso, bool, 0444); |
| 35 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 36 | /* FIXME: MTU in config. */ |
Alex Williamson | e918085a | 2009-01-25 18:06:26 -0800 | [diff] [blame] | 37 | #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 38 | #define GOOD_COPY_LEN 128 |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 39 | |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 40 | #define VIRTNET_SEND_COMMAND_SG_MAX 2 |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 41 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 42 | struct virtnet_info |
| 43 | { |
| 44 | struct virtio_device *vdev; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 45 | struct virtqueue *rvq, *svq, *cvq; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 46 | struct net_device *dev; |
| 47 | struct napi_struct napi; |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 48 | unsigned int status; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 49 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 50 | /* The skb we couldn't send because buffers were full. */ |
| 51 | struct sk_buff *last_xmit_skb; |
| 52 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 53 | /* If we need to free in a timer, this is it. */ |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 54 | struct timer_list xmit_free_timer; |
| 55 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 56 | /* Number of input buffers, and max we've ever had. */ |
| 57 | unsigned int num, max; |
| 58 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 59 | /* For cleaning up after transmission. */ |
| 60 | struct tasklet_struct tasklet; |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 61 | bool free_in_tasklet; |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 62 | |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 63 | /* I like... big packets and I cannot lie! */ |
| 64 | bool big_packets; |
| 65 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 66 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
| 67 | bool mergeable_rx_bufs; |
| 68 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 69 | /* Receive & send queues. */ |
| 70 | struct sk_buff_head recv; |
| 71 | struct sk_buff_head send; |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 72 | |
| 73 | /* Chain pages by the private ptr. */ |
| 74 | struct page *pages; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 75 | }; |
| 76 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 77 | static inline void *skb_vnet_hdr(struct sk_buff *skb) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 78 | { |
| 79 | return (struct virtio_net_hdr *)skb->cb; |
| 80 | } |
| 81 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 82 | static void give_a_page(struct virtnet_info *vi, struct page *page) |
| 83 | { |
| 84 | page->private = (unsigned long)vi->pages; |
| 85 | vi->pages = page; |
| 86 | } |
| 87 | |
Mark McLoughlin | 0a888fd | 2008-11-16 22:39:18 -0800 | [diff] [blame] | 88 | static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb) |
| 89 | { |
| 90 | unsigned int i; |
| 91 | |
| 92 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 93 | give_a_page(vi, skb_shinfo(skb)->frags[i].page); |
| 94 | skb_shinfo(skb)->nr_frags = 0; |
| 95 | skb->data_len = 0; |
| 96 | } |
| 97 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 98 | static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask) |
| 99 | { |
| 100 | struct page *p = vi->pages; |
| 101 | |
| 102 | if (p) |
| 103 | vi->pages = (struct page *)p->private; |
| 104 | else |
| 105 | p = alloc_page(gfp_mask); |
| 106 | return p; |
| 107 | } |
| 108 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 109 | static void skb_xmit_done(struct virtqueue *svq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 110 | { |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 111 | struct virtnet_info *vi = svq->vdev->priv; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 112 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 113 | /* Suppress further interrupts. */ |
| 114 | svq->vq_ops->disable_cb(svq); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 115 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 116 | /* We were probably waiting for more output buffers. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 117 | netif_wake_queue(vi->dev); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 118 | |
| 119 | /* Make sure we re-xmit last_xmit_skb: if there are no more packets |
| 120 | * queued, start_xmit won't be called. */ |
| 121 | tasklet_schedule(&vi->tasklet); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, |
| 125 | unsigned len) |
| 126 | { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 127 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 128 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 129 | int err; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 130 | int i; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 131 | |
| 132 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { |
| 133 | pr_debug("%s: short packet %i\n", dev->name, len); |
| 134 | dev->stats.rx_length_errors++; |
| 135 | goto drop; |
| 136 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 137 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 138 | if (vi->mergeable_rx_bufs) { |
| 139 | struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); |
| 140 | unsigned int copy; |
| 141 | char *p = page_address(skb_shinfo(skb)->frags[0].page); |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 142 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 143 | if (len > PAGE_SIZE) |
| 144 | len = PAGE_SIZE; |
| 145 | len -= sizeof(struct virtio_net_hdr_mrg_rxbuf); |
| 146 | |
| 147 | memcpy(hdr, p, sizeof(*mhdr)); |
| 148 | p += sizeof(*mhdr); |
| 149 | |
| 150 | copy = len; |
| 151 | if (copy > skb_tailroom(skb)) |
| 152 | copy = skb_tailroom(skb); |
| 153 | |
| 154 | memcpy(skb_put(skb, copy), p, copy); |
| 155 | |
| 156 | len -= copy; |
| 157 | |
| 158 | if (!len) { |
| 159 | give_a_page(vi, skb_shinfo(skb)->frags[0].page); |
| 160 | skb_shinfo(skb)->nr_frags--; |
| 161 | } else { |
| 162 | skb_shinfo(skb)->frags[0].page_offset += |
| 163 | sizeof(*mhdr) + copy; |
| 164 | skb_shinfo(skb)->frags[0].size = len; |
| 165 | skb->data_len += len; |
| 166 | skb->len += len; |
| 167 | } |
| 168 | |
| 169 | while (--mhdr->num_buffers) { |
| 170 | struct sk_buff *nskb; |
| 171 | |
| 172 | i = skb_shinfo(skb)->nr_frags; |
| 173 | if (i >= MAX_SKB_FRAGS) { |
| 174 | pr_debug("%s: packet too long %d\n", dev->name, |
| 175 | len); |
| 176 | dev->stats.rx_length_errors++; |
| 177 | goto drop; |
| 178 | } |
| 179 | |
| 180 | nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len); |
| 181 | if (!nskb) { |
| 182 | pr_debug("%s: rx error: %d buffers missing\n", |
| 183 | dev->name, mhdr->num_buffers); |
| 184 | dev->stats.rx_length_errors++; |
| 185 | goto drop; |
| 186 | } |
| 187 | |
| 188 | __skb_unlink(nskb, &vi->recv); |
| 189 | vi->num--; |
| 190 | |
| 191 | skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0]; |
| 192 | skb_shinfo(nskb)->nr_frags = 0; |
| 193 | kfree_skb(nskb); |
| 194 | |
| 195 | if (len > PAGE_SIZE) |
| 196 | len = PAGE_SIZE; |
| 197 | |
| 198 | skb_shinfo(skb)->frags[i].size = len; |
| 199 | skb_shinfo(skb)->nr_frags++; |
| 200 | skb->data_len += len; |
| 201 | skb->len += len; |
| 202 | } |
| 203 | } else { |
| 204 | len -= sizeof(struct virtio_net_hdr); |
| 205 | |
| 206 | if (len <= MAX_PACKET_LEN) |
| 207 | trim_pages(vi, skb); |
| 208 | |
| 209 | err = pskb_trim(skb, len); |
| 210 | if (err) { |
| 211 | pr_debug("%s: pskb_trim failed %i %d\n", dev->name, |
| 212 | len, err); |
| 213 | dev->stats.rx_dropped++; |
| 214 | goto drop; |
| 215 | } |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 216 | } |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 217 | |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 218 | skb->truesize += skb->data_len; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 219 | dev->stats.rx_bytes += skb->len; |
| 220 | dev->stats.rx_packets++; |
| 221 | |
| 222 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 223 | pr_debug("Needs csum!\n"); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 224 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 225 | goto frame_err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 226 | } |
| 227 | |
Mark McLoughlin | 23cde76 | 2008-06-08 20:49:00 +1000 | [diff] [blame] | 228 | skb->protocol = eth_type_trans(skb, dev); |
| 229 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
| 230 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
| 231 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 232 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 233 | pr_debug("GSO!\n"); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 234 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 235 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 236 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 237 | break; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 238 | case VIRTIO_NET_HDR_GSO_UDP: |
| 239 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
| 240 | break; |
| 241 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 242 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 243 | break; |
| 244 | default: |
| 245 | if (net_ratelimit()) |
| 246 | printk(KERN_WARNING "%s: bad gso type %u.\n", |
| 247 | dev->name, hdr->gso_type); |
| 248 | goto frame_err; |
| 249 | } |
| 250 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 251 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 252 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
| 253 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 254 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
| 255 | if (skb_shinfo(skb)->gso_size == 0) { |
| 256 | if (net_ratelimit()) |
| 257 | printk(KERN_WARNING "%s: zero gso size.\n", |
| 258 | dev->name); |
| 259 | goto frame_err; |
| 260 | } |
| 261 | |
| 262 | /* Header must be checked, and gso_segs computed. */ |
| 263 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 264 | skb_shinfo(skb)->gso_segs = 0; |
| 265 | } |
| 266 | |
| 267 | netif_receive_skb(skb); |
| 268 | return; |
| 269 | |
| 270 | frame_err: |
| 271 | dev->stats.rx_frame_errors++; |
| 272 | drop: |
| 273 | dev_kfree_skb(skb); |
| 274 | } |
| 275 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 276 | static void try_fill_recv_maxbufs(struct virtnet_info *vi) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 277 | { |
| 278 | struct sk_buff *skb; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 279 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 280 | int num, err, i; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 281 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 282 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 283 | for (;;) { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 284 | struct virtio_net_hdr *hdr; |
| 285 | |
Herbert Xu | 8981f01 | 2009-06-11 20:55:17 -0700 | [diff] [blame] | 286 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 287 | if (unlikely(!skb)) |
| 288 | break; |
| 289 | |
Herbert Xu | 8981f01 | 2009-06-11 20:55:17 -0700 | [diff] [blame] | 290 | skb_reserve(skb, NET_IP_ALIGN); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 291 | skb_put(skb, MAX_PACKET_LEN); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 292 | |
| 293 | hdr = skb_vnet_hdr(skb); |
Ira W. Snyder | 8527bec | 2009-01-26 21:00:33 -0800 | [diff] [blame] | 294 | sg_set_buf(sg, hdr, sizeof(*hdr)); |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 295 | |
| 296 | if (vi->big_packets) { |
| 297 | for (i = 0; i < MAX_SKB_FRAGS; i++) { |
| 298 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 299 | f->page = get_a_page(vi, GFP_ATOMIC); |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 300 | if (!f->page) |
| 301 | break; |
| 302 | |
| 303 | f->page_offset = 0; |
| 304 | f->size = PAGE_SIZE; |
| 305 | |
| 306 | skb->data_len += PAGE_SIZE; |
| 307 | skb->len += PAGE_SIZE; |
| 308 | |
| 309 | skb_shinfo(skb)->nr_frags++; |
| 310 | } |
| 311 | } |
| 312 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 313 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 314 | skb_queue_head(&vi->recv, skb); |
| 315 | |
| 316 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); |
| 317 | if (err) { |
| 318 | skb_unlink(skb, &vi->recv); |
Mark McLoughlin | 0a888fd | 2008-11-16 22:39:18 -0800 | [diff] [blame] | 319 | trim_pages(vi, skb); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 320 | kfree_skb(skb); |
| 321 | break; |
| 322 | } |
| 323 | vi->num++; |
| 324 | } |
| 325 | if (unlikely(vi->num > vi->max)) |
| 326 | vi->max = vi->num; |
| 327 | vi->rvq->vq_ops->kick(vi->rvq); |
| 328 | } |
| 329 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 330 | static void try_fill_recv(struct virtnet_info *vi) |
| 331 | { |
| 332 | struct sk_buff *skb; |
| 333 | struct scatterlist sg[1]; |
| 334 | int err; |
| 335 | |
| 336 | if (!vi->mergeable_rx_bufs) { |
| 337 | try_fill_recv_maxbufs(vi); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | for (;;) { |
| 342 | skb_frag_t *f; |
| 343 | |
| 344 | skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); |
| 345 | if (unlikely(!skb)) |
| 346 | break; |
| 347 | |
| 348 | skb_reserve(skb, NET_IP_ALIGN); |
| 349 | |
| 350 | f = &skb_shinfo(skb)->frags[0]; |
| 351 | f->page = get_a_page(vi, GFP_ATOMIC); |
| 352 | if (!f->page) { |
| 353 | kfree_skb(skb); |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | f->page_offset = 0; |
| 358 | f->size = PAGE_SIZE; |
| 359 | |
| 360 | skb_shinfo(skb)->nr_frags++; |
| 361 | |
| 362 | sg_init_one(sg, page_address(f->page), PAGE_SIZE); |
| 363 | skb_queue_head(&vi->recv, skb); |
| 364 | |
| 365 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); |
| 366 | if (err) { |
| 367 | skb_unlink(skb, &vi->recv); |
| 368 | kfree_skb(skb); |
| 369 | break; |
| 370 | } |
| 371 | vi->num++; |
| 372 | } |
| 373 | if (unlikely(vi->num > vi->max)) |
| 374 | vi->max = vi->num; |
| 375 | vi->rvq->vq_ops->kick(vi->rvq); |
| 376 | } |
| 377 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 378 | static void skb_recv_done(struct virtqueue *rvq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 379 | { |
| 380 | struct virtnet_info *vi = rvq->vdev->priv; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 381 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 382 | if (napi_schedule_prep(&vi->napi)) { |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 383 | rvq->vq_ops->disable_cb(rvq); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 384 | __napi_schedule(&vi->napi); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 385 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int virtnet_poll(struct napi_struct *napi, int budget) |
| 389 | { |
| 390 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); |
| 391 | struct sk_buff *skb = NULL; |
| 392 | unsigned int len, received = 0; |
| 393 | |
| 394 | again: |
| 395 | while (received < budget && |
| 396 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { |
| 397 | __skb_unlink(skb, &vi->recv); |
| 398 | receive_skb(vi->dev, skb, len); |
| 399 | vi->num--; |
| 400 | received++; |
| 401 | } |
| 402 | |
| 403 | /* FIXME: If we oom and completely run out of inbufs, we need |
| 404 | * to start a timer trying to fill more. */ |
| 405 | if (vi->num < vi->max / 2) |
| 406 | try_fill_recv(vi); |
| 407 | |
Rusty Russell | 8329d98 | 2007-11-19 11:20:43 -0500 | [diff] [blame] | 408 | /* Out of packets? */ |
| 409 | if (received < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 410 | napi_complete(napi); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 411 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 412 | && napi_schedule_prep(napi)) { |
| 413 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 414 | __napi_schedule(napi); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 415 | goto again; |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 416 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | return received; |
| 420 | } |
| 421 | |
| 422 | static void free_old_xmit_skbs(struct virtnet_info *vi) |
| 423 | { |
| 424 | struct sk_buff *skb; |
| 425 | unsigned int len; |
| 426 | |
| 427 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { |
| 428 | pr_debug("Sent skb %p\n", skb); |
| 429 | __skb_unlink(skb, &vi->send); |
Rusty Russell | 655aa31 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 430 | vi->dev->stats.tx_bytes += skb->len; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 431 | vi->dev->stats.tx_packets++; |
| 432 | kfree_skb(skb); |
| 433 | } |
| 434 | } |
| 435 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 436 | /* If the virtio transport doesn't always notify us when all in-flight packets |
| 437 | * are consumed, we fall back to using this function on a timer to free them. */ |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 438 | static void xmit_free(unsigned long data) |
| 439 | { |
| 440 | struct virtnet_info *vi = (void *)data; |
| 441 | |
| 442 | netif_tx_lock(vi->dev); |
| 443 | |
| 444 | free_old_xmit_skbs(vi); |
| 445 | |
| 446 | if (!skb_queue_empty(&vi->send)) |
| 447 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); |
| 448 | |
| 449 | netif_tx_unlock(vi->dev); |
| 450 | } |
| 451 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 452 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 453 | { |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 454 | int num, err; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 455 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 456 | struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); |
| 457 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 458 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 459 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 460 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 4d125de | 2007-11-07 16:34:49 +1100 | [diff] [blame] | 461 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 462 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 463 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 464 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 465 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 466 | hdr->csum_start = skb->csum_start - skb_headroom(skb); |
| 467 | hdr->csum_offset = skb->csum_offset; |
| 468 | } else { |
| 469 | hdr->flags = 0; |
| 470 | hdr->csum_offset = hdr->csum_start = 0; |
| 471 | } |
| 472 | |
| 473 | if (skb_is_gso(skb)) { |
Herbert Xu | b82f08e | 2009-06-04 00:59:18 +0000 | [diff] [blame] | 474 | hdr->hdr_len = skb_headlen(skb); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 475 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 476 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 477 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 478 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 479 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 480 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 481 | hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; |
| 482 | else |
| 483 | BUG(); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 484 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
| 485 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 486 | } else { |
| 487 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 488 | hdr->gso_size = hdr->hdr_len = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 489 | } |
| 490 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 491 | mhdr->num_buffers = 0; |
| 492 | |
| 493 | /* Encode metadata header at front. */ |
| 494 | if (vi->mergeable_rx_bufs) |
Ira W. Snyder | 8527bec | 2009-01-26 21:00:33 -0800 | [diff] [blame] | 495 | sg_set_buf(sg, mhdr, sizeof(*mhdr)); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 496 | else |
Ira W. Snyder | 8527bec | 2009-01-26 21:00:33 -0800 | [diff] [blame] | 497 | sg_set_buf(sg, hdr, sizeof(*hdr)); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 498 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 499 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 500 | |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 501 | err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 502 | if (!err && !vi->free_in_tasklet) |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 503 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); |
| 504 | |
| 505 | return err; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 508 | static void xmit_tasklet(unsigned long data) |
| 509 | { |
| 510 | struct virtnet_info *vi = (void *)data; |
| 511 | |
| 512 | netif_tx_lock_bh(vi->dev); |
| 513 | if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { |
| 514 | vi->svq->vq_ops->kick(vi->svq); |
| 515 | vi->last_xmit_skb = NULL; |
| 516 | } |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 517 | if (vi->free_in_tasklet) |
| 518 | free_old_xmit_skbs(vi); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 519 | netif_tx_unlock_bh(vi->dev); |
| 520 | } |
| 521 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 522 | static int start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 523 | { |
| 524 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 525 | |
| 526 | again: |
| 527 | /* Free up any pending old buffers before queueing new ones. */ |
| 528 | free_old_xmit_skbs(vi); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 529 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 530 | /* If we has a buffer left over from last time, send it now. */ |
Mark McLoughlin | 9953ca6 | 2008-05-27 12:06:26 +0100 | [diff] [blame] | 531 | if (unlikely(vi->last_xmit_skb) && |
| 532 | xmit_skb(vi, vi->last_xmit_skb) != 0) |
| 533 | goto stop_queue; |
| 534 | |
| 535 | vi->last_xmit_skb = NULL; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 536 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 537 | /* Put new one in send queue and do transmit */ |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 538 | if (likely(skb)) { |
| 539 | __skb_queue_head(&vi->send, skb); |
| 540 | if (xmit_skb(vi, skb) != 0) { |
| 541 | vi->last_xmit_skb = skb; |
| 542 | skb = NULL; |
| 543 | goto stop_queue; |
| 544 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 545 | } |
| 546 | done: |
| 547 | vi->svq->vq_ops->kick(vi->svq); |
| 548 | return NETDEV_TX_OK; |
| 549 | |
| 550 | stop_queue: |
| 551 | pr_debug("%s: virtio not prepared to send\n", dev->name); |
| 552 | netif_stop_queue(dev); |
| 553 | |
| 554 | /* Activate callback for using skbs: if this returns false it |
| 555 | * means some were used in the meantime. */ |
| 556 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { |
| 557 | vi->svq->vq_ops->disable_cb(vi->svq); |
| 558 | netif_start_queue(dev); |
| 559 | goto again; |
| 560 | } |
Mark McLoughlin | 9953ca6 | 2008-05-27 12:06:26 +0100 | [diff] [blame] | 561 | if (skb) { |
| 562 | /* Drop this skb: we only queue one. */ |
| 563 | vi->dev->stats.tx_dropped++; |
| 564 | kfree_skb(skb); |
| 565 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 566 | goto done; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 567 | } |
| 568 | |
Alex Williamson | 9c46f6d | 2009-02-04 16:36:34 -0800 | [diff] [blame] | 569 | static int virtnet_set_mac_address(struct net_device *dev, void *p) |
| 570 | { |
| 571 | struct virtnet_info *vi = netdev_priv(dev); |
| 572 | struct virtio_device *vdev = vi->vdev; |
| 573 | int ret; |
| 574 | |
| 575 | ret = eth_mac_addr(dev, p); |
| 576 | if (ret) |
| 577 | return ret; |
| 578 | |
Alex Williamson | 62994b2 | 2009-04-04 16:40:19 -0700 | [diff] [blame] | 579 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) |
| 580 | vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), |
| 581 | dev->dev_addr, dev->addr_len); |
Alex Williamson | 9c46f6d | 2009-02-04 16:36:34 -0800 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 586 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 587 | static void virtnet_netpoll(struct net_device *dev) |
| 588 | { |
| 589 | struct virtnet_info *vi = netdev_priv(dev); |
| 590 | |
| 591 | napi_schedule(&vi->napi); |
| 592 | } |
| 593 | #endif |
| 594 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 595 | static int virtnet_open(struct net_device *dev) |
| 596 | { |
| 597 | struct virtnet_info *vi = netdev_priv(dev); |
| 598 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 599 | napi_enable(&vi->napi); |
Rusty Russell | a48bd8f | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 600 | |
| 601 | /* If all buffers were filled by other side before we napi_enabled, we |
| 602 | * won't get another interrupt, so process any outstanding packets |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 603 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
| 604 | * We synchronize against interrupts via NAPI_STATE_SCHED */ |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 605 | if (napi_schedule_prep(&vi->napi)) { |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 606 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 607 | __napi_schedule(&vi->napi); |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 608 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 612 | /* |
| 613 | * Send command via the control virtqueue and check status. Commands |
| 614 | * supported by the hypervisor, as indicated by feature bits, should |
| 615 | * never fail unless improperly formated. |
| 616 | */ |
| 617 | static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, |
| 618 | struct scatterlist *data, int out, int in) |
| 619 | { |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 620 | struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 621 | struct virtio_net_ctrl_hdr ctrl; |
| 622 | virtio_net_ctrl_ack status = ~0; |
| 623 | unsigned int tmp; |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 624 | int i; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 625 | |
Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 626 | /* Caller should know better */ |
| 627 | BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) || |
| 628 | (out + in > VIRTNET_SEND_COMMAND_SG_MAX)); |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 629 | |
| 630 | out++; /* Add header */ |
| 631 | in++; /* Add return status */ |
| 632 | |
| 633 | ctrl.class = class; |
| 634 | ctrl.cmd = cmd; |
| 635 | |
| 636 | sg_init_table(sg, out + in); |
| 637 | |
| 638 | sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 639 | for_each_sg(data, s, out + in - 2, i) |
| 640 | sg_set_buf(&sg[i + 1], sg_virt(s), s->length); |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 641 | sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); |
| 642 | |
Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 643 | BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi)); |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 644 | |
| 645 | vi->cvq->vq_ops->kick(vi->cvq); |
| 646 | |
| 647 | /* |
| 648 | * Spin for a response, the kick causes an ioport write, trapping |
| 649 | * into the hypervisor, so the request should be handled immediately. |
| 650 | */ |
| 651 | while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) |
| 652 | cpu_relax(); |
| 653 | |
| 654 | return status == VIRTIO_NET_OK; |
| 655 | } |
| 656 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 657 | static int virtnet_close(struct net_device *dev) |
| 658 | { |
| 659 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 660 | |
| 661 | napi_disable(&vi->napi); |
| 662 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 666 | static int virtnet_set_tx_csum(struct net_device *dev, u32 data) |
| 667 | { |
| 668 | struct virtnet_info *vi = netdev_priv(dev); |
| 669 | struct virtio_device *vdev = vi->vdev; |
| 670 | |
| 671 | if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) |
| 672 | return -ENOSYS; |
| 673 | |
| 674 | return ethtool_op_set_tx_hw_csum(dev, data); |
| 675 | } |
| 676 | |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 677 | static void virtnet_set_rx_mode(struct net_device *dev) |
| 678 | { |
| 679 | struct virtnet_info *vi = netdev_priv(dev); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 680 | struct scatterlist sg[2]; |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 681 | u8 promisc, allmulti; |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 682 | struct virtio_net_ctrl_mac *mac_data; |
| 683 | struct dev_addr_list *addr; |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 684 | struct netdev_hw_addr *ha; |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 685 | void *buf; |
| 686 | int i; |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 687 | |
| 688 | /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ |
| 689 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) |
| 690 | return; |
| 691 | |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 692 | promisc = ((dev->flags & IFF_PROMISC) != 0); |
| 693 | allmulti = ((dev->flags & IFF_ALLMULTI) != 0); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 694 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 695 | sg_init_one(sg, &promisc, sizeof(promisc)); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 696 | |
| 697 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, |
| 698 | VIRTIO_NET_CTRL_RX_PROMISC, |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 699 | sg, 1, 0)) |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 700 | dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", |
| 701 | promisc ? "en" : "dis"); |
| 702 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 703 | sg_init_one(sg, &allmulti, sizeof(allmulti)); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 704 | |
| 705 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, |
| 706 | VIRTIO_NET_CTRL_RX_ALLMULTI, |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 707 | sg, 1, 0)) |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 708 | dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", |
| 709 | allmulti ? "en" : "dis"); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 710 | |
| 711 | /* MAC filter - use one buffer for both lists */ |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 712 | mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) + |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 713 | (2 * sizeof(mac_data->entries)), GFP_ATOMIC); |
| 714 | if (!buf) { |
| 715 | dev_warn(&dev->dev, "No memory for MAC address buffer\n"); |
| 716 | return; |
| 717 | } |
| 718 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 719 | sg_init_table(sg, 2); |
| 720 | |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 721 | /* Store the unicast list and count in the front of the buffer */ |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 722 | mac_data->entries = dev->uc.count; |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 723 | i = 0; |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 724 | list_for_each_entry(ha, &dev->uc.list, list) |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 725 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 726 | |
| 727 | sg_set_buf(&sg[0], mac_data, |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 728 | sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN)); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 729 | |
| 730 | /* multicast list and count fill the end */ |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 731 | mac_data = (void *)&mac_data->macs[dev->uc.count][0]; |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 732 | |
| 733 | mac_data->entries = dev->mc_count; |
| 734 | addr = dev->mc_list; |
| 735 | for (i = 0; i < dev->mc_count; i++, addr = addr->next) |
| 736 | memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); |
| 737 | |
| 738 | sg_set_buf(&sg[1], mac_data, |
| 739 | sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); |
| 740 | |
| 741 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, |
| 742 | VIRTIO_NET_CTRL_MAC_TABLE_SET, |
| 743 | sg, 2, 0)) |
| 744 | dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); |
| 745 | |
| 746 | kfree(buf); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Alex Williamson | 1824a98 | 2009-05-01 17:31:10 +0000 | [diff] [blame] | 749 | static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 750 | { |
| 751 | struct virtnet_info *vi = netdev_priv(dev); |
| 752 | struct scatterlist sg; |
| 753 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 754 | sg_init_one(&sg, &vid, sizeof(vid)); |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 755 | |
| 756 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, |
| 757 | VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) |
| 758 | dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); |
| 759 | } |
| 760 | |
Alex Williamson | 1824a98 | 2009-05-01 17:31:10 +0000 | [diff] [blame] | 761 | static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 762 | { |
| 763 | struct virtnet_info *vi = netdev_priv(dev); |
| 764 | struct scatterlist sg; |
| 765 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 766 | sg_init_one(&sg, &vid, sizeof(vid)); |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 767 | |
| 768 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, |
| 769 | VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) |
| 770 | dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); |
| 771 | } |
| 772 | |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 773 | static struct ethtool_ops virtnet_ethtool_ops = { |
| 774 | .set_tx_csum = virtnet_set_tx_csum, |
| 775 | .set_sg = ethtool_op_set_sg, |
Mark McLoughlin | 0276b49 | 2008-11-16 22:40:36 -0800 | [diff] [blame] | 776 | .set_tso = ethtool_op_set_tso, |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 777 | .get_link = ethtool_op_get_link, |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 778 | }; |
| 779 | |
Mark McLoughlin | 39da581 | 2008-11-26 13:58:11 +0000 | [diff] [blame] | 780 | #define MIN_MTU 68 |
| 781 | #define MAX_MTU 65535 |
| 782 | |
| 783 | static int virtnet_change_mtu(struct net_device *dev, int new_mtu) |
| 784 | { |
| 785 | if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) |
| 786 | return -EINVAL; |
| 787 | dev->mtu = new_mtu; |
| 788 | return 0; |
| 789 | } |
| 790 | |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 791 | static const struct net_device_ops virtnet_netdev = { |
| 792 | .ndo_open = virtnet_open, |
| 793 | .ndo_stop = virtnet_close, |
| 794 | .ndo_start_xmit = start_xmit, |
| 795 | .ndo_validate_addr = eth_validate_addr, |
Alex Williamson | 9c46f6d | 2009-02-04 16:36:34 -0800 | [diff] [blame] | 796 | .ndo_set_mac_address = virtnet_set_mac_address, |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 797 | .ndo_set_rx_mode = virtnet_set_rx_mode, |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 798 | .ndo_change_mtu = virtnet_change_mtu, |
Alex Williamson | 1824a98 | 2009-05-01 17:31:10 +0000 | [diff] [blame] | 799 | .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, |
| 800 | .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 801 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 802 | .ndo_poll_controller = virtnet_netpoll, |
| 803 | #endif |
| 804 | }; |
| 805 | |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 806 | static void virtnet_update_status(struct virtnet_info *vi) |
| 807 | { |
| 808 | u16 v; |
| 809 | |
| 810 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) |
| 811 | return; |
| 812 | |
| 813 | vi->vdev->config->get(vi->vdev, |
| 814 | offsetof(struct virtio_net_config, status), |
| 815 | &v, sizeof(v)); |
| 816 | |
| 817 | /* Ignore unknown (future) status bits */ |
| 818 | v &= VIRTIO_NET_S_LINK_UP; |
| 819 | |
| 820 | if (vi->status == v) |
| 821 | return; |
| 822 | |
| 823 | vi->status = v; |
| 824 | |
| 825 | if (vi->status & VIRTIO_NET_S_LINK_UP) { |
| 826 | netif_carrier_on(vi->dev); |
| 827 | netif_wake_queue(vi->dev); |
| 828 | } else { |
| 829 | netif_carrier_off(vi->dev); |
| 830 | netif_stop_queue(vi->dev); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | static void virtnet_config_changed(struct virtio_device *vdev) |
| 835 | { |
| 836 | struct virtnet_info *vi = vdev->priv; |
| 837 | |
| 838 | virtnet_update_status(vi); |
| 839 | } |
| 840 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 841 | static int virtnet_probe(struct virtio_device *vdev) |
| 842 | { |
| 843 | int err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 844 | struct net_device *dev; |
| 845 | struct virtnet_info *vi; |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 846 | struct virtqueue *vqs[3]; |
| 847 | vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL}; |
| 848 | const char *names[] = { "input", "output", "control" }; |
| 849 | int nvqs; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 850 | |
| 851 | /* Allocate ourselves a network device with room for our info */ |
| 852 | dev = alloc_etherdev(sizeof(struct virtnet_info)); |
| 853 | if (!dev) |
| 854 | return -ENOMEM; |
| 855 | |
| 856 | /* Set up network device as normal. */ |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 857 | dev->netdev_ops = &virtnet_netdev; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 858 | dev->features = NETIF_F_HIGHDMA; |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 859 | SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 860 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 861 | |
| 862 | /* Do we support "hardware" checksums? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 863 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 864 | /* This opens up the world of extra features. */ |
| 865 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 866 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 867 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
| 868 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
| 869 | } |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 870 | /* Individual feature bits: what can host handle? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 871 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 872 | dev->features |= NETIF_F_TSO; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 873 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 874 | dev->features |= NETIF_F_TSO6; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 875 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 876 | dev->features |= NETIF_F_TSO_ECN; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 877 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 878 | dev->features |= NETIF_F_UFO; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* Configuration may specify what MAC to use. Otherwise random. */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 882 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 883 | vdev->config->get(vdev, |
| 884 | offsetof(struct virtio_net_config, mac), |
| 885 | dev->dev_addr, dev->addr_len); |
Alex Williamson | 62994b2 | 2009-04-04 16:40:19 -0700 | [diff] [blame] | 886 | } else |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 887 | random_ether_addr(dev->dev_addr); |
| 888 | |
| 889 | /* Set up our device-specific information */ |
| 890 | vi = netdev_priv(dev); |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 891 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 892 | vi->dev = dev; |
| 893 | vi->vdev = vdev; |
Christian Borntraeger | d9d5dcc | 2008-02-18 10:02:51 +0100 | [diff] [blame] | 894 | vdev->priv = vi; |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 895 | vi->pages = NULL; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 896 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 897 | /* If they give us a callback when all buffers are done, we don't need |
| 898 | * the timer. */ |
| 899 | vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); |
| 900 | |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 901 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
| 902 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) |
| 903 | || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) |
| 904 | || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) |
| 905 | vi->big_packets = true; |
| 906 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 907 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
| 908 | vi->mergeable_rx_bufs = true; |
| 909 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 910 | /* We expect two virtqueues, receive then send, |
| 911 | * and optionally control. */ |
| 912 | nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 913 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 914 | err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names); |
| 915 | if (err) |
| 916 | goto free; |
| 917 | |
| 918 | vi->rvq = vqs[0]; |
| 919 | vi->svq = vqs[1]; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 920 | |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 921 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 922 | vi->cvq = vqs[2]; |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 923 | |
| 924 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) |
| 925 | dev->features |= NETIF_F_HW_VLAN_FILTER; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 928 | /* Initialize our empty receive and send queues. */ |
| 929 | skb_queue_head_init(&vi->recv); |
| 930 | skb_queue_head_init(&vi->send); |
| 931 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 932 | tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); |
| 933 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 934 | if (!vi->free_in_tasklet) |
| 935 | setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 936 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 937 | err = register_netdev(dev); |
| 938 | if (err) { |
| 939 | pr_debug("virtio_net: registering device failed\n"); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 940 | goto free_vqs; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 941 | } |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 942 | |
| 943 | /* Last of all, set up some receive buffers. */ |
| 944 | try_fill_recv(vi); |
| 945 | |
| 946 | /* If we didn't even get one input buffer, we're useless. */ |
| 947 | if (vi->num == 0) { |
| 948 | err = -ENOMEM; |
| 949 | goto unregister; |
| 950 | } |
| 951 | |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 952 | vi->status = VIRTIO_NET_S_LINK_UP; |
| 953 | virtnet_update_status(vi); |
Pantelis Koukousoulas | 4783256 | 2009-03-18 18:40:02 -0700 | [diff] [blame] | 954 | netif_carrier_on(dev); |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 955 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 956 | pr_debug("virtnet: registered device %s\n", dev->name); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 957 | return 0; |
| 958 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 959 | unregister: |
| 960 | unregister_netdev(dev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 961 | free_vqs: |
| 962 | vdev->config->del_vqs(vdev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 963 | free: |
| 964 | free_netdev(dev); |
| 965 | return err; |
| 966 | } |
| 967 | |
| 968 | static void virtnet_remove(struct virtio_device *vdev) |
| 969 | { |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 970 | struct virtnet_info *vi = vdev->priv; |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 971 | struct sk_buff *skb; |
| 972 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 973 | /* Stop all the virtqueues. */ |
| 974 | vdev->config->reset(vdev); |
| 975 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 976 | if (!vi->free_in_tasklet) |
| 977 | del_timer_sync(&vi->xmit_free_timer); |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 978 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 979 | /* Free our skbs in send and recv queues, if any. */ |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 980 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
| 981 | kfree_skb(skb); |
| 982 | vi->num--; |
| 983 | } |
Wang Chen | 288369c | 2008-05-22 18:07:43 +0800 | [diff] [blame] | 984 | __skb_queue_purge(&vi->send); |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 985 | |
| 986 | BUG_ON(vi->num != 0); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 987 | |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 988 | unregister_netdev(vi->dev); |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 989 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 990 | vdev->config->del_vqs(vi->vdev); |
| 991 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 992 | while (vi->pages) |
| 993 | __free_pages(get_a_page(vi, GFP_KERNEL), 0); |
| 994 | |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 995 | free_netdev(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static struct virtio_device_id id_table[] = { |
| 999 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, |
| 1000 | { 0 }, |
| 1001 | }; |
| 1002 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 1003 | static unsigned int features[] = { |
Mark McLoughlin | 5e4fe5c | 2008-07-08 17:10:42 +1000 | [diff] [blame] | 1004 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, |
| 1005 | VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 1006 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 1007 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, |
| 1008 | VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */ |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 1009 | VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 1010 | VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 1011 | VIRTIO_F_NOTIFY_ON_EMPTY, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 1012 | }; |
| 1013 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1014 | static struct virtio_driver virtio_net = { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 1015 | .feature_table = features, |
| 1016 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1017 | .driver.name = KBUILD_MODNAME, |
| 1018 | .driver.owner = THIS_MODULE, |
| 1019 | .id_table = id_table, |
| 1020 | .probe = virtnet_probe, |
| 1021 | .remove = __devexit_p(virtnet_remove), |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 1022 | .config_changed = virtnet_config_changed, |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1023 | }; |
| 1024 | |
| 1025 | static int __init init(void) |
| 1026 | { |
| 1027 | return register_virtio_driver(&virtio_net); |
| 1028 | } |
| 1029 | |
| 1030 | static void __exit fini(void) |
| 1031 | { |
| 1032 | unregister_virtio_driver(&virtio_net); |
| 1033 | } |
| 1034 | module_init(init); |
| 1035 | module_exit(fini); |
| 1036 | |
| 1037 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 1038 | MODULE_DESCRIPTION("Virtio network driver"); |
| 1039 | MODULE_LICENSE("GPL"); |