bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 "client.h"
  28. #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
  29. #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
  30. static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
  31. {
  32. struct mei_cl_device *device = to_mei_cl_device(dev);
  33. struct mei_cl_driver *driver = to_mei_cl_driver(drv);
  34. const struct mei_cl_device_id *id;
  35. if (!device)
  36. return 0;
  37. if (!driver || !driver->id_table)
  38. return 0;
  39. id = driver->id_table;
  40. while (id->name[0]) {
  41. if (!strncmp(dev_name(dev), id->name, sizeof(id->name)))
  42. return 1;
  43. id++;
  44. }
  45. return 0;
  46. }
  47. static int mei_cl_device_probe(struct device *dev)
  48. {
  49. struct mei_cl_device *device = to_mei_cl_device(dev);
  50. struct mei_cl_driver *driver;
  51. struct mei_cl_device_id id;
  52. if (!device)
  53. return 0;
  54. driver = to_mei_cl_driver(dev->driver);
  55. if (!driver || !driver->probe)
  56. return -ENODEV;
  57. dev_dbg(dev, "Device probe\n");
  58. strncpy(id.name, dev_name(dev), sizeof(id.name));
  59. return driver->probe(device, &id);
  60. }
  61. static int mei_cl_device_remove(struct device *dev)
  62. {
  63. struct mei_cl_device *device = to_mei_cl_device(dev);
  64. struct mei_cl_driver *driver;
  65. if (!device || !dev->driver)
  66. return 0;
  67. if (device->event_cb) {
  68. device->event_cb = NULL;
  69. cancel_work_sync(&device->event_work);
  70. }
  71. driver = to_mei_cl_driver(dev->driver);
  72. if (!driver->remove) {
  73. dev->driver = NULL;
  74. return 0;
  75. }
  76. return driver->remove(device);
  77. }
  78. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  79. char *buf)
  80. {
  81. int len;
  82. len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev));
  83. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  84. }
  85. static DEVICE_ATTR_RO(modalias);
  86. static struct attribute *mei_cl_dev_attrs[] = {
  87. &dev_attr_modalias.attr,
  88. NULL,
  89. };
  90. ATTRIBUTE_GROUPS(mei_cl_dev);
  91. static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
  92. {
  93. if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev)))
  94. return -ENOMEM;
  95. return 0;
  96. }
  97. static struct bus_type mei_cl_bus_type = {
  98. .name = "mei",
  99. .dev_groups = mei_cl_dev_groups,
  100. .match = mei_cl_device_match,
  101. .probe = mei_cl_device_probe,
  102. .remove = mei_cl_device_remove,
  103. .uevent = mei_cl_uevent,
  104. };
  105. static void mei_cl_dev_release(struct device *dev)
  106. {
  107. kfree(to_mei_cl_device(dev));
  108. }
  109. static struct device_type mei_cl_device_type = {
  110. .release = mei_cl_dev_release,
  111. };
  112. static struct mei_cl *mei_bus_find_mei_cl_by_uuid(struct mei_device *dev,
  113. uuid_le uuid)
  114. {
  115. struct mei_cl *cl;
  116. list_for_each_entry(cl, &dev->device_list, device_link) {
  117. if (!uuid_le_cmp(uuid, cl->device_uuid))
  118. return cl;
  119. }
  120. return NULL;
  121. }
  122. struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
  123. uuid_le uuid, char *name,
  124. struct mei_cl_ops *ops)
  125. {
  126. struct mei_cl_device *device;
  127. struct mei_cl *cl;
  128. int status;
  129. cl = mei_bus_find_mei_cl_by_uuid(dev, uuid);
  130. if (cl == NULL)
  131. return NULL;
  132. device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
  133. if (!device)
  134. return NULL;
  135. device->cl = cl;
  136. device->ops = ops;
  137. device->dev.parent = &dev->pdev->dev;
  138. device->dev.bus = &mei_cl_bus_type;
  139. device->dev.type = &mei_cl_device_type;
  140. dev_set_name(&device->dev, "%s", name);
  141. status = device_register(&device->dev);
  142. if (status) {
  143. dev_err(&dev->pdev->dev, "Failed to register MEI device\n");
  144. kfree(device);
  145. return NULL;
  146. }
  147. cl->device = device;
  148. dev_dbg(&device->dev, "client %s registered\n", name);
  149. return device;
  150. }
  151. EXPORT_SYMBOL_GPL(mei_cl_add_device);
  152. void mei_cl_remove_device(struct mei_cl_device *device)
  153. {
  154. device_unregister(&device->dev);
  155. }
  156. EXPORT_SYMBOL_GPL(mei_cl_remove_device);
  157. int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
  158. {
  159. int err;
  160. driver->driver.name = driver->name;
  161. driver->driver.owner = owner;
  162. driver->driver.bus = &mei_cl_bus_type;
  163. err = driver_register(&driver->driver);
  164. if (err)
  165. return err;
  166. pr_debug("mei: driver [%s] registered\n", driver->driver.name);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
  170. void mei_cl_driver_unregister(struct mei_cl_driver *driver)
  171. {
  172. driver_unregister(&driver->driver);
  173. pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
  174. }
  175. EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
  176. static int ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
  177. bool blocking)
  178. {
  179. struct mei_device *dev;
  180. struct mei_cl_cb *cb;
  181. int id;
  182. int rets;
  183. if (WARN_ON(!cl || !cl->dev))
  184. return -ENODEV;
  185. dev = cl->dev;
  186. if (cl->state != MEI_FILE_CONNECTED)
  187. return -ENODEV;
  188. /* Check if we have an ME client device */
  189. id = mei_me_cl_by_id(dev, cl->me_client_id);
  190. if (id < 0)
  191. return id;
  192. if (length > dev->me_clients[id].props.max_msg_length)
  193. return -EFBIG;
  194. cb = mei_io_cb_init(cl, NULL);
  195. if (!cb)
  196. return -ENOMEM;
  197. rets = mei_io_cb_alloc_req_buf(cb, length);
  198. if (rets < 0) {
  199. mei_io_cb_free(cb);
  200. return rets;
  201. }
  202. memcpy(cb->request_buffer.data, buf, length);
  203. mutex_lock(&dev->device_lock);
  204. rets = mei_cl_write(cl, cb, blocking);
  205. mutex_unlock(&dev->device_lock);
  206. if (rets < 0)
  207. mei_io_cb_free(cb);
  208. return rets;
  209. }
  210. int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
  211. {
  212. struct mei_device *dev;
  213. struct mei_cl_cb *cb;
  214. size_t r_length;
  215. int err;
  216. if (WARN_ON(!cl || !cl->dev))
  217. return -ENODEV;
  218. dev = cl->dev;
  219. mutex_lock(&dev->device_lock);
  220. if (!cl->read_cb) {
  221. err = mei_cl_read_start(cl, length);
  222. if (err < 0) {
  223. mutex_unlock(&dev->device_lock);
  224. return err;
  225. }
  226. }
  227. if (cl->reading_state != MEI_READ_COMPLETE &&
  228. !waitqueue_active(&cl->rx_wait)) {
  229. mutex_unlock(&dev->device_lock);
  230. if (wait_event_interruptible(cl->rx_wait,
  231. cl->reading_state == MEI_READ_COMPLETE ||
  232. mei_cl_is_transitioning(cl))) {
  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. err = mei_cl_connect(cl, NULL);
  324. if (err < 0) {
  325. mutex_unlock(&dev->device_lock);
  326. dev_err(&dev->pdev->dev, "Could not connect to the ME client");
  327. return err;
  328. }
  329. mutex_unlock(&dev->device_lock);
  330. if (device->event_cb && !cl->read_cb)
  331. mei_cl_read_start(device->cl, 0);
  332. if (!device->ops || !device->ops->enable)
  333. return 0;
  334. return device->ops->enable(device);
  335. }
  336. EXPORT_SYMBOL_GPL(mei_cl_enable_device);
  337. int mei_cl_disable_device(struct mei_cl_device *device)
  338. {
  339. int err;
  340. struct mei_device *dev;
  341. struct mei_cl *cl = device->cl;
  342. if (cl == NULL)
  343. return -ENODEV;
  344. dev = cl->dev;
  345. mutex_lock(&dev->device_lock);
  346. if (cl->state != MEI_FILE_CONNECTED) {
  347. mutex_unlock(&dev->device_lock);
  348. dev_err(&dev->pdev->dev, "Already disconnected");
  349. return 0;
  350. }
  351. cl->state = MEI_FILE_DISCONNECTING;
  352. err = mei_cl_disconnect(cl);
  353. if (err < 0) {
  354. mutex_unlock(&dev->device_lock);
  355. dev_err(&dev->pdev->dev,
  356. "Could not disconnect from the ME client");
  357. return err;
  358. }
  359. /* Flush queues and remove any pending read */
  360. mei_cl_flush_queues(cl);
  361. if (cl->read_cb) {
  362. struct mei_cl_cb *cb = NULL;
  363. cb = mei_cl_find_read_cb(cl);
  364. /* Remove entry from read list */
  365. if (cb)
  366. list_del(&cb->list);
  367. cb = cl->read_cb;
  368. cl->read_cb = NULL;
  369. if (cb) {
  370. mei_io_cb_free(cb);
  371. cb = NULL;
  372. }
  373. }
  374. device->event_cb = NULL;
  375. mutex_unlock(&dev->device_lock);
  376. if (!device->ops || !device->ops->disable)
  377. return 0;
  378. return device->ops->disable(device);
  379. }
  380. EXPORT_SYMBOL_GPL(mei_cl_disable_device);
  381. void mei_cl_bus_rx_event(struct mei_cl *cl)
  382. {
  383. struct mei_cl_device *device = cl->device;
  384. if (!device || !device->event_cb)
  385. return;
  386. set_bit(MEI_CL_EVENT_RX, &device->events);
  387. schedule_work(&device->event_work);
  388. }
  389. void mei_cl_bus_remove_devices(struct mei_device *dev)
  390. {
  391. struct mei_cl *cl, *next;
  392. mutex_lock(&dev->device_lock);
  393. list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
  394. if (cl->device)
  395. mei_cl_remove_device(cl->device);
  396. list_del(&cl->device_link);
  397. mei_cl_unlink(cl);
  398. kfree(cl);
  399. }
  400. mutex_unlock(&dev->device_lock);
  401. }
  402. int __init mei_cl_bus_init(void)
  403. {
  404. return bus_register(&mei_cl_bus_type);
  405. }
  406. void __exit mei_cl_bus_exit(void)
  407. {
  408. bus_unregister(&mei_cl_bus_type);
  409. }