xref: /illumos-gate/usr/src/uts/common/io/ena/ena_hw.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2021 Oxide Computer Company
14  */
15 
16 #include "ena_hw.h"
17 #include "ena.h"
18 
19 uint32_t
20 ena_hw_bar_read32(const ena_t *ena, const uint16_t offset)
21 {
22 	caddr_t addr = ena->ena_reg_base + offset;
23 	return (ena_hw_abs_read32(ena, (uint32_t *)addr));
24 }
25 
26 uint32_t
27 ena_hw_abs_read32(const ena_t *ena, uint32_t *addr)
28 {
29 	VERIFY3U(addr, >=, ena->ena_reg_base);
30 	VERIFY3U(addr, <, ena->ena_reg_base + (ena->ena_reg_size - 4));
31 
32 	return (ddi_get32(ena->ena_reg_hdl, addr));
33 }
34 
35 void
36 ena_hw_bar_write32(const ena_t *ena, const uint16_t offset, const uint32_t val)
37 {
38 	caddr_t addr = ena->ena_reg_base + offset;
39 	ena_hw_abs_write32(ena, (uint32_t *)addr, val);
40 }
41 
42 void
43 ena_hw_abs_write32(const ena_t *ena, uint32_t *addr, const uint32_t val)
44 {
45 	VERIFY3P(ena, !=, NULL);
46 	VERIFY3P(addr, !=, NULL);
47 	VERIFY3U(addr, >=, ena->ena_reg_base);
48 	VERIFY3U(addr, <, ena->ena_reg_base + (ena->ena_reg_size - 4));
49 
50 	ddi_put32(ena->ena_reg_hdl, addr, val);
51 }
52 
53 int
54 enahw_resp_status_to_errno(ena_t *ena, enahw_resp_status_t status)
55 {
56 	int ret = 0;
57 
58 	switch (status) {
59 	case ENAHW_RESP_SUCCESS:
60 		break;
61 
62 	case ENAHW_RESP_RESOURCE_ALLOCATION_FAILURE:
63 		ret = ENOMEM;
64 		break;
65 
66 	case ENAHW_RESP_UNSUPPORTED_OPCODE:
67 		ret = ENOTSUP;
68 		break;
69 
70 	case ENAHW_RESP_BAD_OPCODE:
71 	case ENAHW_RESP_MALFORMED_REQUEST:
72 	case ENAHW_RESP_ILLEGAL_PARAMETER:
73 		ret = EINVAL;
74 		break;
75 
76 	case ENAHW_RESP_RESOURCE_BUSY:
77 		ret = EAGAIN;
78 		break;
79 
80 	case ENAHW_RESP_UNKNOWN_ERROR:
81 	default:
82 		/*
83 		 * If the device presents us with an "unknwon error"
84 		 * code, or the status code is undefined, then we log
85 		 * an error and convert it to EIO.
86 		 */
87 		ena_err(ena, "unexpected status code: %d", status);
88 		ret = EIO;
89 		break;
90 	}
91 
92 	return (ret);
93 }
94