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