2
0

smbus_ipmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * QEMU IPMI SMBus (SSIF) emulation
  3. *
  4. * Copyright (c) 2015,2016 Corey Minyard, MontaVista Software, LLC
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "migration/vmstate.h"
  26. #include "hw/i2c/smbus_slave.h"
  27. #include "qapi/error.h"
  28. #include "qemu/error-report.h"
  29. #include "hw/ipmi/ipmi.h"
  30. #define TYPE_SMBUS_IPMI "smbus-ipmi"
  31. #define SMBUS_IPMI(obj) OBJECT_CHECK(SMBusIPMIDevice, (obj), TYPE_SMBUS_IPMI)
  32. #define SSIF_IPMI_REQUEST 2
  33. #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
  34. #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
  35. #define SSIF_IPMI_MULTI_PART_REQUEST_END 8
  36. #define SSIF_IPMI_RESPONSE 3
  37. #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
  38. #define SSIF_IPMI_MULTI_PART_RETRY 0xa
  39. #define MAX_SSIF_IPMI_MSG_SIZE 255
  40. #define MAX_SSIF_IPMI_MSG_CHUNK 32
  41. #define IPMI_GET_SYS_INTF_CAP_CMD 0x57
  42. typedef struct SMBusIPMIDevice {
  43. SMBusDevice parent;
  44. IPMIBmc *bmc;
  45. uint8_t outmsg[MAX_SSIF_IPMI_MSG_SIZE];
  46. uint32_t outlen;
  47. uint32_t currblk;
  48. /* Holds the SMBUS message currently being sent to the host. */
  49. uint8_t outbuf[MAX_SSIF_IPMI_MSG_CHUNK + 1]; /* len + message. */
  50. uint32_t outpos;
  51. uint8_t inmsg[MAX_SSIF_IPMI_MSG_SIZE];
  52. uint32_t inlen;
  53. /*
  54. * This is a response number that we send with the command to make
  55. * sure that the response matches the command.
  56. */
  57. uint8_t waiting_rsp;
  58. uint32_t uuid;
  59. } SMBusIPMIDevice;
  60. static void smbus_ipmi_handle_event(IPMIInterface *ii)
  61. {
  62. /* No interrupts, so nothing to do here. */
  63. }
  64. static void smbus_ipmi_handle_rsp(IPMIInterface *ii, uint8_t msg_id,
  65. unsigned char *rsp, unsigned int rsp_len)
  66. {
  67. SMBusIPMIDevice *sid = SMBUS_IPMI(ii);
  68. if (sid->waiting_rsp == msg_id) {
  69. sid->waiting_rsp++;
  70. if (rsp_len > MAX_SSIF_IPMI_MSG_SIZE) {
  71. rsp[2] = IPMI_CC_REQUEST_DATA_TRUNCATED;
  72. rsp_len = MAX_SSIF_IPMI_MSG_SIZE;
  73. }
  74. memcpy(sid->outmsg, rsp, rsp_len);
  75. sid->outlen = rsp_len;
  76. sid->outpos = 0;
  77. sid->currblk = 0;
  78. }
  79. }
  80. static void smbus_ipmi_set_atn(IPMIInterface *ii, int val, int irq)
  81. {
  82. /* This is where PEC would go. */
  83. }
  84. static void smbus_ipmi_set_irq_enable(IPMIInterface *ii, int val)
  85. {
  86. }
  87. static void smbus_ipmi_send_msg(SMBusIPMIDevice *sid)
  88. {
  89. uint8_t *msg = sid->inmsg;
  90. uint32_t len = sid->inlen;
  91. IPMIBmcClass *bk = IPMI_BMC_GET_CLASS(sid->bmc);
  92. sid->outlen = 0;
  93. sid->outpos = 0;
  94. sid->currblk = 0;
  95. if (msg[0] == (IPMI_NETFN_APP << 2) && msg[1] == IPMI_GET_SYS_INTF_CAP_CMD)
  96. {
  97. /* We handle this ourself. */
  98. sid->outmsg[0] = (IPMI_NETFN_APP + 1) << 2;
  99. sid->outmsg[1] = msg[1];
  100. if (len < 3) {
  101. sid->outmsg[2] = IPMI_CC_REQUEST_DATA_LENGTH_INVALID;
  102. sid->outlen = 3;
  103. } else if ((msg[2] & 0x0f) != 0) {
  104. sid->outmsg[2] = IPMI_CC_INVALID_DATA_FIELD;
  105. sid->outlen = 3;
  106. } else {
  107. sid->outmsg[2] = 0;
  108. sid->outmsg[3] = 0;
  109. sid->outmsg[4] = (2 << 6); /* Multi-part supported. */
  110. sid->outmsg[5] = MAX_SSIF_IPMI_MSG_SIZE;
  111. sid->outmsg[6] = MAX_SSIF_IPMI_MSG_SIZE;
  112. sid->outlen = 7;
  113. }
  114. return;
  115. }
  116. bk->handle_command(sid->bmc, sid->inmsg, sid->inlen, sizeof(sid->inmsg),
  117. sid->waiting_rsp);
  118. }
  119. static uint8_t ipmi_receive_byte(SMBusDevice *dev)
  120. {
  121. SMBusIPMIDevice *sid = SMBUS_IPMI(dev);
  122. if (sid->outpos >= sizeof(sid->outbuf)) {
  123. return 0xff;
  124. }
  125. return sid->outbuf[sid->outpos++];
  126. }
  127. static int ipmi_load_readbuf(SMBusIPMIDevice *sid)
  128. {
  129. unsigned int block = sid->currblk, pos, len;
  130. if (sid->outlen == 0) {
  131. return -1;
  132. }
  133. if (sid->outlen <= 32) {
  134. if (block != 0) {
  135. return -1;
  136. }
  137. sid->outbuf[0] = sid->outlen;
  138. memcpy(sid->outbuf + 1, sid->outmsg, sid->outlen);
  139. sid->outpos = 0;
  140. return 0;
  141. }
  142. if (block == 0) {
  143. sid->outbuf[0] = 32;
  144. sid->outbuf[1] = 0;
  145. sid->outbuf[2] = 1;
  146. memcpy(sid->outbuf + 3, sid->outmsg, 30);
  147. sid->outpos = 0;
  148. return 0;
  149. }
  150. /*
  151. * Calculate the position in outmsg. 30 for the first block, 31
  152. * for the rest of the blocks.
  153. */
  154. pos = 30 + (block - 1) * 31;
  155. if (pos >= sid->outlen) {
  156. return -1;
  157. }
  158. len = sid->outlen - pos;
  159. if (len > 31) {
  160. /* More chunks after this. */
  161. len = 31;
  162. /* Blocks start at 0 for the first middle transaction. */
  163. sid->outbuf[1] = block - 1;
  164. } else {
  165. sid->outbuf[1] = 0xff; /* End of message marker. */
  166. }
  167. sid->outbuf[0] = len + 1;
  168. memcpy(sid->outbuf + 2, sid->outmsg + pos, len);
  169. sid->outpos = 0;
  170. return 0;
  171. }
  172. static int ipmi_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len)
  173. {
  174. SMBusIPMIDevice *sid = SMBUS_IPMI(dev);
  175. bool send = false;
  176. uint8_t cmd;
  177. int ret = 0;
  178. /* length is guaranteed to be >= 1. */
  179. cmd = *buf++;
  180. len--;
  181. /* Handle read request, which don't have any data in the write part. */
  182. switch (cmd) {
  183. case SSIF_IPMI_RESPONSE:
  184. sid->currblk = 0;
  185. ret = ipmi_load_readbuf(sid);
  186. break;
  187. case SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE:
  188. sid->currblk++;
  189. ret = ipmi_load_readbuf(sid);
  190. break;
  191. case SSIF_IPMI_MULTI_PART_RETRY:
  192. if (len >= 1) {
  193. sid->currblk = buf[0];
  194. ret = ipmi_load_readbuf(sid);
  195. } else {
  196. ret = -1;
  197. }
  198. break;
  199. default:
  200. break;
  201. }
  202. /* This should be a message write, make the length is there and correct. */
  203. if (len >= 1) {
  204. if (*buf != len - 1 || *buf > MAX_SSIF_IPMI_MSG_CHUNK) {
  205. return -1; /* Bogus message */
  206. }
  207. buf++;
  208. len--;
  209. }
  210. switch (cmd) {
  211. case SSIF_IPMI_REQUEST:
  212. send = true;
  213. /* FALLTHRU */
  214. case SSIF_IPMI_MULTI_PART_REQUEST_START:
  215. if (len < 2) {
  216. return -1; /* Bogus. */
  217. }
  218. memcpy(sid->inmsg, buf, len);
  219. sid->inlen = len;
  220. break;
  221. case SSIF_IPMI_MULTI_PART_REQUEST_END:
  222. send = true;
  223. /* FALLTHRU */
  224. case SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE:
  225. if (!sid->inlen) {
  226. return -1; /* Bogus. */
  227. }
  228. if (sid->inlen + len > MAX_SSIF_IPMI_MSG_SIZE) {
  229. sid->inlen = 0; /* Discard the message. */
  230. return -1; /* Bogus. */
  231. }
  232. if (len < 32) {
  233. /*
  234. * Special hack, a multi-part middle that is less than 32 bytes
  235. * marks the end of a message. The specification is fairly
  236. * confusing, so some systems to this, even sending a zero
  237. * length end message to mark the end.
  238. */
  239. send = true;
  240. }
  241. memcpy(sid->inmsg + sid->inlen, buf, len);
  242. sid->inlen += len;
  243. break;
  244. }
  245. if (send && sid->inlen) {
  246. smbus_ipmi_send_msg(sid);
  247. }
  248. return ret;
  249. }
  250. static const VMStateDescription vmstate_smbus_ipmi = {
  251. .name = TYPE_SMBUS_IPMI,
  252. .version_id = 1,
  253. .minimum_version_id = 1,
  254. .fields = (VMStateField[]) {
  255. VMSTATE_SMBUS_DEVICE(parent, SMBusIPMIDevice),
  256. VMSTATE_UINT8(waiting_rsp, SMBusIPMIDevice),
  257. VMSTATE_UINT32(outlen, SMBusIPMIDevice),
  258. VMSTATE_UINT32(currblk, SMBusIPMIDevice),
  259. VMSTATE_UINT8_ARRAY(outmsg, SMBusIPMIDevice, MAX_SSIF_IPMI_MSG_SIZE),
  260. VMSTATE_UINT32(outpos, SMBusIPMIDevice),
  261. VMSTATE_UINT8_ARRAY(outbuf, SMBusIPMIDevice,
  262. MAX_SSIF_IPMI_MSG_CHUNK + 1),
  263. VMSTATE_UINT32(inlen, SMBusIPMIDevice),
  264. VMSTATE_UINT8_ARRAY(inmsg, SMBusIPMIDevice, MAX_SSIF_IPMI_MSG_SIZE),
  265. VMSTATE_END_OF_LIST()
  266. }
  267. };
  268. static void smbus_ipmi_realize(DeviceState *dev, Error **errp)
  269. {
  270. SMBusIPMIDevice *sid = SMBUS_IPMI(dev);
  271. IPMIInterface *ii = IPMI_INTERFACE(dev);
  272. if (!sid->bmc) {
  273. error_setg(errp, "IPMI device requires a bmc attribute to be set");
  274. return;
  275. }
  276. sid->uuid = ipmi_next_uuid();
  277. sid->bmc->intf = ii;
  278. }
  279. static void smbus_ipmi_init(Object *obj)
  280. {
  281. SMBusIPMIDevice *sid = SMBUS_IPMI(obj);
  282. ipmi_bmc_find_and_link(OBJECT(obj), (Object **) &sid->bmc);
  283. }
  284. static void smbus_ipmi_get_fwinfo(struct IPMIInterface *ii, IPMIFwInfo *info)
  285. {
  286. SMBusIPMIDevice *sid = SMBUS_IPMI(ii);
  287. info->interface_name = "smbus";
  288. info->interface_type = IPMI_SMBIOS_SSIF;
  289. info->ipmi_spec_major_revision = 2;
  290. info->ipmi_spec_minor_revision = 0;
  291. info->i2c_slave_address = sid->bmc->slave_addr;
  292. info->base_address = sid->parent.i2c.address;
  293. info->memspace = IPMI_MEMSPACE_SMBUS;
  294. info->register_spacing = 1;
  295. info->uuid = sid->uuid;
  296. }
  297. static void smbus_ipmi_class_init(ObjectClass *oc, void *data)
  298. {
  299. DeviceClass *dc = DEVICE_CLASS(oc);
  300. IPMIInterfaceClass *iic = IPMI_INTERFACE_CLASS(oc);
  301. SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(oc);
  302. sc->receive_byte = ipmi_receive_byte;
  303. sc->write_data = ipmi_write_data;
  304. dc->vmsd = &vmstate_smbus_ipmi;
  305. dc->realize = smbus_ipmi_realize;
  306. iic->set_atn = smbus_ipmi_set_atn;
  307. iic->handle_rsp = smbus_ipmi_handle_rsp;
  308. iic->handle_if_event = smbus_ipmi_handle_event;
  309. iic->set_irq_enable = smbus_ipmi_set_irq_enable;
  310. iic->get_fwinfo = smbus_ipmi_get_fwinfo;
  311. }
  312. static const TypeInfo smbus_ipmi_info = {
  313. .name = TYPE_SMBUS_IPMI,
  314. .parent = TYPE_SMBUS_DEVICE,
  315. .instance_size = sizeof(SMBusIPMIDevice),
  316. .instance_init = smbus_ipmi_init,
  317. .class_init = smbus_ipmi_class_init,
  318. .interfaces = (InterfaceInfo[]) {
  319. { TYPE_IPMI_INTERFACE },
  320. { }
  321. }
  322. };
  323. static void smbus_ipmi_register_types(void)
  324. {
  325. type_register_static(&smbus_ipmi_info);
  326. }
  327. type_init(smbus_ipmi_register_types)