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