44d8d6fa93dc78c0ef52256c112a9b515ee1cb69
[firefly-linux-kernel-4.4.55.git] / drivers / staging / hv / netvsc_drv.c
1 /*
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
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/highmem.h>
24 #include <linux/device.h>
25 #include <linux/io.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/inetdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/in.h>
32 #include <net/arp.h>
33 #include <net/route.h>
34 #include <net/sock.h>
35 #include <net/pkt_sched.h>
36 #include "osd.h"
37 #include "logging.h"
38 #include "vmbus.h"
39 #include "NetVscApi.h"
40
41 MODULE_LICENSE("GPL");
42
43 struct net_device_context {
44         /* point back to our device context */
45         struct device_context *device_ctx;
46         struct net_device_stats stats;
47         struct work_struct work;
48 };
49
50 struct netvsc_driver_context {
51         /* !! These must be the first 2 fields !! */
52         /* Which is a bug FIXME! */
53         struct driver_context drv_ctx;
54         struct netvsc_driver drv_obj;
55 };
56
57 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
58
59 /* The one and only one */
60 static struct netvsc_driver_context g_netvsc_drv;
61
62 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
63 {
64         struct net_device_context *net_device_ctx = netdev_priv(net);
65
66         return &net_device_ctx->stats;
67 }
68
69 static void netvsc_set_multicast_list(struct net_device *net)
70 {
71 }
72
73 static int netvsc_open(struct net_device *net)
74 {
75         struct net_device_context *net_device_ctx = netdev_priv(net);
76         struct driver_context *driver_ctx =
77             driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
78         struct netvsc_driver_context *net_drv_ctx =
79                 (struct netvsc_driver_context *)driver_ctx;
80         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
81         struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
82         int ret = 0;
83
84         DPRINT_ENTER(NETVSC_DRV);
85
86         if (netif_carrier_ok(net)) {
87                 memset(&net_device_ctx->stats, 0,
88                        sizeof(struct net_device_stats));
89
90                 /* Open up the device */
91                 ret = net_drv_obj->OnOpen(device_obj);
92                 if (ret != 0) {
93                         DPRINT_ERR(NETVSC_DRV,
94                                    "unable to open device (ret %d).", ret);
95                         return ret;
96                 }
97
98                 netif_start_queue(net);
99         } else {
100                 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
101         }
102
103         DPRINT_EXIT(NETVSC_DRV);
104         return ret;
105 }
106
107 static int netvsc_close(struct net_device *net)
108 {
109         struct net_device_context *net_device_ctx = netdev_priv(net);
110         struct driver_context *driver_ctx =
111             driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
112         struct netvsc_driver_context *net_drv_ctx =
113                 (struct netvsc_driver_context *)driver_ctx;
114         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
115         struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
116         int ret;
117
118         DPRINT_ENTER(NETVSC_DRV);
119
120         netif_stop_queue(net);
121
122         ret = net_drv_obj->OnClose(device_obj);
123         if (ret != 0)
124                 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
125
126         DPRINT_EXIT(NETVSC_DRV);
127
128         return ret;
129 }
130
131 static void netvsc_xmit_completion(void *context)
132 {
133         struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
134         struct sk_buff *skb = (struct sk_buff *)
135                 (unsigned long)packet->Completion.Send.SendCompletionTid;
136         struct net_device *net;
137
138         DPRINT_ENTER(NETVSC_DRV);
139
140         kfree(packet);
141
142         if (skb) {
143                 net = skb->dev;
144                 dev_kfree_skb_any(skb);
145
146                 if (netif_queue_stopped(net)) {
147                         DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...",
148                                     net);
149
150                         netif_wake_queue(net);
151                 }
152         }
153
154         DPRINT_EXIT(NETVSC_DRV);
155 }
156
157 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
158 {
159         struct net_device_context *net_device_ctx = netdev_priv(net);
160         struct driver_context *driver_ctx =
161             driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
162         struct netvsc_driver_context *net_drv_ctx =
163                 (struct netvsc_driver_context *)driver_ctx;
164         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
165         struct hv_netvsc_packet *packet;
166         int i;
167         int ret;
168         int num_frags;
169         int retries = 0;
170
171         DPRINT_ENTER(NETVSC_DRV);
172
173         /* Support only 1 chain of frags */
174         ASSERT(skb_shinfo(skb)->frag_list == NULL);
175         ASSERT(skb->dev == net);
176
177         DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
178                    skb->len, skb->data_len);
179
180         /* Add 1 for skb->data and any additional ones requested */
181         num_frags = skb_shinfo(skb)->nr_frags + 1 +
182                     net_drv_obj->AdditionalRequestPageBufferCount;
183
184         /* Allocate a netvsc packet based on # of frags. */
185         packet = kzalloc(sizeof(struct hv_netvsc_packet) +
186                          (num_frags * sizeof(struct hv_page_buffer)) +
187                          net_drv_obj->RequestExtSize, GFP_ATOMIC);
188         if (!packet) {
189                 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
190                 return -1;
191         }
192
193         packet->Extension = (void *)(unsigned long)packet +
194                                 sizeof(struct hv_netvsc_packet) +
195                                     (num_frags * sizeof(struct hv_page_buffer));
196
197         /* Setup the rndis header */
198         packet->PageBufferCount = num_frags;
199
200         /* TODO: Flush all write buffers/ memory fence ??? */
201         /* wmb(); */
202
203         /* Initialize it from the skb */
204         ASSERT(skb->data);
205         packet->TotalDataBufferLength   = skb->len;
206
207         /*
208          * Start filling in the page buffers starting at
209          * AdditionalRequestPageBufferCount offset
210          */
211         packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
212         packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE - 1);
213         packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len;
214
215         ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
216
217         for (i = net_drv_obj->AdditionalRequestPageBufferCount + 1;
218              i < num_frags; i++) {
219                 packet->PageBuffers[i].Pfn =
220                         page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
221                 packet->PageBuffers[i].Offset =
222                         skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
223                 packet->PageBuffers[i].Length =
224                         skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
225         }
226
227         /* Set the completion routine */
228         packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
229         packet->Completion.Send.SendCompletionContext = packet;
230         packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
231
232 retry_send:
233         ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
234                                   packet);
235
236         if (ret == 0) {
237                 ret = NETDEV_TX_OK;
238                 net_device_ctx->stats.tx_bytes += skb->len;
239                 net_device_ctx->stats.tx_packets++;
240         } else {
241                 retries++;
242                 if (retries < 4) {
243                         DPRINT_ERR(NETVSC_DRV, "unable to send..."
244                                         "retrying %d...", retries);
245                         udelay(100);
246                         goto retry_send;
247                 }
248
249                 /* no more room or we are shutting down */
250                 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)..."
251                            "marking net device (%p) busy", ret, net);
252                 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
253
254                 ret = NETDEV_TX_BUSY;
255                 net_device_ctx->stats.tx_dropped++;
256
257                 netif_stop_queue(net);
258
259                 /*
260                  * Null it since the caller will free it instead of the
261                  * completion routine
262                  */
263                 packet->Completion.Send.SendCompletionTid = 0;
264
265                 /*
266                  * Release the resources since we will not get any send
267                  * completion
268                  */
269                 netvsc_xmit_completion((void *)packet);
270         }
271
272         DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
273                    net_device_ctx->stats.tx_packets,
274                    net_device_ctx->stats.tx_bytes);
275
276         DPRINT_EXIT(NETVSC_DRV);
277         return ret;
278 }
279
280 /**
281  * netvsc_linkstatus_callback - Link up/down notification
282  */
283 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
284                                        unsigned int status)
285 {
286         struct device_context *device_ctx = to_device_context(device_obj);
287         struct net_device *net = dev_get_drvdata(&device_ctx->device);
288         struct net_device_context *ndev_ctx;
289
290         DPRINT_ENTER(NETVSC_DRV);
291
292         if (!net) {
293                 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
294                                 "not initialized yet");
295                 return;
296         }
297
298         if (status == 1) {
299                 netif_carrier_on(net);
300                 netif_wake_queue(net);
301                 netif_notify_peers(net);
302                 ndev_ctx = netdev_priv(net);
303                 schedule_work(&ndev_ctx->work);
304         } else {
305                 netif_carrier_off(net);
306                 netif_stop_queue(net);
307         }
308         DPRINT_EXIT(NETVSC_DRV);
309 }
310
311 /**
312  * netvsc_recv_callback -  Callback when we receive a packet from the "wire" on the specified device.
313  */
314 static int netvsc_recv_callback(struct hv_device *device_obj,
315                                 struct hv_netvsc_packet *packet)
316 {
317         struct device_context *device_ctx = to_device_context(device_obj);
318         struct net_device *net = dev_get_drvdata(&device_ctx->device);
319         struct net_device_context *net_device_ctx;
320         struct sk_buff *skb;
321         void *data;
322         int ret;
323         int i;
324         unsigned long flags;
325
326         DPRINT_ENTER(NETVSC_DRV);
327
328         if (!net) {
329                 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
330                                 "not initialized yet");
331                 return 0;
332         }
333
334         net_device_ctx = netdev_priv(net);
335
336         /* Allocate a skb - TODO preallocate this */
337         /* Pad 2-bytes to align IP header to 16 bytes */
338         skb = dev_alloc_skb(packet->TotalDataBufferLength + 2);
339         ASSERT(skb);
340         skb_reserve(skb, 2);
341         skb->dev = net;
342
343         /* for kmap_atomic */
344         local_irq_save(flags);
345
346         /*
347          * Copy to skb. This copy is needed here since the memory pointed by
348          * hv_netvsc_packet cannot be deallocated
349          */
350         for (i = 0; i < packet->PageBufferCount; i++) {
351                 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
352                                                KM_IRQ1);
353                 data = (void *)(unsigned long)data +
354                                 packet->PageBuffers[i].Offset;
355
356                 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
357                        packet->PageBuffers[i].Length);
358
359                 kunmap_atomic((void *)((unsigned long)data -
360                                        packet->PageBuffers[i].Offset), KM_IRQ1);
361         }
362
363         local_irq_restore(flags);
364
365         skb->protocol = eth_type_trans(skb, net);
366
367         skb->ip_summed = CHECKSUM_NONE;
368
369         /*
370          * Pass the skb back up. Network stack will deallocate the skb when it
371          * is done
372          */
373         ret = netif_rx(skb);
374
375         switch (ret) {
376         case NET_RX_DROP:
377                 net_device_ctx->stats.rx_dropped++;
378                 break;
379         default:
380                 net_device_ctx->stats.rx_packets++;
381                 net_device_ctx->stats.rx_bytes += skb->len;
382                 break;
383
384         }
385         DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
386                    net_device_ctx->stats.rx_packets,
387                    net_device_ctx->stats.rx_bytes);
388
389         DPRINT_EXIT(NETVSC_DRV);
390
391         return 0;
392 }
393
394 static const struct net_device_ops device_ops = {
395         .ndo_open =                     netvsc_open,
396         .ndo_stop =                     netvsc_close,
397         .ndo_start_xmit =               netvsc_start_xmit,
398         .ndo_get_stats =                netvsc_get_stats,
399         .ndo_set_multicast_list =       netvsc_set_multicast_list,
400         .ndo_change_mtu =               eth_change_mtu,
401         .ndo_validate_addr =            eth_validate_addr,
402         .ndo_set_mac_address =          eth_mac_addr,
403 };
404
405 /*
406  * Send GARP packet to network peers after migrations.
407  * After Quick Migration, the network is not immediately operational in the
408  * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
409  * another netif_notify_peers() into a scheduled work, otherwise GARP packet
410  * will not be sent after quick migration, and cause network disconnection.
411  */
412 static void netvsc_send_garp(struct work_struct *w)
413 {
414         struct net_device_context *ndev_ctx;
415         struct net_device *net;
416
417         msleep(20);
418         ndev_ctx = container_of(w, struct net_device_context, work);
419         net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
420         netif_notify_peers(net);
421 }
422
423
424 static int netvsc_probe(struct device *device)
425 {
426         struct driver_context *driver_ctx =
427                 driver_to_driver_context(device->driver);
428         struct netvsc_driver_context *net_drv_ctx =
429                 (struct netvsc_driver_context *)driver_ctx;
430         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
431         struct device_context *device_ctx = device_to_device_context(device);
432         struct hv_device *device_obj = &device_ctx->device_obj;
433         struct net_device *net = NULL;
434         struct net_device_context *net_device_ctx;
435         struct netvsc_device_info device_info;
436         int ret;
437
438         DPRINT_ENTER(NETVSC_DRV);
439
440         if (!net_drv_obj->Base.OnDeviceAdd)
441                 return -1;
442
443         net = alloc_etherdev(sizeof(struct net_device_context));
444         if (!net)
445                 return -1;
446
447         /* Set initial state */
448         netif_carrier_off(net);
449         netif_stop_queue(net);
450
451         net_device_ctx = netdev_priv(net);
452         net_device_ctx->device_ctx = device_ctx;
453         dev_set_drvdata(device, net);
454         INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
455
456         /* Notify the netvsc driver of the new device */
457         ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
458         if (ret != 0) {
459                 free_netdev(net);
460                 dev_set_drvdata(device, NULL);
461
462                 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
463                            ret);
464                 return ret;
465         }
466
467         /*
468          * If carrier is still off ie we did not get a link status callback,
469          * update it if necessary
470          */
471         /*
472          * FIXME: We should use a atomic or test/set instead to avoid getting
473          * out of sync with the device's link status
474          */
475         if (!netif_carrier_ok(net))
476                 if (!device_info.LinkState)
477                         netif_carrier_on(net);
478
479         memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
480
481         net->netdev_ops = &device_ops;
482
483         SET_NETDEV_DEV(net, device);
484
485         ret = register_netdev(net);
486         if (ret != 0) {
487                 /* Remove the device and release the resource */
488                 net_drv_obj->Base.OnDeviceRemove(device_obj);
489                 free_netdev(net);
490         }
491
492         DPRINT_EXIT(NETVSC_DRV);
493         return ret;
494 }
495
496 static int netvsc_remove(struct device *device)
497 {
498         struct driver_context *driver_ctx =
499                 driver_to_driver_context(device->driver);
500         struct netvsc_driver_context *net_drv_ctx =
501                 (struct netvsc_driver_context *)driver_ctx;
502         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
503         struct device_context *device_ctx = device_to_device_context(device);
504         struct net_device *net = dev_get_drvdata(&device_ctx->device);
505         struct hv_device *device_obj = &device_ctx->device_obj;
506         int ret;
507
508         DPRINT_ENTER(NETVSC_DRV);
509
510         if (net == NULL) {
511                 DPRINT_INFO(NETVSC, "no net device to remove");
512                 DPRINT_EXIT(NETVSC_DRV);
513                 return 0;
514         }
515
516         if (!net_drv_obj->Base.OnDeviceRemove) {
517                 DPRINT_EXIT(NETVSC_DRV);
518                 return -1;
519         }
520
521         /* Stop outbound asap */
522         netif_stop_queue(net);
523         /* netif_carrier_off(net); */
524
525         unregister_netdev(net);
526
527         /*
528          * Call to the vsc driver to let it know that the device is being
529          * removed
530          */
531         ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
532         if (ret != 0) {
533                 /* TODO: */
534                 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
535         }
536
537         free_netdev(net);
538         DPRINT_EXIT(NETVSC_DRV);
539         return ret;
540 }
541
542 static int netvsc_drv_exit_cb(struct device *dev, void *data)
543 {
544         struct device **curr = (struct device **)data;
545
546         *curr = dev;
547         /* stop iterating */
548         return 1;
549 }
550
551 static void netvsc_drv_exit(void)
552 {
553         struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
554         struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
555         struct device *current_dev;
556         int ret;
557
558         DPRINT_ENTER(NETVSC_DRV);
559
560         while (1) {
561                 current_dev = NULL;
562
563                 /* Get the device */
564                 ret = driver_for_each_device(&drv_ctx->driver, NULL,
565                                              &current_dev, netvsc_drv_exit_cb);
566                 if (ret)
567                         DPRINT_WARN(NETVSC_DRV,
568                                     "driver_for_each_device returned %d", ret);
569
570                 if (current_dev == NULL)
571                         break;
572
573                 /* Initiate removal from the top-down */
574                 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
575                             current_dev);
576
577                 device_unregister(current_dev);
578         }
579
580         if (netvsc_drv_obj->Base.OnCleanup)
581                 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
582
583         vmbus_child_driver_unregister(drv_ctx);
584
585         DPRINT_EXIT(NETVSC_DRV);
586
587         return;
588 }
589
590 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
591 {
592         struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
593         struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
594         int ret;
595
596         DPRINT_ENTER(NETVSC_DRV);
597
598         vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
599
600         net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
601         net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
602         net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
603
604         /* Callback to client driver to complete the initialization */
605         drv_init(&net_drv_obj->Base);
606
607         drv_ctx->driver.name = net_drv_obj->Base.name;
608         memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
609                sizeof(struct hv_guid));
610
611         drv_ctx->probe = netvsc_probe;
612         drv_ctx->remove = netvsc_remove;
613
614         /* The driver belongs to vmbus */
615         ret = vmbus_child_driver_register(drv_ctx);
616
617         DPRINT_EXIT(NETVSC_DRV);
618
619         return ret;
620 }
621
622 static int __init netvsc_init(void)
623 {
624         int ret;
625
626         DPRINT_ENTER(NETVSC_DRV);
627         DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
628
629         ret = netvsc_drv_init(NetVscInitialize);
630
631         DPRINT_EXIT(NETVSC_DRV);
632
633         return ret;
634 }
635
636 static void __exit netvsc_exit(void)
637 {
638         DPRINT_ENTER(NETVSC_DRV);
639         netvsc_drv_exit();
640         DPRINT_EXIT(NETVSC_DRV);
641 }
642
643 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
644
645 module_init(netvsc_init);
646 module_exit(netvsc_exit);