ibmaem.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
  3. * temperature/power/energy sensors and capping functionality.
  4. * Copyright (C) 2008 IBM
  5. *
  6. * Author: Darrick J. Wong <djwong@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/ipmi.h>
  24. #include <linux/module.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/mutex.h>
  29. #include <linux/kdev_t.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/idr.h>
  32. #include <linux/slab.h>
  33. #include <linux/sched.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/math64.h>
  36. #include <linux/time.h>
  37. #define REFRESH_INTERVAL (HZ)
  38. #define IPMI_TIMEOUT (30 * HZ)
  39. #define DRVNAME "aem"
  40. #define AEM_NETFN 0x2E
  41. #define AEM_FIND_FW_CMD 0x80
  42. #define AEM_ELEMENT_CMD 0x81
  43. #define AEM_FW_INSTANCE_CMD 0x82
  44. #define AEM_READ_ELEMENT_CFG 0x80
  45. #define AEM_READ_BUFFER 0x81
  46. #define AEM_READ_REGISTER 0x82
  47. #define AEM_WRITE_REGISTER 0x83
  48. #define AEM_SET_REG_MASK 0x84
  49. #define AEM_CLEAR_REG_MASK 0x85
  50. #define AEM_READ_ELEMENT_CFG2 0x86
  51. #define AEM_CONTROL_ELEMENT 0
  52. #define AEM_ENERGY_ELEMENT 1
  53. #define AEM_CLOCK_ELEMENT 4
  54. #define AEM_POWER_CAP_ELEMENT 7
  55. #define AEM_EXHAUST_ELEMENT 9
  56. #define AEM_POWER_ELEMENT 10
  57. #define AEM_MODULE_TYPE_ID 0x0001
  58. #define AEM2_NUM_ENERGY_REGS 2
  59. #define AEM2_NUM_PCAP_REGS 6
  60. #define AEM2_NUM_TEMP_REGS 2
  61. #define AEM2_NUM_SENSORS 14
  62. #define AEM1_NUM_ENERGY_REGS 1
  63. #define AEM1_NUM_SENSORS 3
  64. /* AEM 2.x has more energy registers */
  65. #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
  66. /* AEM 2.x needs more sensor files */
  67. #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
  68. #define POWER_CAP 0
  69. #define POWER_CAP_MAX_HOTPLUG 1
  70. #define POWER_CAP_MAX 2
  71. #define POWER_CAP_MIN_WARNING 3
  72. #define POWER_CAP_MIN 4
  73. #define POWER_AUX 5
  74. #define AEM_DEFAULT_POWER_INTERVAL 1000
  75. #define AEM_MIN_POWER_INTERVAL 200
  76. #define UJ_PER_MJ 1000L
  77. static DEFINE_IDR(aem_idr);
  78. static DEFINE_SPINLOCK(aem_idr_lock);
  79. static struct platform_driver aem_driver = {
  80. .driver = {
  81. .name = DRVNAME,
  82. .bus = &platform_bus_type,
  83. }
  84. };
  85. struct aem_ipmi_data {
  86. struct completion read_complete;
  87. struct ipmi_addr address;
  88. ipmi_user_t user;
  89. int interface;
  90. struct kernel_ipmi_msg tx_message;
  91. long tx_msgid;
  92. void *rx_msg_data;
  93. unsigned short rx_msg_len;
  94. unsigned char rx_result;
  95. int rx_recv_type;
  96. struct device *bmc_device;
  97. };
  98. struct aem_ro_sensor_template {
  99. char *label;
  100. ssize_t (*show)(struct device *dev,
  101. struct device_attribute *devattr,
  102. char *buf);
  103. int index;
  104. };
  105. struct aem_rw_sensor_template {
  106. char *label;
  107. ssize_t (*show)(struct device *dev,
  108. struct device_attribute *devattr,
  109. char *buf);
  110. ssize_t (*set)(struct device *dev,
  111. struct device_attribute *devattr,
  112. const char *buf, size_t count);
  113. int index;
  114. };
  115. struct aem_data {
  116. struct list_head list;
  117. struct device *hwmon_dev;
  118. struct platform_device *pdev;
  119. struct mutex lock;
  120. char valid;
  121. unsigned long last_updated; /* In jiffies */
  122. u8 ver_major;
  123. u8 ver_minor;
  124. u8 module_handle;
  125. int id;
  126. struct aem_ipmi_data ipmi;
  127. /* Function to update sensors */
  128. void (*update)(struct aem_data *data);
  129. /*
  130. * AEM 1.x sensors:
  131. * Available sensors:
  132. * Energy meter
  133. * Power meter
  134. *
  135. * AEM 2.x sensors:
  136. * Two energy meters
  137. * Two power meters
  138. * Two temperature sensors
  139. * Six power cap registers
  140. */
  141. /* sysfs attrs */
  142. struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
  143. /* energy use in mJ */
  144. u64 energy[AEM_NUM_ENERGY_REGS];
  145. /* power sampling interval in ms */
  146. unsigned long power_period[AEM_NUM_ENERGY_REGS];
  147. /* Everything past here is for AEM2 only */
  148. /* power caps in dW */
  149. u16 pcap[AEM2_NUM_PCAP_REGS];
  150. /* exhaust temperature in C */
  151. u8 temp[AEM2_NUM_TEMP_REGS];
  152. };
  153. /* Data structures returned by the AEM firmware */
  154. struct aem_iana_id {
  155. u8 bytes[3];
  156. };
  157. static struct aem_iana_id system_x_id = {
  158. .bytes = {0x4D, 0x4F, 0x00}
  159. };
  160. /* These are used to find AEM1 instances */
  161. struct aem_find_firmware_req {
  162. struct aem_iana_id id;
  163. u8 rsvd;
  164. __be16 index;
  165. __be16 module_type_id;
  166. } __packed;
  167. struct aem_find_firmware_resp {
  168. struct aem_iana_id id;
  169. u8 num_instances;
  170. } __packed;
  171. /* These are used to find AEM2 instances */
  172. struct aem_find_instance_req {
  173. struct aem_iana_id id;
  174. u8 instance_number;
  175. __be16 module_type_id;
  176. } __packed;
  177. struct aem_find_instance_resp {
  178. struct aem_iana_id id;
  179. u8 num_instances;
  180. u8 major;
  181. u8 minor;
  182. u8 module_handle;
  183. u16 record_id;
  184. } __packed;
  185. /* These are used to query sensors */
  186. struct aem_read_sensor_req {
  187. struct aem_iana_id id;
  188. u8 module_handle;
  189. u8 element;
  190. u8 subcommand;
  191. u8 reg;
  192. u8 rx_buf_size;
  193. } __packed;
  194. struct aem_read_sensor_resp {
  195. struct aem_iana_id id;
  196. u8 bytes[0];
  197. } __packed;
  198. /* Data structures to talk to the IPMI layer */
  199. struct aem_driver_data {
  200. struct list_head aem_devices;
  201. struct ipmi_smi_watcher bmc_events;
  202. struct ipmi_user_hndl ipmi_hndlrs;
  203. };
  204. static void aem_register_bmc(int iface, struct device *dev);
  205. static void aem_bmc_gone(int iface);
  206. static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
  207. static void aem_remove_sensors(struct aem_data *data);
  208. static int aem_init_aem1(struct aem_ipmi_data *probe);
  209. static int aem_init_aem2(struct aem_ipmi_data *probe);
  210. static int aem1_find_sensors(struct aem_data *data);
  211. static int aem2_find_sensors(struct aem_data *data);
  212. static void update_aem1_sensors(struct aem_data *data);
  213. static void update_aem2_sensors(struct aem_data *data);
  214. static struct aem_driver_data driver_data = {
  215. .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
  216. .bmc_events = {
  217. .owner = THIS_MODULE,
  218. .new_smi = aem_register_bmc,
  219. .smi_gone = aem_bmc_gone,
  220. },
  221. .ipmi_hndlrs = {
  222. .ipmi_recv_hndl = aem_msg_handler,
  223. },
  224. };
  225. /* Functions to talk to the IPMI layer */
  226. /* Initialize IPMI address, message buffers and user data */
  227. static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
  228. struct device *bmc)
  229. {
  230. int err;
  231. init_completion(&data->read_complete);
  232. data->bmc_device = bmc;
  233. /* Initialize IPMI address */
  234. data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  235. data->address.channel = IPMI_BMC_CHANNEL;
  236. data->address.data[0] = 0;
  237. data->interface = iface;
  238. /* Initialize message buffers */
  239. data->tx_msgid = 0;
  240. data->tx_message.netfn = AEM_NETFN;
  241. /* Create IPMI messaging interface user */
  242. err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
  243. data, &data->user);
  244. if (err < 0) {
  245. dev_err(bmc, "Unable to register user with IPMI "
  246. "interface %d\n", data->interface);
  247. return -EACCES;
  248. }
  249. return 0;
  250. }
  251. /* Send an IPMI command */
  252. static int aem_send_message(struct aem_ipmi_data *data)
  253. {
  254. int err;
  255. err = ipmi_validate_addr(&data->address, sizeof(data->address));
  256. if (err)
  257. goto out;
  258. data->tx_msgid++;
  259. err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
  260. &data->tx_message, data, 0, 0, 0);
  261. if (err)
  262. goto out1;
  263. return 0;
  264. out1:
  265. dev_err(data->bmc_device, "request_settime=%x\n", err);
  266. return err;
  267. out:
  268. dev_err(data->bmc_device, "validate_addr=%x\n", err);
  269. return err;
  270. }
  271. /* Dispatch IPMI messages to callers */
  272. static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
  273. {
  274. unsigned short rx_len;
  275. struct aem_ipmi_data *data = user_msg_data;
  276. if (msg->msgid != data->tx_msgid) {
  277. dev_err(data->bmc_device, "Mismatch between received msgid "
  278. "(%02x) and transmitted msgid (%02x)!\n",
  279. (int)msg->msgid,
  280. (int)data->tx_msgid);
  281. ipmi_free_recv_msg(msg);
  282. return;
  283. }
  284. data->rx_recv_type = msg->recv_type;
  285. if (msg->msg.data_len > 0)
  286. data->rx_result = msg->msg.data[0];
  287. else
  288. data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
  289. if (msg->msg.data_len > 1) {
  290. rx_len = msg->msg.data_len - 1;
  291. if (data->rx_msg_len < rx_len)
  292. rx_len = data->rx_msg_len;
  293. data->rx_msg_len = rx_len;
  294. memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
  295. } else
  296. data->rx_msg_len = 0;
  297. ipmi_free_recv_msg(msg);
  298. complete(&data->read_complete);
  299. }
  300. /* ID functions */
  301. /* Obtain an id */
  302. static int aem_idr_get(int *id)
  303. {
  304. int i, err;
  305. again:
  306. if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
  307. return -ENOMEM;
  308. spin_lock(&aem_idr_lock);
  309. err = idr_get_new(&aem_idr, NULL, &i);
  310. spin_unlock(&aem_idr_lock);
  311. if (unlikely(err == -EAGAIN))
  312. goto again;
  313. else if (unlikely(err))
  314. return err;
  315. *id = i & MAX_ID_MASK;
  316. return 0;
  317. }
  318. /* Release an object ID */
  319. static void aem_idr_put(int id)
  320. {
  321. spin_lock(&aem_idr_lock);
  322. idr_remove(&aem_idr, id);
  323. spin_unlock(&aem_idr_lock);
  324. }
  325. /* Sensor support functions */
  326. /* Read a sensor value */
  327. static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
  328. void *buf, size_t size)
  329. {
  330. int rs_size, res;
  331. struct aem_read_sensor_req rs_req;
  332. struct aem_read_sensor_resp *rs_resp;
  333. struct aem_ipmi_data *ipmi = &data->ipmi;
  334. /* AEM registers are 1, 2, 4 or 8 bytes */
  335. switch (size) {
  336. case 1:
  337. case 2:
  338. case 4:
  339. case 8:
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. rs_req.id = system_x_id;
  345. rs_req.module_handle = data->module_handle;
  346. rs_req.element = elt;
  347. rs_req.subcommand = AEM_READ_REGISTER;
  348. rs_req.reg = reg;
  349. rs_req.rx_buf_size = size;
  350. ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
  351. ipmi->tx_message.data = (char *)&rs_req;
  352. ipmi->tx_message.data_len = sizeof(rs_req);
  353. rs_size = sizeof(*rs_resp) + size;
  354. rs_resp = kzalloc(rs_size, GFP_KERNEL);
  355. if (!rs_resp)
  356. return -ENOMEM;
  357. ipmi->rx_msg_data = rs_resp;
  358. ipmi->rx_msg_len = rs_size;
  359. aem_send_message(ipmi);
  360. res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
  361. if (!res) {
  362. res = -ETIMEDOUT;
  363. goto out;
  364. }
  365. if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
  366. memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
  367. res = -ENOENT;
  368. goto out;
  369. }
  370. switch (size) {
  371. case 1: {
  372. u8 *x = buf;
  373. *x = rs_resp->bytes[0];
  374. break;
  375. }
  376. case 2: {
  377. u16 *x = buf;
  378. *x = be16_to_cpup((__be16 *)rs_resp->bytes);
  379. break;
  380. }
  381. case 4: {
  382. u32 *x = buf;
  383. *x = be32_to_cpup((__be32 *)rs_resp->bytes);
  384. break;
  385. }
  386. case 8: {
  387. u64 *x = buf;
  388. *x = be64_to_cpup((__be64 *)rs_resp->bytes);
  389. break;
  390. }
  391. }
  392. res = 0;
  393. out:
  394. kfree(rs_resp);
  395. return res;
  396. }
  397. /* Update AEM energy registers */
  398. static void update_aem_energy_one(struct aem_data *data, int which)
  399. {
  400. aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
  401. &data->energy[which], 8);
  402. }
  403. static void update_aem_energy(struct aem_data *data)
  404. {
  405. update_aem_energy_one(data, 0);
  406. if (data->ver_major < 2)
  407. return;
  408. update_aem_energy_one(data, 1);
  409. }
  410. /* Update all AEM1 sensors */
  411. static void update_aem1_sensors(struct aem_data *data)
  412. {
  413. mutex_lock(&data->lock);
  414. if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
  415. data->valid)
  416. goto out;
  417. update_aem_energy(data);
  418. out:
  419. mutex_unlock(&data->lock);
  420. }
  421. /* Update all AEM2 sensors */
  422. static void update_aem2_sensors(struct aem_data *data)
  423. {
  424. int i;
  425. mutex_lock(&data->lock);
  426. if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
  427. data->valid)
  428. goto out;
  429. update_aem_energy(data);
  430. aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
  431. aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
  432. for (i = POWER_CAP; i <= POWER_AUX; i++)
  433. aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
  434. &data->pcap[i], 2);
  435. out:
  436. mutex_unlock(&data->lock);
  437. }
  438. /* Delete an AEM instance */
  439. static void aem_delete(struct aem_data *data)
  440. {
  441. list_del(&data->list);
  442. aem_remove_sensors(data);
  443. hwmon_device_unregister(data->hwmon_dev);
  444. ipmi_destroy_user(data->ipmi.user);
  445. platform_set_drvdata(data->pdev, NULL);
  446. platform_device_unregister(data->pdev);
  447. aem_idr_put(data->id);
  448. kfree(data);
  449. }
  450. /* Probe functions for AEM1 devices */
  451. /* Retrieve version and module handle for an AEM1 instance */
  452. static int aem_find_aem1_count(struct aem_ipmi_data *data)
  453. {
  454. int res;
  455. struct aem_find_firmware_req ff_req;
  456. struct aem_find_firmware_resp ff_resp;
  457. ff_req.id = system_x_id;
  458. ff_req.index = 0;
  459. ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
  460. data->tx_message.cmd = AEM_FIND_FW_CMD;
  461. data->tx_message.data = (char *)&ff_req;
  462. data->tx_message.data_len = sizeof(ff_req);
  463. data->rx_msg_data = &ff_resp;
  464. data->rx_msg_len = sizeof(ff_resp);
  465. aem_send_message(data);
  466. res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
  467. if (!res)
  468. return -ETIMEDOUT;
  469. if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
  470. memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
  471. return -ENOENT;
  472. return ff_resp.num_instances;
  473. }
  474. /* Find and initialize one AEM1 instance */
  475. static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
  476. {
  477. struct aem_data *data;
  478. int i;
  479. int res = -ENOMEM;
  480. data = kzalloc(sizeof(*data), GFP_KERNEL);
  481. if (!data)
  482. return res;
  483. mutex_init(&data->lock);
  484. /* Copy instance data */
  485. data->ver_major = 1;
  486. data->ver_minor = 0;
  487. data->module_handle = module_handle;
  488. for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
  489. data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
  490. /* Create sub-device for this fw instance */
  491. if (aem_idr_get(&data->id))
  492. goto id_err;
  493. data->pdev = platform_device_alloc(DRVNAME, data->id);
  494. if (!data->pdev)
  495. goto dev_err;
  496. data->pdev->dev.driver = &aem_driver.driver;
  497. res = platform_device_add(data->pdev);
  498. if (res)
  499. goto ipmi_err;
  500. platform_set_drvdata(data->pdev, data);
  501. /* Set up IPMI interface */
  502. if (aem_init_ipmi_data(&data->ipmi, probe->interface,
  503. probe->bmc_device))
  504. goto ipmi_err;
  505. /* Register with hwmon */
  506. data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
  507. if (IS_ERR(data->hwmon_dev)) {
  508. dev_err(&data->pdev->dev, "Unable to register hwmon "
  509. "device for IPMI interface %d\n",
  510. probe->interface);
  511. goto hwmon_reg_err;
  512. }
  513. data->update = update_aem1_sensors;
  514. /* Find sensors */
  515. if (aem1_find_sensors(data))
  516. goto sensor_err;
  517. /* Add to our list of AEM devices */
  518. list_add_tail(&data->list, &driver_data.aem_devices);
  519. dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
  520. data->ver_major, data->ver_minor,
  521. data->module_handle);
  522. return 0;
  523. sensor_err:
  524. hwmon_device_unregister(data->hwmon_dev);
  525. hwmon_reg_err:
  526. ipmi_destroy_user(data->ipmi.user);
  527. ipmi_err:
  528. platform_set_drvdata(data->pdev, NULL);
  529. platform_device_unregister(data->pdev);
  530. dev_err:
  531. aem_idr_put(data->id);
  532. id_err:
  533. kfree(data);
  534. return res;
  535. }
  536. /* Find and initialize all AEM1 instances */
  537. static int aem_init_aem1(struct aem_ipmi_data *probe)
  538. {
  539. int num, i, err;
  540. num = aem_find_aem1_count(probe);
  541. for (i = 0; i < num; i++) {
  542. err = aem_init_aem1_inst(probe, i);
  543. if (err) {
  544. dev_err(probe->bmc_device,
  545. "Error %d initializing AEM1 0x%X\n",
  546. err, i);
  547. return err;
  548. }
  549. }
  550. return 0;
  551. }
  552. /* Probe functions for AEM2 devices */
  553. /* Retrieve version and module handle for an AEM2 instance */
  554. static int aem_find_aem2(struct aem_ipmi_data *data,
  555. struct aem_find_instance_resp *fi_resp,
  556. int instance_num)
  557. {
  558. int res;
  559. struct aem_find_instance_req fi_req;
  560. fi_req.id = system_x_id;
  561. fi_req.instance_number = instance_num;
  562. fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
  563. data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
  564. data->tx_message.data = (char *)&fi_req;
  565. data->tx_message.data_len = sizeof(fi_req);
  566. data->rx_msg_data = fi_resp;
  567. data->rx_msg_len = sizeof(*fi_resp);
  568. aem_send_message(data);
  569. res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
  570. if (!res)
  571. return -ETIMEDOUT;
  572. if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
  573. memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
  574. fi_resp->num_instances <= instance_num)
  575. return -ENOENT;
  576. return 0;
  577. }
  578. /* Find and initialize one AEM2 instance */
  579. static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
  580. struct aem_find_instance_resp *fi_resp)
  581. {
  582. struct aem_data *data;
  583. int i;
  584. int res = -ENOMEM;
  585. data = kzalloc(sizeof(*data), GFP_KERNEL);
  586. if (!data)
  587. return res;
  588. mutex_init(&data->lock);
  589. /* Copy instance data */
  590. data->ver_major = fi_resp->major;
  591. data->ver_minor = fi_resp->minor;
  592. data->module_handle = fi_resp->module_handle;
  593. for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
  594. data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
  595. /* Create sub-device for this fw instance */
  596. if (aem_idr_get(&data->id))
  597. goto id_err;
  598. data->pdev = platform_device_alloc(DRVNAME, data->id);
  599. if (!data->pdev)
  600. goto dev_err;
  601. data->pdev->dev.driver = &aem_driver.driver;
  602. res = platform_device_add(data->pdev);
  603. if (res)
  604. goto ipmi_err;
  605. platform_set_drvdata(data->pdev, data);
  606. /* Set up IPMI interface */
  607. if (aem_init_ipmi_data(&data->ipmi, probe->interface,
  608. probe->bmc_device))
  609. goto ipmi_err;
  610. /* Register with hwmon */
  611. data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
  612. if (IS_ERR(data->hwmon_dev)) {
  613. dev_err(&data->pdev->dev, "Unable to register hwmon "
  614. "device for IPMI interface %d\n",
  615. probe->interface);
  616. goto hwmon_reg_err;
  617. }
  618. data->update = update_aem2_sensors;
  619. /* Find sensors */
  620. if (aem2_find_sensors(data))
  621. goto sensor_err;
  622. /* Add to our list of AEM devices */
  623. list_add_tail(&data->list, &driver_data.aem_devices);
  624. dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
  625. data->ver_major, data->ver_minor,
  626. data->module_handle);
  627. return 0;
  628. sensor_err:
  629. hwmon_device_unregister(data->hwmon_dev);
  630. hwmon_reg_err:
  631. ipmi_destroy_user(data->ipmi.user);
  632. ipmi_err:
  633. platform_set_drvdata(data->pdev, NULL);
  634. platform_device_unregister(data->pdev);
  635. dev_err:
  636. aem_idr_put(data->id);
  637. id_err:
  638. kfree(data);
  639. return res;
  640. }
  641. /* Find and initialize all AEM2 instances */
  642. static int aem_init_aem2(struct aem_ipmi_data *probe)
  643. {
  644. struct aem_find_instance_resp fi_resp;
  645. int err;
  646. int i = 0;
  647. while (!aem_find_aem2(probe, &fi_resp, i)) {
  648. if (fi_resp.major != 2) {
  649. dev_err(probe->bmc_device, "Unknown AEM v%d; please "
  650. "report this to the maintainer.\n",
  651. fi_resp.major);
  652. i++;
  653. continue;
  654. }
  655. err = aem_init_aem2_inst(probe, &fi_resp);
  656. if (err) {
  657. dev_err(probe->bmc_device,
  658. "Error %d initializing AEM2 0x%X\n",
  659. err, fi_resp.module_handle);
  660. return err;
  661. }
  662. i++;
  663. }
  664. return 0;
  665. }
  666. /* Probe a BMC for AEM firmware instances */
  667. static void aem_register_bmc(int iface, struct device *dev)
  668. {
  669. struct aem_ipmi_data probe;
  670. if (aem_init_ipmi_data(&probe, iface, dev))
  671. return;
  672. /* Ignore probe errors; they won't cause problems */
  673. aem_init_aem1(&probe);
  674. aem_init_aem2(&probe);
  675. ipmi_destroy_user(probe.user);
  676. }
  677. /* Handle BMC deletion */
  678. static void aem_bmc_gone(int iface)
  679. {
  680. struct aem_data *p1, *next1;
  681. list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
  682. if (p1->ipmi.interface == iface)
  683. aem_delete(p1);
  684. }
  685. /* sysfs support functions */
  686. /* AEM device name */
  687. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  688. char *buf)
  689. {
  690. struct aem_data *data = dev_get_drvdata(dev);
  691. return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
  692. }
  693. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
  694. /* AEM device version */
  695. static ssize_t show_version(struct device *dev,
  696. struct device_attribute *devattr,
  697. char *buf)
  698. {
  699. struct aem_data *data = dev_get_drvdata(dev);
  700. return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
  701. }
  702. static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
  703. /* Display power use */
  704. static ssize_t aem_show_power(struct device *dev,
  705. struct device_attribute *devattr,
  706. char *buf)
  707. {
  708. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  709. struct aem_data *data = dev_get_drvdata(dev);
  710. u64 before, after, delta, time;
  711. signed long leftover;
  712. struct timespec b, a;
  713. mutex_lock(&data->lock);
  714. update_aem_energy_one(data, attr->index);
  715. getnstimeofday(&b);
  716. before = data->energy[attr->index];
  717. leftover = schedule_timeout_interruptible(
  718. msecs_to_jiffies(data->power_period[attr->index])
  719. );
  720. if (leftover) {
  721. mutex_unlock(&data->lock);
  722. return 0;
  723. }
  724. update_aem_energy_one(data, attr->index);
  725. getnstimeofday(&a);
  726. after = data->energy[attr->index];
  727. mutex_unlock(&data->lock);
  728. time = timespec_to_ns(&a) - timespec_to_ns(&b);
  729. delta = (after - before) * UJ_PER_MJ;
  730. return sprintf(buf, "%llu\n",
  731. (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
  732. }
  733. /* Display energy use */
  734. static ssize_t aem_show_energy(struct device *dev,
  735. struct device_attribute *devattr,
  736. char *buf)
  737. {
  738. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  739. struct aem_data *a = dev_get_drvdata(dev);
  740. mutex_lock(&a->lock);
  741. update_aem_energy_one(a, attr->index);
  742. mutex_unlock(&a->lock);
  743. return sprintf(buf, "%llu\n",
  744. (unsigned long long)a->energy[attr->index] * 1000);
  745. }
  746. /* Display power interval registers */
  747. static ssize_t aem_show_power_period(struct device *dev,
  748. struct device_attribute *devattr,
  749. char *buf)
  750. {
  751. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  752. struct aem_data *a = dev_get_drvdata(dev);
  753. a->update(a);
  754. return sprintf(buf, "%lu\n", a->power_period[attr->index]);
  755. }
  756. /* Set power interval registers */
  757. static ssize_t aem_set_power_period(struct device *dev,
  758. struct device_attribute *devattr,
  759. const char *buf, size_t count)
  760. {
  761. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  762. struct aem_data *a = dev_get_drvdata(dev);
  763. unsigned long temp;
  764. int res;
  765. res = strict_strtoul(buf, 10, &temp);
  766. if (res)
  767. return res;
  768. if (temp < AEM_MIN_POWER_INTERVAL)
  769. return -EINVAL;
  770. mutex_lock(&a->lock);
  771. a->power_period[attr->index] = temp;
  772. mutex_unlock(&a->lock);
  773. return count;
  774. }
  775. /* Discover sensors on an AEM device */
  776. static int aem_register_sensors(struct aem_data *data,
  777. struct aem_ro_sensor_template *ro,
  778. struct aem_rw_sensor_template *rw)
  779. {
  780. struct device *dev = &data->pdev->dev;
  781. struct sensor_device_attribute *sensors = data->sensors;
  782. int err;
  783. /* Set up read-only sensors */
  784. while (ro->label) {
  785. sysfs_attr_init(&sensors->dev_attr.attr);
  786. sensors->dev_attr.attr.name = ro->label;
  787. sensors->dev_attr.attr.mode = S_IRUGO;
  788. sensors->dev_attr.show = ro->show;
  789. sensors->index = ro->index;
  790. err = device_create_file(dev, &sensors->dev_attr);
  791. if (err) {
  792. sensors->dev_attr.attr.name = NULL;
  793. goto error;
  794. }
  795. sensors++;
  796. ro++;
  797. }
  798. /* Set up read-write sensors */
  799. while (rw->label) {
  800. sysfs_attr_init(&sensors->dev_attr.attr);
  801. sensors->dev_attr.attr.name = rw->label;
  802. sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
  803. sensors->dev_attr.show = rw->show;
  804. sensors->dev_attr.store = rw->set;
  805. sensors->index = rw->index;
  806. err = device_create_file(dev, &sensors->dev_attr);
  807. if (err) {
  808. sensors->dev_attr.attr.name = NULL;
  809. goto error;
  810. }
  811. sensors++;
  812. rw++;
  813. }
  814. err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
  815. if (err)
  816. goto error;
  817. err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
  818. return err;
  819. error:
  820. aem_remove_sensors(data);
  821. return err;
  822. }
  823. /* sysfs support functions for AEM2 sensors */
  824. /* Display temperature use */
  825. static ssize_t aem2_show_temp(struct device *dev,
  826. struct device_attribute *devattr,
  827. char *buf)
  828. {
  829. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  830. struct aem_data *a = dev_get_drvdata(dev);
  831. a->update(a);
  832. return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
  833. }
  834. /* Display power-capping registers */
  835. static ssize_t aem2_show_pcap_value(struct device *dev,
  836. struct device_attribute *devattr,
  837. char *buf)
  838. {
  839. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  840. struct aem_data *a = dev_get_drvdata(dev);
  841. a->update(a);
  842. return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
  843. }
  844. /* Remove sensors attached to an AEM device */
  845. static void aem_remove_sensors(struct aem_data *data)
  846. {
  847. int i;
  848. for (i = 0; i < AEM_NUM_SENSORS; i++) {
  849. if (!data->sensors[i].dev_attr.attr.name)
  850. continue;
  851. device_remove_file(&data->pdev->dev,
  852. &data->sensors[i].dev_attr);
  853. }
  854. device_remove_file(&data->pdev->dev,
  855. &sensor_dev_attr_name.dev_attr);
  856. device_remove_file(&data->pdev->dev,
  857. &sensor_dev_attr_version.dev_attr);
  858. }
  859. /* Sensor probe functions */
  860. /* Description of AEM1 sensors */
  861. static struct aem_ro_sensor_template aem1_ro_sensors[] = {
  862. {"energy1_input", aem_show_energy, 0},
  863. {"power1_average", aem_show_power, 0},
  864. {NULL, NULL, 0},
  865. };
  866. static struct aem_rw_sensor_template aem1_rw_sensors[] = {
  867. {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
  868. {NULL, NULL, NULL, 0},
  869. };
  870. /* Description of AEM2 sensors */
  871. static struct aem_ro_sensor_template aem2_ro_sensors[] = {
  872. {"energy1_input", aem_show_energy, 0},
  873. {"energy2_input", aem_show_energy, 1},
  874. {"power1_average", aem_show_power, 0},
  875. {"power2_average", aem_show_power, 1},
  876. {"temp1_input", aem2_show_temp, 0},
  877. {"temp2_input", aem2_show_temp, 1},
  878. {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
  879. {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
  880. {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
  881. {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
  882. {"power3_average", aem2_show_pcap_value, POWER_AUX},
  883. {"power_cap", aem2_show_pcap_value, POWER_CAP},
  884. {NULL, NULL, 0},
  885. };
  886. static struct aem_rw_sensor_template aem2_rw_sensors[] = {
  887. {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
  888. {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
  889. {NULL, NULL, NULL, 0},
  890. };
  891. /* Set up AEM1 sensor attrs */
  892. static int aem1_find_sensors(struct aem_data *data)
  893. {
  894. return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
  895. }
  896. /* Set up AEM2 sensor attrs */
  897. static int aem2_find_sensors(struct aem_data *data)
  898. {
  899. return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
  900. }
  901. /* Module init/exit routines */
  902. static int __init aem_init(void)
  903. {
  904. int res;
  905. res = driver_register(&aem_driver.driver);
  906. if (res) {
  907. pr_err("Can't register aem driver\n");
  908. return res;
  909. }
  910. res = ipmi_smi_watcher_register(&driver_data.bmc_events);
  911. if (res)
  912. goto ipmi_reg_err;
  913. return 0;
  914. ipmi_reg_err:
  915. driver_unregister(&aem_driver.driver);
  916. return res;
  917. }
  918. static void __exit aem_exit(void)
  919. {
  920. struct aem_data *p1, *next1;
  921. ipmi_smi_watcher_unregister(&driver_data.bmc_events);
  922. driver_unregister(&aem_driver.driver);
  923. list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
  924. aem_delete(p1);
  925. }
  926. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  927. MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
  928. MODULE_LICENSE("GPL");
  929. module_init(aem_init);
  930. module_exit(aem_exit);
  931. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
  932. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
  933. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
  934. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
  935. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
  936. MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
  937. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");