xref: /freebsd/sys/dev/ipmi/ipmivars.h (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef __IPMIVARS_H__
32 #define	__IPMIVARS_H__
33 
34 struct ipmi_get_info {
35 	int		iface_type;
36 	uint64_t	address;
37 	int		offset;
38 	int		io_mode;
39 	int		irq;
40 };
41 
42 struct ipmi_device;
43 
44 struct ipmi_request {
45 	TAILQ_ENTRY(ipmi_request) ir_link;
46 	struct ipmi_device *ir_owner;	/* Driver uses NULL. */
47 	u_char		*ir_request;	/* Request is data to send to BMC. */
48 	size_t		ir_requestlen;
49 	u_char		*ir_reply;	/* Reply is data read from BMC. */
50 	size_t		ir_replybuflen;	/* Length of ir_reply[] buffer. */
51 	int		ir_replylen;	/* Length of reply from BMC. */
52 	int		ir_error;
53 	long		ir_msgid;
54 	uint8_t		ir_addr;
55 	uint8_t		ir_command;
56 	uint8_t		ir_compcode;
57 	bool		ir_ipmb;
58 	uint8_t		ir_ipmb_addr;
59 	uint8_t		ir_ipmb_command;
60 };
61 
62 #define	IPMI_IF_KCS_NRES		2
63 #define	IPMI_IF_SMIC_NRES		3
64 #define	IPMI_IF_BT_NRES			3
65 
66 #define	MAX_RES				3
67 #define KCS_DATA			0
68 #define KCS_CTL_STS			1
69 #define SMIC_DATA			0
70 #define SMIC_CTL_STS			1
71 #define SMIC_FLAGS			2
72 
73 struct ipmi_softc;
74 
75 /* Per file descriptor data. */
76 struct ipmi_device {
77 	TAILQ_ENTRY(ipmi_device) ipmi_link;
78 	TAILQ_HEAD(,ipmi_request) ipmi_completed_requests;
79 	struct selinfo		ipmi_select;
80 	struct ipmi_softc	*ipmi_softc;
81 	int			ipmi_closing;
82 	int			ipmi_requests;
83 	u_char			ipmi_address;	/* IPMB address. */
84 	u_char			ipmi_lun;
85 };
86 
87 struct ipmi_bt {
88 	uint8_t	seq;
89 };
90 
91 struct ipmi_kcs {
92 };
93 
94 struct ipmi_smic {
95 };
96 
97 struct ipmi_ssif {
98 	device_t smbus;
99 	int	smbus_address;
100 };
101 
102 struct ipmi_softc {
103 	device_t		ipmi_dev;
104 	union {
105 		struct ipmi_bt bt;
106 		struct ipmi_kcs kcs;
107 		struct ipmi_smic smic;
108 		struct ipmi_ssif ssif;
109 	} _iface;
110 	int			ipmi_io_rid;
111 	int			ipmi_io_type;
112 	struct mtx		ipmi_io_lock;
113 	struct resource		*ipmi_io_res[MAX_RES];
114 	int			ipmi_io_spacing;
115 	int			ipmi_irq_rid;
116 	struct resource		*ipmi_irq_res;
117 	void			*ipmi_irq;
118 	int			ipmi_detaching;
119 	int			ipmi_opened;
120 	uint8_t			ipmi_dev_support;	/* IPMI_ADS_* */
121 	struct cdev		*ipmi_cdev;
122 	TAILQ_HEAD(,ipmi_request) ipmi_pending_requests;
123 	TAILQ_HEAD(,ipmi_request) ipmi_pending_requests_highpri;
124 	int			ipmi_driver_requests_polled;
125 	eventhandler_tag	ipmi_power_cycle_tag;
126 	eventhandler_tag	ipmi_watchdog_tag;
127 	eventhandler_tag	ipmi_shutdown_tag;
128 	int			ipmi_watchdog_active;
129 	int			ipmi_watchdog_actions;
130 	int			ipmi_watchdog_pretimeout;
131 	struct intr_config_hook	ipmi_ich;
132 	struct mtx		ipmi_requests_lock;
133 	struct cv		ipmi_request_added;
134 	struct proc		*ipmi_kthread;
135 	driver_intr_t		*ipmi_intr;
136 	int			(*ipmi_startup)(struct ipmi_softc *);
137 	int			(*ipmi_enqueue_request)(struct ipmi_softc *, struct ipmi_request *);
138 	int			(*ipmi_driver_request)(struct ipmi_softc *, struct ipmi_request *, int);
139 };
140 
141 #define	ipmi_ssif_smbus_address		_iface.ssif.smbus_address
142 #define	ipmi_ssif_smbus			_iface.ssif.smbus
143 #define	ipmi_bt_seq			_iface.bt.seq
144 
145 #define	KCS_MODE			0x01
146 #define	SMIC_MODE			0x02
147 #define	BT_MODE				0x03
148 #define	SSIF_MODE			0x04
149 
150 /* KCS status flags */
151 #define KCS_STATUS_OBF			0x01 /* Data Out ready from BMC */
152 #define KCS_STATUS_IBF			0x02 /* Data In from System */
153 #define KCS_STATUS_SMS_ATN		0x04 /* Ready in RX queue */
154 #define KCS_STATUS_C_D			0x08 /* Command/Data register write*/
155 #define KCS_STATUS_OEM1			0x10
156 #define KCS_STATUS_OEM2			0x20
157 #define KCS_STATUS_S0			0x40
158 #define KCS_STATUS_S1			0x80
159  #define KCS_STATUS_STATE(x)		((x)>>6)
160  #define KCS_STATUS_STATE_IDLE		0x0
161  #define KCS_STATUS_STATE_READ		0x1
162  #define KCS_STATUS_STATE_WRITE		0x2
163  #define KCS_STATUS_STATE_ERROR		0x3
164 #define	KCS_IFACE_STATUS_OK		0x00
165 #define KCS_IFACE_STATUS_ABORT		0x01
166 #define KCS_IFACE_STATUS_ILLEGAL	0x02
167 #define KCS_IFACE_STATUS_LENGTH_ERR	0x06
168 #define	KCS_IFACE_STATUS_UNKNOWN_ERR	0xff
169 
170 /* KCS control codes */
171 #define KCS_CONTROL_GET_STATUS_ABORT	0x60
172 #define KCS_CONTROL_WRITE_START		0x61
173 #define KCS_CONTROL_WRITE_END		0x62
174 #define KCS_DATA_IN_READ		0x68
175 
176 /* SMIC status flags */
177 #define SMIC_STATUS_BUSY		0x01 /* System set and BMC clears it */
178 #define SMIC_STATUS_SMS_ATN		0x04 /* BMC has a message */
179 #define SMIC_STATUS_EVT_ATN		0x08 /* Event has been RX */
180 #define SMIC_STATUS_SMI			0x10 /* asserted SMI */
181 #define SMIC_STATUS_TX_RDY		0x40 /* Ready to accept WRITE */
182 #define SMIC_STATUS_RX_RDY		0x80 /* Ready to read */
183 #define	SMIC_STATUS_RESERVED		0x22
184 
185 /* SMIC control codes */
186 #define SMIC_CC_SMS_GET_STATUS		0x40
187 #define SMIC_CC_SMS_WR_START		0x41
188 #define SMIC_CC_SMS_WR_NEXT		0x42
189 #define SMIC_CC_SMS_WR_END		0x43
190 #define SMIC_CC_SMS_RD_START		0x44
191 #define SMIC_CC_SMS_RD_NEXT		0x45
192 #define SMIC_CC_SMS_RD_END		0x46
193 
194 /* SMIC status codes */
195 #define SMIC_SC_SMS_RDY			0xc0
196 #define SMIC_SC_SMS_WR_START		0xc1
197 #define SMIC_SC_SMS_WR_NEXT		0xc2
198 #define SMIC_SC_SMS_WR_END		0xc3
199 #define SMIC_SC_SMS_RD_START		0xc4
200 #define SMIC_SC_SMS_RD_NEXT		0xc5
201 #define SMIC_SC_SMS_RD_END		0xc6
202 
203 #define	IPMI_ADDR(netfn, lun)		((netfn) << 2 | (lun))
204 #define	IPMI_REPLY_ADDR(addr)		((addr) + 0x4)
205 
206 #define	IPMI_LOCK(sc)		mtx_lock(&(sc)->ipmi_requests_lock)
207 #define	IPMI_UNLOCK(sc)		mtx_unlock(&(sc)->ipmi_requests_lock)
208 #define	IPMI_LOCK_ASSERT(sc)	mtx_assert(&(sc)->ipmi_requests_lock, MA_OWNED)
209 
210 #define	IPMI_IO_LOCK(sc)	mtx_lock(&(sc)->ipmi_io_lock)
211 #define	IPMI_IO_UNLOCK(sc)	mtx_unlock(&(sc)->ipmi_io_lock)
212 #define	IPMI_IO_LOCK_ASSERT(sc)	mtx_assert(&(sc)->ipmi_io_lock, MA_OWNED)
213 
214 /* I/O to a single I/O resource. */
215 #define INB_SINGLE(sc, x)						\
216 	bus_read_1((sc)->ipmi_io_res[0], (sc)->ipmi_io_spacing * (x))
217 #define OUTB_SINGLE(sc, x, value)					\
218 	bus_write_1((sc)->ipmi_io_res[0], (sc)->ipmi_io_spacing * (x), value)
219 
220 /* I/O with each register in its in I/O resource. */
221 #define INB_MULTIPLE(sc, x)			\
222 	bus_read_1((sc)->ipmi_io_res[(x)], 0)
223 #define OUTB_MULTIPLE(sc, x, value)					\
224 	bus_write_1((sc)->ipmi_io_res[(x)], 0, value)
225 
226 /*
227  * Determine I/O method based on whether or not we have more than one I/O
228  * resource.
229  */
230 #define	INB(sc, x)							\
231 	((sc)->ipmi_io_res[1] != NULL ? INB_MULTIPLE(sc, x) : INB_SINGLE(sc, x))
232 #define	OUTB(sc, x, value)						\
233 	((sc)->ipmi_io_res[1] != NULL ? OUTB_MULTIPLE(sc, x, value) :	\
234 	    OUTB_SINGLE(sc, x, value))
235 
236 #define MAX_TIMEOUT 6 * hz
237 
238 int	ipmi_attach(device_t);
239 int	ipmi_detach(device_t);
240 void	ipmi_release_resources(device_t);
241 
242 /* Manage requests. */
243 void ipmi_init_request(struct ipmi_request *, struct ipmi_device *, long,
244 	    uint8_t, uint8_t, size_t, size_t);
245 struct ipmi_request *ipmi_alloc_request(struct ipmi_device *, long, uint8_t,
246 	    uint8_t, size_t, size_t);
247 void	ipmi_complete_request(struct ipmi_softc *, struct ipmi_request *);
248 struct ipmi_request *ipmi_dequeue_request(struct ipmi_softc *);
249 void	ipmi_free_request(struct ipmi_request *);
250 int	ipmi_polled_enqueue_request(struct ipmi_softc *, struct ipmi_request *);
251 int	ipmi_polled_enqueue_request_highpri(struct ipmi_softc *, struct ipmi_request *);
252 int	ipmi_submit_driver_request(struct ipmi_softc *, struct ipmi_request *,
253 	    int);
254 
255 /* Identify BMC interface via SMBIOS. */
256 int	ipmi_smbios_identify(struct ipmi_get_info *);
257 
258 /* Match BMC PCI device listed in SMBIOS. */
259 const char *ipmi_pci_match(uint16_t, uint16_t);
260 
261 /* Interface attach routines. */
262 int	ipmi_kcs_attach(struct ipmi_softc *);
263 int	ipmi_kcs_probe_align(struct ipmi_softc *);
264 int	ipmi_smic_attach(struct ipmi_softc *);
265 int	ipmi_bt_attach(struct ipmi_softc *);
266 int	ipmi_ssif_attach(struct ipmi_softc *, device_t, int);
267 
268 extern int ipmi_attached;
269 
270 #endif	/* !__IPMIVARS_H__ */
271