ipmi_ipmb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. *val = 0xff;
  170. ipmi_ipmb_check_msg_done(iidev);
  171. break;
  172. case I2C_SLAVE_STOP:
  173. ipmi_ipmb_check_msg_done(iidev);
  174. break;
  175. case I2C_SLAVE_READ_PROCESSED:
  176. *val = 0xff;
  177. break;
  178. }
  179. return 0;
  180. }
  181. static void ipmi_ipmb_send_response(struct ipmi_ipmb_dev *iidev,
  182. struct ipmi_smi_msg *msg, u8 cc)
  183. {
  184. if ((msg->data[0] >> 2) & 1) {
  185. /*
  186. * It's a response being sent, we need to return a
  187. * response to the response. Fake a send msg command
  188. * response with channel 0. This will always be ipmb
  189. * direct.
  190. */
  191. msg->data[0] = (IPMI_NETFN_APP_REQUEST | 1) << 2;
  192. msg->data[3] = IPMI_SEND_MSG_CMD;
  193. msg->data[4] = cc;
  194. msg->data_size = 5;
  195. }
  196. msg->rsp[0] = msg->data[0] | (1 << 2);
  197. if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  198. msg->rsp[1] = msg->data[1];
  199. msg->rsp[2] = msg->data[2];
  200. msg->rsp[3] = msg->data[3];
  201. msg->rsp[4] = cc;
  202. msg->rsp_size = 5;
  203. } else {
  204. msg->rsp[1] = msg->data[1];
  205. msg->rsp[2] = cc;
  206. msg->rsp_size = 3;
  207. }
  208. ipmi_smi_msg_received(iidev->intf, msg);
  209. }
  210. static void ipmi_ipmb_format_for_xmit(struct ipmi_ipmb_dev *iidev,
  211. struct ipmi_smi_msg *msg)
  212. {
  213. if (msg->type == IPMI_SMI_MSG_TYPE_IPMB_DIRECT) {
  214. iidev->xmitmsg[0] = msg->data[1];
  215. iidev->xmitmsg[1] = msg->data[0];
  216. memcpy(iidev->xmitmsg + 4, msg->data + 2, msg->data_size - 2);
  217. iidev->xmitlen = msg->data_size + 2;
  218. } else {
  219. iidev->xmitmsg[0] = iidev->bmcaddr;
  220. iidev->xmitmsg[1] = msg->data[0];
  221. iidev->xmitmsg[4] = 0;
  222. memcpy(iidev->xmitmsg + 5, msg->data + 1, msg->data_size - 1);
  223. iidev->xmitlen = msg->data_size + 4;
  224. }
  225. iidev->xmitmsg[3] = iidev->slave->addr << 1;
  226. if (((msg->data[0] >> 2) & 1) == 0)
  227. /* If it's a command, put in our own sequence number. */
  228. iidev->xmitmsg[4] = ((iidev->xmitmsg[4] & 0x03) |
  229. (iidev->curr_seq << 2));
  230. /* Now add on the final checksums. */
  231. iidev->xmitmsg[2] = ipmb_checksum(iidev->xmitmsg, 2);
  232. iidev->xmitmsg[iidev->xmitlen] =
  233. ipmb_checksum(iidev->xmitmsg + 3, iidev->xmitlen - 3);
  234. iidev->xmitlen++;
  235. }
  236. static int ipmi_ipmb_thread(void *data)
  237. {
  238. struct ipmi_ipmb_dev *iidev = data;
  239. while (!kthread_should_stop()) {
  240. long ret;
  241. struct i2c_msg i2c_msg;
  242. struct ipmi_smi_msg *msg = NULL;
  243. unsigned long flags;
  244. unsigned int retries = 0;
  245. /* Wait for a message to send */
  246. ret = down_interruptible(&iidev->wake_thread);
  247. if (iidev->stopping)
  248. break;
  249. if (ret)
  250. continue;
  251. spin_lock_irqsave(&iidev->lock, flags);
  252. if (iidev->next_msg) {
  253. msg = iidev->next_msg;
  254. iidev->next_msg = NULL;
  255. }
  256. spin_unlock_irqrestore(&iidev->lock, flags);
  257. if (!msg)
  258. continue;
  259. ipmi_ipmb_format_for_xmit(iidev, msg);
  260. retry:
  261. i2c_msg.len = iidev->xmitlen - 1;
  262. if (i2c_msg.len > 32) {
  263. ipmi_ipmb_send_response(iidev, msg,
  264. IPMI_REQ_LEN_EXCEEDED_ERR);
  265. continue;
  266. }
  267. i2c_msg.addr = iidev->xmitmsg[0] >> 1;
  268. i2c_msg.flags = 0;
  269. i2c_msg.buf = iidev->xmitmsg + 1;
  270. /* Rely on i2c_transfer for a barrier. */
  271. iidev->working_msg = msg;
  272. ret = i2c_transfer(iidev->client->adapter, &i2c_msg, 1);
  273. if ((msg->data[0] >> 2) & 1) {
  274. /*
  275. * It's a response, nothing will be returned
  276. * by the other end.
  277. */
  278. iidev->working_msg = NULL;
  279. ipmi_ipmb_send_response(iidev, msg,
  280. ret < 0 ? IPMI_BUS_ERR : 0);
  281. continue;
  282. }
  283. if (ret < 0) {
  284. iidev->working_msg = NULL;
  285. ipmi_ipmb_send_response(iidev, msg, IPMI_BUS_ERR);
  286. continue;
  287. }
  288. /* A command was sent, wait for its response. */
  289. ret = down_timeout(&iidev->got_rsp,
  290. msecs_to_jiffies(iidev->retry_time_ms));
  291. /*
  292. * Grab the message if we can. If the handler hasn't
  293. * already handled it, the message will still be there.
  294. */
  295. spin_lock_irqsave(&iidev->lock, flags);
  296. msg = iidev->working_msg;
  297. iidev->working_msg = NULL;
  298. spin_unlock_irqrestore(&iidev->lock, flags);
  299. if (!msg && ret) {
  300. /*
  301. * If working_msg is not set and we timed out,
  302. * that means the message grabbed by
  303. * check_msg_done before we could grab it
  304. * here. Wait again for check_msg_done to up
  305. * the semaphore.
  306. */
  307. down(&iidev->got_rsp);
  308. } else if (msg && ++retries <= iidev->max_retries) {
  309. spin_lock_irqsave(&iidev->lock, flags);
  310. iidev->working_msg = msg;
  311. spin_unlock_irqrestore(&iidev->lock, flags);
  312. goto retry;
  313. }
  314. if (msg)
  315. ipmi_ipmb_send_response(iidev, msg, IPMI_TIMEOUT_ERR);
  316. }
  317. if (iidev->next_msg)
  318. /* Return an unspecified error. */
  319. ipmi_ipmb_send_response(iidev, iidev->next_msg, 0xff);
  320. return 0;
  321. }
  322. static int ipmi_ipmb_start_processing(void *send_info,
  323. struct ipmi_smi *new_intf)
  324. {
  325. struct ipmi_ipmb_dev *iidev = send_info;
  326. iidev->intf = new_intf;
  327. iidev->ready = true;
  328. return 0;
  329. }
  330. static void ipmi_ipmb_stop_thread(struct ipmi_ipmb_dev *iidev)
  331. {
  332. if (iidev->thread) {
  333. struct task_struct *t = iidev->thread;
  334. iidev->thread = NULL;
  335. iidev->stopping = true;
  336. up(&iidev->wake_thread);
  337. up(&iidev->got_rsp);
  338. kthread_stop(t);
  339. }
  340. }
  341. static void ipmi_ipmb_shutdown(void *send_info)
  342. {
  343. struct ipmi_ipmb_dev *iidev = send_info;
  344. ipmi_ipmb_stop_thread(iidev);
  345. }
  346. static int ipmi_ipmb_sender(void *send_info, struct ipmi_smi_msg *msg)
  347. {
  348. struct ipmi_ipmb_dev *iidev = send_info;
  349. unsigned long flags;
  350. spin_lock_irqsave(&iidev->lock, flags);
  351. BUG_ON(iidev->next_msg);
  352. iidev->next_msg = msg;
  353. spin_unlock_irqrestore(&iidev->lock, flags);
  354. up(&iidev->wake_thread);
  355. return IPMI_CC_NO_ERROR;
  356. }
  357. static void ipmi_ipmb_request_events(void *send_info)
  358. {
  359. /* We don't fetch events here. */
  360. }
  361. static void ipmi_ipmb_cleanup(struct ipmi_ipmb_dev *iidev)
  362. {
  363. if (iidev->slave) {
  364. i2c_slave_unregister(iidev->slave);
  365. if (iidev->slave != iidev->client)
  366. i2c_unregister_device(iidev->slave);
  367. }
  368. iidev->slave = NULL;
  369. iidev->client = NULL;
  370. ipmi_ipmb_stop_thread(iidev);
  371. }
  372. static void ipmi_ipmb_remove(struct i2c_client *client)
  373. {
  374. struct ipmi_ipmb_dev *iidev = i2c_get_clientdata(client);
  375. ipmi_ipmb_cleanup(iidev);
  376. ipmi_unregister_smi(iidev->intf);
  377. }
  378. static int ipmi_ipmb_probe(struct i2c_client *client)
  379. {
  380. struct device *dev = &client->dev;
  381. struct ipmi_ipmb_dev *iidev;
  382. struct device_node *slave_np;
  383. struct i2c_adapter *slave_adap = NULL;
  384. struct i2c_client *slave = NULL;
  385. int rv;
  386. iidev = devm_kzalloc(&client->dev, sizeof(*iidev), GFP_KERNEL);
  387. if (!iidev)
  388. return -ENOMEM;
  389. if (of_property_read_u8(dev->of_node, "bmcaddr", &iidev->bmcaddr) != 0)
  390. iidev->bmcaddr = bmcaddr;
  391. if (iidev->bmcaddr == 0 || iidev->bmcaddr & 1) {
  392. /* Can't have the write bit set. */
  393. dev_notice(&client->dev,
  394. "Invalid bmc address value %2.2x\n", iidev->bmcaddr);
  395. return -EINVAL;
  396. }
  397. if (of_property_read_u32(dev->of_node, "retry-time",
  398. &iidev->retry_time_ms) != 0)
  399. iidev->retry_time_ms = retry_time_ms;
  400. if (of_property_read_u32(dev->of_node, "max-retries",
  401. &iidev->max_retries) != 0)
  402. iidev->max_retries = max_retries;
  403. slave_np = of_parse_phandle(dev->of_node, "slave-dev", 0);
  404. if (slave_np) {
  405. slave_adap = of_get_i2c_adapter_by_node(slave_np);
  406. of_node_put(slave_np);
  407. if (!slave_adap) {
  408. dev_notice(&client->dev,
  409. "Could not find slave adapter\n");
  410. return -EINVAL;
  411. }
  412. }
  413. iidev->client = client;
  414. if (slave_adap) {
  415. struct i2c_board_info binfo;
  416. memset(&binfo, 0, sizeof(binfo));
  417. strscpy(binfo.type, "ipmb-slave", I2C_NAME_SIZE);
  418. binfo.addr = client->addr;
  419. binfo.flags = I2C_CLIENT_SLAVE;
  420. slave = i2c_new_client_device(slave_adap, &binfo);
  421. i2c_put_adapter(slave_adap);
  422. if (IS_ERR(slave)) {
  423. rv = PTR_ERR(slave);
  424. dev_notice(&client->dev,
  425. "Could not allocate slave device: %d\n", rv);
  426. return rv;
  427. }
  428. i2c_set_clientdata(slave, iidev);
  429. } else {
  430. slave = client;
  431. }
  432. i2c_set_clientdata(client, iidev);
  433. slave->flags |= I2C_CLIENT_SLAVE;
  434. rv = i2c_slave_register(slave, ipmi_ipmb_slave_cb);
  435. if (rv)
  436. goto out_err;
  437. iidev->slave = slave;
  438. slave = NULL;
  439. iidev->handlers.flags = IPMI_SMI_CAN_HANDLE_IPMB_DIRECT;
  440. iidev->handlers.start_processing = ipmi_ipmb_start_processing;
  441. iidev->handlers.shutdown = ipmi_ipmb_shutdown;
  442. iidev->handlers.sender = ipmi_ipmb_sender;
  443. iidev->handlers.request_events = ipmi_ipmb_request_events;
  444. spin_lock_init(&iidev->lock);
  445. sema_init(&iidev->wake_thread, 0);
  446. sema_init(&iidev->got_rsp, 0);
  447. iidev->thread = kthread_run(ipmi_ipmb_thread, iidev,
  448. "kipmb%4.4x", client->addr);
  449. if (IS_ERR(iidev->thread)) {
  450. rv = PTR_ERR(iidev->thread);
  451. dev_notice(&client->dev,
  452. "Could not start kernel thread: error %d\n", rv);
  453. goto out_err;
  454. }
  455. rv = ipmi_register_smi(&iidev->handlers,
  456. iidev,
  457. &client->dev,
  458. iidev->bmcaddr);
  459. if (rv)
  460. goto out_err;
  461. return 0;
  462. out_err:
  463. if (slave && slave != client)
  464. i2c_unregister_device(slave);
  465. ipmi_ipmb_cleanup(iidev);
  466. return rv;
  467. }
  468. #ifdef CONFIG_OF
  469. static const struct of_device_id of_ipmi_ipmb_match[] = {
  470. { .type = "ipmi", .compatible = DEVICE_NAME },
  471. {},
  472. };
  473. MODULE_DEVICE_TABLE(of, of_ipmi_ipmb_match);
  474. #else
  475. #define of_ipmi_ipmb_match NULL
  476. #endif
  477. static const struct i2c_device_id ipmi_ipmb_id[] = {
  478. { DEVICE_NAME },
  479. {}
  480. };
  481. MODULE_DEVICE_TABLE(i2c, ipmi_ipmb_id);
  482. static struct i2c_driver ipmi_ipmb_driver = {
  483. .class = I2C_CLASS_HWMON,
  484. .driver = {
  485. .name = DEVICE_NAME,
  486. .of_match_table = of_ipmi_ipmb_match,
  487. },
  488. .probe = ipmi_ipmb_probe,
  489. .remove = ipmi_ipmb_remove,
  490. .id_table = ipmi_ipmb_id,
  491. };
  492. module_i2c_driver(ipmi_ipmb_driver);
  493. MODULE_AUTHOR("Corey Minyard");
  494. MODULE_DESCRIPTION("IPMI IPMB driver");
  495. MODULE_LICENSE("GPL v2");