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