bus.c 11 KB

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