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