ibmaem.c 27 KB

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