ipmi_bmc_extern.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * IPMI BMC external connection
  3. *
  4. * Copyright (c) 2015 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. /*
  25. * This is designed to connect with OpenIPMI's lanserv serial interface
  26. * using the "VM" connection type. See that for details.
  27. */
  28. #include "qemu/osdep.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/module.h"
  31. #include "qapi/error.h"
  32. #include "qemu/timer.h"
  33. #include "chardev/char-fe.h"
  34. #include "hw/ipmi/ipmi.h"
  35. #include "hw/qdev-properties.h"
  36. #include "migration/vmstate.h"
  37. #define VM_MSG_CHAR 0xA0 /* Marks end of message */
  38. #define VM_CMD_CHAR 0xA1 /* Marks end of a command */
  39. #define VM_ESCAPE_CHAR 0xAA /* Set bit 4 from the next byte to 0 */
  40. #define VM_PROTOCOL_VERSION 1
  41. #define VM_CMD_VERSION 0xff /* A version number byte follows */
  42. #define VM_CMD_NOATTN 0x00
  43. #define VM_CMD_ATTN 0x01
  44. #define VM_CMD_ATTN_IRQ 0x02
  45. #define VM_CMD_POWEROFF 0x03
  46. #define VM_CMD_RESET 0x04
  47. #define VM_CMD_ENABLE_IRQ 0x05 /* Enable/disable the messaging irq */
  48. #define VM_CMD_DISABLE_IRQ 0x06
  49. #define VM_CMD_SEND_NMI 0x07
  50. #define VM_CMD_CAPABILITIES 0x08
  51. #define VM_CAPABILITIES_POWER 0x01
  52. #define VM_CAPABILITIES_RESET 0x02
  53. #define VM_CAPABILITIES_IRQ 0x04
  54. #define VM_CAPABILITIES_NMI 0x08
  55. #define VM_CAPABILITIES_ATTN 0x10
  56. #define VM_CAPABILITIES_GRACEFUL_SHUTDOWN 0x20
  57. #define VM_CMD_GRACEFUL_SHUTDOWN 0x09
  58. #define TYPE_IPMI_BMC_EXTERN "ipmi-bmc-extern"
  59. #define IPMI_BMC_EXTERN(obj) OBJECT_CHECK(IPMIBmcExtern, (obj), \
  60. TYPE_IPMI_BMC_EXTERN)
  61. typedef struct IPMIBmcExtern {
  62. IPMIBmc parent;
  63. CharBackend chr;
  64. bool connected;
  65. unsigned char inbuf[MAX_IPMI_MSG_SIZE + 2];
  66. unsigned int inpos;
  67. bool in_escape;
  68. bool in_too_many;
  69. bool waiting_rsp;
  70. bool sending_cmd;
  71. unsigned char outbuf[(MAX_IPMI_MSG_SIZE + 2) * 2 + 1];
  72. unsigned int outpos;
  73. unsigned int outlen;
  74. struct QEMUTimer *extern_timer;
  75. /* A reset event is pending to be sent upstream. */
  76. bool send_reset;
  77. } IPMIBmcExtern;
  78. static int can_receive(void *opaque);
  79. static void receive(void *opaque, const uint8_t *buf, int size);
  80. static void chr_event(void *opaque, int event);
  81. static unsigned char
  82. ipmb_checksum(const unsigned char *data, int size, unsigned char start)
  83. {
  84. unsigned char csum = start;
  85. for (; size > 0; size--, data++) {
  86. csum += *data;
  87. }
  88. return csum;
  89. }
  90. static void continue_send(IPMIBmcExtern *ibe)
  91. {
  92. int ret;
  93. if (ibe->outlen == 0) {
  94. goto check_reset;
  95. }
  96. send:
  97. ret = qemu_chr_fe_write(&ibe->chr, ibe->outbuf + ibe->outpos,
  98. ibe->outlen - ibe->outpos);
  99. if (ret > 0) {
  100. ibe->outpos += ret;
  101. }
  102. if (ibe->outpos < ibe->outlen) {
  103. /* Not fully transmitted, try again in a 10ms */
  104. timer_mod_ns(ibe->extern_timer,
  105. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 10000000);
  106. } else {
  107. /* Sent */
  108. ibe->outlen = 0;
  109. ibe->outpos = 0;
  110. if (!ibe->sending_cmd) {
  111. ibe->waiting_rsp = true;
  112. } else {
  113. ibe->sending_cmd = false;
  114. }
  115. check_reset:
  116. if (ibe->connected && ibe->send_reset) {
  117. /* Send the reset */
  118. ibe->outbuf[0] = VM_CMD_RESET;
  119. ibe->outbuf[1] = VM_CMD_CHAR;
  120. ibe->outlen = 2;
  121. ibe->outpos = 0;
  122. ibe->send_reset = false;
  123. ibe->sending_cmd = true;
  124. goto send;
  125. }
  126. if (ibe->waiting_rsp) {
  127. /* Make sure we get a response within 4 seconds. */
  128. timer_mod_ns(ibe->extern_timer,
  129. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 4000000000ULL);
  130. }
  131. }
  132. return;
  133. }
  134. static void extern_timeout(void *opaque)
  135. {
  136. IPMIBmcExtern *ibe = opaque;
  137. IPMIInterface *s = ibe->parent.intf;
  138. if (ibe->connected) {
  139. if (ibe->waiting_rsp && (ibe->outlen == 0)) {
  140. IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
  141. /* The message response timed out, return an error. */
  142. ibe->waiting_rsp = false;
  143. ibe->inbuf[1] = ibe->outbuf[1] | 0x04;
  144. ibe->inbuf[2] = ibe->outbuf[2];
  145. ibe->inbuf[3] = IPMI_CC_TIMEOUT;
  146. k->handle_rsp(s, ibe->outbuf[0], ibe->inbuf + 1, 3);
  147. } else {
  148. continue_send(ibe);
  149. }
  150. }
  151. }
  152. static void addchar(IPMIBmcExtern *ibe, unsigned char ch)
  153. {
  154. switch (ch) {
  155. case VM_MSG_CHAR:
  156. case VM_CMD_CHAR:
  157. case VM_ESCAPE_CHAR:
  158. ibe->outbuf[ibe->outlen] = VM_ESCAPE_CHAR;
  159. ibe->outlen++;
  160. ch |= 0x10;
  161. /* fall through */
  162. default:
  163. ibe->outbuf[ibe->outlen] = ch;
  164. ibe->outlen++;
  165. }
  166. }
  167. static void ipmi_bmc_extern_handle_command(IPMIBmc *b,
  168. uint8_t *cmd, unsigned int cmd_len,
  169. unsigned int max_cmd_len,
  170. uint8_t msg_id)
  171. {
  172. IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(b);
  173. IPMIInterface *s = ibe->parent.intf;
  174. uint8_t err = 0, csum;
  175. unsigned int i;
  176. if (ibe->outlen) {
  177. /* We already have a command queued. Shouldn't ever happen. */
  178. error_report("IPMI KCS: Got command when not finished with the"
  179. " previous command");
  180. abort();
  181. }
  182. /* If it's too short or it was truncated, return an error. */
  183. if (cmd_len < 2) {
  184. err = IPMI_CC_REQUEST_DATA_LENGTH_INVALID;
  185. } else if ((cmd_len > max_cmd_len) || (cmd_len > MAX_IPMI_MSG_SIZE)) {
  186. err = IPMI_CC_REQUEST_DATA_TRUNCATED;
  187. } else if (!ibe->connected) {
  188. err = IPMI_CC_BMC_INIT_IN_PROGRESS;
  189. }
  190. if (err) {
  191. IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
  192. unsigned char rsp[3];
  193. rsp[0] = cmd[0] | 0x04;
  194. rsp[1] = cmd[1];
  195. rsp[2] = err;
  196. ibe->waiting_rsp = false;
  197. k->handle_rsp(s, msg_id, rsp, 3);
  198. goto out;
  199. }
  200. addchar(ibe, msg_id);
  201. for (i = 0; i < cmd_len; i++) {
  202. addchar(ibe, cmd[i]);
  203. }
  204. csum = ipmb_checksum(&msg_id, 1, 0);
  205. addchar(ibe, -ipmb_checksum(cmd, cmd_len, csum));
  206. ibe->outbuf[ibe->outlen] = VM_MSG_CHAR;
  207. ibe->outlen++;
  208. /* Start the transmit */
  209. continue_send(ibe);
  210. out:
  211. return;
  212. }
  213. static void handle_hw_op(IPMIBmcExtern *ibe, unsigned char hw_op)
  214. {
  215. IPMIInterface *s = ibe->parent.intf;
  216. IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
  217. switch (hw_op) {
  218. case VM_CMD_VERSION:
  219. /* We only support one version at this time. */
  220. break;
  221. case VM_CMD_NOATTN:
  222. k->set_atn(s, 0, 0);
  223. break;
  224. case VM_CMD_ATTN:
  225. k->set_atn(s, 1, 0);
  226. break;
  227. case VM_CMD_ATTN_IRQ:
  228. k->set_atn(s, 1, 1);
  229. break;
  230. case VM_CMD_POWEROFF:
  231. k->do_hw_op(s, IPMI_POWEROFF_CHASSIS, 0);
  232. break;
  233. case VM_CMD_RESET:
  234. k->do_hw_op(s, IPMI_RESET_CHASSIS, 0);
  235. break;
  236. case VM_CMD_ENABLE_IRQ:
  237. k->set_irq_enable(s, 1);
  238. break;
  239. case VM_CMD_DISABLE_IRQ:
  240. k->set_irq_enable(s, 0);
  241. break;
  242. case VM_CMD_SEND_NMI:
  243. k->do_hw_op(s, IPMI_SEND_NMI, 0);
  244. break;
  245. case VM_CMD_GRACEFUL_SHUTDOWN:
  246. k->do_hw_op(s, IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP, 0);
  247. break;
  248. }
  249. }
  250. static void handle_msg(IPMIBmcExtern *ibe)
  251. {
  252. IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(ibe->parent.intf);
  253. if (ibe->in_escape) {
  254. ipmi_debug("msg escape not ended\n");
  255. return;
  256. }
  257. if (ibe->inpos < 5) {
  258. ipmi_debug("msg too short\n");
  259. return;
  260. }
  261. if (ibe->in_too_many) {
  262. ibe->inbuf[3] = IPMI_CC_REQUEST_DATA_TRUNCATED;
  263. ibe->inpos = 4;
  264. } else if (ipmb_checksum(ibe->inbuf, ibe->inpos, 0) != 0) {
  265. ipmi_debug("msg checksum failure\n");
  266. return;
  267. } else {
  268. ibe->inpos--; /* Remove checkum */
  269. }
  270. timer_del(ibe->extern_timer);
  271. ibe->waiting_rsp = false;
  272. k->handle_rsp(ibe->parent.intf, ibe->inbuf[0], ibe->inbuf + 1, ibe->inpos - 1);
  273. }
  274. static int can_receive(void *opaque)
  275. {
  276. return 1;
  277. }
  278. static void receive(void *opaque, const uint8_t *buf, int size)
  279. {
  280. IPMIBmcExtern *ibe = opaque;
  281. int i;
  282. unsigned char hw_op;
  283. for (i = 0; i < size; i++) {
  284. unsigned char ch = buf[i];
  285. switch (ch) {
  286. case VM_MSG_CHAR:
  287. handle_msg(ibe);
  288. ibe->in_too_many = false;
  289. ibe->inpos = 0;
  290. break;
  291. case VM_CMD_CHAR:
  292. if (ibe->in_too_many) {
  293. ipmi_debug("cmd in too many\n");
  294. ibe->in_too_many = false;
  295. ibe->inpos = 0;
  296. break;
  297. }
  298. if (ibe->in_escape) {
  299. ipmi_debug("cmd in escape\n");
  300. ibe->in_too_many = false;
  301. ibe->inpos = 0;
  302. ibe->in_escape = false;
  303. break;
  304. }
  305. ibe->in_too_many = false;
  306. if (ibe->inpos < 1) {
  307. break;
  308. }
  309. hw_op = ibe->inbuf[0];
  310. ibe->inpos = 0;
  311. goto out_hw_op;
  312. break;
  313. case VM_ESCAPE_CHAR:
  314. ibe->in_escape = true;
  315. break;
  316. default:
  317. if (ibe->in_escape) {
  318. ch &= ~0x10;
  319. ibe->in_escape = false;
  320. }
  321. if (ibe->in_too_many) {
  322. break;
  323. }
  324. if (ibe->inpos >= sizeof(ibe->inbuf)) {
  325. ibe->in_too_many = true;
  326. break;
  327. }
  328. ibe->inbuf[ibe->inpos] = ch;
  329. ibe->inpos++;
  330. break;
  331. }
  332. }
  333. return;
  334. out_hw_op:
  335. handle_hw_op(ibe, hw_op);
  336. }
  337. static void chr_event(void *opaque, int event)
  338. {
  339. IPMIBmcExtern *ibe = opaque;
  340. IPMIInterface *s = ibe->parent.intf;
  341. IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
  342. unsigned char v;
  343. switch (event) {
  344. case CHR_EVENT_OPENED:
  345. ibe->connected = true;
  346. ibe->outpos = 0;
  347. ibe->outlen = 0;
  348. addchar(ibe, VM_CMD_VERSION);
  349. addchar(ibe, VM_PROTOCOL_VERSION);
  350. ibe->outbuf[ibe->outlen] = VM_CMD_CHAR;
  351. ibe->outlen++;
  352. addchar(ibe, VM_CMD_CAPABILITIES);
  353. v = VM_CAPABILITIES_IRQ | VM_CAPABILITIES_ATTN;
  354. if (k->do_hw_op(ibe->parent.intf, IPMI_POWEROFF_CHASSIS, 1) == 0) {
  355. v |= VM_CAPABILITIES_POWER;
  356. }
  357. if (k->do_hw_op(ibe->parent.intf, IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP, 1)
  358. == 0) {
  359. v |= VM_CAPABILITIES_GRACEFUL_SHUTDOWN;
  360. }
  361. if (k->do_hw_op(ibe->parent.intf, IPMI_RESET_CHASSIS, 1) == 0) {
  362. v |= VM_CAPABILITIES_RESET;
  363. }
  364. if (k->do_hw_op(ibe->parent.intf, IPMI_SEND_NMI, 1) == 0) {
  365. v |= VM_CAPABILITIES_NMI;
  366. }
  367. addchar(ibe, v);
  368. ibe->outbuf[ibe->outlen] = VM_CMD_CHAR;
  369. ibe->outlen++;
  370. ibe->sending_cmd = false;
  371. continue_send(ibe);
  372. break;
  373. case CHR_EVENT_CLOSED:
  374. if (!ibe->connected) {
  375. return;
  376. }
  377. ibe->connected = false;
  378. /*
  379. * Don't hang the OS trying to handle the ATN bit, other end will
  380. * resend on a reconnect.
  381. */
  382. k->set_atn(s, 0, 0);
  383. if (ibe->waiting_rsp) {
  384. ibe->waiting_rsp = false;
  385. ibe->inbuf[1] = ibe->outbuf[1] | 0x04;
  386. ibe->inbuf[2] = ibe->outbuf[2];
  387. ibe->inbuf[3] = IPMI_CC_BMC_INIT_IN_PROGRESS;
  388. k->handle_rsp(s, ibe->outbuf[0], ibe->inbuf + 1, 3);
  389. }
  390. break;
  391. }
  392. }
  393. static void ipmi_bmc_extern_handle_reset(IPMIBmc *b)
  394. {
  395. IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(b);
  396. ibe->send_reset = true;
  397. continue_send(ibe);
  398. }
  399. static void ipmi_bmc_extern_realize(DeviceState *dev, Error **errp)
  400. {
  401. IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(dev);
  402. if (!qemu_chr_fe_backend_connected(&ibe->chr)) {
  403. error_setg(errp, "IPMI external bmc requires chardev attribute");
  404. return;
  405. }
  406. qemu_chr_fe_set_handlers(&ibe->chr, can_receive, receive,
  407. chr_event, NULL, ibe, NULL, true);
  408. }
  409. static int ipmi_bmc_extern_post_migrate(void *opaque, int version_id)
  410. {
  411. IPMIBmcExtern *ibe = opaque;
  412. /*
  413. * We don't directly restore waiting_rsp, Instead, we return an
  414. * error on the interface if a response was being waited for.
  415. */
  416. if (ibe->waiting_rsp) {
  417. IPMIInterface *ii = ibe->parent.intf;
  418. IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
  419. ibe->waiting_rsp = false;
  420. ibe->inbuf[1] = ibe->outbuf[1] | 0x04;
  421. ibe->inbuf[2] = ibe->outbuf[2];
  422. ibe->inbuf[3] = IPMI_CC_BMC_INIT_IN_PROGRESS;
  423. iic->handle_rsp(ii, ibe->outbuf[0], ibe->inbuf + 1, 3);
  424. }
  425. return 0;
  426. }
  427. static const VMStateDescription vmstate_ipmi_bmc_extern = {
  428. .name = TYPE_IPMI_BMC_EXTERN,
  429. .version_id = 1,
  430. .minimum_version_id = 1,
  431. .post_load = ipmi_bmc_extern_post_migrate,
  432. .fields = (VMStateField[]) {
  433. VMSTATE_BOOL(send_reset, IPMIBmcExtern),
  434. VMSTATE_BOOL(waiting_rsp, IPMIBmcExtern),
  435. VMSTATE_END_OF_LIST()
  436. }
  437. };
  438. static void ipmi_bmc_extern_init(Object *obj)
  439. {
  440. IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(obj);
  441. ibe->extern_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, extern_timeout, ibe);
  442. vmstate_register(NULL, 0, &vmstate_ipmi_bmc_extern, ibe);
  443. }
  444. static void ipmi_bmc_extern_finalize(Object *obj)
  445. {
  446. IPMIBmcExtern *ibe = IPMI_BMC_EXTERN(obj);
  447. timer_del(ibe->extern_timer);
  448. timer_free(ibe->extern_timer);
  449. }
  450. static Property ipmi_bmc_extern_properties[] = {
  451. DEFINE_PROP_CHR("chardev", IPMIBmcExtern, chr),
  452. DEFINE_PROP_END_OF_LIST(),
  453. };
  454. static void ipmi_bmc_extern_class_init(ObjectClass *oc, void *data)
  455. {
  456. DeviceClass *dc = DEVICE_CLASS(oc);
  457. IPMIBmcClass *bk = IPMI_BMC_CLASS(oc);
  458. bk->handle_command = ipmi_bmc_extern_handle_command;
  459. bk->handle_reset = ipmi_bmc_extern_handle_reset;
  460. dc->hotpluggable = false;
  461. dc->realize = ipmi_bmc_extern_realize;
  462. dc->props = ipmi_bmc_extern_properties;
  463. }
  464. static const TypeInfo ipmi_bmc_extern_type = {
  465. .name = TYPE_IPMI_BMC_EXTERN,
  466. .parent = TYPE_IPMI_BMC,
  467. .instance_size = sizeof(IPMIBmcExtern),
  468. .instance_init = ipmi_bmc_extern_init,
  469. .instance_finalize = ipmi_bmc_extern_finalize,
  470. .class_init = ipmi_bmc_extern_class_init,
  471. };
  472. static void ipmi_bmc_extern_register_types(void)
  473. {
  474. type_register_static(&ipmi_bmc_extern_type);
  475. }
  476. type_init(ipmi_bmc_extern_register_types)