linux-2.6.20.4-ipmisensors-20070314-1214.patch 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. diff -rduNp linux-2.6.20.3.orig/drivers/char/ipmi/ipmi_msghandler.c linux-2.6.20.3/drivers/char/ipmi/ipmi_msghandler.c
  2. --- linux-2.6.20.3.orig/drivers/char/ipmi/ipmi_msghandler.c 2007-03-13 19:27:08.000000000 +0100
  3. +++ linux-2.6.20.3/drivers/char/ipmi/ipmi_msghandler.c 2007-03-14 14:23:02.000000000 +0100
  4. @@ -1954,6 +1954,24 @@ static void remove_proc_entries(ipmi_smi
  5. #endif /* CONFIG_PROC_FS */
  6. }
  7. +/*
  8. + * Retrieves the bmc_device struct for a given ipmi interface number (or NULL if none).
  9. + */
  10. +struct device *ipmi_get_bmcdevice(int if_num)
  11. +{
  12. + ipmi_smi_t intf;
  13. + mutex_lock(&ipmi_interfaces_mutex);
  14. + list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  15. + if (intf->intf_num == if_num){
  16. + mutex_unlock(&ipmi_interfaces_mutex);
  17. + return &intf->bmc->dev->dev;
  18. + }
  19. + }
  20. + mutex_unlock(&ipmi_interfaces_mutex);
  21. +
  22. + return NULL;
  23. +}
  24. +
  25. static int __find_bmc_guid(struct device *dev, void *data)
  26. {
  27. unsigned char *id = data;
  28. @@ -4183,3 +4201,4 @@ EXPORT_SYMBOL(ipmi_get_my_LUN);
  29. EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
  30. EXPORT_SYMBOL(ipmi_user_set_run_to_completion);
  31. EXPORT_SYMBOL(ipmi_free_recv_msg);
  32. +EXPORT_SYMBOL(ipmi_get_bmcdevice);
  33. diff -rduNp linux-2.6.20.3.orig/drivers/hwmon/Kconfig linux-2.6.20.3/drivers/hwmon/Kconfig
  34. --- linux-2.6.20.3.orig/drivers/hwmon/Kconfig 2007-03-13 19:27:08.000000000 +0100
  35. +++ linux-2.6.20.3/drivers/hwmon/Kconfig 2007-03-14 14:23:02.000000000 +0100
  36. @@ -218,6 +218,16 @@ config SENSORS_GL520SM
  37. This driver can also be built as a module. If so, the module
  38. will be called gl520sm.
  39. +config SENSORS_IPMI
  40. + tristate "IPMI Hardware Monitoring Support"
  41. + depends on HWMON && IPMI_HANDLER && EXPERIMENTAL
  42. + help
  43. + If you say yes here you get support for sensors monitored by
  44. + an IPMI baseboard management controller (BMC).
  45. +
  46. + This driver can also be built as a module. If so, the module
  47. + will be called ipmisensors.
  48. +
  49. config SENSORS_IT87
  50. tristate "ITE IT87xx and compatibles"
  51. depends on HWMON && I2C
  52. diff -rduNp linux-2.6.20.3.orig/drivers/hwmon/Makefile linux-2.6.20.3/drivers/hwmon/Makefile
  53. --- linux-2.6.20.3.orig/drivers/hwmon/Makefile 2007-03-13 19:27:08.000000000 +0100
  54. +++ linux-2.6.20.3/drivers/hwmon/Makefile 2007-03-14 14:23:02.000000000 +0100
  55. @@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_FSCPOS) += fscpos.o
  56. obj-$(CONFIG_SENSORS_GL518SM) += gl518sm.o
  57. obj-$(CONFIG_SENSORS_GL520SM) += gl520sm.o
  58. obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
  59. +obj-$(CONFIG_SENSORS_IPMI) += ipmisensors.o
  60. obj-$(CONFIG_SENSORS_IT87) += it87.o
  61. obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o
  62. obj-$(CONFIG_SENSORS_LM63) += lm63.o
  63. diff -rduNp linux-2.6.20.3.orig/drivers/hwmon/ipmisensors.c linux-2.6.20.3/drivers/hwmon/ipmisensors.c
  64. --- linux-2.6.20.3.orig/drivers/hwmon/ipmisensors.c 1970-01-01 01:00:00.000000000 +0100
  65. +++ linux-2.6.20.3/drivers/hwmon/ipmisensors.c 2007-03-14 14:44:42.000000000 +0100
  66. @@ -0,0 +1,1552 @@
  67. +/*
  68. + * ipmisensors.c - lm-sensors/hwmon interface to IPMI sensors.
  69. + *
  70. + * Copyright (C) 2004-2006 Yani Ioannou <yani.ioannou@gmail.com>
  71. + *
  72. + * Adapted from bmcsensors (lm-sensors for linux 2.4)
  73. + * bmcsensors (C) Mark D. Studebaker <mdsxyz123@yahoo.com>
  74. + *
  75. + * This program is free software; you can redistribute it and/or modify
  76. + * it under the terms of the GNU General Public License as published by
  77. + * the Free Software Foundation; either version 2 of the License, or
  78. + * (at your option) any later version.
  79. + *
  80. + * This program is distributed in the hope that it will be useful,
  81. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  83. + * GNU General Public License for more details.
  84. + *
  85. + * You should have received a copy of the GNU General Public License
  86. + * along with this program; if not, write to the Free Software
  87. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  88. + */
  89. +
  90. +#include <linux/init.h>
  91. +#include <linux/module.h>
  92. +#include <linux/param.h>
  93. +#include <linux/hwmon.h>
  94. +#include <linux/list.h>
  95. +#include <linux/slab.h>
  96. +#include <linux/device.h>
  97. +#include <linux/hwmon.h>
  98. +
  99. +#include "ipmisensors.h"
  100. +
  101. +/****** Function Prototypes ******/
  102. +static void ipmisensors_send_message(struct ipmisensors_bmc_data *bmc,
  103. + long msgid, struct kernel_ipmi_msg *msg);
  104. +static void ipmisensors_reserve_sdr(struct ipmisensors_bmc_data *bmc);
  105. +static void ipmisensors_get_sdr(struct ipmisensors_bmc_data *bmc, u16 res_id,
  106. + u16 record, u8 offset);
  107. +static void ipmisensors_set_sensor_threshold(struct ipmisensors_bmc_data *bmc,
  108. + u8 number, int value,
  109. + int lim_index);
  110. +static void ipmisensors_get_reading(struct ipmisensors_bmc_data *bmc,
  111. + struct sdrdata *sdr);
  112. +static void ipmisensors_msg_handler(struct ipmi_recv_msg *msg,
  113. + void *user_msg_data);
  114. +static int ipmisensors_intf_registered(int ipmi_intf);
  115. +static int ipmisensors_bmc_registered(struct device *bmc);
  116. +static void ipmisensors_register_bmc(int ipmi_intf, struct ipmi_addr *address);
  117. +static void ipmisensors_unregister_bmc(int ipmi_intf);
  118. +static void ipmisensors_unregister_bmc_all(void);
  119. +static void ipmisensors_new_smi(int if_num, struct device *dev);
  120. +static void ipmisensors_smi_gone(int if_num);
  121. +static void ipmisensors_update_bmc(struct work_struct *);
  122. +static void ipmisensors_cleanup(void);
  123. +
  124. +/****** Static Vars ******/
  125. +
  126. +/* set when module is being removed */
  127. +static int cleanup = 0;
  128. +
  129. +/* ipmisensors driver data */
  130. +static struct ipmisensors_data driver_data = {
  131. + .driver_name = "bmc",
  132. + .bmc_data = LIST_HEAD_INIT(driver_data.bmc_data),
  133. + .interfaces = 0,
  134. + .smi_watcher = {
  135. + .owner = THIS_MODULE,
  136. + .new_smi = ipmisensors_new_smi,
  137. + .smi_gone = ipmisensors_smi_gone,
  138. + },
  139. + .ipmi_hndlrs = {
  140. + .ipmi_recv_hndl = ipmisensors_msg_handler,
  141. + },
  142. +};
  143. +
  144. +/* sensor refresh workqueue */
  145. +static struct workqueue_struct *ipmisensors_workqueue;
  146. +
  147. +/****** SDR List Functions ******/
  148. +/**
  149. + * Creates a new sdrdata struct, or returns NULL if insufficient memory.
  150. + */
  151. +static struct sdrdata *ipmisensors_new_sdr(void)
  152. +{
  153. + struct sdrdata *sdr;
  154. +
  155. + sdr = kmem_cache_alloc(driver_data.sdrdata_cache, GFP_ATOMIC);
  156. + if (sdr) {
  157. + memset(sdr, 0, sizeof(struct sdrdata));
  158. + } else {
  159. + printk(KERN_ERR
  160. + "ipmisensors: Couldn't allocate memory for new SDR\n");
  161. + }
  162. +
  163. + return sdr;
  164. +}
  165. +
  166. +/**
  167. + * Adds the given sdrdata struct to the given bmc's SDR list.
  168. + *
  169. + * @bmc: the bmc to send the message to.
  170. + */
  171. +static inline void ipmisensors_add_sdr(struct ipmisensors_bmc_data *bmc,
  172. + struct sdrdata *sdr)
  173. +{
  174. + list_add(&sdr->list, &bmc->sdrs);
  175. + printk(KERN_DEBUG
  176. + "ipmisensors: SDR %d: type 0x%02x (%s)\n",
  177. + bmc->sdr_count, sdr->stype, sdr->id);
  178. + bmc->sdr_count++;
  179. +}
  180. +
  181. +/**
  182. + * Cleanup the sdr list for the given BMC.
  183. + *
  184. + * @bmc: the bmc to send the message to.
  185. + */
  186. +static void ipmisensors_sdr_cleanup(struct ipmisensors_bmc_data *bmc)
  187. +{
  188. + struct sdrdata *cursor, *next;
  189. +
  190. + /* find and free each sdr data struct */
  191. + list_for_each_entry_safe(cursor, next, &bmc->sdrs, list) {
  192. + device_remove_file(bmc->dev, &cursor->attr.dev_attr);
  193. + device_remove_file(bmc->dev, &cursor->attr_min.dev_attr);
  194. + device_remove_file(bmc->dev, &cursor->attr_max.dev_attr);
  195. + device_remove_file(bmc->dev, &cursor->attr_label.dev_attr);
  196. +
  197. + kfree(cursor->attr_name);
  198. + kfree(cursor->attr_max_name);
  199. + kfree(cursor->attr_min_name);
  200. + kfree(cursor->attr_label_name);
  201. +
  202. + list_del(&cursor->list);
  203. + kmem_cache_free(driver_data.sdrdata_cache, cursor);
  204. + }
  205. +}
  206. +
  207. +/* worker function for workqueue ipmisensors_workqueue */
  208. +static void ipmisensors_update_bmc(struct work_struct *work)
  209. +{
  210. + struct ipmisensors_bmc_data *bmc = container_of(work, struct ipmisensors_bmc_data, update_work.work);
  211. +
  212. + /* don't start an update cycle if one already in progress */
  213. + if (bmc->state != STATE_READING) {
  214. + struct sdrdata *cursor, *next;
  215. + bmc->state = STATE_READING;
  216. + printk(KERN_DEBUG "ipmisensors: starting update\n");
  217. +
  218. + /* init semaphore to 1 for update cycle */
  219. + sema_init(&bmc->update_semaphore, 1);
  220. +
  221. + /* update each sdr reading */
  222. + list_for_each_entry_safe(cursor, next, &bmc->sdrs, list) {
  223. + ipmisensors_get_reading(bmc, cursor);
  224. + }
  225. + }
  226. +
  227. + /* wait for readings (need timeout?) */
  228. + down_interruptible(&bmc->update_semaphore);
  229. +
  230. + printk(KERN_DEBUG "ipmisensors: update complete\n");
  231. +
  232. + bmc->state = STATE_DONE;
  233. +
  234. + /* if the module isn't cleaning up, schedule another update */
  235. + if (!cleanup)
  236. + queue_delayed_work(ipmisensors_workqueue, &bmc->update_work,
  237. + bmc->update_period * HZ);
  238. +}
  239. +
  240. +/****** IPMI Message Sending ******/
  241. +
  242. +/**
  243. + * Send a message to the IPMI BMC
  244. + *
  245. + * @bmc: the bmc to send the message to.
  246. + * @msgid: the message id to use.
  247. + * @msg: the ipmi message structure.
  248. + */
  249. +static void ipmisensors_send_message(struct ipmisensors_bmc_data *bmc,
  250. + long msgid, struct kernel_ipmi_msg *msg)
  251. +{
  252. + if (msg->data == NULL)
  253. + printk(KERN_DEBUG "ipmisensors: Send 0x%x\n", msg->cmd);
  254. + else
  255. + printk(KERN_DEBUG "ipmisensors: Send 0x%x 0x%x 0x%x\n",
  256. + msg->cmd, msg->data[0], msg->data[1]);
  257. +
  258. + /* This should be ipmi_request, but Corey had to remove
  259. + * that due to it being unused at the moment, as soon as
  260. + * this makes it into the kernel we should request it be re-instated.
  261. + */
  262. + ipmi_request_settime(bmc->user, &bmc->address, msgid, msg, bmc, 0,
  263. + -1, 0);
  264. +}
  265. +
  266. +/**
  267. + * Compose and send a "reserve SDR" message
  268. + *
  269. + * @bmc: the bmc to send the message to.
  270. + */
  271. +static void ipmisensors_reserve_sdr(struct ipmisensors_bmc_data *bmc)
  272. +{
  273. + bmc->tx_message.netfn = IPMI_NETFN_STORAGE_REQUEST;
  274. + bmc->tx_message.cmd = IPMI_RESERVE_SDR;
  275. + bmc->tx_message.data_len = 0;
  276. + bmc->tx_message.data = NULL;
  277. +
  278. + ipmisensors_send_message(bmc, bmc->msgid++, &bmc->tx_message);
  279. +}
  280. +
  281. +/**
  282. + * Componse and send a "get SDR" message
  283. + *
  284. + * @bmc: the bmc to send the message to.
  285. + * @res_id:
  286. + * @record:
  287. + * @offset:
  288. + */
  289. +static void ipmisensors_get_sdr(struct ipmisensors_bmc_data *bmc, u16 res_id,
  290. + u16 record, u8 offset)
  291. +{
  292. + printk(KERN_DEBUG "ipmisensors: Get SDR 0x%x 0x%x 0x%x\n",
  293. + res_id, record, offset);
  294. + bmc->tx_message.netfn = IPMI_NETFN_STORAGE_REQUEST;
  295. + bmc->tx_message.cmd = IPMI_GET_SDR;
  296. + bmc->tx_message.data_len = 6;
  297. + bmc->tx_message.data = bmc->tx_msg_data;
  298. + bmc->tx_msg_data[0] = res_id & 0xff;
  299. + bmc->tx_msg_data[1] = res_id >> 8;
  300. + bmc->tx_msg_data[2] = record & 0xff;
  301. + bmc->tx_msg_data[3] = record >> 8;
  302. + bmc->tx_msg_data[4] = offset;
  303. + bmc->tx_msg_data[5] = bmc->ipmi_sdr_partial_size;
  304. +
  305. + ipmisensors_send_message(bmc, bmc->msgid++, &bmc->tx_message);
  306. +}
  307. +
  308. +/**
  309. + * Compose and send a "set sensor threshold" message
  310. + *
  311. + * @bmc: the bmc to send the message to.
  312. + * @id: the ipmi id number of the sensor.
  313. + * @value: the new value for the threshold.
  314. + * @lim_index: the index in the lim[] array for which this value applies.
  315. + */
  316. +static void ipmisensors_set_sensor_threshold(struct ipmisensors_bmc_data *bmc,
  317. + u8 number, int value,
  318. + int lim_index)
  319. +{
  320. + int i;
  321. +
  322. + printk(KERN_DEBUG "ipmisensors: Set SDR Threshold %d %d %d\n",
  323. + number, value, lim_index);
  324. + bmc->tx_message.netfn = IPMI_NETFN_STORAGE_REQUEST;
  325. + bmc->tx_message.cmd = IPMI_SET_SENSOR_THRESHOLD;
  326. + bmc->tx_message.data_len = 8;
  327. + bmc->tx_message.data = bmc->tx_msg_data;
  328. + bmc->tx_msg_data[0] = number & 0xff;
  329. + bmc->tx_msg_data[1] = 0x01 << lim_index;
  330. +
  331. + if (lim_index > 5 || lim_index < 0) {
  332. + printk(KERN_INFO
  333. + "ipmisensors: Error - ipmisensors_set_sensor_threshold given invalid lim_index\n");
  334. + return;
  335. + }
  336. +
  337. + for (i = 2; i < 8; i++)
  338. + bmc->tx_msg_data[i] = 0x00;
  339. +
  340. + bmc->tx_msg_data[lim_index] = value && 0xff;
  341. +
  342. + ipmisensors_send_message(bmc, bmc->msgid++, &bmc->tx_message);
  343. +}
  344. +
  345. +/**
  346. + * Compose and send a "get sensor reading" message for the given sdr.
  347. + *
  348. + * @bmc: the bmc to send the message to.
  349. + * @sdr: the sdr of the sensor to get the reading for.
  350. + */
  351. +static void ipmisensors_get_reading(struct ipmisensors_bmc_data *bmc,
  352. + struct sdrdata *sdr)
  353. +{
  354. + bmc->tx_message.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
  355. + bmc->tx_message.cmd = IPMI_GET_SENSOR_STATE_READING;
  356. + bmc->tx_message.data_len = 1;
  357. + bmc->tx_message.data = bmc->tx_msg_data;
  358. + bmc->tx_msg_data[0] = sdr->number;
  359. + bmc->current_sdr = sdr;
  360. +
  361. + ipmisensors_send_message(bmc, bmc->msgid++, &bmc->tx_message);
  362. + down_interruptible(&bmc->update_semaphore);
  363. +}
  364. +
  365. +/****** IPMI Message Receiving ******/
  366. +
  367. +/**
  368. + * Process an sensor reading response message.
  369. + *
  370. + * @bmc: the bmc the message is from
  371. + * @msg: the IPMI SDR response message
  372. + */
  373. +static void ipmisensors_rcv_reading_msg(struct ipmisensors_bmc_data *bmc,
  374. + struct kernel_ipmi_msg *msg)
  375. +{
  376. + struct sdrdata *sdr = bmc->current_sdr;
  377. +
  378. + if (sdr == NULL) {
  379. + printk(KERN_ERR
  380. + "ipmisensors: Error ipmisensors_rcv_reading with NULL sdr\n");
  381. + return;
  382. + }
  383. +
  384. + sdr->reading = msg->data[1];
  385. + sdr->status = msg->data[2];
  386. + sdr->thresholds = msg->data[3];
  387. +
  388. + printk(KERN_DEBUG "ipmisensors: sensor %d (type %d) reading %d\n",
  389. + sdr->number, sdr->stype, msg->data[1]);
  390. +
  391. + up(&bmc->update_semaphore);
  392. +}
  393. +
  394. +/**
  395. + * Unpack based on string type, convert to normal, null terminate.
  396. + */
  397. +static void ipmisensors_sprintf(u8 * to, u8 * from, u8 type, u8 length)
  398. +{
  399. + static const u8 *bcdplus = "0123456789 -.:,_";
  400. + int i;
  401. +
  402. + switch (type) {
  403. + case 0: /* unicode */
  404. + for (i = 0; i < length; i++)
  405. + *to++ = (*from++ & 0x7f);
  406. + *to = 0;
  407. + break;
  408. + case 1: /* BCD Plus */
  409. + for (i = 0; i < length; i++)
  410. + *to++ = bcdplus[*from++ & 0x0f];
  411. + *to = 0;
  412. + break;
  413. + case 2: /* packed ascii *//* if not a mult. of 3 this will run over */
  414. + for (i = 0; i < length; i += 3) {
  415. + *to++ = *from & 0x3f;
  416. + *to++ = *from >> 6 | ((*(from+1) & 0xf) << 2);
  417. + from++;
  418. + *to++ = *from >> 4 | ((*(from+1) & 0x3) << 4);
  419. + from++;
  420. + *to++ = (*from++ >> 2) & 0x3f;
  421. + }
  422. + *to = 0;
  423. + break;
  424. + case 3: /* normal */
  425. + if (length > 1)
  426. + memcpy(to, from, length);
  427. + to[length] = 0;
  428. + break;
  429. + }
  430. +}
  431. +
  432. +/* IPMI V1.5 Section 30 */
  433. +static const int exps[] =
  434. + { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
  435. +
  436. +/* Return 0 for fan, 2 for temp, 3 for voltage
  437. + We could make it variable based on the accuracy (= log10(m * 10**k2));
  438. + this would work for /proc output, however libsensors resolution
  439. + is statically set in lib/chips.c */
  440. +static int decplaces(struct sdrdata *sd)
  441. +{
  442. + switch (sd->stype) {
  443. + case STYPE_TEMP:
  444. + return 2;
  445. + case STYPE_CURR:
  446. + case STYPE_VOLT:
  447. + return 3;
  448. + case STYPE_FAN:
  449. + default:
  450. + return 0;
  451. + }
  452. +}
  453. +
  454. +/* convert a raw value to a reading. IMPI V1.5 Section 30 */
  455. +static long conv_val(int value, struct sdrdata *sd)
  456. +{
  457. + u8 k1, k2;
  458. + long r;
  459. +
  460. + r = value * sd->m;
  461. + k1 = sd->k & 0x0f;
  462. + k2 = sd->k >> 4;
  463. + if (k1 < 8)
  464. + r += sd->b * exps[k1];
  465. + else
  466. + r += sd->b / exps[16 - k1];
  467. + r *= exps[decplaces(sd)];
  468. + if (k2 < 8) {
  469. + if (sd->linear != 7)
  470. + r *= exps[k2];
  471. + else
  472. + /* this will always truncate to 0: r = 1 / (exps[k2] * r); */
  473. + r = 0;
  474. + } else {
  475. + if (sd->linear != 7)
  476. + r /= exps[16 - k2];
  477. + else {
  478. + if (r != 0)
  479. + /* 1 / x * 10 ** (-m) == 10 ** m / x */
  480. + r = exps[16 - k2] / r;
  481. + else
  482. + r = 0;
  483. + }
  484. + }
  485. +
  486. + return r;
  487. +}
  488. +
  489. +static const char *threshold_text[] = {
  490. + "upper non-recoverable threshold",
  491. + "upper critical threshold",
  492. + "upper non-critical threshold",
  493. + "lower non-recoverable threshold",
  494. + "lower critical threshold",
  495. + "lower non-critical threshold",
  496. + "positive-going hysteresis",
  497. + "negative-going hysteresis" /* unused */
  498. +};
  499. +
  500. +/* select two out of the 8 possible readable thresholds, and place indexes into the limits
  501. + array into lim1 and lim2. Set writable flags */
  502. +static void ipmisensors_select_thresholds(struct sdrdata *sd)
  503. +{
  504. + u8 capab = sd->capab;
  505. + u16 mask = sd->thresh_mask;
  506. + int tmp;
  507. +
  508. + sd->lim1 = -1;
  509. + sd->lim2 = -1;
  510. + sd->lim1_write = 0;
  511. + sd->lim2_write = 0;
  512. +
  513. + if (((capab & 0x0c) == 0x04) || /* readable thresholds ? */
  514. + ((capab & 0x0c) == 0x08)) {
  515. + /* select upper threshold */
  516. + if (mask & 0x10) { /* upper crit */
  517. + sd->lim1 = 1;
  518. + if ((capab & 0x0c) == 0x08 && (mask & 0x1000))
  519. + sd->lim1_write = 1;
  520. + } else if (mask & 0x20) { /* upper non-recov */
  521. + sd->lim1 = 0;
  522. + if ((capab & 0x0c) == 0x08 && (mask & 0x2000))
  523. + sd->lim1_write = 1;
  524. + } else if (mask & 0x08) { /* upper non-crit */
  525. + sd->lim1 = 2;
  526. + if ((capab & 0x0c) == 0x08 && (mask & 0x0800))
  527. + sd->lim1_write = 1;
  528. + }
  529. +
  530. + /* select lower threshold */
  531. + if ((((capab & 0x30) == 0x10) || /* readable ? */
  532. + ((capab & 0x30) == 0x20)) && /* pos hyst */
  533. + sd->stype == STYPE_TEMP)
  534. + sd->lim2 = 6;
  535. + else if (mask & 0x02) { /* lower crit */
  536. + sd->lim2 = 4;
  537. + if ((capab & 0x0c) == 0x08 && (mask & 0x0200))
  538. + sd->lim2_write = 1;
  539. + } else if (mask & 0x04) { /* lower non-recov */
  540. + sd->lim2 = 3;
  541. + if ((capab & 0x0c) == 0x08 && (mask & 0x0400))
  542. + sd->lim2_write = 1;
  543. + } else if (mask & 0x01) { /* lower non-crit */
  544. + sd->lim2 = 5;
  545. + if ((capab & 0x0c) == 0x08 && (mask & 0x0100))
  546. + sd->lim2_write = 1;
  547. + }
  548. + }
  549. +
  550. + /* swap lim1/lim2 if m < 0 or function is 1/x (but not both!) */
  551. + if ((sd->m < 0 && sd->linear != 7) || (sd->m >= 0 && sd->linear == 7)) {
  552. + tmp = sd->lim1;
  553. + sd->lim1 = sd->lim2;
  554. + sd->lim2 = tmp;
  555. + }
  556. +
  557. + if (sd->lim1 >= 0)
  558. + printk(KERN_INFO "ipmisensors: using %s for upper limit\n",
  559. + threshold_text[sd->lim1]);
  560. + else
  561. + printk(KERN_DEBUG "ipmisensors: no readable upper limit\n");
  562. +
  563. + if (sd->lim2 >= 0)
  564. + printk(KERN_INFO "ipmisensors: using %s for lower limit\n",
  565. + threshold_text[sd->lim2]);
  566. + else
  567. + printk(KERN_DEBUG "ipmisensors: no readable lower limit\n");
  568. +}
  569. +
  570. +/************* sysfs callback functions *********/
  571. +static ssize_t show_update_period(struct device *dev,
  572. + struct device_attribute *attr, char *buf)
  573. +{
  574. + struct ipmisensors_bmc_device_attribute *aattr =
  575. + to_ipmisensors_bmc_dev_attr(attr);
  576. +
  577. + return snprintf(buf, 20, "%d\n", aattr->bmc->update_period);
  578. +}
  579. +
  580. +static ssize_t store_update_period(struct device *dev,
  581. + struct device_attribute *attr,
  582. + const char *buf, size_t count)
  583. +{
  584. + struct ipmisensors_bmc_device_attribute *aattr =
  585. + to_ipmisensors_bmc_dev_attr(attr);
  586. +
  587. + aattr->bmc->update_period = simple_strtoul(buf, NULL, 10);;
  588. + return count;
  589. +};
  590. +
  591. +static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
  592. + char *buf)
  593. +{
  594. + struct ipmisensors_device_attribute *sattr =
  595. + to_ipmisensors_dev_attr(attr);
  596. + return snprintf(buf, 20, "%ld\n",
  597. + conv_val(sattr->sdr->reading, sattr->sdr));
  598. +}
  599. +
  600. +static ssize_t show_sensor_max(struct device *dev,
  601. + struct device_attribute *attr, char *buf)
  602. +{
  603. + long max = 0;
  604. + struct ipmisensors_device_attribute *sattr =
  605. + to_ipmisensors_dev_attr(attr);
  606. +
  607. + if (sattr->sdr->lim1 >= 0)
  608. + max = conv_val(sattr->sdr->limits[sattr->sdr->lim1],
  609. + sattr->sdr);
  610. + return snprintf(buf, 20, "%ld\n", max);
  611. +}
  612. +
  613. +static ssize_t show_sensor_min(struct device *dev,
  614. + struct device_attribute *attr, char *buf)
  615. +{
  616. + long min = 0;
  617. + struct ipmisensors_device_attribute *sattr =
  618. + to_ipmisensors_dev_attr(attr);
  619. +
  620. + if (sattr->sdr->lim2 >= 0)
  621. + min = conv_val(sattr->sdr->limits[sattr->sdr->lim2],
  622. + sattr->sdr);
  623. + return snprintf(buf, 20, "%ld\n", min);
  624. +};
  625. +
  626. +static ssize_t show_sensor_label(struct device *dev,
  627. + struct device_attribute *attr, char *buf)
  628. +{
  629. + u8 label[SDR_MAX_UNPACKED_ID_LENGTH];
  630. + struct ipmisensors_device_attribute *sattr =
  631. + to_ipmisensors_dev_attr(attr);
  632. +
  633. + ipmisensors_sprintf(label, sattr->sdr->id, sattr->sdr->string_type,
  634. + sattr->sdr->id_length);
  635. + return snprintf(buf, 20, "%s\n", label);
  636. +};
  637. +
  638. +static ssize_t store_sensor_max(struct device *dev,
  639. + struct device_attribute *attr, const char *buf,
  640. + size_t count)
  641. +{
  642. + long val = simple_strtoul(buf, NULL, 10);
  643. + struct ipmisensors_device_attribute *sattr =
  644. + to_ipmisensors_dev_attr(attr);
  645. + printk(KERN_DEBUG "ipmisensors: set max on sensor #%d to %ld",
  646. + sattr->sdr->number, val);
  647. + ipmisensors_set_sensor_threshold(sattr->sdr->bmc, sattr->sdr->number,
  648. + val, sattr->sdr->lim1);
  649. + return count;
  650. +};
  651. +
  652. +static ssize_t store_sensor_min(struct device *dev,
  653. + struct device_attribute *attr, const char *buf,
  654. + size_t count)
  655. +{
  656. + long val = simple_strtoul(buf, NULL, 10);
  657. + struct ipmisensors_device_attribute *sattr =
  658. + to_ipmisensors_dev_attr(attr);
  659. + printk(KERN_DEBUG "ipmisensors: set min on sensor #%d to %ld",
  660. + sattr->sdr->number, val);
  661. + ipmisensors_set_sensor_threshold(sattr->sdr->bmc, sattr->sdr->number,
  662. + val, sattr->sdr->lim2);
  663. + return count;
  664. +};
  665. +
  666. +static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  667. + char *buf)
  668. +{
  669. + struct ipmisensors_bmc_device_attribute *aattr =
  670. + to_ipmisensors_bmc_dev_attr(attr);
  671. + return snprintf(buf, 20, "%d\n", aattr->bmc->alarms);
  672. +};
  673. +
  674. +static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  675. + char *buf)
  676. +{
  677. + return snprintf(buf, 20, "%s\n", driver_data.driver_name);
  678. +};
  679. +
  680. +/* work function to build the sysfs entries using the ipmi sdrs */
  681. +static void ipmisensors_build_sysfs(struct work_struct *work)
  682. +{
  683. + int temps = 0, volts = 0, currs = 0, fans = 0;
  684. + struct sdrdata *cursor, *next;
  685. + struct ipmisensors_bmc_data *bmc = container_of(work, struct ipmisensors_bmc_data, sysfs_work);
  686. +
  687. + /* find and create entries for each sdr data struct */
  688. + list_for_each_entry_safe(cursor, next, &bmc->sdrs, list) {
  689. + u8 id[SDR_MAX_UNPACKED_ID_LENGTH];
  690. +
  691. + cursor->attr_name =
  692. + (char *)kmalloc(sizeof(char) * MAX_FILENAME_LENGTH,
  693. + GFP_KERNEL);
  694. + cursor->attr_max_name =
  695. + (char *)kmalloc(sizeof(char) * MAX_FILENAME_LENGTH,
  696. + GFP_KERNEL);
  697. + cursor->attr_min_name =
  698. + (char *)kmalloc(sizeof(char) * MAX_FILENAME_LENGTH,
  699. + GFP_KERNEL);
  700. +
  701. + if (cursor->id_length > 0) {
  702. + cursor->attr_label_name =
  703. + (char *)kmalloc(sizeof(char) * MAX_FILENAME_LENGTH,
  704. + GFP_KERNEL);
  705. +
  706. + if (cursor->attr_label_name == NULL) {
  707. + printk(KERN_INFO
  708. + "ipmisensors: Out of memory (kmalloc failed)");
  709. + kfree(cursor->attr_name);
  710. + kfree(cursor->attr_max_name);
  711. + kfree(cursor->attr_min_name);
  712. + return;
  713. + }
  714. + }
  715. +
  716. + if (cursor->attr_name == NULL || cursor->attr_max_name == NULL
  717. + || cursor->attr_min_name == NULL
  718. + || cursor->attr_label_name == NULL) {
  719. + printk(KERN_INFO
  720. + "ipmisensors: Out of memory (kmalloc failed)");
  721. + kfree(cursor->attr_name);
  722. + kfree(cursor->attr_max_name);
  723. + kfree(cursor->attr_min_name);
  724. + kfree(cursor->attr_label_name);
  725. + return;
  726. + }
  727. +
  728. + switch (cursor->stype) {
  729. + case (STYPE_TEMP):
  730. + /* create the name of the sensor */
  731. + snprintf(cursor->attr_name, MAX_FILENAME_LENGTH,
  732. + "temp%d_input", ++temps);
  733. + /* create min, max attributes */
  734. + snprintf(cursor->attr_max_name, MAX_FILENAME_LENGTH,
  735. + "temp%d_max", temps);
  736. + snprintf(cursor->attr_min_name, MAX_FILENAME_LENGTH,
  737. + "temp%d_min", temps);
  738. + /* create the label of the sensor */
  739. + snprintf(cursor->attr_label_name, MAX_FILENAME_LENGTH,
  740. + "temp%d_label", temps);
  741. + break;
  742. + case (STYPE_VOLT):
  743. + /* create the name of the sensor */
  744. + snprintf(cursor->attr_name, MAX_FILENAME_LENGTH,
  745. + "in%d_input", ++volts);
  746. + /* create min, max attributes */
  747. + snprintf(cursor->attr_max_name, MAX_FILENAME_LENGTH,
  748. + "in%d_max", volts);
  749. + snprintf(cursor->attr_min_name, MAX_FILENAME_LENGTH,
  750. + "in%d_min", volts);
  751. + /* create the label of the sensor */
  752. + snprintf(cursor->attr_label_name, MAX_FILENAME_LENGTH,
  753. + "in%d_label", volts);
  754. + break;
  755. + case (STYPE_CURR):
  756. + /* create the name of the sensor */
  757. + snprintf(cursor->attr_name, MAX_FILENAME_LENGTH,
  758. + "curr%d_input", ++currs);
  759. + /* create min, max attributes */
  760. + sprintf(cursor->attr_max_name, "curr%d_max", currs);
  761. + sprintf(cursor->attr_min_name, "curr%d_min", currs);
  762. + /* create the label of the sensor */
  763. + snprintf(cursor->attr_label_name, MAX_FILENAME_LENGTH,
  764. + "curr%d_label", currs);
  765. + break;
  766. + case (STYPE_FAN):
  767. + /* create the name of the sensor */
  768. + snprintf(cursor->attr_name, MAX_FILENAME_LENGTH,
  769. + "fan%d_input", ++fans);
  770. + /* create min, max attributes */
  771. + sprintf(cursor->attr_max_name, "fan%d_max", fans);
  772. + sprintf(cursor->attr_min_name, "fan%d_min", fans);
  773. + /* create the label of the sensor */
  774. + snprintf(cursor->attr_label_name, MAX_FILENAME_LENGTH,
  775. + "fan%d_label", fans);
  776. + break;
  777. + default:
  778. + printk(KERN_INFO "ipmisensors: unkown sensor type\n");
  779. + continue;
  780. + }
  781. +
  782. + cursor->attr.dev_attr.attr.name = cursor->attr_name;
  783. + cursor->attr.dev_attr.attr.mode = S_IRUGO;
  784. + cursor->attr.dev_attr.attr.owner = THIS_MODULE;
  785. + cursor->attr.dev_attr.show = show_sensor;
  786. + cursor->attr.dev_attr.store = NULL;
  787. + cursor->attr.sdr = cursor;
  788. +
  789. + cursor->attr_min.dev_attr.attr.name = cursor->attr_min_name;
  790. + cursor->attr_min.dev_attr.attr.owner = THIS_MODULE;
  791. + cursor->attr_min.dev_attr.show = show_sensor_min;
  792. + cursor->attr_min.sdr = cursor;
  793. +
  794. + if (cursor->lim2_write) {
  795. + printk(KERN_INFO
  796. + "ipmisensors: You have a writable sensor threshold! Send me an e-mail at <yani.ioannou@gmail.com>.\n");
  797. + cursor->attr_min.dev_attr.store = store_sensor_min;
  798. + cursor->attr_min.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  799. + } else {
  800. + cursor->attr_min.dev_attr.store = NULL;
  801. + cursor->attr_min.dev_attr.attr.mode = S_IRUGO;
  802. + }
  803. +
  804. + cursor->attr_max.dev_attr.attr.name = cursor->attr_max_name;
  805. + cursor->attr_max.dev_attr.attr.owner = THIS_MODULE;
  806. + cursor->attr_max.dev_attr.show = show_sensor_max;
  807. + cursor->attr_max.sdr = cursor;
  808. +
  809. + if (cursor->lim1_write) {
  810. + printk(KERN_INFO
  811. + "ipmisensors: You have a writable sensor threshold! Send me an e-mail at <yani.ioannou@gmail.com>.\n");
  812. + cursor->attr_max.dev_attr.store = store_sensor_max;
  813. + cursor->attr_max.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  814. + } else {
  815. + cursor->attr_max.dev_attr.store = NULL;
  816. + cursor->attr_max.dev_attr.attr.mode = S_IRUGO;
  817. + }
  818. +
  819. + if (cursor->id_length > 0) {
  820. + cursor->attr_label.dev_attr.attr.name =
  821. + cursor->attr_label_name;
  822. + cursor->attr_label.dev_attr.attr.mode = S_IRUGO;
  823. + cursor->attr_label.dev_attr.attr.owner = THIS_MODULE;
  824. + cursor->attr_label.dev_attr.show = show_sensor_label;
  825. + cursor->attr_label.dev_attr.store = NULL;
  826. + cursor->attr_label.sdr = cursor;
  827. + }
  828. +
  829. + printk(KERN_INFO
  830. + "ipmisensors: registering sensor %d: (type 0x%.2x) "
  831. + "(fmt=%d; m=%d; b=%d; k1=%d; k2=%d; cap=0x%.2x; mask=0x%.4x)\n",
  832. + cursor->number, cursor->stype, cursor->format, cursor->m,
  833. + cursor->b, cursor->k & 0xf, cursor->k >> 4,
  834. + cursor->capab, cursor->thresh_mask);
  835. +
  836. + if (cursor->id_length > 0) {
  837. + ipmisensors_sprintf(id, cursor->id, cursor->string_type,
  838. + cursor->id_length);
  839. + switch (cursor->stype) {
  840. + case (STYPE_TEMP):
  841. + printk(KERN_INFO
  842. + "ipmisensors: sensors.conf: label temp%d \"%s\"\n",
  843. + temps, id);
  844. + break;
  845. + case (STYPE_VOLT):
  846. + printk(KERN_INFO
  847. + "ipmisensors: sensors.conf: label in%d \"%s\"\n",
  848. + volts, id);
  849. + break;
  850. + case (STYPE_CURR):
  851. + printk(KERN_INFO
  852. + "ipmisensors: sensors.conf: label curr%d \"%s\"\n",
  853. + currs, id);
  854. + break;
  855. + case (STYPE_FAN):
  856. + printk(KERN_INFO
  857. + "ipmisensors: sensors.conf: label fan%d \"%s\"\n",
  858. + fans, id);
  859. + break;
  860. + }
  861. + }
  862. +
  863. + ipmisensors_select_thresholds(cursor);
  864. +
  865. + if (cursor->linear != 0 && cursor->linear != 7) {
  866. + printk(KERN_INFO
  867. + "ipmisensors: sensor %d: nonlinear function 0x%.2x unsupported, expect bad results\n",
  868. + cursor->number, cursor->linear);
  869. + }
  870. +
  871. + if ((cursor->format & 0x03) == 0x02) {
  872. + printk(KERN_INFO
  873. + "ipmisensors: sensor %d: 1's complement format unsupported, expect bad results\n",
  874. + cursor->number);
  875. + } else if ((cursor->format & 0x03) == 0x03) {
  876. + printk(KERN_INFO
  877. + "ipmisensors: sensor %d: threshold sensor only, no readings available",
  878. + cursor->number);
  879. + }
  880. +
  881. + if (cursor->lim1_write || cursor->lim2_write)
  882. + cursor->attr.dev_attr.attr.mode = 0644;
  883. + else
  884. + cursor->attr.dev_attr.attr.mode = 0444;
  885. +
  886. + if (device_create_file(bmc->dev, &cursor->attr.dev_attr) < 0
  887. + || device_create_file(bmc->dev,
  888. + &cursor->attr_min.dev_attr) < 0
  889. + || device_create_file(bmc->dev,
  890. + &cursor->attr_max.dev_attr) < 0
  891. + || (cursor->id_length >
  892. + 0 ? device_create_file(bmc->dev,
  893. + &cursor->attr_label.dev_attr) <
  894. + 0 : 0)
  895. + ) {
  896. + printk(KERN_INFO
  897. + "ipmisensors: sysfs file creation failed for SDR %d (%s).\n",
  898. + cursor->number, cursor->id);
  899. + kfree(cursor->attr_name);
  900. + kfree(cursor->attr_max_name);
  901. + kfree(cursor->attr_min_name);
  902. + kfree(cursor->attr_label_name);
  903. + return;
  904. + }
  905. + }
  906. +
  907. + bmc->alarms_attr.dev_attr.attr.name = "alarms";
  908. + bmc->alarms_attr.dev_attr.attr.mode = S_IRUGO;
  909. + bmc->alarms_attr.dev_attr.attr.owner = THIS_MODULE;
  910. + bmc->alarms_attr.dev_attr.show = show_alarms;
  911. + bmc->alarms_attr.dev_attr.store = NULL;
  912. + bmc->alarms_attr.bmc = bmc;
  913. +
  914. + if (device_create_file(bmc->dev, &bmc->alarms_attr.dev_attr) < 0) {
  915. + printk(KERN_INFO
  916. + "ipmisensors: Failed to create sysfs entry 'alarms'");
  917. + return;
  918. + }
  919. +
  920. + bmc->name_attr.attr.name = "name";
  921. + bmc->name_attr.attr.mode = S_IRUGO;
  922. + bmc->name_attr.attr.owner = THIS_MODULE;
  923. + bmc->name_attr.show = show_name;
  924. +
  925. + if (device_create_file(bmc->dev, &bmc->name_attr) < 0) {
  926. + printk(KERN_INFO
  927. + "ipmisensors: Failed to create sysfs entry 'name'");
  928. + return;
  929. + }
  930. +
  931. + bmc->update_attr.dev_attr.attr.name = "update_period";
  932. + bmc->update_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  933. + bmc->update_attr.dev_attr.attr.owner = THIS_MODULE;
  934. + bmc->update_attr.dev_attr.show = show_update_period;
  935. + bmc->update_attr.dev_attr.store = store_update_period;
  936. + bmc->update_attr.bmc = bmc;
  937. +
  938. + if (device_create_file(bmc->dev, &bmc->update_attr.dev_attr) < 0) {
  939. + printk(KERN_INFO
  940. + "ipmisensors: Failed to create sysfs entry 'update_period'");
  941. + return;
  942. + }
  943. +
  944. + printk(KERN_INFO
  945. + "ipmisensors: registered %d temp, %d volt, %d current, %d fan sensors\n",
  946. + temps, volts, currs, fans);
  947. +
  948. + /* This completes the initialization. We can now kickoff the
  949. + * periodic update of the bmc sensor's values by scheduling
  950. + * the first work.
  951. + */
  952. + queue_work(ipmisensors_workqueue, &bmc->update_work.work);
  953. +
  954. +}
  955. +
  956. +/**
  957. + * Process an SDR response message, save the SDRs we like in the sdr
  958. + * list for the given BMC.
  959. + *
  960. + * @bmc: the bmc the message is from
  961. + * @msg: the IPMI SDR response message
  962. + */
  963. +static void ipmisensors_rcv_sdr_msg(struct ipmisensors_bmc_data *bmc,
  964. + struct kernel_ipmi_msg *msg)
  965. +{
  966. + u16 record;
  967. + int type;
  968. + int stype;
  969. + int id_length;
  970. + int i;
  971. + int ipmi_ver = 0;
  972. + unsigned char *data;
  973. + u8 id[SDR_MAX_UNPACKED_ID_LENGTH];
  974. + struct sdrdata *sdr;
  975. +
  976. + if (msg->data[0] != 0) {
  977. + /* cut request in half and try again */
  978. + bmc->ipmi_sdr_partial_size /= 2;
  979. + if (bmc->ipmi_sdr_partial_size < 8) {
  980. + printk(KERN_INFO
  981. + "ipmisensors: IPMI buffers too small, giving up\n");
  982. + bmc->state = STATE_DONE;
  983. + return;
  984. + }
  985. + printk(KERN_DEBUG
  986. + "ipmisensors: Reducing SDR request size to %d\n",
  987. + bmc->ipmi_sdr_partial_size);
  988. +
  989. + ipmisensors_get_sdr(bmc, 0, 0, 0);
  990. + bmc->state = STATE_SDR;
  991. + return;
  992. + }
  993. + if (bmc->ipmi_sdr_partial_size < IPMI_SDR_SIZE) {
  994. + if (bmc->rx_msg_data_offset == 0) {
  995. + memcpy(bmc->rx_msg_data, msg->data,
  996. + bmc->ipmi_sdr_partial_size + 3);
  997. + bmc->rx_msg_data_offset =
  998. + bmc->ipmi_sdr_partial_size + 3;
  999. + } else {
  1000. + memcpy(bmc->rx_msg_data + bmc->rx_msg_data_offset,
  1001. + msg->data + 3, bmc->ipmi_sdr_partial_size);
  1002. + bmc->rx_msg_data_offset += bmc->ipmi_sdr_partial_size;
  1003. + }
  1004. + if (bmc->rx_msg_data_offset > bmc->rx_msg_data[7] + 7) {
  1005. + /* got last chunk */
  1006. + bmc->rx_msg_data_offset = 0;
  1007. + data = bmc->rx_msg_data;
  1008. + } else {
  1009. + /* get more */
  1010. + record =
  1011. + (bmc->rx_msg_data[4] << 8) | bmc->rx_msg_data[3];
  1012. + ipmisensors_get_sdr(bmc, bmc->resid, record,
  1013. + bmc->rx_msg_data_offset - 3);
  1014. + bmc->state = STATE_SDR;
  1015. + return;
  1016. + }
  1017. + } else {
  1018. + /* got it in one chunk */
  1019. + data = msg->data;
  1020. + }
  1021. +
  1022. + bmc->nextrecord = (data[2] << 8) | data[1];
  1023. +
  1024. + /* If the ipmi version is 0.9 we have to remap some things.
  1025. + * Yes this is very ugly, but we aren't the ones who
  1026. + * implemented an incomplete spec!
  1027. + */
  1028. + ipmi_ver = data[5];
  1029. +
  1030. + type = data[6];
  1031. + /* known SDR type */
  1032. + if (type == 1 || type == 2) {
  1033. + stype = data[(ipmi_ver == 0x90 ? 16 : 15)];
  1034. + /* known sensor type */
  1035. + if (stype <= STYPE_MAX) {
  1036. + if (data[(ipmi_ver == 0x90 ? 17 : 16)] != 0x01) {
  1037. + if (type == 1)
  1038. + ipmisensors_sprintf(id, &data[51],
  1039. + data[50] >> 6,
  1040. + data[50] & 0x1f);
  1041. + else
  1042. + ipmisensors_sprintf(id,
  1043. + &data[(ipmi_ver ==
  1044. + 0x90 ? 30 :
  1045. + 35)],
  1046. + data[(ipmi_ver ==
  1047. + 0x90 ? 29 :
  1048. + 34)] >> 6,
  1049. + data[(ipmi_ver ==
  1050. + 0x90 ? 29 :
  1051. + 34)] & 0x1f);
  1052. + printk(KERN_INFO
  1053. + "ipmisensors: skipping non-threshold sensor \"%s\"\n",
  1054. + id);
  1055. + } else {
  1056. + /* add entry to sdrd table */
  1057. + sdr = ipmisensors_new_sdr();
  1058. + if (!sdr) {
  1059. + printk(KERN_ERR
  1060. + "ipmisensors: could not allocate memory for new SDR");
  1061. + return;
  1062. + }
  1063. + sdr->bmc = bmc;
  1064. + sdr->stype = stype;
  1065. + sdr->number = data[10];
  1066. + sdr->capab = data[(ipmi_ver == 0x90 ? 15 : 14)];
  1067. + sdr->thresh_mask =
  1068. + (((u16) data[(ipmi_ver == 0x90 ? 21 : 22)])
  1069. + << 8) | data[21];
  1070. + if (type == 1) {
  1071. + sdr->format =
  1072. + data[(ipmi_ver ==
  1073. + 0x90 ? 22 : 24)] >> 6;
  1074. + sdr->linear =
  1075. + data[(ipmi_ver ==
  1076. + 0x90 ? 25 : 26)] & 0x7f;
  1077. + sdr->m =
  1078. + data[(ipmi_ver == 0x90 ? 26 : 27)];
  1079. + sdr->m |= ((u16)
  1080. + (data
  1081. + [(ipmi_ver ==
  1082. + 0x90 ? 27 : 28)]
  1083. + & 0xc0)) << 2;
  1084. + if (sdr->m & 0x0200) {
  1085. + /* sign extend */
  1086. + sdr->m |= 0xfc00;
  1087. + }
  1088. + sdr->b =
  1089. + data[(ipmi_ver == 0x90 ? 28 : 29)];
  1090. + sdr->b |= ((u16)
  1091. + (data
  1092. + [(ipmi_ver ==
  1093. + 0x90 ? 29 : 30)]
  1094. + & 0xc0)) << 2;
  1095. + if (sdr->b & 0x0200) {
  1096. + /* sign extend */
  1097. + sdr->b |= 0xfc00;
  1098. + }
  1099. + sdr->k =
  1100. + data[(ipmi_ver == 0x90 ? 31 : 32)];
  1101. + sdr->nominal =
  1102. + data[(ipmi_ver == 0x90 ? 33 : 34)];
  1103. + for (i = 0; i < SDR_LIMITS; i++) {
  1104. + /* assume readable */
  1105. + sdr->limits[i] =
  1106. + data[(ipmi_ver ==
  1107. + 0x90 ? 40 : 39) + i];
  1108. + }
  1109. + sdr->string_type = data[50] >> 6;
  1110. + id_length = data[50] & 0x1f;
  1111. + memcpy(sdr->id, &data[51], id_length);
  1112. + sdr->id_length = id_length;
  1113. + } else {
  1114. + sdr->m = 1;
  1115. + sdr->b = 0;
  1116. + sdr->k = 0;
  1117. + sdr->string_type =
  1118. + data[(ipmi_ver ==
  1119. + 0x90 ? 29 : 34)] >> 6;
  1120. + id_length = data[34] & 0x1f;
  1121. + if (id_length > 0) {
  1122. + memcpy(sdr->id,
  1123. + &data[(ipmi_ver ==
  1124. + 0x90 ? 30 : 35)],
  1125. + id_length);
  1126. + }
  1127. + sdr->id_length = id_length;
  1128. + /* limits?? */
  1129. + if (ipmi_ver == 0x90) {
  1130. + memcpy(sdr->id,
  1131. + &data[30], id_length);
  1132. + sdr->id_length = id_length;
  1133. + }
  1134. + }
  1135. + ipmisensors_add_sdr(bmc, sdr);
  1136. + }
  1137. + }
  1138. + /* peek at the other SDR types */
  1139. + } else if (type == 0x10 || type == 0x11 || type == 0x12) {
  1140. + ipmisensors_sprintf(id, data + 19, data[18] >> 6,
  1141. + data[18] & 0x1f);
  1142. + if (type == 0x10) {
  1143. + printk(KERN_INFO
  1144. + "ipmisensors: Generic Device acc=0x%x; slv=0x%x; lun=0x%x; type=0x%x; \"%s\"\n",
  1145. + data[8], data[9], data[10], data[13], id);
  1146. + } else if (type == 0x11) {
  1147. + printk(KERN_INFO
  1148. + "ipmisensors: FRU Device acc=0x%x; slv=0x%x; log=0x%x; ch=0x%x; type=0x%x; \"%s\"\n",
  1149. + data[8], data[9], data[10], data[11], data[13],
  1150. + id);
  1151. + } else {
  1152. + printk(KERN_INFO
  1153. + "ipmisensors: Mgmt Ctllr Device slv=0x%x; \"%s\"\n",
  1154. + data[8], id);
  1155. + }
  1156. + } else if (type == 0x14) {
  1157. + printk(KERN_INFO
  1158. + "ipmisensors: Message Channel Info Records:\n");
  1159. + for (i = 0; i < 8; i++) {
  1160. + printk(KERN_INFO "ipmisensors: Channel %d info 0x%x\n",
  1161. + i, data[9 + i]);
  1162. + }
  1163. + } else {
  1164. + printk(KERN_INFO "ipmisensors: Skipping SDR type 0x%x\n", type);
  1165. + }
  1166. + if (ipmi_ver != 0x90) {
  1167. + if (bmc->nextrecord >= 6224) {
  1168. + /*YJ stop sensor scan on poweredge 1750 */
  1169. + bmc->nextrecord = 0xffff;
  1170. + }
  1171. + }
  1172. +
  1173. + if (bmc->nextrecord == 0xFFFF) {
  1174. + if (bmc->sdr_count == 0) {
  1175. + printk(KERN_INFO
  1176. + "ipmisensors: No recognized sensors found.\n");
  1177. + bmc->state = STATE_DONE;
  1178. + } else {
  1179. + printk(KERN_INFO "ipmisensors: all sensors detected\n");
  1180. + bmc->state = STATE_SYSTABLE;
  1181. +
  1182. + /* Schedule sysfs build/registration work */
  1183. + INIT_WORK(&bmc->sysfs_work, ipmisensors_build_sysfs);
  1184. + queue_work(ipmisensors_workqueue, &bmc->sysfs_work);
  1185. + }
  1186. + } else {
  1187. + ipmisensors_get_sdr(bmc, 0, bmc->nextrecord, 0);
  1188. + bmc->state = STATE_SDR;
  1189. + }
  1190. +}
  1191. +
  1192. +/**
  1193. + * Process incoming messages based on internal state
  1194. + *
  1195. + * @bmc: the bmc the message is from.
  1196. + * @msg: the ipmi message to process.
  1197. + */
  1198. +static void ipmisensors_rcv_msg(struct ipmisensors_bmc_data *bmc,
  1199. + struct kernel_ipmi_msg *msg)
  1200. +{
  1201. + switch (bmc->state) {
  1202. + case STATE_INIT:
  1203. + case STATE_RESERVE:
  1204. + bmc->resid = (((u16) msg->data[2]) << 8) | msg->data[1];
  1205. +
  1206. + printk(KERN_DEBUG "ipmisensors: Got first resid 0x%.4x\n",
  1207. + bmc->resid);
  1208. +
  1209. + ipmisensors_get_sdr(bmc, 0, 0, 0);
  1210. + bmc->state = STATE_SDR;
  1211. + break;
  1212. +
  1213. + case STATE_SDR:
  1214. + case STATE_SDRPARTIAL:
  1215. + ipmisensors_rcv_sdr_msg(bmc, msg);
  1216. + break;
  1217. +
  1218. + case STATE_READING:
  1219. + ipmisensors_rcv_reading_msg(bmc, msg);
  1220. + break;
  1221. +
  1222. + case STATE_UNCANCEL:
  1223. + bmc->resid = (((u16) msg->data[2]) << 8) | msg->data[1];
  1224. +
  1225. + printk(KERN_DEBUG "ipmisensors: Got new resid 0x%.4x\n",
  1226. + bmc->resid);
  1227. +
  1228. + bmc->rx_msg_data_offset = 0;
  1229. + ipmisensors_get_sdr(bmc, 0, bmc->nextrecord, 0);
  1230. + bmc->state = STATE_SDR;
  1231. + break;
  1232. +
  1233. + case STATE_DONE:
  1234. + case STATE_SYSTABLE:
  1235. + break;
  1236. + default:
  1237. + bmc->state = STATE_INIT;
  1238. + }
  1239. +}
  1240. +
  1241. +/**
  1242. + * Callback to handle a received IPMI message from a given BMC.
  1243. + *
  1244. + * @msg: the received message.
  1245. + * @handler_data: a pointer to the particular bmc ipmisensors_bmc_data struct.
  1246. + */
  1247. +static void ipmisensors_msg_handler(struct ipmi_recv_msg *msg,
  1248. + void *user_msg_data)
  1249. +{
  1250. + struct ipmisensors_bmc_data *bmc =
  1251. + (struct ipmisensors_bmc_data *)user_msg_data;
  1252. +
  1253. + if (msg->msg.data[0] != 0)
  1254. + printk(KERN_WARNING
  1255. + "ipmisensors: Error 0x%x on cmd 0x%x/0x%x\n",
  1256. + msg->msg.data[0], msg->msg.netfn, msg->msg.cmd);
  1257. +
  1258. + if (bmc != NULL && ipmisensors_intf_registered(bmc->interface_id)) {
  1259. + if (bmc->state == STATE_SDR &&
  1260. + msg->msg.data[0] == IPMI_INVALID_RESERVATION_ID) {
  1261. + /* reservation cancelled, get new resid */
  1262. + if (++bmc->errorcount > 275) {
  1263. + printk(KERN_ERR
  1264. + "ipmisensors: Too many reservations cancelled, giving up\n");
  1265. + bmc->state = STATE_DONE;
  1266. + } else {
  1267. + printk(KERN_DEBUG
  1268. + "ipmisensors: resid 0x%04x cancelled, getting new one\n",
  1269. + bmc->resid);
  1270. +
  1271. + ipmisensors_reserve_sdr(bmc);
  1272. + bmc->state = STATE_UNCANCEL;
  1273. + }
  1274. + } else if (msg->msg.data[0] != IPMI_CC_NO_ERROR &&
  1275. + msg->msg.data[0] != IPMI_ERR_RETURNING_REQ_BYTES &&
  1276. + msg->msg.data[0] != IPMI_ERR_PROVIDING_RESPONSE) {
  1277. + printk(KERN_ERR
  1278. + "ipmisensors: Error 0x%x on cmd 0x%x/0x%x; state = %d; probably fatal.\n",
  1279. + msg->msg.data[0], msg->msg.netfn & 0xfe,
  1280. + msg->msg.cmd, bmc->state);
  1281. + } else {
  1282. + printk(KERN_DEBUG "ipmisensors: received message\n");
  1283. + ipmisensors_rcv_msg(bmc, &msg->msg);
  1284. + }
  1285. +
  1286. + } else {
  1287. + printk(KERN_WARNING
  1288. + "ipmisensors: Response for non-registered BMC\n");
  1289. + if (bmc != NULL)
  1290. + printk(KERN_DEBUG "ipmisensors: BMC ID: %d\n",
  1291. + bmc->interface_id);
  1292. + else
  1293. + printk(KERN_DEBUG "ipmisensors: BMC NULL!\n");
  1294. + }
  1295. +
  1296. + ipmi_free_recv_msg(msg);
  1297. +}
  1298. +
  1299. +/****** IPMI Interface Initialization ******/
  1300. +
  1301. +/**
  1302. + * Return true if the given ipmi interface has been registered.
  1303. + *
  1304. + * @ipmi_intf: The IPMI interface number.
  1305. + */
  1306. +static int ipmisensors_intf_registered(int ipmi_intf)
  1307. +{
  1308. + int found = 0;
  1309. + struct ipmisensors_bmc_data *cursor, *next;
  1310. +
  1311. + /* find and free the ipmisensors_bmc_data struct */
  1312. + list_for_each_entry_safe(cursor, next, &driver_data.bmc_data, list) {
  1313. + if (cursor->interface_id == ipmi_intf) {
  1314. + found++;
  1315. + }
  1316. + }
  1317. +
  1318. + return found;
  1319. +}
  1320. +
  1321. +/**
  1322. + * Return true if the given BMC has been registered.
  1323. + *
  1324. + * @bmc: The BMC device.
  1325. + */
  1326. +static int ipmisensors_bmc_registered(struct device *bmc)
  1327. +{
  1328. + int found = 0;
  1329. + struct ipmisensors_bmc_data *cursor, *next;
  1330. +
  1331. + /* find and free the ipmisensors_bmc_data struct */
  1332. + list_for_each_entry_safe(cursor, next, &driver_data.bmc_data, list) {
  1333. + if (cursor->dev == bmc) {
  1334. + found++;
  1335. + }
  1336. + }
  1337. +
  1338. + return found;
  1339. +}
  1340. +
  1341. +/**
  1342. + * Register new IPMI BMC interface. Interface indpendent callback created
  1343. + * for flexibility in adding new types of interface callbacks in future.
  1344. + *
  1345. + * @ipmi_intf: The IPMI interface number.
  1346. + */
  1347. +static void ipmisensors_register_bmc(int ipmi_intf, struct ipmi_addr *address)
  1348. +{
  1349. + int error;
  1350. +
  1351. + /* allocate a new ipmisensors_bmc_data struct */
  1352. +
  1353. + struct ipmisensors_bmc_data *bmc = (struct ipmisensors_bmc_data *)
  1354. + kmalloc(sizeof(struct ipmisensors_bmc_data), GFP_KERNEL);
  1355. +
  1356. + /* initialize members */
  1357. + INIT_LIST_HEAD(&bmc->sdrs);
  1358. + bmc->interface_id = ipmi_intf;
  1359. +
  1360. + bmc->address = *address;
  1361. +
  1362. + bmc->sdr_count = 0;
  1363. + bmc->msgid = 0;
  1364. + bmc->ipmi_sdr_partial_size = IPMI_CHUNK_SIZE;
  1365. + bmc->state = STATE_INIT;
  1366. + bmc->errorcount = 0;
  1367. + bmc->rx_msg_data_offset = 0;
  1368. + bmc->dev = ipmi_get_bmcdevice(ipmi_intf);
  1369. +
  1370. + /* default to 3 second min update interval */
  1371. + bmc->update_period = 3;
  1372. +
  1373. + if (bmc->dev == NULL) {
  1374. + printk(KERN_ERR
  1375. + "ipmisensors: Error, couldn't get BMC device for interface %d\n",
  1376. + bmc->interface_id);
  1377. + kfree(bmc);
  1378. + return;
  1379. + }
  1380. +
  1381. + /* Create IPMI messaging interface user */
  1382. + error = ipmi_create_user(bmc->interface_id, &driver_data.ipmi_hndlrs,
  1383. + bmc, &bmc->user);
  1384. + if (error < 0) {
  1385. + printk(KERN_ERR
  1386. + "ipmisensors: Error, unable to register user with ipmi interface %d\n",
  1387. + bmc->interface_id);
  1388. + kfree(bmc);
  1389. + return;
  1390. + }
  1391. +
  1392. + /* Register the BMC as a HWMON class device */
  1393. + bmc->class_dev = hwmon_device_register(bmc->dev);
  1394. +
  1395. + if (IS_ERR(bmc->class_dev)) {
  1396. + printk(KERN_ERR
  1397. + "ipmisensors: Error, unable to register hwmon class device for interface %d\n",
  1398. + bmc->interface_id);
  1399. + kfree(bmc);
  1400. + return;
  1401. + }
  1402. +
  1403. + /* Register the BMC in the driver */
  1404. + if (ipmisensors_bmc_registered(bmc->dev)) {
  1405. + printk(KERN_ERR
  1406. + "ipmisensors: BMC on interface %d already registered\n",
  1407. + bmc->interface_id);
  1408. + hwmon_device_unregister(bmc->class_dev);
  1409. + kfree(bmc);
  1410. + return;
  1411. + }
  1412. +
  1413. + ipmi_get_version(bmc->user, &bmc->ipmi_version_major,
  1414. + &bmc->ipmi_version_minor);
  1415. +
  1416. + /* finally add the new bmc data to the bmc data list */
  1417. + list_add_tail(&bmc->list, &driver_data.bmc_data);
  1418. + driver_data.interfaces++;
  1419. +
  1420. + printk(KERN_INFO
  1421. + "ipmisensors: Registered IPMI %d.%d BMC over interface %d\n",
  1422. + bmc->ipmi_version_major,
  1423. + bmc->ipmi_version_minor, bmc->interface_id);
  1424. +
  1425. + /* Send a reserve SDR command to the bmc */
  1426. + ipmisensors_reserve_sdr(bmc);
  1427. +
  1428. + /* initialize the bmc's update work struct */
  1429. + INIT_DELAYED_WORK(&bmc->update_work, ipmisensors_update_bmc);
  1430. +}
  1431. +
  1432. +/**
  1433. + * Callback for when an IPMI BMC is gone. Interface indpendent callback created
  1434. + * for flexibility in adding new types of interface callbacks in future.
  1435. + *
  1436. + * @ipmi_intf: The IPMI interface number.
  1437. + */
  1438. +static void ipmisensors_unregister_bmc(int ipmi_intf)
  1439. +{
  1440. + struct ipmisensors_bmc_data *cursor, *next;
  1441. +
  1442. + /* find and free the ipmisensors_bmc_data struct */
  1443. + list_for_each_entry_safe(cursor, next, &driver_data.bmc_data, list) {
  1444. + if (cursor->interface_id == ipmi_intf) {
  1445. + list_del(&cursor->list);
  1446. + printk(KERN_DEBUG
  1447. + "ipmisensors: cancelling queued work\n");
  1448. + /* cancel update work queued for this bmc */
  1449. + cancel_delayed_work(&cursor->update_work);
  1450. + printk(KERN_DEBUG
  1451. + "ipmisensors: waiting for update to finish\n");
  1452. + /* wait for readings to finish */
  1453. + while (cursor->state != STATE_DONE) ;
  1454. +
  1455. + device_remove_file(cursor->dev,
  1456. + &cursor->alarms_attr.dev_attr);
  1457. + device_remove_file(cursor->dev,
  1458. + &cursor->update_attr.dev_attr);
  1459. + hwmon_device_unregister(cursor->class_dev);
  1460. + ipmisensors_sdr_cleanup(cursor);
  1461. + ipmi_destroy_user(cursor->user);
  1462. +
  1463. + printk(KERN_INFO
  1464. + "ipmisensors: Unegistered IPMI interface %d\n",
  1465. + cursor->interface_id);
  1466. +
  1467. + kfree(cursor);
  1468. + driver_data.interfaces--;
  1469. + }
  1470. + }
  1471. +
  1472. +}
  1473. +
  1474. +/**
  1475. + * Unregister all registered bmcs.
  1476. + */
  1477. +static void ipmisensors_unregister_bmc_all(void)
  1478. +{
  1479. + struct ipmisensors_bmc_data *cursor, *next;
  1480. +
  1481. + /* find and free the ipmisensors_bmc_data struct */
  1482. + list_for_each_entry_safe(cursor, next, &driver_data.bmc_data, list) {
  1483. + list_del(&cursor->list);
  1484. +
  1485. + /* cancel update work queued for this bmc */
  1486. + printk(KERN_DEBUG "ipmisensors: cancelling queued work\n");
  1487. + cancel_delayed_work(&cursor->update_work);
  1488. +
  1489. + printk(KERN_DEBUG
  1490. + "ipmisensors: waiting for update to finish\n");
  1491. + /* wait for readings to finish */
  1492. + while (cursor->state != STATE_DONE) ;
  1493. +
  1494. + device_remove_file(cursor->dev, &cursor->alarms_attr.dev_attr);
  1495. + device_remove_file(cursor->dev, &cursor->update_attr.dev_attr);
  1496. + hwmon_device_unregister(cursor->class_dev);
  1497. + ipmisensors_sdr_cleanup(cursor);
  1498. + ipmi_destroy_user(cursor->user);
  1499. +
  1500. + printk(KERN_INFO
  1501. + "ipmisensors: Unegistered IPMI interface %d\n",
  1502. + cursor->interface_id);
  1503. +
  1504. + kfree(cursor);
  1505. + }
  1506. +
  1507. + driver_data.interfaces = 0;
  1508. +}
  1509. +
  1510. +/**
  1511. + * Callback for when a new IPMI SMI type interface is found.
  1512. + *
  1513. + * @if_num: The IPMI interface number.
  1514. + */
  1515. +static void ipmisensors_new_smi(int if_num, struct device *dev)
  1516. +{
  1517. + struct ipmi_addr smi_address = {
  1518. + IPMI_SYSTEM_INTERFACE_ADDR_TYPE,
  1519. + IPMI_BMC_CHANNEL,
  1520. + {0},
  1521. + };
  1522. +
  1523. + /* calls the generic new interface function */
  1524. + ipmisensors_register_bmc(if_num, &smi_address);
  1525. +}
  1526. +
  1527. +/**
  1528. + * Callback for when an exisiting IPMI SMI type interface is gone.
  1529. + *
  1530. + * @if_num: The IPMI interface number.
  1531. + */
  1532. +static void ipmisensors_smi_gone(int if_num)
  1533. +{
  1534. + if (driver_data.interfaces > 0) {
  1535. + ipmisensors_unregister_bmc(if_num);
  1536. + }
  1537. +}
  1538. +
  1539. +/**
  1540. + * Initialize the module.
  1541. + */
  1542. +static int __init ipmisensors_init(void)
  1543. +{
  1544. + int error;
  1545. + printk(KERN_INFO "ipmisensors - IPMI BMC sensors interface\n");
  1546. +
  1547. + /* init cache managers */
  1548. + driver_data.sdrdata_cache =
  1549. + kmem_cache_create("ipmisensors_sdrdata", sizeof(struct sdrdata), 0,
  1550. + 0, NULL, NULL);
  1551. + driver_data.sysfsattr_cache =
  1552. + kmem_cache_create("ipmisensors_sysfsattr",
  1553. + sizeof(struct ipmisensors_device_attribute), 0, 0,
  1554. + NULL, NULL);
  1555. +
  1556. + if (!driver_data.sdrdata_cache || !driver_data.sysfsattr_cache) {
  1557. + if (driver_data.sdrdata_cache)
  1558. + kmem_cache_destroy(driver_data.sdrdata_cache);
  1559. + if (driver_data.sysfsattr_cache)
  1560. + kmem_cache_destroy(driver_data.sysfsattr_cache);
  1561. + return -ENOMEM;
  1562. + }
  1563. +
  1564. + /* register IPMI interface callback(s) */
  1565. + error = ipmi_smi_watcher_register(&driver_data.smi_watcher);
  1566. + if (error) {
  1567. + printk(KERN_WARNING
  1568. + "ipmisensors: can't register smi watcher\n");
  1569. + return error;
  1570. + }
  1571. +
  1572. + /* create work queue, keep it simple, single-threaded */
  1573. + ipmisensors_workqueue =
  1574. + create_singlethread_workqueue("ipmisensors_workqueue");
  1575. +
  1576. + return 0;
  1577. +}
  1578. +
  1579. +/**
  1580. + * Cleanup
  1581. + */
  1582. +static void ipmisensors_cleanup(void)
  1583. +{
  1584. + /* start cleanup */
  1585. + cleanup = 1;
  1586. +
  1587. + /* unregister bmcs */
  1588. + printk(KERN_DEBUG "ipmisensors: unregister bmcs\n");
  1589. + ipmi_smi_watcher_unregister(&driver_data.smi_watcher);
  1590. + ipmisensors_unregister_bmc_all();
  1591. +
  1592. + /* flush & destroy work queue */
  1593. + printk(KERN_DEBUG "ipmisensors: destroy workqueue\n");
  1594. + flush_workqueue(ipmisensors_workqueue);
  1595. + destroy_workqueue(ipmisensors_workqueue);
  1596. +
  1597. + /* remove cache managers */
  1598. + if (driver_data.sdrdata_cache)
  1599. + kmem_cache_destroy(driver_data.sdrdata_cache);
  1600. + if (driver_data.sysfsattr_cache)
  1601. + kmem_cache_destroy(driver_data.sysfsattr_cache);
  1602. +}
  1603. +
  1604. +/**
  1605. + * Cleanup and exit the module
  1606. + */
  1607. +static void __exit ipmisensors_exit(void)
  1608. +{
  1609. + ipmisensors_cleanup();
  1610. + printk(KERN_DEBUG "ipmisensors: cleanup finished\n");
  1611. +}
  1612. +
  1613. +MODULE_AUTHOR("Yani Ioannou <yani.ioannou@gmail.com>");
  1614. +MODULE_DESCRIPTION("IPMI BMC sensors");
  1615. +MODULE_LICENSE("GPL");
  1616. +
  1617. +module_init(ipmisensors_init);
  1618. +module_exit(ipmisensors_exit);
  1619. diff -rduNp linux-2.6.20.3.orig/drivers/hwmon/ipmisensors.h linux-2.6.20.3/drivers/hwmon/ipmisensors.h
  1620. --- linux-2.6.20.3.orig/drivers/hwmon/ipmisensors.h 1970-01-01 01:00:00.000000000 +0100
  1621. +++ linux-2.6.20.3/drivers/hwmon/ipmisensors.h 2007-03-14 14:41:23.000000000 +0100
  1622. @@ -0,0 +1,240 @@
  1623. +/*
  1624. + * ipmisensors.h - lm_sensors interface to IPMI sensors.
  1625. + *
  1626. + * Copyright (C) 2004-2006 Yani Ioannou <yani.ioannou@gmail.com>
  1627. + *
  1628. + * Adapted from bmcsensors (lm-sensors for linux 2.4)
  1629. + * bmcsensors (C) Mark D. Studebaker <mdsxyz123@yahoo.com>
  1630. + *
  1631. + * This program is free software; you can redistribute it and/or modify
  1632. + * it under the terms of the GNU General Public License as published by
  1633. + * the Free Software Foundation; either version 2 of the License, or
  1634. + * (at your option) any later version.
  1635. + *
  1636. + * This program is distributed in the hope that it will be useful,
  1637. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1638. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1639. + * GNU General Public License for more details.
  1640. + *
  1641. + * You should have received a copy of the GNU General Public License
  1642. + * along with this program; if not, write to the Free Software
  1643. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1644. + */
  1645. +
  1646. +#include <linux/ipmi.h>
  1647. +#include <linux/list.h>
  1648. +#include <linux/slab.h>
  1649. +#include <linux/workqueue.h>
  1650. +
  1651. +/* SDR defs */
  1652. +#define STYPE_TEMP 0x01
  1653. +#define STYPE_VOLT 0x02
  1654. +#define STYPE_CURR 0x03
  1655. +#define STYPE_FAN 0x04
  1656. +
  1657. +#define SDR_LIMITS 8
  1658. +#define SDR_MAX_ID_LENGTH 16
  1659. +#define SDR_MAX_UNPACKED_ID_LENGTH ((SDR_MAX_ID_LENGTH * 4 / 3) + 2)
  1660. +
  1661. +/* the last sensor type we are interested in */
  1662. +#define STYPE_MAX 4
  1663. +
  1664. +#define IPMI_SDR_SIZE 67
  1665. +#define IPMI_CHUNK_SIZE 16
  1666. +
  1667. +#define MAX_FILENAME_LENGTH 30
  1668. +
  1669. +struct ipmisensors_device_attribute {
  1670. + struct device_attribute dev_attr;
  1671. + struct sdrdata *sdr;
  1672. +};
  1673. +#define to_ipmisensors_dev_attr(_dev_attr) \
  1674. + container_of(_dev_attr, struct ipmisensors_device_attribute, dev_attr)
  1675. +
  1676. +#define IPMISENSORS_DEVICE_ATTR(_name,_mode,_show,_store,_index) \
  1677. +struct ipmisensors_attribute sensor_dev_attr_##_name = { \
  1678. + .dev_attr = __ATTR(_name,_mode,_show,_store), \
  1679. + .index = _index, \
  1680. +}
  1681. +
  1682. +struct ipmisensors_bmc_device_attribute {
  1683. + struct device_attribute dev_attr;
  1684. + struct ipmisensors_bmc_data *bmc;
  1685. +};
  1686. +#define to_ipmisensors_bmc_dev_attr(_dev_attr) \
  1687. + container_of(_dev_attr, struct ipmisensors_bmc_device_attribute, dev_attr)
  1688. +
  1689. +/**
  1690. + * &struct_sdrdata stores the IPMI Sensor Data Record (SDR) data, as recieved from the BMC, along with the corresponding sysfs attributes
  1691. + */
  1692. +struct sdrdata {
  1693. + struct list_head list;
  1694. + /* retrieved from SDR, not expected to change */
  1695. + /* Sensor Type Code */
  1696. + u8 stype;
  1697. + u8 number;
  1698. + /* Sensor Capability Code */
  1699. + u8 capab;
  1700. + u16 thresh_mask;
  1701. + u8 format;
  1702. + u8 linear;
  1703. + s16 m;
  1704. + s16 b;
  1705. + u8 k;
  1706. + u8 nominal;
  1707. + u8 limits[SDR_LIMITS];
  1708. + /* index into limits for reported upper and lower limit */
  1709. + int lim1, lim2;
  1710. + u8 lim1_write, lim2_write;
  1711. + u8 string_type;
  1712. + u8 id_length;
  1713. + u8 id[SDR_MAX_ID_LENGTH];
  1714. + /* retrieved from reading */
  1715. + u8 reading;
  1716. + u8 status;
  1717. + u8 thresholds;
  1718. + /* sensor's bmc */
  1719. + struct ipmisensors_bmc_data *bmc;
  1720. + /* sysfs entries */
  1721. + struct ipmisensors_device_attribute attr;
  1722. + char *attr_name;
  1723. + struct ipmisensors_device_attribute attr_min;
  1724. + char *attr_min_name;
  1725. + struct ipmisensors_device_attribute attr_max;
  1726. + char *attr_max_name;
  1727. + struct ipmisensors_device_attribute attr_label;
  1728. + char *attr_label_name;
  1729. +
  1730. +};
  1731. +
  1732. +/**
  1733. + * &struct_ipmisensors_data stores the data for the ipmisensors driver.
  1734. + */
  1735. +struct ipmisensors_data {
  1736. + /* Driver struct */
  1737. + char *driver_name;
  1738. +
  1739. + /* Linked list of ipmisensors_bmc_data structs, one for each BMC */
  1740. + struct list_head bmc_data;
  1741. +
  1742. + /* Number of ipmi interfaces (and hence ipmisensors_data structs). */
  1743. + int interfaces;
  1744. +
  1745. + /* IPMI kernel interface - SMI watcher */
  1746. + struct ipmi_smi_watcher smi_watcher;
  1747. +
  1748. + /* IPMI kernel interface - user handlers */
  1749. + struct ipmi_user_hndl ipmi_hndlrs;
  1750. +
  1751. + /* Cache manager for sdrdata cache */
  1752. + struct kmem_cache *sdrdata_cache;
  1753. +
  1754. + /* Cache manager for ipmi_sensor_device_attribute cache */
  1755. + struct kmem_cache *sysfsattr_cache;
  1756. +};
  1757. +
  1758. +/**
  1759. + * &states: enumeration of state codes for a bmc specific ipmisensors
  1760. + */
  1761. +enum states {
  1762. + STATE_INIT,
  1763. + STATE_RESERVE,
  1764. + STATE_SDR,
  1765. + STATE_SDRPARTIAL,
  1766. + STATE_READING,
  1767. + STATE_UNCANCEL,
  1768. + STATE_SYSTABLE,
  1769. + STATE_DONE
  1770. +};
  1771. +
  1772. +/**
  1773. + * &struct_ipmisensors_bmc_data stores the data for a particular IPMI BMC.
  1774. + */
  1775. +struct ipmisensors_bmc_data {
  1776. + struct list_head list;
  1777. +
  1778. + /* The IPMI interface number */
  1779. + int interface_id;
  1780. +
  1781. + /* The IPMI address */
  1782. + struct ipmi_addr address;
  1783. +
  1784. + /* List of sdrdata structs (sdrs) recieved from the BMC */
  1785. + struct list_head sdrs;
  1786. +
  1787. + /* Count of the number of sdrs stored in the sdr list */
  1788. + int sdr_count;
  1789. +
  1790. + /* next message id */
  1791. + int msgid;
  1792. +
  1793. + /* The ipmi interface 'user' used to access this particular bmc */
  1794. + ipmi_user_t user;
  1795. +
  1796. + /* BMC IPMI Version (major) */
  1797. + unsigned char ipmi_version_major;
  1798. +
  1799. + /* BMC IPMI Version (minor) */
  1800. + unsigned char ipmi_version_minor;
  1801. +
  1802. + /* The size of the SDR request message */
  1803. + int ipmi_sdr_partial_size;
  1804. +
  1805. + /* transmit message buffer */
  1806. + struct kernel_ipmi_msg tx_message;
  1807. +
  1808. + /* ipmi transmited data buffer */
  1809. + unsigned char tx_msg_data[IPMI_MAX_MSG_LENGTH + 50]; /* why the +50 in bmcsensors? */
  1810. +
  1811. + /* ipmi recieved data buffer */
  1812. + unsigned char rx_msg_data[IPMI_MAX_MSG_LENGTH + 50];
  1813. +
  1814. + /* current recieve buffer offset */
  1815. + int rx_msg_data_offset;
  1816. +
  1817. + /* The id of then next SDR record to read during update cycle */
  1818. + u16 nextrecord;
  1819. +
  1820. + /* BMC SDR Reservation ID */
  1821. + u16 resid;
  1822. +
  1823. + /* Alarm status */
  1824. + u8 alarms;
  1825. +
  1826. + /* The cumalative error count for this bmc */
  1827. + int errorcount;
  1828. +
  1829. + /* The current state of this bmc w.r.t. ipmisensors (see enum states) */
  1830. + int state;
  1831. +
  1832. + /* The current sdr for which a reading is pending */
  1833. + struct sdrdata *current_sdr;
  1834. +
  1835. + /* The BMC's device struct */
  1836. + struct device *dev;
  1837. +
  1838. + /* hwmon class device */
  1839. + struct class_device *class_dev;
  1840. +
  1841. + /* hwmon device name */
  1842. + struct device_attribute name_attr;
  1843. +
  1844. + /* alarms attribute */
  1845. + struct ipmisensors_bmc_device_attribute alarms_attr;
  1846. +
  1847. + /* update_period attribute */
  1848. + struct ipmisensors_bmc_device_attribute update_attr;
  1849. +
  1850. + /* lower bound on time between updates (in seconds) */
  1851. + unsigned int update_period;
  1852. +
  1853. + /* semaphore used to do a headcount of the SDR readings we are waiting
  1854. + * on in a given bmc update */
  1855. + struct semaphore update_semaphore;
  1856. +
  1857. + /* bmc's work struct for updating sensors */
  1858. + struct delayed_work update_work;
  1859. +
  1860. + /* bmc's work struct for building the sysfs workqueue */
  1861. + struct work_struct sysfs_work;
  1862. +};
  1863. diff -rduNp linux-2.6.20.3.orig/include/linux/ipmi.h linux-2.6.20.3/include/linux/ipmi.h
  1864. --- linux-2.6.20.3.orig/include/linux/ipmi.h 2007-03-13 19:27:08.000000000 +0100
  1865. +++ linux-2.6.20.3/include/linux/ipmi.h 2007-03-14 14:23:02.000000000 +0100
  1866. @@ -300,6 +300,9 @@ int ipmi_create_user(unsigned int
  1867. safe, too. */
  1868. int ipmi_destroy_user(ipmi_user_t user);
  1869. +/* Get the IPMI BMC's device struct */
  1870. +struct device *ipmi_get_bmcdevice(int ipmi_intf);
  1871. +
  1872. /* Get the IPMI version of the BMC we are talking to. */
  1873. void ipmi_get_version(ipmi_user_t user,
  1874. unsigned char *major,
  1875. diff -rduNp linux-2.6.20.3.orig/include/linux/ipmi_msgdefs.h linux-2.6.20.3/include/linux/ipmi_msgdefs.h
  1876. --- linux-2.6.20.3.orig/include/linux/ipmi_msgdefs.h 2007-03-13 19:27:08.000000000 +0100
  1877. +++ linux-2.6.20.3/include/linux/ipmi_msgdefs.h 2007-03-14 14:23:02.000000000 +0100
  1878. @@ -45,6 +45,7 @@
  1879. #define IPMI_NETFN_APP_REQUEST 0x06
  1880. #define IPMI_NETFN_APP_RESPONSE 0x07
  1881. +#define IPMI_GET_DEVICE_GUID_CMD 0x08
  1882. #define IPMI_GET_DEVICE_ID_CMD 0x01
  1883. #define IPMI_COLD_RESET_CMD 0x02
  1884. #define IPMI_WARM_RESET_CMD 0x03
  1885. @@ -57,6 +58,11 @@
  1886. #define IPMI_GET_BMC_GLOBAL_ENABLES_CMD 0x2f
  1887. #define IPMI_READ_EVENT_MSG_BUFFER_CMD 0x35
  1888. #define IPMI_GET_CHANNEL_INFO_CMD 0x42
  1889. +#define IPMI_RESERVE_SDR 0x22
  1890. +#define IPMI_GET_SDR 0x23
  1891. +#define IPMI_GET_SENSOR_STATE_READING 0x2D
  1892. +#define IPMI_SET_SENSOR_HYSTERESIS 0x24
  1893. +#define IPMI_SET_SENSOR_THRESHOLD 0x26
  1894. #define IPMI_NETFN_STORAGE_REQUEST 0x0a
  1895. #define IPMI_NETFN_STORAGE_RESPONSE 0x0b
  1896. @@ -79,10 +85,13 @@
  1897. #define IPMI_NODE_BUSY_ERR 0xc0
  1898. #define IPMI_INVALID_COMMAND_ERR 0xc1
  1899. #define IPMI_TIMEOUT_ERR 0xc3
  1900. +#define IPMI_INVALID_RESERVATION_ID 0xc5
  1901. #define IPMI_ERR_MSG_TRUNCATED 0xc6
  1902. #define IPMI_REQ_LEN_INVALID_ERR 0xc7
  1903. #define IPMI_REQ_LEN_EXCEEDED_ERR 0xc8
  1904. #define IPMI_NOT_IN_MY_STATE_ERR 0xd5 /* IPMI 2.0 */
  1905. +#define IPMI_ERR_RETURNING_REQ_BYTES 0xca
  1906. +#define IPMI_ERR_PROVIDING_RESPONSE 0xce
  1907. #define IPMI_LOST_ARBITRATION_ERR 0x81
  1908. #define IPMI_BUS_ERR 0x82
  1909. #define IPMI_NAK_ON_WRITE_ERR 0x83