xref: /spdk/lib/virtio/virtio_pci.c (revision 22898a91b9b6f289933db19b0175821cfb7e7820)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
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  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/mmio.h"
37 #include "spdk/string.h"
38 #include "spdk/env.h"
39 
40 #include "spdk_internal/virtio.h"
41 
42 struct virtio_hw {
43 	uint8_t	    use_msix;
44 	uint32_t    notify_off_multiplier;
45 	uint8_t     *isr;
46 	uint16_t    *notify_base;
47 
48 	struct {
49 		/** Mem-mapped resources from given PCI BAR */
50 		void        *vaddr;
51 
52 		/** Length of the address space */
53 		uint32_t    len;
54 	} pci_bar[6];
55 
56 	struct virtio_pci_common_cfg *common_cfg;
57 	struct spdk_pci_device *pci_dev;
58 
59 	/** Device-specific PCI config space */
60 	void *dev_cfg;
61 };
62 
63 struct virtio_pci_probe_ctx {
64 	virtio_pci_create_cb enum_cb;
65 	void *enum_ctx;
66 	uint16_t device_id;
67 };
68 
69 /*
70  * Following macros are derived from linux/pci_regs.h, however,
71  * we can't simply include that header here, as there is no such
72  * file for non-Linux platform.
73  */
74 #define PCI_CAPABILITY_LIST	0x34
75 #define PCI_CAP_ID_VNDR		0x09
76 #define PCI_CAP_ID_MSIX		0x11
77 
78 static inline int
79 check_vq_phys_addr_ok(struct virtqueue *vq)
80 {
81 	/* Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
82 	 * and only accepts 32 bit page frame number.
83 	 * Check if the allocated physical memory exceeds 16TB.
84 	 */
85 	if ((vq->vq_ring_mem + vq->vq_ring_size - 1) >>
86 	    (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
87 		SPDK_ERRLOG("vring address shouldn't be above 16TB!\n");
88 		return 0;
89 	}
90 
91 	return 1;
92 }
93 
94 static void
95 free_virtio_hw(struct virtio_hw *hw)
96 {
97 	unsigned i;
98 
99 	for (i = 0; i < 6; ++i) {
100 		if (hw->pci_bar[i].vaddr == NULL) {
101 			continue;
102 		}
103 
104 		spdk_pci_device_unmap_bar(hw->pci_dev, i, hw->pci_bar[i].vaddr);
105 	}
106 
107 	free(hw);
108 }
109 
110 static void
111 pci_dump_json_info(struct virtio_dev *dev, struct spdk_json_write_ctx *w)
112 {
113 	struct virtio_hw *hw = dev->ctx;
114 	struct spdk_pci_addr pci_addr = spdk_pci_device_get_addr((struct spdk_pci_device *)hw->pci_dev);
115 	char addr[32];
116 
117 	spdk_json_write_name(w, "type");
118 	if (dev->modern) {
119 		spdk_json_write_string(w, "pci-modern");
120 	} else {
121 		spdk_json_write_string(w, "pci-legacy");
122 	}
123 
124 	spdk_json_write_name(w, "pci_address");
125 	spdk_pci_addr_fmt(addr, sizeof(addr), &pci_addr);
126 	spdk_json_write_string(w, addr);
127 }
128 
129 static void
130 pci_write_json_config(struct virtio_dev *dev, struct spdk_json_write_ctx *w)
131 {
132 	struct virtio_hw *hw = dev->ctx;
133 	struct spdk_pci_addr pci_addr = spdk_pci_device_get_addr(hw->pci_dev);
134 	char addr[32];
135 
136 	spdk_pci_addr_fmt(addr, sizeof(addr), &pci_addr);
137 
138 	spdk_json_write_named_string(w, "trtype", "pci");
139 	spdk_json_write_named_string(w, "traddr", addr);
140 }
141 
142 static inline void
143 io_write64_twopart(uint64_t val, uint32_t *lo, uint32_t *hi)
144 {
145 	spdk_mmio_write_4(lo, val & ((1ULL << 32) - 1));
146 	spdk_mmio_write_4(hi, val >> 32);
147 }
148 
149 static void
150 modern_read_dev_config(struct virtio_dev *dev, size_t offset,
151 		       void *dst, int length)
152 {
153 	struct virtio_hw *hw = dev->ctx;
154 	int i;
155 	uint8_t *p;
156 	uint8_t old_gen, new_gen;
157 
158 	do {
159 		old_gen = spdk_mmio_read_1(&hw->common_cfg->config_generation);
160 
161 		p = dst;
162 		for (i = 0;  i < length; i++) {
163 			*p++ = spdk_mmio_read_1((uint8_t *)hw->dev_cfg + offset + i);
164 		}
165 
166 		new_gen = spdk_mmio_read_1(&hw->common_cfg->config_generation);
167 	} while (old_gen != new_gen);
168 }
169 
170 static void
171 modern_write_dev_config(struct virtio_dev *dev, size_t offset,
172 			const void *src, int length)
173 {
174 	struct virtio_hw *hw = dev->ctx;
175 	int i;
176 	const uint8_t *p = src;
177 
178 	for (i = 0;  i < length; i++) {
179 		spdk_mmio_write_1(((uint8_t *)hw->dev_cfg) + offset + i, *p++);
180 	}
181 }
182 
183 static uint64_t
184 modern_get_features(struct virtio_dev *dev)
185 {
186 	struct virtio_hw *hw = dev->ctx;
187 	uint32_t features_lo, features_hi;
188 
189 	spdk_mmio_write_4(&hw->common_cfg->device_feature_select, 0);
190 	features_lo = spdk_mmio_read_4(&hw->common_cfg->device_feature);
191 
192 	spdk_mmio_write_4(&hw->common_cfg->device_feature_select, 1);
193 	features_hi = spdk_mmio_read_4(&hw->common_cfg->device_feature);
194 
195 	return ((uint64_t)features_hi << 32) | features_lo;
196 }
197 
198 static int
199 modern_set_features(struct virtio_dev *dev, uint64_t features)
200 {
201 	struct virtio_hw *hw = dev->ctx;
202 
203 	if ((features & (1ULL << VIRTIO_F_VERSION_1)) == 0) {
204 		SPDK_ERRLOG("VIRTIO_F_VERSION_1 feature is not enabled.\n");
205 		return -1;
206 	}
207 
208 	spdk_mmio_write_4(&hw->common_cfg->guest_feature_select, 0);
209 	spdk_mmio_write_4(&hw->common_cfg->guest_feature, features & ((1ULL << 32) - 1));
210 
211 	spdk_mmio_write_4(&hw->common_cfg->guest_feature_select, 1);
212 	spdk_mmio_write_4(&hw->common_cfg->guest_feature, features >> 32);
213 
214 	dev->negotiated_features = features;
215 
216 	return 0;
217 }
218 
219 static void
220 modern_destruct_dev(struct virtio_dev *vdev)
221 {
222 	struct virtio_hw *hw = vdev->ctx;
223 	struct spdk_pci_device *pci_dev = hw->pci_dev;
224 
225 	free_virtio_hw(hw);
226 	spdk_pci_device_detach(pci_dev);
227 }
228 
229 static uint8_t
230 modern_get_status(struct virtio_dev *dev)
231 {
232 	struct virtio_hw *hw = dev->ctx;
233 
234 	return spdk_mmio_read_1(&hw->common_cfg->device_status);
235 }
236 
237 static void
238 modern_set_status(struct virtio_dev *dev, uint8_t status)
239 {
240 	struct virtio_hw *hw = dev->ctx;
241 
242 	spdk_mmio_write_1(&hw->common_cfg->device_status, status);
243 }
244 
245 static uint16_t
246 modern_get_queue_size(struct virtio_dev *dev, uint16_t queue_id)
247 {
248 	struct virtio_hw *hw = dev->ctx;
249 
250 	spdk_mmio_write_2(&hw->common_cfg->queue_select, queue_id);
251 	return spdk_mmio_read_2(&hw->common_cfg->queue_size);
252 }
253 
254 static int
255 modern_setup_queue(struct virtio_dev *dev, struct virtqueue *vq)
256 {
257 	struct virtio_hw *hw = dev->ctx;
258 	uint64_t desc_addr, avail_addr, used_addr;
259 	uint16_t notify_off;
260 
261 	if (!check_vq_phys_addr_ok(vq)) {
262 		return -1;
263 	}
264 
265 	desc_addr = vq->vq_ring_mem;
266 	avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
267 	used_addr = (avail_addr + offsetof(struct vring_avail, ring[vq->vq_nentries])
268 		     + VIRTIO_PCI_VRING_ALIGN - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1);
269 
270 	spdk_mmio_write_2(&hw->common_cfg->queue_select, vq->vq_queue_index);
271 
272 	io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
273 			   &hw->common_cfg->queue_desc_hi);
274 	io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
275 			   &hw->common_cfg->queue_avail_hi);
276 	io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
277 			   &hw->common_cfg->queue_used_hi);
278 
279 	notify_off = spdk_mmio_read_2(&hw->common_cfg->queue_notify_off);
280 	vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
281 				   notify_off * hw->notify_off_multiplier);
282 
283 	spdk_mmio_write_2(&hw->common_cfg->queue_enable, 1);
284 
285 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "queue %"PRIu16" addresses:\n", vq->vq_queue_index);
286 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t desc_addr: %" PRIx64 "\n", desc_addr);
287 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t aval_addr: %" PRIx64 "\n", avail_addr);
288 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t used_addr: %" PRIx64 "\n", used_addr);
289 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t notify addr: %p (notify offset: %"PRIu16")\n",
290 		      vq->notify_addr, notify_off);
291 
292 	return 0;
293 }
294 
295 static void
296 modern_del_queue(struct virtio_dev *dev, struct virtqueue *vq)
297 {
298 	struct virtio_hw *hw = dev->ctx;
299 
300 	spdk_mmio_write_2(&hw->common_cfg->queue_select, vq->vq_queue_index);
301 
302 	io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
303 			   &hw->common_cfg->queue_desc_hi);
304 	io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
305 			   &hw->common_cfg->queue_avail_hi);
306 	io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
307 			   &hw->common_cfg->queue_used_hi);
308 
309 	spdk_mmio_write_2(&hw->common_cfg->queue_enable, 0);
310 }
311 
312 static void
313 modern_notify_queue(struct virtio_dev *dev, struct virtqueue *vq)
314 {
315 	spdk_mmio_write_2(vq->notify_addr, vq->vq_queue_index);
316 }
317 
318 static const struct virtio_dev_ops modern_ops = {
319 	.read_dev_cfg	= modern_read_dev_config,
320 	.write_dev_cfg	= modern_write_dev_config,
321 	.get_status	= modern_get_status,
322 	.set_status	= modern_set_status,
323 	.get_features	= modern_get_features,
324 	.set_features	= modern_set_features,
325 	.destruct_dev	= modern_destruct_dev,
326 	.get_queue_size	= modern_get_queue_size,
327 	.setup_queue	= modern_setup_queue,
328 	.del_queue	= modern_del_queue,
329 	.notify_queue	= modern_notify_queue,
330 	.dump_json_info = pci_dump_json_info,
331 	.write_json_config = pci_write_json_config,
332 };
333 
334 static void *
335 get_cfg_addr(struct virtio_hw *hw, struct virtio_pci_cap *cap)
336 {
337 	uint8_t  bar    = cap->bar;
338 	uint32_t length = cap->length;
339 	uint32_t offset = cap->offset;
340 
341 	if (bar > 5) {
342 		SPDK_ERRLOG("invalid bar: %"PRIu8"\n", bar);
343 		return NULL;
344 	}
345 
346 	if (offset + length < offset) {
347 		SPDK_ERRLOG("offset(%"PRIu32") + length(%"PRIu32") overflows\n",
348 			    offset, length);
349 		return NULL;
350 	}
351 
352 	if (offset + length > hw->pci_bar[bar].len) {
353 		SPDK_ERRLOG("invalid cap: overflows bar space: %"PRIu32" > %"PRIu32"\n",
354 			    offset + length, hw->pci_bar[bar].len);
355 		return NULL;
356 	}
357 
358 	if (hw->pci_bar[bar].vaddr == NULL) {
359 		SPDK_ERRLOG("bar %"PRIu8" base addr is NULL\n", bar);
360 		return NULL;
361 	}
362 
363 	return hw->pci_bar[bar].vaddr + offset;
364 }
365 
366 static int
367 virtio_read_caps(struct virtio_hw *hw)
368 {
369 	uint8_t pos;
370 	struct virtio_pci_cap cap;
371 	int ret;
372 
373 	ret = spdk_pci_device_cfg_read(hw->pci_dev, &pos, 1, PCI_CAPABILITY_LIST);
374 	if (ret < 0) {
375 		SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "failed to read pci capability list\n");
376 		return -1;
377 	}
378 
379 	while (pos) {
380 		ret = spdk_pci_device_cfg_read(hw->pci_dev, &cap, sizeof(cap), pos);
381 		if (ret < 0) {
382 			SPDK_ERRLOG("failed to read pci cap at pos: %"PRIx8"\n", pos);
383 			break;
384 		}
385 
386 		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
387 			hw->use_msix = 1;
388 		}
389 
390 		if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
391 			SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI,
392 				      "[%2"PRIx8"] skipping non VNDR cap id: %02"PRIx8"\n",
393 				      pos, cap.cap_vndr);
394 			goto next;
395 		}
396 
397 		SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI,
398 			      "[%2"PRIx8"] cfg type: %"PRIu8", bar: %"PRIu8", offset: %04"PRIx32", len: %"PRIu32"\n",
399 			      pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
400 
401 		switch (cap.cfg_type) {
402 		case VIRTIO_PCI_CAP_COMMON_CFG:
403 			hw->common_cfg = get_cfg_addr(hw, &cap);
404 			break;
405 		case VIRTIO_PCI_CAP_NOTIFY_CFG:
406 			spdk_pci_device_cfg_read(hw->pci_dev, &hw->notify_off_multiplier,
407 						 4, pos + sizeof(cap));
408 			hw->notify_base = get_cfg_addr(hw, &cap);
409 			break;
410 		case VIRTIO_PCI_CAP_DEVICE_CFG:
411 			hw->dev_cfg = get_cfg_addr(hw, &cap);
412 			break;
413 		case VIRTIO_PCI_CAP_ISR_CFG:
414 			hw->isr = get_cfg_addr(hw, &cap);
415 			break;
416 		}
417 
418 next:
419 		pos = cap.cap_next;
420 	}
421 
422 	if (hw->common_cfg == NULL || hw->notify_base == NULL ||
423 	    hw->dev_cfg == NULL    || hw->isr == NULL) {
424 		SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "no modern virtio pci device found.\n");
425 		return -1;
426 	}
427 
428 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "found modern virtio pci device.\n");
429 
430 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "common cfg mapped at: %p\n", hw->common_cfg);
431 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "device cfg mapped at: %p\n", hw->dev_cfg);
432 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "isr cfg mapped at: %p\n", hw->isr);
433 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "notify base: %p, notify off multiplier: %u\n",
434 		      hw->notify_base, hw->notify_off_multiplier);
435 
436 	return 0;
437 }
438 
439 static int
440 virtio_pci_dev_probe(struct spdk_pci_device *pci_dev, struct virtio_pci_probe_ctx *ctx)
441 {
442 	struct virtio_hw *hw;
443 	uint8_t *bar_vaddr;
444 	uint64_t bar_paddr, bar_len;
445 	int rc;
446 	unsigned i;
447 	char bdf[32];
448 	struct spdk_pci_addr addr;
449 
450 	addr = spdk_pci_device_get_addr(pci_dev);
451 	rc = spdk_pci_addr_fmt(bdf, sizeof(bdf), &addr);
452 	if (rc != 0) {
453 		SPDK_ERRLOG("Ignoring a device with non-parseable PCI address\n");
454 		return -1;
455 	}
456 
457 	hw = calloc(1, sizeof(*hw));
458 	if (hw == NULL) {
459 		SPDK_ERRLOG("%s: calloc failed\n", bdf);
460 		return -1;
461 	}
462 
463 	hw->pci_dev = pci_dev;
464 
465 	for (i = 0; i < 6; ++i) {
466 		rc = spdk_pci_device_map_bar(pci_dev, i, (void *) &bar_vaddr, &bar_paddr,
467 					     &bar_len);
468 		if (rc != 0) {
469 			SPDK_ERRLOG("%s: failed to memmap PCI BAR %u\n", bdf, i);
470 			free_virtio_hw(hw);
471 			return -1;
472 		}
473 
474 		hw->pci_bar[i].vaddr = bar_vaddr;
475 		hw->pci_bar[i].len = bar_len;
476 	}
477 
478 	/* Virtio PCI caps exist only on modern PCI devices.
479 	 * Legacy devices are not supported.
480 	 */
481 	if (virtio_read_caps(hw) != 0) {
482 		SPDK_NOTICELOG("Ignoring legacy PCI device at %s\n", bdf);
483 		free_virtio_hw(hw);
484 		return -1;
485 	}
486 
487 	rc = ctx->enum_cb((struct virtio_pci_ctx *)hw, ctx->enum_ctx);
488 	if (rc != 0) {
489 		free_virtio_hw(hw);
490 	}
491 
492 	return rc;
493 }
494 
495 static int
496 virtio_pci_dev_probe_cb(void *probe_ctx, struct spdk_pci_device *pci_dev)
497 {
498 	struct virtio_pci_probe_ctx *ctx = probe_ctx;
499 	uint16_t pci_device_id = spdk_pci_device_get_device_id(pci_dev);
500 
501 	if (pci_device_id != ctx->device_id) {
502 		return 1;
503 	}
504 
505 	return virtio_pci_dev_probe(pci_dev, ctx);
506 }
507 
508 int
509 virtio_pci_dev_enumerate(virtio_pci_create_cb enum_cb, void *enum_ctx,
510 			 uint16_t pci_device_id)
511 {
512 	struct virtio_pci_probe_ctx ctx;
513 
514 	if (!spdk_process_is_primary()) {
515 		SPDK_WARNLOG("virtio_pci secondary process support is not implemented yet.\n");
516 		return 0;
517 	}
518 
519 	ctx.enum_cb = enum_cb;
520 	ctx.enum_ctx = enum_ctx;
521 	ctx.device_id = pci_device_id;
522 
523 	return spdk_pci_virtio_enumerate(virtio_pci_dev_probe_cb, &ctx);
524 }
525 
526 int
527 virtio_pci_dev_attach(virtio_pci_create_cb enum_cb, void *enum_ctx,
528 		      uint16_t pci_device_id, struct spdk_pci_addr *pci_address)
529 {
530 	struct virtio_pci_probe_ctx ctx;
531 
532 	if (!spdk_process_is_primary()) {
533 		SPDK_WARNLOG("virtio_pci secondary process support is not implemented yet.\n");
534 		return 0;
535 	}
536 
537 	ctx.enum_cb = enum_cb;
538 	ctx.enum_ctx = enum_ctx;
539 	ctx.device_id = pci_device_id;
540 
541 	return spdk_pci_virtio_device_attach(virtio_pci_dev_probe_cb, &ctx, pci_address);
542 }
543 
544 int
545 virtio_pci_dev_init(struct virtio_dev *vdev, const char *name,
546 		    struct virtio_pci_ctx *pci_ctx)
547 {
548 	int rc;
549 
550 	rc = virtio_dev_construct(vdev, name, &modern_ops, pci_ctx);
551 	if (rc != 0) {
552 		return -1;
553 	}
554 
555 	vdev->is_hw = 1;
556 	vdev->modern = 1;
557 
558 	return 0;
559 }
560 
561 SPDK_LOG_REGISTER_COMPONENT("virtio_pci", SPDK_LOG_VIRTIO_PCI)
562