hpm2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2012 Pigeon Point Systems. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * Redistribution of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * Neither the name of Pigeon Point Systems nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * This software is provided "AS IS," without a warranty of any kind.
  20. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  21. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  23. * PIGEON POINT SYSTEMS ("PPS") AND ITS LICENSORS SHALL NOT BE LIABLE
  24. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  25. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  26. * PPS OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  27. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  28. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  29. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  30. * EVEN IF PPS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  31. */
  32. #include <ipmitool/bswap.h>
  33. #include <ipmitool/hpm2.h>
  34. #include <ipmitool/ipmi_intf.h>
  35. #include <ipmitool/log.h>
  36. #include <ipmitool/bswap.h>
  37. /* From src/plugins/ipmi_intf.c: */
  38. void
  39. ipmi_intf_set_max_request_data_size(struct ipmi_intf * intf, uint16_t size);
  40. void
  41. ipmi_intf_set_max_response_data_size(struct ipmi_intf * intf, uint16_t size);
  42. #if HAVE_PRAGMA_PACK
  43. # pragma pack(push, 1)
  44. #endif
  45. /* HPM.x Get Capabilities request */
  46. struct hpmx_cmd_get_capabilities_rq {
  47. uint8_t picmg_id;
  48. uint8_t hpmx_id;
  49. } ATTRIBUTE_PACKING;
  50. /* HPM.2 Get Capabilities response */
  51. struct hpm2_cmd_get_capabilities_rp {
  52. uint8_t picmg_id;
  53. struct hpm2_lan_attach_capabilities caps;
  54. } ATTRIBUTE_PACKING;
  55. #if HAVE_PRAGMA_PACK
  56. # pragma pack(pop)
  57. #endif
  58. /* IPMI Get LAN Configuration Parameters command */
  59. #define IPMI_LAN_GET_CONFIG 0x02
  60. int hpm2_get_capabilities(struct ipmi_intf * intf,
  61. struct hpm2_lan_attach_capabilities * caps)
  62. {
  63. struct ipmi_rq req;
  64. struct ipmi_rs * rsp;
  65. struct hpmx_cmd_get_capabilities_rq rq;
  66. printf("---> hpm2_get_capabilities\n");
  67. /* reset result */
  68. memset(caps, 0, sizeof(struct hpm2_lan_attach_capabilities));
  69. /* prepare request */
  70. rq.picmg_id = 0;
  71. rq.hpmx_id = 2;
  72. /* prepare request */
  73. memset(&req, 0, sizeof(req));
  74. req.msg.netfn = IPMI_NETFN_PICMG;
  75. req.msg.cmd = HPM2_GET_LAN_ATTACH_CAPABILITIES;
  76. req.msg.data = (uint8_t *)&rq;
  77. req.msg.data_len = sizeof(rq);
  78. /* send */
  79. rsp = intf->sendrecv(intf, &req);
  80. if (!rsp) {
  81. lprintf(LOG_NOTICE, "Error sending request.");
  82. return -1;
  83. }
  84. if (rsp->ccode == 0xC1) {
  85. lprintf(LOG_DEBUG, "IPM Controller is not HPM.2 compatible");
  86. return rsp->ccode;
  87. } else if (rsp->ccode) {
  88. lprintf(LOG_NOTICE, "Get HPM.x Capabilities request failed,"
  89. " compcode = %x", rsp->ccode);
  90. return rsp->ccode;
  91. }
  92. /* check response length */
  93. if (rsp->data_len < 2 || rsp->data_len > 10) {
  94. lprintf(LOG_NOTICE, "Bad response length, len=%d", rsp->data_len);
  95. return -1;
  96. }
  97. /* check HPM.x identifier */
  98. if (rsp->data[1] != 2) {
  99. lprintf(LOG_NOTICE, "Bad HPM.x ID, id=%d", rsp->data[1]);
  100. return rsp->ccode;
  101. }
  102. /*
  103. * this hardly can happen, since completion code is already checked.
  104. * but check for safety
  105. */
  106. if (rsp->data_len < 4) {
  107. lprintf(LOG_NOTICE, "Bad response length, len=%d", rsp->data_len);
  108. return -1;
  109. }
  110. /* copy HPM.2 capabilities */
  111. memcpy(caps, rsp->data + 2, rsp->data_len - 2);
  112. #if WORDS_BIGENDIAN
  113. /* swap bytes to convert from little-endian format */
  114. caps->lan_channel_mask = BSWAP_16(caps->lan_channel_mask);
  115. #endif
  116. /* check HPM.2 revision */
  117. if (caps->hpm2_revision_id == 0) {
  118. lprintf(LOG_NOTICE, "Bad HPM.2 revision, rev=%d",
  119. caps->hpm2_revision_id);
  120. return -1;
  121. }
  122. if (!caps->lan_channel_mask) {
  123. return -1;
  124. }
  125. /* check response length */
  126. if (rsp->data_len < 8) {
  127. lprintf(LOG_NOTICE, "Bad response length, len=%d", rsp->data_len);
  128. return -1;
  129. }
  130. /* check HPM.2 LAN parameters start */
  131. if (caps->hpm2_lan_params_start < 0xC0) {
  132. lprintf(LOG_NOTICE, "Bad HPM.2 LAN params start, start=%x",
  133. caps->hpm2_lan_params_start);
  134. return -1;
  135. }
  136. /* check HPM.2 LAN parameters revision */
  137. if (caps->hpm2_lan_params_rev != HPM2_LAN_PARAMS_REV) {
  138. lprintf(LOG_NOTICE, "Bad HPM.2 LAN params revision, rev=%d",
  139. caps->hpm2_lan_params_rev);
  140. return -1;
  141. }
  142. /* check for HPM.2 SOL extension */
  143. if (!(caps->hpm2_caps & HPM2_CAPS_SOL_EXTENSION)) {
  144. /* no further checks */
  145. return 0;
  146. }
  147. /* check response length */
  148. if (rsp->data_len < 10) {
  149. lprintf(LOG_NOTICE, "Bad response length, len=%d", rsp->data_len);
  150. return -1;
  151. }
  152. /* check HPM.2 SOL parameters start */
  153. if (caps->hpm2_sol_params_start < 0xC0) {
  154. lprintf(LOG_NOTICE, "Bad HPM.2 SOL params start, start=%x",
  155. caps->hpm2_sol_params_start);
  156. return -1;
  157. }
  158. /* check HPM.2 SOL parameters revision */
  159. if (caps->hpm2_sol_params_rev != HPM2_SOL_PARAMS_REV) {
  160. lprintf(LOG_NOTICE, "Bad HPM.2 SOL params revision, rev=%d",
  161. caps->hpm2_sol_params_rev);
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. int hpm2_get_lan_channel_capabilities(struct ipmi_intf * intf,
  167. uint8_t hpm2_lan_params_start,
  168. struct hpm2_lan_channel_capabilities * caps)
  169. {
  170. struct ipmi_rq req;
  171. struct ipmi_rs * rsp;
  172. uint8_t rq[4];
  173. /* reset result */
  174. memset(caps, 0, sizeof(struct hpm2_lan_channel_capabilities));
  175. /* prepare request */
  176. memset(&req, 0, sizeof(req));
  177. req.msg.netfn = IPMI_NETFN_TRANSPORT;
  178. req.msg.cmd = IPMI_LAN_GET_CONFIG;
  179. req.msg.data = (uint8_t *)&rq;
  180. req.msg.data_len = sizeof(rq);
  181. /* prepare request data */
  182. rq[0] = 0xE; /* sending channel */
  183. rq[1] = hpm2_lan_params_start; /* HPM.2 Channel Caps */
  184. rq[2] = rq[3] = 0;
  185. /* send */
  186. rsp = intf->sendrecv(intf, &req);
  187. if (!rsp) {
  188. lprintf(LOG_NOTICE, "Error sending request.");
  189. return -1;
  190. }
  191. if (rsp->ccode == 0x80) {
  192. lprintf(LOG_DEBUG, "HPM.2 Channel Caps parameter is not supported");
  193. return rsp->ccode;
  194. } else if (rsp->ccode) {
  195. lprintf(LOG_NOTICE, "Get LAN Configuration Parameters request failed,"
  196. " compcode = %x", rsp->ccode);
  197. return rsp->ccode;
  198. }
  199. /* check response length */
  200. if (rsp->data_len != sizeof (struct hpm2_lan_channel_capabilities) + 1) {
  201. lprintf(LOG_NOTICE, "Bad response length, len=%d", rsp->data_len);
  202. return -1;
  203. }
  204. /* check parameter revision */
  205. if (rsp->data[0] !=
  206. LAN_PARAM_REV(HPM2_LAN_PARAMS_REV, HPM2_LAN_PARAMS_REV)) {
  207. lprintf(LOG_NOTICE, "Bad HPM.2 LAN parameter revision, rev=%d",
  208. rsp->data[0]);
  209. return -1;
  210. }
  211. /* copy parameter data */
  212. memcpy(caps, &rsp->data[1], sizeof (struct hpm2_lan_channel_capabilities));
  213. #if WORDS_BIGENDIAN
  214. /* swap bytes to convert from little-endian format */
  215. caps->max_inbound_pld_size = BSWAP_16(caps->max_inbound_pld_size);
  216. caps->max_outbound_pld_size = BSWAP_16(caps->max_outbound_pld_size);
  217. #endif
  218. return 0;
  219. }
  220. int hpm2_detect_max_payload_size(struct ipmi_intf * intf)
  221. {
  222. struct hpm2_lan_attach_capabilities attach_caps;
  223. struct hpm2_lan_channel_capabilities channel_caps;
  224. int err;
  225. /* query HPM.2 support */
  226. err = hpm2_get_capabilities(intf, &attach_caps);
  227. /* check if HPM.2 is supported */
  228. if (err != 0 || !attach_caps.lan_channel_mask) {
  229. return err;
  230. }
  231. /* query channel capabilities */
  232. err = hpm2_get_lan_channel_capabilities(intf,
  233. attach_caps.hpm2_lan_params_start, &channel_caps);
  234. /* check if succeeded */
  235. if (err != 0) {
  236. return err;
  237. }
  238. /* update request and response sizes */
  239. ipmi_intf_set_max_request_data_size(intf,
  240. channel_caps.max_inbound_pld_size - 7);
  241. ipmi_intf_set_max_response_data_size(intf,
  242. channel_caps.max_outbound_pld_size - 8);
  243. /* print debug info */
  244. lprintf(LOG_DEBUG, "Set maximum request size to %d\n"
  245. "Set maximum response size to %d",
  246. intf->max_request_data_size, intf->max_response_data_size);
  247. return 0;
  248. }