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