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