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 | */ |
eb335bc4 HJ |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
5654e932 | 23 | #include <linux/kernel.h> |
0c3b7b2f S |
24 | #include <linux/sched.h> |
25 | #include <linux/wait.h> | |
0ffa63b0 | 26 | #include <linux/mm.h> |
b4362c9c | 27 | #include <linux/delay.h> |
21a80820 | 28 | #include <linux/io.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
e3fe0bb6 | 30 | #include "hv_api.h" |
645954c5 | 31 | #include "logging.h" |
af167ae9 | 32 | #include "netvsc.h" |
043efcc3 | 33 | #include "rndis_filter.h" |
314bf1d1 | 34 | #include "channel.h" |
fceaf24a HJ |
35 | |
36 | ||
454f18a9 | 37 | /* Globals */ |
85799a37 | 38 | static const char *driver_name = "netvsc"; |
fceaf24a | 39 | |
454f18a9 | 40 | /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */ |
85799a37 | 41 | static const struct hv_guid netvsc_device_type = { |
caf26a31 GKH |
42 | .data = { |
43 | 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, | |
44 | 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E | |
45 | } | |
fceaf24a HJ |
46 | }; |
47 | ||
48 | ||
5a71ae30 | 49 | static struct netvsc_device *alloc_net_device(struct hv_device *device) |
fceaf24a | 50 | { |
85799a37 | 51 | struct netvsc_device *net_device; |
fceaf24a | 52 | |
85799a37 HZ |
53 | net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL); |
54 | if (!net_device) | |
fceaf24a HJ |
55 | return NULL; |
56 | ||
454f18a9 | 57 | /* Set to 2 to allow both inbound and outbound traffic */ |
53d21fdb | 58 | atomic_cmpxchg(&net_device->refcnt, 0, 2); |
fceaf24a | 59 | |
53d21fdb | 60 | net_device->dev = device; |
ca623ad3 | 61 | device->ext = net_device; |
fceaf24a | 62 | |
85799a37 | 63 | return net_device; |
fceaf24a HJ |
64 | } |
65 | ||
5a71ae30 | 66 | static void free_net_device(struct netvsc_device *device) |
fceaf24a | 67 | { |
31acaa50 | 68 | WARN_ON(atomic_read(&device->refcnt) != 0); |
ca623ad3 | 69 | device->dev->ext = NULL; |
85799a37 | 70 | kfree(device); |
fceaf24a HJ |
71 | } |
72 | ||
73 | ||
454f18a9 | 74 | /* Get the net device object iff exists and its refcount > 1 */ |
5a71ae30 | 75 | static struct netvsc_device *get_outbound_net_device(struct hv_device *device) |
fceaf24a | 76 | { |
85799a37 | 77 | struct netvsc_device *net_device; |
fceaf24a | 78 | |
ca623ad3 | 79 | net_device = device->ext; |
53d21fdb HZ |
80 | if (net_device && atomic_read(&net_device->refcnt) > 1) |
81 | atomic_inc(&net_device->refcnt); | |
fceaf24a | 82 | else |
85799a37 | 83 | net_device = NULL; |
fceaf24a | 84 | |
85799a37 | 85 | return net_device; |
fceaf24a HJ |
86 | } |
87 | ||
454f18a9 | 88 | /* Get the net device object iff exists and its refcount > 0 */ |
5a71ae30 | 89 | static struct netvsc_device *get_inbound_net_device(struct hv_device *device) |
fceaf24a | 90 | { |
85799a37 | 91 | struct netvsc_device *net_device; |
fceaf24a | 92 | |
ca623ad3 | 93 | net_device = device->ext; |
53d21fdb HZ |
94 | if (net_device && atomic_read(&net_device->refcnt)) |
95 | atomic_inc(&net_device->refcnt); | |
fceaf24a | 96 | else |
85799a37 | 97 | net_device = NULL; |
fceaf24a | 98 | |
85799a37 | 99 | return net_device; |
fceaf24a HJ |
100 | } |
101 | ||
5a71ae30 | 102 | static void put_net_device(struct hv_device *device) |
fceaf24a | 103 | { |
85799a37 | 104 | struct netvsc_device *net_device; |
fceaf24a | 105 | |
ca623ad3 | 106 | net_device = device->ext; |
fceaf24a | 107 | |
53d21fdb | 108 | atomic_dec(&net_device->refcnt); |
fceaf24a HJ |
109 | } |
110 | ||
5a71ae30 HZ |
111 | static struct netvsc_device *release_outbound_net_device( |
112 | struct hv_device *device) | |
fceaf24a | 113 | { |
85799a37 | 114 | struct netvsc_device *net_device; |
fceaf24a | 115 | |
ca623ad3 | 116 | net_device = device->ext; |
85799a37 | 117 | if (net_device == NULL) |
fceaf24a HJ |
118 | return NULL; |
119 | ||
454f18a9 | 120 | /* Busy wait until the ref drop to 2, then set it to 1 */ |
53d21fdb | 121 | while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2) |
b4362c9c | 122 | udelay(100); |
fceaf24a | 123 | |
85799a37 | 124 | return net_device; |
fceaf24a HJ |
125 | } |
126 | ||
5a71ae30 HZ |
127 | static struct netvsc_device *release_inbound_net_device( |
128 | struct hv_device *device) | |
fceaf24a | 129 | { |
85799a37 | 130 | struct netvsc_device *net_device; |
fceaf24a | 131 | |
ca623ad3 | 132 | net_device = device->ext; |
85799a37 | 133 | if (net_device == NULL) |
fceaf24a HJ |
134 | return NULL; |
135 | ||
454f18a9 | 136 | /* Busy wait until the ref drop to 1, then set it to 0 */ |
53d21fdb | 137 | while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1) |
b4362c9c | 138 | udelay(100); |
fceaf24a | 139 | |
ca623ad3 | 140 | device->ext = NULL; |
85799a37 | 141 | return net_device; |
fceaf24a HJ |
142 | } |
143 | ||
ec91cd09 HZ |
144 | static int netvsc_destroy_recv_buf(struct netvsc_device *net_device) |
145 | { | |
146 | struct nvsp_message *revoke_packet; | |
147 | int ret = 0; | |
148 | ||
149 | /* | |
150 | * If we got a section count, it means we received a | |
151 | * SendReceiveBufferComplete msg (ie sent | |
152 | * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need | |
153 | * to send a revoke msg here | |
154 | */ | |
155 | if (net_device->recv_section_cnt) { | |
156 | /* Send the revoke receive buffer */ | |
157 | revoke_packet = &net_device->revoke_packet; | |
158 | memset(revoke_packet, 0, sizeof(struct nvsp_message)); | |
159 | ||
160 | revoke_packet->hdr.msg_type = | |
161 | NVSP_MSG1_TYPE_REVOKE_RECV_BUF; | |
162 | revoke_packet->msg.v1_msg. | |
163 | revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; | |
164 | ||
165 | ret = vmbus_sendpacket(net_device->dev->channel, | |
166 | revoke_packet, | |
167 | sizeof(struct nvsp_message), | |
168 | (unsigned long)revoke_packet, | |
169 | VM_PKT_DATA_INBAND, 0); | |
170 | /* | |
171 | * If we failed here, we might as well return and | |
172 | * have a leak rather than continue and a bugchk | |
173 | */ | |
174 | if (ret != 0) { | |
175 | dev_err(&net_device->dev->device, "unable to send " | |
176 | "revoke receive buffer to netvsp"); | |
177 | return -1; | |
178 | } | |
179 | } | |
180 | ||
181 | /* Teardown the gpadl on the vsp end */ | |
182 | if (net_device->recv_buf_gpadl_handle) { | |
183 | ret = vmbus_teardown_gpadl(net_device->dev->channel, | |
184 | net_device->recv_buf_gpadl_handle); | |
185 | ||
186 | /* If we failed here, we might as well return and have a leak | |
187 | * rather than continue and a bugchk | |
188 | */ | |
189 | if (ret != 0) { | |
190 | dev_err(&net_device->dev->device, | |
191 | "unable to teardown receive buffer's gpadl"); | |
192 | return -1; | |
193 | } | |
194 | net_device->recv_buf_gpadl_handle = 0; | |
195 | } | |
196 | ||
197 | if (net_device->recv_buf) { | |
198 | /* Free up the receive buffer */ | |
199 | free_pages((unsigned long)net_device->recv_buf, | |
200 | get_order(net_device->recv_buf_size)); | |
201 | net_device->recv_buf = NULL; | |
202 | } | |
203 | ||
204 | if (net_device->recv_section) { | |
205 | net_device->recv_section_cnt = 0; | |
206 | kfree(net_device->recv_section); | |
207 | net_device->recv_section = NULL; | |
208 | } | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
5a71ae30 | 213 | static int netvsc_init_recv_buf(struct hv_device *device) |
fceaf24a | 214 | { |
21a80820 | 215 | int ret = 0; |
85799a37 HZ |
216 | struct netvsc_device *net_device; |
217 | struct nvsp_message *init_packet; | |
fceaf24a | 218 | |
5a71ae30 | 219 | net_device = get_outbound_net_device(device); |
85799a37 | 220 | if (!net_device) { |
eb335bc4 | 221 | dev_err(&device->device, "unable to get net device..." |
21a80820 | 222 | "device being destroyed?"); |
fceaf24a HJ |
223 | return -1; |
224 | } | |
fceaf24a | 225 | |
53d21fdb | 226 | net_device->recv_buf = |
df3493e0 S |
227 | (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, |
228 | get_order(net_device->recv_buf_size)); | |
53d21fdb | 229 | if (!net_device->recv_buf) { |
eb335bc4 HJ |
230 | dev_err(&device->device, "unable to allocate receive " |
231 | "buffer of size %d", net_device->recv_buf_size); | |
fceaf24a | 232 | ret = -1; |
0c3b7b2f | 233 | goto cleanup; |
fceaf24a | 234 | } |
fceaf24a | 235 | |
454f18a9 BP |
236 | /* |
237 | * Establish the gpadl handle for this buffer on this | |
238 | * channel. Note: This call uses the vmbus connection rather | |
239 | * than the channel to establish the gpadl handle. | |
240 | */ | |
53d21fdb HZ |
241 | ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf, |
242 | net_device->recv_buf_size, | |
243 | &net_device->recv_buf_gpadl_handle); | |
21a80820 | 244 | if (ret != 0) { |
eb335bc4 HJ |
245 | dev_err(&device->device, |
246 | "unable to establish receive buffer's gpadl"); | |
0c3b7b2f | 247 | goto cleanup; |
fceaf24a HJ |
248 | } |
249 | ||
fceaf24a | 250 | |
454f18a9 | 251 | /* Notify the NetVsp of the gpadl handle */ |
53d21fdb | 252 | init_packet = &net_device->channel_init_pkt; |
fceaf24a | 253 | |
85799a37 | 254 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
fceaf24a | 255 | |
53d21fdb HZ |
256 | init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF; |
257 | init_packet->msg.v1_msg.send_recv_buf. | |
258 | gpadl_handle = net_device->recv_buf_gpadl_handle; | |
259 | init_packet->msg.v1_msg. | |
260 | send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; | |
fceaf24a | 261 | |
454f18a9 | 262 | /* Send the gpadl notification request */ |
0c3b7b2f | 263 | net_device->wait_condition = 0; |
85799a37 | 264 | ret = vmbus_sendpacket(device->channel, init_packet, |
5a4df290 | 265 | sizeof(struct nvsp_message), |
85799a37 | 266 | (unsigned long)init_packet, |
415f2287 | 267 | VM_PKT_DATA_INBAND, |
5a4df290 | 268 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
21a80820 | 269 | if (ret != 0) { |
eb335bc4 HJ |
270 | dev_err(&device->device, |
271 | "unable to send receive buffer's gpadl to netvsp"); | |
0c3b7b2f | 272 | goto cleanup; |
fceaf24a HJ |
273 | } |
274 | ||
0c3b7b2f S |
275 | wait_event_timeout(net_device->channel_init_wait, |
276 | net_device->wait_condition, | |
277 | msecs_to_jiffies(1000)); | |
278 | BUG_ON(net_device->wait_condition == 0); | |
279 | ||
fceaf24a | 280 | |
454f18a9 | 281 | /* Check the response */ |
53d21fdb HZ |
282 | if (init_packet->msg.v1_msg. |
283 | send_recv_buf_complete.status != NVSP_STAT_SUCCESS) { | |
eb335bc4 | 284 | dev_err(&device->device, "Unable to complete receive buffer " |
21a80820 | 285 | "initialzation with NetVsp - status %d", |
53d21fdb HZ |
286 | init_packet->msg.v1_msg. |
287 | send_recv_buf_complete.status); | |
fceaf24a | 288 | ret = -1; |
0c3b7b2f | 289 | goto cleanup; |
fceaf24a HJ |
290 | } |
291 | ||
454f18a9 | 292 | /* Parse the response */ |
fceaf24a | 293 | |
53d21fdb HZ |
294 | net_device->recv_section_cnt = init_packet->msg. |
295 | v1_msg.send_recv_buf_complete.num_sections; | |
fceaf24a | 296 | |
53d21fdb | 297 | net_device->recv_section = kmalloc(net_device->recv_section_cnt |
85799a37 | 298 | * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL); |
53d21fdb | 299 | if (net_device->recv_section == NULL) { |
fceaf24a | 300 | ret = -1; |
0c3b7b2f | 301 | goto cleanup; |
fceaf24a HJ |
302 | } |
303 | ||
53d21fdb HZ |
304 | memcpy(net_device->recv_section, |
305 | init_packet->msg.v1_msg. | |
306 | send_recv_buf_complete.sections, | |
307 | net_device->recv_section_cnt * | |
85799a37 | 308 | sizeof(struct nvsp_1_receive_buffer_section)); |
fceaf24a | 309 | |
21a80820 GKH |
310 | /* |
311 | * For 1st release, there should only be 1 section that represents the | |
312 | * entire receive buffer | |
313 | */ | |
53d21fdb HZ |
314 | if (net_device->recv_section_cnt != 1 || |
315 | net_device->recv_section->offset != 0) { | |
fceaf24a | 316 | ret = -1; |
0c3b7b2f | 317 | goto cleanup; |
fceaf24a HJ |
318 | } |
319 | ||
0c3b7b2f | 320 | goto exit; |
fceaf24a | 321 | |
0c3b7b2f | 322 | cleanup: |
5a71ae30 | 323 | netvsc_destroy_recv_buf(net_device); |
fceaf24a | 324 | |
0c3b7b2f | 325 | exit: |
5a71ae30 | 326 | put_net_device(device); |
fceaf24a HJ |
327 | return ret; |
328 | } | |
329 | ||
0021e71e HZ |
330 | static int netvsc_destroy_send_buf(struct netvsc_device *net_device) |
331 | { | |
332 | struct nvsp_message *revoke_packet; | |
333 | int ret = 0; | |
334 | ||
335 | /* | |
336 | * If we got a section count, it means we received a | |
337 | * SendReceiveBufferComplete msg (ie sent | |
338 | * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need | |
339 | * to send a revoke msg here | |
340 | */ | |
341 | if (net_device->send_section_size) { | |
342 | /* Send the revoke send buffer */ | |
343 | revoke_packet = &net_device->revoke_packet; | |
344 | memset(revoke_packet, 0, sizeof(struct nvsp_message)); | |
345 | ||
346 | revoke_packet->hdr.msg_type = | |
347 | NVSP_MSG1_TYPE_REVOKE_SEND_BUF; | |
348 | revoke_packet->msg.v1_msg. | |
349 | revoke_send_buf.id = NETVSC_SEND_BUFFER_ID; | |
350 | ||
351 | ret = vmbus_sendpacket(net_device->dev->channel, | |
352 | revoke_packet, | |
353 | sizeof(struct nvsp_message), | |
354 | (unsigned long)revoke_packet, | |
355 | VM_PKT_DATA_INBAND, 0); | |
356 | /* | |
357 | * If we failed here, we might as well return and have a leak | |
358 | * rather than continue and a bugchk | |
359 | */ | |
360 | if (ret != 0) { | |
361 | dev_err(&net_device->dev->device, "unable to send " | |
362 | "revoke send buffer to netvsp"); | |
363 | return -1; | |
364 | } | |
365 | } | |
366 | ||
367 | /* Teardown the gpadl on the vsp end */ | |
368 | if (net_device->send_buf_gpadl_handle) { | |
369 | ret = vmbus_teardown_gpadl(net_device->dev->channel, | |
370 | net_device->send_buf_gpadl_handle); | |
371 | ||
372 | /* | |
373 | * If we failed here, we might as well return and have a leak | |
374 | * rather than continue and a bugchk | |
375 | */ | |
376 | if (ret != 0) { | |
377 | dev_err(&net_device->dev->device, | |
378 | "unable to teardown send buffer's gpadl"); | |
379 | return -1; | |
380 | } | |
381 | net_device->send_buf_gpadl_handle = 0; | |
382 | } | |
383 | ||
384 | if (net_device->send_buf) { | |
385 | /* Free up the receive buffer */ | |
386 | free_pages((unsigned long)net_device->send_buf, | |
387 | get_order(net_device->send_buf_size)); | |
388 | net_device->send_buf = NULL; | |
389 | } | |
390 | ||
391 | return ret; | |
392 | } | |
393 | ||
5a71ae30 | 394 | static int netvsc_init_send_buf(struct hv_device *device) |
fceaf24a | 395 | { |
21a80820 | 396 | int ret = 0; |
85799a37 HZ |
397 | struct netvsc_device *net_device; |
398 | struct nvsp_message *init_packet; | |
fceaf24a | 399 | |
5a71ae30 | 400 | net_device = get_outbound_net_device(device); |
85799a37 | 401 | if (!net_device) { |
eb335bc4 | 402 | dev_err(&device->device, "unable to get net device..." |
21a80820 | 403 | "device being destroyed?"); |
fceaf24a HJ |
404 | return -1; |
405 | } | |
53d21fdb | 406 | if (net_device->send_buf_size <= 0) { |
79069684 | 407 | ret = -EINVAL; |
0c3b7b2f | 408 | goto cleanup; |
79069684 BP |
409 | } |
410 | ||
53d21fdb | 411 | net_device->send_buf = |
df3493e0 S |
412 | (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, |
413 | get_order(net_device->send_buf_size)); | |
53d21fdb | 414 | if (!net_device->send_buf) { |
eb335bc4 HJ |
415 | dev_err(&device->device, "unable to allocate send " |
416 | "buffer of size %d", net_device->send_buf_size); | |
fceaf24a | 417 | ret = -1; |
0c3b7b2f | 418 | goto cleanup; |
fceaf24a | 419 | } |
fceaf24a | 420 | |
454f18a9 BP |
421 | /* |
422 | * Establish the gpadl handle for this buffer on this | |
423 | * channel. Note: This call uses the vmbus connection rather | |
424 | * than the channel to establish the gpadl handle. | |
425 | */ | |
53d21fdb HZ |
426 | ret = vmbus_establish_gpadl(device->channel, net_device->send_buf, |
427 | net_device->send_buf_size, | |
428 | &net_device->send_buf_gpadl_handle); | |
21a80820 | 429 | if (ret != 0) { |
eb335bc4 | 430 | dev_err(&device->device, "unable to establish send buffer's gpadl"); |
0c3b7b2f | 431 | goto cleanup; |
fceaf24a HJ |
432 | } |
433 | ||
454f18a9 | 434 | /* Notify the NetVsp of the gpadl handle */ |
53d21fdb | 435 | init_packet = &net_device->channel_init_pkt; |
fceaf24a | 436 | |
85799a37 | 437 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
fceaf24a | 438 | |
53d21fdb HZ |
439 | init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF; |
440 | init_packet->msg.v1_msg.send_recv_buf. | |
441 | gpadl_handle = net_device->send_buf_gpadl_handle; | |
442 | init_packet->msg.v1_msg.send_recv_buf.id = | |
85799a37 | 443 | NETVSC_SEND_BUFFER_ID; |
fceaf24a | 444 | |
454f18a9 | 445 | /* Send the gpadl notification request */ |
0c3b7b2f | 446 | net_device->wait_condition = 0; |
85799a37 | 447 | ret = vmbus_sendpacket(device->channel, init_packet, |
5a4df290 | 448 | sizeof(struct nvsp_message), |
85799a37 | 449 | (unsigned long)init_packet, |
415f2287 | 450 | VM_PKT_DATA_INBAND, |
5a4df290 | 451 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
21a80820 | 452 | if (ret != 0) { |
eb335bc4 | 453 | dev_err(&device->device, |
21a80820 | 454 | "unable to send receive buffer's gpadl to netvsp"); |
0c3b7b2f | 455 | goto cleanup; |
fceaf24a HJ |
456 | } |
457 | ||
0c3b7b2f S |
458 | wait_event_timeout(net_device->channel_init_wait, |
459 | net_device->wait_condition, | |
460 | msecs_to_jiffies(1000)); | |
461 | BUG_ON(net_device->wait_condition == 0); | |
fceaf24a | 462 | |
454f18a9 | 463 | /* Check the response */ |
53d21fdb HZ |
464 | if (init_packet->msg.v1_msg. |
465 | send_send_buf_complete.status != NVSP_STAT_SUCCESS) { | |
eb335bc4 | 466 | dev_err(&device->device, "Unable to complete send buffer " |
21a80820 | 467 | "initialzation with NetVsp - status %d", |
53d21fdb HZ |
468 | init_packet->msg.v1_msg. |
469 | send_send_buf_complete.status); | |
fceaf24a | 470 | ret = -1; |
0c3b7b2f | 471 | goto cleanup; |
fceaf24a HJ |
472 | } |
473 | ||
53d21fdb HZ |
474 | net_device->send_section_size = init_packet-> |
475 | msg.v1_msg.send_send_buf_complete.section_size; | |
fceaf24a | 476 | |
0c3b7b2f | 477 | goto exit; |
fceaf24a | 478 | |
0c3b7b2f | 479 | cleanup: |
5a71ae30 | 480 | netvsc_destroy_send_buf(net_device); |
fceaf24a | 481 | |
0c3b7b2f | 482 | exit: |
5a71ae30 | 483 | put_net_device(device); |
fceaf24a HJ |
484 | return ret; |
485 | } | |
486 | ||
fceaf24a | 487 | |
5a71ae30 | 488 | static int netvsc_connect_vsp(struct hv_device *device) |
fceaf24a | 489 | { |
21a80820 | 490 | int ret; |
85799a37 HZ |
491 | struct netvsc_device *net_device; |
492 | struct nvsp_message *init_packet; | |
493 | int ndis_version; | |
fceaf24a | 494 | |
5a71ae30 | 495 | net_device = get_outbound_net_device(device); |
85799a37 | 496 | if (!net_device) { |
eb335bc4 | 497 | dev_err(&device->device, "unable to get net device..." |
21a80820 | 498 | "device being destroyed?"); |
fceaf24a HJ |
499 | return -1; |
500 | } | |
501 | ||
53d21fdb | 502 | init_packet = &net_device->channel_init_pkt; |
fceaf24a | 503 | |
85799a37 | 504 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
53d21fdb HZ |
505 | init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT; |
506 | init_packet->msg.init_msg.init.min_protocol_ver = | |
85799a37 | 507 | NVSP_MIN_PROTOCOL_VERSION; |
53d21fdb | 508 | init_packet->msg.init_msg.init.max_protocol_ver = |
85799a37 | 509 | NVSP_MAX_PROTOCOL_VERSION; |
fceaf24a | 510 | |
454f18a9 | 511 | /* Send the init request */ |
0c3b7b2f | 512 | net_device->wait_condition = 0; |
85799a37 | 513 | ret = vmbus_sendpacket(device->channel, init_packet, |
5a4df290 | 514 | sizeof(struct nvsp_message), |
85799a37 | 515 | (unsigned long)init_packet, |
415f2287 | 516 | VM_PKT_DATA_INBAND, |
5a4df290 | 517 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
21a80820 | 518 | |
b8a3d52b | 519 | if (ret != 0) |
0c3b7b2f | 520 | goto cleanup; |
fceaf24a | 521 | |
0c3b7b2f S |
522 | wait_event_timeout(net_device->channel_init_wait, |
523 | net_device->wait_condition, | |
524 | msecs_to_jiffies(1000)); | |
525 | if (net_device->wait_condition == 0) { | |
526 | ret = -ETIMEDOUT; | |
527 | goto cleanup; | |
528 | } | |
fceaf24a | 529 | |
53d21fdb HZ |
530 | if (init_packet->msg.init_msg.init_complete.status != |
531 | NVSP_STAT_SUCCESS) { | |
fceaf24a | 532 | ret = -1; |
0c3b7b2f | 533 | goto cleanup; |
fceaf24a HJ |
534 | } |
535 | ||
53d21fdb HZ |
536 | if (init_packet->msg.init_msg.init_complete. |
537 | negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) { | |
fceaf24a | 538 | ret = -1; |
0c3b7b2f | 539 | goto cleanup; |
fceaf24a | 540 | } |
454f18a9 | 541 | /* Send the ndis version */ |
85799a37 | 542 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
fceaf24a | 543 | |
85799a37 | 544 | ndis_version = 0x00050000; |
fceaf24a | 545 | |
53d21fdb HZ |
546 | init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER; |
547 | init_packet->msg.v1_msg. | |
548 | send_ndis_ver.ndis_major_ver = | |
85799a37 | 549 | (ndis_version & 0xFFFF0000) >> 16; |
53d21fdb HZ |
550 | init_packet->msg.v1_msg. |
551 | send_ndis_ver.ndis_minor_ver = | |
85799a37 | 552 | ndis_version & 0xFFFF; |
fceaf24a | 553 | |
454f18a9 | 554 | /* Send the init request */ |
85799a37 | 555 | ret = vmbus_sendpacket(device->channel, init_packet, |
0c3b7b2f S |
556 | sizeof(struct nvsp_message), |
557 | (unsigned long)init_packet, | |
558 | VM_PKT_DATA_INBAND, 0); | |
21a80820 | 559 | if (ret != 0) { |
fceaf24a | 560 | ret = -1; |
0c3b7b2f | 561 | goto cleanup; |
fceaf24a | 562 | } |
454f18a9 BP |
563 | |
564 | /* Post the big receive buffer to NetVSP */ | |
5a71ae30 | 565 | ret = netvsc_init_recv_buf(device); |
fceaf24a | 566 | if (ret == 0) |
5a71ae30 | 567 | ret = netvsc_init_send_buf(device); |
fceaf24a | 568 | |
0c3b7b2f | 569 | cleanup: |
5a71ae30 | 570 | put_net_device(device); |
fceaf24a HJ |
571 | return ret; |
572 | } | |
573 | ||
648dc598 | 574 | static void netvsc_disconnect_vsp(struct netvsc_device *net_device) |
fceaf24a | 575 | { |
5a71ae30 HZ |
576 | netvsc_destroy_recv_buf(net_device); |
577 | netvsc_destroy_send_buf(net_device); | |
fceaf24a HJ |
578 | } |
579 | ||
3e189519 | 580 | /* |
5a71ae30 | 581 | * netvsc_device_remove - Callback when the root bus device is removed |
21a80820 | 582 | */ |
5a71ae30 | 583 | static int netvsc_device_remove(struct hv_device *device) |
fceaf24a | 584 | { |
85799a37 HZ |
585 | struct netvsc_device *net_device; |
586 | struct hv_netvsc_packet *netvsc_packet, *pos; | |
fceaf24a | 587 | |
454f18a9 | 588 | /* Stop outbound traffic ie sends and receives completions */ |
5a71ae30 | 589 | net_device = release_outbound_net_device(device); |
85799a37 | 590 | if (!net_device) { |
eb335bc4 | 591 | dev_err(&device->device, "No net device present!!"); |
fceaf24a HJ |
592 | return -1; |
593 | } | |
594 | ||
454f18a9 | 595 | /* Wait for all send completions */ |
53d21fdb | 596 | while (atomic_read(&net_device->num_outstanding_sends)) { |
eb335bc4 HJ |
597 | dev_err(&device->device, |
598 | "waiting for %d requests to complete...", | |
599 | atomic_read(&net_device->num_outstanding_sends)); | |
b4362c9c | 600 | udelay(100); |
fceaf24a HJ |
601 | } |
602 | ||
648dc598 | 603 | netvsc_disconnect_vsp(net_device); |
fceaf24a | 604 | |
454f18a9 | 605 | /* Stop inbound traffic ie receives and sends completions */ |
5a71ae30 | 606 | net_device = release_inbound_net_device(device); |
fceaf24a | 607 | |
454f18a9 | 608 | /* At this point, no one should be accessing netDevice except in here */ |
eb335bc4 | 609 | dev_notice(&device->device, "net device safe to remove"); |
fceaf24a | 610 | |
454f18a9 | 611 | /* Now, we can close the channel safely */ |
85799a37 | 612 | vmbus_close(device->channel); |
fceaf24a | 613 | |
454f18a9 | 614 | /* Release all resources */ |
85799a37 | 615 | list_for_each_entry_safe(netvsc_packet, pos, |
53d21fdb | 616 | &net_device->recv_pkt_list, list_ent) { |
72a2f5bd | 617 | list_del(&netvsc_packet->list_ent); |
85799a37 | 618 | kfree(netvsc_packet); |
fceaf24a HJ |
619 | } |
620 | ||
5a71ae30 | 621 | free_net_device(net_device); |
21a80820 | 622 | return 0; |
fceaf24a HJ |
623 | } |
624 | ||
3e189519 | 625 | /* |
5a71ae30 | 626 | * netvsc_cleanup - Perform any cleanup when the driver is removed |
21a80820 | 627 | */ |
5a71ae30 | 628 | static void netvsc_cleanup(struct hv_driver *drv) |
fceaf24a | 629 | { |
fceaf24a HJ |
630 | } |
631 | ||
5a71ae30 | 632 | static void netvsc_send_completion(struct hv_device *device, |
85799a37 | 633 | struct vmpacket_descriptor *packet) |
fceaf24a | 634 | { |
85799a37 HZ |
635 | struct netvsc_device *net_device; |
636 | struct nvsp_message *nvsp_packet; | |
637 | struct hv_netvsc_packet *nvsc_packet; | |
fceaf24a | 638 | |
5a71ae30 | 639 | net_device = get_inbound_net_device(device); |
85799a37 | 640 | if (!net_device) { |
eb335bc4 | 641 | dev_err(&device->device, "unable to get net device..." |
21a80820 | 642 | "device being destroyed?"); |
fceaf24a HJ |
643 | return; |
644 | } | |
645 | ||
85799a37 | 646 | nvsp_packet = (struct nvsp_message *)((unsigned long)packet + |
415f2287 | 647 | (packet->offset8 << 3)); |
fceaf24a | 648 | |
53d21fdb HZ |
649 | if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) || |
650 | (nvsp_packet->hdr.msg_type == | |
651 | NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) || | |
652 | (nvsp_packet->hdr.msg_type == | |
653 | NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) { | |
454f18a9 | 654 | /* Copy the response back */ |
53d21fdb | 655 | memcpy(&net_device->channel_init_pkt, nvsp_packet, |
21a80820 | 656 | sizeof(struct nvsp_message)); |
0c3b7b2f S |
657 | net_device->wait_condition = 1; |
658 | wake_up(&net_device->channel_init_wait); | |
53d21fdb HZ |
659 | } else if (nvsp_packet->hdr.msg_type == |
660 | NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) { | |
454f18a9 | 661 | /* Get the send context */ |
85799a37 | 662 | nvsc_packet = (struct hv_netvsc_packet *)(unsigned long) |
415f2287 | 663 | packet->trans_id; |
fceaf24a | 664 | |
454f18a9 | 665 | /* Notify the layer above us */ |
72a2f5bd HZ |
666 | nvsc_packet->completion.send.send_completion( |
667 | nvsc_packet->completion.send.send_completion_ctx); | |
fceaf24a | 668 | |
53d21fdb | 669 | atomic_dec(&net_device->num_outstanding_sends); |
21a80820 | 670 | } else { |
eb335bc4 | 671 | dev_err(&device->device, "Unknown send completion packet type- " |
53d21fdb | 672 | "%d received!!", nvsp_packet->hdr.msg_type); |
fceaf24a HJ |
673 | } |
674 | ||
5a71ae30 | 675 | put_net_device(device); |
fceaf24a HJ |
676 | } |
677 | ||
5a71ae30 | 678 | static int netvsc_send(struct hv_device *device, |
85799a37 | 679 | struct hv_netvsc_packet *packet) |
fceaf24a | 680 | { |
85799a37 | 681 | struct netvsc_device *net_device; |
21a80820 | 682 | int ret = 0; |
fceaf24a | 683 | |
223c1aa6 | 684 | struct nvsp_message sendMessage; |
fceaf24a | 685 | |
5a71ae30 | 686 | net_device = get_outbound_net_device(device); |
85799a37 | 687 | if (!net_device) { |
eb335bc4 | 688 | dev_err(&device->device, "net device (%p) shutting down..." |
85799a37 | 689 | "ignoring outbound packets", net_device); |
fceaf24a HJ |
690 | return -2; |
691 | } | |
692 | ||
53d21fdb | 693 | sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT; |
72a2f5bd | 694 | if (packet->is_data_pkt) { |
21a80820 | 695 | /* 0 is RMC_DATA; */ |
53d21fdb | 696 | sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0; |
21a80820 GKH |
697 | } else { |
698 | /* 1 is RMC_CONTROL; */ | |
53d21fdb | 699 | sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1; |
21a80820 | 700 | } |
fceaf24a | 701 | |
454f18a9 | 702 | /* Not using send buffer section */ |
53d21fdb HZ |
703 | sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index = |
704 | 0xFFFFFFFF; | |
705 | sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0; | |
21a80820 | 706 | |
72a2f5bd | 707 | if (packet->page_buf_cnt) { |
85799a37 | 708 | ret = vmbus_sendpacket_pagebuffer(device->channel, |
72a2f5bd HZ |
709 | packet->page_buf, |
710 | packet->page_buf_cnt, | |
ff3f8eec GKH |
711 | &sendMessage, |
712 | sizeof(struct nvsp_message), | |
85799a37 | 713 | (unsigned long)packet); |
21a80820 | 714 | } else { |
85799a37 | 715 | ret = vmbus_sendpacket(device->channel, &sendMessage, |
5a4df290 | 716 | sizeof(struct nvsp_message), |
85799a37 | 717 | (unsigned long)packet, |
415f2287 | 718 | VM_PKT_DATA_INBAND, |
5a4df290 | 719 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
fceaf24a HJ |
720 | |
721 | } | |
722 | ||
723 | if (ret != 0) | |
eb335bc4 | 724 | dev_err(&device->device, "Unable to send packet %p ret %d", |
85799a37 | 725 | packet, ret); |
fceaf24a | 726 | |
53d21fdb | 727 | atomic_inc(&net_device->num_outstanding_sends); |
5a71ae30 | 728 | put_net_device(device); |
fceaf24a HJ |
729 | return ret; |
730 | } | |
731 | ||
5fa9d3c5 HZ |
732 | static void netvsc_send_recv_completion(struct hv_device *device, |
733 | u64 transaction_id) | |
734 | { | |
735 | struct nvsp_message recvcompMessage; | |
736 | int retries = 0; | |
737 | int ret; | |
738 | ||
739 | recvcompMessage.hdr.msg_type = | |
740 | NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE; | |
741 | ||
742 | /* FIXME: Pass in the status */ | |
743 | recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = | |
744 | NVSP_STAT_SUCCESS; | |
745 | ||
746 | retry_send_cmplt: | |
747 | /* Send the completion */ | |
748 | ret = vmbus_sendpacket(device->channel, &recvcompMessage, | |
749 | sizeof(struct nvsp_message), transaction_id, | |
750 | VM_PKT_COMP, 0); | |
751 | if (ret == 0) { | |
752 | /* success */ | |
753 | /* no-op */ | |
754 | } else if (ret == -1) { | |
755 | /* no more room...wait a bit and attempt to retry 3 times */ | |
756 | retries++; | |
757 | dev_err(&device->device, "unable to send receive completion pkt" | |
758 | " (tid %llx)...retrying %d", transaction_id, retries); | |
759 | ||
760 | if (retries < 4) { | |
761 | udelay(100); | |
762 | goto retry_send_cmplt; | |
763 | } else { | |
764 | dev_err(&device->device, "unable to send receive " | |
765 | "completion pkt (tid %llx)...give up retrying", | |
766 | transaction_id); | |
767 | } | |
768 | } else { | |
769 | dev_err(&device->device, "unable to send receive " | |
770 | "completion pkt - %llx", transaction_id); | |
771 | } | |
772 | } | |
773 | ||
57991156 HZ |
774 | /* Send a receive completion packet to RNDIS device (ie NetVsp) */ |
775 | static void netvsc_receive_completion(void *context) | |
776 | { | |
777 | struct hv_netvsc_packet *packet = context; | |
778 | struct hv_device *device = (struct hv_device *)packet->device; | |
779 | struct netvsc_device *net_device; | |
780 | u64 transaction_id = 0; | |
781 | bool fsend_receive_comp = false; | |
782 | unsigned long flags; | |
783 | ||
784 | /* | |
785 | * Even though it seems logical to do a GetOutboundNetDevice() here to | |
786 | * send out receive completion, we are using GetInboundNetDevice() | |
787 | * since we may have disable outbound traffic already. | |
788 | */ | |
789 | net_device = get_inbound_net_device(device); | |
790 | if (!net_device) { | |
791 | dev_err(&device->device, "unable to get net device..." | |
792 | "device being destroyed?"); | |
793 | return; | |
794 | } | |
795 | ||
796 | /* Overloading use of the lock. */ | |
797 | spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags); | |
798 | ||
799 | packet->xfer_page_pkt->count--; | |
800 | ||
801 | /* | |
802 | * Last one in the line that represent 1 xfer page packet. | |
803 | * Return the xfer page packet itself to the freelist | |
804 | */ | |
805 | if (packet->xfer_page_pkt->count == 0) { | |
806 | fsend_receive_comp = true; | |
807 | transaction_id = packet->completion.recv.recv_completion_tid; | |
808 | list_add_tail(&packet->xfer_page_pkt->list_ent, | |
809 | &net_device->recv_pkt_list); | |
810 | ||
811 | } | |
812 | ||
813 | /* Put the packet back */ | |
814 | list_add_tail(&packet->list_ent, &net_device->recv_pkt_list); | |
815 | spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags); | |
816 | ||
817 | /* Send a receive completion for the xfer page packet */ | |
818 | if (fsend_receive_comp) | |
819 | netvsc_send_recv_completion(device, transaction_id); | |
820 | ||
821 | put_net_device(device); | |
822 | } | |
823 | ||
5a71ae30 | 824 | static void netvsc_receive(struct hv_device *device, |
85799a37 | 825 | struct vmpacket_descriptor *packet) |
fceaf24a | 826 | { |
85799a37 HZ |
827 | struct netvsc_device *net_device; |
828 | struct vmtransfer_page_packet_header *vmxferpage_packet; | |
829 | struct nvsp_message *nvsp_packet; | |
830 | struct hv_netvsc_packet *netvsc_packet = NULL; | |
c4b0bc94 | 831 | unsigned long start; |
85799a37 | 832 | unsigned long end, end_virtual; |
7e23a6e9 | 833 | /* struct netvsc_driver *netvscDriver; */ |
85799a37 | 834 | struct xferpage_packet *xferpage_packet = NULL; |
21a80820 | 835 | int i, j; |
85799a37 | 836 | int count = 0, bytes_remain = 0; |
6436873a | 837 | unsigned long flags; |
d29274ef | 838 | LIST_HEAD(listHead); |
fceaf24a | 839 | |
5a71ae30 | 840 | net_device = get_inbound_net_device(device); |
85799a37 | 841 | if (!net_device) { |
eb335bc4 | 842 | dev_err(&device->device, "unable to get net device..." |
21a80820 | 843 | "device being destroyed?"); |
fceaf24a HJ |
844 | return; |
845 | } | |
846 | ||
21a80820 GKH |
847 | /* |
848 | * All inbound packets other than send completion should be xfer page | |
849 | * packet | |
850 | */ | |
415f2287 | 851 | if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) { |
eb335bc4 | 852 | dev_err(&device->device, "Unknown packet type received - %d", |
415f2287 | 853 | packet->type); |
5a71ae30 | 854 | put_net_device(device); |
fceaf24a HJ |
855 | return; |
856 | } | |
857 | ||
85799a37 | 858 | nvsp_packet = (struct nvsp_message *)((unsigned long)packet + |
415f2287 | 859 | (packet->offset8 << 3)); |
fceaf24a | 860 | |
454f18a9 | 861 | /* Make sure this is a valid nvsp packet */ |
53d21fdb HZ |
862 | if (nvsp_packet->hdr.msg_type != |
863 | NVSP_MSG1_TYPE_SEND_RNDIS_PKT) { | |
eb335bc4 HJ |
864 | dev_err(&device->device, "Unknown nvsp packet type received-" |
865 | " %d", nvsp_packet->hdr.msg_type); | |
5a71ae30 | 866 | put_net_device(device); |
fceaf24a HJ |
867 | return; |
868 | } | |
869 | ||
85799a37 | 870 | vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet; |
fceaf24a | 871 | |
415f2287 | 872 | if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) { |
eb335bc4 | 873 | dev_err(&device->device, "Invalid xfer page set id - " |
21a80820 | 874 | "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, |
415f2287 | 875 | vmxferpage_packet->xfer_pageset_id); |
5a71ae30 | 876 | put_net_device(device); |
fceaf24a HJ |
877 | return; |
878 | } | |
879 | ||
454f18a9 BP |
880 | /* |
881 | * Grab free packets (range count + 1) to represent this xfer | |
882 | * page packet. +1 to represent the xfer page packet itself. | |
883 | * We grab it here so that we know exactly how many we can | |
884 | * fulfil | |
885 | */ | |
53d21fdb HZ |
886 | spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags); |
887 | while (!list_empty(&net_device->recv_pkt_list)) { | |
888 | list_move_tail(net_device->recv_pkt_list.next, &listHead); | |
415f2287 | 889 | if (++count == vmxferpage_packet->range_cnt + 1) |
fceaf24a HJ |
890 | break; |
891 | } | |
53d21fdb | 892 | spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags); |
fceaf24a | 893 | |
454f18a9 BP |
894 | /* |
895 | * We need at least 2 netvsc pkts (1 to represent the xfer | |
896 | * page and at least 1 for the range) i.e. we can handled | |
897 | * some of the xfer page packet ranges... | |
898 | */ | |
21a80820 | 899 | if (count < 2) { |
eb335bc4 HJ |
900 | dev_err(&device->device, "Got only %d netvsc pkt...needed " |
901 | "%d pkts. Dropping this xfer page packet completely!", | |
902 | count, vmxferpage_packet->range_cnt + 1); | |
fceaf24a | 903 | |
454f18a9 | 904 | /* Return it to the freelist */ |
53d21fdb | 905 | spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags); |
21a80820 | 906 | for (i = count; i != 0; i--) { |
92ec0893 | 907 | list_move_tail(listHead.next, |
53d21fdb | 908 | &net_device->recv_pkt_list); |
fceaf24a | 909 | } |
53d21fdb | 910 | spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, |
21a80820 | 911 | flags); |
fceaf24a | 912 | |
5a71ae30 | 913 | netvsc_send_recv_completion(device, |
415f2287 | 914 | vmxferpage_packet->d.trans_id); |
fceaf24a | 915 | |
5a71ae30 | 916 | put_net_device(device); |
fceaf24a HJ |
917 | return; |
918 | } | |
919 | ||
454f18a9 | 920 | /* Remove the 1st packet to represent the xfer page packet itself */ |
85799a37 | 921 | xferpage_packet = (struct xferpage_packet *)listHead.next; |
72a2f5bd | 922 | list_del(&xferpage_packet->list_ent); |
d29274ef | 923 | |
21a80820 | 924 | /* This is how much we can satisfy */ |
72a2f5bd | 925 | xferpage_packet->count = count - 1; |
21a80820 | 926 | |
415f2287 | 927 | if (xferpage_packet->count != vmxferpage_packet->range_cnt) { |
eb335bc4 HJ |
928 | dev_err(&device->device, "Needed %d netvsc pkts to satisy " |
929 | "this xfer page...got %d", | |
930 | vmxferpage_packet->range_cnt, xferpage_packet->count); | |
fceaf24a HJ |
931 | } |
932 | ||
454f18a9 | 933 | /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */ |
21a80820 | 934 | for (i = 0; i < (count - 1); i++) { |
85799a37 | 935 | netvsc_packet = (struct hv_netvsc_packet *)listHead.next; |
72a2f5bd | 936 | list_del(&netvsc_packet->list_ent); |
fceaf24a | 937 | |
454f18a9 | 938 | /* Initialize the netvsc packet */ |
72a2f5bd HZ |
939 | netvsc_packet->xfer_page_pkt = xferpage_packet; |
940 | netvsc_packet->completion.recv.recv_completion = | |
5a71ae30 | 941 | netvsc_receive_completion; |
72a2f5bd | 942 | netvsc_packet->completion.recv.recv_completion_ctx = |
85799a37 | 943 | netvsc_packet; |
72a2f5bd | 944 | netvsc_packet->device = device; |
21a80820 | 945 | /* Save this so that we can send it back */ |
72a2f5bd | 946 | netvsc_packet->completion.recv.recv_completion_tid = |
415f2287 | 947 | vmxferpage_packet->d.trans_id; |
fceaf24a | 948 | |
72a2f5bd | 949 | netvsc_packet->total_data_buflen = |
415f2287 | 950 | vmxferpage_packet->ranges[i].byte_count; |
72a2f5bd | 951 | netvsc_packet->page_buf_cnt = 1; |
fceaf24a | 952 | |
ca623ad3 | 953 | netvsc_packet->page_buf[0].len = |
415f2287 | 954 | vmxferpage_packet->ranges[i].byte_count; |
fceaf24a | 955 | |
85799a37 | 956 | start = virt_to_phys((void *)((unsigned long)net_device-> |
415f2287 | 957 | recv_buf + vmxferpage_packet->ranges[i].byte_offset)); |
fceaf24a | 958 | |
ca623ad3 | 959 | netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT; |
53d21fdb | 960 | end_virtual = (unsigned long)net_device->recv_buf |
415f2287 HZ |
961 | + vmxferpage_packet->ranges[i].byte_offset |
962 | + vmxferpage_packet->ranges[i].byte_count - 1; | |
85799a37 | 963 | end = virt_to_phys((void *)end_virtual); |
fceaf24a | 964 | |
454f18a9 | 965 | /* Calculate the page relative offset */ |
ca623ad3 | 966 | netvsc_packet->page_buf[0].offset = |
415f2287 | 967 | vmxferpage_packet->ranges[i].byte_offset & |
85799a37 | 968 | (PAGE_SIZE - 1); |
21a80820 GKH |
969 | if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) { |
970 | /* Handle frame across multiple pages: */ | |
ca623ad3 HZ |
971 | netvsc_packet->page_buf[0].len = |
972 | (netvsc_packet->page_buf[0].pfn << | |
85799a37 | 973 | PAGE_SHIFT) |
21a80820 | 974 | + PAGE_SIZE - start; |
72a2f5bd | 975 | bytes_remain = netvsc_packet->total_data_buflen - |
ca623ad3 | 976 | netvsc_packet->page_buf[0].len; |
21a80820 | 977 | for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) { |
ca623ad3 | 978 | netvsc_packet->page_buf[j].offset = 0; |
85799a37 | 979 | if (bytes_remain <= PAGE_SIZE) { |
ca623ad3 | 980 | netvsc_packet->page_buf[j].len = |
85799a37 HZ |
981 | bytes_remain; |
982 | bytes_remain = 0; | |
21a80820 | 983 | } else { |
ca623ad3 | 984 | netvsc_packet->page_buf[j].len = |
85799a37 HZ |
985 | PAGE_SIZE; |
986 | bytes_remain -= PAGE_SIZE; | |
21a80820 | 987 | } |
ca623ad3 | 988 | netvsc_packet->page_buf[j].pfn = |
85799a37 HZ |
989 | virt_to_phys((void *)(end_virtual - |
990 | bytes_remain)) >> PAGE_SHIFT; | |
72a2f5bd | 991 | netvsc_packet->page_buf_cnt++; |
85799a37 | 992 | if (bytes_remain == 0) |
21a80820 | 993 | break; |
fceaf24a | 994 | } |
fceaf24a | 995 | } |
fceaf24a | 996 | |
454f18a9 | 997 | /* Pass it to the upper layer */ |
ca623ad3 | 998 | ((struct netvsc_driver *)device->drv)-> |
72a2f5bd | 999 | recv_cb(device, netvsc_packet); |
fceaf24a | 1000 | |
5a71ae30 | 1001 | netvsc_receive_completion(netvsc_packet-> |
72a2f5bd | 1002 | completion.recv.recv_completion_ctx); |
fceaf24a HJ |
1003 | } |
1004 | ||
5a71ae30 | 1005 | put_net_device(device); |
fceaf24a HJ |
1006 | } |
1007 | ||
5a71ae30 | 1008 | static void netvsc_channel_cb(void *context) |
fceaf24a | 1009 | { |
21a80820 | 1010 | int ret; |
85799a37 HZ |
1011 | struct hv_device *device = context; |
1012 | struct netvsc_device *net_device; | |
1013 | u32 bytes_recvd; | |
1014 | u64 request_id; | |
c6fcf0ba | 1015 | unsigned char *packet; |
8dc0a06a | 1016 | struct vmpacket_descriptor *desc; |
c6fcf0ba BP |
1017 | unsigned char *buffer; |
1018 | int bufferlen = NETVSC_PACKET_SIZE; | |
fceaf24a | 1019 | |
c6fcf0ba | 1020 | packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char), |
d70c6731 | 1021 | GFP_ATOMIC); |
c6fcf0ba BP |
1022 | if (!packet) |
1023 | return; | |
1024 | buffer = packet; | |
1025 | ||
5a71ae30 | 1026 | net_device = get_inbound_net_device(device); |
85799a37 | 1027 | if (!net_device) { |
eb335bc4 | 1028 | dev_err(&device->device, "net device (%p) shutting down..." |
85799a37 | 1029 | "ignoring inbound packets", net_device); |
c6fcf0ba | 1030 | goto out; |
fceaf24a HJ |
1031 | } |
1032 | ||
21a80820 | 1033 | do { |
9f630068 | 1034 | ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen, |
85799a37 | 1035 | &bytes_recvd, &request_id); |
21a80820 | 1036 | if (ret == 0) { |
85799a37 | 1037 | if (bytes_recvd > 0) { |
21a80820 | 1038 | desc = (struct vmpacket_descriptor *)buffer; |
415f2287 HZ |
1039 | switch (desc->type) { |
1040 | case VM_PKT_COMP: | |
5a71ae30 | 1041 | netvsc_send_completion(device, desc); |
21a80820 GKH |
1042 | break; |
1043 | ||
415f2287 | 1044 | case VM_PKT_DATA_USING_XFER_PAGES: |
5a71ae30 | 1045 | netvsc_receive(device, desc); |
21a80820 GKH |
1046 | break; |
1047 | ||
1048 | default: | |
eb335bc4 | 1049 | dev_err(&device->device, |
21a80820 GKH |
1050 | "unhandled packet type %d, " |
1051 | "tid %llx len %d\n", | |
415f2287 | 1052 | desc->type, request_id, |
85799a37 | 1053 | bytes_recvd); |
21a80820 | 1054 | break; |
fceaf24a HJ |
1055 | } |
1056 | ||
454f18a9 | 1057 | /* reset */ |
c6fcf0ba | 1058 | if (bufferlen > NETVSC_PACKET_SIZE) { |
8c69f52a | 1059 | kfree(buffer); |
fceaf24a | 1060 | buffer = packet; |
c6fcf0ba | 1061 | bufferlen = NETVSC_PACKET_SIZE; |
fceaf24a | 1062 | } |
21a80820 | 1063 | } else { |
454f18a9 | 1064 | /* reset */ |
c6fcf0ba | 1065 | if (bufferlen > NETVSC_PACKET_SIZE) { |
8c69f52a | 1066 | kfree(buffer); |
fceaf24a | 1067 | buffer = packet; |
c6fcf0ba | 1068 | bufferlen = NETVSC_PACKET_SIZE; |
fceaf24a HJ |
1069 | } |
1070 | ||
1071 | break; | |
1072 | } | |
21a80820 GKH |
1073 | } else if (ret == -2) { |
1074 | /* Handle large packet */ | |
85799a37 | 1075 | buffer = kmalloc(bytes_recvd, GFP_ATOMIC); |
21a80820 | 1076 | if (buffer == NULL) { |
454f18a9 | 1077 | /* Try again next time around */ |
eb335bc4 | 1078 | dev_err(&device->device, |
21a80820 | 1079 | "unable to allocate buffer of size " |
85799a37 | 1080 | "(%d)!!", bytes_recvd); |
fceaf24a HJ |
1081 | break; |
1082 | } | |
1083 | ||
85799a37 | 1084 | bufferlen = bytes_recvd; |
fceaf24a HJ |
1085 | } |
1086 | } while (1); | |
1087 | ||
5a71ae30 | 1088 | put_net_device(device); |
c6fcf0ba BP |
1089 | out: |
1090 | kfree(buffer); | |
fceaf24a HJ |
1091 | return; |
1092 | } | |
af24ce42 | 1093 | |
b637e023 HZ |
1094 | /* |
1095 | * netvsc_device_add - Callback when the device belonging to this | |
1096 | * driver is added | |
1097 | */ | |
1098 | static int netvsc_device_add(struct hv_device *device, void *additional_info) | |
1099 | { | |
1100 | int ret = 0; | |
1101 | int i; | |
1102 | struct netvsc_device *net_device; | |
1103 | struct hv_netvsc_packet *packet, *pos; | |
1104 | struct netvsc_driver *net_driver = | |
1105 | (struct netvsc_driver *)device->drv; | |
1106 | ||
1107 | net_device = alloc_net_device(device); | |
1108 | if (!net_device) { | |
1109 | ret = -1; | |
1110 | goto cleanup; | |
1111 | } | |
1112 | ||
1113 | /* Initialize the NetVSC channel extension */ | |
1114 | net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE; | |
1115 | spin_lock_init(&net_device->recv_pkt_list_lock); | |
1116 | ||
1117 | net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE; | |
1118 | ||
1119 | INIT_LIST_HEAD(&net_device->recv_pkt_list); | |
1120 | ||
1121 | for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) { | |
1122 | packet = kzalloc(sizeof(struct hv_netvsc_packet) + | |
1123 | (NETVSC_RECEIVE_SG_COUNT * | |
1124 | sizeof(struct hv_page_buffer)), GFP_KERNEL); | |
1125 | if (!packet) | |
1126 | break; | |
1127 | ||
1128 | list_add_tail(&packet->list_ent, | |
1129 | &net_device->recv_pkt_list); | |
1130 | } | |
1131 | init_waitqueue_head(&net_device->channel_init_wait); | |
1132 | ||
1133 | /* Open the channel */ | |
1134 | ret = vmbus_open(device->channel, net_driver->ring_buf_size, | |
1135 | net_driver->ring_buf_size, NULL, 0, | |
1136 | netvsc_channel_cb, device); | |
1137 | ||
1138 | if (ret != 0) { | |
1139 | dev_err(&device->device, "unable to open channel: %d", ret); | |
1140 | ret = -1; | |
1141 | goto cleanup; | |
1142 | } | |
1143 | ||
1144 | /* Channel is opened */ | |
1145 | pr_info("hv_netvsc channel opened successfully"); | |
1146 | ||
1147 | /* Connect with the NetVsp */ | |
1148 | ret = netvsc_connect_vsp(device); | |
1149 | if (ret != 0) { | |
1150 | dev_err(&device->device, | |
1151 | "unable to connect to NetVSP - %d", ret); | |
1152 | ret = -1; | |
1153 | goto close; | |
1154 | } | |
1155 | ||
1156 | return ret; | |
1157 | ||
1158 | close: | |
1159 | /* Now, we can close the channel safely */ | |
1160 | vmbus_close(device->channel); | |
1161 | ||
1162 | cleanup: | |
1163 | ||
1164 | if (net_device) { | |
1165 | list_for_each_entry_safe(packet, pos, | |
1166 | &net_device->recv_pkt_list, | |
1167 | list_ent) { | |
1168 | list_del(&packet->list_ent); | |
1169 | kfree(packet); | |
1170 | } | |
1171 | ||
1172 | release_outbound_net_device(device); | |
1173 | release_inbound_net_device(device); | |
1174 | ||
1175 | free_net_device(net_device); | |
1176 | } | |
1177 | ||
1178 | return ret; | |
1179 | } | |
1180 | ||
af24ce42 HZ |
1181 | /* |
1182 | * netvsc_initialize - Main entry point | |
1183 | */ | |
1184 | int netvsc_initialize(struct hv_driver *drv) | |
1185 | { | |
1186 | struct netvsc_driver *driver = (struct netvsc_driver *)drv; | |
1187 | ||
1188 | drv->name = driver_name; | |
1189 | memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid)); | |
1190 | ||
1191 | /* Setup the dispatch table */ | |
1192 | driver->base.dev_add = netvsc_device_add; | |
1193 | driver->base.dev_rm = netvsc_device_remove; | |
1194 | driver->base.cleanup = netvsc_cleanup; | |
1195 | ||
1196 | driver->send = netvsc_send; | |
1197 | ||
1198 | rndis_filter_init(driver); | |
1199 | return 0; | |
1200 | } |