Merge branch 'for-davem' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / drivers / net / hyperv / netvsc_drv.c
CommitLineData
fceaf24a 1/*
fceaf24a
HJ
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
adf8d3ff 14 * this program; if not, see <http://www.gnu.org/licenses/>.
fceaf24a
HJ
15 *
16 * Authors:
d0e94d17 17 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 18 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 19 */
eb335bc4
HJ
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
fceaf24a 22#include <linux/init.h>
9079ce69 23#include <linux/atomic.h>
fceaf24a
HJ
24#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
fceaf24a 27#include <linux/io.h>
fceaf24a
HJ
28#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
c802db11 33#include <linux/if_vlan.h>
fceaf24a 34#include <linux/in.h>
5a0e3ad6 35#include <linux/slab.h>
fceaf24a
HJ
36#include <net/arp.h>
37#include <net/route.h>
38#include <net/sock.h>
39#include <net/pkt_sched.h>
3f335ea2 40
5ca7252a 41#include "hyperv_net.h"
fceaf24a 42
fceaf24a 43struct net_device_context {
02fafbc6 44 /* point back to our device context */
6bad88da 45 struct hv_device *device_ctx;
122a5f64 46 struct delayed_work dwork;
792df872 47 struct work_struct work;
fceaf24a
HJ
48};
49
fa85a6c2 50#define RING_SIZE_MIN 64
99c8da0f 51static int ring_size = 128;
450d7a4b
SH
52module_param(ring_size, int, S_IRUGO);
53MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
fceaf24a 54
d426b2e3
HZ
55static void do_set_multicast(struct work_struct *w)
56{
792df872
WM
57 struct net_device_context *ndevctx =
58 container_of(w, struct net_device_context, work);
d426b2e3
HZ
59 struct netvsc_device *nvdev;
60 struct rndis_device *rdev;
61
62 nvdev = hv_get_drvdata(ndevctx->device_ctx);
792df872
WM
63 if (nvdev == NULL || nvdev->ndev == NULL)
64 return;
d426b2e3
HZ
65
66 rdev = nvdev->extension;
67 if (rdev == NULL)
792df872 68 return;
d426b2e3 69
792df872 70 if (nvdev->ndev->flags & IFF_PROMISC)
d426b2e3
HZ
71 rndis_filter_set_packet_filter(rdev,
72 NDIS_PACKET_TYPE_PROMISCUOUS);
73 else
74 rndis_filter_set_packet_filter(rdev,
75 NDIS_PACKET_TYPE_BROADCAST |
76 NDIS_PACKET_TYPE_ALL_MULTICAST |
77 NDIS_PACKET_TYPE_DIRECTED);
d426b2e3
HZ
78}
79
4e9bfefa 80static void netvsc_set_multicast_list(struct net_device *net)
fceaf24a 81{
792df872 82 struct net_device_context *net_device_ctx = netdev_priv(net);
d426b2e3 83
792df872 84 schedule_work(&net_device_ctx->work);
fceaf24a
HJ
85}
86
fceaf24a
HJ
87static int netvsc_open(struct net_device *net)
88{
fceaf24a 89 struct net_device_context *net_device_ctx = netdev_priv(net);
6bad88da 90 struct hv_device *device_obj = net_device_ctx->device_ctx;
891de74d
HZ
91 struct netvsc_device *nvdev;
92 struct rndis_device *rdev;
02fafbc6 93 int ret = 0;
fceaf24a 94
891de74d
HZ
95 netif_carrier_off(net);
96
d515d0ff
HZ
97 /* Open up the device */
98 ret = rndis_filter_open(device_obj);
99 if (ret != 0) {
100 netdev_err(net, "unable to open device (ret %d).\n", ret);
101 return ret;
fceaf24a
HJ
102 }
103
5b54dac8 104 netif_tx_start_all_queues(net);
d515d0ff 105
891de74d
HZ
106 nvdev = hv_get_drvdata(device_obj);
107 rdev = nvdev->extension;
108 if (!rdev->link_state)
109 netif_carrier_on(net);
110
fceaf24a
HJ
111 return ret;
112}
113
fceaf24a
HJ
114static int netvsc_close(struct net_device *net)
115{
fceaf24a 116 struct net_device_context *net_device_ctx = netdev_priv(net);
6bad88da 117 struct hv_device *device_obj = net_device_ctx->device_ctx;
02fafbc6 118 int ret;
fceaf24a 119
0a282538 120 netif_tx_disable(net);
fceaf24a 121
792df872
WM
122 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
123 cancel_work_sync(&net_device_ctx->work);
9c26aa0d 124 ret = rndis_filter_close(device_obj);
fceaf24a 125 if (ret != 0)
eb335bc4 126 netdev_err(net, "unable to close device (ret %d).\n", ret);
fceaf24a 127
fceaf24a
HJ
128 return ret;
129}
130
8a00251a
KS
131static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
132 int pkt_type)
133{
134 struct rndis_packet *rndis_pkt;
135 struct rndis_per_packet_info *ppi;
136
137 rndis_pkt = &msg->msg.pkt;
138 rndis_pkt->data_offset += ppi_size;
139
140 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
141 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
142
143 ppi->size = ppi_size;
144 ppi->type = pkt_type;
145 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
146
147 rndis_pkt->per_pkt_info_len += ppi_size;
148
149 return ppi;
150}
151
5b54dac8
HZ
152union sub_key {
153 u64 k;
154 struct {
155 u8 pad[3];
156 u8 kb;
157 u32 ka;
158 };
159};
160
161/* Toeplitz hash function
162 * data: network byte order
163 * return: host byte order
164 */
f88e6714 165static u32 comp_hash(u8 *key, int klen, void *data, int dlen)
5b54dac8
HZ
166{
167 union sub_key subk;
168 int k_next = 4;
169 u8 dt;
170 int i, j;
171 u32 ret = 0;
172
173 subk.k = 0;
174 subk.ka = ntohl(*(u32 *)key);
175
176 for (i = 0; i < dlen; i++) {
177 subk.kb = key[k_next];
178 k_next = (k_next + 1) % klen;
f88e6714 179 dt = ((u8 *)data)[i];
5b54dac8
HZ
180 for (j = 0; j < 8; j++) {
181 if (dt & 0x80)
182 ret ^= subk.ka;
183 dt <<= 1;
184 subk.k <<= 1;
185 }
186 }
187
188 return ret;
189}
190
191static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)
192{
f88e6714 193 struct flow_keys flow;
5b54dac8 194 int data_len;
5b54dac8 195
4c87454a
HZ
196 if (!skb_flow_dissect(skb, &flow) ||
197 !(flow.n_proto == htons(ETH_P_IP) ||
198 flow.n_proto == htons(ETH_P_IPV6)))
5b54dac8
HZ
199 return false;
200
f88e6714
HZ
201 if (flow.ip_proto == IPPROTO_TCP)
202 data_len = 12;
203 else
204 data_len = 8;
5b54dac8 205
f88e6714 206 *hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
5b54dac8 207
f88e6714 208 return true;
5b54dac8
HZ
209}
210
211static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
212 void *accel_priv, select_queue_fallback_t fallback)
213{
214 struct net_device_context *net_device_ctx = netdev_priv(ndev);
215 struct hv_device *hdev = net_device_ctx->device_ctx;
216 struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
217 u32 hash;
218 u16 q_idx = 0;
219
220 if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
221 return 0;
222
307f0995 223 if (netvsc_set_hash(&hash, skb)) {
5b54dac8
HZ
224 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
225 ndev->real_num_tx_queues;
307f0995
HZ
226 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
227 }
5b54dac8
HZ
228
229 return q_idx;
230}
231
ee90b812 232void netvsc_xmit_completion(void *context)
fceaf24a 233{
4193d4f4 234 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
02fafbc6 235 struct sk_buff *skb = (struct sk_buff *)
893f6627 236 (unsigned long)packet->send_completion_tid;
fceaf24a 237
b08cc791
KS
238 if (!packet->part_of_skb)
239 kfree(packet);
fceaf24a 240
cbacec76 241 if (skb)
b220f5f9 242 dev_kfree_skb_any(skb);
fceaf24a
HJ
243}
244
54a7357f
KS
245static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
246 struct hv_page_buffer *pb)
247{
248 int j = 0;
249
250 /* Deal with compund pages by ignoring unused part
251 * of the page.
252 */
253 page += (offset >> PAGE_SHIFT);
254 offset &= ~PAGE_MASK;
255
256 while (len > 0) {
257 unsigned long bytes;
258
259 bytes = PAGE_SIZE - offset;
260 if (bytes > len)
261 bytes = len;
262 pb[j].pfn = page_to_pfn(page);
263 pb[j].offset = offset;
264 pb[j].len = bytes;
265
266 offset += bytes;
267 len -= bytes;
268
269 if (offset == PAGE_SIZE && len) {
270 page++;
271 offset = 0;
272 j++;
273 }
274 }
275
276 return j + 1;
277}
278
8a00251a
KS
279static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
280 struct hv_page_buffer *pb)
54a7357f
KS
281{
282 u32 slots_used = 0;
283 char *data = skb->data;
284 int frags = skb_shinfo(skb)->nr_frags;
285 int i;
286
287 /* The packet is laid out thus:
288 * 1. hdr
289 * 2. skb linear data
290 * 3. skb fragment data
291 */
292 if (hdr != NULL)
293 slots_used += fill_pg_buf(virt_to_page(hdr),
294 offset_in_page(hdr),
295 len, &pb[slots_used]);
296
297 slots_used += fill_pg_buf(virt_to_page(data),
298 offset_in_page(data),
299 skb_headlen(skb), &pb[slots_used]);
300
301 for (i = 0; i < frags; i++) {
302 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
303
304 slots_used += fill_pg_buf(skb_frag_page(frag),
305 frag->page_offset,
306 skb_frag_size(frag), &pb[slots_used]);
307 }
8a00251a 308 return slots_used;
54a7357f
KS
309}
310
311static int count_skb_frag_slots(struct sk_buff *skb)
312{
313 int i, frags = skb_shinfo(skb)->nr_frags;
314 int pages = 0;
315
316 for (i = 0; i < frags; i++) {
317 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
318 unsigned long size = skb_frag_size(frag);
319 unsigned long offset = frag->page_offset;
320
321 /* Skip unused frames from start of page */
322 offset &= ~PAGE_MASK;
323 pages += PFN_UP(offset + size);
324 }
325 return pages;
326}
327
328static int netvsc_get_slots(struct sk_buff *skb)
329{
330 char *data = skb->data;
331 unsigned int offset = offset_in_page(data);
332 unsigned int len = skb_headlen(skb);
333 int slots;
334 int frag_slots;
335
336 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
337 frag_slots = count_skb_frag_slots(skb);
338 return slots + frag_slots;
339}
340
08cd04bf
KS
341static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
342{
343 u32 ret_val = TRANSPORT_INFO_NOT_IP;
344
345 if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
346 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
347 goto not_ip;
348 }
349
350 *trans_off = skb_transport_offset(skb);
351
352 if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
353 struct iphdr *iphdr = ip_hdr(skb);
354
355 if (iphdr->protocol == IPPROTO_TCP)
356 ret_val = TRANSPORT_INFO_IPV4_TCP;
357 else if (iphdr->protocol == IPPROTO_UDP)
358 ret_val = TRANSPORT_INFO_IPV4_UDP;
359 } else {
360 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
361 ret_val = TRANSPORT_INFO_IPV6_TCP;
362 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
363 ret_val = TRANSPORT_INFO_IPV6_UDP;
364 }
365
366not_ip:
367 return ret_val;
368}
369
02fafbc6 370static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
fceaf24a 371{
fceaf24a 372 struct net_device_context *net_device_ctx = netdev_priv(net);
981a1bd8 373 struct hv_netvsc_packet *packet = NULL;
02fafbc6 374 int ret;
8a00251a
KS
375 unsigned int num_data_pgs;
376 struct rndis_message *rndis_msg;
377 struct rndis_packet *rndis_pkt;
378 u32 rndis_msg_size;
379 bool isvlan;
e88f7e07 380 bool linear = false;
8a00251a 381 struct rndis_per_packet_info *ppi;
08cd04bf 382 struct ndis_tcp_ip_checksum_info *csum_info;
77bf5487 383 struct ndis_tcp_lso_info *lso_info;
08cd04bf
KS
384 int hdr_offset;
385 u32 net_trans_info;
307f0995 386 u32 hash;
e88f7e07
VK
387 u32 skb_length;
388 u32 head_room;
b08cc791
KS
389 u32 pkt_sz;
390 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
08cd04bf 391
fceaf24a 392
54a7357f
KS
393 /* We will atmost need two pages to describe the rndis
394 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
e88f7e07
VK
395 * of pages in a single packet. If skb is scattered around
396 * more pages we try linearizing it.
54a7357f 397 */
e88f7e07
VK
398
399check_size:
400 skb_length = skb->len;
401 head_room = skb_headroom(skb);
8a00251a 402 num_data_pgs = netvsc_get_slots(skb) + 2;
e88f7e07
VK
403 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
404 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
405 num_data_pgs, skb->len);
981a1bd8
VK
406 ret = -EFAULT;
407 goto drop;
e88f7e07
VK
408 } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
409 if (skb_linearize(skb)) {
410 net_alert_ratelimited("failed to linearize skb\n");
411 ret = -ENOMEM;
412 goto drop;
413 }
414 linear = true;
415 goto check_size;
54a7357f 416 }
fceaf24a 417
72151422 418 pkt_sz = sizeof(struct hv_netvsc_packet) + RNDIS_AND_PPI_SIZE;
b08cc791
KS
419
420 if (head_room < pkt_sz) {
421 packet = kmalloc(pkt_sz, GFP_ATOMIC);
422 if (!packet) {
423 /* out of memory, drop packet */
424 netdev_err(net, "unable to alloc hv_netvsc_packet\n");
981a1bd8
VK
425 ret = -ENOMEM;
426 goto drop;
b08cc791
KS
427 }
428 packet->part_of_skb = false;
429 } else {
430 /* Use the headroom for building up the packet */
431 packet = (struct hv_netvsc_packet *)skb->head;
432 packet->part_of_skb = true;
fceaf24a
HJ
433 }
434
b08cc791 435 packet->status = 0;
7c3877f2
HZ
436 packet->xmit_more = skb->xmit_more;
437
1f5f3a75 438 packet->vlan_tci = skb->vlan_tci;
b08cc791 439 packet->page_buf = page_buf;
1f5f3a75 440
5b54dac8
HZ
441 packet->q_idx = skb_get_queue_mapping(skb);
442
8a00251a 443 packet->is_data_pkt = true;
4d447c9a 444 packet->total_data_buflen = skb->len;
fceaf24a 445
8a00251a 446 packet->rndis_msg = (struct rndis_message *)((unsigned long)packet +
b08cc791
KS
447 sizeof(struct hv_netvsc_packet));
448
72151422 449 memset(packet->rndis_msg, 0, RNDIS_AND_PPI_SIZE);
fceaf24a 450
454f18a9 451 /* Set the completion routine */
893f6627
HZ
452 packet->send_completion = netvsc_xmit_completion;
453 packet->send_completion_ctx = packet;
454 packet->send_completion_tid = (unsigned long)skb;
fceaf24a 455
8a00251a
KS
456 isvlan = packet->vlan_tci & VLAN_TAG_PRESENT;
457
458 /* Add the rndis header */
459 rndis_msg = packet->rndis_msg;
460 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
461 rndis_msg->msg_len = packet->total_data_buflen;
462 rndis_pkt = &rndis_msg->msg.pkt;
463 rndis_pkt->data_offset = sizeof(struct rndis_packet);
464 rndis_pkt->data_len = packet->total_data_buflen;
465 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
466
467 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
468
307f0995
HZ
469 hash = skb_get_hash_raw(skb);
470 if (hash != 0 && net->real_num_tx_queues > 1) {
471 rndis_msg_size += NDIS_HASH_PPI_SIZE;
472 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
473 NBL_HASH_VALUE);
474 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
475 }
476
8a00251a
KS
477 if (isvlan) {
478 struct ndis_pkt_8021q_info *vlan;
479
480 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
481 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
482 IEEE_8021Q_INFO);
483 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
484 ppi->ppi_offset);
485 vlan->vlanid = packet->vlan_tci & VLAN_VID_MASK;
486 vlan->pri = (packet->vlan_tci & VLAN_PRIO_MASK) >>
487 VLAN_PRIO_SHIFT;
488 }
489
08cd04bf
KS
490 net_trans_info = get_net_transport_info(skb, &hdr_offset);
491 if (net_trans_info == TRANSPORT_INFO_NOT_IP)
492 goto do_send;
493
494 /*
495 * Setup the sendside checksum offload only if this is not a
496 * GSO packet.
497 */
498 if (skb_is_gso(skb))
77bf5487 499 goto do_lso;
08cd04bf 500
22041fb0
KS
501 if ((skb->ip_summed == CHECKSUM_NONE) ||
502 (skb->ip_summed == CHECKSUM_UNNECESSARY))
503 goto do_send;
504
08cd04bf
KS
505 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
506 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
507 TCPIP_CHKSUM_PKTINFO);
508
509 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
510 ppi->ppi_offset);
511
512 if (net_trans_info & (INFO_IPV4 << 16))
513 csum_info->transmit.is_ipv4 = 1;
514 else
515 csum_info->transmit.is_ipv6 = 1;
516
517 if (net_trans_info & INFO_TCP) {
518 csum_info->transmit.tcp_checksum = 1;
519 csum_info->transmit.tcp_header_offset = hdr_offset;
520 } else if (net_trans_info & INFO_UDP) {
af9893a3
KS
521 /* UDP checksum offload is not supported on ws2008r2.
522 * Furthermore, on ws2012 and ws2012r2, there are some
523 * issues with udp checksum offload from Linux guests.
524 * (these are host issues).
525 * For now compute the checksum here.
526 */
527 struct udphdr *uh;
528 u16 udp_len;
529
530 ret = skb_cow_head(skb, 0);
531 if (ret)
532 goto drop;
533
534 uh = udp_hdr(skb);
535 udp_len = ntohs(uh->len);
536 uh->check = 0;
537 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
538 ip_hdr(skb)->daddr,
539 udp_len, IPPROTO_UDP,
540 csum_partial(uh, udp_len, 0));
541 if (uh->check == 0)
542 uh->check = CSUM_MANGLED_0;
543
544 csum_info->transmit.udp_checksum = 0;
08cd04bf 545 }
77bf5487
KS
546 goto do_send;
547
548do_lso:
549 rndis_msg_size += NDIS_LSO_PPI_SIZE;
550 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
551 TCP_LARGESEND_PKTINFO);
552
553 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
554 ppi->ppi_offset);
555
556 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
557 if (net_trans_info & (INFO_IPV4 << 16)) {
558 lso_info->lso_v2_transmit.ip_version =
559 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
560 ip_hdr(skb)->tot_len = 0;
561 ip_hdr(skb)->check = 0;
562 tcp_hdr(skb)->check =
563 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
564 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
565 } else {
566 lso_info->lso_v2_transmit.ip_version =
567 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
568 ipv6_hdr(skb)->payload_len = 0;
569 tcp_hdr(skb)->check =
570 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
571 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
572 }
573 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
574 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
08cd04bf
KS
575
576do_send:
8a00251a
KS
577 /* Start filling in the page buffers with the rndis hdr */
578 rndis_msg->msg_len += rndis_msg_size;
942396b0 579 packet->total_data_buflen = rndis_msg->msg_len;
8a00251a 580 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
b08cc791 581 skb, &page_buf[0]);
8a00251a
KS
582
583 ret = netvsc_send(net_device_ctx->device_ctx, packet);
584
af9893a3 585drop:
02fafbc6 586 if (ret == 0) {
dedb845d 587 net->stats.tx_bytes += skb_length;
b852fdce 588 net->stats.tx_packets++;
b220f5f9 589 } else {
981a1bd8 590 if (packet && !packet->part_of_skb)
b08cc791 591 kfree(packet);
33be96e4
HZ
592 if (ret != -EAGAIN) {
593 dev_kfree_skb_any(skb);
594 net->stats.tx_dropped++;
595 }
fceaf24a
HJ
596 }
597
33be96e4 598 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
fceaf24a
HJ
599}
600
3e189519 601/*
02fafbc6
GKH
602 * netvsc_linkstatus_callback - Link up/down notification
603 */
90ef117a 604void netvsc_linkstatus_callback(struct hv_device *device_obj,
3a494e71 605 struct rndis_message *resp)
fceaf24a 606{
3a494e71 607 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
2ddd5e5f 608 struct net_device *net;
c996edcf 609 struct net_device_context *ndev_ctx;
2ddd5e5f 610 struct netvsc_device *net_device;
891de74d 611 struct rndis_device *rdev;
2ddd5e5f
S
612
613 net_device = hv_get_drvdata(device_obj);
891de74d
HZ
614 rdev = net_device->extension;
615
3a494e71
HZ
616 switch (indicate->status) {
617 case RNDIS_STATUS_MEDIA_CONNECT:
618 rdev->link_state = false;
619 break;
620 case RNDIS_STATUS_MEDIA_DISCONNECT:
621 rdev->link_state = true;
622 break;
623 case RNDIS_STATUS_NETWORK_CHANGE:
624 rdev->link_change = true;
625 break;
626 default:
627 return;
628 }
891de74d 629
2ddd5e5f 630 net = net_device->ndev;
fceaf24a 631
891de74d 632 if (!net || net->reg_state != NETREG_REGISTERED)
fceaf24a 633 return;
fceaf24a 634
891de74d 635 ndev_ctx = netdev_priv(net);
3a494e71 636 if (!rdev->link_state) {
c4b6a2ea 637 schedule_delayed_work(&ndev_ctx->dwork, 0);
122a5f64 638 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
02fafbc6 639 } else {
891de74d 640 schedule_delayed_work(&ndev_ctx->dwork, 0);
fceaf24a 641 }
fceaf24a
HJ
642}
643
3e189519
HJ
644/*
645 * netvsc_recv_callback - Callback when we receive a packet from the
646 * "wire" on the specified device.
02fafbc6 647 */
f79adf8f 648int netvsc_recv_callback(struct hv_device *device_obj,
e3d605ed
KS
649 struct hv_netvsc_packet *packet,
650 struct ndis_tcp_ip_checksum_info *csum_info)
fceaf24a 651{
6f4c4446 652 struct net_device *net;
fceaf24a 653 struct sk_buff *skb;
fceaf24a 654
6f4c4446 655 net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
a68f9614 656 if (!net || net->reg_state != NETREG_REGISTERED) {
63f6921d 657 packet->status = NVSP_STAT_FAIL;
fceaf24a
HJ
658 return 0;
659 }
660
9495c282 661 /* Allocate a skb - TODO direct I/O to pages? */
72a2f5bd 662 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
9495c282
SH
663 if (unlikely(!skb)) {
664 ++net->stats.rx_dropped;
63f6921d 665 packet->status = NVSP_STAT_FAIL;
9495c282
SH
666 return 0;
667 }
fceaf24a 668
02fafbc6
GKH
669 /*
670 * Copy to skb. This copy is needed here since the memory pointed by
671 * hv_netvsc_packet cannot be deallocated
672 */
45326342
HZ
673 memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
674 packet->total_data_buflen);
fceaf24a
HJ
675
676 skb->protocol = eth_type_trans(skb, net);
e3d605ed
KS
677 if (csum_info) {
678 /* We only look at the IP checksum here.
679 * Should we be dropping the packet if checksum
680 * failed? How do we deal with other checksums - TCP/UDP?
681 */
682 if (csum_info->receive.ip_checksum_succeeded)
683 skb->ip_summed = CHECKSUM_UNNECESSARY;
684 else
685 skb->ip_summed = CHECKSUM_NONE;
686 }
687
93725cbd
HZ
688 if (packet->vlan_tci & VLAN_TAG_PRESENT)
689 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
690 packet->vlan_tci);
fceaf24a 691
4baab261 692 skb_record_rx_queue(skb, packet->channel->
e565e803 693 offermsg.offer.sub_channel_index);
5b54dac8 694
9495c282 695 net->stats.rx_packets++;
48c38839 696 net->stats.rx_bytes += packet->total_data_buflen;
9495c282 697
02fafbc6
GKH
698 /*
699 * Pass the skb back up. Network stack will deallocate the skb when it
9495c282
SH
700 * is done.
701 * TODO - use NAPI?
02fafbc6 702 */
9495c282 703 netif_rx(skb);
fceaf24a 704
fceaf24a
HJ
705 return 0;
706}
707
f82f4ad7
SH
708static void netvsc_get_drvinfo(struct net_device *net,
709 struct ethtool_drvinfo *info)
710{
7826d43f 711 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
7826d43f 712 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
f82f4ad7
SH
713}
714
59995370
AS
715static void netvsc_get_channels(struct net_device *net,
716 struct ethtool_channels *channel)
717{
718 struct net_device_context *net_device_ctx = netdev_priv(net);
719 struct hv_device *dev = net_device_ctx->device_ctx;
720 struct netvsc_device *nvdev = hv_get_drvdata(dev);
721
722 if (nvdev) {
723 channel->max_combined = nvdev->max_chn;
724 channel->combined_count = nvdev->num_chn;
725 }
726}
727
4d447c9a
HZ
728static int netvsc_change_mtu(struct net_device *ndev, int mtu)
729{
730 struct net_device_context *ndevctx = netdev_priv(ndev);
731 struct hv_device *hdev = ndevctx->device_ctx;
732 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
733 struct netvsc_device_info device_info;
734 int limit = ETH_DATA_LEN;
735
736 if (nvdev == NULL || nvdev->destroy)
737 return -ENODEV;
738
a1eabb01 739 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
4d3c9d37 740 limit = NETVSC_MTU - ETH_HLEN;
4d447c9a 741
4d3c9d37
HZ
742 /* Hyper-V hosts don't support MTU < ETH_DATA_LEN (1500) */
743 if (mtu < ETH_DATA_LEN || mtu > limit)
4d447c9a
HZ
744 return -EINVAL;
745
746 nvdev->start_remove = true;
792df872 747 cancel_work_sync(&ndevctx->work);
0a282538 748 netif_tx_disable(ndev);
4d447c9a
HZ
749 rndis_filter_device_remove(hdev);
750
751 ndev->mtu = mtu;
752
753 ndevctx->device_ctx = hdev;
754 hv_set_drvdata(hdev, ndev);
755 device_info.ring_size = ring_size;
756 rndis_filter_device_add(hdev, &device_info);
5b54dac8 757 netif_tx_wake_all_queues(ndev);
4d447c9a
HZ
758
759 return 0;
760}
761
1ce09e89
HZ
762
763static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
764{
765 struct net_device_context *ndevctx = netdev_priv(ndev);
766 struct hv_device *hdev = ndevctx->device_ctx;
767 struct sockaddr *addr = p;
9a4c831e 768 char save_adr[ETH_ALEN];
1ce09e89
HZ
769 unsigned char save_aatype;
770 int err;
771
772 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
773 save_aatype = ndev->addr_assign_type;
774
775 err = eth_mac_addr(ndev, p);
776 if (err != 0)
777 return err;
778
779 err = rndis_filter_set_device_mac(hdev, addr->sa_data);
780 if (err != 0) {
781 /* roll back to saved MAC */
782 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
783 ndev->addr_assign_type = save_aatype;
784 }
785
786 return err;
787}
788
316158fe
RW
789#ifdef CONFIG_NET_POLL_CONTROLLER
790static void netvsc_poll_controller(struct net_device *net)
791{
792 /* As netvsc_start_xmit() works synchronous we don't have to
793 * trigger anything here.
794 */
795}
796#endif
1ce09e89 797
f82f4ad7
SH
798static const struct ethtool_ops ethtool_ops = {
799 .get_drvinfo = netvsc_get_drvinfo,
f82f4ad7 800 .get_link = ethtool_op_get_link,
59995370 801 .get_channels = netvsc_get_channels,
f82f4ad7
SH
802};
803
df2fff28
GKH
804static const struct net_device_ops device_ops = {
805 .ndo_open = netvsc_open,
806 .ndo_stop = netvsc_close,
807 .ndo_start_xmit = netvsc_start_xmit,
afc4b13d 808 .ndo_set_rx_mode = netvsc_set_multicast_list,
4d447c9a 809 .ndo_change_mtu = netvsc_change_mtu,
b681b588 810 .ndo_validate_addr = eth_validate_addr,
1ce09e89 811 .ndo_set_mac_address = netvsc_set_mac_addr,
5b54dac8 812 .ndo_select_queue = netvsc_select_queue,
316158fe
RW
813#ifdef CONFIG_NET_POLL_CONTROLLER
814 .ndo_poll_controller = netvsc_poll_controller,
815#endif
df2fff28
GKH
816};
817
c996edcf
HZ
818/*
819 * Send GARP packet to network peers after migrations.
820 * After Quick Migration, the network is not immediately operational in the
821 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
122a5f64 822 * another netif_notify_peers() into a delayed work, otherwise GARP packet
c996edcf 823 * will not be sent after quick migration, and cause network disconnection.
891de74d 824 * Also, we update the carrier status here.
c996edcf 825 */
891de74d 826static void netvsc_link_change(struct work_struct *w)
c996edcf
HZ
827{
828 struct net_device_context *ndev_ctx;
829 struct net_device *net;
2ddd5e5f 830 struct netvsc_device *net_device;
891de74d 831 struct rndis_device *rdev;
3a494e71
HZ
832 bool notify, refresh = false;
833 char *argv[] = { "/etc/init.d/network", "restart", NULL };
834 char *envp[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
891de74d
HZ
835
836 rtnl_lock();
c996edcf 837
122a5f64 838 ndev_ctx = container_of(w, struct net_device_context, dwork.work);
2ddd5e5f 839 net_device = hv_get_drvdata(ndev_ctx->device_ctx);
891de74d 840 rdev = net_device->extension;
2ddd5e5f 841 net = net_device->ndev;
891de74d
HZ
842
843 if (rdev->link_state) {
844 netif_carrier_off(net);
845 notify = false;
846 } else {
847 netif_carrier_on(net);
848 notify = true;
3a494e71
HZ
849 if (rdev->link_change) {
850 rdev->link_change = false;
851 refresh = true;
852 }
891de74d
HZ
853 }
854
855 rtnl_unlock();
856
3a494e71
HZ
857 if (refresh)
858 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
859
891de74d
HZ
860 if (notify)
861 netdev_notify_peers(net);
c996edcf
HZ
862}
863
864
84946899
S
865static int netvsc_probe(struct hv_device *dev,
866 const struct hv_vmbus_device_id *dev_id)
df2fff28 867{
df2fff28
GKH
868 struct net_device *net = NULL;
869 struct net_device_context *net_device_ctx;
870 struct netvsc_device_info device_info;
5b54dac8 871 struct netvsc_device *nvdev;
df2fff28 872 int ret;
b08cc791 873 u32 max_needed_headroom;
df2fff28 874
5b54dac8
HZ
875 net = alloc_etherdev_mq(sizeof(struct net_device_context),
876 num_online_cpus());
df2fff28 877 if (!net)
51a805d0 878 return -ENOMEM;
df2fff28 879
b08cc791 880 max_needed_headroom = sizeof(struct hv_netvsc_packet) +
72151422 881 RNDIS_AND_PPI_SIZE;
b08cc791 882
1b07da51
HZ
883 netif_carrier_off(net);
884
df2fff28 885 net_device_ctx = netdev_priv(net);
9efd21e1 886 net_device_ctx->device_ctx = dev;
2ddd5e5f 887 hv_set_drvdata(dev, net);
891de74d 888 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
792df872 889 INIT_WORK(&net_device_ctx->work, do_set_multicast);
df2fff28 890
df2fff28
GKH
891 net->netdev_ops = &device_ops;
892
77bf5487
KS
893 net->hw_features = NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_IP_CSUM |
894 NETIF_F_TSO;
08cd04bf 895 net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_RXCSUM |
77bf5487 896 NETIF_F_IP_CSUM | NETIF_F_TSO;
6048718d 897
7ad24ea4 898 net->ethtool_ops = &ethtool_ops;
9efd21e1 899 SET_NETDEV_DEV(net, &dev->device);
df2fff28 900
b08cc791
KS
901 /*
902 * Request additional head room in the skb.
903 * We will use this space to build the rndis
904 * heaser and other state we need to maintain.
905 */
906 net->needed_headroom = max_needed_headroom;
907
692e084e
HZ
908 /* Notify the netvsc driver of the new device */
909 device_info.ring_size = ring_size;
910 ret = rndis_filter_device_add(dev, &device_info);
911 if (ret != 0) {
912 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
df2fff28 913 free_netdev(net);
2ddd5e5f 914 hv_set_drvdata(dev, NULL);
692e084e 915 return ret;
df2fff28 916 }
692e084e
HZ
917 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
918
5b54dac8
HZ
919 nvdev = hv_get_drvdata(dev);
920 netif_set_real_num_tx_queues(net, nvdev->num_chn);
921 netif_set_real_num_rx_queues(net, nvdev->num_chn);
5b54dac8 922
a68f9614
HZ
923 ret = register_netdev(net);
924 if (ret != 0) {
925 pr_err("Unable to register netdev.\n");
926 rndis_filter_device_remove(dev);
927 free_netdev(net);
1b07da51
HZ
928 } else {
929 schedule_delayed_work(&net_device_ctx->dwork, 0);
a68f9614
HZ
930 }
931
df2fff28
GKH
932 return ret;
933}
934
415b023a 935static int netvsc_remove(struct hv_device *dev)
df2fff28 936{
2ddd5e5f 937 struct net_device *net;
122a5f64 938 struct net_device_context *ndev_ctx;
2ddd5e5f
S
939 struct netvsc_device *net_device;
940
941 net_device = hv_get_drvdata(dev);
942 net = net_device->ndev;
df2fff28 943
df2fff28 944 if (net == NULL) {
415b023a 945 dev_err(&dev->device, "No net device to remove\n");
df2fff28
GKH
946 return 0;
947 }
948
4d447c9a
HZ
949 net_device->start_remove = true;
950
122a5f64
HZ
951 ndev_ctx = netdev_priv(net);
952 cancel_delayed_work_sync(&ndev_ctx->dwork);
792df872 953 cancel_work_sync(&ndev_ctx->work);
122a5f64 954
df2fff28 955 /* Stop outbound asap */
0a282538 956 netif_tx_disable(net);
df2fff28
GKH
957
958 unregister_netdev(net);
959
960 /*
961 * Call to the vsc driver to let it know that the device is being
962 * removed
963 */
df06bcff 964 rndis_filter_device_remove(dev);
df2fff28
GKH
965
966 free_netdev(net);
df06bcff 967 return 0;
df2fff28
GKH
968}
969
345c4cc3 970static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4 971 /* Network guid */
8f505944 972 { HV_NIC_GUID, },
c45cf2d4 973 { },
345c4cc3
S
974};
975
976MODULE_DEVICE_TABLE(vmbus, id_table);
977
f1542a66 978/* The one and only one */
fde0ef9b 979static struct hv_driver netvsc_drv = {
d31b20fc 980 .name = KBUILD_MODNAME,
345c4cc3 981 .id_table = id_table,
fde0ef9b
S
982 .probe = netvsc_probe,
983 .remove = netvsc_remove,
d4890970 984};
f1542a66 985
a9869c94 986static void __exit netvsc_drv_exit(void)
fceaf24a 987{
768fa219 988 vmbus_driver_unregister(&netvsc_drv);
fceaf24a
HJ
989}
990
1fde28cf 991static int __init netvsc_drv_init(void)
df2fff28 992{
fa85a6c2
HZ
993 if (ring_size < RING_SIZE_MIN) {
994 ring_size = RING_SIZE_MIN;
995 pr_info("Increased ring_size to %d (min allowed)\n",
996 ring_size);
997 }
768fa219 998 return vmbus_driver_register(&netvsc_drv);
df2fff28
GKH
999}
1000
26c14cc1 1001MODULE_LICENSE("GPL");
7880fc54 1002MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
fceaf24a 1003
1fde28cf 1004module_init(netvsc_drv_init);
a9869c94 1005module_exit(netvsc_drv_exit);