rndis_filter.c 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2009, Microsoft Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 * Authors:
 *   Haiyang Zhang <haiyangz@microsoft.com>
 *   Hank Janssen  <hjanssen@microsoft.com>
 */
21
#include <linux/kernel.h>
22 23
#include <linux/sched.h>
#include <linux/wait.h>
24
#include <linux/highmem.h>
25
#include <linux/slab.h>
26
#include <linux/io.h>
27
#include <linux/if_ether.h>
28
#include <linux/netdevice.h>
29

30
#include "hyperv_net.h"
31 32


33
struct rndis_request {
34
	struct list_head list_ent;
35
	struct completion  wait_event;
36

37 38 39 40 41
	/*
	 * FIXME: We assumed a fixed size response here. If we do ever need to
	 * handle a bigger response, we can either define a max response
	 * message or add a response buffer variable above this field
	 */
42
	struct rndis_message response_msg;
43

44
	/* Simplify allocation by having a netvsc packet inline */
45 46
	struct hv_netvsc_packet	pkt;
	struct hv_page_buffer buf;
47
	/* FIXME: We assumed a fixed size request here. */
48
	struct rndis_message request_msg;
49
};
50

51
static void rndis_filter_send_completion(void *ctx);
52

53
static void rndis_filter_send_request_completion(void *ctx);
54 55


56

57
static struct rndis_device *get_rndis_device(void)
58
{
59
	struct rndis_device *device;
60

61
	device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
62 63 64
	if (!device)
		return NULL;

65
	spin_lock_init(&device->request_lock);
66

67
	INIT_LIST_HEAD(&device->req_list);
68

69
	device->state = RNDIS_DEV_UNINITIALIZED;
70 71 72 73

	return device;
}

74
static struct rndis_request *get_rndis_request(struct rndis_device *dev,
75 76
					     u32 msg_type,
					     u32 msg_len)
77
{
78
	struct rndis_request *request;
79
	struct rndis_message *rndis_msg;
80
	struct rndis_set_request *set;
81
	unsigned long flags;
82

83
	request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
84 85 86
	if (!request)
		return NULL;

87
	init_completion(&request->wait_event);
88

89
	rndis_msg = &request->request_msg;
90 91
	rndis_msg->ndis_msg_type = msg_type;
	rndis_msg->msg_len = msg_len;
92

93 94 95 96 97
	/*
	 * Set the request id. This field is always after the rndis header for
	 * request/response packet types so we just used the SetRequest as a
	 * template
	 */
98 99
	set = &rndis_msg->msg.set_req;
	set->req_id = atomic_inc_return(&dev->new_req_id);
100

101
	/* Add to the request list */
102 103 104
	spin_lock_irqsave(&dev->request_lock, flags);
	list_add_tail(&request->list_ent, &dev->req_list);
	spin_unlock_irqrestore(&dev->request_lock, flags);
105 106 107 108

	return request;
}

109
static void put_rndis_request(struct rndis_device *dev,
110
			    struct rndis_request *req)
111
{
112 113
	unsigned long flags;

114 115 116
	spin_lock_irqsave(&dev->request_lock, flags);
	list_del(&req->list_ent);
	spin_unlock_irqrestore(&dev->request_lock, flags);
117

118
	kfree(req);
119 120
}

121 122
static void dump_rndis_message(struct hv_device *hv_dev,
			struct rndis_message *rndis_msg)
