main.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/aio.h>
  26. #include <linux/pci.h>
  27. #include <linux/poll.h>
  28. #include <linux/init.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/cdev.h>
  31. #include <linux/sched.h>
  32. #include <linux/uuid.h>
  33. #include <linux/compat.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/mei.h>
  38. #include "mei_dev.h"
  39. #include "hw-me.h"
  40. #include "client.h"
  41. /* AMT device is a singleton on the platform */
  42. static struct pci_dev *mei_pdev;
  43. /* mei_pci_tbl - PCI Device ID Table */
  44. static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
  45. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
  46. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
  49. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
  50. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
  51. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
  52. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
  53. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
  54. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
  55. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
  56. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
  57. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
  58. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
  59. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
  60. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
  61. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
  62. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
  63. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
  64. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
  65. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
  66. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
  67. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
  68. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
  69. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
  70. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
  71. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
  72. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
  73. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
  74. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
  75. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
  76. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
  77. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
  78. /* required last entry */
  79. {0, }
  80. };
  81. MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
  82. static DEFINE_MUTEX(mei_mutex);
  83. /**
  84. * mei_open - the open function
  85. *
  86. * @inode: pointer to inode structure
  87. * @file: pointer to file structure
  88. *
  89. * returns 0 on success, <0 on error
  90. */
  91. static int mei_open(struct inode *inode, struct file *file)
  92. {
  93. struct mei_cl *cl;
  94. struct mei_device *dev;
  95. int err;
  96. err = -ENODEV;
  97. if (!mei_pdev)
  98. goto out;
  99. dev = pci_get_drvdata(mei_pdev);
  100. if (!dev)
  101. goto out;
  102. mutex_lock(&dev->device_lock);
  103. err = -ENOMEM;
  104. cl = mei_cl_allocate(dev);
  105. if (!cl)
  106. goto out_unlock;
  107. err = -ENODEV;
  108. if (dev->dev_state != MEI_DEV_ENABLED) {
  109. dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  110. mei_dev_state_str(dev->dev_state));
  111. goto out_unlock;
  112. }
  113. err = -EMFILE;
  114. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  115. dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
  116. MEI_MAX_OPEN_HANDLE_COUNT);
  117. goto out_unlock;
  118. }
  119. err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
  120. if (err)
  121. goto out_unlock;
  122. file->private_data = cl;
  123. mutex_unlock(&dev->device_lock);
  124. return nonseekable_open(inode, file);
  125. out_unlock:
  126. mutex_unlock(&dev->device_lock);
  127. kfree(cl);
  128. out:
  129. return err;
  130. }
  131. /**
  132. * mei_release - the release function
  133. *
  134. * @inode: pointer to inode structure
  135. * @file: pointer to file structure
  136. *
  137. * returns 0 on success, <0 on error
  138. */
  139. static int mei_release(struct inode *inode, struct file *file)
  140. {
  141. struct mei_cl *cl = file->private_data;
  142. struct mei_cl_cb *cb;
  143. struct mei_device *dev;
  144. int rets = 0;
  145. if (WARN_ON(!cl || !cl->dev))
  146. return -ENODEV;
  147. dev = cl->dev;
  148. mutex_lock(&dev->device_lock);
  149. if (cl == &dev->iamthif_cl) {
  150. rets = mei_amthif_release(dev, file);
  151. goto out;
  152. }
  153. if (cl->state == MEI_FILE_CONNECTED) {
  154. cl->state = MEI_FILE_DISCONNECTING;
  155. dev_dbg(&dev->pdev->dev,
  156. "disconnecting client host client = %d, "
  157. "ME client = %d\n",
  158. cl->host_client_id,
  159. cl->me_client_id);
  160. rets = mei_cl_disconnect(cl);
  161. }
  162. mei_cl_flush_queues(cl);
  163. dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
  164. cl->host_client_id,
  165. cl->me_client_id);
  166. if (dev->open_handle_count > 0) {
  167. clear_bit(cl->host_client_id, dev->host_clients_map);
  168. dev->open_handle_count--;
  169. }
  170. mei_cl_unlink(cl);
  171. /* free read cb */
  172. cb = NULL;
  173. if (cl->read_cb) {
  174. cb = mei_cl_find_read_cb(cl);
  175. /* Remove entry from read list */
  176. if (cb)
  177. list_del(&cb->list);
  178. cb = cl->read_cb;
  179. cl->read_cb = NULL;
  180. }
  181. file->private_data = NULL;
  182. if (cb) {
  183. mei_io_cb_free(cb);
  184. cb = NULL;
  185. }
  186. kfree(cl);
  187. out:
  188. mutex_unlock(&dev->device_lock);
  189. return rets;
  190. }
  191. /**
  192. * mei_read - the read function.
  193. *
  194. * @file: pointer to file structure
  195. * @ubuf: pointer to user buffer
  196. * @length: buffer length
  197. * @offset: data offset in buffer
  198. *
  199. * returns >=0 data length on success , <0 on error
  200. */
  201. static ssize_t mei_read(struct file *file, char __user *ubuf,
  202. size_t length, loff_t *offset)
  203. {
  204. struct mei_cl *cl = file->private_data;
  205. struct mei_cl_cb *cb_pos = NULL;
  206. struct mei_cl_cb *cb = NULL;
  207. struct mei_device *dev;
  208. int i;
  209. int rets;
  210. int err;
  211. if (WARN_ON(!cl || !cl->dev))
  212. return -ENODEV;
  213. dev = cl->dev;
  214. mutex_lock(&dev->device_lock);
  215. if (dev->dev_state != MEI_DEV_ENABLED) {
  216. rets = -ENODEV;
  217. goto out;
  218. }
  219. if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
  220. /* Do not allow to read watchdog client */
  221. i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
  222. if (i >= 0) {
  223. struct mei_me_client *me_client = &dev->me_clients[i];
  224. if (cl->me_client_id == me_client->client_id) {
  225. rets = -EBADF;
  226. goto out;
  227. }
  228. }
  229. } else {
  230. cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  231. }
  232. if (cl == &dev->iamthif_cl) {
  233. rets = mei_amthif_read(dev, file, ubuf, length, offset);
  234. goto out;
  235. }
  236. if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
  237. cb = cl->read_cb;
  238. goto copy_buffer;
  239. } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
  240. cl->read_cb->buf_idx <= *offset) {
  241. cb = cl->read_cb;
  242. rets = 0;
  243. goto free;
  244. } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
  245. /*Offset needs to be cleaned for contiguous reads*/
  246. *offset = 0;
  247. rets = 0;
  248. goto out;
  249. }
  250. err = mei_cl_read_start(cl);
  251. if (err && err != -EBUSY) {
  252. dev_dbg(&dev->pdev->dev,
  253. "mei start read failure with status = %d\n", err);
  254. rets = err;
  255. goto out;
  256. }
  257. if (MEI_READ_COMPLETE != cl->reading_state &&
  258. !waitqueue_active(&cl->rx_wait)) {
  259. if (file->f_flags & O_NONBLOCK) {
  260. rets = -EAGAIN;
  261. goto out;
  262. }
  263. mutex_unlock(&dev->device_lock);
  264. if (wait_event_interruptible(cl->rx_wait,
  265. (MEI_READ_COMPLETE == cl->reading_state ||
  266. MEI_FILE_INITIALIZING == cl->state ||
  267. MEI_FILE_DISCONNECTED == cl->state ||
  268. MEI_FILE_DISCONNECTING == cl->state))) {
  269. if (signal_pending(current))
  270. return -EINTR;
  271. return -ERESTARTSYS;
  272. }
  273. mutex_lock(&dev->device_lock);
  274. if (MEI_FILE_INITIALIZING == cl->state ||
  275. MEI_FILE_DISCONNECTED == cl->state ||
  276. MEI_FILE_DISCONNECTING == cl->state) {
  277. rets = -EBUSY;
  278. goto out;
  279. }
  280. }
  281. cb = cl->read_cb;
  282. if (!cb) {
  283. rets = -ENODEV;
  284. goto out;
  285. }
  286. if (cl->reading_state != MEI_READ_COMPLETE) {
  287. rets = 0;
  288. goto out;
  289. }
  290. /* now copy the data to user space */
  291. copy_buffer:
  292. dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
  293. cb->response_buffer.size);
  294. dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
  295. if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
  296. rets = -EMSGSIZE;
  297. goto free;
  298. }
  299. /* length is being truncated to PAGE_SIZE,
  300. * however buf_idx may point beyond that */
  301. length = min_t(size_t, length, cb->buf_idx - *offset);
  302. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
  303. rets = -EFAULT;
  304. goto free;
  305. }
  306. rets = length;
  307. *offset += length;
  308. if ((unsigned long)*offset < cb->buf_idx)
  309. goto out;
  310. free:
  311. cb_pos = mei_cl_find_read_cb(cl);
  312. /* Remove entry from read list */
  313. if (cb_pos)
  314. list_del(&cb_pos->list);
  315. mei_io_cb_free(cb);
  316. cl->reading_state = MEI_IDLE;
  317. cl->read_cb = NULL;
  318. out:
  319. dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
  320. mutex_unlock(&dev->device_lock);
  321. return rets;
  322. }
  323. /**
  324. * mei_write - the write function.
  325. *
  326. * @file: pointer to file structure
  327. * @ubuf: pointer to user buffer
  328. * @length: buffer length
  329. * @offset: data offset in buffer
  330. *
  331. * returns >=0 data length on success , <0 on error
  332. */
  333. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  334. size_t length, loff_t *offset)
  335. {
  336. struct mei_cl *cl = file->private_data;
  337. struct mei_cl_cb *write_cb = NULL;
  338. struct mei_msg_hdr mei_hdr;
  339. struct mei_device *dev;
  340. unsigned long timeout = 0;
  341. int rets;
  342. int i;
  343. if (WARN_ON(!cl || !cl->dev))
  344. return -ENODEV;
  345. dev = cl->dev;
  346. mutex_lock(&dev->device_lock);
  347. if (dev->dev_state != MEI_DEV_ENABLED) {
  348. rets = -ENODEV;
  349. goto err;
  350. }
  351. i = mei_me_cl_by_id(dev, cl->me_client_id);
  352. if (i < 0) {
  353. rets = -ENODEV;
  354. goto err;
  355. }
  356. if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
  357. rets = -EMSGSIZE;
  358. goto err;
  359. }
  360. if (cl->state != MEI_FILE_CONNECTED) {
  361. rets = -ENODEV;
  362. dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
  363. cl->host_client_id, cl->me_client_id);
  364. goto err;
  365. }
  366. if (cl == &dev->iamthif_cl) {
  367. write_cb = mei_amthif_find_read_list_entry(dev, file);
  368. if (write_cb) {
  369. timeout = write_cb->read_time +
  370. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  371. if (time_after(jiffies, timeout) ||
  372. cl->reading_state == MEI_READ_COMPLETE) {
  373. *offset = 0;
  374. list_del(&write_cb->list);
  375. mei_io_cb_free(write_cb);
  376. write_cb = NULL;
  377. }
  378. }
  379. }
  380. /* free entry used in read */
  381. if (cl->reading_state == MEI_READ_COMPLETE) {
  382. *offset = 0;
  383. write_cb = mei_cl_find_read_cb(cl);
  384. if (write_cb) {
  385. list_del(&write_cb->list);
  386. mei_io_cb_free(write_cb);
  387. write_cb = NULL;
  388. cl->reading_state = MEI_IDLE;
  389. cl->read_cb = NULL;
  390. }
  391. } else if (cl->reading_state == MEI_IDLE)
  392. *offset = 0;
  393. write_cb = mei_io_cb_init(cl, file);
  394. if (!write_cb) {
  395. dev_err(&dev->pdev->dev, "write cb allocation failed\n");
  396. rets = -ENOMEM;
  397. goto err;
  398. }
  399. rets = mei_io_cb_alloc_req_buf(write_cb, length);
  400. if (rets)
  401. goto err;
  402. dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
  403. rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
  404. if (rets)
  405. goto err;
  406. cl->sm_state = 0;
  407. if (length == 4 &&
  408. ((memcmp(mei_wd_state_independence_msg[0],
  409. write_cb->request_buffer.data, 4) == 0) ||
  410. (memcmp(mei_wd_state_independence_msg[1],
  411. write_cb->request_buffer.data, 4) == 0) ||
  412. (memcmp(mei_wd_state_independence_msg[2],
  413. write_cb->request_buffer.data, 4) == 0)))
  414. cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  415. if (cl == &dev->iamthif_cl) {
  416. rets = mei_amthif_write(dev, write_cb);
  417. if (rets) {
  418. dev_err(&dev->pdev->dev,
  419. "amthif write failed with status = %d\n", rets);
  420. goto err;
  421. }
  422. mutex_unlock(&dev->device_lock);
  423. return length;
  424. }
  425. write_cb->fop_type = MEI_FOP_WRITE;
  426. dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
  427. cl->host_client_id, cl->me_client_id);
  428. rets = mei_cl_flow_ctrl_creds(cl);
  429. if (rets < 0)
  430. goto err;
  431. if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
  432. write_cb->buf_idx = 0;
  433. mei_hdr.msg_complete = 0;
  434. cl->writing_state = MEI_WRITING;
  435. goto out;
  436. }
  437. dev->mei_host_buffer_is_empty = false;
  438. if (length > mei_hbuf_max_data(dev)) {
  439. mei_hdr.length = mei_hbuf_max_data(dev);
  440. mei_hdr.msg_complete = 0;
  441. } else {
  442. mei_hdr.length = length;
  443. mei_hdr.msg_complete = 1;
  444. }
  445. mei_hdr.host_addr = cl->host_client_id;
  446. mei_hdr.me_addr = cl->me_client_id;
  447. mei_hdr.reserved = 0;
  448. dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
  449. MEI_HDR_PRM(&mei_hdr));
  450. if (mei_write_message(dev, &mei_hdr, write_cb->request_buffer.data)) {
  451. rets = -ENODEV;
  452. goto err;
  453. }
  454. cl->writing_state = MEI_WRITING;
  455. write_cb->buf_idx = mei_hdr.length;
  456. out:
  457. if (mei_hdr.msg_complete) {
  458. if (mei_cl_flow_ctrl_reduce(cl)) {
  459. rets = -ENODEV;
  460. goto err;
  461. }
  462. list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
  463. } else {
  464. list_add_tail(&write_cb->list, &dev->write_list.list);
  465. }
  466. mutex_unlock(&dev->device_lock);
  467. return length;
  468. err:
  469. mutex_unlock(&dev->device_lock);
  470. mei_io_cb_free(write_cb);
  471. return rets;
  472. }
  473. /**
  474. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  475. *
  476. * @dev: the device structure
  477. * @data: IOCTL connect data, input and output parameters
  478. * @file: private data of the file object
  479. *
  480. * Locking: called under "dev->device_lock" lock
  481. *
  482. * returns 0 on success, <0 on failure.
  483. */
  484. static int mei_ioctl_connect_client(struct file *file,
  485. struct mei_connect_client_data *data)
  486. {
  487. struct mei_device *dev;
  488. struct mei_client *client;
  489. struct mei_cl *cl;
  490. int i;
  491. int rets;
  492. cl = file->private_data;
  493. if (WARN_ON(!cl || !cl->dev))
  494. return -ENODEV;
  495. dev = cl->dev;
  496. if (dev->dev_state != MEI_DEV_ENABLED) {
  497. rets = -ENODEV;
  498. goto end;
  499. }
  500. if (cl->state != MEI_FILE_INITIALIZING &&
  501. cl->state != MEI_FILE_DISCONNECTED) {
  502. rets = -EBUSY;
  503. goto end;
  504. }
  505. /* find ME client we're trying to connect to */
  506. i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  507. if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
  508. cl->me_client_id = dev->me_clients[i].client_id;
  509. cl->state = MEI_FILE_CONNECTING;
  510. }
  511. dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
  512. cl->me_client_id);
  513. dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
  514. dev->me_clients[i].props.protocol_version);
  515. dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
  516. dev->me_clients[i].props.max_msg_length);
  517. /* if we're connecting to amthif client then we will use the
  518. * existing connection
  519. */
  520. if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
  521. dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
  522. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  523. rets = -ENODEV;
  524. goto end;
  525. }
  526. clear_bit(cl->host_client_id, dev->host_clients_map);
  527. mei_cl_unlink(cl);
  528. kfree(cl);
  529. cl = NULL;
  530. file->private_data = &dev->iamthif_cl;
  531. client = &data->out_client_properties;
  532. client->max_msg_length =
  533. dev->me_clients[i].props.max_msg_length;
  534. client->protocol_version =
  535. dev->me_clients[i].props.protocol_version;
  536. rets = dev->iamthif_cl.status;
  537. goto end;
  538. }
  539. if (cl->state != MEI_FILE_CONNECTING) {
  540. rets = -ENODEV;
  541. goto end;
  542. }
  543. /* prepare the output buffer */
  544. client = &data->out_client_properties;
  545. client->max_msg_length = dev->me_clients[i].props.max_msg_length;
  546. client->protocol_version = dev->me_clients[i].props.protocol_version;
  547. dev_dbg(&dev->pdev->dev, "Can connect?\n");
  548. rets = mei_cl_connect(cl, file);
  549. end:
  550. dev_dbg(&dev->pdev->dev, "free connect cb memory.");
  551. return rets;
  552. }
  553. /**
  554. * mei_ioctl - the IOCTL function
  555. *
  556. * @file: pointer to file structure
  557. * @cmd: ioctl command
  558. * @data: pointer to mei message structure
  559. *
  560. * returns 0 on success , <0 on error
  561. */
  562. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  563. {
  564. struct mei_device *dev;
  565. struct mei_cl *cl = file->private_data;
  566. struct mei_connect_client_data *connect_data = NULL;
  567. int rets;
  568. if (cmd != IOCTL_MEI_CONNECT_CLIENT)
  569. return -EINVAL;
  570. if (WARN_ON(!cl || !cl->dev))
  571. return -ENODEV;
  572. dev = cl->dev;
  573. dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
  574. mutex_lock(&dev->device_lock);
  575. if (dev->dev_state != MEI_DEV_ENABLED) {
  576. rets = -ENODEV;
  577. goto out;
  578. }
  579. dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  580. connect_data = kzalloc(sizeof(struct mei_connect_client_data),
  581. GFP_KERNEL);
  582. if (!connect_data) {
  583. rets = -ENOMEM;
  584. goto out;
  585. }
  586. dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
  587. if (copy_from_user(connect_data, (char __user *)data,
  588. sizeof(struct mei_connect_client_data))) {
  589. dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
  590. rets = -EFAULT;
  591. goto out;
  592. }
  593. rets = mei_ioctl_connect_client(file, connect_data);
  594. /* if all is ok, copying the data back to user. */
  595. if (rets)
  596. goto out;
  597. dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
  598. if (copy_to_user((char __user *)data, connect_data,
  599. sizeof(struct mei_connect_client_data))) {
  600. dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
  601. rets = -EFAULT;
  602. goto out;
  603. }
  604. out:
  605. kfree(connect_data);
  606. mutex_unlock(&dev->device_lock);
  607. return rets;
  608. }
  609. /**
  610. * mei_compat_ioctl - the compat IOCTL function
  611. *
  612. * @file: pointer to file structure
  613. * @cmd: ioctl command
  614. * @data: pointer to mei message structure
  615. *
  616. * returns 0 on success , <0 on error
  617. */
  618. #ifdef CONFIG_COMPAT
  619. static long mei_compat_ioctl(struct file *file,
  620. unsigned int cmd, unsigned long data)
  621. {
  622. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  623. }
  624. #endif
  625. /**
  626. * mei_poll - the poll function
  627. *
  628. * @file: pointer to file structure
  629. * @wait: pointer to poll_table structure
  630. *
  631. * returns poll mask
  632. */
  633. static unsigned int mei_poll(struct file *file, poll_table *wait)
  634. {
  635. struct mei_cl *cl = file->private_data;
  636. struct mei_device *dev;
  637. unsigned int mask = 0;
  638. if (WARN_ON(!cl || !cl->dev))
  639. return mask;
  640. dev = cl->dev;
  641. mutex_lock(&dev->device_lock);
  642. if (dev->dev_state != MEI_DEV_ENABLED)
  643. goto out;
  644. if (cl == &dev->iamthif_cl) {
  645. mask = mei_amthif_poll(dev, file, wait);
  646. goto out;
  647. }
  648. mutex_unlock(&dev->device_lock);
  649. poll_wait(file, &cl->tx_wait, wait);
  650. mutex_lock(&dev->device_lock);
  651. if (MEI_WRITE_COMPLETE == cl->writing_state)
  652. mask |= (POLLIN | POLLRDNORM);
  653. out:
  654. mutex_unlock(&dev->device_lock);
  655. return mask;
  656. }
  657. /*
  658. * file operations structure will be used for mei char device.
  659. */
  660. static const struct file_operations mei_fops = {
  661. .owner = THIS_MODULE,
  662. .read = mei_read,
  663. .unlocked_ioctl = mei_ioctl,
  664. #ifdef CONFIG_COMPAT
  665. .compat_ioctl = mei_compat_ioctl,
  666. #endif
  667. .open = mei_open,
  668. .release = mei_release,
  669. .write = mei_write,
  670. .poll = mei_poll,
  671. .llseek = no_llseek
  672. };
  673. /*
  674. * Misc Device Struct
  675. */
  676. static struct miscdevice mei_misc_device = {
  677. .name = "mei",
  678. .fops = &mei_fops,
  679. .minor = MISC_DYNAMIC_MINOR,
  680. };
  681. /**
  682. * mei_quirk_probe - probe for devices that doesn't valid ME interface
  683. * @pdev: PCI device structure
  684. * @ent: entry into pci_device_table
  685. *
  686. * returns true if ME Interface is valid, false otherwise
  687. */
  688. static bool mei_quirk_probe(struct pci_dev *pdev,
  689. const struct pci_device_id *ent)
  690. {
  691. u32 reg;
  692. if (ent->device == MEI_DEV_ID_PBG_1) {
  693. pci_read_config_dword(pdev, 0x48, &reg);
  694. /* make sure that bit 9 is up and bit 10 is down */
  695. if ((reg & 0x600) == 0x200) {
  696. dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
  697. return false;
  698. }
  699. }
  700. return true;
  701. }
  702. /**
  703. * mei_probe - Device Initialization Routine
  704. *
  705. * @pdev: PCI device structure
  706. * @ent: entry in kcs_pci_tbl
  707. *
  708. * returns 0 on success, <0 on failure.
  709. */
  710. static int mei_probe(struct pci_dev *pdev,
  711. const struct pci_device_id *ent)
  712. {
  713. struct mei_device *dev;
  714. int err;
  715. mutex_lock(&mei_mutex);
  716. if (!mei_quirk_probe(pdev, ent)) {
  717. err = -ENODEV;
  718. goto end;
  719. }
  720. if (mei_pdev) {
  721. err = -EEXIST;
  722. goto end;
  723. }
  724. /* enable pci dev */
  725. err = pci_enable_device(pdev);
  726. if (err) {
  727. dev_err(&pdev->dev, "failed to enable pci device.\n");
  728. goto end;
  729. }
  730. /* set PCI host mastering */
  731. pci_set_master(pdev);
  732. /* pci request regions for mei driver */
  733. err = pci_request_regions(pdev, KBUILD_MODNAME);
  734. if (err) {
  735. dev_err(&pdev->dev, "failed to get pci regions.\n");
  736. goto disable_device;
  737. }
  738. /* allocates and initializes the mei dev structure */
  739. dev = mei_device_init(pdev);
  740. if (!dev) {
  741. err = -ENOMEM;
  742. goto release_regions;
  743. }
  744. /* mapping IO device memory */
  745. dev->mem_addr = pci_iomap(pdev, 0, 0);
  746. if (!dev->mem_addr) {
  747. dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
  748. err = -ENOMEM;
  749. goto free_device;
  750. }
  751. pci_enable_msi(pdev);
  752. /* request and enable interrupt */
  753. if (pci_dev_msi_enabled(pdev))
  754. err = request_threaded_irq(pdev->irq,
  755. NULL,
  756. mei_interrupt_thread_handler,
  757. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  758. else
  759. err = request_threaded_irq(pdev->irq,
  760. mei_interrupt_quick_handler,
  761. mei_interrupt_thread_handler,
  762. IRQF_SHARED, KBUILD_MODNAME, dev);
  763. if (err) {
  764. dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
  765. pdev->irq);
  766. goto disable_msi;
  767. }
  768. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  769. INIT_WORK(&dev->init_work, mei_host_client_init);
  770. if (mei_hw_init(dev)) {
  771. dev_err(&pdev->dev, "init hw failure.\n");
  772. err = -ENODEV;
  773. goto release_irq;
  774. }
  775. err = misc_register(&mei_misc_device);
  776. if (err)
  777. goto release_irq;
  778. mei_pdev = pdev;
  779. pci_set_drvdata(pdev, dev);
  780. schedule_delayed_work(&dev->timer_work, HZ);
  781. mutex_unlock(&mei_mutex);
  782. pr_debug("initialization successful.\n");
  783. return 0;
  784. release_irq:
  785. /* disable interrupts */
  786. dev->host_hw_state = mei_hcsr_read(dev);
  787. mei_disable_interrupts(dev);
  788. flush_scheduled_work();
  789. free_irq(pdev->irq, dev);
  790. disable_msi:
  791. pci_disable_msi(pdev);
  792. pci_iounmap(pdev, dev->mem_addr);
  793. free_device:
  794. kfree(dev);
  795. release_regions:
  796. pci_release_regions(pdev);
  797. disable_device:
  798. pci_disable_device(pdev);
  799. end:
  800. mutex_unlock(&mei_mutex);
  801. dev_err(&pdev->dev, "initialization failed.\n");
  802. return err;
  803. }
  804. /**
  805. * mei_remove - Device Removal Routine
  806. *
  807. * @pdev: PCI device structure
  808. *
  809. * mei_remove is called by the PCI subsystem to alert the driver
  810. * that it should release a PCI device.
  811. */
  812. static void mei_remove(struct pci_dev *pdev)
  813. {
  814. struct mei_device *dev;
  815. if (mei_pdev != pdev)
  816. return;
  817. dev = pci_get_drvdata(pdev);
  818. if (!dev)
  819. return;
  820. mutex_lock(&dev->device_lock);
  821. cancel_delayed_work(&dev->timer_work);
  822. mei_wd_stop(dev);
  823. mei_pdev = NULL;
  824. if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
  825. dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
  826. mei_cl_disconnect(&dev->iamthif_cl);
  827. }
  828. if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
  829. dev->wd_cl.state = MEI_FILE_DISCONNECTING;
  830. mei_cl_disconnect(&dev->wd_cl);
  831. }
  832. /* Unregistering watchdog device */
  833. mei_watchdog_unregister(dev);
  834. /* remove entry if already in list */
  835. dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
  836. if (dev->open_handle_count > 0)
  837. dev->open_handle_count--;
  838. mei_cl_unlink(&dev->wd_cl);
  839. if (dev->open_handle_count > 0)
  840. dev->open_handle_count--;
  841. mei_cl_unlink(&dev->iamthif_cl);
  842. dev->iamthif_current_cb = NULL;
  843. dev->me_clients_num = 0;
  844. mutex_unlock(&dev->device_lock);
  845. flush_scheduled_work();
  846. /* disable interrupts */
  847. mei_disable_interrupts(dev);
  848. free_irq(pdev->irq, dev);
  849. pci_disable_msi(pdev);
  850. pci_set_drvdata(pdev, NULL);
  851. if (dev->mem_addr)
  852. pci_iounmap(pdev, dev->mem_addr);
  853. kfree(dev);
  854. pci_release_regions(pdev);
  855. pci_disable_device(pdev);
  856. misc_deregister(&mei_misc_device);
  857. }
  858. #ifdef CONFIG_PM
  859. static int mei_pci_suspend(struct device *device)
  860. {
  861. struct pci_dev *pdev = to_pci_dev(device);
  862. struct mei_device *dev = pci_get_drvdata(pdev);
  863. int err;
  864. if (!dev)
  865. return -ENODEV;
  866. mutex_lock(&dev->device_lock);
  867. cancel_delayed_work(&dev->timer_work);
  868. /* Stop watchdog if exists */
  869. err = mei_wd_stop(dev);
  870. /* Set new mei state */
  871. if (dev->dev_state == MEI_DEV_ENABLED ||
  872. dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  873. dev->dev_state = MEI_DEV_POWER_DOWN;
  874. mei_reset(dev, 0);
  875. }
  876. mutex_unlock(&dev->device_lock);
  877. free_irq(pdev->irq, dev);
  878. pci_disable_msi(pdev);
  879. return err;
  880. }
  881. static int mei_pci_resume(struct device *device)
  882. {
  883. struct pci_dev *pdev = to_pci_dev(device);
  884. struct mei_device *dev;
  885. int err;
  886. dev = pci_get_drvdata(pdev);
  887. if (!dev)
  888. return -ENODEV;
  889. pci_enable_msi(pdev);
  890. /* request and enable interrupt */
  891. if (pci_dev_msi_enabled(pdev))
  892. err = request_threaded_irq(pdev->irq,
  893. NULL,
  894. mei_interrupt_thread_handler,
  895. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  896. else
  897. err = request_threaded_irq(pdev->irq,
  898. mei_interrupt_quick_handler,
  899. mei_interrupt_thread_handler,
  900. IRQF_SHARED, KBUILD_MODNAME, dev);
  901. if (err) {
  902. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  903. pdev->irq);
  904. return err;
  905. }
  906. mutex_lock(&dev->device_lock);
  907. dev->dev_state = MEI_DEV_POWER_UP;
  908. mei_reset(dev, 1);
  909. mutex_unlock(&dev->device_lock);
  910. /* Start timer if stopped in suspend */
  911. schedule_delayed_work(&dev->timer_work, HZ);
  912. return err;
  913. }
  914. static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
  915. #define MEI_PM_OPS (&mei_pm_ops)
  916. #else
  917. #define MEI_PM_OPS NULL
  918. #endif /* CONFIG_PM */
  919. /*
  920. * PCI driver structure
  921. */
  922. static struct pci_driver mei_driver = {
  923. .name = KBUILD_MODNAME,
  924. .id_table = mei_pci_tbl,
  925. .probe = mei_probe,
  926. .remove = mei_remove,
  927. .shutdown = mei_remove,
  928. .driver.pm = MEI_PM_OPS,
  929. };
  930. module_pci_driver(mei_driver);
  931. MODULE_AUTHOR("Intel Corporation");
  932. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  933. MODULE_LICENSE("GPL v2");