Staging: hv: vmbus: Change the signature of struct hv_driver probe function
[pandora-kernel.git] / drivers / staging / hv / hyperv.h
1 /*
2  *
3  * Copyright (c) 2011, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *   K. Y. Srinivasan <kys@microsoft.com>
22  *
23  */
24
25 #ifndef _HYPERV_H
26 #define _HYPERV_H
27
28 #include <linux/scatterlist.h>
29 #include <linux/list.h>
30 #include <linux/uuid.h>
31 #include <linux/timer.h>
32 #include <linux/workqueue.h>
33 #include <linux/completion.h>
34 #include <linux/device.h>
35 #include <linux/mod_devicetable.h>
36
37
38 #include <asm/hyperv.h>
39
40
41 #define MAX_PAGE_BUFFER_COUNT                           16
42 #define MAX_MULTIPAGE_BUFFER_COUNT                      32 /* 128K */
43
44 #pragma pack(push, 1)
45
46 /* Single-page buffer */
47 struct hv_page_buffer {
48         u32 len;
49         u32 offset;
50         u64 pfn;
51 };
52
53 /* Multiple-page buffer */
54 struct hv_multipage_buffer {
55         /* Length and Offset determines the # of pfns in the array */
56         u32 len;
57         u32 offset;
58         u64 pfn_array[MAX_MULTIPAGE_BUFFER_COUNT];
59 };
60
61 /* 0x18 includes the proprietary packet header */
62 #define MAX_PAGE_BUFFER_PACKET          (0x18 +                 \
63                                         (sizeof(struct hv_page_buffer) * \
64                                          MAX_PAGE_BUFFER_COUNT))
65 #define MAX_MULTIPAGE_BUFFER_PACKET     (0x18 +                 \
66                                          sizeof(struct hv_multipage_buffer))
67
68
69 #pragma pack(pop)
70
71 struct hv_ring_buffer {
72         /* Offset in bytes from the start of ring data below */
73         u32 write_index;
74
75         /* Offset in bytes from the start of ring data below */
76         u32 read_index;
77
78         u32 interrupt_mask;
79
80         /* Pad it to PAGE_SIZE so that data starts on page boundary */
81         u8      reserved[4084];
82
83         /* NOTE:
84          * The interrupt_mask field is used only for channels but since our
85          * vmbus connection also uses this data structure and its data starts
86          * here, we commented out this field.
87          */
88
89         /*
90          * Ring data starts here + RingDataStartOffset
91          * !!! DO NOT place any fields below this !!!
92          */
93         u8 buffer[0];
94 } __packed;
95
96 struct hv_ring_buffer_info {
97         struct hv_ring_buffer *ring_buffer;
98         u32 ring_size;                  /* Include the shared header */
99         spinlock_t ring_lock;
100
101         u32 ring_datasize;              /* < ring_size */
102         u32 ring_data_startoffset;
103 };
104
105 struct hv_ring_buffer_debug_info {
106         u32 current_interrupt_mask;
107         u32 current_read_index;
108         u32 current_write_index;
109         u32 bytes_avail_toread;
110         u32 bytes_avail_towrite;
111 };
112
113 /*
114  * We use the same version numbering for all Hyper-V modules.
115  *
116  * Definition of versioning is as follows;
117  *
118  *      Major Number    Changes for these scenarios;
119  *                      1.      When a new version of Windows Hyper-V
120  *                              is released.
121  *                      2.      A Major change has occurred in the
122  *                              Linux IC's.
123  *                      (For example the merge for the first time
124  *                      into the kernel) Every time the Major Number
125  *                      changes, the Revision number is reset to 0.
126  *      Minor Number    Changes when new functionality is added
127  *                      to the Linux IC's that is not a bug fix.
128  *
129  * 3.1 - Added completed hv_utils driver. Shutdown/Heartbeat/Timesync
130  */
131 #define HV_DRV_VERSION           "3.1"
132
133
134 /*
135  * A revision number of vmbus that is used for ensuring both ends on a
136  * partition are using compatible versions.
137  */
138 #define VMBUS_REVISION_NUMBER           13
139
140 /* Make maximum size of pipe payload of 16K */
141 #define MAX_PIPE_DATA_PAYLOAD           (sizeof(u8) * 16384)
142
143 /* Define PipeMode values. */
144 #define VMBUS_PIPE_TYPE_BYTE            0x00000000
145 #define VMBUS_PIPE_TYPE_MESSAGE         0x00000004
146
147 /* The size of the user defined data buffer for non-pipe offers. */
148 #define MAX_USER_DEFINED_BYTES          120
149
150 /* The size of the user defined data buffer for pipe offers. */
151 #define MAX_PIPE_USER_DEFINED_BYTES     116
152
153 /*
154  * At the center of the Channel Management library is the Channel Offer. This
155  * struct contains the fundamental information about an offer.
156  */
157 struct vmbus_channel_offer {
158         uuid_le if_type;
159         uuid_le if_instance;
160         u64 int_latency; /* in 100ns units */
161         u32 if_revision;
162         u32 server_ctx_size;    /* in bytes */
163         u16 chn_flags;
164         u16 mmio_megabytes;             /* in bytes * 1024 * 1024 */
165
166         union {
167                 /* Non-pipes: The user has MAX_USER_DEFINED_BYTES bytes. */
168                 struct {
169                         unsigned char user_def[MAX_USER_DEFINED_BYTES];
170                 } std;
171
172                 /*
173                  * Pipes:
174                  * The following sructure is an integrated pipe protocol, which
175                  * is implemented on top of standard user-defined data. Pipe
176                  * clients have MAX_PIPE_USER_DEFINED_BYTES left for their own
177                  * use.
178                  */
179                 struct {
180                         u32  pipe_mode;
181                         unsigned char user_def[MAX_PIPE_USER_DEFINED_BYTES];
182                 } pipe;
183         } u;
184         u32 padding;
185 } __packed;
186
187 /* Server Flags */
188 #define VMBUS_CHANNEL_ENUMERATE_DEVICE_INTERFACE        1
189 #define VMBUS_CHANNEL_SERVER_SUPPORTS_TRANSFER_PAGES    2
190 #define VMBUS_CHANNEL_SERVER_SUPPORTS_GPADLS            4
191 #define VMBUS_CHANNEL_NAMED_PIPE_MODE                   0x10
192 #define VMBUS_CHANNEL_LOOPBACK_OFFER                    0x100
193 #define VMBUS_CHANNEL_PARENT_OFFER                      0x200
194 #define VMBUS_CHANNEL_REQUEST_MONITORED_NOTIFICATION    0x400
195
196 struct vmpacket_descriptor {
197         u16 type;
198         u16 offset8;
199         u16 len8;
200         u16 flags;
201         u64 trans_id;
202 } __packed;
203
204 struct vmpacket_header {
205         u32 prev_pkt_start_offset;
206         struct vmpacket_descriptor descriptor;
207 } __packed;
208
209 struct vmtransfer_page_range {
210         u32 byte_count;
211         u32 byte_offset;
212 } __packed;
213
214 struct vmtransfer_page_packet_header {
215         struct vmpacket_descriptor d;
216         u16 xfer_pageset_id;
217         bool sender_owns_set;
218         u8 reserved;
219         u32 range_cnt;
220         struct vmtransfer_page_range ranges[1];
221 } __packed;
222
223 struct vmgpadl_packet_header {
224         struct vmpacket_descriptor d;
225         u32 gpadl;
226         u32 reserved;
227 } __packed;
228
229 struct vmadd_remove_transfer_page_set {
230         struct vmpacket_descriptor d;
231         u32 gpadl;
232         u16 xfer_pageset_id;
233         u16 reserved;
234 } __packed;
235
236 /*
237  * This structure defines a range in guest physical space that can be made to
238  * look virtually contiguous.
239  */
240 struct gpa_range {
241         u32 byte_count;
242         u32 byte_offset;
243         u64 pfn_array[0];
244 };
245
246 /*
247  * This is the format for an Establish Gpadl packet, which contains a handle by
248  * which this GPADL will be known and a set of GPA ranges associated with it.
249  * This can be converted to a MDL by the guest OS.  If there are multiple GPA
250  * ranges, then the resulting MDL will be "chained," representing multiple VA
251  * ranges.
252  */
253 struct vmestablish_gpadl {
254         struct vmpacket_descriptor d;
255         u32 gpadl;
256         u32 range_cnt;
257         struct gpa_range range[1];
258 } __packed;
259
260 /*
261  * This is the format for a Teardown Gpadl packet, which indicates that the
262  * GPADL handle in the Establish Gpadl packet will never be referenced again.
263  */
264 struct vmteardown_gpadl {
265         struct vmpacket_descriptor d;
266         u32 gpadl;
267         u32 reserved;   /* for alignment to a 8-byte boundary */
268 } __packed;
269
270 /*
271  * This is the format for a GPA-Direct packet, which contains a set of GPA
272  * ranges, in addition to commands and/or data.
273  */
274 struct vmdata_gpa_direct {
275         struct vmpacket_descriptor d;
276         u32 reserved;
277         u32 range_cnt;
278         struct gpa_range range[1];
279 } __packed;
280
281 /* This is the format for a Additional Data Packet. */
282 struct vmadditional_data {
283         struct vmpacket_descriptor d;
284         u64 total_bytes;
285         u32 offset;
286         u32 byte_cnt;
287         unsigned char data[1];
288 } __packed;
289
290 union vmpacket_largest_possible_header {
291         struct vmpacket_descriptor simple_hdr;
292         struct vmtransfer_page_packet_header xfer_page_hdr;
293         struct vmgpadl_packet_header gpadl_hdr;
294         struct vmadd_remove_transfer_page_set add_rm_xfer_page_hdr;
295         struct vmestablish_gpadl establish_gpadl_hdr;
296         struct vmteardown_gpadl teardown_gpadl_hdr;
297         struct vmdata_gpa_direct data_gpa_direct_hdr;
298 };
299
300 #define VMPACKET_DATA_START_ADDRESS(__packet)   \
301         (void *)(((unsigned char *)__packet) +  \
302          ((struct vmpacket_descriptor)__packet)->offset8 * 8)
303
304 #define VMPACKET_DATA_LENGTH(__packet)          \
305         ((((struct vmpacket_descriptor)__packet)->len8 -        \
306           ((struct vmpacket_descriptor)__packet)->offset8) * 8)
307
308 #define VMPACKET_TRANSFER_MODE(__packet)        \
309         (((struct IMPACT)__packet)->type)
310
311 enum vmbus_packet_type {
312         VM_PKT_INVALID                          = 0x0,
313         VM_PKT_SYNCH                            = 0x1,
314         VM_PKT_ADD_XFER_PAGESET                 = 0x2,
315         VM_PKT_RM_XFER_PAGESET                  = 0x3,
316         VM_PKT_ESTABLISH_GPADL                  = 0x4,
317         VM_PKT_TEARDOWN_GPADL                   = 0x5,
318         VM_PKT_DATA_INBAND                      = 0x6,
319         VM_PKT_DATA_USING_XFER_PAGES            = 0x7,
320         VM_PKT_DATA_USING_GPADL                 = 0x8,
321         VM_PKT_DATA_USING_GPA_DIRECT            = 0x9,
322         VM_PKT_CANCEL_REQUEST                   = 0xa,
323         VM_PKT_COMP                             = 0xb,
324         VM_PKT_DATA_USING_ADDITIONAL_PKT        = 0xc,
325         VM_PKT_ADDITIONAL_DATA                  = 0xd
326 };
327
328 #define VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED     1
329
330
331 /* Version 1 messages */
332 enum vmbus_channel_message_type {
333         CHANNELMSG_INVALID                      =  0,
334         CHANNELMSG_OFFERCHANNEL         =  1,
335         CHANNELMSG_RESCIND_CHANNELOFFER =  2,
336         CHANNELMSG_REQUESTOFFERS                =  3,
337         CHANNELMSG_ALLOFFERS_DELIVERED  =  4,
338         CHANNELMSG_OPENCHANNEL          =  5,
339         CHANNELMSG_OPENCHANNEL_RESULT           =  6,
340         CHANNELMSG_CLOSECHANNEL         =  7,
341         CHANNELMSG_GPADL_HEADER         =  8,
342         CHANNELMSG_GPADL_BODY                   =  9,
343         CHANNELMSG_GPADL_CREATED                = 10,
344         CHANNELMSG_GPADL_TEARDOWN               = 11,
345         CHANNELMSG_GPADL_TORNDOWN               = 12,
346         CHANNELMSG_RELID_RELEASED               = 13,
347         CHANNELMSG_INITIATE_CONTACT             = 14,
348         CHANNELMSG_VERSION_RESPONSE             = 15,
349         CHANNELMSG_UNLOAD                       = 16,
350 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
351         CHANNELMSG_VIEWRANGE_ADD                = 17,
352         CHANNELMSG_VIEWRANGE_REMOVE             = 18,
353 #endif
354         CHANNELMSG_COUNT
355 };
356
357 struct vmbus_channel_message_header {
358         enum vmbus_channel_message_type msgtype;
359         u32 padding;
360 } __packed;
361
362 /* Query VMBus Version parameters */
363 struct vmbus_channel_query_vmbus_version {
364         struct vmbus_channel_message_header header;
365         u32 version;
366 } __packed;
367
368 /* VMBus Version Supported parameters */
369 struct vmbus_channel_version_supported {
370         struct vmbus_channel_message_header header;
371         bool version_supported;
372 } __packed;
373
374 /* Offer Channel parameters */
375 struct vmbus_channel_offer_channel {
376         struct vmbus_channel_message_header header;
377         struct vmbus_channel_offer offer;
378         u32 child_relid;
379         u8 monitorid;
380         bool monitor_allocated;
381 } __packed;
382
383 /* Rescind Offer parameters */
384 struct vmbus_channel_rescind_offer {
385         struct vmbus_channel_message_header header;
386         u32 child_relid;
387 } __packed;
388
389 /*
390  * Request Offer -- no parameters, SynIC message contains the partition ID
391  * Set Snoop -- no parameters, SynIC message contains the partition ID
392  * Clear Snoop -- no parameters, SynIC message contains the partition ID
393  * All Offers Delivered -- no parameters, SynIC message contains the partition
394  *                         ID
395  * Flush Client -- no parameters, SynIC message contains the partition ID
396  */
397
398 /* Open Channel parameters */
399 struct vmbus_channel_open_channel {
400         struct vmbus_channel_message_header header;
401
402         /* Identifies the specific VMBus channel that is being opened. */
403         u32 child_relid;
404
405         /* ID making a particular open request at a channel offer unique. */
406         u32 openid;
407
408         /* GPADL for the channel's ring buffer. */
409         u32 ringbuffer_gpadlhandle;
410
411         /* GPADL for the channel's server context save area. */
412         u32 server_contextarea_gpadlhandle;
413
414         /*
415         * The upstream ring buffer begins at offset zero in the memory
416         * described by RingBufferGpadlHandle. The downstream ring buffer
417         * follows it at this offset (in pages).
418         */
419         u32 downstream_ringbuffer_pageoffset;
420
421         /* User-specific data to be passed along to the server endpoint. */
422         unsigned char userdata[MAX_USER_DEFINED_BYTES];
423 } __packed;
424
425 /* Open Channel Result parameters */
426 struct vmbus_channel_open_result {
427         struct vmbus_channel_message_header header;
428         u32 child_relid;
429         u32 openid;
430         u32 status;
431 } __packed;
432
433 /* Close channel parameters; */
434 struct vmbus_channel_close_channel {
435         struct vmbus_channel_message_header header;
436         u32 child_relid;
437 } __packed;
438
439 /* Channel Message GPADL */
440 #define GPADL_TYPE_RING_BUFFER          1
441 #define GPADL_TYPE_SERVER_SAVE_AREA     2
442 #define GPADL_TYPE_TRANSACTION          8
443
444 /*
445  * The number of PFNs in a GPADL message is defined by the number of
446  * pages that would be spanned by ByteCount and ByteOffset.  If the
447  * implied number of PFNs won't fit in this packet, there will be a
448  * follow-up packet that contains more.
449  */
450 struct vmbus_channel_gpadl_header {
451         struct vmbus_channel_message_header header;
452         u32 child_relid;
453         u32 gpadl;
454         u16 range_buflen;
455         u16 rangecount;
456         struct gpa_range range[0];
457 } __packed;
458
459 /* This is the followup packet that contains more PFNs. */
460 struct vmbus_channel_gpadl_body {
461         struct vmbus_channel_message_header header;
462         u32 msgnumber;
463         u32 gpadl;
464         u64 pfn[0];
465 } __packed;
466
467 struct vmbus_channel_gpadl_created {
468         struct vmbus_channel_message_header header;
469         u32 child_relid;
470         u32 gpadl;
471         u32 creation_status;
472 } __packed;
473
474 struct vmbus_channel_gpadl_teardown {
475         struct vmbus_channel_message_header header;
476         u32 child_relid;
477         u32 gpadl;
478 } __packed;
479
480 struct vmbus_channel_gpadl_torndown {
481         struct vmbus_channel_message_header header;
482         u32 gpadl;
483 } __packed;
484
485 #ifdef VMBUS_FEATURE_PARENT_OR_PEER_MEMORY_MAPPED_INTO_A_CHILD
486 struct vmbus_channel_view_range_add {
487         struct vmbus_channel_message_header header;
488         PHYSICAL_ADDRESS viewrange_base;
489         u64 viewrange_length;
490         u32 child_relid;
491 } __packed;
492
493 struct vmbus_channel_view_range_remove {
494         struct vmbus_channel_message_header header;
495         PHYSICAL_ADDRESS viewrange_base;
496         u32 child_relid;
497 } __packed;
498 #endif
499
500 struct vmbus_channel_relid_released {
501         struct vmbus_channel_message_header header;
502         u32 child_relid;
503 } __packed;
504
505 struct vmbus_channel_initiate_contact {
506         struct vmbus_channel_message_header header;
507         u32 vmbus_version_requested;
508         u32 padding2;
509         u64 interrupt_page;
510         u64 monitor_page1;
511         u64 monitor_page2;
512 } __packed;
513
514 struct vmbus_channel_version_response {
515         struct vmbus_channel_message_header header;
516         bool version_supported;
517 } __packed;
518
519 enum vmbus_channel_state {
520         CHANNEL_OFFER_STATE,
521         CHANNEL_OPENING_STATE,
522         CHANNEL_OPEN_STATE,
523 };
524
525 struct vmbus_channel_debug_info {
526         u32 relid;
527         enum vmbus_channel_state state;
528         uuid_le interfacetype;
529         uuid_le interface_instance;
530         u32 monitorid;
531         u32 servermonitor_pending;
532         u32 servermonitor_latency;
533         u32 servermonitor_connectionid;
534         u32 clientmonitor_pending;
535         u32 clientmonitor_latency;
536         u32 clientmonitor_connectionid;
537
538         struct hv_ring_buffer_debug_info inbound;
539         struct hv_ring_buffer_debug_info outbound;
540 };
541
542 /*
543  * Represents each channel msg on the vmbus connection This is a
544  * variable-size data structure depending on the msg type itself
545  */
546 struct vmbus_channel_msginfo {
547         /* Bookkeeping stuff */
548         struct list_head msglistentry;
549
550         /* So far, this is only used to handle gpadl body message */
551         struct list_head submsglist;
552
553         /* Synchronize the request/response if needed */
554         struct completion  waitevent;
555         union {
556                 struct vmbus_channel_version_supported version_supported;
557                 struct vmbus_channel_open_result open_result;
558                 struct vmbus_channel_gpadl_torndown gpadl_torndown;
559                 struct vmbus_channel_gpadl_created gpadl_created;
560                 struct vmbus_channel_version_response version_response;
561         } response;
562
563         u32 msgsize;
564         /*
565          * The channel message that goes out on the "wire".
566          * It will contain at minimum the VMBUS_CHANNEL_MESSAGE_HEADER header
567          */
568         unsigned char msg[0];
569 };
570
571 struct vmbus_close_msg {
572         struct vmbus_channel_msginfo info;
573         struct vmbus_channel_close_channel msg;
574 };
575
576 struct vmbus_channel {
577         struct list_head listentry;
578
579         struct hv_device *device_obj;
580
581         struct work_struct work;
582
583         enum vmbus_channel_state state;
584         /*
585          * For util channels, stash the
586          * the service index for easy access.
587          */
588         s8 util_index;
589
590         struct vmbus_channel_offer_channel offermsg;
591         /*
592          * These are based on the OfferMsg.MonitorId.
593          * Save it here for easy access.
594          */
595         u8 monitor_grp;
596         u8 monitor_bit;
597
598         u32 ringbuffer_gpadlhandle;
599
600         /* Allocated memory for ring buffer */
601         void *ringbuffer_pages;
602         u32 ringbuffer_pagecount;
603         struct hv_ring_buffer_info outbound;    /* send to parent */
604         struct hv_ring_buffer_info inbound;     /* receive from parent */
605         spinlock_t inbound_lock;
606         struct workqueue_struct *controlwq;
607
608         struct vmbus_close_msg close_msg;
609
610         /* Channel callback are invoked in this workqueue context */
611         /* HANDLE dataWorkQueue; */
612
613         void (*onchannel_callback)(void *context);
614         void *channel_callback_context;
615 };
616
617 void free_channel(struct vmbus_channel *channel);
618
619 void vmbus_onmessage(void *context);
620
621 int vmbus_request_offers(void);
622
623 /* The format must be the same as struct vmdata_gpa_direct */
624 struct vmbus_channel_packet_page_buffer {
625         u16 type;
626         u16 dataoffset8;
627         u16 length8;
628         u16 flags;
629         u64 transactionid;
630         u32 reserved;
631         u32 rangecount;
632         struct hv_page_buffer range[MAX_PAGE_BUFFER_COUNT];
633 } __packed;
634
635 /* The format must be the same as struct vmdata_gpa_direct */
636 struct vmbus_channel_packet_multipage_buffer {
637         u16 type;
638         u16 dataoffset8;
639         u16 length8;
640         u16 flags;
641         u64 transactionid;
642         u32 reserved;
643         u32 rangecount;         /* Always 1 in this case */
644         struct hv_multipage_buffer range;
645 } __packed;
646
647
648 extern int vmbus_open(struct vmbus_channel *channel,
649                             u32 send_ringbuffersize,
650                             u32 recv_ringbuffersize,
651                             void *userdata,
652                             u32 userdatalen,
653                             void(*onchannel_callback)(void *context),
654                             void *context);
655
656 extern void vmbus_close(struct vmbus_channel *channel);
657
658 extern int vmbus_sendpacket(struct vmbus_channel *channel,
659                                   const void *buffer,
660                                   u32 bufferLen,
661                                   u64 requestid,
662                                   enum vmbus_packet_type type,
663                                   u32 flags);
664
665 extern int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
666                                             struct hv_page_buffer pagebuffers[],
667                                             u32 pagecount,
668                                             void *buffer,
669                                             u32 bufferlen,
670                                             u64 requestid);
671
672 extern int vmbus_sendpacket_multipagebuffer(struct vmbus_channel *channel,
673                                         struct hv_multipage_buffer *mpb,
674                                         void *buffer,
675                                         u32 bufferlen,
676                                         u64 requestid);
677
678 extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
679                                       void *kbuffer,
680                                       u32 size,
681                                       u32 *gpadl_handle);
682
683 extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
684                                      u32 gpadl_handle);
685
686 extern int vmbus_recvpacket(struct vmbus_channel *channel,
687                                   void *buffer,
688                                   u32 bufferlen,
689                                   u32 *buffer_actual_len,
690                                   u64 *requestid);
691
692 extern int vmbus_recvpacket_raw(struct vmbus_channel *channel,
693                                      void *buffer,
694                                      u32 bufferlen,
695                                      u32 *buffer_actual_len,
696                                      u64 *requestid);
697
698
699 extern void vmbus_get_debug_info(struct vmbus_channel *channel,
700                                      struct vmbus_channel_debug_info *debug);
701
702 extern void vmbus_ontimer(unsigned long data);
703
704
705 #define LOWORD(dw) ((unsigned short)(dw))
706 #define HIWORD(dw) ((unsigned short)(((unsigned int) (dw) >> 16) & 0xFFFF))
707
708
709 #define VMBUS                           0x0001
710 #define STORVSC                         0x0002
711 #define NETVSC                          0x0004
712 #define INPUTVSC                        0x0008
713 #define BLKVSC                          0x0010
714 #define VMBUS_DRV                       0x0100
715 #define STORVSC_DRV                     0x0200
716 #define NETVSC_DRV                      0x0400
717 #define INPUTVSC_DRV            0x0800
718 #define BLKVSC_DRV                      0x1000
719
720 #define ALL_MODULES                     (VMBUS          |\
721                                                         STORVSC         |\
722                                                         NETVSC          |\
723                                                         INPUTVSC        |\
724                                                         BLKVSC          |\
725                                                         VMBUS_DRV       |\
726                                                         STORVSC_DRV     |\
727                                                         NETVSC_DRV      |\
728                                                         INPUTVSC_DRV|\
729                                                         BLKVSC_DRV)
730
731 /* Logging Level */
732 #define ERROR_LVL                               3
733 #define WARNING_LVL                             4
734 #define INFO_LVL                                6
735 #define DEBUG_LVL                               7
736 #define DEBUG_LVL_ENTEREXIT                     8
737 #define DEBUG_RING_LVL                          9
738
739 extern unsigned int vmbus_loglevel;
740
741 #define DPRINT(mod, lvl, fmt, args...) do {\
742         if ((mod & (HIWORD(vmbus_loglevel))) && \
743             (lvl <= LOWORD(vmbus_loglevel)))    \
744                 printk(KERN_DEBUG #mod": %s() " fmt "\n", __func__, ## args);\
745         } while (0)
746
747 #define DPRINT_DBG(mod, fmt, args...) do {\
748         if ((mod & (HIWORD(vmbus_loglevel))) &&         \
749             (DEBUG_LVL <= LOWORD(vmbus_loglevel)))      \
750                 printk(KERN_DEBUG #mod": %s() " fmt "\n", __func__, ## args);\
751         } while (0)
752
753 #define DPRINT_INFO(mod, fmt, args...) do {\
754         if ((mod & (HIWORD(vmbus_loglevel))) &&         \
755             (INFO_LVL <= LOWORD(vmbus_loglevel)))       \
756                 printk(KERN_INFO #mod": " fmt "\n", ## args);\
757         } while (0)
758
759 #define DPRINT_WARN(mod, fmt, args...) do {\
760         if ((mod & (HIWORD(vmbus_loglevel))) &&         \
761             (WARNING_LVL <= LOWORD(vmbus_loglevel)))    \
762                 printk(KERN_WARNING #mod": WARNING! " fmt "\n", ## args);\
763         } while (0)
764
765 #define DPRINT_ERR(mod, fmt, args...) do {\
766         if ((mod & (HIWORD(vmbus_loglevel))) &&         \
767             (ERROR_LVL <= LOWORD(vmbus_loglevel)))      \
768                 printk(KERN_ERR #mod": %s() ERROR!! " fmt "\n", \
769                        __func__, ## args);\
770         } while (0)
771
772
773
774 struct hv_driver;
775 struct hv_device;
776
777 struct hv_dev_port_info {
778         u32 int_mask;
779         u32 read_idx;
780         u32 write_idx;
781         u32 bytes_avail_toread;
782         u32 bytes_avail_towrite;
783 };
784
785 struct hv_device_info {
786         u32 chn_id;
787         u32 chn_state;
788         uuid_le chn_type;
789         uuid_le chn_instance;
790
791         u32 monitor_id;
792         u32 server_monitor_pending;
793         u32 server_monitor_latency;
794         u32 server_monitor_conn_id;
795         u32 client_monitor_pending;
796         u32 client_monitor_latency;
797         u32 client_monitor_conn_id;
798
799         struct hv_dev_port_info inbound;
800         struct hv_dev_port_info outbound;
801 };
802
803 /* Base driver object */
804 struct hv_driver {
805         const char *name;
806
807         /* the device type supported by this driver */
808         uuid_le dev_type;
809         const struct hv_vmbus_device_id *id_table;
810
811         struct device_driver driver;
812
813         int (*probe)(struct hv_device *, const struct hv_vmbus_device_id *);
814         int (*remove)(struct hv_device *);
815         void (*shutdown)(struct hv_device *);
816
817 };
818
819 /* Base device object */
820 struct hv_device {
821         /* the device type id of this device */
822         uuid_le dev_type;
823
824         /* the device instance id of this device */
825         uuid_le dev_instance;
826
827         struct device device;
828
829         struct vmbus_channel *channel;
830
831         /* Device extension; */
832         void *ext;
833 };
834
835
836 static inline struct hv_device *device_to_hv_device(struct device *d)
837 {
838         return container_of(d, struct hv_device, device);
839 }
840
841 static inline struct hv_driver *drv_to_hv_drv(struct device_driver *d)
842 {
843         return container_of(d, struct hv_driver, driver);
844 }
845
846
847 /* Vmbus interface */
848 #define vmbus_driver_register(driver)   \
849         __vmbus_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
850 int __must_check __vmbus_driver_register(struct hv_driver *hv_driver,
851                                          struct module *owner,
852                                          const char *mod_name);
853 void vmbus_driver_unregister(struct hv_driver *hv_driver);
854
855 /**
856  * VMBUS_DEVICE - macro used to describe a specific hyperv vmbus device
857  *
858  * This macro is used to create a struct hv_vmbus_device_id that matches a
859  * specific device.
860  */
861 #define VMBUS_DEVICE(g0, g1, g2, g3, g4, g5, g6, g7,    \
862                      g8, g9, ga, gb, gc, gd, ge, gf)    \
863         .guid = { g0, g1, g2, g3, g4, g5, g6, g7,       \
864                   g8, g9, ga, gb, gc, gd, ge, gf },
865
866 /*
867  * Common header for Hyper-V ICs
868  */
869
870 #define ICMSGTYPE_NEGOTIATE             0
871 #define ICMSGTYPE_HEARTBEAT             1
872 #define ICMSGTYPE_KVPEXCHANGE           2
873 #define ICMSGTYPE_SHUTDOWN              3
874 #define ICMSGTYPE_TIMESYNC              4
875 #define ICMSGTYPE_VSS                   5
876
877 #define ICMSGHDRFLAG_TRANSACTION        1
878 #define ICMSGHDRFLAG_REQUEST            2
879 #define ICMSGHDRFLAG_RESPONSE           4
880
881 #define HV_S_OK                         0x00000000
882 #define HV_E_FAIL                       0x80004005
883 #define HV_ERROR_NOT_SUPPORTED          0x80070032
884 #define HV_ERROR_MACHINE_LOCKED         0x800704F7
885
886 struct vmbuspipe_hdr {
887         u32 flags;
888         u32 msgsize;
889 } __packed;
890
891 struct ic_version {
892         u16 major;
893         u16 minor;
894 } __packed;
895
896 struct icmsg_hdr {
897         struct ic_version icverframe;
898         u16 icmsgtype;
899         struct ic_version icvermsg;
900         u16 icmsgsize;
901         u32 status;
902         u8 ictransaction_id;
903         u8 icflags;
904         u8 reserved[2];
905 } __packed;
906
907 struct icmsg_negotiate {
908         u16 icframe_vercnt;
909         u16 icmsg_vercnt;
910         u32 reserved;
911         struct ic_version icversion_data[1]; /* any size array */
912 } __packed;
913
914 struct shutdown_msg_data {
915         u32 reason_code;
916         u32 timeout_seconds;
917         u32 flags;
918         u8  display_message[2048];
919 } __packed;
920
921 struct heartbeat_msg_data {
922         u64 seq_num;
923         u32 reserved[8];
924 } __packed;
925
926 /* Time Sync IC defs */
927 #define ICTIMESYNCFLAG_PROBE    0
928 #define ICTIMESYNCFLAG_SYNC     1
929 #define ICTIMESYNCFLAG_SAMPLE   2
930
931 #ifdef __x86_64__
932 #define WLTIMEDELTA     116444736000000000L     /* in 100ns unit */
933 #else
934 #define WLTIMEDELTA     116444736000000000LL
935 #endif
936
937 struct ictimesync_data {
938         u64 parenttime;
939         u64 childtime;
940         u64 roundtriptime;
941         u8 flags;
942 } __packed;
943
944 /* Index for each IC struct in array hv_cb_utils[] */
945 #define HV_SHUTDOWN_MSG         0
946 #define HV_TIMESYNC_MSG         1
947 #define HV_HEARTBEAT_MSG        2
948 #define HV_KVP_MSG              3
949
950 struct hyperv_service_callback {
951         u8 msg_type;
952         char *log_msg;
953         uuid_le data;
954         struct vmbus_channel *channel;
955         void (*callback) (void *context);
956 };
957
958 extern void prep_negotiate_resp(struct icmsg_hdr *,
959                                 struct icmsg_negotiate *, u8 *);
960 extern void chn_cb_negotiate(void *);
961 extern struct hyperv_service_callback hv_cb_utils[];
962
963 #endif /* _HYPERV_H */