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