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