1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright �� 2023 Dmitry Salychev
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/systm.h>
31
32#include <machine/bus.h>
33
34#include "dpaa2_types.h"
35
36#define COMPARE_TYPE(t, v)		(strncmp((v), (t), strlen((v))) == 0)
37
38/**
39 * @brief Convert DPAA2 device type to string.
40 */
41const char *
42dpaa2_ttos(enum dpaa2_dev_type type)
43{
44	switch (type) {
45	case DPAA2_DEV_MC:
46		return ("mc"); /* NOTE: to print as information only. */
47	case DPAA2_DEV_RC:
48		return ("dprc");
49	case DPAA2_DEV_IO:
50		return ("dpio");
51	case DPAA2_DEV_NI:
52		return ("dpni");
53	case DPAA2_DEV_MCP:
54		return ("dpmcp");
55	case DPAA2_DEV_BP:
56		return ("dpbp");
57	case DPAA2_DEV_CON:
58		return ("dpcon");
59	case DPAA2_DEV_MAC:
60		return ("dpmac");
61	case DPAA2_DEV_MUX:
62		return ("dpdmux");
63	case DPAA2_DEV_SW:
64		return ("dpsw");
65	default:
66		break;
67	}
68
69	return ("notype");
70}
71
72/**
73 * @brief Convert string to DPAA2 device type.
74 */
75enum dpaa2_dev_type
76dpaa2_stot(const char *str)
77{
78	if (COMPARE_TYPE(str, "dprc")) {
79		return (DPAA2_DEV_RC);
80	} else if (COMPARE_TYPE(str, "dpio")) {
81		return (DPAA2_DEV_IO);
82	} else if (COMPARE_TYPE(str, "dpni")) {
83		return (DPAA2_DEV_NI);
84	} else if (COMPARE_TYPE(str, "dpmcp")) {
85		return (DPAA2_DEV_MCP);
86	} else if (COMPARE_TYPE(str, "dpbp")) {
87		return (DPAA2_DEV_BP);
88	} else if (COMPARE_TYPE(str, "dpcon")) {
89		return (DPAA2_DEV_CON);
90	} else if (COMPARE_TYPE(str, "dpmac")) {
91		return (DPAA2_DEV_MAC);
92	} else if (COMPARE_TYPE(str, "dpdmux")) {
93		return (DPAA2_DEV_MUX);
94	} else if (COMPARE_TYPE(str, "dpsw")) {
95		return (DPAA2_DEV_SW);
96	}
97
98	return (DPAA2_DEV_NOTYPE);
99}
100
101/**
102 * @brief Callback to obtain a physical address of the only DMA segment mapped.
103 */
104void
105dpaa2_dmamap_oneseg_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
106{
107	if (error == 0) {
108		KASSERT(nseg == 1, ("%s: too many segments: nseg=%d\n",
109		    __func__, nseg));
110		*(bus_addr_t *)arg = segs[0].ds_addr;
111	} else {
112		panic("%s: error=%d\n", __func__, error);
113	}
114}
115