2 * Copyright (c) 2006 IronPort Systems Inc. <ambrisko@ironport.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/condvar.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/module.h>
38 #include <sys/selinfo.h>
40 #include <dev/smbus/smbconf.h>
41 #include <dev/smbus/smb.h>
48 #include <dev/ipmi/ipmivars.h>
51 #define SMBUS_WRITE_SINGLE 0x02
52 #define SMBUS_WRITE_START 0x06
53 #define SMBUS_WRITE_CONT 0x07
54 #define SMBUS_READ_START 0x03
55 #define SMBUS_READ_CONT 0x09
56 #define SMBUS_DATA_SIZE 32
60 dump_buffer(device_t dev, const char *msg, u_char *bytes, int len)
64 device_printf(dev, "%s:", msg);
65 for (i = 0; i < len; i++)
66 printf(" %02x", bytes[i]);
72 ssif_polled_request(struct ipmi_softc *sc, struct ipmi_request *req)
74 u_char ssif_buf[SMBUS_DATA_SIZE];
75 device_t dev = sc->ipmi_dev;
76 device_t smbus = sc->ipmi_ssif_smbus;
77 u_char *cp, block, count, offset;
81 /* Acquire the bus while we send the request. */
82 if (smbus_request_bus(smbus, dev, SMB_WAIT) != 0)
86 * First, send out the request. Begin by filling out the first
87 * packet which includes the NetFn/LUN and command.
89 ssif_buf[0] = req->ir_addr;
90 ssif_buf[1] = req->ir_command;
91 if (req->ir_requestlen > 0)
92 bcopy(req->ir_request, &ssif_buf[2],
93 min(req->ir_requestlen, SMBUS_DATA_SIZE - 2));
95 /* Small requests are sent with a single command. */
96 if (req->ir_requestlen <= 30) {
98 dump_buffer(dev, "WRITE_SINGLE", ssif_buf,
99 req->ir_requestlen + 2);
101 error = smbus_error(smbus_bwrite(smbus,
102 sc->ipmi_ssif_smbus_address, SMBUS_WRITE_SINGLE,
103 req->ir_requestlen + 2, ssif_buf));
105 #ifdef SSIF_ERROR_DEBUG
106 device_printf(dev, "SSIF: WRITE_SINGLE error %d\n",
112 /* Longer requests are sent out in 32-byte messages. */
114 dump_buffer(dev, "WRITE_START", ssif_buf, SMBUS_DATA_SIZE);
116 error = smbus_error(smbus_bwrite(smbus,
117 sc->ipmi_ssif_smbus_address, SMBUS_WRITE_START,
118 SMBUS_DATA_SIZE, ssif_buf));
120 #ifdef SSIF_ERROR_DEBUG
121 device_printf(dev, "SSIF: WRITE_START error %d\n",
127 len = req->ir_requestlen - (SMBUS_DATA_SIZE - 2);
128 cp = req->ir_request + (SMBUS_DATA_SIZE - 2);
131 dump_buffer(dev, "WRITE_CONT", cp,
132 min(len, SMBUS_DATA_SIZE));
134 error = smbus_error(smbus_bwrite(smbus,
135 sc->ipmi_ssif_smbus_address, SMBUS_WRITE_CONT,
136 min(len, SMBUS_DATA_SIZE), cp));
138 #ifdef SSIF_ERROR_DEBUG
139 device_printf(dev, "SSIF: WRITE_CONT error %d\n",
144 cp += SMBUS_DATA_SIZE;
145 len -= SMBUS_DATA_SIZE;
149 * The final WRITE_CONT transaction has to have a non-zero
150 * length that is also not SMBUS_DATA_SIZE. If our last
151 * WRITE_CONT transaction in the loop sent SMBUS_DATA_SIZE
152 * bytes, then len will be 0, and we send an extra 0x00 byte
153 * to terminate the transaction.
159 dump_buffer(dev, "WRITE_CONT", &c, 1);
161 error = smbus_error(smbus_bwrite(smbus,
162 sc->ipmi_ssif_smbus_address, SMBUS_WRITE_CONT,
165 #ifdef SSIF_ERROR_DEBUG
166 device_printf(dev, "SSIF: WRITE_CONT error %d\n",
174 /* Release the bus. */
175 smbus_release_bus(smbus, dev);
177 /* Give the BMC 100ms to chew on the request. */
178 pause("ssifwt", hz / 10);
180 /* Try to read the first packet. */
182 if (smbus_request_bus(smbus, dev, SMB_WAIT) != 0)
184 count = SMBUS_DATA_SIZE;
185 error = smbus_error(smbus_bread(smbus,
186 sc->ipmi_ssif_smbus_address, SMBUS_READ_START, &count, ssif_buf));
187 if (error == ENXIO || error == EBUSY) {
188 smbus_release_bus(smbus, dev);
190 device_printf(dev, "SSIF: READ_START retry\n");
192 /* Give the BMC another 10ms. */
193 pause("ssifwt", hz / 100);
197 #ifdef SSIF_ERROR_DEBUG
198 device_printf(dev, "SSIF: READ_START failed: %d\n", error);
203 device_printf("SSIF: READ_START: ok\n");
207 * If this is the first part of a multi-part read, then we need to
208 * skip the first two bytes.
210 if (count == SMBUS_DATA_SIZE && ssif_buf[0] == 0 && ssif_buf[1] == 1)
215 /* We had better get the reply header. */
217 device_printf(dev, "SSIF: Short reply packet\n");
221 /* Verify the NetFn/LUN. */
222 if (ssif_buf[offset] != IPMI_REPLY_ADDR(req->ir_addr)) {
223 device_printf(dev, "SSIF: Reply address mismatch\n");
227 /* Verify the command. */
228 if (ssif_buf[offset + 1] != req->ir_command) {
229 device_printf(dev, "SMIC: Command mismatch\n");
233 /* Read the completion code. */
234 req->ir_compcode = ssif_buf[offset + 2];
236 /* If this is a single read, just copy the data and return. */
239 dump_buffer(dev, "READ_SINGLE", ssif_buf, count);
242 bcopy(&ssif_buf[3], req->ir_reply,
243 min(req->ir_replybuflen, len));
248 * This is the first part of a multi-read transaction, so copy
249 * out the payload and start looping.
252 dump_buffer(dev, "READ_START", ssif_buf + 2, count - 2);
254 bcopy(&ssif_buf[5], req->ir_reply, min(req->ir_replybuflen, count - 5));
259 /* Read another packet via READ_CONT. */
260 count = SMBUS_DATA_SIZE;
261 error = smbus_error(smbus_bread(smbus,
262 sc->ipmi_ssif_smbus_address, SMBUS_READ_CONT, &count,
265 #ifdef SSIF_ERROR_DEBUG
266 printf("SSIF: READ_CONT failed: %d\n", error);
271 device_printf(dev, "SSIF: READ_CONT... ok\n");
274 /* Verify the block number. 0xff marks the last block. */
275 if (ssif_buf[0] != 0xff && ssif_buf[0] != block) {
276 device_printf(dev, "SSIF: Read wrong block %d %d\n",
280 if (ssif_buf[0] != 0xff && count < SMBUS_DATA_SIZE) {
282 "SSIF: Read short middle block, length %d\n",
287 if (ssif_buf[0] == 0xff)
288 dump_buffer(dev, "READ_END", ssif_buf + 1, count - 1);
290 dump_buffer(dev, "READ_CONT", ssif_buf + 1, count - 1);
292 if (len < req->ir_replybuflen)
293 bcopy(&ssif_buf[1], &req->ir_reply[len],
294 min(req->ir_replybuflen - len, count - 1));
297 /* If this was the last block we are done. */
298 if (ssif_buf[0] != 0xff)
304 /* Save the total length and return success. */
305 req->ir_replylen = len;
306 smbus_release_bus(smbus, dev);
310 smbus_release_bus(smbus, dev);
317 struct ipmi_softc *sc = arg;
318 struct ipmi_request *req;
322 while ((req = ipmi_dequeue_request(sc)) != NULL) {
325 for (i = 0; i < 5; i++) {
326 ok = ssif_polled_request(sc, req);
330 /* Wait 60 ms between retries. */
331 pause("retry", 60 * hz / 1000);
332 #ifdef SSIF_RETRY_DEBUG
333 device_printf(sc->ipmi_dev,
334 "SSIF: Retrying request (%d)\n", i + 1);
342 ipmi_complete_request(sc, req);
345 /* Enforce 10ms between requests. */
346 pause("delay", hz / 100);
355 ssif_startup(struct ipmi_softc *sc)
358 return (kthread_create(ssif_loop, sc, &sc->ipmi_kthread, 0, 0,
359 "%s: ssif", device_get_nameunit(sc->ipmi_dev)));
363 ipmi_ssif_attach(struct ipmi_softc *sc, device_t smbus, int smbus_address)
366 /* Setup smbus address. */
367 sc->ipmi_ssif_smbus = smbus;
368 sc->ipmi_ssif_smbus_address = smbus_address;
370 /* Setup function pointers. */
371 sc->ipmi_startup = ssif_startup;
372 sc->ipmi_enqueue_request = ipmi_polled_enqueue_request;