17dd8881cSSepherosa Ziehau /*-
27dd8881cSSepherosa Ziehau * Copyright (c) 2009-2012,2016 Microsoft Corp.
37dd8881cSSepherosa Ziehau * Copyright (c) 2012 NetApp Inc.
47dd8881cSSepherosa Ziehau * Copyright (c) 2012 Citrix Inc.
57dd8881cSSepherosa Ziehau * All rights reserved.
67dd8881cSSepherosa Ziehau *
77dd8881cSSepherosa Ziehau * Redistribution and use in source and binary forms, with or without
87dd8881cSSepherosa Ziehau * modification, are permitted provided that the following conditions
97dd8881cSSepherosa Ziehau * are met:
107dd8881cSSepherosa Ziehau * 1. Redistributions of source code must retain the above copyright
117dd8881cSSepherosa Ziehau * notice unmodified, this list of conditions, and the following
127dd8881cSSepherosa Ziehau * disclaimer.
137dd8881cSSepherosa Ziehau * 2. Redistributions in binary form must reproduce the above copyright
147dd8881cSSepherosa Ziehau * notice, this list of conditions and the following disclaimer in the
157dd8881cSSepherosa Ziehau * documentation and/or other materials provided with the distribution.
167dd8881cSSepherosa Ziehau *
177dd8881cSSepherosa Ziehau * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187dd8881cSSepherosa Ziehau * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197dd8881cSSepherosa Ziehau * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207dd8881cSSepherosa Ziehau * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217dd8881cSSepherosa Ziehau * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227dd8881cSSepherosa Ziehau * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237dd8881cSSepherosa Ziehau * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247dd8881cSSepherosa Ziehau * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257dd8881cSSepherosa Ziehau * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267dd8881cSSepherosa Ziehau * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277dd8881cSSepherosa Ziehau */
287dd8881cSSepherosa Ziehau
29afffab7eSSepherosa Ziehau #ifndef _VMBUS_BRVAR_H_
30afffab7eSSepherosa Ziehau #define _VMBUS_BRVAR_H_
317dd8881cSSepherosa Ziehau
327dd8881cSSepherosa Ziehau #include <sys/param.h>
337dd8881cSSepherosa Ziehau #include <sys/lock.h>
347dd8881cSSepherosa Ziehau #include <sys/mutex.h>
357dd8881cSSepherosa Ziehau #include <sys/_iovec.h>
367dd8881cSSepherosa Ziehau
37ea06b5d3SSepherosa Ziehau struct vmbus_br {
38ea06b5d3SSepherosa Ziehau struct vmbus_bufring *vbr;
39ea06b5d3SSepherosa Ziehau uint32_t vbr_dsize; /* total data size */
40ea06b5d3SSepherosa Ziehau };
41ea06b5d3SSepherosa Ziehau
42ea06b5d3SSepherosa Ziehau #define vbr_windex vbr->br_windex
43ea06b5d3SSepherosa Ziehau #define vbr_rindex vbr->br_rindex
44ea06b5d3SSepherosa Ziehau #define vbr_imask vbr->br_imask
45a560f3ebSWei Hu #define vbr_psndsz vbr->br_pending_snd_sz
46a560f3ebSWei Hu #define vbr_fpsndsz vbr->br_feature_bits.feat_pending_snd_sz
47a560f3ebSWei Hu #define vbr_fvalue vbr->br_feature_bits.value
48a560f3ebSWei Hu #define vbr_intrcnt vbr->br_g2h_intr_cnt
49ea06b5d3SSepherosa Ziehau #define vbr_data vbr->br_data
50ea06b5d3SSepherosa Ziehau
51ea06b5d3SSepherosa Ziehau struct vmbus_rxbr {
52ea06b5d3SSepherosa Ziehau struct mtx rxbr_lock;
53ea06b5d3SSepherosa Ziehau struct vmbus_br rxbr;
54ea06b5d3SSepherosa Ziehau };
55ea06b5d3SSepherosa Ziehau
56ea06b5d3SSepherosa Ziehau #define rxbr_windex rxbr.vbr_windex
57ea06b5d3SSepherosa Ziehau #define rxbr_rindex rxbr.vbr_rindex
58ea06b5d3SSepherosa Ziehau #define rxbr_imask rxbr.vbr_imask
59a560f3ebSWei Hu #define rxbr_psndsz rxbr.vbr_psndsz
60a560f3ebSWei Hu #define rxbr_fpsndsz rxbr.vbr_fpsndsz
61a560f3ebSWei Hu #define rxbr_fvalue rxbr.vbr_fvalue
62a560f3ebSWei Hu #define rxbr_intrcnt rxbr.vbr_intrcnt
63ea06b5d3SSepherosa Ziehau #define rxbr_data rxbr.vbr_data
64ea06b5d3SSepherosa Ziehau #define rxbr_dsize rxbr.vbr_dsize
65ea06b5d3SSepherosa Ziehau
66ea06b5d3SSepherosa Ziehau struct vmbus_txbr {
67ea06b5d3SSepherosa Ziehau struct mtx txbr_lock;
68ea06b5d3SSepherosa Ziehau struct vmbus_br txbr;
69ea06b5d3SSepherosa Ziehau };
70ea06b5d3SSepherosa Ziehau
71ea06b5d3SSepherosa Ziehau #define txbr_windex txbr.vbr_windex
72ea06b5d3SSepherosa Ziehau #define txbr_rindex txbr.vbr_rindex
73ea06b5d3SSepherosa Ziehau #define txbr_imask txbr.vbr_imask
74a560f3ebSWei Hu #define txbr_psndsz txbr.vbr_psndsz
75a560f3ebSWei Hu #define txbr_fpsndsz txbr.vbr_fpsndsz
76a560f3ebSWei Hu #define txbr_fvalue txbr.vbr_fvalue
77a560f3ebSWei Hu #define txbr_intrcnt txbr.vbr_intrcnt
78ea06b5d3SSepherosa Ziehau #define txbr_data txbr.vbr_data
79ea06b5d3SSepherosa Ziehau #define txbr_dsize txbr.vbr_dsize
807dd8881cSSepherosa Ziehau
817dd8881cSSepherosa Ziehau struct sysctl_ctx_list;
827dd8881cSSepherosa Ziehau struct sysctl_oid;
837dd8881cSSepherosa Ziehau
849d6016a7SSepherosa Ziehau static __inline int
vmbus_txbr_maxpktsz(const struct vmbus_txbr * tbr)859d6016a7SSepherosa Ziehau vmbus_txbr_maxpktsz(const struct vmbus_txbr *tbr)
869d6016a7SSepherosa Ziehau {
87dd9af410SSepherosa Ziehau
886fd14eb9SSepherosa Ziehau /*
896fd14eb9SSepherosa Ziehau * - 64 bits for the trailing start index (- sizeof(uint64_t)).
906fd14eb9SSepherosa Ziehau * - The rindex and windex can't be same (- 1). See
916fd14eb9SSepherosa Ziehau * the comment near vmbus_bufring.br_{r,w}index.
926fd14eb9SSepherosa Ziehau */
936fd14eb9SSepherosa Ziehau return (tbr->txbr_dsize - sizeof(uint64_t) - 1);
949d6016a7SSepherosa Ziehau }
959d6016a7SSepherosa Ziehau
96e8223cc6SSepherosa Ziehau static __inline bool
vmbus_txbr_empty(const struct vmbus_txbr * tbr)97e8223cc6SSepherosa Ziehau vmbus_txbr_empty(const struct vmbus_txbr *tbr)
98e8223cc6SSepherosa Ziehau {
99e8223cc6SSepherosa Ziehau
10017050a2bSWei Hu return (atomic_load_acq_32(&tbr->txbr_windex) ==
10117050a2bSWei Hu atomic_load_acq_32(&tbr->txbr_rindex) ? true : false);
102e8223cc6SSepherosa Ziehau }
103e8223cc6SSepherosa Ziehau
104e8223cc6SSepherosa Ziehau static __inline bool
vmbus_rxbr_empty(const struct vmbus_rxbr * rbr)105e8223cc6SSepherosa Ziehau vmbus_rxbr_empty(const struct vmbus_rxbr *rbr)
106e8223cc6SSepherosa Ziehau {
107e8223cc6SSepherosa Ziehau
10817050a2bSWei Hu return (atomic_load_acq_32(&rbr->rxbr_windex) ==
10917050a2bSWei Hu atomic_load_acq_32(&rbr->rxbr_rindex) ? true : false);
110e8223cc6SSepherosa Ziehau }
111e8223cc6SSepherosa Ziehau
112dd9af410SSepherosa Ziehau static __inline int
vmbus_br_nelem(int br_size,int elem_size)113dd9af410SSepherosa Ziehau vmbus_br_nelem(int br_size, int elem_size)
114dd9af410SSepherosa Ziehau {
115dd9af410SSepherosa Ziehau
116dd9af410SSepherosa Ziehau /* Strip bufring header */
117dd9af410SSepherosa Ziehau br_size -= sizeof(struct vmbus_bufring);
118dd9af410SSepherosa Ziehau /* Add per-element trailing index */
119dd9af410SSepherosa Ziehau elem_size += sizeof(uint64_t);
120dd9af410SSepherosa Ziehau return (br_size / elem_size);
121dd9af410SSepherosa Ziehau }
122dd9af410SSepherosa Ziehau
1237dd8881cSSepherosa Ziehau void vmbus_br_sysctl_create(struct sysctl_ctx_list *ctx,
124ea06b5d3SSepherosa Ziehau struct sysctl_oid *br_tree, struct vmbus_br *br,
1257dd8881cSSepherosa Ziehau const char *name);
1267dd8881cSSepherosa Ziehau
127ea06b5d3SSepherosa Ziehau void vmbus_rxbr_init(struct vmbus_rxbr *rbr);
128ea06b5d3SSepherosa Ziehau void vmbus_rxbr_deinit(struct vmbus_rxbr *rbr);
129ea06b5d3SSepherosa Ziehau void vmbus_rxbr_setup(struct vmbus_rxbr *rbr, void *buf, int blen);
130ea06b5d3SSepherosa Ziehau int vmbus_rxbr_peek(struct vmbus_rxbr *rbr, void *data, int dlen);
131ea06b5d3SSepherosa Ziehau int vmbus_rxbr_read(struct vmbus_rxbr *rbr, void *data, int dlen,
132*49fa9a64SWei Hu uint32_t skip, boolean_t *need_sig);
133a560f3ebSWei Hu int vmbus_rxbr_idxadv(struct vmbus_rxbr *rbr, uint32_t idx_adv,
134a560f3ebSWei Hu boolean_t *need_sig);
135a560f3ebSWei Hu int vmbus_rxbr_idxadv_peek(struct vmbus_rxbr *rbr, void *data,
136a560f3ebSWei Hu int dlen, uint32_t idx_adv, boolean_t *need_sig);
137a560f3ebSWei Hu int vmbus_rxbr_peek_call(struct vmbus_rxbr *rbr, int dlen,
138a560f3ebSWei Hu uint32_t skip, vmbus_br_copy_callback_t cb, void *cbarg);
139ea06b5d3SSepherosa Ziehau void vmbus_rxbr_intr_mask(struct vmbus_rxbr *rbr);
140ea06b5d3SSepherosa Ziehau uint32_t vmbus_rxbr_intr_unmask(struct vmbus_rxbr *rbr);
141a560f3ebSWei Hu uint32_t vmbus_rxbr_available(const struct vmbus_rxbr *rbr);
1427dd8881cSSepherosa Ziehau
143ea06b5d3SSepherosa Ziehau void vmbus_txbr_init(struct vmbus_txbr *tbr);
144ea06b5d3SSepherosa Ziehau void vmbus_txbr_deinit(struct vmbus_txbr *tbr);
145ea06b5d3SSepherosa Ziehau void vmbus_txbr_setup(struct vmbus_txbr *tbr, void *buf, int blen);
146ea06b5d3SSepherosa Ziehau int vmbus_txbr_write(struct vmbus_txbr *tbr,
147ea06b5d3SSepherosa Ziehau const struct iovec iov[], int iovlen, boolean_t *need_sig);
148a560f3ebSWei Hu int vmbus_txbr_write_call(struct vmbus_txbr *tbr,
149a560f3ebSWei Hu const struct iovec iov[], int iovlen,
150a560f3ebSWei Hu vmbus_br_copy_callback_t cb, void *cbarg,
151a560f3ebSWei Hu boolean_t *need_sig);
152a560f3ebSWei Hu uint32_t vmbus_txbr_available(const struct vmbus_txbr *tbr);
153a560f3ebSWei Hu uint32_t vmbus_txbr_get_imask(const struct vmbus_txbr *tbr);
154a560f3ebSWei Hu void vmbus_txbr_set_pending_snd_sz(struct vmbus_txbr *tbr,
155a560f3ebSWei Hu uint32_t size);
1567dd8881cSSepherosa Ziehau
157afffab7eSSepherosa Ziehau #endif /* _VMBUS_BRVAR_H_ */
158