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