ipmb_dev_int.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IPMB driver to receive a request and send a response
  4. *
  5. * Copyright (C) 2019 Mellanox Techologies, Ltd.
  6. *
  7. * This was inspired by Brendan Higgins' ipmi-bmc-bt-i2c driver.
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/errno.h>
  11. #include <linux/i2c.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/poll.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/wait.h>
  19. #define MAX_MSG_LEN 240
  20. #define IPMB_REQUEST_LEN_MIN 7
  21. #define NETFN_RSP_BIT_MASK 0x4
  22. #define REQUEST_QUEUE_MAX_LEN 256
  23. #define IPMB_MSG_LEN_IDX 0
  24. #define RQ_SA_8BIT_IDX 1
  25. #define NETFN_LUN_IDX 2
  26. #define GET_7BIT_ADDR(addr_8bit) (addr_8bit >> 1)
  27. #define GET_8BIT_ADDR(addr_7bit) ((addr_7bit << 1) & 0xff)
  28. #define IPMB_MSG_PAYLOAD_LEN_MAX (MAX_MSG_LEN - IPMB_REQUEST_LEN_MIN - 1)
  29. #define SMBUS_MSG_HEADER_LENGTH 2
  30. #define SMBUS_MSG_IDX_OFFSET (SMBUS_MSG_HEADER_LENGTH + 1)
  31. struct ipmb_msg {
  32. u8 len;
  33. u8 rs_sa;
  34. u8 netfn_rs_lun;
  35. u8 checksum1;
  36. u8 rq_sa;
  37. u8 rq_seq_rq_lun;
  38. u8 cmd;
  39. u8 payload[IPMB_MSG_PAYLOAD_LEN_MAX];
  40. /* checksum2 is included in payload */
  41. } __packed;
  42. struct ipmb_request_elem {
  43. struct list_head list;
  44. struct ipmb_msg request;
  45. };
  46. struct ipmb_dev {
  47. struct i2c_client *client;
  48. struct miscdevice miscdev;
  49. struct ipmb_msg request;
  50. struct list_head request_queue;
  51. atomic_t request_queue_len;
  52. size_t msg_idx;
  53. spinlock_t lock;
  54. wait_queue_head_t wait_queue;
  55. struct mutex file_mutex;
  56. bool is_i2c_protocol;
  57. };
  58. static inline struct ipmb_dev *to_ipmb_dev(struct file *file)
  59. {
  60. return container_of(file->private_data, struct ipmb_dev, miscdev);
  61. }
  62. static ssize_t ipmb_read(struct file *file, char __user *buf, size_t count,
  63. loff_t *ppos)
  64. {
  65. struct ipmb_dev *ipmb_dev = to_ipmb_dev(file);
  66. struct ipmb_request_elem *queue_elem;
  67. struct ipmb_msg msg;
  68. ssize_t ret = 0;
  69. memset(&msg, 0, sizeof(msg));
  70. spin_lock_irq(&ipmb_dev->lock);
  71. while (list_empty(&ipmb_dev->request_queue)) {
  72. spin_unlock_irq(&ipmb_dev->lock);
  73. if (file->f_flags & O_NONBLOCK)
  74. return -EAGAIN;
  75. ret = wait_event_interruptible(ipmb_dev->wait_queue,
  76. !list_empty(&ipmb_dev->request_queue));
  77. if (ret)
  78. return ret;
  79. spin_lock_irq(&ipmb_dev->lock);
  80. }
  81. queue_elem = list_first_entry(&ipmb_dev->request_queue,
  82. struct ipmb_request_elem, list);
  83. memcpy(&msg, &queue_elem->request, sizeof(msg));
  84. list_del(&queue_elem->list);
  85. kfree(queue_elem);
  86. atomic_dec(&ipmb_dev->request_queue_len);
  87. spin_unlock_irq(&ipmb_dev->lock);
  88. count = min_t(size_t, count, msg.len + 1);
  89. if (copy_to_user(buf, &msg, count))
  90. ret = -EFAULT;
  91. return ret < 0 ? ret : count;
  92. }
  93. static int ipmb_i2c_write(struct i2c_client *client, u8 *msg, u8 addr)
  94. {
  95. struct i2c_msg i2c_msg;
  96. /*
  97. * subtract 1 byte (rq_sa) from the length of the msg passed to
  98. * raw i2c_transfer
  99. */
  100. i2c_msg.len = msg[IPMB_MSG_LEN_IDX] - 1;
  101. /* Assign message to buffer except first 2 bytes (length and address) */
  102. i2c_msg.buf = msg + 2;
  103. i2c_msg.addr = addr;
  104. i2c_msg.flags = client->flags & I2C_CLIENT_PEC;
  105. return i2c_transfer(client->adapter, &i2c_msg, 1);
  106. }
  107. static ssize_t ipmb_write(struct file *file, const char __user *buf,
  108. size_t count, loff_t *ppos)
  109. {
  110. struct ipmb_dev *ipmb_dev = to_ipmb_dev(file);
  111. u8 rq_sa, netf_rq_lun, msg_len;
  112. struct i2c_client *temp_client;
  113. u8 msg[MAX_MSG_LEN];
  114. ssize_t ret;
  115. if (count > sizeof(msg))
  116. return -EINVAL;
  117. if (copy_from_user(&msg, buf, count))
  118. return -EFAULT;
  119. if (count < msg[0])
  120. return -EINVAL;
  121. rq_sa = GET_7BIT_ADDR(msg[RQ_SA_8BIT_IDX]);
  122. netf_rq_lun = msg[NETFN_LUN_IDX];
  123. /* Check i2c block transfer vs smbus */
  124. if (ipmb_dev->is_i2c_protocol) {
  125. ret = ipmb_i2c_write(ipmb_dev->client, msg, rq_sa);
  126. return (ret == 1) ? count : ret;
  127. }
  128. /*
  129. * subtract rq_sa and netf_rq_lun from the length of the msg. Fill the
  130. * temporary client. Note that its use is an exception for IPMI.
  131. */
  132. msg_len = msg[IPMB_MSG_LEN_IDX] - SMBUS_MSG_HEADER_LENGTH;
  133. temp_client = kmemdup(ipmb_dev->client, sizeof(*temp_client), GFP_KERNEL);
  134. if (!temp_client)
  135. return -ENOMEM;
  136. temp_client->addr = rq_sa;
  137. ret = i2c_smbus_write_block_data(temp_client, netf_rq_lun, msg_len,
  138. msg + SMBUS_MSG_IDX_OFFSET);
  139. kfree(temp_client);
  140. return ret < 0 ? ret : count;
  141. }
  142. static __poll_t ipmb_poll(struct file *file, poll_table *wait)
  143. {
  144. struct ipmb_dev *ipmb_dev = to_ipmb_dev(file);
  145. __poll_t mask = EPOLLOUT;
  146. mutex_lock(&ipmb_dev->file_mutex);
  147. poll_wait(file, &ipmb_dev->wait_queue, wait);
  148. if (atomic_read(&ipmb_dev->request_queue_len))
  149. mask |= EPOLLIN;
  150. mutex_unlock(&ipmb_dev->file_mutex);
  151. return mask;
  152. }
  153. static const struct file_operations ipmb_fops = {
  154. .owner = THIS_MODULE,
  155. .read = ipmb_read,
  156. .write = ipmb_write,
  157. .poll = ipmb_poll,
  158. };
  159. /* Called with ipmb_dev->lock held. */
  160. static void ipmb_handle_request(struct ipmb_dev *ipmb_dev)
  161. {
  162. struct ipmb_request_elem *queue_elem;
  163. if (atomic_read(&ipmb_dev->request_queue_len) >=
  164. REQUEST_QUEUE_MAX_LEN)
  165. return;
  166. queue_elem = kmalloc(sizeof(*queue_elem), GFP_ATOMIC);
  167. if (!queue_elem)
  168. return;
  169. memcpy(&queue_elem->request, &ipmb_dev->request,
  170. sizeof(struct ipmb_msg));
  171. list_add(&queue_elem->list, &ipmb_dev->request_queue);
  172. atomic_inc(&ipmb_dev->request_queue_len);
  173. wake_up_all(&ipmb_dev->wait_queue);
  174. }
  175. static u8 ipmb_verify_checksum1(struct ipmb_dev *ipmb_dev, u8 rs_sa)
  176. {
  177. /* The 8 lsb of the sum is 0 when the checksum is valid */
  178. return (rs_sa + ipmb_dev->request.netfn_rs_lun +
  179. ipmb_dev->request.checksum1);
  180. }
  181. /*
  182. * Verify if message has proper ipmb header with minimum length
  183. * and correct checksum byte.
  184. */
  185. static bool is_ipmb_msg(struct ipmb_dev *ipmb_dev, u8 rs_sa)
  186. {
  187. if ((ipmb_dev->msg_idx >= IPMB_REQUEST_LEN_MIN) &&
  188. (!ipmb_verify_checksum1(ipmb_dev, rs_sa)))
  189. return true;
  190. return false;
  191. }
  192. /*
  193. * The IPMB protocol only supports I2C Writes so there is no need
  194. * to support I2C_SLAVE_READ* events.
  195. * This i2c callback function only monitors IPMB request messages
  196. * and adds them in a queue, so that they can be handled by
  197. * receive_ipmb_request.
  198. */
  199. static int ipmb_slave_cb(struct i2c_client *client,
  200. enum i2c_slave_event event, u8 *val)
  201. {
  202. struct ipmb_dev *ipmb_dev = i2c_get_clientdata(client);
  203. u8 *buf = (u8 *)&ipmb_dev->request;
  204. unsigned long flags;
  205. spin_lock_irqsave(&ipmb_dev->lock, flags);
  206. switch (event) {
  207. case I2C_SLAVE_WRITE_REQUESTED:
  208. memset(&ipmb_dev->request, 0, sizeof(ipmb_dev->request));
  209. ipmb_dev->msg_idx = 0;
  210. /*
  211. * At index 0, ipmb_msg stores the length of msg,
  212. * skip it for now.
  213. * The len will be populated once the whole
  214. * buf is populated.
  215. *
  216. * The I2C bus driver's responsibility is to pass the
  217. * data bytes to the backend driver; it does not
  218. * forward the i2c slave address.
  219. * Since the first byte in the IPMB message is the
  220. * address of the responder, it is the responsibility
  221. * of the IPMB driver to format the message properly.
  222. * So this driver prepends the address of the responder
  223. * to the received i2c data before the request message
  224. * is handled in userland.
  225. */
  226. buf[++ipmb_dev->msg_idx] = GET_8BIT_ADDR(client->addr);
  227. break;
  228. case I2C_SLAVE_WRITE_RECEIVED:
  229. if (ipmb_dev->msg_idx >= sizeof(struct ipmb_msg) - 1)
  230. break;
  231. buf[++ipmb_dev->msg_idx] = *val;
  232. break;
  233. case I2C_SLAVE_STOP:
  234. ipmb_dev->request.len = ipmb_dev->msg_idx;
  235. if (is_ipmb_msg(ipmb_dev, GET_8BIT_ADDR(client->addr)))
  236. ipmb_handle_request(ipmb_dev);
  237. break;
  238. default:
  239. break;
  240. }
  241. spin_unlock_irqrestore(&ipmb_dev->lock, flags);
  242. return 0;
  243. }
  244. static int ipmb_probe(struct i2c_client *client)
  245. {
  246. struct ipmb_dev *ipmb_dev;
  247. int ret;
  248. ipmb_dev = devm_kzalloc(&client->dev, sizeof(*ipmb_dev),
  249. GFP_KERNEL);
  250. if (!ipmb_dev)
  251. return -ENOMEM;
  252. spin_lock_init(&ipmb_dev->lock);
  253. init_waitqueue_head(&ipmb_dev->wait_queue);
  254. atomic_set(&ipmb_dev->request_queue_len, 0);
  255. INIT_LIST_HEAD(&ipmb_dev->request_queue);
  256. mutex_init(&ipmb_dev->file_mutex);
  257. ipmb_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
  258. ipmb_dev->miscdev.name = devm_kasprintf(&client->dev, GFP_KERNEL,
  259. "%s%d", "ipmb-",
  260. client->adapter->nr);
  261. ipmb_dev->miscdev.fops = &ipmb_fops;
  262. ipmb_dev->miscdev.parent = &client->dev;
  263. ret = misc_register(&ipmb_dev->miscdev);
  264. if (ret)
  265. return ret;
  266. ipmb_dev->is_i2c_protocol
  267. = device_property_read_bool(&client->dev, "i2c-protocol");
  268. ipmb_dev->client = client;
  269. i2c_set_clientdata(client, ipmb_dev);
  270. ret = i2c_slave_register(client, ipmb_slave_cb);
  271. if (ret) {
  272. misc_deregister(&ipmb_dev->miscdev);
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static void ipmb_remove(struct i2c_client *client)
  278. {
  279. struct ipmb_dev *ipmb_dev = i2c_get_clientdata(client);
  280. i2c_slave_unregister(client);
  281. misc_deregister(&ipmb_dev->miscdev);
  282. }
  283. static const struct i2c_device_id ipmb_id[] = {
  284. { "ipmb-dev", 0 },
  285. {},
  286. };
  287. MODULE_DEVICE_TABLE(i2c, ipmb_id);
  288. static const struct acpi_device_id acpi_ipmb_id[] = {
  289. { "IPMB0001", 0 },
  290. {},
  291. };
  292. MODULE_DEVICE_TABLE(acpi, acpi_ipmb_id);
  293. static struct i2c_driver ipmb_driver = {
  294. .driver = {
  295. .name = "ipmb-dev",
  296. .acpi_match_table = ACPI_PTR(acpi_ipmb_id),
  297. },
  298. .probe_new = ipmb_probe,
  299. .remove = ipmb_remove,
  300. .id_table = ipmb_id,
  301. };
  302. module_i2c_driver(ipmb_driver);
  303. MODULE_AUTHOR("Mellanox Technologies");
  304. MODULE_DESCRIPTION("IPMB driver");
  305. MODULE_LICENSE("GPL v2");