main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. if (mei_hw_init(dev)) {
  769. dev_err(&pdev->dev, "init hw failure.\n");
  770. err = -ENODEV;
  771. goto release_irq;
  772. }
  773. err = misc_register(&mei_misc_device);
  774. if (err)
  775. goto release_irq;
  776. mei_pdev = pdev;
  777. pci_set_drvdata(pdev, dev);
  778. schedule_delayed_work(&dev->timer_work, HZ);
  779. mutex_unlock(&mei_mutex);
  780. pr_debug("initialization successful.\n");
  781. return 0;
  782. release_irq:
  783. mei_disable_interrupts(dev);
  784. flush_scheduled_work();
  785. free_irq(pdev->irq, dev);
  786. disable_msi:
  787. pci_disable_msi(pdev);
  788. pci_iounmap(pdev, dev->mem_addr);
  789. free_device:
  790. kfree(dev);
  791. release_regions:
  792. pci_release_regions(pdev);
  793. disable_device:
  794. pci_disable_device(pdev);
  795. end:
  796. mutex_unlock(&mei_mutex);
  797. dev_err(&pdev->dev, "initialization failed.\n");
  798. return err;
  799. }
  800. /**
  801. * mei_remove - Device Removal Routine
  802. *
  803. * @pdev: PCI device structure
  804. *
  805. * mei_remove is called by the PCI subsystem to alert the driver
  806. * that it should release a PCI device.
  807. */
  808. static void mei_remove(struct pci_dev *pdev)
  809. {
  810. struct mei_device *dev;
  811. if (mei_pdev != pdev)
  812. return;
  813. dev = pci_get_drvdata(pdev);
  814. if (!dev)
  815. return;
  816. mutex_lock(&dev->device_lock);
  817. cancel_delayed_work(&dev->timer_work);
  818. mei_wd_stop(dev);
  819. mei_pdev = NULL;
  820. if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
  821. dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
  822. mei_cl_disconnect(&dev->iamthif_cl);
  823. }
  824. if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
  825. dev->wd_cl.state = MEI_FILE_DISCONNECTING;
  826. mei_cl_disconnect(&dev->wd_cl);
  827. }
  828. /* Unregistering watchdog device */
  829. mei_watchdog_unregister(dev);
  830. /* remove entry if already in list */
  831. dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
  832. if (dev->open_handle_count > 0)
  833. dev->open_handle_count--;
  834. mei_cl_unlink(&dev->wd_cl);
  835. if (dev->open_handle_count > 0)
  836. dev->open_handle_count--;
  837. mei_cl_unlink(&dev->iamthif_cl);
  838. dev->iamthif_current_cb = NULL;
  839. dev->me_clients_num = 0;
  840. mutex_unlock(&dev->device_lock);
  841. flush_scheduled_work();
  842. /* disable interrupts */
  843. mei_disable_interrupts(dev);
  844. free_irq(pdev->irq, dev);
  845. pci_disable_msi(pdev);
  846. pci_set_drvdata(pdev, NULL);
  847. if (dev->mem_addr)
  848. pci_iounmap(pdev, dev->mem_addr);
  849. kfree(dev);
  850. pci_release_regions(pdev);
  851. pci_disable_device(pdev);
  852. misc_deregister(&mei_misc_device);
  853. }
  854. #ifdef CONFIG_PM
  855. static int mei_pci_suspend(struct device *device)
  856. {
  857. struct pci_dev *pdev = to_pci_dev(device);
  858. struct mei_device *dev = pci_get_drvdata(pdev);
  859. int err;
  860. if (!dev)
  861. return -ENODEV;
  862. mutex_lock(&dev->device_lock);
  863. cancel_delayed_work(&dev->timer_work);
  864. /* Stop watchdog if exists */
  865. err = mei_wd_stop(dev);
  866. /* Set new mei state */
  867. if (dev->dev_state == MEI_DEV_ENABLED ||
  868. dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  869. dev->dev_state = MEI_DEV_POWER_DOWN;
  870. mei_reset(dev, 0);
  871. }
  872. mutex_unlock(&dev->device_lock);
  873. free_irq(pdev->irq, dev);
  874. pci_disable_msi(pdev);
  875. return err;
  876. }
  877. static int mei_pci_resume(struct device *device)
  878. {
  879. struct pci_dev *pdev = to_pci_dev(device);
  880. struct mei_device *dev;
  881. int err;
  882. dev = pci_get_drvdata(pdev);
  883. if (!dev)
  884. return -ENODEV;
  885. pci_enable_msi(pdev);
  886. /* request and enable interrupt */
  887. if (pci_dev_msi_enabled(pdev))
  888. err = request_threaded_irq(pdev->irq,
  889. NULL,
  890. mei_interrupt_thread_handler,
  891. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  892. else
  893. err = request_threaded_irq(pdev->irq,
  894. mei_interrupt_quick_handler,
  895. mei_interrupt_thread_handler,
  896. IRQF_SHARED, KBUILD_MODNAME, dev);
  897. if (err) {
  898. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  899. pdev->irq);
  900. return err;
  901. }
  902. mutex_lock(&dev->device_lock);
  903. dev->dev_state = MEI_DEV_POWER_UP;
  904. mei_reset(dev, 1);
  905. mutex_unlock(&dev->device_lock);
  906. /* Start timer if stopped in suspend */
  907. schedule_delayed_work(&dev->timer_work, HZ);
  908. return err;
  909. }
  910. static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
  911. #define MEI_PM_OPS (&mei_pm_ops)
  912. #else
  913. #define MEI_PM_OPS NULL
  914. #endif /* CONFIG_PM */
  915. /*
  916. * PCI driver structure
  917. */
  918. static struct pci_driver mei_driver = {
  919. .name = KBUILD_MODNAME,
  920. .id_table = mei_pci_tbl,
  921. .probe = mei_probe,
  922. .remove = mei_remove,
  923. .shutdown = mei_remove,
  924. .driver.pm = MEI_PM_OPS,
  925. };
  926. module_pci_driver(mei_driver);
  927. MODULE_AUTHOR("Intel Corporation");
  928. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  929. MODULE_LICENSE("GPL v2");