bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Intel Management Engine Interface (Intel MEI) Linux driver
  3. * Copyright (c) 2012-2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/mutex.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/mei_cl_bus.h>
  26. #include "mei_dev.h"
  27. #include "hw-me.h"
  28. #include "client.h"
  29. #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
  30. #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
  31. static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
  32. {
  33. struct mei_cl_device *device = to_mei_cl_device(dev);
  34. struct mei_cl_driver *driver = to_mei_cl_driver(drv);
  35. const struct mei_cl_device_id *id;
  36. if (!device)
  37. return 0;
  38. if (!driver || !driver->id_table)
  39. return 0;
  40. id = driver->id_table;
  41. while (id->name[0]) {
  42. if (!strcmp(dev_name(dev), id->name))
  43. return 1;
  44. id++;
  45. }
  46. return 0;
  47. }
  48. static int mei_cl_device_probe(struct device *dev)
  49. {
  50. struct mei_cl_device *device = to_mei_cl_device(dev);
  51. struct mei_cl_driver *driver;
  52. struct mei_cl_device_id id;
  53. if (!device)
  54. return 0;
  55. driver = to_mei_cl_driver(dev->driver);
  56. if (!driver || !driver->probe)
  57. return -ENODEV;
  58. dev_dbg(dev, "Device probe\n");
  59. strncpy(id.name, dev_name(dev), MEI_CL_NAME_SIZE);
  60. return driver->probe(device, &id);
  61. }
  62. static int mei_cl_device_remove(struct device *dev)
  63. {
  64. struct mei_cl_device *device = to_mei_cl_device(dev);
  65. struct mei_cl_driver *driver;
  66. if (!device || !dev->driver)
  67. return 0;
  68. if (device->event_cb) {
  69. device->event_cb = NULL;
  70. cancel_work_sync(&device->event_work);
  71. }
  72. driver = to_mei_cl_driver(dev->driver);
  73. if (!driver->remove) {
  74. dev->driver = NULL;
  75. return 0;
  76. }
  77. return driver->remove(device);
  78. }
  79. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  80. char *buf)
  81. {
  82. int len;
  83. len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
  84. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  85. }
  86. static struct device_attribute mei_cl_dev_attrs[] = {
  87. __ATTR_RO(modalias),
  88. __ATTR_NULL,
  89. };
  90. static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
  91. {
  92. if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
  93. return -ENOMEM;
  94. return 0;
  95. }
  96. static struct bus_type mei_cl_bus_type = {
  97. .name = "mei",
  98. .dev_attrs = mei_cl_dev_attrs,
  99. .match = mei_cl_device_match,
  100. .probe = mei_cl_device_probe,
  101. .remove = mei_cl_device_remove,
  102. .uevent = mei_cl_uevent,
  103. };
  104. static void mei_cl_dev_release(struct device *dev)
  105. {
  106. kfree(to_mei_cl_device(dev));
  107. }
  108. static struct device_type mei_cl_device_type = {
  109. .release = mei_cl_dev_release,
  110. };
  111. static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
  112. uuid_le uuid)
  113. {
  114. struct mei_cl *cl, *next;
  115. list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
  116. if (!uuid_le_cmp(uuid, cl->device_uuid))
  117. return cl;
  118. }
  119. return NULL;
  120. }
  121. struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
  122. uuid_le uuid, char *name)
  123. {
  124. struct mei_cl_device *device;
  125. struct mei_cl *cl;
  126. int status;
  127. cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
  128. if (cl == NULL)
  129. return NULL;
  130. device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
  131. if (!device)
  132. return NULL;
  133. device->cl = cl;
  134. device->dev.parent = &dev->pdev->dev;
  135. device->dev.bus = &mei_cl_bus_type;
  136. device->dev.type = &mei_cl_device_type;
  137. dev_set_name(&device->dev, "%s", name);
  138. status = device_register(&device->dev);
  139. if (status) {
  140. dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
  141. kfree(device);
  142. return NULL;
  143. }
  144. cl->device = device;
  145. dev_dbg(&device->dev, "client %s registered\n", name);
  146. return device;
  147. }
  148. EXPORT_SYMBOL_GPL(mei_cl_add_device);
  149. void mei_cl_remove_device(struct mei_cl_device *device)
  150. {
  151. device_unregister(&device->dev);
  152. }
  153. EXPORT_SYMBOL_GPL(mei_cl_remove_device);
  154. int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
  155. {
  156. int err;
  157. driver->driver.name = driver->name;
  158. driver->driver.owner = owner;
  159. driver->driver.bus = &mei_cl_bus_type;
  160. err = driver_register(&driver->driver);
  161. if (err)
  162. return err;
  163. pr_debug("mei: driver [%s] registered\n", driver->driver.name);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
  167. void mei_cl_driver_unregister(struct mei_cl_driver *driver)
  168. {
  169. driver_unregister(&driver->driver);
  170. pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
  171. }
  172. EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
  173. static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
  174. bool blocking)
  175. {
  176. struct mei_device *dev;
  177. struct mei_msg_hdr mei_hdr;
  178. struct mei_cl_cb *cb;
  179. int me_cl_id, err;
  180. if (WARN_ON(!cl || !cl->dev))
  181. return -ENODEV;
  182. if (cl->state != MEI_FILE_CONNECTED)
  183. return -ENODEV;
  184. cb = mei_io_cb_init(cl, NULL);
  185. if (!cb)
  186. return -ENOMEM;
  187. err = mei_io_cb_alloc_req_buf(cb, length);
  188. if (err < 0) {
  189. mei_io_cb_free(cb);
  190. return err;
  191. }
  192. memcpy(cb->request_buffer.data, buf, length);
  193. cb->fop_type = MEI_FOP_WRITE;
  194. dev = cl->dev;
  195. mutex_lock(&dev->device_lock);
  196. /* Check if we have an ME client device */
  197. me_cl_id = mei_me_cl_by_id(dev, cl->me_client_id);
  198. if (me_cl_id == dev->me_clients_num) {
  199. err = -ENODEV;
  200. goto out_err;
  201. }
  202. if (length > dev->me_clients[me_cl_id].props.max_msg_length) {
  203. err = -EINVAL;
  204. goto out_err;
  205. }
  206. err = mei_cl_flow_ctrl_creds(cl);
  207. if (err < 0)
  208. goto out_err;
  209. /* Host buffer is not ready, we queue the request */
  210. if (err == 0 || !dev->hbuf_is_ready) {
  211. cb->buf_idx = 0;
  212. mei_hdr.msg_complete = 0;
  213. cl->writing_state = MEI_WRITING;
  214. goto out;
  215. }
  216. dev->hbuf_is_ready = false;
  217. /* Check for a maximum length */
  218. if (length > mei_hbuf_max_len(dev)) {
  219. mei_hdr.length = mei_hbuf_max_len(dev);
  220. mei_hdr.msg_complete = 0;
  221. } else {
  222. mei_hdr.length = length;
  223. mei_hdr.msg_complete = 1;
  224. }
  225. mei_hdr.host_addr = cl->host_client_id;
  226. mei_hdr.me_addr = cl->me_client_id;
  227. mei_hdr.reserved = 0;
  228. if (mei_write_message(dev, &mei_hdr, buf)) {
  229. err = -EIO;
  230. goto out_err;
  231. }
  232. cl->writing_state = MEI_WRITING;
  233. cb->buf_idx = mei_hdr.length;
  234. out:
  235. if (mei_hdr.msg_complete) {
  236. if (mei_cl_flow_ctrl_reduce(cl)) {
  237. err = -ENODEV;
  238. goto out_err;
  239. }
  240. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  241. } else {
  242. list_add_tail(&cb->list, &dev->write_list.list);
  243. }
  244. mutex_unlock(&dev->device_lock);
  245. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  246. if (wait_event_interruptible(cl->tx_wait,
  247. cl->writing_state == MEI_WRITE_COMPLETE)) {
  248. if (signal_pending(current))
  249. err = -EINTR;
  250. err = -ERESTARTSYS;
  251. mutex_lock(&dev->device_lock);
  252. goto out_err;
  253. }
  254. }
  255. return mei_hdr.length;
  256. out_err:
  257. mutex_unlock(&dev->device_lock);
  258. mei_io_cb_free(cb);
  259. return err;
  260. }
  261. int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
  262. {
  263. struct mei_device *dev;
  264. struct mei_cl_cb *cb;
  265. size_t r_length;
  266. int err;
  267. if (WARN_ON(!cl || !cl->dev))
  268. return -ENODEV;
  269. dev = cl->dev;
  270. mutex_lock(&dev->device_lock);
  271. if (!cl->read_cb) {
  272. err = mei_cl_read_start(cl);
  273. if (err < 0) {
  274. mutex_unlock(&dev->device_lock);
  275. return err;
  276. }
  277. }
  278. if (cl->reading_state != MEI_READ_COMPLETE &&
  279. !waitqueue_active(&cl->rx_wait)) {
  280. mutex_unlock(&dev->device_lock);
  281. if (wait_event_interruptible(cl->rx_wait,
  282. (MEI_READ_COMPLETE == cl->reading_state))) {
  283. if (signal_pending(current))
  284. return -EINTR;
  285. return -ERESTARTSYS;
  286. }
  287. mutex_lock(&dev->device_lock);
  288. }
  289. cb = cl->read_cb;
  290. if (cl->reading_state != MEI_READ_COMPLETE) {
  291. r_length = 0;
  292. goto out;
  293. }
  294. r_length = min_t(size_t, length, cb->buf_idx);
  295. memcpy(buf, cb->response_buffer.data, r_length);
  296. mei_io_cb_free(cb);
  297. cl->reading_state = MEI_IDLE;
  298. cl->read_cb = NULL;
  299. out:
  300. mutex_unlock(&dev->device_lock);
  301. return r_length;
  302. }
  303. inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
  304. {
  305. return ___mei_cl_send(cl, buf, length, 0);
  306. }
  307. inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
  308. {
  309. return ___mei_cl_send(cl, buf, length, 1);
  310. }
  311. int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
  312. {
  313. struct mei_cl *cl = device->cl;
  314. if (cl == NULL)
  315. return -ENODEV;
  316. if (device->ops && device->ops->send)
  317. return device->ops->send(device, buf, length);
  318. return __mei_cl_send(cl, buf, length);
  319. }
  320. EXPORT_SYMBOL_GPL(mei_cl_send);
  321. int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
  322. {
  323. struct mei_cl *cl = device->cl;
  324. if (cl == NULL)
  325. return -ENODEV;
  326. if (device->ops && device->ops->recv)
  327. return device->ops->recv(device, buf, length);
  328. return __mei_cl_recv(cl, buf, length);
  329. }
  330. EXPORT_SYMBOL_GPL(mei_cl_recv);
  331. static void mei_bus_event_work(struct work_struct *work)
  332. {
  333. struct mei_cl_device *device;
  334. device = container_of(work, struct mei_cl_device, event_work);
  335. if (device->event_cb)
  336. device->event_cb(device, device->events, device->event_context);
  337. device->events = 0;
  338. /* Prepare for the next read */
  339. mei_cl_read_start(device->cl);
  340. }
  341. int mei_cl_register_event_cb(struct mei_cl_device *device,
  342. mei_cl_event_cb_t event_cb, void *context)
  343. {
  344. if (device->event_cb)
  345. return -EALREADY;
  346. device->events = 0;
  347. device->event_cb = event_cb;
  348. device->event_context = context;
  349. INIT_WORK(&device->event_work, mei_bus_event_work);
  350. mei_cl_read_start(device->cl);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
  354. void *mei_cl_get_drvdata(const struct mei_cl_device *device)
  355. {
  356. return dev_get_drvdata(&device->dev);
  357. }
  358. EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
  359. void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
  360. {
  361. dev_set_drvdata(&device->dev, data);
  362. }
  363. EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
  364. void mei_cl_bus_rx_event(struct mei_cl *cl)
  365. {
  366. struct mei_cl_device *device = cl->device;
  367. if (!device || !device->event_cb)
  368. return;
  369. set_bit(MEI_CL_EVENT_RX, &device->events);
  370. schedule_work(&device->event_work);
  371. }
  372. int __init mei_cl_bus_init(void)
  373. {
  374. return bus_register(&mei_cl_bus_type);
  375. }
  376. void __exit mei_cl_bus_exit(void)
  377. {
  378. bus_unregister(&mei_cl_bus_type);
  379. }