123
{
124 125 126 127 128
	struct net_device *netdev;
	struct netvsc_device *net_device;

	net_device = hv_get_drvdata(hv_dev);
	netdev = net_device->ndev;
129

130
	switch (rndis_msg->ndis_msg_type) {
131
	case REMOTE_NDIS_PACKET_MSG:
132
		netdev_dbg(netdev, "REMOTE_NDIS_PACKET_MSG (len %u, "
133 134
			   "data offset %u data len %u, # oob %u, "
			   "oob offset %u, oob len %u, pkt offset %u, "
135
			   "pkt len %u\n",
136 137 138 139 140 141 142 143
			   rndis_msg->msg_len,
			   rndis_msg->msg.pkt.data_offset,
			   rndis_msg->msg.pkt.data_len,
			   rndis_msg->msg.pkt.num_oob_data_elements,
			   rndis_msg->msg.pkt.oob_data_offset,
			   rndis_msg->msg.pkt.oob_data_len,
			   rndis_msg->msg.pkt.per_pkt_info_offset,
			   rndis_msg->msg.pkt.per_pkt_info_len);
144 145 146
		break;

	case REMOTE_NDIS_INITIALIZE_CMPLT:
147
		netdev_dbg(netdev, "REMOTE_NDIS_INITIALIZE_CMPLT "
148 149
			"(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
			"device flags %d, max xfer size 0x%x, max pkts %u, "
150
			"pkt aligned %u)\n",
151 152 153 154 155 156 157 158 159 160 161
			rndis_msg->msg_len,
			rndis_msg->msg.init_complete.req_id,
			rndis_msg->msg.init_complete.status,
			rndis_msg->msg.init_complete.major_ver,
			rndis_msg->msg.init_complete.minor_ver,
			rndis_msg->msg.init_complete.dev_flags,
			rndis_msg->msg.init_complete.max_xfer_size,
			rndis_msg->msg.init_complete.
			   max_pkt_per_msg,
			rndis_msg->msg.init_complete.
			   pkt_alignment_factor);
162 163 164
		break;

	case REMOTE_NDIS_QUERY_CMPLT:
165
		netdev_dbg(netdev, "REMOTE_NDIS_QUERY_CMPLT "
166
			"(len %u, id 0x%x, status 0x%x, buf len %u, "
167
			"buf offset %u)\n",
168 169 170 171 172 173 174
			rndis_msg->msg_len,
			rndis_msg->msg.query_complete.req_id,
			rndis_msg->msg.query_complete.status,
			rndis_msg->msg.query_complete.
			   info_buflen,
			rndis_msg->msg.query_complete.
			   info_buf_offset);
175 176 177
		break;

	case REMOTE_NDIS_SET_CMPLT:
178 179
		netdev_dbg(netdev,
			"REMOTE_NDIS_SET_CMPLT (len %u, id 0x%x, status 0x%x)\n",
180 181 182
			rndis_msg->msg_len,
			rndis_msg->msg.set_complete.req_id,
			rndis_msg->msg.set_complete.status);
183 184 185
		break;

	case REMOTE_NDIS_INDICATE_STATUS_MSG:
186 187
		netdev_dbg(netdev, "REMOTE_NDIS_INDICATE_STATUS_MSG "
			"(len %u, status 0x%x, buf len %u, buf offset %u)\n",
188 189 190 191
			rndis_msg->msg_len,
			rndis_msg->msg.indicate_status.status,
			rndis_msg->msg.indicate_status.status_buflen,
			rndis_msg->msg.indicate_status.status_buf_offset);
192 193 194
		break;

	default:
195
		netdev_dbg(netdev, "0x%x (len %u)\n",
196 197
			rndis_msg->ndis_msg_type,
			rndis_msg->msg_len);
198 199 200 201
		break;
	}
}

202
static int rndis_filter_send_request(struct rndis_device *dev,
203
				  struct rndis_request *req)
204
{
205
	int ret;
206
	struct hv_netvsc_packet *packet;
207

208
	/* Setup the packet to send it */
209
	packet = &req->pkt;
210

211
	packet->is_data_pkt = false;
212
	packet->total_data_buflen = req->request_msg.msg_len;
213
	packet->page_buf_cnt = 1;
214

215
	packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
216
					PAGE_SHIFT;
217 218
	packet->page_buf[0].len = req->request_msg.msg_len;
	packet->page_buf[0].offset =
219
		(unsigned long)&req->request_msg & (PAGE_SIZE - 1);
220

221 222
	packet->completion.send.send_completion_ctx = req;/* packet; */
	packet->completion.send.send_completion =
223
		rndis_filter_send_request_completion;
224
	packet->completion.send.send_completion_tid = (unsigned long)dev;
225

226
	ret = netvsc_send(dev->net_dev->dev, packet);
227 228 229
	return ret;
}

