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