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