ibmpex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * A hwmon driver for the IBM PowerExecutive temperature/power sensors
  3. * Copyright (C) 2007 IBM
  4. *
  5. * Author: Darrick J. Wong <djwong@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ipmi.h>
  22. #include <linux/module.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/mutex.h>
  27. #include <linux/slab.h>
  28. #define REFRESH_INTERVAL (2 * HZ)
  29. #define DRVNAME "ibmpex"
  30. #define PEX_GET_VERSION 1
  31. #define PEX_GET_SENSOR_COUNT 2
  32. #define PEX_GET_SENSOR_NAME 3
  33. #define PEX_RESET_HIGH_LOW 4
  34. #define PEX_GET_SENSOR_DATA 6
  35. #define PEX_NET_FUNCTION 0x3A
  36. #define PEX_COMMAND 0x3C
  37. static inline u16 extract_value(const char *data, int offset)
  38. {
  39. return be16_to_cpup((__be16 *)&data[offset]);
  40. }
  41. #define TEMP_SENSOR 1
  42. #define POWER_SENSOR 2
  43. #define PEX_SENSOR_TYPE_LEN 3
  44. static u8 const power_sensor_sig[] = {0x70, 0x77, 0x72};
  45. static u8 const temp_sensor_sig[] = {0x74, 0x65, 0x6D};
  46. #define PEX_MULT_LEN 2
  47. static u8 const watt_sensor_sig[] = {0x41, 0x43};
  48. #define PEX_NUM_SENSOR_FUNCS 3
  49. static char const * const power_sensor_name_templates[] = {
  50. "%s%d_average",
  51. "%s%d_average_lowest",
  52. "%s%d_average_highest"
  53. };
  54. static char const * const temp_sensor_name_templates[] = {
  55. "%s%d_input",
  56. "%s%d_input_lowest",
  57. "%s%d_input_highest"
  58. };
  59. static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
  60. static void ibmpex_register_bmc(int iface, struct device *dev);
  61. static void ibmpex_bmc_gone(int iface);
  62. struct ibmpex_sensor_data {
  63. int in_use;
  64. s16 values[PEX_NUM_SENSOR_FUNCS];
  65. int multiplier;
  66. struct sensor_device_attribute_2 attr[PEX_NUM_SENSOR_FUNCS];
  67. };
  68. struct ibmpex_bmc_data {
  69. struct list_head list;
  70. struct device *hwmon_dev;
  71. struct device *bmc_device;
  72. struct mutex lock;
  73. char valid;
  74. unsigned long last_updated; /* In jiffies */
  75. struct ipmi_addr address;
  76. struct completion read_complete;
  77. ipmi_user_t user;
  78. int interface;
  79. struct kernel_ipmi_msg tx_message;
  80. unsigned char tx_msg_data[IPMI_MAX_MSG_LENGTH];
  81. long tx_msgid;
  82. unsigned char rx_msg_data[IPMI_MAX_MSG_LENGTH];
  83. unsigned long rx_msg_len;
  84. unsigned char rx_result;
  85. int rx_recv_type;
  86. unsigned char sensor_major;
  87. unsigned char sensor_minor;
  88. unsigned char num_sensors;
  89. struct ibmpex_sensor_data *sensors;
  90. };
  91. struct ibmpex_driver_data {
  92. struct list_head bmc_data;
  93. struct ipmi_smi_watcher bmc_events;
  94. struct ipmi_user_hndl ipmi_hndlrs;
  95. };
  96. static struct ibmpex_driver_data driver_data = {
  97. .bmc_data = LIST_HEAD_INIT(driver_data.bmc_data),
  98. .bmc_events = {
  99. .owner = THIS_MODULE,
  100. .new_smi = ibmpex_register_bmc,
  101. .smi_gone = ibmpex_bmc_gone,
  102. },
  103. .ipmi_hndlrs = {
  104. .ipmi_recv_hndl = ibmpex_msg_handler,
  105. },
  106. };
  107. static int ibmpex_send_message(struct ibmpex_bmc_data *data)
  108. {
  109. int err;
  110. err = ipmi_validate_addr(&data->address, sizeof(data->address));
  111. if (err)
  112. goto out;
  113. data->tx_msgid++;
  114. err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
  115. &data->tx_message, data, 0, 0, 0);
  116. if (err)
  117. goto out1;
  118. return 0;
  119. out1:
  120. dev_err(data->bmc_device, "request_settime=%x\n", err);
  121. return err;
  122. out:
  123. dev_err(data->bmc_device, "validate_addr=%x\n", err);
  124. return err;
  125. }
  126. static int ibmpex_ver_check(struct ibmpex_bmc_data *data)
  127. {
  128. data->tx_msg_data[0] = PEX_GET_VERSION;
  129. data->tx_message.data_len = 1;
  130. ibmpex_send_message(data);
  131. wait_for_completion(&data->read_complete);
  132. if (data->rx_result || data->rx_msg_len != 6)
  133. return -ENOENT;
  134. data->sensor_major = data->rx_msg_data[0];
  135. data->sensor_minor = data->rx_msg_data[1];
  136. dev_info(data->bmc_device, "Found BMC with sensor interface "
  137. "v%d.%d %d-%02d-%02d on interface %d\n",
  138. data->sensor_major,
  139. data->sensor_minor,
  140. extract_value(data->rx_msg_data, 2),
  141. data->rx_msg_data[4],
  142. data->rx_msg_data[5],
  143. data->interface);
  144. return 0;
  145. }
  146. static int ibmpex_query_sensor_count(struct ibmpex_bmc_data *data)
  147. {
  148. data->tx_msg_data[0] = PEX_GET_SENSOR_COUNT;
  149. data->tx_message.data_len = 1;
  150. ibmpex_send_message(data);
  151. wait_for_completion(&data->read_complete);
  152. if (data->rx_result || data->rx_msg_len != 1)
  153. return -ENOENT;
  154. return data->rx_msg_data[0];
  155. }
  156. static int ibmpex_query_sensor_name(struct ibmpex_bmc_data *data, int sensor)
  157. {
  158. data->tx_msg_data[0] = PEX_GET_SENSOR_NAME;
  159. data->tx_msg_data[1] = sensor;
  160. data->tx_message.data_len = 2;
  161. ibmpex_send_message(data);
  162. wait_for_completion(&data->read_complete);
  163. if (data->rx_result || data->rx_msg_len < 1)
  164. return -ENOENT;
  165. return 0;
  166. }
  167. static int ibmpex_query_sensor_data(struct ibmpex_bmc_data *data, int sensor)
  168. {
  169. data->tx_msg_data[0] = PEX_GET_SENSOR_DATA;
  170. data->tx_msg_data[1] = sensor;
  171. data->tx_message.data_len = 2;
  172. ibmpex_send_message(data);
  173. wait_for_completion(&data->read_complete);
  174. if (data->rx_result || data->rx_msg_len < 26) {
  175. dev_err(data->bmc_device, "Error reading sensor %d.\n",
  176. sensor);
  177. return -ENOENT;
  178. }
  179. return 0;
  180. }
  181. static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data *data)
  182. {
  183. data->tx_msg_data[0] = PEX_RESET_HIGH_LOW;
  184. data->tx_message.data_len = 1;
  185. ibmpex_send_message(data);
  186. wait_for_completion(&data->read_complete);
  187. return 0;
  188. }
  189. static void ibmpex_update_device(struct ibmpex_bmc_data *data)
  190. {
  191. int i, err;
  192. mutex_lock(&data->lock);
  193. if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
  194. data->valid)
  195. goto out;
  196. for (i = 0; i < data->num_sensors; i++) {
  197. if (!data->sensors[i].in_use)
  198. continue;
  199. err = ibmpex_query_sensor_data(data, i);
  200. if (err)
  201. continue;
  202. data->sensors[i].values[0] =
  203. extract_value(data->rx_msg_data, 16);
  204. data->sensors[i].values[1] =
  205. extract_value(data->rx_msg_data, 18);
  206. data->sensors[i].values[2] =
  207. extract_value(data->rx_msg_data, 20);
  208. }
  209. data->last_updated = jiffies;
  210. data->valid = 1;
  211. out:
  212. mutex_unlock(&data->lock);
  213. }
  214. static struct ibmpex_bmc_data *get_bmc_data(int iface)
  215. {
  216. struct ibmpex_bmc_data *p, *next;
  217. list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
  218. if (p->interface == iface)
  219. return p;
  220. return NULL;
  221. }
  222. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  223. char *buf)
  224. {
  225. return sprintf(buf, "%s\n", DRVNAME);
  226. }
  227. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  228. static ssize_t ibmpex_show_sensor(struct device *dev,
  229. struct device_attribute *devattr,
  230. char *buf)
  231. {
  232. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  233. struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
  234. int mult = data->sensors[attr->index].multiplier;
  235. ibmpex_update_device(data);
  236. return sprintf(buf, "%d\n",
  237. data->sensors[attr->index].values[attr->nr] * mult);
  238. }
  239. static ssize_t ibmpex_reset_high_low(struct device *dev,
  240. struct device_attribute *devattr,
  241. const char *buf,
  242. size_t count)
  243. {
  244. struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
  245. ibmpex_reset_high_low_data(data);
  246. return count;
  247. }
  248. static SENSOR_DEVICE_ATTR(reset_high_low, S_IWUSR, NULL,
  249. ibmpex_reset_high_low, 0);
  250. static int is_power_sensor(const char *sensor_id, int len)
  251. {
  252. if (len < PEX_SENSOR_TYPE_LEN)
  253. return 0;
  254. if (!memcmp(sensor_id, power_sensor_sig, PEX_SENSOR_TYPE_LEN))
  255. return 1;
  256. return 0;
  257. }
  258. static int is_temp_sensor(const char *sensor_id, int len)
  259. {
  260. if (len < PEX_SENSOR_TYPE_LEN)
  261. return 0;
  262. if (!memcmp(sensor_id, temp_sensor_sig, PEX_SENSOR_TYPE_LEN))
  263. return 1;
  264. return 0;
  265. }
  266. static int power_sensor_multiplier(struct ibmpex_bmc_data *data,
  267. const char *sensor_id, int len)
  268. {
  269. int i;
  270. if (data->sensor_major == 2)
  271. return 1000000;
  272. for (i = PEX_SENSOR_TYPE_LEN; i < len - 1; i++)
  273. if (!memcmp(&sensor_id[i], watt_sensor_sig, PEX_MULT_LEN))
  274. return 1000000;
  275. return 100000;
  276. }
  277. static int create_sensor(struct ibmpex_bmc_data *data, int type,
  278. int counter, int sensor, int func)
  279. {
  280. int err;
  281. char *n;
  282. n = kmalloc(32, GFP_KERNEL);
  283. if (!n)
  284. return -ENOMEM;
  285. if (type == TEMP_SENSOR)
  286. sprintf(n, temp_sensor_name_templates[func], "temp", counter);
  287. else if (type == POWER_SENSOR)
  288. sprintf(n, power_sensor_name_templates[func], "power", counter);
  289. sysfs_attr_init(&data->sensors[sensor].attr[func].dev_attr.attr);
  290. data->sensors[sensor].attr[func].dev_attr.attr.name = n;
  291. data->sensors[sensor].attr[func].dev_attr.attr.mode = S_IRUGO;
  292. data->sensors[sensor].attr[func].dev_attr.show = ibmpex_show_sensor;
  293. data->sensors[sensor].attr[func].index = sensor;
  294. data->sensors[sensor].attr[func].nr = func;
  295. err = device_create_file(data->bmc_device,
  296. &data->sensors[sensor].attr[func].dev_attr);
  297. if (err) {
  298. data->sensors[sensor].attr[func].dev_attr.attr.name = NULL;
  299. kfree(n);
  300. return err;
  301. }
  302. return 0;
  303. }
  304. static int ibmpex_find_sensors(struct ibmpex_bmc_data *data)
  305. {
  306. int i, j, err;
  307. int sensor_type;
  308. int sensor_counter;
  309. int num_power = 0;
  310. int num_temp = 0;
  311. err = ibmpex_query_sensor_count(data);
  312. if (err <= 0)
  313. return -ENOENT;
  314. data->num_sensors = err;
  315. data->sensors = kzalloc(data->num_sensors * sizeof(*data->sensors),
  316. GFP_KERNEL);
  317. if (!data->sensors)
  318. return -ENOMEM;
  319. for (i = 0; i < data->num_sensors; i++) {
  320. err = ibmpex_query_sensor_name(data, i);
  321. if (err)
  322. continue;
  323. if (is_power_sensor(data->rx_msg_data, data->rx_msg_len)) {
  324. sensor_type = POWER_SENSOR;
  325. num_power++;
  326. sensor_counter = num_power;
  327. data->sensors[i].multiplier =
  328. power_sensor_multiplier(data,
  329. data->rx_msg_data,
  330. data->rx_msg_len);
  331. } else if (is_temp_sensor(data->rx_msg_data,
  332. data->rx_msg_len)) {
  333. sensor_type = TEMP_SENSOR;
  334. num_temp++;
  335. sensor_counter = num_temp;
  336. data->sensors[i].multiplier = 1000;
  337. } else
  338. continue;
  339. data->sensors[i].in_use = 1;
  340. /* Create attributes */
  341. for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
  342. err = create_sensor(data, sensor_type, sensor_counter,
  343. i, j);
  344. if (err)
  345. goto exit_remove;
  346. }
  347. }
  348. err = device_create_file(data->bmc_device,
  349. &sensor_dev_attr_reset_high_low.dev_attr);
  350. if (err)
  351. goto exit_remove;
  352. err = device_create_file(data->bmc_device,
  353. &sensor_dev_attr_name.dev_attr);
  354. if (err)
  355. goto exit_remove;
  356. return 0;
  357. exit_remove:
  358. device_remove_file(data->bmc_device,
  359. &sensor_dev_attr_reset_high_low.dev_attr);
  360. device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
  361. for (i = 0; i < data->num_sensors; i++)
  362. for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
  363. if (!data->sensors[i].attr[j].dev_attr.attr.name)
  364. continue;
  365. device_remove_file(data->bmc_device,
  366. &data->sensors[i].attr[j].dev_attr);
  367. kfree(data->sensors[i].attr[j].dev_attr.attr.name);
  368. }
  369. kfree(data->sensors);
  370. return err;
  371. }
  372. static void ibmpex_register_bmc(int iface, struct device *dev)
  373. {
  374. struct ibmpex_bmc_data *data;
  375. int err;
  376. data = kzalloc(sizeof(*data), GFP_KERNEL);
  377. if (!data) {
  378. dev_err(dev, "Insufficient memory for BMC interface.\n");
  379. return;
  380. }
  381. data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  382. data->address.channel = IPMI_BMC_CHANNEL;
  383. data->address.data[0] = 0;
  384. data->interface = iface;
  385. data->bmc_device = dev;
  386. /* Create IPMI messaging interface user */
  387. err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
  388. data, &data->user);
  389. if (err < 0) {
  390. dev_err(dev, "Unable to register user with IPMI "
  391. "interface %d\n", data->interface);
  392. goto out;
  393. }
  394. mutex_init(&data->lock);
  395. /* Initialize message */
  396. data->tx_msgid = 0;
  397. init_completion(&data->read_complete);
  398. data->tx_message.netfn = PEX_NET_FUNCTION;
  399. data->tx_message.cmd = PEX_COMMAND;
  400. data->tx_message.data = data->tx_msg_data;
  401. /* Does this BMC support PowerExecutive? */
  402. err = ibmpex_ver_check(data);
  403. if (err)
  404. goto out_user;
  405. /* Register the BMC as a HWMON class device */
  406. data->hwmon_dev = hwmon_device_register(data->bmc_device);
  407. if (IS_ERR(data->hwmon_dev)) {
  408. dev_err(data->bmc_device, "Unable to register hwmon "
  409. "device for IPMI interface %d\n",
  410. data->interface);
  411. goto out_user;
  412. }
  413. /* finally add the new bmc data to the bmc data list */
  414. dev_set_drvdata(dev, data);
  415. list_add_tail(&data->list, &driver_data.bmc_data);
  416. /* Now go find all the sensors */
  417. err = ibmpex_find_sensors(data);
  418. if (err) {
  419. dev_err(data->bmc_device, "Error %d finding sensors\n", err);
  420. goto out_register;
  421. }
  422. return;
  423. out_register:
  424. hwmon_device_unregister(data->hwmon_dev);
  425. out_user:
  426. ipmi_destroy_user(data->user);
  427. out:
  428. kfree(data);
  429. }
  430. static void ibmpex_bmc_delete(struct ibmpex_bmc_data *data)
  431. {
  432. int i, j;
  433. device_remove_file(data->bmc_device,
  434. &sensor_dev_attr_reset_high_low.dev_attr);
  435. device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
  436. for (i = 0; i < data->num_sensors; i++)
  437. for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
  438. if (!data->sensors[i].attr[j].dev_attr.attr.name)
  439. continue;
  440. device_remove_file(data->bmc_device,
  441. &data->sensors[i].attr[j].dev_attr);
  442. kfree(data->sensors[i].attr[j].dev_attr.attr.name);
  443. }
  444. list_del(&data->list);
  445. dev_set_drvdata(data->bmc_device, NULL);
  446. hwmon_device_unregister(data->hwmon_dev);
  447. ipmi_destroy_user(data->user);
  448. kfree(data->sensors);
  449. kfree(data);
  450. }
  451. static void ibmpex_bmc_gone(int iface)
  452. {
  453. struct ibmpex_bmc_data *data = get_bmc_data(iface);
  454. if (!data)
  455. return;
  456. ibmpex_bmc_delete(data);
  457. }
  458. static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
  459. {
  460. struct ibmpex_bmc_data *data = (struct ibmpex_bmc_data *)user_msg_data;
  461. if (msg->msgid != data->tx_msgid) {
  462. dev_err(data->bmc_device, "Mismatch between received msgid "
  463. "(%02x) and transmitted msgid (%02x)!\n",
  464. (int)msg->msgid,
  465. (int)data->tx_msgid);
  466. ipmi_free_recv_msg(msg);
  467. return;
  468. }
  469. data->rx_recv_type = msg->recv_type;
  470. if (msg->msg.data_len > 0)
  471. data->rx_result = msg->msg.data[0];
  472. else
  473. data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
  474. if (msg->msg.data_len > 1) {
  475. data->rx_msg_len = msg->msg.data_len - 1;
  476. memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
  477. } else
  478. data->rx_msg_len = 0;
  479. ipmi_free_recv_msg(msg);
  480. complete(&data->read_complete);
  481. }
  482. static int __init ibmpex_init(void)
  483. {
  484. return ipmi_smi_watcher_register(&driver_data.bmc_events);
  485. }
  486. static void __exit ibmpex_exit(void)
  487. {
  488. struct ibmpex_bmc_data *p, *next;
  489. ipmi_smi_watcher_unregister(&driver_data.bmc_events);
  490. list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
  491. ibmpex_bmc_delete(p);
  492. }
  493. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  494. MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
  495. MODULE_LICENSE("GPL");
  496. module_init(ibmpex_init);
  497. module_exit(ibmpex_exit);
  498. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
  499. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
  500. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
  501. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
  502. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");