230
static void rndis_filter_receive_response(struct rndis_device *dev,
231
				       struct rndis_message *resp)
232
{
233
	struct rndis_request *request = NULL;
234
	bool found = false;
235
	unsigned long flags;
236 237 238
	struct net_device *ndev;

	ndev = dev->net_dev->ndev;
239

240 241
	spin_lock_irqsave(&dev->request_lock, flags);
	list_for_each_entry(request, &dev->req_list, list_ent) {
242 243 244 245
		/*
		 * All request/response message contains RequestId as the 1st
		 * field
		 */
246 247
		if (request->request_msg.msg.init_req.req_id
		    == resp->msg.init_complete.req_id) {
248
			found = true;
249 250 251
			break;
		}
	}
252
	spin_unlock_irqrestore(&dev->request_lock, flags);
253

254
	if (found) {
255
		if (resp->msg_len <= sizeof(struct rndis_message)) {
256
			memcpy(&request->response_msg, resp,
257
			       resp->msg_len);
258
		} else {
259
			netdev_err(ndev,
260 261 262 263
				"rndis response buffer overflow "
				"detected (size %u max %zu)\n",
				resp->msg_len,
				sizeof(struct rndis_filter_packet));
264

265
			if (resp->ndis_msg_type ==
266 267
			    REMOTE_NDIS_RESET_CMPLT) {
				/* does not have a request id field */
268 269
				request->response_msg.msg.reset_complete.
					status = STATUS_BUFFER_OVERFLOW;
270
			} else {
271 272
				request->response_msg.msg.
				init_complete.status =
273
					STATUS_BUFFER_OVERFLOW;
274 275 276
			}
		}

277
		complete(&request->wait_event);
278
	} else {
279
		netdev_err(ndev,
280 281 282 283
			"no rndis request found for this response "
			"(id 0x%x res type 0x%x)\n",
			resp->msg.init_complete.req_id,
			resp->ndis_msg_type);
284 285 286
	}
}

287
static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
288
					     struct rndis_message *resp)
289
{
290
	struct rndis_indicate_status *indicate =
291
			&resp->msg.indicate_status;
292

293
	if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
294
		netvsc_linkstatus_callback(
295
			dev->net_dev->dev, 1);
296
	} else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
297
		netvsc_linkstatus_callback(
298
			dev->net_dev->dev, 0);
299 300 301 302
	} else {
		/*
		 * TODO:
		 */
303 304 305
	}
}

306
static void rndis_filter_receive_data(struct rndis_device *dev,
307 308
				   struct rndis_message *msg,
				   struct hv_netvsc_packet *pkt)
309
{
310 311
	struct rndis_packet *rndis_pkt;
	u32 data_offset;
312
	int i;
313

314
	rndis_pkt = &msg->msg.pkt;
315

316 317 318 319
	/*
	 * FIXME: Handle multiple rndis pkt msgs that maybe enclosed in this
	 * netvsc packet (ie TotalDataBufferLength != MessageLength)
	 */
320

321
	/* Remove the rndis header and pass it back up the stack */
322
	data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
323

324
	pkt->total_data_buflen -= data_offset;
325 326
	pkt->page_buf[0].offset += data_offset;
	pkt->page_buf[0].len -= data_offset;
327

328 329 330 331 332 333 334 335 336
	/* Drop the 0th page, if rndis data go beyond page boundary */
	if (pkt->page_buf[0].offset >= PAGE_SIZE) {
		pkt->page_buf[1].offset = pkt->page_buf[0].offset - PAGE_SIZE;
		pkt->page_buf[1].len -= pkt->page_buf[1].offset;
		pkt->page_buf_cnt--;
		for (i = 0; i < pkt->page_buf_cnt; i++)
			pkt->page_buf[i] = pkt->page_buf[i+1];
	}

337
	pkt->is_data_pkt = true;
338

339
	netvsc_recv_callback(dev->net_dev->dev, pkt);
340 341
}

342
int rndis_filter_receive(struct hv_device *dev,
343
				struct hv_netvsc_packet	*pkt)
