ipmi_ipmb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver to talk to a remote management controller on IPMB.
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/errno.h>
  7. #include <linux/i2c.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/poll.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/semaphore.h>
  15. #include <linux/kthread.h>
  16. #include <linux/wait.h>
  17. #include <linux/ipmi_msgdefs.h>
  18. #include <linux/ipmi_smi.h>
  19. #define DEVICE_NAME "ipmi-ipmb"
  20. static int bmcaddr = 0x20;
  21. module_param(bmcaddr, int, 0644);
  22. MODULE_PARM_DESC(bmcaddr, "Address to use for BMC.");
  23. static unsigned int retry_time_ms = 250;
  24. module_param(retry_time_ms, uint, 0644);
  25. MODULE_PARM_DESC(retry_time_ms, "Timeout time between retries, in milliseconds.");
  26. static unsigned int max_retries = 1;
  27. module_param(max_retries, uint, 0644);
  28. MODULE_PARM_DESC(max_retries, "Max resends of a command before timing out.");
  29. /* Add room for the two slave addresses, two checksums, and rqSeq. */
  30. #define IPMB_MAX_MSG_LEN (IPMI_MAX_MSG_LENGTH + 5)
  31. struct ipmi_ipmb_dev {
  32. struct ipmi_smi *intf;
  33. struct i2c_client *client;
  34. struct i2c_client *slave;
  35. struct ipmi_smi_handlers handlers;
  36. bool ready;
  37. u8 curr_seq;
  38. u8 bmcaddr;
  39. u32 retry_time_ms;
  40. u32 max_retries;
  41. struct ipmi_smi_msg *next_msg;
  42. struct ipmi_smi_msg *working_msg;
  43. /* Transmit thread. */
  44. struct task_struct *thread;
  45. struct semaphore wake_thread;
  46. struct semaphore got_rsp;
  47. spinlock_t lock;
  48. bool stopping;
  49. u8 xmitmsg[IPMB_MAX_MSG_LEN];
  50. unsigned int xmitlen;
  51. u8 rcvmsg[IPMB_MAX_MSG_LEN];
  52. unsigned int rcvlen;
  53. bool overrun;
  54. };
  55. static bool valid_ipmb(struct ipmi_ipmb_dev *iidev)
  56. {
  57. u8 *msg = iidev->rcvmsg;
  58. u8 netfn;
  59. if (iidev->overrun)
  60. return false;
  61. /* Minimum message size. */
  62. if (iidev->rcvlen < 7)
  63. return false;
  64. /* Is it a response? */
  65. netfn = msg[1] >> 2;
  66. if (netfn & 1) {
  67. /* Response messages have an added completion code. */
  68. if (iidev->rcvlen < 8)
  69. return false;
  70. }
  71. if (ipmb_checksum(msg, 3) != 0)
  72. return false;
  73. if (ipmb_checksum(msg + 3, iidev->rcvlen - 3) != 0)
  74. return false;
  75. return true;
  76. }
  77. static void ipmi_ipmb_check_msg_done(struct ipmi_ipmb_dev *iidev)
  78. {
  79. struct ipmi_smi_msg *imsg = NULL;
  80. u8 *msg = iidev->rcvmsg;
  81. bool is_cmd;
  82. unsigned long flags;
  83. if (iidev->rcvlen == 0)
  84. return;
  85. if (!valid_ipmb(iidev))
  86. goto done;
  87. is_cmd = ((msg[1] >> 2) & 1) == 0;
  88. if (is_cmd) {
  89. /* Ignore commands until we are up. */
  90. if (!iidev->ready)
  91. goto done;
  92. /* It's a command, allocate a message for it. */
  93. imsg = ipmi_alloc_smi_msg();
  94. if (!imsg)
  95. goto done;
  96. imsg->type = IPMI_SMI_MSG_TYPE_IPMB_DIRECT;
  97. imsg->data_size = 0;
  98. } else {
  99. spin_lock_irqsave(&iidev->lock, flags);
  100. if (iidev->working_msg) {
  101. u8 seq = msg[4] >> 2;
  102. bool xmit_rsp = (iidev->working_msg->data[0] >> 2) & 1;
  103. /*
  104. * Responses should carry the sequence we sent
  105. * them with. If it's a transmitted response,
  106. * ignore it. And if the message hasn't been
  107. * transmitted, ignore it.
  108. */
  109. if (!xmit_rsp && seq == iidev->curr_seq) {
  110. iidev->curr_seq = (iidev->curr_seq + 1) & 0x3f;
  111. imsg = iidev->working_msg;
  112. iidev->working_msg = NULL;
  113. }
  114. }
  115. spin_unlock_irqrestore(&iidev->lock, flags);
  116. }
  117. if (!imsg)
  118. goto done;
  119. if (imsg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  120. imsg->rsp[0] = msg[1]; /* NetFn/LUN */
  121. /*
  122. * Keep the source address, rqSeq. Drop the trailing
  123. * checksum.
  124. */
  125. memcpy(imsg->rsp + 1, msg + 3, iidev->rcvlen - 4);
  126. imsg->rsp_size = iidev->rcvlen - 3;
  127. } else {
  128. imsg->rsp[0] = msg[1]; /* NetFn/LUN */
  129. /*
  130. * Skip the source address, rqSeq. Drop the trailing
  131. * checksum.
  132. */
  133. memcpy(imsg->rsp + 1, msg + 5, iidev->rcvlen - 6);
  134. imsg->rsp_size = iidev->rcvlen - 5;
  135. }
  136. ipmi_smi_msg_received(iidev->intf, imsg);
  137. if (!is_cmd)
  138. up(&iidev->got_rsp);
  139. done:
  140. iidev->overrun = false;
  141. iidev->rcvlen = 0;
  142. }
  143. /*
  144. * The IPMB protocol only supports i2c writes so there is no need to
  145. * support I2C_SLAVE_READ* events, except to know if the other end has
  146. * issued a read without going to stop mode.
  147. */
  148. static int ipmi_ipmb_slave_cb(struct i2c_client *client,
  149. enum i2c_slave_event event, u8 *val)
  150. {
  151. struct ipmi_ipmb_dev *iidev = i2c_get_clientdata(client);
  152. switch (event) {
  153. case I2C_SLAVE_WRITE_REQUESTED:
  154. ipmi_ipmb_check_msg_done(iidev);
  155. /*
  156. * First byte is the slave address, to ease the checksum
  157. * calculation.
  158. */
  159. iidev->rcvmsg[0] = client->addr << 1;
  160. iidev->rcvlen = 1;
  161. break;
  162. case I2C_SLAVE_WRITE_RECEIVED:
  163. if (iidev->rcvlen >= sizeof(iidev->rcvmsg))
  164. iidev->overrun = true;
  165. else
  166. iidev->rcvmsg[iidev->rcvlen++] = *val;
  167. break;
  168. case I2C_SLAVE_READ_REQUESTED:
  169. case I2C_SLAVE_STOP:
  170. ipmi_ipmb_check_msg_done(iidev);
  171. break;
  172. case I2C_SLAVE_READ_PROCESSED:
  173. break;
  174. }
  175. return 0;
  176. }
  177. static void ipmi_ipmb_send_response(struct ipmi_ipmb_dev *iidev,
  178. struct ipmi_smi_msg *msg, u8 cc)
  179. {
  180. if ((msg->data[0] >> 2) & 1) {
  181. /*
  182. * It's a response being sent, we need to return a
  183. * response to the response. Fake a send msg command
  184. * response with channel 0. This will always be ipmb
  185. * direct.
  186. */
  187. msg->data[0] = (IPMI_NETFN_APP_REQUEST | 1) << 2;
  188. msg->data[3] = IPMI_SEND_MSG_CMD;
  189. msg->data[4] = cc;
  190. msg->data_size = 5;
  191. }
  192. msg->rsp[0] = msg->data[0] | (1 << 2);
  193. if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  194. msg->rsp[1] = msg->data[1];
  195. msg->rsp[2] = msg->data[2];
  196. msg->rsp[3] = msg->data[3];
  197. msg->rsp[4] = cc;
  198. msg->rsp_size = 5;
  199. } else {
  200. msg->rsp[1] = msg->data[1];
  201. msg->rsp[2] = cc;
  202. msg->rsp_size = 3;
  203. }
  204. ipmi_smi_msg_received(iidev->intf, msg);
  205. }
  206. static void ipmi_ipmb_format_for_xmit(struct ipmi_ipmb_dev *iidev,
  207. struct ipmi_smi_msg *msg)
  208. {
  209. if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  210. iidev->xmitmsg[0] = msg->data[1];
  211. iidev->xmitmsg[1] = msg->data[0];
  212. memcpy(iidev->xmitmsg + 4, msg->data + 2, msg->data_size - 2);
  213. iidev->xmitlen = msg->data_size + 2;
  214. } else {
  215. iidev->xmitmsg[0] = iidev->bmcaddr;
  216. iidev->xmitmsg[1] = msg->data[0];
  217. iidev->xmitmsg[4] = 0;
  218. memcpy(iidev->xmitmsg + 5, msg->data + 1, msg->data_size - 1);
  219. iidev->xmitlen = msg->data_size + 4;
  220. }
  221. iidev->xmitmsg[3] = iidev->slave->addr << 1;
  222. if (((msg->data[0] >> 2) & 1) == 0)
  223. /* If it's a command, put in our own sequence number. */
  224. iidev->xmitmsg[4] = ((iidev->xmitmsg[4] & 0x03) |
  225. (iidev->curr_seq << 2));
  226. /* Now add on the final checksums. */
  227. iidev->xmitmsg[2] = ipmb_checksum(iidev->xmitmsg, 2);
  228. iidev->xmitmsg[iidev->xmitlen] =
  229. ipmb_checksum(iidev->xmitmsg + 3, iidev->xmitlen - 3);
  230. iidev->xmitlen++;
  231. }
  232. static int ipmi_ipmb_thread(void *data)
  233. {
  234. struct ipmi_ipmb_dev *iidev = data;
  235. while (!kthread_should_stop()) {
  236. long ret;
  237. struct i2c_msg i2c_msg;
  238. struct ipmi_smi_msg *msg = NULL;
  239. unsigned long flags;
  240. unsigned int retries = 0;
  241. /* Wait for a message to send */
  242. ret = down_interruptible(&iidev->wake_thread);
  243. if (iidev->stopping)
  244. break;
  245. if (ret)
  246. continue;
  247. spin_lock_irqsave(&iidev->lock, flags);
  248. if (iidev->next_msg) {
  249. msg = iidev->next_msg;
  250. iidev->next_msg = NULL;
  251. }
  252. spin_unlock_irqrestore(&iidev->lock, flags);
  253. if (!msg)
  254. continue;
  255. ipmi_ipmb_format_for_xmit(iidev, msg);
  256. retry:
  257. i2c_msg.len = iidev->xmitlen - 1;
  258. if (i2c_msg.len > 32) {
  259. ipmi_ipmb_send_response(iidev, msg,
  260. IPMI_REQ_LEN_EXCEEDED_ERR);
  261. continue;
  262. }
  263. i2c_msg.addr = iidev->xmitmsg[0] >> 1;
  264. i2c_msg.flags = 0;
  265. i2c_msg.buf = iidev->xmitmsg + 1;
  266. /* Rely on i2c_transfer for a barrier. */
  267. iidev->working_msg = msg;
  268. ret = i2c_transfer(iidev->client->adapter, &i2c_msg, 1);
  269. if ((msg->data[0] >> 2) & 1) {
  270. /*
  271. * It's a response, nothing will be returned
  272. * by the other end.
  273. */
  274. iidev->working_msg = NULL;
  275. ipmi_ipmb_send_response(iidev, msg,
  276. ret < 0 ? IPMI_BUS_ERR : 0);
  277. continue;
  278. }
  279. if (ret < 0) {
  280. iidev->working_msg = NULL;
  281. ipmi_ipmb_send_response(iidev, msg, IPMI_BUS_ERR);
  282. continue;
  283. }
  284. /* A command was sent, wait for its response. */
  285. ret = down_timeout(&iidev->got_rsp,
  286. msecs_to_jiffies(iidev->retry_time_ms));
  287. /*
  288. * Grab the message if we can. If the handler hasn't
  289. * already handled it, the message will still be there.
  290. */
  291. spin_lock_irqsave(&iidev->lock, flags);
  292. msg = iidev->working_msg;
  293. iidev->working_msg = NULL;
  294. spin_unlock_irqrestore(&iidev->lock, flags);
  295. if (!msg && ret) {
  296. /*
  297. * If working_msg is not set and we timed out,
  298. * that means the message grabbed by
  299. * check_msg_done before we could grab it
  300. * here. Wait again for check_msg_done to up
  301. * the semaphore.
  302. */
  303. down(&iidev->got_rsp);
  304. } else if (msg && ++retries <= iidev->max_retries) {
  305. spin_lock_irqsave(&iidev->lock, flags);
  306. iidev->working_msg = msg;
  307. spin_unlock_irqrestore(&iidev->lock, flags);
  308. goto retry;
  309. }
  310. if (msg)
  311. ipmi_ipmb_send_response(iidev, msg, IPMI_TIMEOUT_ERR);
  312. }
  313. if (iidev->next_msg)
  314. /* Return an unspecified error. */
  315. ipmi_ipmb_send_response(iidev, iidev->next_msg, 0xff);
  316. return 0;
  317. }
  318. static int ipmi_ipmb_start_processing(void *send_info,
  319. struct ipmi_smi *new_intf)
  320. {
  321. struct ipmi_ipmb_dev *iidev = send_info;
  322. iidev->intf = new_intf;
  323. iidev->ready = true;
  324. return 0;
  325. }
  326. static void ipmi_ipmb_stop_thread(struct ipmi_ipmb_dev *iidev)
  327. {
  328. if (iidev->thread) {
  329. struct task_struct *t = iidev->thread;
  330. iidev->thread = NULL;
  331. iidev->stopping = true;
  332. up(&iidev->wake_thread);
  333. up(&iidev->got_rsp);
  334. kthread_stop(t);
  335. }
  336. }
  337. static void ipmi_ipmb_shutdown(void *send_info)
  338. {
  339. struct ipmi_ipmb_dev *iidev = send_info;
  340. ipmi_ipmb_stop_thread(iidev);
  341. }
  342. static void ipmi_ipmb_sender(void *send_info,
  343. struct ipmi_smi_msg *msg)
  344. {
  345. struct ipmi_ipmb_dev *iidev = send_info;
  346. unsigned long flags;
  347. spin_lock_irqsave(&iidev->lock, flags);
  348. BUG_ON(iidev->next_msg);
  349. iidev->next_msg = msg;
  350. spin_unlock_irqrestore(&iidev->lock, flags);
  351. up(&iidev->wake_thread);
  352. }
  353. static void ipmi_ipmb_request_events(void *send_info)
  354. {
  355. /* We don't fetch events here. */
  356. }
  357. static void ipmi_ipmb_cleanup(struct ipmi_ipmb_dev *iidev)
  358. {
  359. if (iidev->slave) {
  360. i2c_slave_unregister(iidev->slave);
  361. if (iidev->slave != iidev->client)
  362. i2c_unregister_device(iidev->slave);
  363. }
  364. iidev->slave = NULL;
  365. iidev->client = NULL;
  366. ipmi_ipmb_stop_thread(iidev);
  367. }
  368. static void ipmi_ipmb_remove(struct i2c_client *client)
  369. {
  370. struct ipmi_ipmb_dev *iidev = i2c_get_clientdata(client);
  371. ipmi_ipmb_cleanup(iidev);
  372. ipmi_unregister_smi(iidev->intf);
  373. }
  374. static int ipmi_ipmb_probe(struct i2c_client *client)
  375. {
  376. struct device *dev = &client->dev;
  377. struct ipmi_ipmb_dev *iidev;
  378. struct device_node *slave_np;
  379. struct i2c_adapter *slave_adap = NULL;
  380. struct i2c_client *slave = NULL;
  381. int rv;
  382. iidev = devm_kzalloc(&client->dev, sizeof(*iidev), GFP_KERNEL);
  383. if (!iidev)
  384. return -ENOMEM;
  385. if (of_property_read_u8(dev->of_node, "bmcaddr", &iidev->bmcaddr) != 0)
  386. iidev->bmcaddr = bmcaddr;
  387. if (iidev->bmcaddr == 0 || iidev->bmcaddr & 1) {
  388. /* Can't have the write bit set. */
  389. dev_notice(&client->dev,
  390. "Invalid bmc address value %2.2x\n", iidev->bmcaddr);
  391. return -EINVAL;
  392. }
  393. if (of_property_read_u32(dev->of_node, "retry-time",
  394. &iidev->retry_time_ms) != 0)
  395. iidev->retry_time_ms = retry_time_ms;
  396. if (of_property_read_u32(dev->of_node, "max-retries",
  397. &iidev->max_retries) != 0)
  398. iidev->max_retries = max_retries;
  399. slave_np = of_parse_phandle(dev->of_node, "slave-dev", 0);
  400. if (slave_np) {
  401. slave_adap = of_get_i2c_adapter_by_node(slave_np);
  402. of_node_put(slave_np);
  403. if (!slave_adap) {
  404. dev_notice(&client->dev,
  405. "Could not find slave adapter\n");
  406. return -EINVAL;
  407. }
  408. }
  409. iidev->client = client;
  410. if (slave_adap) {
  411. struct i2c_board_info binfo;
  412. memset(&binfo, 0, sizeof(binfo));
  413. strscpy(binfo.type, "ipmb-slave", I2C_NAME_SIZE);
  414. binfo.addr = client->addr;
  415. binfo.flags = I2C_CLIENT_SLAVE;
  416. slave = i2c_new_client_device(slave_adap, &binfo);
  417. i2c_put_adapter(slave_adap);
  418. if (IS_ERR(slave)) {
  419. rv = PTR_ERR(slave);
  420. dev_notice(&client->dev,
  421. "Could not allocate slave device: %d\n", rv);
  422. return rv;
  423. }
  424. i2c_set_clientdata(slave, iidev);
  425. } else {
  426. slave = client;
  427. }
  428. i2c_set_clientdata(client, iidev);
  429. slave->flags |= I2C_CLIENT_SLAVE;
  430. rv = i2c_slave_register(slave, ipmi_ipmb_slave_cb);
  431. if (rv)
  432. goto out_err;
  433. iidev->slave = slave;
  434. slave = NULL;
  435. iidev->handlers.flags = IPMI_SMI_CAN_HANDLE_IPMB_DIRECT;
  436. iidev->handlers.start_processing = ipmi_ipmb_start_processing;
  437. iidev->handlers.shutdown = ipmi_ipmb_shutdown;
  438. iidev->handlers.sender = ipmi_ipmb_sender;
  439. iidev->handlers.request_events = ipmi_ipmb_request_events;
  440. spin_lock_init(&iidev->lock);
  441. sema_init(&iidev->wake_thread, 0);
  442. sema_init(&iidev->got_rsp, 0);
  443. iidev->thread = kthread_run(ipmi_ipmb_thread, iidev,
  444. "kipmb%4.4x", client->addr);
  445. if (IS_ERR(iidev->thread)) {
  446. rv = PTR_ERR(iidev->thread);
  447. dev_notice(&client->dev,
  448. "Could not start kernel thread: error %d\n", rv);
  449. goto out_err;
  450. }
  451. rv = ipmi_register_smi(&iidev->handlers,
  452. iidev,
  453. &client->dev,
  454. iidev->bmcaddr);
  455. if (rv)
  456. goto out_err;
  457. return 0;
  458. out_err:
  459. if (slave && slave != client)
  460. i2c_unregister_device(slave);
  461. ipmi_ipmb_cleanup(iidev);
  462. return rv;
  463. }
  464. #ifdef CONFIG_OF
  465. static const struct of_device_id of_ipmi_ipmb_match[] = {
  466. { .type = "ipmi", .compatible = DEVICE_NAME },
  467. {},
  468. };
  469. MODULE_DEVICE_TABLE(of, of_ipmi_ipmb_match);
  470. #else
  471. #define of_ipmi_ipmb_match NULL
  472. #endif
  473. static const struct i2c_device_id ipmi_ipmb_id[] = {
  474. { DEVICE_NAME, 0 },
  475. {},
  476. };
  477. MODULE_DEVICE_TABLE(i2c, ipmi_ipmb_id);
  478. static struct i2c_driver ipmi_ipmb_driver = {
  479. .class = I2C_CLASS_HWMON,
  480. .driver = {
  481. .name = DEVICE_NAME,
  482. .of_match_table = of_ipmi_ipmb_match,
  483. },
  484. .probe_new = ipmi_ipmb_probe,
  485. .remove = ipmi_ipmb_remove,
  486. .id_table = ipmi_ipmb_id,
  487. };
  488. module_i2c_driver(ipmi_ipmb_driver);
  489. MODULE_AUTHOR("Corey Minyard");
  490. MODULE_DESCRIPTION("IPMI IPMB driver");
  491. MODULE_LICENSE("GPL v2");