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