344
{
345
	struct netvsc_device *net_dev = hv_get_drvdata(dev);
346 347 348
	struct rndis_device *rndis_dev;
	struct rndis_message rndis_msg;
	struct rndis_message *rndis_hdr;
349 350
	struct net_device *ndev;

351
	if (!net_dev)
352 353
		return -EINVAL;

354 355
	ndev = net_dev->ndev;

356
	/* Make sure the rndis device state is initialized */
357
	if (!net_dev->extension) {
358
		netdev_err(ndev, "got rndis message but no rndis device - "
359
			  "dropping this message!\n");
360
		return -ENODEV;
361 362
	}

363
	rndis_dev = (struct rndis_device *)net_dev->extension;
364
	if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
365
		netdev_err(ndev, "got rndis message but rndis device "
366
			   "uninitialized...dropping this message!\n");
367
		return -ENODEV;
368 369
	}

370
	rndis_hdr = (struct rndis_message *)kmap_atomic(
371
			pfn_to_page(pkt->page_buf[0].pfn), KM_IRQ0);
372

373
	rndis_hdr = (void *)((unsigned long)rndis_hdr +
374
			pkt->page_buf[0].offset);
375

376
	/* Make sure we got a valid rndis message */
377 378
	if ((rndis_hdr->ndis_msg_type != REMOTE_NDIS_PACKET_MSG) &&
	    (rndis_hdr->msg_len > sizeof(struct rndis_message))) {
379
		netdev_err(ndev, "incoming rndis message buffer overflow "
380
			   "detected (got %u, max %zu)..marking it an error!\n",
381
			   rndis_hdr->msg_len,
382
			   sizeof(struct rndis_message));
383 384
	}

385
	memcpy(&rndis_msg, rndis_hdr,
386
		(rndis_hdr->msg_len > sizeof(struct rndis_message)) ?
387
			sizeof(struct rndis_message) :
388
			rndis_hdr->msg_len);
389

390
	kunmap_atomic(rndis_hdr - pkt->page_buf[0].offset, KM_IRQ0);
391

392
	dump_rndis_message(dev, &rndis_msg);
393

394
	switch (rndis_msg.ndis_msg_type) {
395
	case REMOTE_NDIS_PACKET_MSG:
396
		/* data msg */
397
		rndis_filter_receive_data(rndis_dev, &rndis_msg, pkt);
398 399 400 401 402
		break;

	case REMOTE_NDIS_INITIALIZE_CMPLT:
	case REMOTE_NDIS_QUERY_CMPLT:
	case REMOTE_NDIS_SET_CMPLT:
403
		/* completion msgs */
404
		rndis_filter_receive_response(rndis_dev, &rndis_msg);
405 406 407
		break;

	case REMOTE_NDIS_INDICATE_STATUS_MSG:
408
		/* notification msgs */
409
		rndis_filter_receive_indicate_status(rndis_dev, &rndis_msg);
410 411
		break;
	default:
412
		netdev_err(ndev,
413
			"unhandled rndis message (type %u len %u)\n",
414 415
			   rndis_msg.ndis_msg_type,
			   rndis_msg.msg_len);
416 417 418 419 420 421
		break;
	}

	return 0;
}

422
static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
423
				  void *result, u32 *result_size)
424
{
425
	struct rndis_request *request;
426
	u32 inresult_size = *result_size;
427
	struct rndis_query_request *query;
428
	struct rndis_query_complete *query_complete;
429
	int ret = 0;
430
	int t;
431

432
	if (!result)
433
		return -EINVAL;
434

435
	*result_size = 0;
436
	request = get_rndis_request(dev, REMOTE_NDIS_QUERY_MSG,
437 438
			RNDIS_MESSAGE_SIZE(struct rndis_query_request));
	if (!request) {
439
		ret = -ENOMEM;
440
		goto cleanup;
441 442
	}

443
	/* Setup the rndis query */
444 445 446 447 448
	query = &request->request_msg.msg.query_req;
	query->oid = oid;
	query->info_buf_offset = sizeof(struct rndis_query_request);
	query->info_buflen = 0;
	query->dev_vc_handle = 0;
449

450
	ret = rndis_filter_send_request(dev, request);
451
	if (ret != 0)
452
		goto cleanup;
453

454
	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
455
	if (t == 0) {
456
		ret = -ETIMEDOUT;
457
		goto cleanup;
458
	}
459

460
	/* Copy the response back */
461
	query_complete = &request->response_msg.msg.query_complete;
462

463
	if (query_complete->info_buflen > inresult_size) {
464
		ret = -1;
465
		goto cleanup;
466 467
	}

468 469
	memcpy(result,
	       (void *)((unsigned long)query_complete +
470 471
			 query_complete->info_buf_offset),
	       query_complete->info_buflen);
472

473
	*result_size = query_complete->info_buflen;
474

475
cleanup:
476
	if (request)
477
		put_rndis_request(dev, request);
478 479 480 481

	return ret;
}

