Input: i8042 - add Pegatron touchpad to noloop table
[pandora-kernel.git] / drivers / hv / channel.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/slab.h>
28 #include <linux/module.h>
29 #include <linux/hyperv.h>
30
31 #include "hyperv_vmbus.h"
32
33 #define NUM_PAGES_SPANNED(addr, len) \
34 ((PAGE_ALIGN(addr + len) >> PAGE_SHIFT) - (addr >> PAGE_SHIFT))
35
36 /* Internal routines */
37 static int create_gpadl_header(
38         void *kbuffer,  /* must be phys and virt contiguous */
39         u32 size,       /* page-size multiple */
40         struct vmbus_channel_msginfo **msginfo,
41         u32 *messagecount);
42 static void vmbus_setevent(struct vmbus_channel *channel);
43
44 /*
45  * vmbus_setevent- Trigger an event notification on the specified
46  * channel.
47  */
48 static void vmbus_setevent(struct vmbus_channel *channel)
49 {
50         struct hv_monitor_page *monitorpage;
51
52         if (channel->offermsg.monitor_allocated) {
53                 /* Each u32 represents 32 channels */
54                 sync_set_bit(channel->offermsg.child_relid & 31,
55                         (unsigned long *) vmbus_connection.send_int_page +
56                         (channel->offermsg.child_relid >> 5));
57
58                 monitorpage = vmbus_connection.monitor_pages;
59                 monitorpage++; /* Get the child to parent monitor page */
60
61                 sync_set_bit(channel->monitor_bit,
62                         (unsigned long *)&monitorpage->trigger_group
63                                         [channel->monitor_grp].pending);
64
65         } else {
66                 vmbus_set_event(channel->offermsg.child_relid);
67         }
68 }
69
70 /*
71  * vmbus_get_debug_info -Retrieve various channel debug info
72  */
73 void vmbus_get_debug_info(struct vmbus_channel *channel,
74                               struct vmbus_channel_debug_info *debuginfo)
75 {
76         struct hv_monitor_page *monitorpage;
77         u8 monitor_group = (u8)channel->offermsg.monitorid / 32;
78         u8 monitor_offset = (u8)channel->offermsg.monitorid % 32;
79
80         debuginfo->relid = channel->offermsg.child_relid;
81         debuginfo->state = channel->state;
82         memcpy(&debuginfo->interfacetype,
83                &channel->offermsg.offer.if_type, sizeof(uuid_le));
84         memcpy(&debuginfo->interface_instance,
85                &channel->offermsg.offer.if_instance,
86                sizeof(uuid_le));
87
88         monitorpage = (struct hv_monitor_page *)vmbus_connection.monitor_pages;
89
90         debuginfo->monitorid = channel->offermsg.monitorid;
91
92         debuginfo->servermonitor_pending =
93                         monitorpage->trigger_group[monitor_group].pending;
94         debuginfo->servermonitor_latency =
95                         monitorpage->latency[monitor_group][monitor_offset];
96         debuginfo->servermonitor_connectionid =
97                         monitorpage->parameter[monitor_group]
98                                         [monitor_offset].connectionid.u.id;
99
100         monitorpage++;
101
102         debuginfo->clientmonitor_pending =
103                         monitorpage->trigger_group[monitor_group].pending;
104         debuginfo->clientmonitor_latency =
105                         monitorpage->latency[monitor_group][monitor_offset];
106         debuginfo->clientmonitor_connectionid =
107                         monitorpage->parameter[monitor_group]
108                                         [monitor_offset].connectionid.u.id;
109
110         hv_ringbuffer_get_debuginfo(&channel->inbound, &debuginfo->inbound);
111         hv_ringbuffer_get_debuginfo(&channel->outbound, &debuginfo->outbound);
112 }
113
114 /*
115  * vmbus_open - Open the specified channel.
116  */
117 int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
118                      u32 recv_ringbuffer_size, void *userdata, u32 userdatalen,
119                      void (*onchannelcallback)(void *context), void *context)
120 {
121         struct vmbus_channel_open_channel *open_msg;
122         struct vmbus_channel_msginfo *open_info = NULL;
123         void *in, *out;
124         unsigned long flags;
125         int ret, t, err = 0;
126
127         newchannel->onchannel_callback = onchannelcallback;
128         newchannel->channel_callback_context = context;
129
130         /* Allocate the ring buffer */
131         out = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
132                 get_order(send_ringbuffer_size + recv_ringbuffer_size));
133
134         if (!out)
135                 return -ENOMEM;
136
137
138         in = (void *)((unsigned long)out + send_ringbuffer_size);
139
140         newchannel->ringbuffer_pages = out;
141         newchannel->ringbuffer_pagecount = (send_ringbuffer_size +
142                                            recv_ringbuffer_size) >> PAGE_SHIFT;
143
144         ret = hv_ringbuffer_init(
145                 &newchannel->outbound, out, send_ringbuffer_size);
146
147         if (ret != 0) {
148                 err = ret;
149                 goto error0;
150         }
151
152         ret = hv_ringbuffer_init(
153                 &newchannel->inbound, in, recv_ringbuffer_size);
154         if (ret != 0) {
155                 err = ret;
156                 goto error0;
157         }
158
159
160         /* Establish the gpadl for the ring buffer */
161         newchannel->ringbuffer_gpadlhandle = 0;
162
163         ret = vmbus_establish_gpadl(newchannel,
164                                          newchannel->outbound.ring_buffer,
165                                          send_ringbuffer_size +
166                                          recv_ringbuffer_size,
167                                          &newchannel->ringbuffer_gpadlhandle);
168
169         if (ret != 0) {
170                 err = ret;
171                 goto error0;
172         }
173
174         /* Create and init the channel open message */
175         open_info = kmalloc(sizeof(*open_info) +
176                            sizeof(struct vmbus_channel_open_channel),
177                            GFP_KERNEL);
178         if (!open_info) {
179                 err = -ENOMEM;
180                 goto error_gpadl;
181         }
182
183         init_completion(&open_info->waitevent);
184
185         open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
186         open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
187         open_msg->openid = newchannel->offermsg.child_relid;
188         open_msg->child_relid = newchannel->offermsg.child_relid;
189         open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
190         open_msg->downstream_ringbuffer_pageoffset = send_ringbuffer_size >>
191                                                   PAGE_SHIFT;
192         open_msg->server_contextarea_gpadlhandle = 0;
193
194         if (userdatalen > MAX_USER_DEFINED_BYTES) {
195                 err = -EINVAL;
196                 goto error_gpadl;
197         }
198
199         if (userdatalen)
200                 memcpy(open_msg->userdata, userdata, userdatalen);
201
202         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
203         list_add_tail(&open_info->msglistentry,
204                       &vmbus_connection.chn_msg_list);
205         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
206
207         ret = vmbus_post_msg(open_msg,
208                                sizeof(struct vmbus_channel_open_channel));
209
210         if (ret != 0) {
211                 err = ret;
212                 goto error1;
213         }
214
215         t = wait_for_completion_timeout(&open_info->waitevent, 5*HZ);
216         if (t == 0) {
217                 err = -ETIMEDOUT;
218                 goto error1;
219         }
220
221
222         if (open_info->response.open_result.status)
223                 err = open_info->response.open_result.status;
224
225         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
226         list_del(&open_info->msglistentry);
227         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
228
229         kfree(open_info);
230         return err;
231
232 error1:
233         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
234         list_del(&open_info->msglistentry);
235         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
236
237 error_gpadl:
238         vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
239
240 error0:
241         free_pages((unsigned long)out,
242                 get_order(send_ringbuffer_size + recv_ringbuffer_size));
243         kfree(open_info);
244         return err;
245 }
246 EXPORT_SYMBOL_GPL(vmbus_open);
247
248 /*
249  * create_gpadl_header - Creates a gpadl for the specified buffer
250  */
251 static int create_gpadl_header(void *kbuffer, u32 size,
252                                          struct vmbus_channel_msginfo **msginfo,
253                                          u32 *messagecount)
254 {
255         int i;
256         int pagecount;
257         unsigned long long pfn;
258         struct vmbus_channel_gpadl_header *gpadl_header;
259         struct vmbus_channel_gpadl_body *gpadl_body;
260         struct vmbus_channel_msginfo *msgheader;
261         struct vmbus_channel_msginfo *msgbody = NULL;
262         u32 msgsize;
263
264         int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
265
266         pagecount = size >> PAGE_SHIFT;
267         pfn = virt_to_phys(kbuffer) >> PAGE_SHIFT;
268
269         /* do we need a gpadl body msg */
270         pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
271                   sizeof(struct vmbus_channel_gpadl_header) -
272                   sizeof(struct gpa_range);
273         pfncount = pfnsize / sizeof(u64);
274
275         if (pagecount > pfncount) {
276                 /* we need a gpadl body */
277                 /* fill in the header */
278                 msgsize = sizeof(struct vmbus_channel_msginfo) +
279                           sizeof(struct vmbus_channel_gpadl_header) +
280                           sizeof(struct gpa_range) + pfncount * sizeof(u64);
281                 msgheader =  kzalloc(msgsize, GFP_KERNEL);
282                 if (!msgheader)
283                         goto nomem;
284
285                 INIT_LIST_HEAD(&msgheader->submsglist);
286                 msgheader->msgsize = msgsize;
287
288                 gpadl_header = (struct vmbus_channel_gpadl_header *)
289                         msgheader->msg;
290                 gpadl_header->rangecount = 1;
291                 gpadl_header->range_buflen = sizeof(struct gpa_range) +
292                                          pagecount * sizeof(u64);
293                 gpadl_header->range[0].byte_offset = 0;
294                 gpadl_header->range[0].byte_count = size;
295                 for (i = 0; i < pfncount; i++)
296                         gpadl_header->range[0].pfn_array[i] = pfn+i;
297                 *msginfo = msgheader;
298                 *messagecount = 1;
299
300                 pfnsum = pfncount;
301                 pfnleft = pagecount - pfncount;
302
303                 /* how many pfns can we fit */
304                 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
305                           sizeof(struct vmbus_channel_gpadl_body);
306                 pfncount = pfnsize / sizeof(u64);
307
308                 /* fill in the body */
309                 while (pfnleft) {
310                         if (pfnleft > pfncount)
311                                 pfncurr = pfncount;
312                         else
313                                 pfncurr = pfnleft;
314
315                         msgsize = sizeof(struct vmbus_channel_msginfo) +
316                                   sizeof(struct vmbus_channel_gpadl_body) +
317                                   pfncurr * sizeof(u64);
318                         msgbody = kzalloc(msgsize, GFP_KERNEL);
319
320                         if (!msgbody) {
321                                 struct vmbus_channel_msginfo *pos = NULL;
322                                 struct vmbus_channel_msginfo *tmp = NULL;
323                                 /*
324                                  * Free up all the allocated messages.
325                                  */
326                                 list_for_each_entry_safe(pos, tmp,
327                                         &msgheader->submsglist,
328                                         msglistentry) {
329
330                                         list_del(&pos->msglistentry);
331                                         kfree(pos);
332                                 }
333
334                                 goto nomem;
335                         }
336
337                         msgbody->msgsize = msgsize;
338                         (*messagecount)++;
339                         gpadl_body =
340                                 (struct vmbus_channel_gpadl_body *)msgbody->msg;
341
342                         /*
343                          * Gpadl is u32 and we are using a pointer which could
344                          * be 64-bit
345                          * This is governed by the guest/host protocol and
346                          * so the hypervisor gurantees that this is ok.
347                          */
348                         for (i = 0; i < pfncurr; i++)
349                                 gpadl_body->pfn[i] = pfn + pfnsum + i;
350
351                         /* add to msg header */
352                         list_add_tail(&msgbody->msglistentry,
353                                       &msgheader->submsglist);
354                         pfnsum += pfncurr;
355                         pfnleft -= pfncurr;
356                 }
357         } else {
358                 /* everything fits in a header */
359                 msgsize = sizeof(struct vmbus_channel_msginfo) +
360                           sizeof(struct vmbus_channel_gpadl_header) +
361                           sizeof(struct gpa_range) + pagecount * sizeof(u64);
362                 msgheader = kzalloc(msgsize, GFP_KERNEL);
363                 if (msgheader == NULL)
364                         goto nomem;
365                 msgheader->msgsize = msgsize;
366
367                 gpadl_header = (struct vmbus_channel_gpadl_header *)
368                         msgheader->msg;
369                 gpadl_header->rangecount = 1;
370                 gpadl_header->range_buflen = sizeof(struct gpa_range) +
371                                          pagecount * sizeof(u64);
372                 gpadl_header->range[0].byte_offset = 0;
373                 gpadl_header->range[0].byte_count = size;
374                 for (i = 0; i < pagecount; i++)
375                         gpadl_header->range[0].pfn_array[i] = pfn+i;
376
377                 *msginfo = msgheader;
378                 *messagecount = 1;
379         }
380
381         return 0;
382 nomem:
383         kfree(msgheader);
384         kfree(msgbody);
385         return -ENOMEM;
386 }
387
388 /*
389  * vmbus_establish_gpadl - Estabish a GPADL for the specified buffer
390  *
391  * @channel: a channel
392  * @kbuffer: from kmalloc
393  * @size: page-size multiple
394  * @gpadl_handle: some funky thing
395  */
396 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
397                                u32 size, u32 *gpadl_handle)
398 {
399         struct vmbus_channel_gpadl_header *gpadlmsg;
400         struct vmbus_channel_gpadl_body *gpadl_body;
401         struct vmbus_channel_msginfo *msginfo = NULL;
402         struct vmbus_channel_msginfo *submsginfo;
403         u32 msgcount;
404         struct list_head *curr;
405         u32 next_gpadl_handle;
406         unsigned long flags;
407         int ret = 0;
408
409         next_gpadl_handle = atomic_read(&vmbus_connection.next_gpadl_handle);
410         atomic_inc(&vmbus_connection.next_gpadl_handle);
411
412         ret = create_gpadl_header(kbuffer, size, &msginfo, &msgcount);
413         if (ret)
414                 return ret;
415
416         init_completion(&msginfo->waitevent);
417
418         gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
419         gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
420         gpadlmsg->child_relid = channel->offermsg.child_relid;
421         gpadlmsg->gpadl = next_gpadl_handle;
422
423
424         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
425         list_add_tail(&msginfo->msglistentry,
426                       &vmbus_connection.chn_msg_list);
427
428         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
429
430         ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
431                                sizeof(*msginfo));
432         if (ret != 0)
433                 goto cleanup;
434
435         if (msgcount > 1) {
436                 list_for_each(curr, &msginfo->submsglist) {
437
438                         submsginfo = (struct vmbus_channel_msginfo *)curr;
439                         gpadl_body =
440                              (struct vmbus_channel_gpadl_body *)submsginfo->msg;
441
442                         gpadl_body->header.msgtype =
443                                 CHANNELMSG_GPADL_BODY;
444                         gpadl_body->gpadl = next_gpadl_handle;
445
446                         ret = vmbus_post_msg(gpadl_body,
447                                                submsginfo->msgsize -
448                                                sizeof(*submsginfo));
449                         if (ret != 0)
450                                 goto cleanup;
451
452                 }
453         }
454         wait_for_completion(&msginfo->waitevent);
455
456         /* At this point, we received the gpadl created msg */
457         *gpadl_handle = gpadlmsg->gpadl;
458
459 cleanup:
460         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
461         list_del(&msginfo->msglistentry);
462         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
463
464         kfree(msginfo);
465         return ret;
466 }
467 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
468
469 /*
470  * vmbus_teardown_gpadl -Teardown the specified GPADL handle
471  */
472 int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
473 {
474         struct vmbus_channel_gpadl_teardown *msg;
475         struct vmbus_channel_msginfo *info;
476         unsigned long flags;
477         int ret;
478
479         info = kmalloc(sizeof(*info) +
480                        sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
481         if (!info)
482                 return -ENOMEM;
483
484         init_completion(&info->waitevent);
485
486         msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
487
488         msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
489         msg->child_relid = channel->offermsg.child_relid;
490         msg->gpadl = gpadl_handle;
491
492         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
493         list_add_tail(&info->msglistentry,
494                       &vmbus_connection.chn_msg_list);
495         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
496         ret = vmbus_post_msg(msg,
497                                sizeof(struct vmbus_channel_gpadl_teardown));
498
499         if (ret)
500                 goto post_msg_err;
501
502         wait_for_completion(&info->waitevent);
503
504 post_msg_err:
505         spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
506         list_del(&info->msglistentry);
507         spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
508
509         kfree(info);
510         return ret;
511 }
512 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
513
514 /*
515  * vmbus_close - Close the specified channel
516  */
517 void vmbus_close(struct vmbus_channel *channel)
518 {
519         struct vmbus_channel_close_channel *msg;
520         int ret;
521         unsigned long flags;
522
523         /* Stop callback and cancel the timer asap */
524         spin_lock_irqsave(&channel->inbound_lock, flags);
525         channel->onchannel_callback = NULL;
526         spin_unlock_irqrestore(&channel->inbound_lock, flags);
527
528         /* Send a closing message */
529
530         msg = &channel->close_msg.msg;
531
532         msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
533         msg->child_relid = channel->offermsg.child_relid;
534
535         ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel));
536
537         if (ret) {
538                 pr_err("Close failed: close post msg return is %d\n", ret);
539                 /*
540                  * If we failed to post the close msg,
541                  * it is perhaps better to leak memory.
542                  */
543                 return;
544         }
545
546         /* Tear down the gpadl for the channel's ring buffer */
547         if (channel->ringbuffer_gpadlhandle) {
548                 ret = vmbus_teardown_gpadl(channel,
549                                            channel->ringbuffer_gpadlhandle);
550                 if (ret) {
551                         pr_err("Close failed: teardown gpadl return %d\n", ret);
552                         /*
553                          * If we failed to teardown gpadl,
554                          * it is perhaps better to leak memory.
555                          */
556                         return;
557                 }
558         }
559
560         /* Cleanup the ring buffers for this channel */
561         hv_ringbuffer_cleanup(&channel->outbound);
562         hv_ringbuffer_cleanup(&channel->inbound);
563
564         free_pages((unsigned long)channel->ringbuffer_pages,
565                 get_order(channel->ringbuffer_pagecount * PAGE_SIZE));
566
567
568 }
569 EXPORT_SYMBOL_GPL(vmbus_close);
570
571 /**
572  * vmbus_sendpacket() - Send the specified buffer on the given channel
573  * @channel: Pointer to vmbus_channel structure.
574  * @buffer: Pointer to the buffer you want to receive the data into.
575  * @bufferlen: Maximum size of what the the buffer will hold
576  * @requestid: Identifier of the request
577  * @type: Type of packet that is being send e.g. negotiate, time
578  * packet etc.
579  *
580  * Sends data in @buffer directly to hyper-v via the vmbus
581  * This will send the data unparsed to hyper-v.
582  *
583  * Mainly used by Hyper-V drivers.
584  */
585 int vmbus_sendpacket(struct vmbus_channel *channel, const void *buffer,
586                            u32 bufferlen, u64 requestid,
587                            enum vmbus_packet_type type, u32 flags)
588 {
589         struct vmpacket_descriptor desc;
590         u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
591         u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
592         struct scatterlist bufferlist[3];
593         u64 aligned_data = 0;
594         int ret;
595
596
597         /* Setup the descriptor */
598         desc.type = type; /* VmbusPacketTypeDataInBand; */
599         desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
600         /* in 8-bytes granularity */
601         desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
602         desc.len8 = (u16)(packetlen_aligned >> 3);
603         desc.trans_id = requestid;
604
605         sg_init_table(bufferlist, 3);
606         sg_set_buf(&bufferlist[0], &desc, sizeof(struct vmpacket_descriptor));
607         sg_set_buf(&bufferlist[1], buffer, bufferlen);
608         sg_set_buf(&bufferlist[2], &aligned_data,
609                    packetlen_aligned - packetlen);
610
611         ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
612
613         if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
614                 vmbus_setevent(channel);
615
616         return ret;
617 }
618 EXPORT_SYMBOL(vmbus_sendpacket);
619
620 /*
621  * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
622  * packets using a GPADL Direct packet type.
623  */
624 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
625                                      struct hv_page_buffer pagebuffers[],
626                                      u32 pagecount, void *buffer, u32 bufferlen,
627                                      u64 requestid)
628 {
629         int ret;
630         int i;
631         struct vmbus_channel_packet_page_buffer desc;
632         u32 descsize;
633         u32 packetlen;
634         u32 packetlen_aligned;
635         struct scatterlist bufferlist[3];
636         u64 aligned_data = 0;
637
638         if (pagecount > MAX_PAGE_BUFFER_COUNT)
639                 return -EINVAL;
640
641
642         /*
643          * Adjust the size down since vmbus_channel_packet_page_buffer is the
644          * largest size we support
645          */
646         descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
647                           ((MAX_PAGE_BUFFER_COUNT - pagecount) *
648                           sizeof(struct hv_page_buffer));
649         packetlen = descsize + bufferlen;
650         packetlen_aligned = ALIGN(packetlen, sizeof(u64));
651
652         /* Setup the descriptor */
653         desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
654         desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
655         desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
656         desc.length8 = (u16)(packetlen_aligned >> 3);
657         desc.transactionid = requestid;
658         desc.rangecount = pagecount;
659
660         for (i = 0; i < pagecount; i++) {
661                 desc.range[i].len = pagebuffers[i].len;
662                 desc.range[i].offset = pagebuffers[i].offset;
663                 desc.range[i].pfn        = pagebuffers[i].pfn;
664         }
665
666         sg_init_table(bufferlist, 3);
667         sg_set_buf(&bufferlist[0], &desc, descsize);
668         sg_set_buf(&bufferlist[1], buffer, bufferlen);
669         sg_set_buf(&bufferlist[2], &aligned_data,
670                 packetlen_aligned - packetlen);
671
672         ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
673
674         if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
675                 vmbus_setevent(channel);
676
677         return ret;
678 }
679 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
680
681 /*
682  * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
683  * using a GPADL Direct packet type.
684  */
685 int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
686                                 struct hv_multipage_buffer *multi_pagebuffer,
687                                 void *buffer, u32 bufferlen, u64 requestid)
688 {
689         int ret;
690         struct vmbus_channel_packet_multipage_buffer desc;
691         u32 descsize;
692         u32 packetlen;
693         u32 packetlen_aligned;
694         struct scatterlist bufferlist[3];
695         u64 aligned_data = 0;
696         u32 pfncount = NUM_PAGES_SPANNED(multi_pagebuffer->offset,
697                                          multi_pagebuffer->len);
698
699
700         if ((pfncount < 0) || (pfncount > MAX_MULTIPAGE_BUFFER_COUNT))
701                 return -EINVAL;
702
703         /*
704          * Adjust the size down since vmbus_channel_packet_multipage_buffer is
705          * the largest size we support
706          */
707         descsize = sizeof(struct vmbus_channel_packet_multipage_buffer) -
708                           ((MAX_MULTIPAGE_BUFFER_COUNT - pfncount) *
709                           sizeof(u64));
710         packetlen = descsize + bufferlen;
711         packetlen_aligned = ALIGN(packetlen, sizeof(u64));
712
713
714         /* Setup the descriptor */
715         desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
716         desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
717         desc.dataoffset8 = descsize >> 3; /* in 8-bytes grandularity */
718         desc.length8 = (u16)(packetlen_aligned >> 3);
719         desc.transactionid = requestid;
720         desc.rangecount = 1;
721
722         desc.range.len = multi_pagebuffer->len;
723         desc.range.offset = multi_pagebuffer->offset;
724
725         memcpy(desc.range.pfn_array, multi_pagebuffer->pfn_array,
726                pfncount * sizeof(u64));
727
728         sg_init_table(bufferlist, 3);
729         sg_set_buf(&bufferlist[0], &desc, descsize);
730         sg_set_buf(&bufferlist[1], buffer, bufferlen);
731         sg_set_buf(&bufferlist[2], &aligned_data,
732                 packetlen_aligned - packetlen);
733
734         ret = hv_ringbuffer_write(&channel->outbound, bufferlist, 3);
735
736         if (ret == 0 && !hv_get_ringbuffer_interrupt_mask(&channel->outbound))
737                 vmbus_setevent(channel);
738
739         return ret;
740 }
741 EXPORT_SYMBOL_GPL(vmbus_sendpacket_multipagebuffer);
742
743 /**
744  * vmbus_recvpacket() - Retrieve the user packet on the specified channel
745  * @channel: Pointer to vmbus_channel structure.
746  * @buffer: Pointer to the buffer you want to receive the data into.
747  * @bufferlen: Maximum size of what the the buffer will hold
748  * @buffer_actual_len: The actual size of the data after it was received
749  * @requestid: Identifier of the request
750  *
751  * Receives directly from the hyper-v vmbus and puts the data it received
752  * into Buffer. This will receive the data unparsed from hyper-v.
753  *
754  * Mainly used by Hyper-V drivers.
755  */
756 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
757                         u32 bufferlen, u32 *buffer_actual_len, u64 *requestid)
758 {
759         struct vmpacket_descriptor desc;
760         u32 packetlen;
761         u32 userlen;
762         int ret;
763
764         *buffer_actual_len = 0;
765         *requestid = 0;
766
767
768         ret = hv_ringbuffer_peek(&channel->inbound, &desc,
769                              sizeof(struct vmpacket_descriptor));
770         if (ret != 0)
771                 return 0;
772
773         packetlen = desc.len8 << 3;
774         userlen = packetlen - (desc.offset8 << 3);
775
776         *buffer_actual_len = userlen;
777
778         if (userlen > bufferlen) {
779
780                 pr_err("Buffer too small - got %d needs %d\n",
781                            bufferlen, userlen);
782                 return -ETOOSMALL;
783         }
784
785         *requestid = desc.trans_id;
786
787         /* Copy over the packet to the user buffer */
788         ret = hv_ringbuffer_read(&channel->inbound, buffer, userlen,
789                              (desc.offset8 << 3));
790
791
792         return 0;
793 }
794 EXPORT_SYMBOL(vmbus_recvpacket);
795
796 /*
797  * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
798  */
799 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
800                               u32 bufferlen, u32 *buffer_actual_len,
801                               u64 *requestid)
802 {
803         struct vmpacket_descriptor desc;
804         u32 packetlen;
805         u32 userlen;
806         int ret;
807
808         *buffer_actual_len = 0;
809         *requestid = 0;
810
811
812         ret = hv_ringbuffer_peek(&channel->inbound, &desc,
813                              sizeof(struct vmpacket_descriptor));
814         if (ret != 0)
815                 return 0;
816
817
818         packetlen = desc.len8 << 3;
819         userlen = packetlen - (desc.offset8 << 3);
820
821         *buffer_actual_len = packetlen;
822
823         if (packetlen > bufferlen) {
824                 pr_err("Buffer too small - needed %d bytes but "
825                         "got space for only %d bytes\n",
826                         packetlen, bufferlen);
827                 return -ENOBUFS;
828         }
829
830         *requestid = desc.trans_id;
831
832         /* Copy over the entire packet to the user buffer */
833         ret = hv_ringbuffer_read(&channel->inbound, buffer, packetlen, 0);
834
835         return 0;
836 }
837 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);