aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/hyperv/vmbus/vmbus_brvar.h
blob: 883b74e92d951a354e08a931304e8e1b8df1ce44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*-
 * Copyright (c) 2009-2012,2016 Microsoft Corp.
 * Copyright (c) 2012 NetApp Inc.
 * Copyright (c) 2012 Citrix Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _VMBUS_BRVAR_H_
#define _VMBUS_BRVAR_H_

#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/_iovec.h>

struct vmbus_br {
	struct vmbus_bufring	*vbr;
	uint32_t		vbr_dsize;	/* total data size */
};

#define vbr_windex		vbr->br_windex
#define vbr_rindex		vbr->br_rindex
#define vbr_imask		vbr->br_imask
#define vbr_psndsz		vbr->br_pending_snd_sz
#define vbr_fpsndsz		vbr->br_feature_bits.feat_pending_snd_sz
#define vbr_fvalue		vbr->br_feature_bits.value
#define vbr_intrcnt		vbr->br_g2h_intr_cnt
#define vbr_data		vbr->br_data

struct vmbus_rxbr {
	struct mtx		rxbr_lock;
	struct vmbus_br		rxbr;
};

#define rxbr_windex		rxbr.vbr_windex
#define rxbr_rindex		rxbr.vbr_rindex
#define rxbr_imask		rxbr.vbr_imask
#define rxbr_psndsz		rxbr.vbr_psndsz
#define rxbr_fpsndsz		rxbr.vbr_fpsndsz
#define rxbr_fvalue		rxbr.vbr_fvalue
#define rxbr_intrcnt		rxbr.vbr_intrcnt
#define rxbr_data		rxbr.vbr_data
#define rxbr_dsize		rxbr.vbr_dsize

struct vmbus_txbr {
	struct mtx		txbr_lock;
	struct vmbus_br		txbr;
};

#define txbr_windex		txbr.vbr_windex
#define txbr_rindex		txbr.vbr_rindex
#define txbr_imask		txbr.vbr_imask
#define txbr_psndsz		txbr.vbr_psndsz
#define txbr_fpsndsz		txbr.vbr_fpsndsz
#define txbr_fvalue		txbr.vbr_fvalue
#define txbr_intrcnt		txbr.vbr_intrcnt
#define txbr_data		txbr.vbr_data
#define txbr_dsize		txbr.vbr_dsize

struct sysctl_ctx_list;
struct sysctl_oid;

static __inline int
vmbus_txbr_maxpktsz(const struct vmbus_txbr *tbr)
{

	/*
	 * - 64 bits for the trailing start index (- sizeof(uint64_t)).
	 * - The rindex and windex can't be same (- 1).  See
	 *   the comment near vmbus_bufring.br_{r,w}index.
	 */
	return (tbr->txbr_dsize - sizeof(uint64_t) - 1);
}

static __inline bool
vmbus_txbr_empty(const struct vmbus_txbr *tbr)
{

	return (tbr->txbr_windex == tbr->txbr_rindex ? true : false);
}

static __inline bool
vmbus_rxbr_empty(const struct vmbus_rxbr *rbr)
{

	return (rbr->rxbr_windex == rbr->rxbr_rindex ? true : false);
}

static __inline int
vmbus_br_nelem(int br_size, int elem_size)
{

	/* Strip bufring header */
	br_size -= sizeof(struct vmbus_bufring);
	/* Add per-element trailing index */
	elem_size += sizeof(uint64_t);
	return (br_size / elem_size);
}

void		vmbus_br_sysctl_create(struct sysctl_ctx_list *ctx,
		    struct sysctl_oid *br_tree, struct vmbus_br *br,
		    const char *name);

void		vmbus_rxbr_init(struct vmbus_rxbr *rbr);
void		vmbus_rxbr_deinit(struct vmbus_rxbr *rbr);
void		vmbus_rxbr_setup(struct vmbus_rxbr *rbr, void *buf, int blen);
int		vmbus_rxbr_peek(struct vmbus_rxbr *rbr, void *data, int dlen);
int		vmbus_rxbr_read(struct vmbus_rxbr *rbr, void *data, int dlen,
		    uint32_t skip);
int		vmbus_rxbr_idxadv(struct vmbus_rxbr *rbr, uint32_t idx_adv,
		    boolean_t *need_sig);
int		vmbus_rxbr_idxadv_peek(struct vmbus_rxbr *rbr, void *data,
		    int dlen, uint32_t idx_adv, boolean_t *need_sig);
int		vmbus_rxbr_peek_call(struct vmbus_rxbr *rbr, int dlen,
		    uint32_t skip, vmbus_br_copy_callback_t cb, void *cbarg);
void		vmbus_rxbr_intr_mask(struct vmbus_rxbr *rbr);
uint32_t	vmbus_rxbr_intr_unmask(struct vmbus_rxbr *rbr);
uint32_t	vmbus_rxbr_available(const struct vmbus_rxbr *rbr);

void		vmbus_txbr_init(struct vmbus_txbr *tbr);
void		vmbus_txbr_deinit(struct vmbus_txbr *tbr);
void		vmbus_txbr_setup(struct vmbus_txbr *tbr, void *buf, int blen);
int		vmbus_txbr_write(struct vmbus_txbr *tbr,
		    const struct iovec iov[], int iovlen, boolean_t *need_sig);
int		vmbus_txbr_write_call(struct vmbus_txbr *tbr,
		    const struct iovec iov[], int iovlen,
		    vmbus_br_copy_callback_t cb, void *cbarg,
		    boolean_t *need_sig);
uint32_t	vmbus_txbr_available(const struct vmbus_txbr *tbr);
uint32_t	vmbus_txbr_get_imask(const struct vmbus_txbr *tbr);
void		vmbus_txbr_set_pending_snd_sz(struct vmbus_txbr *tbr,
		    uint32_t size);

#endif  /* _VMBUS_BRVAR_H_ */