bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. struct mei_cl_ops *ops)
  124. {
  125. struct mei_cl_device *device;
  126. struct mei_cl *cl;
  127. int status;
  128. cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
  129. if (cl == NULL)
  130. return NULL;
  131. device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
  132. if (!device)
  133. return NULL;
  134. device->cl = cl;
  135. device->ops = ops;
  136. device->dev.parent = &dev->pdev->dev;
  137. device->dev.bus = &mei_cl_bus_type;
  138. device->dev.type = &mei_cl_device_type;
  139. dev_set_name(&device->dev, "%s", name);
  140. status = device_register(&device->dev);
  141. if (status) {
  142. dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
  143. kfree(device);
  144. return NULL;
  145. }
  146. cl->device = device;
  147. dev_dbg(&device->dev, "client %s registered\n", name);
  148. return device;
  149. }
  150. EXPORT_SYMBOL_GPL(mei_cl_add_device);
  151. void mei_cl_remove_device(struct mei_cl_device *device)
  152. {
  153. device_unregister(&device->dev);
  154. }
  155. EXPORT_SYMBOL_GPL(mei_cl_remove_device);
  156. int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
  157. {
  158. int err;
  159. driver->driver.name = driver->name;
  160. driver->driver.owner = owner;
  161. driver->driver.bus = &mei_cl_bus_type;
  162. err = driver_register(&driver->driver);
  163. if (err)
  164. return err;
  165. pr_debug("mei: driver [%s] registered\n", driver->driver.name);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
  169. void mei_cl_driver_unregister(struct mei_cl_driver *driver)
  170. {
  171. driver_unregister(&driver->driver);
  172. pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
  173. }
  174. EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
  175. static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
  176. bool blocking)
  177. {
  178. struct mei_device *dev;
  179. struct mei_cl_cb *cb;
  180. int id;
  181. int rets;
  182. if (WARN_ON(!cl || !cl->dev))
  183. return -ENODEV;
  184. dev = cl->dev;
  185. if (cl->state != MEI_FILE_CONNECTED)
  186. return -ENODEV;
  187. /* Check if we have an ME client device */
  188. id = mei_me_cl_by_id(dev, cl->me_client_id);
  189. if (id < 0)
  190. return -ENODEV;
  191. if (length > dev->me_clients[id].props.max_msg_length)
  192. return -EINVAL;
  193. cb = mei_io_cb_init(cl, NULL);
  194. if (!cb)
  195. return -ENOMEM;
  196. rets = mei_io_cb_alloc_req_buf(cb, length);
  197. if (rets < 0) {
  198. mei_io_cb_free(cb);
  199. return rets;
  200. }
  201. memcpy(cb->request_buffer.data, buf, length);
  202. mutex_lock(&dev->device_lock);
  203. rets = mei_cl_write(cl, cb, blocking);
  204. mutex_unlock(&dev->device_lock);
  205. if (rets < 0)
  206. mei_io_cb_free(cb);
  207. return rets;
  208. }
  209. int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
  210. {
  211. struct mei_device *dev;
  212. struct mei_cl_cb *cb;
  213. size_t r_length;
  214. int err;
  215. if (WARN_ON(!cl || !cl->dev))
  216. return -ENODEV;
  217. dev = cl->dev;
  218. mutex_lock(&dev->device_lock);
  219. if (!cl->read_cb) {
  220. err = mei_cl_read_start(cl, length);
  221. if (err < 0) {
  222. mutex_unlock(&dev->device_lock);
  223. return err;
  224. }
  225. }
  226. if (cl->reading_state != MEI_READ_COMPLETE &&
  227. !waitqueue_active(&cl->rx_wait)) {
  228. mutex_unlock(&dev->device_lock);
  229. if (wait_event_interruptible(cl->rx_wait,
  230. (MEI_READ_COMPLETE == cl->reading_state))) {
  231. if (signal_pending(current))
  232. return -EINTR;
  233. return -ERESTARTSYS;
  234. }
  235. mutex_lock(&dev->device_lock);
  236. }
  237. cb = cl->read_cb;
  238. if (cl->reading_state != MEI_READ_COMPLETE) {
  239. r_length = 0;
  240. goto out;
  241. }
  242. r_length = min_t(size_t, length, cb->buf_idx);
  243. memcpy(buf, cb->response_buffer.data, r_length);
  244. mei_io_cb_free(cb);
  245. cl->reading_state = MEI_IDLE;
  246. cl->read_cb = NULL;
  247. out:
  248. mutex_unlock(&dev->device_lock);
  249. return r_length;
  250. }
  251. inline int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length)
  252. {
  253. return ___mei_cl_send(cl, buf, length, 0);
  254. }
  255. inline int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length)
  256. {
  257. return ___mei_cl_send(cl, buf, length, 1);
  258. }
  259. int mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
  260. {
  261. struct mei_cl *cl = device->cl;
  262. if (cl == NULL)
  263. return -ENODEV;
  264. if (device->ops && device->ops->send)
  265. return device->ops->send(device, buf, length);
  266. return __mei_cl_send(cl, buf, length);
  267. }
  268. EXPORT_SYMBOL_GPL(mei_cl_send);
  269. int mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
  270. {
  271. struct mei_cl *cl = device->cl;
  272. if (cl == NULL)
  273. return -ENODEV;
  274. if (device->ops && device->ops->recv)
  275. return device->ops->recv(device, buf, length);
  276. return __mei_cl_recv(cl, buf, length);
  277. }
  278. EXPORT_SYMBOL_GPL(mei_cl_recv);
  279. static void mei_bus_event_work(struct work_struct *work)
  280. {
  281. struct mei_cl_device *device;
  282. device = container_of(work, struct mei_cl_device, event_work);
  283. if (device->event_cb)
  284. device->event_cb(device, device->events, device->event_context);
  285. device->events = 0;
  286. /* Prepare for the next read */
  287. mei_cl_read_start(device->cl, 0);
  288. }
  289. int mei_cl_register_event_cb(struct mei_cl_device *device,
  290. mei_cl_event_cb_t event_cb, void *context)
  291. {
  292. if (device->event_cb)
  293. return -EALREADY;
  294. device->events = 0;
  295. device->event_cb = event_cb;
  296. device->event_context = context;
  297. INIT_WORK(&device->event_work, mei_bus_event_work);
  298. mei_cl_read_start(device->cl, 0);
  299. return 0;
  300. }
  301. EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
  302. void *mei_cl_get_drvdata(const struct mei_cl_device *device)
  303. {
  304. return dev_get_drvdata(&device->dev);
  305. }
  306. EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
  307. void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
  308. {
  309. dev_set_drvdata(&device->dev, data);
  310. }
  311. EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
  312. int mei_cl_enable_device(struct mei_cl_device *device)
  313. {
  314. int err;
  315. struct mei_device *dev;
  316. struct mei_cl *cl = device->cl;
  317. if (cl == NULL)
  318. return -ENODEV;
  319. dev = cl->dev;
  320. mutex_lock(&dev->device_lock);
  321. cl->state = MEI_FILE_CONNECTING;
  322. err = mei_cl_connect(cl, NULL);
  323. if (err < 0) {
  324. mutex_unlock(&dev->device_lock);
  325. dev_err(&dev->pdev->dev, "Could not connect to the ME client");
  326. return err;
  327. }
  328. mutex_unlock(&dev->device_lock);
  329. if (device->event_cb && !cl->read_cb)
  330. mei_cl_read_start(device->cl, 0);
  331. if (!device->ops || !device->ops->enable)
  332. return 0;
  333. return device->ops->enable(device);
  334. }
  335. EXPORT_SYMBOL_GPL(mei_cl_enable_device);
  336. int mei_cl_disable_device(struct mei_cl_device *device)
  337. {
  338. int err;
  339. struct mei_device *dev;
  340. struct mei_cl *cl = device->cl;
  341. if (cl == NULL)
  342. return -ENODEV;
  343. dev = cl->dev;
  344. mutex_lock(&dev->device_lock);
  345. if (cl->state != MEI_FILE_CONNECTED) {
  346. mutex_unlock(&dev->device_lock);
  347. dev_err(&dev->pdev->dev, "Already disconnected");
  348. return 0;
  349. }
  350. cl->state = MEI_FILE_DISCONNECTING;
  351. err = mei_cl_disconnect(cl);
  352. if (err < 0) {
  353. mutex_unlock(&dev->device_lock);
  354. dev_err(&dev->pdev->dev,
  355. "Could not disconnect from the ME client");
  356. return err;
  357. }
  358. /* Flush queues and remove any pending read */
  359. mei_cl_flush_queues(cl);
  360. if (cl->read_cb) {
  361. struct mei_cl_cb *cb = NULL;
  362. cb = mei_cl_find_read_cb(cl);
  363. /* Remove entry from read list */
  364. if (cb)
  365. list_del(&cb->list);
  366. cb = cl->read_cb;
  367. cl->read_cb = NULL;
  368. if (cb) {
  369. mei_io_cb_free(cb);
  370. cb = NULL;
  371. }
  372. }
  373. mutex_unlock(&dev->device_lock);
  374. if (!device->ops || !device->ops->disable)
  375. return 0;
  376. return device->ops->disable(device);
  377. }
  378. EXPORT_SYMBOL_GPL(mei_cl_disable_device);
  379. void mei_cl_bus_rx_event(struct mei_cl *cl)
  380. {
  381. struct mei_cl_device *device = cl->device;
  382. if (!device || !device->event_cb)
  383. return;
  384. set_bit(MEI_CL_EVENT_RX, &device->events);
  385. schedule_work(&device->event_work);
  386. }
  387. int __init mei_cl_bus_init(void)
  388. {
  389. return bus_register(&mei_cl_bus_type);
  390. }
  391. void __exit mei_cl_bus_exit(void)
  392. {
  393. bus_unregister(&mei_cl_bus_type);
  394. }