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