Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2018, Microsoft Corporation.
3 : : * All Rights Reserved.
4 : : */
5 : :
6 : : #include <unistd.h>
7 : : #include <stdint.h>
8 : : #include <string.h>
9 : : #include <sys/uio.h>
10 : :
11 : : #include <eal_export.h>
12 : : #include <rte_eal.h>
13 : : #include <rte_tailq.h>
14 : : #include <rte_log.h>
15 : : #include <rte_malloc.h>
16 : : #include <rte_atomic.h>
17 : : #include <rte_memory.h>
18 : : #include <rte_bus_vmbus.h>
19 : :
20 : : #include "private.h"
21 : :
22 : : static inline void
23 : : vmbus_sync_set_bit(volatile RTE_ATOMIC(uint32_t) *addr, uint32_t mask)
24 : : {
25 : 0 : rte_atomic_fetch_or_explicit(addr, mask, rte_memory_order_seq_cst);
26 : : }
27 : :
28 : : static inline void
29 : 0 : vmbus_send_interrupt(const struct rte_vmbus_device *dev, uint32_t relid)
30 : : {
31 : : RTE_ATOMIC(uint32_t) *int_addr;
32 : : uint32_t int_mask;
33 : :
34 : 0 : int_addr = (RTE_ATOMIC(uint32_t) *) (dev->int_page + relid / 32);
35 : 0 : int_mask = 1u << (relid % 32);
36 : : vmbus_sync_set_bit(int_addr, int_mask);
37 : :
38 : 0 : vmbus_uio_irq_control(dev, 1);
39 : 0 : }
40 : :
41 : : static inline void
42 : : vmbus_set_monitor(const struct vmbus_channel *channel, uint32_t monitor_id)
43 : : {
44 : : RTE_ATOMIC(uint32_t) *monitor_addr;
45 : : uint32_t monitor_mask;
46 : : unsigned int trigger_index;
47 : :
48 : 0 : trigger_index = monitor_id / HV_MON_TRIG_LEN;
49 : 0 : monitor_mask = 1u << (monitor_id % HV_MON_TRIG_LEN);
50 : :
51 : 0 : monitor_addr = &channel->monitor_page->trigs[trigger_index].pending;
52 : : vmbus_sync_set_bit(monitor_addr, monitor_mask);
53 : 0 : }
54 : :
55 : : static void
56 : 0 : vmbus_set_event(struct rte_vmbus_device *dev, const struct vmbus_channel *chan)
57 : : {
58 : : /* Use monitored bit if supported, otherwise use interrupt/Hypercall */
59 [ # # ]: 0 : if (chan->monitor_id != UINT8_MAX)
60 : 0 : vmbus_set_monitor(chan, chan->monitor_id);
61 : : else
62 : 0 : vmbus_send_interrupt(dev, chan->relid);
63 : 0 : }
64 : :
65 : : /*
66 : : * Set the wait between when hypervisor examines the trigger.
67 : : */
68 : : RTE_EXPORT_SYMBOL(rte_vmbus_set_latency)
69 : : void
70 : 0 : rte_vmbus_set_latency(const struct rte_vmbus_device *dev,
71 : : const struct vmbus_channel *chan,
72 : : uint32_t latency)
73 : : {
74 [ # # ]: 0 : if (chan->monitor_id == UINT8_MAX)
75 : : return;
76 : :
77 : 0 : uint32_t trig_idx = chan->monitor_id / VMBUS_MONTRIG_LEN;
78 : 0 : uint32_t trig_offs = chan->monitor_id % VMBUS_MONTRIG_LEN;
79 : :
80 [ # # ]: 0 : if (latency >= UINT16_MAX * 100) {
81 : 0 : VMBUS_LOG(ERR, "invalid latency value %u", latency);
82 : 0 : return;
83 : : }
84 : :
85 [ # # ]: 0 : if (trig_idx >= VMBUS_MONTRIGS_MAX) {
86 : 0 : VMBUS_LOG(ERR, "invalid monitor trigger %u",
87 : : trig_idx);
88 : 0 : return;
89 : : }
90 : :
91 : : /* Host value is expressed in 100 nanosecond units */
92 : 0 : dev->monitor_page->lat[trig_idx][trig_offs] = latency / 100;
93 : : }
94 : :
95 : : /*
96 : : * Notify host that there are data pending on our TX bufring.
97 : : *
98 : : * Since this in userspace, rely on the monitor page.
99 : : * Can't do a hypercall from userspace.
100 : : */
101 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_signal_tx)
102 : : void
103 : 0 : rte_vmbus_chan_signal_tx(struct rte_vmbus_device *dev, const struct vmbus_channel *chan)
104 : : {
105 : : const struct vmbus_br *tbr = &chan->txbr;
106 : :
107 : : /* vmbus_txbr_write() did a full barrier after the write index
108 : : * update, no further ordering needed here.
109 : : */
110 [ # # ]: 0 : if (tbr->vbr->imask)
111 : : return;
112 : :
113 : 0 : vmbus_set_event(dev, chan);
114 : : }
115 : :
116 : :
117 : : /* Do a simple send directly using transmit ring. */
118 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_send)
119 : 0 : int rte_vmbus_chan_send(struct rte_vmbus_device *dev,
120 : : struct vmbus_channel *chan, uint16_t type, void *data,
121 : : uint32_t dlen, uint64_t xactid, uint32_t flags,
122 : : bool *need_sig)
123 : : {
124 : : struct vmbus_chanpkt pkt;
125 : : unsigned int pktlen, pad_pktlen;
126 : : const uint32_t hlen = sizeof(pkt);
127 : 0 : bool send_evt = false;
128 : 0 : uint64_t pad = 0;
129 : : struct iovec iov[3];
130 : : int error;
131 : :
132 : 0 : pktlen = hlen + dlen;
133 : 0 : pad_pktlen = RTE_ALIGN(pktlen, sizeof(uint64_t));
134 : :
135 : 0 : pkt.hdr.type = type;
136 : 0 : pkt.hdr.flags = flags;
137 : 0 : pkt.hdr.hlen = hlen >> VMBUS_CHANPKT_SIZE_SHIFT;
138 : 0 : pkt.hdr.tlen = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT;
139 : 0 : pkt.hdr.xactid = xactid;
140 : :
141 : 0 : iov[0].iov_base = &pkt;
142 : 0 : iov[0].iov_len = hlen;
143 : 0 : iov[1].iov_base = data;
144 : 0 : iov[1].iov_len = dlen;
145 : 0 : iov[2].iov_base = &pad;
146 : 0 : iov[2].iov_len = pad_pktlen - pktlen;
147 : :
148 : 0 : error = vmbus_txbr_write(&chan->txbr, iov, 3, &send_evt);
149 : :
150 : : /*
151 : : * caller sets need_sig to non-NULL if it will handle
152 : : * signaling if required later.
153 : : * if need_sig is NULL, signal now if needed.
154 : : */
155 [ # # ]: 0 : if (need_sig)
156 : 0 : *need_sig |= send_evt;
157 [ # # # # ]: 0 : else if (error == 0 && send_evt)
158 : 0 : rte_vmbus_chan_signal_tx(dev, chan);
159 : 0 : return error;
160 : : }
161 : :
162 : : /* Do a scatter/gather send where the descriptor points to data. */
163 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_send_sglist)
164 : 0 : int rte_vmbus_chan_send_sglist(struct rte_vmbus_device *dev,
165 : : struct vmbus_channel *chan,
166 : : struct vmbus_gpa sg[], uint32_t sglen,
167 : : void *data, uint32_t dlen,
168 : : uint64_t xactid, bool *need_sig)
169 : : {
170 : : struct vmbus_chanpkt_sglist pkt;
171 : : unsigned int pktlen, pad_pktlen, hlen;
172 : 0 : bool send_evt = false;
173 : : struct iovec iov[4];
174 : 0 : uint64_t pad = 0;
175 : : int error;
176 : :
177 : 0 : hlen = offsetof(struct vmbus_chanpkt_sglist, gpa[sglen]);
178 : 0 : pktlen = hlen + dlen;
179 : 0 : pad_pktlen = RTE_ALIGN(pktlen, sizeof(uint64_t));
180 : :
181 : 0 : pkt.hdr.type = VMBUS_CHANPKT_TYPE_GPA;
182 : 0 : pkt.hdr.flags = VMBUS_CHANPKT_FLAG_RC;
183 : 0 : pkt.hdr.hlen = hlen >> VMBUS_CHANPKT_SIZE_SHIFT;
184 : 0 : pkt.hdr.tlen = pad_pktlen >> VMBUS_CHANPKT_SIZE_SHIFT;
185 : 0 : pkt.hdr.xactid = xactid;
186 : 0 : pkt.rsvd = 0;
187 : 0 : pkt.gpa_cnt = sglen;
188 : :
189 : 0 : iov[0].iov_base = &pkt;
190 : 0 : iov[0].iov_len = sizeof(pkt);
191 : 0 : iov[1].iov_base = sg;
192 : 0 : iov[1].iov_len = sizeof(struct vmbus_gpa) * sglen;
193 : 0 : iov[2].iov_base = data;
194 : 0 : iov[2].iov_len = dlen;
195 : 0 : iov[3].iov_base = &pad;
196 : 0 : iov[3].iov_len = pad_pktlen - pktlen;
197 : :
198 : 0 : error = vmbus_txbr_write(&chan->txbr, iov, 4, &send_evt);
199 : :
200 : : /* if caller is batching, just propagate the status */
201 [ # # ]: 0 : if (need_sig)
202 : 0 : *need_sig |= send_evt;
203 [ # # # # ]: 0 : else if (error == 0 && send_evt)
204 : 0 : rte_vmbus_chan_signal_tx(dev, chan);
205 : 0 : return error;
206 : : }
207 : :
208 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_rx_empty)
209 : 0 : bool rte_vmbus_chan_rx_empty(const struct vmbus_channel *channel)
210 : : {
211 : : const struct vmbus_br *br = &channel->rxbr;
212 : :
213 : : rte_atomic_thread_fence(rte_memory_order_acquire);
214 : 0 : return br->vbr->rindex == br->vbr->windex;
215 : : }
216 : :
217 : : /* Signal host after reading N bytes */
218 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_signal_read)
219 : 0 : void rte_vmbus_chan_signal_read(struct rte_vmbus_device *dev,
220 : : struct vmbus_channel *chan,
221 : : uint32_t bytes_read)
222 : : {
223 : : struct vmbus_br *rbr = &chan->rxbr;
224 : : uint32_t write_sz, pending_sz;
225 : :
226 : : /* No need for signaling on older versions */
227 [ # # ]: 0 : if (!rbr->vbr->feature_bits.feat_pending_send_sz)
228 : : return;
229 : :
230 : : /* Make sure reading of pending happens after new read index */
231 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
232 : :
233 : 0 : pending_sz = rbr->vbr->pending_send;
234 [ # # ]: 0 : if (!pending_sz)
235 : : return;
236 : :
237 : : rte_atomic_thread_fence(rte_memory_order_acquire);
238 [ # # ]: 0 : write_sz = vmbus_br_availwrite(rbr, rbr->vbr->windex);
239 : :
240 : : /* If there was space before then host was not blocked */
241 [ # # ]: 0 : if (write_sz - bytes_read > pending_sz)
242 : : return;
243 : :
244 : : /* If pending write will not fit */
245 [ # # ]: 0 : if (write_sz <= pending_sz)
246 : : return;
247 : :
248 : 0 : vmbus_set_event(dev, chan);
249 : : }
250 : :
251 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_recv)
252 : 0 : int rte_vmbus_chan_recv(struct rte_vmbus_device *dev,
253 : : struct vmbus_channel *chan, void *data, uint32_t *len,
254 : : uint64_t *request_id)
255 : : {
256 : : struct vmbus_chanpkt_hdr pkt;
257 : 0 : uint32_t dlen, hlen, bufferlen = *len;
258 : : int error;
259 : :
260 : 0 : *len = 0;
261 : :
262 : 0 : error = vmbus_rxbr_peek(&chan->rxbr, &pkt, sizeof(pkt));
263 [ # # ]: 0 : if (error)
264 : : return error;
265 : :
266 [ # # ]: 0 : if (unlikely(pkt.hlen < VMBUS_CHANPKT_HLEN_MIN)) {
267 : 0 : VMBUS_LOG(ERR, "VMBUS recv, invalid hlen %u", pkt.hlen);
268 : : /* XXX this channel is dead actually. */
269 : 0 : return -EIO;
270 : : }
271 : :
272 [ # # ]: 0 : if (unlikely(pkt.hlen > pkt.tlen)) {
273 : 0 : VMBUS_LOG(ERR, "VMBUS recv,invalid hlen %u and tlen %u",
274 : : pkt.hlen, pkt.tlen);
275 : 0 : return -EIO;
276 : : }
277 : :
278 : : /* Length are in quad words */
279 : 0 : hlen = pkt.hlen << VMBUS_CHANPKT_SIZE_SHIFT;
280 : 0 : dlen = (pkt.tlen << VMBUS_CHANPKT_SIZE_SHIFT) - hlen;
281 : 0 : *len = dlen;
282 : :
283 : : /* If caller buffer is not large enough */
284 [ # # ]: 0 : if (unlikely(dlen > bufferlen))
285 : : return -ENOBUFS;
286 : :
287 [ # # ]: 0 : if (request_id)
288 : 0 : *request_id = pkt.xactid;
289 : :
290 : : /* Read data and skip packet header */
291 : 0 : error = vmbus_rxbr_read(&chan->rxbr, data, dlen, hlen);
292 [ # # ]: 0 : if (error)
293 : : return error;
294 : :
295 : 0 : rte_vmbus_chan_signal_read(dev, chan, dlen + hlen + sizeof(uint64_t));
296 : 0 : return 0;
297 : : }
298 : :
299 : : /* TODO: replace this with inplace ring buffer (no copy) */
300 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_recv_raw)
301 : 0 : int rte_vmbus_chan_recv_raw(struct vmbus_channel *chan,
302 : : void *data, uint32_t *len)
303 : : {
304 : : struct vmbus_chanpkt_hdr pkt;
305 : 0 : uint32_t dlen, bufferlen = *len;
306 : : int error;
307 : :
308 : 0 : error = vmbus_rxbr_peek(&chan->rxbr, &pkt, sizeof(pkt));
309 [ # # ]: 0 : if (error)
310 : : return error;
311 : :
312 [ # # ]: 0 : if (unlikely(pkt.hlen < VMBUS_CHANPKT_HLEN_MIN)) {
313 : 0 : VMBUS_LOG(ERR, "VMBUS recv, invalid hlen %u", pkt.hlen);
314 : : /* XXX this channel is dead actually. */
315 : 0 : return -EIO;
316 : : }
317 : :
318 [ # # ]: 0 : if (unlikely(pkt.hlen > pkt.tlen)) {
319 : 0 : VMBUS_LOG(ERR, "VMBUS recv,invalid hlen %u and tlen %u",
320 : : pkt.hlen, pkt.tlen);
321 : 0 : return -EIO;
322 : : }
323 : :
324 : : /* Length are in quad words */
325 : 0 : dlen = pkt.tlen << VMBUS_CHANPKT_SIZE_SHIFT;
326 : 0 : *len = dlen;
327 : :
328 : : /* If caller buffer is not large enough */
329 [ # # ]: 0 : if (unlikely(dlen > bufferlen))
330 : : return -ENOBUFS;
331 : :
332 : : /* Read data and skip packet header */
333 : 0 : error = vmbus_rxbr_read(&chan->rxbr, data, dlen, 0);
334 [ # # ]: 0 : if (error)
335 : : return error;
336 : :
337 : : /* Return the number of bytes read */
338 : 0 : return dlen + sizeof(uint64_t);
339 : : }
340 : :
341 : 0 : int vmbus_chan_create(const struct rte_vmbus_device *device,
342 : : uint16_t relid, uint16_t subid, uint8_t monitor_id,
343 : : struct vmbus_channel **new_chan)
344 : : {
345 : : struct vmbus_channel *chan;
346 : : int err;
347 : :
348 : 0 : chan = rte_zmalloc_socket("VMBUS", sizeof(*chan), RTE_CACHE_LINE_SIZE,
349 : 0 : device->device.numa_node);
350 [ # # ]: 0 : if (!chan)
351 : : return -ENOMEM;
352 : :
353 : 0 : STAILQ_INIT(&chan->subchannel_list);
354 : 0 : chan->device = device;
355 : 0 : chan->subchannel_id = subid;
356 : 0 : chan->relid = relid;
357 : 0 : chan->monitor_id = monitor_id;
358 : 0 : chan->monitor_page = device->monitor_page;
359 : 0 : *new_chan = chan;
360 : :
361 : 0 : err = vmbus_uio_map_rings(chan);
362 [ # # ]: 0 : if (err) {
363 : 0 : rte_free(chan);
364 : 0 : return err;
365 : : }
366 : :
367 : : return 0;
368 : : }
369 : :
370 : : /* Setup the primary channel */
371 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_open)
372 : 0 : int rte_vmbus_chan_open(struct rte_vmbus_device *device,
373 : : struct vmbus_channel **new_chan)
374 : : {
375 : : struct mapped_vmbus_resource *uio_res;
376 : : int err;
377 : :
378 : 0 : uio_res = vmbus_uio_find_resource(device);
379 [ # # ]: 0 : if (!uio_res) {
380 : 0 : VMBUS_LOG(ERR, "can't find uio resource");
381 : 0 : return -EINVAL;
382 : : }
383 : :
384 : 0 : err = vmbus_chan_create(device, device->relid, 0,
385 : 0 : device->monitor_id, new_chan);
386 [ # # ]: 0 : if (!err)
387 : 0 : device->primary = *new_chan;
388 : :
389 : : return err;
390 : : }
391 : :
392 : : RTE_EXPORT_SYMBOL(rte_vmbus_max_channels)
393 : 0 : int rte_vmbus_max_channels(const struct rte_vmbus_device *device)
394 : : {
395 [ # # ]: 0 : if (vmbus_uio_subchannels_supported(device, device->primary))
396 : : return VMBUS_MAX_CHANNELS;
397 : : else
398 : 0 : return 1;
399 : : }
400 : :
401 : : /* Setup secondary channel */
402 : : RTE_EXPORT_SYMBOL(rte_vmbus_subchan_open)
403 : 0 : int rte_vmbus_subchan_open(struct vmbus_channel *primary,
404 : : struct vmbus_channel **new_chan)
405 : : {
406 : : struct vmbus_channel *chan;
407 : : int err;
408 : :
409 : 0 : err = vmbus_uio_get_subchan(primary, &chan);
410 [ # # ]: 0 : if (err)
411 : : return err;
412 : :
413 : 0 : STAILQ_INSERT_TAIL(&primary->subchannel_list, chan, next);
414 : 0 : *new_chan = chan;
415 : 0 : return 0;
416 : : }
417 : :
418 : : RTE_EXPORT_SYMBOL(rte_vmbus_sub_channel_index)
419 : 0 : uint16_t rte_vmbus_sub_channel_index(const struct vmbus_channel *chan)
420 : : {
421 : 0 : return chan->subchannel_id;
422 : : }
423 : :
424 : : RTE_EXPORT_SYMBOL(rte_vmbus_chan_close)
425 : 0 : void rte_vmbus_chan_close(struct vmbus_channel *chan)
426 : : {
427 : 0 : const struct rte_vmbus_device *device = chan->device;
428 : 0 : struct vmbus_channel *primary = device->primary;
429 : :
430 : : /*
431 : : * intentionally leak primary channel because
432 : : * secondary may still reference it
433 : : */
434 [ # # ]: 0 : if (chan != primary) {
435 [ # # # # : 0 : STAILQ_REMOVE(&primary->subchannel_list, chan,
# # # # ]
436 : : vmbus_channel, next);
437 : 0 : rte_free(chan);
438 : : }
439 : :
440 : 0 : }
441 : :
442 : 0 : static void vmbus_dump_ring(FILE *f, const char *id, const struct vmbus_br *br)
443 : : {
444 : 0 : const struct vmbus_bufring *vbr = br->vbr;
445 : : struct vmbus_chanpkt_hdr pkt;
446 : :
447 : : fprintf(f, "%s windex=%u rindex=%u mask=%u pending=%u feature=%#x\n",
448 : 0 : id, vbr->windex, vbr->rindex, vbr->imask,
449 : 0 : vbr->pending_send, vbr->feature_bits.value);
450 : 0 : fprintf(f, " size=%u avail write=%u read=%u\n",
451 [ # # ]: 0 : br->dsize, vmbus_br_availwrite(br, vbr->windex),
452 : : vmbus_br_availread(br));
453 : :
454 [ # # ]: 0 : if (vmbus_rxbr_peek(br, &pkt, sizeof(pkt)) == 0)
455 : 0 : fprintf(f, " pkt type %#x len %u flags %#x xactid %#"PRIx64"\n",
456 : 0 : pkt.type,
457 : 0 : pkt.tlen << VMBUS_CHANPKT_SIZE_SHIFT,
458 : 0 : pkt.flags, pkt.xactid);
459 : 0 : }
460 : :
461 : 0 : void rte_vmbus_chan_dump(FILE *f, const struct vmbus_channel *chan)
462 : : {
463 : 0 : fprintf(f, "channel[%u] relid=%u monitor=%u\n",
464 : 0 : chan->subchannel_id, chan->relid, chan->monitor_id);
465 : 0 : vmbus_dump_ring(f, "rxbr", &chan->rxbr);
466 : 0 : vmbus_dump_ring(f, "txbr", &chan->txbr);
467 : 0 : }
|