xref: /spdk/lib/virtio/virtio_pci.c (revision 1196deb504b4d146ce750be88cdd2517a73edfc7)
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 int
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 	return 0;
170 }
171 
172 static int
173 modern_write_dev_config(struct virtio_dev *dev, size_t offset,
174 			const void *src, int length)
175 {
176 	struct virtio_hw *hw = dev->ctx;
177 	int i;
178 	const uint8_t *p = src;
179 
180 	for (i = 0;  i < length; i++) {
181 		spdk_mmio_write_1(((uint8_t *)hw->dev_cfg) + offset + i, *p++);
182 	}
183 
184 	return 0;
185 }
186 
187 static uint64_t
188 modern_get_features(struct virtio_dev *dev)
189 {
190 	struct virtio_hw *hw = dev->ctx;
191 	uint32_t features_lo, features_hi;
192 
193 	spdk_mmio_write_4(&hw->common_cfg->device_feature_select, 0);
194 	features_lo = spdk_mmio_read_4(&hw->common_cfg->device_feature);
195 
196 	spdk_mmio_write_4(&hw->common_cfg->device_feature_select, 1);
197 	features_hi = spdk_mmio_read_4(&hw->common_cfg->device_feature);
198 
199 	return ((uint64_t)features_hi << 32) | features_lo;
200 }
201 
202 static int
203 modern_set_features(struct virtio_dev *dev, uint64_t features)
204 {
205 	struct virtio_hw *hw = dev->ctx;
206 
207 	if ((features & (1ULL << VIRTIO_F_VERSION_1)) == 0) {
208 		SPDK_ERRLOG("VIRTIO_F_VERSION_1 feature is not enabled.\n");
209 		return -1;
210 	}
211 
212 	spdk_mmio_write_4(&hw->common_cfg->guest_feature_select, 0);
213 	spdk_mmio_write_4(&hw->common_cfg->guest_feature, features & ((1ULL << 32) - 1));
214 
215 	spdk_mmio_write_4(&hw->common_cfg->guest_feature_select, 1);
216 	spdk_mmio_write_4(&hw->common_cfg->guest_feature, features >> 32);
217 
218 	dev->negotiated_features = features;
219 
220 	return 0;
221 }
222 
223 static void
224 modern_destruct_dev(struct virtio_dev *vdev)
225 {
226 	struct virtio_hw *hw = vdev->ctx;
227 	struct spdk_pci_device *pci_dev = hw->pci_dev;
228 
229 	free_virtio_hw(hw);
230 	spdk_pci_device_detach(pci_dev);
231 }
232 
233 static uint8_t
234 modern_get_status(struct virtio_dev *dev)
235 {
236 	struct virtio_hw *hw = dev->ctx;
237 
238 	return spdk_mmio_read_1(&hw->common_cfg->device_status);
239 }
240 
241 static void
242 modern_set_status(struct virtio_dev *dev, uint8_t status)
243 {
244 	struct virtio_hw *hw = dev->ctx;
245 
246 	spdk_mmio_write_1(&hw->common_cfg->device_status, status);
247 }
248 
249 static uint16_t
250 modern_get_queue_size(struct virtio_dev *dev, uint16_t queue_id)
251 {
252 	struct virtio_hw *hw = dev->ctx;
253 
254 	spdk_mmio_write_2(&hw->common_cfg->queue_select, queue_id);
255 	return spdk_mmio_read_2(&hw->common_cfg->queue_size);
256 }
257 
258 static int
259 modern_setup_queue(struct virtio_dev *dev, struct virtqueue *vq)
260 {
261 	struct virtio_hw *hw = dev->ctx;
262 	uint64_t desc_addr, avail_addr, used_addr;
263 	uint16_t notify_off;
264 	void *queue_mem;
265 	uint64_t queue_mem_phys_addr;
266 
267 	queue_mem = spdk_dma_zmalloc(vq->vq_ring_size, VIRTIO_PCI_VRING_ALIGN, &queue_mem_phys_addr);
268 	if (queue_mem == NULL) {
269 		return -ENOMEM;
270 	}
271 
272 	vq->vq_ring_mem = queue_mem_phys_addr;
273 	vq->vq_ring_virt_mem = queue_mem;
274 
275 	if (!check_vq_phys_addr_ok(vq)) {
276 		return -1;
277 	}
278 
279 	desc_addr = vq->vq_ring_mem;
280 	avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
281 	used_addr = (avail_addr + offsetof(struct vring_avail, ring[vq->vq_nentries])
282 		     + VIRTIO_PCI_VRING_ALIGN - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1);
283 
284 	spdk_mmio_write_2(&hw->common_cfg->queue_select, vq->vq_queue_index);
285 
286 	io_write64_twopart(desc_addr, &hw->common_cfg->queue_desc_lo,
287 			   &hw->common_cfg->queue_desc_hi);
288 	io_write64_twopart(avail_addr, &hw->common_cfg->queue_avail_lo,
289 			   &hw->common_cfg->queue_avail_hi);
290 	io_write64_twopart(used_addr, &hw->common_cfg->queue_used_lo,
291 			   &hw->common_cfg->queue_used_hi);
292 
293 	notify_off = spdk_mmio_read_2(&hw->common_cfg->queue_notify_off);
294 	vq->notify_addr = (void *)((uint8_t *)hw->notify_base +
295 				   notify_off * hw->notify_off_multiplier);
296 
297 	spdk_mmio_write_2(&hw->common_cfg->queue_enable, 1);
298 
299 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "queue %"PRIu16" addresses:\n", vq->vq_queue_index);
300 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t desc_addr: %" PRIx64 "\n", desc_addr);
301 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t aval_addr: %" PRIx64 "\n", avail_addr);
302 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t used_addr: %" PRIx64 "\n", used_addr);
303 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "\t notify addr: %p (notify offset: %"PRIu16")\n",
304 		      vq->notify_addr, notify_off);
305 
306 	return 0;
307 }
308 
309 static void
310 modern_del_queue(struct virtio_dev *dev, struct virtqueue *vq)
311 {
312 	struct virtio_hw *hw = dev->ctx;
313 
314 	spdk_mmio_write_2(&hw->common_cfg->queue_select, vq->vq_queue_index);
315 
316 	io_write64_twopart(0, &hw->common_cfg->queue_desc_lo,
317 			   &hw->common_cfg->queue_desc_hi);
318 	io_write64_twopart(0, &hw->common_cfg->queue_avail_lo,
319 			   &hw->common_cfg->queue_avail_hi);
320 	io_write64_twopart(0, &hw->common_cfg->queue_used_lo,
321 			   &hw->common_cfg->queue_used_hi);
322 
323 	spdk_mmio_write_2(&hw->common_cfg->queue_enable, 0);
324 
325 	spdk_dma_free(vq->vq_ring_virt_mem);
326 }
327 
328 static void
329 modern_notify_queue(struct virtio_dev *dev, struct virtqueue *vq)
330 {
331 	spdk_mmio_write_2(vq->notify_addr, vq->vq_queue_index);
332 }
333 
334 static const struct virtio_dev_ops modern_ops = {
335 	.read_dev_cfg	= modern_read_dev_config,
336 	.write_dev_cfg	= modern_write_dev_config,
337 	.get_status	= modern_get_status,
338 	.set_status	= modern_set_status,
339 	.get_features	= modern_get_features,
340 	.set_features	= modern_set_features,
341 	.destruct_dev	= modern_destruct_dev,
342 	.get_queue_size	= modern_get_queue_size,
343 	.setup_queue	= modern_setup_queue,
344 	.del_queue	= modern_del_queue,
345 	.notify_queue	= modern_notify_queue,
346 	.dump_json_info = pci_dump_json_info,
347 	.write_json_config = pci_write_json_config,
348 };
349 
350 static void *
351 get_cfg_addr(struct virtio_hw *hw, struct virtio_pci_cap *cap)
352 {
353 	uint8_t  bar    = cap->bar;
354 	uint32_t length = cap->length;
355 	uint32_t offset = cap->offset;
356 
357 	if (bar > 5) {
358 		SPDK_ERRLOG("invalid bar: %"PRIu8"\n", bar);
359 		return NULL;
360 	}
361 
362 	if (offset + length < offset) {
363 		SPDK_ERRLOG("offset(%"PRIu32") + length(%"PRIu32") overflows\n",
364 			    offset, length);
365 		return NULL;
366 	}
367 
368 	if (offset + length > hw->pci_bar[bar].len) {
369 		SPDK_ERRLOG("invalid cap: overflows bar space: %"PRIu32" > %"PRIu32"\n",
370 			    offset + length, hw->pci_bar[bar].len);
371 		return NULL;
372 	}
373 
374 	if (hw->pci_bar[bar].vaddr == NULL) {
375 		SPDK_ERRLOG("bar %"PRIu8" base addr is NULL\n", bar);
376 		return NULL;
377 	}
378 
379 	return hw->pci_bar[bar].vaddr + offset;
380 }
381 
382 static int
383 virtio_read_caps(struct virtio_hw *hw)
384 {
385 	uint8_t pos;
386 	struct virtio_pci_cap cap;
387 	int ret;
388 
389 	ret = spdk_pci_device_cfg_read(hw->pci_dev, &pos, 1, PCI_CAPABILITY_LIST);
390 	if (ret < 0) {
391 		SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "failed to read pci capability list\n");
392 		return -1;
393 	}
394 
395 	while (pos) {
396 		ret = spdk_pci_device_cfg_read(hw->pci_dev, &cap, sizeof(cap), pos);
397 		if (ret < 0) {
398 			SPDK_ERRLOG("failed to read pci cap at pos: %"PRIx8"\n", pos);
399 			break;
400 		}
401 
402 		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
403 			hw->use_msix = 1;
404 		}
405 
406 		if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
407 			SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI,
408 				      "[%2"PRIx8"] skipping non VNDR cap id: %02"PRIx8"\n",
409 				      pos, cap.cap_vndr);
410 			goto next;
411 		}
412 
413 		SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI,
414 			      "[%2"PRIx8"] cfg type: %"PRIu8", bar: %"PRIu8", offset: %04"PRIx32", len: %"PRIu32"\n",
415 			      pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
416 
417 		switch (cap.cfg_type) {
418 		case VIRTIO_PCI_CAP_COMMON_CFG:
419 			hw->common_cfg = get_cfg_addr(hw, &cap);
420 			break;
421 		case VIRTIO_PCI_CAP_NOTIFY_CFG:
422 			spdk_pci_device_cfg_read(hw->pci_dev, &hw->notify_off_multiplier,
423 						 4, pos + sizeof(cap));
424 			hw->notify_base = get_cfg_addr(hw, &cap);
425 			break;
426 		case VIRTIO_PCI_CAP_DEVICE_CFG:
427 			hw->dev_cfg = get_cfg_addr(hw, &cap);
428 			break;
429 		case VIRTIO_PCI_CAP_ISR_CFG:
430 			hw->isr = get_cfg_addr(hw, &cap);
431 			break;
432 		}
433 
434 next:
435 		pos = cap.cap_next;
436 	}
437 
438 	if (hw->common_cfg == NULL || hw->notify_base == NULL ||
439 	    hw->dev_cfg == NULL    || hw->isr == NULL) {
440 		SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "no modern virtio pci device found.\n");
441 		return -1;
442 	}
443 
444 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "found modern virtio pci device.\n");
445 
446 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "common cfg mapped at: %p\n", hw->common_cfg);
447 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "device cfg mapped at: %p\n", hw->dev_cfg);
448 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "isr cfg mapped at: %p\n", hw->isr);
449 	SPDK_DEBUGLOG(SPDK_LOG_VIRTIO_PCI, "notify base: %p, notify off multiplier: %u\n",
450 		      hw->notify_base, hw->notify_off_multiplier);
451 
452 	return 0;
453 }
454 
455 static int
456 virtio_pci_dev_probe(struct spdk_pci_device *pci_dev, struct virtio_pci_probe_ctx *ctx)
457 {
458 	struct virtio_hw *hw;
459 	uint8_t *bar_vaddr;
460 	uint64_t bar_paddr, bar_len;
461 	int rc;
462 	unsigned i;
463 	char bdf[32];
464 	struct spdk_pci_addr addr;
465 
466 	addr = spdk_pci_device_get_addr(pci_dev);
467 	rc = spdk_pci_addr_fmt(bdf, sizeof(bdf), &addr);
468 	if (rc != 0) {
469 		SPDK_ERRLOG("Ignoring a device with non-parseable PCI address\n");
470 		return -1;
471 	}
472 
473 	hw = calloc(1, sizeof(*hw));
474 	if (hw == NULL) {
475 		SPDK_ERRLOG("%s: calloc failed\n", bdf);
476 		return -1;
477 	}
478 
479 	hw->pci_dev = pci_dev;
480 
481 	for (i = 0; i < 6; ++i) {
482 		rc = spdk_pci_device_map_bar(pci_dev, i, (void *) &bar_vaddr, &bar_paddr,
483 					     &bar_len);
484 		if (rc != 0) {
485 			SPDK_ERRLOG("%s: failed to memmap PCI BAR %u\n", bdf, i);
486 			free_virtio_hw(hw);
487 			return -1;
488 		}
489 
490 		hw->pci_bar[i].vaddr = bar_vaddr;
491 		hw->pci_bar[i].len = bar_len;
492 	}
493 
494 	/* Virtio PCI caps exist only on modern PCI devices.
495 	 * Legacy devices are not supported.
496 	 */
497 	if (virtio_read_caps(hw) != 0) {
498 		SPDK_NOTICELOG("Ignoring legacy PCI device at %s\n", bdf);
499 		free_virtio_hw(hw);
500 		return -1;
501 	}
502 
503 	rc = ctx->enum_cb((struct virtio_pci_ctx *)hw, ctx->enum_ctx);
504 	if (rc != 0) {
505 		free_virtio_hw(hw);
506 	}
507 
508 	return rc;
509 }
510 
511 static int
512 virtio_pci_dev_probe_cb(void *probe_ctx, struct spdk_pci_device *pci_dev)
513 {
514 	struct virtio_pci_probe_ctx *ctx = probe_ctx;
515 	uint16_t pci_device_id = spdk_pci_device_get_device_id(pci_dev);
516 
517 	if (pci_device_id != ctx->device_id) {
518 		return 1;
519 	}
520 
521 	return virtio_pci_dev_probe(pci_dev, ctx);
522 }
523 
524 int
525 virtio_pci_dev_enumerate(virtio_pci_create_cb enum_cb, void *enum_ctx,
526 			 uint16_t pci_device_id)
527 {
528 	struct virtio_pci_probe_ctx ctx;
529 
530 	if (!spdk_process_is_primary()) {
531 		SPDK_WARNLOG("virtio_pci secondary process support is not implemented yet.\n");
532 		return 0;
533 	}
534 
535 	ctx.enum_cb = enum_cb;
536 	ctx.enum_ctx = enum_ctx;
537 	ctx.device_id = pci_device_id;
538 
539 	return spdk_pci_virtio_enumerate(virtio_pci_dev_probe_cb, &ctx);
540 }
541 
542 int
543 virtio_pci_dev_attach(virtio_pci_create_cb enum_cb, void *enum_ctx,
544 		      uint16_t pci_device_id, struct spdk_pci_addr *pci_address)
545 {
546 	struct virtio_pci_probe_ctx ctx;
547 
548 	if (!spdk_process_is_primary()) {
549 		SPDK_WARNLOG("virtio_pci secondary process support is not implemented yet.\n");
550 		return 0;
551 	}
552 
553 	ctx.enum_cb = enum_cb;
554 	ctx.enum_ctx = enum_ctx;
555 	ctx.device_id = pci_device_id;
556 
557 	return spdk_pci_virtio_device_attach(virtio_pci_dev_probe_cb, &ctx, pci_address);
558 }
559 
560 int
561 virtio_pci_dev_init(struct virtio_dev *vdev, const char *name,
562 		    struct virtio_pci_ctx *pci_ctx)
563 {
564 	int rc;
565 
566 	rc = virtio_dev_construct(vdev, name, &modern_ops, pci_ctx);
567 	if (rc != 0) {
568 		return -1;
569 	}
570 
571 	vdev->is_hw = 1;
572 	vdev->modern = 1;
573 
574 	return 0;
575 }
576 
577 SPDK_LOG_REGISTER_COMPONENT("virtio_pci", SPDK_LOG_VIRTIO_PCI)
578