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