482
static int rndis_filter_query_device_mac(struct rndis_device *dev)
483
{
484
	u32 size = ETH_ALEN;
485

486
	return rndis_filter_query_device(dev,
487
				      RNDIS_OID_802_3_PERMANENT_ADDRESS,
488
				      dev->hw_mac_adr, &size);
489 490
}

491
static int rndis_filter_query_device_link_status(struct rndis_device *dev)
492
{
493
	u32 size = sizeof(u32);
494 495
	u32 link_status;
	int ret;
496

497
	ret = rndis_filter_query_device(dev,
498
				      RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
499 500 501 502
				      &link_status, &size);
	dev->link_state = (link_status != 0) ? true : false;

	return ret;
503 504
}

505
int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
506
{
507
	struct rndis_request *request;
508
	struct rndis_set_request *set;
509
	struct rndis_set_complete *set_complete;
510
	u32 status;
511
	int ret, t;
512 513 514
	struct net_device *ndev;

	ndev = dev->net_dev->ndev;
515

516
	request = get_rndis_request(dev, REMOTE_NDIS_SET_MSG,
517 518 519
			RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
			sizeof(u32));
	if (!request) {
520
		ret = -ENOMEM;
521
		goto cleanup;
522 523
	}

524
	/* Setup the rndis set */
525 526 527 528
	set = &request->request_msg.msg.set_req;
	set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
	set->info_buflen = sizeof(u32);
	set->info_buf_offset = sizeof(struct rndis_set_request);
529

530
	memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
531
	       &new_filter, sizeof(u32));
532

533
	ret = rndis_filter_send_request(dev, request);
534
	if (ret != 0)
535
		goto cleanup;
536

537
	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
538 539

	if (t == 0) {
540
		netdev_err(ndev,
541
			"timeout before we got a set response...\n");
542
		/*
L
Lucas De Marchi 已提交
543
		 * We can't deallocate the request since we may still receive a
544 545
		 * send completion for it.
		 */
546
		goto exit;
547
	} else {
548 549
		set_complete = &request->response_msg.msg.set_complete;
		status = set_complete->status;
550 551
	}

552
cleanup:
553
	if (request)
554
		put_rndis_request(dev, request);
555
exit:
556 557 558 559
	return ret;
}


