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