xref: /freebsd/sys/dev/hyperv/vmbus/vmbus_brvar.h (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _VMBUS_BRVAR_H_
30 #define _VMBUS_BRVAR_H_
31 
32 #include <sys/param.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/_iovec.h>
36 
37 struct vmbus_br {
38 	struct vmbus_bufring	*vbr;
39 	uint32_t		vbr_dsize;	/* total data size */
40 };
41 
42 #define vbr_windex		vbr->br_windex
43 #define vbr_rindex		vbr->br_rindex
44 #define vbr_imask		vbr->br_imask
45 #define vbr_psndsz		vbr->br_pending_snd_sz
46 #define vbr_fpsndsz		vbr->br_feature_bits.feat_pending_snd_sz
47 #define vbr_fvalue		vbr->br_feature_bits.value
48 #define vbr_intrcnt		vbr->br_g2h_intr_cnt
49 #define vbr_data		vbr->br_data
50 
51 struct vmbus_rxbr {
52 	struct mtx		rxbr_lock;
53 	struct vmbus_br		rxbr;
54 };
55 
56 #define rxbr_windex		rxbr.vbr_windex
57 #define rxbr_rindex		rxbr.vbr_rindex
58 #define rxbr_imask		rxbr.vbr_imask
59 #define rxbr_psndsz		rxbr.vbr_psndsz
60 #define rxbr_fpsndsz		rxbr.vbr_fpsndsz
61 #define rxbr_fvalue		rxbr.vbr_fvalue
62 #define rxbr_intrcnt		rxbr.vbr_intrcnt
63 #define rxbr_data		rxbr.vbr_data
64 #define rxbr_dsize		rxbr.vbr_dsize
65 
66 struct vmbus_txbr {
67 	struct mtx		txbr_lock;
68 	struct vmbus_br		txbr;
69 };
70 
71 #define txbr_windex		txbr.vbr_windex
72 #define txbr_rindex		txbr.vbr_rindex
73 #define txbr_imask		txbr.vbr_imask
74 #define txbr_psndsz		txbr.vbr_psndsz
75 #define txbr_fpsndsz		txbr.vbr_fpsndsz
76 #define txbr_fvalue		txbr.vbr_fvalue
77 #define txbr_intrcnt		txbr.vbr_intrcnt
78 #define txbr_data		txbr.vbr_data
79 #define txbr_dsize		txbr.vbr_dsize
80 
81 struct sysctl_ctx_list;
82 struct sysctl_oid;
83 
84 static __inline int
85 vmbus_txbr_maxpktsz(const struct vmbus_txbr *tbr)
86 {
87 
88 	/*
89 	 * - 64 bits for the trailing start index (- sizeof(uint64_t)).
90 	 * - The rindex and windex can't be same (- 1).  See
91 	 *   the comment near vmbus_bufring.br_{r,w}index.
92 	 */
93 	return (tbr->txbr_dsize - sizeof(uint64_t) - 1);
94 }
95 
96 static __inline bool
97 vmbus_txbr_empty(const struct vmbus_txbr *tbr)
98 {
99 
100 	return (atomic_load_acq_32(&tbr->txbr_windex) ==
101 	    atomic_load_acq_32(&tbr->txbr_rindex) ? true : false);
102 }
103 
104 static __inline bool
105 vmbus_rxbr_empty(const struct vmbus_rxbr *rbr)
106 {
107 
108 	return (atomic_load_acq_32(&rbr->rxbr_windex) ==
109 	    atomic_load_acq_32(&rbr->rxbr_rindex) ? true : false);
110 }
111 
112 static __inline int
113 vmbus_br_nelem(int br_size, int elem_size)
114 {
115 
116 	/* Strip bufring header */
117 	br_size -= sizeof(struct vmbus_bufring);
118 	/* Add per-element trailing index */
119 	elem_size += sizeof(uint64_t);
120 	return (br_size / elem_size);
121 }
122 
123 void		vmbus_br_sysctl_create(struct sysctl_ctx_list *ctx,
124 		    struct sysctl_oid *br_tree, struct vmbus_br *br,
125 		    const char *name);
126 
127 void		vmbus_rxbr_init(struct vmbus_rxbr *rbr);
128 void		vmbus_rxbr_deinit(struct vmbus_rxbr *rbr);
129 void		vmbus_rxbr_setup(struct vmbus_rxbr *rbr, void *buf, int blen);
130 int		vmbus_rxbr_peek(struct vmbus_rxbr *rbr, void *data, int dlen);
131 int		vmbus_rxbr_read(struct vmbus_rxbr *rbr, void *data, int dlen,
132 		    uint32_t skip, boolean_t *need_sig);
133 int		vmbus_rxbr_idxadv(struct vmbus_rxbr *rbr, uint32_t idx_adv,
134 		    boolean_t *need_sig);
135 int		vmbus_rxbr_idxadv_peek(struct vmbus_rxbr *rbr, void *data,
136 		    int dlen, uint32_t idx_adv, boolean_t *need_sig);
137 int		vmbus_rxbr_peek_call(struct vmbus_rxbr *rbr, int dlen,
138 		    uint32_t skip, vmbus_br_copy_callback_t cb, void *cbarg);
139 void		vmbus_rxbr_intr_mask(struct vmbus_rxbr *rbr);
140 uint32_t	vmbus_rxbr_intr_unmask(struct vmbus_rxbr *rbr);
141 uint32_t	vmbus_rxbr_available(const struct vmbus_rxbr *rbr);
142 
143 void		vmbus_txbr_init(struct vmbus_txbr *tbr);
144 void		vmbus_txbr_deinit(struct vmbus_txbr *tbr);
145 void		vmbus_txbr_setup(struct vmbus_txbr *tbr, void *buf, int blen);
146 int		vmbus_txbr_write(struct vmbus_txbr *tbr,
147 		    const struct iovec iov[], int iovlen, boolean_t *need_sig);
148 int		vmbus_txbr_write_call(struct vmbus_txbr *tbr,
149 		    const struct iovec iov[], int iovlen,
150 		    vmbus_br_copy_callback_t cb, void *cbarg,
151 		    boolean_t *need_sig);
152 uint32_t	vmbus_txbr_available(const struct vmbus_txbr *tbr);
153 uint32_t	vmbus_txbr_get_imask(const struct vmbus_txbr *tbr);
154 void		vmbus_txbr_set_pending_snd_sz(struct vmbus_txbr *tbr,
155 		    uint32_t size);
156 
157 #endif  /* _VMBUS_BRVAR_H_ */
158