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