ibmpex.c 15 KB

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