560
static int rndis_filter_init_device(struct rndis_device *dev)
561
{
562
	struct rndis_request *request;
563
	struct rndis_initialize_request *init;
564
	struct rndis_initialize_complete *init_complete;
565
	u32 status;
566
	int ret, t;
567

568
	request = get_rndis_request(dev, REMOTE_NDIS_INITIALIZE_MSG,
569 570
			RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
	if (!request) {
571
		ret = -ENOMEM;
572
		goto cleanup;
573 574
	}

575
	/* Setup the rndis set */
576 577 578
	init = &request->request_msg.msg.init_req;
	init->major_ver = RNDIS_MAJOR_VERSION;
	init->minor_ver = RNDIS_MINOR_VERSION;
579
	/* FIXME: Use 1536 - rounded ethernet frame size */
580
	init->max_xfer_size = 2048;
581

582
	dev->state = RNDIS_DEV_INITIALIZING;
583

584
	ret = rndis_filter_send_request(dev, request);
585
	if (ret != 0) {
586
		dev->state = RNDIS_DEV_UNINITIALIZED;
587
		goto cleanup;
588 589
	}

590

591
	t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
592 593

	if (t == 0) {
594
		ret = -ETIMEDOUT;
595
		goto cleanup;
596
	}
597

598 599
	init_complete = &request->response_msg.msg.init_complete;
	status = init_complete->status;
600
	if (status == RNDIS_STATUS_SUCCESS) {
601
		dev->state = RNDIS_DEV_INITIALIZED;
602
		ret = 0;
603
	} else {
604
		dev->state = RNDIS_DEV_UNINITIALIZED;
605
		ret = -EINVAL;
606 607
	}

608
cleanup:
609
	if (request)
610
		put_rndis_request(dev, request);
611 612 613 614

	return ret;
}

615
static void rndis_filter_halt_device(struct rndis_device *dev)
616
{
617
	struct rndis_request *request;
618
	struct rndis_halt_request *halt;
619

620
	/* Attempt to do a rndis device halt */
621
	request = get_rndis_request(dev, REMOTE_NDIS_HALT_MSG,
622
				RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
623
	if (!request)
624
		goto cleanup;
625

626
	/* Setup the rndis set */
627 628
	halt = &request->request_msg.msg.halt_req;
	halt->req_id = atomic_inc_return(&dev->new_req_id);
629

630
	/* Ignore return since this msg is optional. */
631
	rndis_filter_send_request(dev, request);
632

633
	dev->state = RNDIS_DEV_UNINITIALIZED;
634

635
cleanup:
636
	if (request)
637
		put_rndis_request(dev, request);
638 639 640
	return;
}

641
static int rndis_filter_open_device(struct rndis_device *dev)
642
{
643
	int ret;
644

645
	if (dev->state != RNDIS_DEV_INITIALIZED)
646 647
		return 0;

648
	ret = rndis_filter_set_packet_filter(dev,
649
					 NDIS_PACKET_TYPE_BROADCAST |
650
					 NDIS_PACKET_TYPE_ALL_MULTICAST |
651
					 NDIS_PACKET_TYPE_DIRECTED);
652
	if (ret == 0)
653
		dev->state = RNDIS_DEV_DATAINITIALIZED;
654 655 656 657

	return ret;
}

658
static int rndis_filter_close_device(struct rndis_device *dev)
659 660 661
{
	int ret;

662
	if (dev->state != RNDIS_DEV_DATAINITIALIZED)
663 664
		return 0;

665
	ret = rndis_filter_set_packet_filter(dev, 0);
666
	if (ret == 0)
667
		dev->state = RNDIS_DEV_INITIALIZED;
668 669 670 671

	return ret;
}

672
int rndis_filter_device_add(struct hv_device *dev,
673
				  void *additional_info)
674 675
{
	int ret;
676
	struct netvsc_device *net_device;
677
	struct rndis_device *rndis_device;
678
	struct netvsc_device_info *device_info = additional_info;
679

680 681
	rndis_device = get_rndis_device();
	if (!rndis_device)
682
		return -ENODEV;
683

684 685 686 687 688
	/*
	 * Let the inner driver handle this first to create the netvsc channel
	 * NOTE! Once the channel is created, we may get a receive callback
	 * (RndisFilterOnReceive()) before this call is completed
	 */
689
	ret = netvsc_device_add(dev, additional_info);
690
	if (ret != 0) {
691
		kfree(rndis_device);
692 693 694
		return ret;
	}

695 696

	/* Initialize the rndis device */
697
	net_device = hv_get_drvdata(dev);
698

699 700
	net_device->extension = rndis_device;
	rndis_device->net_dev = net_device;
701

702
	/* Send the rndis initialization message */
703
	ret = rndis_filter_init_device(rndis_device);
704 705 706 707 708
	if (ret != 0) {
		/*
		 * TODO: If rndis init failed, we will need to shut down the
		 * channel
		 */
709 710
	}

711
	/* Get the mac address */
712
	ret = rndis_filter_query_device_mac(rndis_device);
713 714 715 716
	if (ret != 0) {
		/*
		 * TODO: shutdown rndis device and the channel
		 */
717 718
	}

719
	memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
720

721
	rndis_filter_query_device_link_status(rndis_device);
722

723
	device_info->link_state = rndis_device->link_state;
724

725
	dev_info(&dev->device, "Device MAC %pM link state %s\n",
726
		 rndis_device->hw_mac_adr,
727
		 device_info->link_state ? "down" : "up");
728 729 730 731

	return ret;
}

732
void rndis_filter_device_remove(struct hv_device *dev)
733
{
734
	struct netvsc_device *net_dev = hv_get_drvdata(dev);
735
	struct rndis_device *rndis_dev = net_dev->extension;
736

737
	/* Halt and release the rndis device */
738
	rndis_filter_halt_device(rndis_dev);
739

740
	kfree(rndis_dev);
741
	net_dev->extension = NULL;
742

743
	netvsc_device_remove(dev);
744 745 746
}


747
int rndis_filter_open(struct hv_device *dev)
748
{
749
	struct netvsc_device *net_device = hv_get_drvdata(dev);
750

751
	if (!net_device)
752 753
		return -EINVAL;

754
	return rndis_filter_open_device(net_device->extension);
755 756
}

757
int rndis_filter_close(struct hv_device *dev)
758
{
759
	struct netvsc_device *netDevice = hv_get_drvdata(dev);
760

761 762 763
	if (!netDevice)
		return -EINVAL;

764
	return rndis_filter_close_device(netDevice->extension);
765 766
}

767
int rndis_filter_send(struct hv_device *dev,
768
			     struct hv_netvsc_packet *pkt)
769
{
770
	int ret;
771
	struct rndis_filter_packet *filterPacket;
772 773
	struct rndis_message *rndisMessage;
	struct rndis_packet *rndisPacket;
774
	u32 rndisMessageSize;
775

776
	/* Add the rndis header */
777
	filterPacket = (struct rndis_filter_packet *)pkt->extension;
778

779
	memset(filterPacket, 0, sizeof(struct rndis_filter_packet));
780

781
	rndisMessage = &filterPacket->msg;
782
	rndisMessageSize = RNDIS_MESSAGE_SIZE(struct rndis_packet);
783

784 785
	rndisMessage->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
	rndisMessage->msg_len = pkt->total_data_buflen +
786
				      rndisMessageSize;
787

788 789 790
	rndisPacket = &rndisMessage->msg.pkt;
	rndisPacket->data_offset = sizeof(struct rndis_packet);
	rndisPacket->data_len = pkt->total_data_buflen;
791

792
	pkt->is_data_pkt = true;
793 794
	pkt->page_buf[0].pfn = virt_to_phys(rndisMessage) >> PAGE_SHIFT;
	pkt->page_buf[0].offset =
795
			(unsigned long)rndisMessage & (PAGE_SIZE-1);
796
	pkt->page_buf[0].len = rndisMessageSize;
797

798
	/* Save the packet send completion and context */
799
	filterPacket->completion = pkt->completion.send.send_completion;
800
	filterPacket->completion_ctx =
801
				pkt->completion.send.send_completion_ctx;
802

803
	/* Use ours */
804 805
	pkt->completion.send.send_completion = rndis_filter_send_completion;
	pkt->completion.send.send_completion_ctx = filterPacket;
806

807
	ret = netvsc_send(dev, pkt);
808 809 810 811 812
	if (ret != 0) {
		/*
		 * Reset the completion to originals to allow retries from
		 * above
		 */
813
		pkt->completion.send.send_completion =
814
				filterPacket->completion;
815
		pkt->completion.send.send_completion_ctx =
816
				filterPacket->completion_ctx;
817 818 819 820 821
	}

	return ret;
}

822
static void rndis_filter_send_completion(void *ctx)
823
{
824
	struct rndis_filter_packet *filterPacket = ctx;
825

826
	/* Pass it back to the original handler */
827
	filterPacket->completion(filterPacket->completion_ctx);
828 829 830
}


831
static void rndis_filter_send_request_completion(void *ctx)
832
{
833
	/* Noop */
834
}