11739d54cSCorey Minyard /*
21739d54cSCorey Minyard * QEMU IPMI BT emulation
31739d54cSCorey Minyard *
41739d54cSCorey Minyard * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
51739d54cSCorey Minyard *
61739d54cSCorey Minyard * Permission is hereby granted, free of charge, to any person obtaining a copy
71739d54cSCorey Minyard * of this software and associated documentation files (the "Software"), to deal
81739d54cSCorey Minyard * in the Software without restriction, including without limitation the rights
91739d54cSCorey Minyard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101739d54cSCorey Minyard * copies of the Software, and to permit persons to whom the Software is
111739d54cSCorey Minyard * furnished to do so, subject to the following conditions:
121739d54cSCorey Minyard *
131739d54cSCorey Minyard * The above copyright notice and this permission notice shall be included in
141739d54cSCorey Minyard * all copies or substantial portions of the Software.
151739d54cSCorey Minyard *
161739d54cSCorey Minyard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171739d54cSCorey Minyard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181739d54cSCorey Minyard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
191739d54cSCorey Minyard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201739d54cSCorey Minyard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211739d54cSCorey Minyard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
221739d54cSCorey Minyard * THE SOFTWARE.
231739d54cSCorey Minyard */
241739d54cSCorey Minyard #include "qemu/osdep.h"
251739d54cSCorey Minyard #include "migration/vmstate.h"
261739d54cSCorey Minyard #include "qemu/log.h"
271739d54cSCorey Minyard #include "qapi/error.h"
281739d54cSCorey Minyard #include "hw/ipmi/ipmi_bt.h"
291739d54cSCorey Minyard
301739d54cSCorey Minyard /* Control register */
311739d54cSCorey Minyard #define IPMI_BT_CLR_WR_BIT 0
321739d54cSCorey Minyard #define IPMI_BT_CLR_RD_BIT 1
331739d54cSCorey Minyard #define IPMI_BT_H2B_ATN_BIT 2
341739d54cSCorey Minyard #define IPMI_BT_B2H_ATN_BIT 3
351739d54cSCorey Minyard #define IPMI_BT_SMS_ATN_BIT 4
361739d54cSCorey Minyard #define IPMI_BT_HBUSY_BIT 6
371739d54cSCorey Minyard #define IPMI_BT_BBUSY_BIT 7
381739d54cSCorey Minyard
391739d54cSCorey Minyard #define IPMI_BT_GET_CLR_WR(d) (((d) >> IPMI_BT_CLR_WR_BIT) & 0x1)
401739d54cSCorey Minyard
411739d54cSCorey Minyard #define IPMI_BT_GET_CLR_RD(d) (((d) >> IPMI_BT_CLR_RD_BIT) & 0x1)
421739d54cSCorey Minyard
431739d54cSCorey Minyard #define IPMI_BT_GET_H2B_ATN(d) (((d) >> IPMI_BT_H2B_ATN_BIT) & 0x1)
441739d54cSCorey Minyard
451739d54cSCorey Minyard #define IPMI_BT_B2H_ATN_MASK (1 << IPMI_BT_B2H_ATN_BIT)
461739d54cSCorey Minyard #define IPMI_BT_GET_B2H_ATN(d) (((d) >> IPMI_BT_B2H_ATN_BIT) & 0x1)
471739d54cSCorey Minyard #define IPMI_BT_SET_B2H_ATN(d, v) ((d) = (((d) & ~IPMI_BT_B2H_ATN_MASK) | \
481739d54cSCorey Minyard (!!(v) << IPMI_BT_B2H_ATN_BIT)))
491739d54cSCorey Minyard
501739d54cSCorey Minyard #define IPMI_BT_SMS_ATN_MASK (1 << IPMI_BT_SMS_ATN_BIT)
511739d54cSCorey Minyard #define IPMI_BT_GET_SMS_ATN(d) (((d) >> IPMI_BT_SMS_ATN_BIT) & 0x1)
521739d54cSCorey Minyard #define IPMI_BT_SET_SMS_ATN(d, v) ((d) = (((d) & ~IPMI_BT_SMS_ATN_MASK) | \
531739d54cSCorey Minyard (!!(v) << IPMI_BT_SMS_ATN_BIT)))
541739d54cSCorey Minyard
551739d54cSCorey Minyard #define IPMI_BT_HBUSY_MASK (1 << IPMI_BT_HBUSY_BIT)
561739d54cSCorey Minyard #define IPMI_BT_GET_HBUSY(d) (((d) >> IPMI_BT_HBUSY_BIT) & 0x1)
571739d54cSCorey Minyard #define IPMI_BT_SET_HBUSY(d, v) ((d) = (((d) & ~IPMI_BT_HBUSY_MASK) | \
581739d54cSCorey Minyard (!!(v) << IPMI_BT_HBUSY_BIT)))
591739d54cSCorey Minyard
601739d54cSCorey Minyard #define IPMI_BT_BBUSY_MASK (1 << IPMI_BT_BBUSY_BIT)
611739d54cSCorey Minyard #define IPMI_BT_SET_BBUSY(d, v) ((d) = (((d) & ~IPMI_BT_BBUSY_MASK) | \
621739d54cSCorey Minyard (!!(v) << IPMI_BT_BBUSY_BIT)))
631739d54cSCorey Minyard
641739d54cSCorey Minyard
651739d54cSCorey Minyard /* Mask register */
661739d54cSCorey Minyard #define IPMI_BT_B2H_IRQ_EN_BIT 0
671739d54cSCorey Minyard #define IPMI_BT_B2H_IRQ_BIT 1
681739d54cSCorey Minyard
691739d54cSCorey Minyard #define IPMI_BT_B2H_IRQ_EN_MASK (1 << IPMI_BT_B2H_IRQ_EN_BIT)
701739d54cSCorey Minyard #define IPMI_BT_GET_B2H_IRQ_EN(d) (((d) >> IPMI_BT_B2H_IRQ_EN_BIT) & 0x1)
711739d54cSCorey Minyard #define IPMI_BT_SET_B2H_IRQ_EN(d, v) ((d) = (((d) & ~IPMI_BT_B2H_IRQ_EN_MASK) |\
721739d54cSCorey Minyard (!!(v) << IPMI_BT_B2H_IRQ_EN_BIT)))
731739d54cSCorey Minyard
741739d54cSCorey Minyard #define IPMI_BT_B2H_IRQ_MASK (1 << IPMI_BT_B2H_IRQ_BIT)
751739d54cSCorey Minyard #define IPMI_BT_GET_B2H_IRQ(d) (((d) >> IPMI_BT_B2H_IRQ_BIT) & 0x1)
761739d54cSCorey Minyard #define IPMI_BT_SET_B2H_IRQ(d, v) ((d) = (((d) & ~IPMI_BT_B2H_IRQ_MASK) | \
771739d54cSCorey Minyard (!!(v) << IPMI_BT_B2H_IRQ_BIT)))
781739d54cSCorey Minyard
791739d54cSCorey Minyard #define IPMI_CMD_GET_BT_INTF_CAP 0x36
801739d54cSCorey Minyard
ipmi_bt_raise_irq(IPMIBT * ib)811739d54cSCorey Minyard static void ipmi_bt_raise_irq(IPMIBT *ib)
821739d54cSCorey Minyard {
831739d54cSCorey Minyard if (ib->use_irq && ib->irqs_enabled && ib->raise_irq) {
841739d54cSCorey Minyard ib->raise_irq(ib);
851739d54cSCorey Minyard }
861739d54cSCorey Minyard }
871739d54cSCorey Minyard
ipmi_bt_lower_irq(IPMIBT * ib)881739d54cSCorey Minyard static void ipmi_bt_lower_irq(IPMIBT *ib)
891739d54cSCorey Minyard {
901739d54cSCorey Minyard if (ib->lower_irq) {
911739d54cSCorey Minyard ib->lower_irq(ib);
921739d54cSCorey Minyard }
931739d54cSCorey Minyard }
941739d54cSCorey Minyard
ipmi_bt_handle_event(IPMIInterface * ii)951739d54cSCorey Minyard static void ipmi_bt_handle_event(IPMIInterface *ii)
961739d54cSCorey Minyard {
971739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
981739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
991739d54cSCorey Minyard
1001739d54cSCorey Minyard if (ib->inlen < 4) {
1011739d54cSCorey Minyard goto out;
1021739d54cSCorey Minyard }
1031739d54cSCorey Minyard /* Note that overruns are handled by handle_command */
1041739d54cSCorey Minyard if (ib->inmsg[0] != (ib->inlen - 1)) {
1051739d54cSCorey Minyard /* Length mismatch, just ignore. */
1061739d54cSCorey Minyard IPMI_BT_SET_BBUSY(ib->control_reg, 1);
1071739d54cSCorey Minyard ib->inlen = 0;
1081739d54cSCorey Minyard goto out;
1091739d54cSCorey Minyard }
1101739d54cSCorey Minyard if ((ib->inmsg[1] == (IPMI_NETFN_APP << 2)) &&
1111739d54cSCorey Minyard (ib->inmsg[3] == IPMI_CMD_GET_BT_INTF_CAP)) {
1121739d54cSCorey Minyard /* We handle this one ourselves. */
1131739d54cSCorey Minyard ib->outmsg[0] = 9;
1141739d54cSCorey Minyard ib->outmsg[1] = ib->inmsg[1] | 0x04;
1151739d54cSCorey Minyard ib->outmsg[2] = ib->inmsg[2];
1161739d54cSCorey Minyard ib->outmsg[3] = ib->inmsg[3];
1171739d54cSCorey Minyard ib->outmsg[4] = 0;
1181739d54cSCorey Minyard ib->outmsg[5] = 1; /* Only support 1 outstanding request. */
1191739d54cSCorey Minyard if (sizeof(ib->inmsg) > 0xff) { /* Input buffer size */
1201739d54cSCorey Minyard ib->outmsg[6] = 0xff;
1211739d54cSCorey Minyard } else {
1221739d54cSCorey Minyard ib->outmsg[6] = (unsigned char) sizeof(ib->inmsg);
1231739d54cSCorey Minyard }
1241739d54cSCorey Minyard if (sizeof(ib->outmsg) > 0xff) { /* Output buffer size */
1251739d54cSCorey Minyard ib->outmsg[7] = 0xff;
1261739d54cSCorey Minyard } else {
1271739d54cSCorey Minyard ib->outmsg[7] = (unsigned char) sizeof(ib->outmsg);
1281739d54cSCorey Minyard }
1291739d54cSCorey Minyard ib->outmsg[8] = 10; /* Max request to response time */
1301739d54cSCorey Minyard ib->outmsg[9] = 0; /* Don't recommend retries */
1311739d54cSCorey Minyard ib->outlen = 10;
1321739d54cSCorey Minyard IPMI_BT_SET_BBUSY(ib->control_reg, 0);
1331739d54cSCorey Minyard IPMI_BT_SET_B2H_ATN(ib->control_reg, 1);
1341739d54cSCorey Minyard if (!IPMI_BT_GET_B2H_IRQ(ib->mask_reg) &&
1351739d54cSCorey Minyard IPMI_BT_GET_B2H_IRQ_EN(ib->mask_reg)) {
1361739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 1);
1371739d54cSCorey Minyard ipmi_bt_raise_irq(ib);
1381739d54cSCorey Minyard }
1391739d54cSCorey Minyard goto out;
1401739d54cSCorey Minyard }
1411739d54cSCorey Minyard ib->waiting_seq = ib->inmsg[2];
1421739d54cSCorey Minyard ib->inmsg[2] = ib->inmsg[1];
1431739d54cSCorey Minyard {
1441739d54cSCorey Minyard IPMIBmcClass *bk = IPMI_BMC_GET_CLASS(ib->bmc);
1451739d54cSCorey Minyard bk->handle_command(ib->bmc, ib->inmsg + 2, ib->inlen - 2,
1461739d54cSCorey Minyard sizeof(ib->inmsg), ib->waiting_rsp);
1471739d54cSCorey Minyard }
1481739d54cSCorey Minyard out:
1491739d54cSCorey Minyard return;
1501739d54cSCorey Minyard }
1511739d54cSCorey Minyard
ipmi_bt_handle_rsp(IPMIInterface * ii,uint8_t msg_id,unsigned char * rsp,unsigned int rsp_len)1521739d54cSCorey Minyard static void ipmi_bt_handle_rsp(IPMIInterface *ii, uint8_t msg_id,
1531739d54cSCorey Minyard unsigned char *rsp, unsigned int rsp_len)
1541739d54cSCorey Minyard {
1551739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
1561739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
1571739d54cSCorey Minyard
1581739d54cSCorey Minyard if (ib->waiting_rsp == msg_id) {
1591739d54cSCorey Minyard ib->waiting_rsp++;
1601739d54cSCorey Minyard if (rsp_len > (sizeof(ib->outmsg) - 2)) {
1611739d54cSCorey Minyard ib->outmsg[0] = 4;
1621739d54cSCorey Minyard ib->outmsg[1] = rsp[0];
1631739d54cSCorey Minyard ib->outmsg[2] = ib->waiting_seq;
1641739d54cSCorey Minyard ib->outmsg[3] = rsp[1];
1651739d54cSCorey Minyard ib->outmsg[4] = IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES;
1661739d54cSCorey Minyard ib->outlen = 5;
1671739d54cSCorey Minyard } else {
1681739d54cSCorey Minyard ib->outmsg[0] = rsp_len + 1;
1691739d54cSCorey Minyard ib->outmsg[1] = rsp[0];
1701739d54cSCorey Minyard ib->outmsg[2] = ib->waiting_seq;
1711739d54cSCorey Minyard memcpy(ib->outmsg + 3, rsp + 1, rsp_len - 1);
1721739d54cSCorey Minyard ib->outlen = rsp_len + 2;
1731739d54cSCorey Minyard }
1741739d54cSCorey Minyard IPMI_BT_SET_BBUSY(ib->control_reg, 0);
1751739d54cSCorey Minyard IPMI_BT_SET_B2H_ATN(ib->control_reg, 1);
1761739d54cSCorey Minyard if (!IPMI_BT_GET_B2H_IRQ(ib->mask_reg) &&
1771739d54cSCorey Minyard IPMI_BT_GET_B2H_IRQ_EN(ib->mask_reg)) {
1781739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 1);
1791739d54cSCorey Minyard ipmi_bt_raise_irq(ib);
1801739d54cSCorey Minyard }
1811739d54cSCorey Minyard }
1821739d54cSCorey Minyard }
1831739d54cSCorey Minyard
1841739d54cSCorey Minyard
ipmi_bt_ioport_read(void * opaque,hwaddr addr,unsigned size)1851739d54cSCorey Minyard static uint64_t ipmi_bt_ioport_read(void *opaque, hwaddr addr, unsigned size)
1861739d54cSCorey Minyard {
1871739d54cSCorey Minyard IPMIInterface *ii = opaque;
1881739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
1891739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
1901739d54cSCorey Minyard uint32_t ret = 0xff;
1911739d54cSCorey Minyard
19279d29a9dSCorey Minyard switch (addr & ib->size_mask) {
1931739d54cSCorey Minyard case 0:
1941739d54cSCorey Minyard ret = ib->control_reg;
1951739d54cSCorey Minyard break;
1961739d54cSCorey Minyard case 1:
1971739d54cSCorey Minyard if (ib->outpos < ib->outlen) {
1981739d54cSCorey Minyard ret = ib->outmsg[ib->outpos];
1991739d54cSCorey Minyard ib->outpos++;
2001739d54cSCorey Minyard if (ib->outpos == ib->outlen) {
2011739d54cSCorey Minyard ib->outpos = 0;
2021739d54cSCorey Minyard ib->outlen = 0;
2031739d54cSCorey Minyard }
2041739d54cSCorey Minyard } else {
2051739d54cSCorey Minyard ret = 0xff;
2061739d54cSCorey Minyard }
2071739d54cSCorey Minyard break;
2081739d54cSCorey Minyard case 2:
2091739d54cSCorey Minyard ret = ib->mask_reg;
2101739d54cSCorey Minyard break;
21179d29a9dSCorey Minyard default:
21279d29a9dSCorey Minyard ret = 0xff;
21379d29a9dSCorey Minyard break;
2141739d54cSCorey Minyard }
2151739d54cSCorey Minyard return ret;
2161739d54cSCorey Minyard }
2171739d54cSCorey Minyard
ipmi_bt_signal(IPMIBT * ib,IPMIInterface * ii)2181739d54cSCorey Minyard static void ipmi_bt_signal(IPMIBT *ib, IPMIInterface *ii)
2191739d54cSCorey Minyard {
2201739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
2211739d54cSCorey Minyard
2221739d54cSCorey Minyard ib->do_wake = 1;
2231739d54cSCorey Minyard while (ib->do_wake) {
2241739d54cSCorey Minyard ib->do_wake = 0;
2251739d54cSCorey Minyard iic->handle_if_event(ii);
2261739d54cSCorey Minyard }
2271739d54cSCorey Minyard }
2281739d54cSCorey Minyard
ipmi_bt_ioport_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)2291739d54cSCorey Minyard static void ipmi_bt_ioport_write(void *opaque, hwaddr addr, uint64_t val,
2301739d54cSCorey Minyard unsigned size)
2311739d54cSCorey Minyard {
2321739d54cSCorey Minyard IPMIInterface *ii = opaque;
2331739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
2341739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
2351739d54cSCorey Minyard
23679d29a9dSCorey Minyard switch (addr & ib->size_mask) {
2371739d54cSCorey Minyard case 0:
2381739d54cSCorey Minyard if (IPMI_BT_GET_CLR_WR(val)) {
2391739d54cSCorey Minyard ib->inlen = 0;
2401739d54cSCorey Minyard }
2411739d54cSCorey Minyard if (IPMI_BT_GET_CLR_RD(val)) {
2421739d54cSCorey Minyard ib->outpos = 0;
2431739d54cSCorey Minyard }
2441739d54cSCorey Minyard if (IPMI_BT_GET_B2H_ATN(val)) {
2451739d54cSCorey Minyard IPMI_BT_SET_B2H_ATN(ib->control_reg, 0);
2461739d54cSCorey Minyard }
2471739d54cSCorey Minyard if (IPMI_BT_GET_SMS_ATN(val)) {
2481739d54cSCorey Minyard IPMI_BT_SET_SMS_ATN(ib->control_reg, 0);
2491739d54cSCorey Minyard }
2501739d54cSCorey Minyard if (IPMI_BT_GET_HBUSY(val)) {
2511739d54cSCorey Minyard /* Toggle */
2521739d54cSCorey Minyard IPMI_BT_SET_HBUSY(ib->control_reg,
2531739d54cSCorey Minyard !IPMI_BT_GET_HBUSY(ib->control_reg));
2541739d54cSCorey Minyard }
2551739d54cSCorey Minyard if (IPMI_BT_GET_H2B_ATN(val)) {
2561739d54cSCorey Minyard IPMI_BT_SET_BBUSY(ib->control_reg, 1);
2571739d54cSCorey Minyard ipmi_bt_signal(ib, ii);
2581739d54cSCorey Minyard }
2591739d54cSCorey Minyard break;
2601739d54cSCorey Minyard
2611739d54cSCorey Minyard case 1:
2621739d54cSCorey Minyard if (ib->inlen < sizeof(ib->inmsg)) {
2631739d54cSCorey Minyard ib->inmsg[ib->inlen] = val;
2641739d54cSCorey Minyard }
2651739d54cSCorey Minyard ib->inlen++;
2661739d54cSCorey Minyard break;
2671739d54cSCorey Minyard
2681739d54cSCorey Minyard case 2:
2691739d54cSCorey Minyard if (IPMI_BT_GET_B2H_IRQ_EN(val) !=
2701739d54cSCorey Minyard IPMI_BT_GET_B2H_IRQ_EN(ib->mask_reg)) {
2711739d54cSCorey Minyard if (IPMI_BT_GET_B2H_IRQ_EN(val)) {
2721739d54cSCorey Minyard if (IPMI_BT_GET_B2H_ATN(ib->control_reg) ||
2731739d54cSCorey Minyard IPMI_BT_GET_SMS_ATN(ib->control_reg)) {
2741739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 1);
2751739d54cSCorey Minyard ipmi_bt_raise_irq(ib);
2761739d54cSCorey Minyard }
2771739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ_EN(ib->mask_reg, 1);
2781739d54cSCorey Minyard } else {
2791739d54cSCorey Minyard if (IPMI_BT_GET_B2H_IRQ(ib->mask_reg)) {
2801739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 0);
2811739d54cSCorey Minyard ipmi_bt_lower_irq(ib);
2821739d54cSCorey Minyard }
2831739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ_EN(ib->mask_reg, 0);
2841739d54cSCorey Minyard }
2851739d54cSCorey Minyard }
2861739d54cSCorey Minyard if (IPMI_BT_GET_B2H_IRQ(val) && IPMI_BT_GET_B2H_IRQ(ib->mask_reg)) {
2871739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 0);
2881739d54cSCorey Minyard ipmi_bt_lower_irq(ib);
2891739d54cSCorey Minyard }
2901739d54cSCorey Minyard break;
29179d29a9dSCorey Minyard default:
29279d29a9dSCorey Minyard /* Ignore. */
29379d29a9dSCorey Minyard break;
2941739d54cSCorey Minyard }
2951739d54cSCorey Minyard }
2961739d54cSCorey Minyard
2971739d54cSCorey Minyard static const MemoryRegionOps ipmi_bt_io_ops = {
2981739d54cSCorey Minyard .read = ipmi_bt_ioport_read,
2991739d54cSCorey Minyard .write = ipmi_bt_ioport_write,
3001739d54cSCorey Minyard .impl = {
3011739d54cSCorey Minyard .min_access_size = 1,
3021739d54cSCorey Minyard .max_access_size = 1,
3031739d54cSCorey Minyard },
3041739d54cSCorey Minyard .endianness = DEVICE_LITTLE_ENDIAN,
3051739d54cSCorey Minyard };
3061739d54cSCorey Minyard
ipmi_bt_set_atn(IPMIInterface * ii,int val,int irq)3071739d54cSCorey Minyard static void ipmi_bt_set_atn(IPMIInterface *ii, int val, int irq)
3081739d54cSCorey Minyard {
3091739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
3101739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
3111739d54cSCorey Minyard
3121739d54cSCorey Minyard if (!!val == IPMI_BT_GET_SMS_ATN(ib->control_reg)) {
3131739d54cSCorey Minyard return;
3141739d54cSCorey Minyard }
3151739d54cSCorey Minyard
3161739d54cSCorey Minyard IPMI_BT_SET_SMS_ATN(ib->control_reg, val);
3171739d54cSCorey Minyard if (val) {
3181739d54cSCorey Minyard if (irq && !IPMI_BT_GET_B2H_ATN(ib->control_reg) &&
3191739d54cSCorey Minyard IPMI_BT_GET_B2H_IRQ_EN(ib->mask_reg)) {
3201739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 1);
3211739d54cSCorey Minyard ipmi_bt_raise_irq(ib);
3221739d54cSCorey Minyard }
3231739d54cSCorey Minyard } else {
3241739d54cSCorey Minyard if (!IPMI_BT_GET_B2H_ATN(ib->control_reg) &&
3251739d54cSCorey Minyard IPMI_BT_GET_B2H_IRQ(ib->mask_reg)) {
3261739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 0);
3271739d54cSCorey Minyard ipmi_bt_lower_irq(ib);
3281739d54cSCorey Minyard }
3291739d54cSCorey Minyard }
3301739d54cSCorey Minyard }
3311739d54cSCorey Minyard
ipmi_bt_handle_reset(IPMIInterface * ii,bool is_cold)3321739d54cSCorey Minyard static void ipmi_bt_handle_reset(IPMIInterface *ii, bool is_cold)
3331739d54cSCorey Minyard {
3341739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
3351739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
3361739d54cSCorey Minyard
3371739d54cSCorey Minyard if (is_cold) {
3381739d54cSCorey Minyard /* Disable the BT interrupt on reset */
3391739d54cSCorey Minyard if (IPMI_BT_GET_B2H_IRQ(ib->mask_reg)) {
3401739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ(ib->mask_reg, 0);
3411739d54cSCorey Minyard ipmi_bt_lower_irq(ib);
3421739d54cSCorey Minyard }
3431739d54cSCorey Minyard IPMI_BT_SET_B2H_IRQ_EN(ib->mask_reg, 0);
3441739d54cSCorey Minyard }
3451739d54cSCorey Minyard }
3461739d54cSCorey Minyard
ipmi_bt_set_irq_enable(IPMIInterface * ii,int val)3471739d54cSCorey Minyard static void ipmi_bt_set_irq_enable(IPMIInterface *ii, int val)
3481739d54cSCorey Minyard {
3491739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
3501739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
3511739d54cSCorey Minyard
3521739d54cSCorey Minyard ib->irqs_enabled = val;
3531739d54cSCorey Minyard }
3541739d54cSCorey Minyard
ipmi_bt_init(IPMIInterface * ii,unsigned int min_size,Error ** errp)35579d29a9dSCorey Minyard static void ipmi_bt_init(IPMIInterface *ii, unsigned int min_size, Error **errp)
3561739d54cSCorey Minyard {
3571739d54cSCorey Minyard IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
3581739d54cSCorey Minyard IPMIBT *ib = iic->get_backend_data(ii);
3591739d54cSCorey Minyard
36079d29a9dSCorey Minyard if (min_size == 0) {
36179d29a9dSCorey Minyard min_size = 4;
36279d29a9dSCorey Minyard }
36379d29a9dSCorey Minyard ib->size_mask = min_size - 1;
3641739d54cSCorey Minyard ib->io_length = 3;
3651739d54cSCorey Minyard
36679d29a9dSCorey Minyard memory_region_init_io(&ib->io, NULL, &ipmi_bt_io_ops, ii, "ipmi-bt",
36779d29a9dSCorey Minyard min_size);
3681739d54cSCorey Minyard }
3691739d54cSCorey Minyard
ipmi_bt_vmstate_post_load(void * opaque,int version)3701739d54cSCorey Minyard int ipmi_bt_vmstate_post_load(void *opaque, int version)
3711739d54cSCorey Minyard {
3721739d54cSCorey Minyard IPMIBT *ib = opaque;
3731739d54cSCorey Minyard
3741739d54cSCorey Minyard /* Make sure all the values are sane. */
3751739d54cSCorey Minyard if (ib->outpos >= MAX_IPMI_MSG_SIZE || ib->outlen >= MAX_IPMI_MSG_SIZE ||
3761739d54cSCorey Minyard ib->outpos >= ib->outlen) {
3771739d54cSCorey Minyard qemu_log_mask(LOG_GUEST_ERROR,
3781739d54cSCorey Minyard "ipmi:bt: vmstate transfer received bad out values: %d %d\n",
3791739d54cSCorey Minyard ib->outpos, ib->outlen);
3801739d54cSCorey Minyard ib->outpos = 0;
3811739d54cSCorey Minyard ib->outlen = 0;
3821739d54cSCorey Minyard }
3831739d54cSCorey Minyard
3841739d54cSCorey Minyard if (ib->inlen >= MAX_IPMI_MSG_SIZE) {
3851739d54cSCorey Minyard qemu_log_mask(LOG_GUEST_ERROR,
3861739d54cSCorey Minyard "ipmi:bt: vmstate transfer received bad in value: %d\n",
3871739d54cSCorey Minyard ib->inlen);
3881739d54cSCorey Minyard ib->inlen = 0;
3891739d54cSCorey Minyard }
3901739d54cSCorey Minyard
3911739d54cSCorey Minyard return 0;
3921739d54cSCorey Minyard }
3931739d54cSCorey Minyard
3941739d54cSCorey Minyard const VMStateDescription vmstate_IPMIBT = {
3951739d54cSCorey Minyard .name = TYPE_IPMI_INTERFACE_PREFIX "bt",
3961739d54cSCorey Minyard .version_id = 1,
3971739d54cSCorey Minyard .minimum_version_id = 1,
3981739d54cSCorey Minyard .post_load = ipmi_bt_vmstate_post_load,
399*09c6ac6dSRichard Henderson .fields = (const VMStateField[]) {
4001739d54cSCorey Minyard VMSTATE_BOOL(obf_irq_set, IPMIBT),
4011739d54cSCorey Minyard VMSTATE_BOOL(atn_irq_set, IPMIBT),
4021739d54cSCorey Minyard VMSTATE_BOOL(irqs_enabled, IPMIBT),
4031739d54cSCorey Minyard VMSTATE_UINT32(outpos, IPMIBT),
4041739d54cSCorey Minyard VMSTATE_UINT32(outlen, IPMIBT),
4051739d54cSCorey Minyard VMSTATE_UINT8_ARRAY(outmsg, IPMIBT, MAX_IPMI_MSG_SIZE),
4061739d54cSCorey Minyard VMSTATE_UINT32(inlen, IPMIBT),
4071739d54cSCorey Minyard VMSTATE_UINT8_ARRAY(inmsg, IPMIBT, MAX_IPMI_MSG_SIZE),
4081739d54cSCorey Minyard VMSTATE_UINT8(control_reg, IPMIBT),
4091739d54cSCorey Minyard VMSTATE_UINT8(mask_reg, IPMIBT),
4101739d54cSCorey Minyard VMSTATE_UINT8(waiting_rsp, IPMIBT),
4111739d54cSCorey Minyard VMSTATE_UINT8(waiting_seq, IPMIBT),
4121739d54cSCorey Minyard VMSTATE_END_OF_LIST()
4131739d54cSCorey Minyard }
4141739d54cSCorey Minyard };
4151739d54cSCorey Minyard
ipmi_bt_get_fwinfo(struct IPMIBT * ib,IPMIFwInfo * info)4161739d54cSCorey Minyard void ipmi_bt_get_fwinfo(struct IPMIBT *ib, IPMIFwInfo *info)
4171739d54cSCorey Minyard {
4181739d54cSCorey Minyard info->interface_name = "bt";
4191739d54cSCorey Minyard info->interface_type = IPMI_SMBIOS_BT;
4201739d54cSCorey Minyard info->ipmi_spec_major_revision = 2;
4211739d54cSCorey Minyard info->ipmi_spec_minor_revision = 0;
4221739d54cSCorey Minyard info->base_address = ib->io_base;
4231739d54cSCorey Minyard info->register_length = ib->io_length;
4241739d54cSCorey Minyard info->register_spacing = 1;
4251739d54cSCorey Minyard info->memspace = IPMI_MEMSPACE_IO;
4261739d54cSCorey Minyard info->irq_type = IPMI_LEVEL_IRQ;
4271739d54cSCorey Minyard }
4281739d54cSCorey Minyard
ipmi_bt_class_init(IPMIInterfaceClass * iic)4291739d54cSCorey Minyard void ipmi_bt_class_init(IPMIInterfaceClass *iic)
4301739d54cSCorey Minyard {
4311739d54cSCorey Minyard iic->init = ipmi_bt_init;
4321739d54cSCorey Minyard iic->set_atn = ipmi_bt_set_atn;
4331739d54cSCorey Minyard iic->handle_rsp = ipmi_bt_handle_rsp;
4341739d54cSCorey Minyard iic->handle_if_event = ipmi_bt_handle_event;
4351739d54cSCorey Minyard iic->set_irq_enable = ipmi_bt_set_irq_enable;
4361739d54cSCorey Minyard iic->reset = ipmi_bt_handle_reset;
4371739d54cSCorey Minyard }
438