main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 "mei_dev.h"
  38. #include <linux/mei.h>
  39. #include "interface.h"
  40. static const char mei_driver_name[] = "mei";
  41. /* The device pointer */
  42. /* Currently this driver works as long as there is only a single AMT device. */
  43. struct pci_dev *mei_device;
  44. /* mei_pci_tbl - PCI Device ID Table */
  45. static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
  46. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
  49. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
  50. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
  51. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
  52. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
  53. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
  54. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
  55. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
  56. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
  57. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
  58. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
  59. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
  60. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
  61. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
  62. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
  63. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
  64. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
  65. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
  66. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
  67. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
  68. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
  69. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
  70. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
  71. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
  72. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
  73. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
  74. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
  75. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
  76. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
  77. /* required last entry */
  78. {0, }
  79. };
  80. MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
  81. static DEFINE_MUTEX(mei_mutex);
  82. /**
  83. * mei_clear_list - removes all callbacks associated with file
  84. * from mei_cb_list
  85. *
  86. * @dev: device structure.
  87. * @file: file structure
  88. * @mei_cb_list: callbacks list
  89. *
  90. * mei_clear_list is called to clear resources associated with file
  91. * when application calls close function or Ctrl-C was pressed
  92. *
  93. * returns true if callback removed from the list, false otherwise
  94. */
  95. static bool mei_clear_list(struct mei_device *dev,
  96. struct file *file, struct list_head *mei_cb_list)
  97. {
  98. struct mei_cl_cb *cb_pos = NULL;
  99. struct mei_cl_cb *cb_next = NULL;
  100. struct file *file_temp;
  101. bool removed = false;
  102. /* list all list member */
  103. list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
  104. file_temp = (struct file *)cb_pos->file_object;
  105. /* check if list member associated with a file */
  106. if (file_temp == file) {
  107. /* remove member from the list */
  108. list_del(&cb_pos->cb_list);
  109. /* check if cb equal to current iamthif cb */
  110. if (dev->iamthif_current_cb == cb_pos) {
  111. dev->iamthif_current_cb = NULL;
  112. /* send flow control to iamthif client */
  113. mei_send_flow_control(dev, &dev->iamthif_cl);
  114. }
  115. /* free all allocated buffers */
  116. mei_free_cb_private(cb_pos);
  117. cb_pos = NULL;
  118. removed = true;
  119. }
  120. }
  121. return removed;
  122. }
  123. /**
  124. * mei_clear_lists - removes all callbacks associated with file
  125. *
  126. * @dev: device structure
  127. * @file: file structure
  128. *
  129. * mei_clear_lists is called to clear resources associated with file
  130. * when application calls close function or Ctrl-C was pressed
  131. *
  132. * returns true if callback removed from the list, false otherwise
  133. */
  134. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  135. {
  136. bool removed = false;
  137. /* remove callbacks associated with a file */
  138. mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
  139. if (mei_clear_list(dev, file,
  140. &dev->amthi_read_complete_list.mei_cb.cb_list))
  141. removed = true;
  142. mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
  143. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
  144. removed = true;
  145. if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
  146. removed = true;
  147. if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
  148. removed = true;
  149. /* check if iamthif_current_cb not NULL */
  150. if (dev->iamthif_current_cb && !removed) {
  151. /* check file and iamthif current cb association */
  152. if (dev->iamthif_current_cb->file_object == file) {
  153. /* remove cb */
  154. mei_free_cb_private(dev->iamthif_current_cb);
  155. dev->iamthif_current_cb = NULL;
  156. removed = true;
  157. }
  158. }
  159. return removed;
  160. }
  161. /**
  162. * find_read_list_entry - find read list entry
  163. *
  164. * @dev: device structure
  165. * @file: pointer to file structure
  166. *
  167. * returns cb on success, NULL on error
  168. */
  169. static struct mei_cl_cb *find_read_list_entry(
  170. struct mei_device *dev,
  171. struct mei_cl *cl)
  172. {
  173. struct mei_cl_cb *pos = NULL;
  174. struct mei_cl_cb *next = NULL;
  175. dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
  176. list_for_each_entry_safe(pos, next,
  177. &dev->read_list.mei_cb.cb_list, cb_list) {
  178. struct mei_cl *cl_temp;
  179. cl_temp = (struct mei_cl *)pos->file_private;
  180. if (mei_cl_cmp_id(cl, cl_temp))
  181. return pos;
  182. }
  183. return NULL;
  184. }
  185. /**
  186. * mei_open - the open function
  187. *
  188. * @inode: pointer to inode structure
  189. * @file: pointer to file structure
  190. *
  191. * returns 0 on success, <0 on error
  192. */
  193. static int mei_open(struct inode *inode, struct file *file)
  194. {
  195. struct mei_cl *cl;
  196. struct mei_device *dev;
  197. unsigned long cl_id;
  198. int err;
  199. err = -ENODEV;
  200. if (!mei_device)
  201. goto out;
  202. dev = pci_get_drvdata(mei_device);
  203. if (!dev)
  204. goto out;
  205. mutex_lock(&dev->device_lock);
  206. err = -ENOMEM;
  207. cl = mei_cl_allocate(dev);
  208. if (!cl)
  209. goto out_unlock;
  210. err = -ENODEV;
  211. if (dev->mei_state != MEI_ENABLED) {
  212. dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
  213. dev->mei_state);
  214. goto out_unlock;
  215. }
  216. err = -EMFILE;
  217. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
  218. goto out_unlock;
  219. cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
  220. if (cl_id >= MEI_CLIENTS_MAX)
  221. goto out_unlock;
  222. cl->host_client_id = cl_id;
  223. dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
  224. dev->open_handle_count++;
  225. list_add_tail(&cl->link, &dev->file_list);
  226. set_bit(cl->host_client_id, dev->host_clients_map);
  227. cl->state = MEI_FILE_INITIALIZING;
  228. cl->sm_state = 0;
  229. file->private_data = cl;
  230. mutex_unlock(&dev->device_lock);
  231. return nonseekable_open(inode, file);
  232. out_unlock:
  233. mutex_unlock(&dev->device_lock);
  234. kfree(cl);
  235. out:
  236. return err;
  237. }
  238. /**
  239. * mei_release - the release function
  240. *
  241. * @inode: pointer to inode structure
  242. * @file: pointer to file structure
  243. *
  244. * returns 0 on success, <0 on error
  245. */
  246. static int mei_release(struct inode *inode, struct file *file)
  247. {
  248. struct mei_cl *cl = file->private_data;
  249. struct mei_cl_cb *cb;
  250. struct mei_device *dev;
  251. int rets = 0;
  252. if (WARN_ON(!cl || !cl->dev))
  253. return -ENODEV;
  254. dev = cl->dev;
  255. mutex_lock(&dev->device_lock);
  256. if (cl != &dev->iamthif_cl) {
  257. if (cl->state == MEI_FILE_CONNECTED) {
  258. cl->state = MEI_FILE_DISCONNECTING;
  259. dev_dbg(&dev->pdev->dev,
  260. "disconnecting client host client = %d, "
  261. "ME client = %d\n",
  262. cl->host_client_id,
  263. cl->me_client_id);
  264. rets = mei_disconnect_host_client(dev, cl);
  265. }
  266. mei_cl_flush_queues(cl);
  267. dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
  268. cl->host_client_id,
  269. cl->me_client_id);
  270. if (dev->open_handle_count > 0) {
  271. clear_bit(cl->host_client_id, dev->host_clients_map);
  272. dev->open_handle_count--;
  273. }
  274. mei_remove_client_from_file_list(dev, cl->host_client_id);
  275. /* free read cb */
  276. cb = NULL;
  277. if (cl->read_cb) {
  278. cb = find_read_list_entry(dev, cl);
  279. /* Remove entry from read list */
  280. if (cb)
  281. list_del(&cb->cb_list);
  282. cb = cl->read_cb;
  283. cl->read_cb = NULL;
  284. }
  285. file->private_data = NULL;
  286. if (cb) {
  287. mei_free_cb_private(cb);
  288. cb = NULL;
  289. }
  290. kfree(cl);
  291. } else {
  292. if (dev->open_handle_count > 0)
  293. dev->open_handle_count--;
  294. if (dev->iamthif_file_object == file &&
  295. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  296. dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
  297. dev->iamthif_state);
  298. dev->iamthif_canceled = true;
  299. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  300. dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
  301. mei_run_next_iamthif_cmd(dev);
  302. }
  303. }
  304. if (mei_clear_lists(dev, file))
  305. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  306. }
  307. mutex_unlock(&dev->device_lock);
  308. return rets;
  309. }
  310. /**
  311. * mei_read - the read function.
  312. *
  313. * @file: pointer to file structure
  314. * @ubuf: pointer to user buffer
  315. * @length: buffer length
  316. * @offset: data offset in buffer
  317. *
  318. * returns >=0 data length on success , <0 on error
  319. */
  320. static ssize_t mei_read(struct file *file, char __user *ubuf,
  321. size_t length, loff_t *offset)
  322. {
  323. struct mei_cl *cl = file->private_data;
  324. struct mei_cl_cb *cb_pos = NULL;
  325. struct mei_cl_cb *cb = NULL;
  326. struct mei_device *dev;
  327. int i;
  328. int rets;
  329. int err;
  330. if (WARN_ON(!cl || !cl->dev))
  331. return -ENODEV;
  332. dev = cl->dev;
  333. mutex_lock(&dev->device_lock);
  334. if (dev->mei_state != MEI_ENABLED) {
  335. rets = -ENODEV;
  336. goto out;
  337. }
  338. if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
  339. /* Do not allow to read watchdog client */
  340. i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
  341. if (i >= 0) {
  342. struct mei_me_client *me_client = &dev->me_clients[i];
  343. if (cl->me_client_id == me_client->client_id) {
  344. rets = -EBADF;
  345. goto out;
  346. }
  347. }
  348. } else {
  349. cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  350. }
  351. if (cl == &dev->iamthif_cl) {
  352. rets = amthi_read(dev, file, ubuf, length, offset);
  353. goto out;
  354. }
  355. if (cl->read_cb && cl->read_cb->information > *offset) {
  356. cb = cl->read_cb;
  357. goto copy_buffer;
  358. } else if (cl->read_cb && cl->read_cb->information > 0 &&
  359. cl->read_cb->information <= *offset) {
  360. cb = cl->read_cb;
  361. rets = 0;
  362. goto free;
  363. } else if ((!cl->read_cb || !cl->read_cb->information) &&
  364. *offset > 0) {
  365. /*Offset needs to be cleaned for contiguous reads*/
  366. *offset = 0;
  367. rets = 0;
  368. goto out;
  369. }
  370. err = mei_start_read(dev, cl);
  371. if (err && err != -EBUSY) {
  372. dev_dbg(&dev->pdev->dev,
  373. "mei start read failure with status = %d\n", err);
  374. rets = err;
  375. goto out;
  376. }
  377. if (MEI_READ_COMPLETE != cl->reading_state &&
  378. !waitqueue_active(&cl->rx_wait)) {
  379. if (file->f_flags & O_NONBLOCK) {
  380. rets = -EAGAIN;
  381. goto out;
  382. }
  383. mutex_unlock(&dev->device_lock);
  384. if (wait_event_interruptible(cl->rx_wait,
  385. (MEI_READ_COMPLETE == cl->reading_state ||
  386. MEI_FILE_INITIALIZING == cl->state ||
  387. MEI_FILE_DISCONNECTED == cl->state ||
  388. MEI_FILE_DISCONNECTING == cl->state))) {
  389. if (signal_pending(current))
  390. return -EINTR;
  391. return -ERESTARTSYS;
  392. }
  393. mutex_lock(&dev->device_lock);
  394. if (MEI_FILE_INITIALIZING == cl->state ||
  395. MEI_FILE_DISCONNECTED == cl->state ||
  396. MEI_FILE_DISCONNECTING == cl->state) {
  397. rets = -EBUSY;
  398. goto out;
  399. }
  400. }
  401. cb = cl->read_cb;
  402. if (!cb) {
  403. rets = -ENODEV;
  404. goto out;
  405. }
  406. if (cl->reading_state != MEI_READ_COMPLETE) {
  407. rets = 0;
  408. goto out;
  409. }
  410. /* now copy the data to user space */
  411. copy_buffer:
  412. dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
  413. cb->response_buffer.size);
  414. dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
  415. cb->information);
  416. if (length == 0 || ubuf == NULL || *offset > cb->information) {
  417. rets = -EMSGSIZE;
  418. goto free;
  419. }
  420. /* length is being truncated to PAGE_SIZE, however, */
  421. /* information size may be longer */
  422. length = min_t(size_t, length, (cb->information - *offset));
  423. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
  424. rets = -EFAULT;
  425. goto free;
  426. }
  427. rets = length;
  428. *offset += length;
  429. if ((unsigned long)*offset < cb->information)
  430. goto out;
  431. free:
  432. cb_pos = find_read_list_entry(dev, cl);
  433. /* Remove entry from read list */
  434. if (cb_pos)
  435. list_del(&cb_pos->cb_list);
  436. mei_free_cb_private(cb);
  437. cl->reading_state = MEI_IDLE;
  438. cl->read_cb = NULL;
  439. cl->read_pending = 0;
  440. out:
  441. dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
  442. mutex_unlock(&dev->device_lock);
  443. return rets;
  444. }
  445. /**
  446. * mei_write - the write function.
  447. *
  448. * @file: pointer to file structure
  449. * @ubuf: pointer to user buffer
  450. * @length: buffer length
  451. * @offset: data offset in buffer
  452. *
  453. * returns >=0 data length on success , <0 on error
  454. */
  455. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  456. size_t length, loff_t *offset)
  457. {
  458. struct mei_cl *cl = file->private_data;
  459. struct mei_cl_cb *write_cb = NULL;
  460. struct mei_msg_hdr mei_hdr;
  461. struct mei_device *dev;
  462. unsigned long timeout = 0;
  463. int rets;
  464. int i;
  465. if (WARN_ON(!cl || !cl->dev))
  466. return -ENODEV;
  467. dev = cl->dev;
  468. mutex_lock(&dev->device_lock);
  469. if (dev->mei_state != MEI_ENABLED) {
  470. mutex_unlock(&dev->device_lock);
  471. return -ENODEV;
  472. }
  473. if (cl == &dev->iamthif_cl) {
  474. write_cb = find_amthi_read_list_entry(dev, file);
  475. if (write_cb) {
  476. timeout = write_cb->read_time +
  477. msecs_to_jiffies(IAMTHIF_READ_TIMER);
  478. if (time_after(jiffies, timeout) ||
  479. cl->reading_state == MEI_READ_COMPLETE) {
  480. *offset = 0;
  481. list_del(&write_cb->cb_list);
  482. mei_free_cb_private(write_cb);
  483. write_cb = NULL;
  484. }
  485. }
  486. }
  487. /* free entry used in read */
  488. if (cl->reading_state == MEI_READ_COMPLETE) {
  489. *offset = 0;
  490. write_cb = find_read_list_entry(dev, cl);
  491. if (write_cb) {
  492. list_del(&write_cb->cb_list);
  493. mei_free_cb_private(write_cb);
  494. write_cb = NULL;
  495. cl->reading_state = MEI_IDLE;
  496. cl->read_cb = NULL;
  497. cl->read_pending = 0;
  498. }
  499. } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
  500. *offset = 0;
  501. write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  502. if (!write_cb) {
  503. mutex_unlock(&dev->device_lock);
  504. return -ENOMEM;
  505. }
  506. write_cb->file_object = file;
  507. write_cb->file_private = cl;
  508. write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
  509. rets = -ENOMEM;
  510. if (!write_cb->request_buffer.data)
  511. goto unlock_dev;
  512. dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
  513. rets = -EFAULT;
  514. if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
  515. goto unlock_dev;
  516. cl->sm_state = 0;
  517. if (length == 4 &&
  518. ((memcmp(mei_wd_state_independence_msg[0],
  519. write_cb->request_buffer.data, 4) == 0) ||
  520. (memcmp(mei_wd_state_independence_msg[1],
  521. write_cb->request_buffer.data, 4) == 0) ||
  522. (memcmp(mei_wd_state_independence_msg[2],
  523. write_cb->request_buffer.data, 4) == 0)))
  524. cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  525. INIT_LIST_HEAD(&write_cb->cb_list);
  526. if (cl == &dev->iamthif_cl) {
  527. write_cb->response_buffer.data =
  528. kmalloc(dev->iamthif_mtu, GFP_KERNEL);
  529. if (!write_cb->response_buffer.data) {
  530. rets = -ENOMEM;
  531. goto unlock_dev;
  532. }
  533. if (dev->mei_state != MEI_ENABLED) {
  534. rets = -ENODEV;
  535. goto unlock_dev;
  536. }
  537. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  538. if (i < 0) {
  539. rets = -ENODEV;
  540. goto unlock_dev;
  541. }
  542. if (length > dev->me_clients[i].props.max_msg_length ||
  543. length <= 0) {
  544. rets = -EMSGSIZE;
  545. goto unlock_dev;
  546. }
  547. write_cb->response_buffer.size = dev->iamthif_mtu;
  548. write_cb->major_file_operations = MEI_IOCTL;
  549. write_cb->information = 0;
  550. write_cb->request_buffer.size = length;
  551. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  552. rets = -ENODEV;
  553. goto unlock_dev;
  554. }
  555. if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
  556. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  557. dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
  558. (int) dev->iamthif_state);
  559. dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
  560. list_add_tail(&write_cb->cb_list,
  561. &dev->amthi_cmd_list.mei_cb.cb_list);
  562. rets = length;
  563. } else {
  564. dev_dbg(&dev->pdev->dev, "call amthi write\n");
  565. rets = amthi_write(dev, write_cb);
  566. if (rets) {
  567. dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
  568. rets);
  569. goto unlock_dev;
  570. }
  571. rets = length;
  572. }
  573. mutex_unlock(&dev->device_lock);
  574. return rets;
  575. }
  576. write_cb->major_file_operations = MEI_WRITE;
  577. /* make sure information is zero before we start */
  578. write_cb->information = 0;
  579. write_cb->request_buffer.size = length;
  580. dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
  581. cl->host_client_id, cl->me_client_id);
  582. if (cl->state != MEI_FILE_CONNECTED) {
  583. rets = -ENODEV;
  584. dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
  585. cl->host_client_id,
  586. cl->me_client_id);
  587. goto unlock_dev;
  588. }
  589. i = mei_me_cl_by_id(dev, cl->me_client_id);
  590. if (i < 0) {
  591. rets = -ENODEV;
  592. goto unlock_dev;
  593. }
  594. if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
  595. rets = -EINVAL;
  596. goto unlock_dev;
  597. }
  598. write_cb->file_private = cl;
  599. rets = mei_flow_ctrl_creds(dev, cl);
  600. if (rets < 0)
  601. goto unlock_dev;
  602. if (rets && dev->mei_host_buffer_is_empty) {
  603. rets = 0;
  604. dev->mei_host_buffer_is_empty = false;
  605. if (length > mei_hbuf_max_data(dev)) {
  606. mei_hdr.length = mei_hbuf_max_data(dev);
  607. mei_hdr.msg_complete = 0;
  608. } else {
  609. mei_hdr.length = length;
  610. mei_hdr.msg_complete = 1;
  611. }
  612. mei_hdr.host_addr = cl->host_client_id;
  613. mei_hdr.me_addr = cl->me_client_id;
  614. mei_hdr.reserved = 0;
  615. dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
  616. *((u32 *) &mei_hdr));
  617. if (mei_write_message(dev, &mei_hdr,
  618. (unsigned char *) (write_cb->request_buffer.data),
  619. mei_hdr.length)) {
  620. rets = -ENODEV;
  621. goto unlock_dev;
  622. }
  623. cl->writing_state = MEI_WRITING;
  624. write_cb->information = mei_hdr.length;
  625. if (mei_hdr.msg_complete) {
  626. if (mei_flow_ctrl_reduce(dev, cl)) {
  627. rets = -ENODEV;
  628. goto unlock_dev;
  629. }
  630. list_add_tail(&write_cb->cb_list,
  631. &dev->write_waiting_list.mei_cb.cb_list);
  632. } else {
  633. list_add_tail(&write_cb->cb_list,
  634. &dev->write_list.mei_cb.cb_list);
  635. }
  636. } else {
  637. write_cb->information = 0;
  638. cl->writing_state = MEI_WRITING;
  639. list_add_tail(&write_cb->cb_list,
  640. &dev->write_list.mei_cb.cb_list);
  641. }
  642. mutex_unlock(&dev->device_lock);
  643. return length;
  644. unlock_dev:
  645. mutex_unlock(&dev->device_lock);
  646. mei_free_cb_private(write_cb);
  647. return rets;
  648. }
  649. /**
  650. * mei_ioctl - the IOCTL function
  651. *
  652. * @file: pointer to file structure
  653. * @cmd: ioctl command
  654. * @data: pointer to mei message structure
  655. *
  656. * returns 0 on success , <0 on error
  657. */
  658. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  659. {
  660. struct mei_device *dev;
  661. struct mei_cl *cl = file->private_data;
  662. struct mei_connect_client_data *connect_data = NULL;
  663. int rets;
  664. if (cmd != IOCTL_MEI_CONNECT_CLIENT)
  665. return -EINVAL;
  666. if (WARN_ON(!cl || !cl->dev))
  667. return -ENODEV;
  668. dev = cl->dev;
  669. dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
  670. mutex_lock(&dev->device_lock);
  671. if (dev->mei_state != MEI_ENABLED) {
  672. rets = -ENODEV;
  673. goto out;
  674. }
  675. dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  676. connect_data = kzalloc(sizeof(struct mei_connect_client_data),
  677. GFP_KERNEL);
  678. if (!connect_data) {
  679. rets = -ENOMEM;
  680. goto out;
  681. }
  682. dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
  683. if (copy_from_user(connect_data, (char __user *)data,
  684. sizeof(struct mei_connect_client_data))) {
  685. dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
  686. rets = -EFAULT;
  687. goto out;
  688. }
  689. rets = mei_ioctl_connect_client(file, connect_data);
  690. /* if all is ok, copying the data back to user. */
  691. if (rets)
  692. goto out;
  693. dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
  694. if (copy_to_user((char __user *)data, connect_data,
  695. sizeof(struct mei_connect_client_data))) {
  696. dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
  697. rets = -EFAULT;
  698. goto out;
  699. }
  700. out:
  701. kfree(connect_data);
  702. mutex_unlock(&dev->device_lock);
  703. return rets;
  704. }
  705. /**
  706. * mei_compat_ioctl - the compat IOCTL function
  707. *
  708. * @file: pointer to file structure
  709. * @cmd: ioctl command
  710. * @data: pointer to mei message structure
  711. *
  712. * returns 0 on success , <0 on error
  713. */
  714. #ifdef CONFIG_COMPAT
  715. static long mei_compat_ioctl(struct file *file,
  716. unsigned int cmd, unsigned long data)
  717. {
  718. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  719. }
  720. #endif
  721. /**
  722. * mei_poll - the poll function
  723. *
  724. * @file: pointer to file structure
  725. * @wait: pointer to poll_table structure
  726. *
  727. * returns poll mask
  728. */
  729. static unsigned int mei_poll(struct file *file, poll_table *wait)
  730. {
  731. struct mei_cl *cl = file->private_data;
  732. struct mei_device *dev;
  733. unsigned int mask = 0;
  734. if (WARN_ON(!cl || !cl->dev))
  735. return mask;
  736. dev = cl->dev;
  737. mutex_lock(&dev->device_lock);
  738. if (dev->mei_state != MEI_ENABLED)
  739. goto out;
  740. if (cl == &dev->iamthif_cl) {
  741. mutex_unlock(&dev->device_lock);
  742. poll_wait(file, &dev->iamthif_cl.wait, wait);
  743. mutex_lock(&dev->device_lock);
  744. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
  745. dev->iamthif_file_object == file) {
  746. mask |= (POLLIN | POLLRDNORM);
  747. dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
  748. mei_run_next_iamthif_cmd(dev);
  749. }
  750. goto out;
  751. }
  752. mutex_unlock(&dev->device_lock);
  753. poll_wait(file, &cl->tx_wait, wait);
  754. mutex_lock(&dev->device_lock);
  755. if (MEI_WRITE_COMPLETE == cl->writing_state)
  756. mask |= (POLLIN | POLLRDNORM);
  757. out:
  758. mutex_unlock(&dev->device_lock);
  759. return mask;
  760. }
  761. /*
  762. * file operations structure will be used for mei char device.
  763. */
  764. static const struct file_operations mei_fops = {
  765. .owner = THIS_MODULE,
  766. .read = mei_read,
  767. .unlocked_ioctl = mei_ioctl,
  768. #ifdef CONFIG_COMPAT
  769. .compat_ioctl = mei_compat_ioctl,
  770. #endif
  771. .open = mei_open,
  772. .release = mei_release,
  773. .write = mei_write,
  774. .poll = mei_poll,
  775. .llseek = no_llseek
  776. };
  777. /*
  778. * Misc Device Struct
  779. */
  780. static struct miscdevice mei_misc_device = {
  781. .name = "mei",
  782. .fops = &mei_fops,
  783. .minor = MISC_DYNAMIC_MINOR,
  784. };
  785. /**
  786. * mei_quirk_probe - probe for devices that doesn't valid ME interface
  787. * @pdev: PCI device structure
  788. * @ent: entry into pci_device_table
  789. *
  790. * returns true if ME Interface is valid, false otherwise
  791. */
  792. static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
  793. const struct pci_device_id *ent)
  794. {
  795. u32 reg;
  796. if (ent->device == MEI_DEV_ID_PBG_1) {
  797. pci_read_config_dword(pdev, 0x48, &reg);
  798. /* make sure that bit 9 is up and bit 10 is down */
  799. if ((reg & 0x600) == 0x200) {
  800. dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
  801. return false;
  802. }
  803. }
  804. return true;
  805. }
  806. /**
  807. * mei_probe - Device Initialization Routine
  808. *
  809. * @pdev: PCI device structure
  810. * @ent: entry in kcs_pci_tbl
  811. *
  812. * returns 0 on success, <0 on failure.
  813. */
  814. static int __devinit mei_probe(struct pci_dev *pdev,
  815. const struct pci_device_id *ent)
  816. {
  817. struct mei_device *dev;
  818. int err;
  819. mutex_lock(&mei_mutex);
  820. if (!mei_quirk_probe(pdev, ent)) {
  821. err = -ENODEV;
  822. goto end;
  823. }
  824. if (mei_device) {
  825. err = -EEXIST;
  826. goto end;
  827. }
  828. /* enable pci dev */
  829. err = pci_enable_device(pdev);
  830. if (err) {
  831. dev_err(&pdev->dev, "failed to enable pci device.\n");
  832. goto end;
  833. }
  834. /* set PCI host mastering */
  835. pci_set_master(pdev);
  836. /* pci request regions for mei driver */
  837. err = pci_request_regions(pdev, mei_driver_name);
  838. if (err) {
  839. dev_err(&pdev->dev, "failed to get pci regions.\n");
  840. goto disable_device;
  841. }
  842. /* allocates and initializes the mei dev structure */
  843. dev = mei_device_init(pdev);
  844. if (!dev) {
  845. err = -ENOMEM;
  846. goto release_regions;
  847. }
  848. /* mapping IO device memory */
  849. dev->mem_addr = pci_iomap(pdev, 0, 0);
  850. if (!dev->mem_addr) {
  851. dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
  852. err = -ENOMEM;
  853. goto free_device;
  854. }
  855. pci_enable_msi(pdev);
  856. /* request and enable interrupt */
  857. if (pci_dev_msi_enabled(pdev))
  858. err = request_threaded_irq(pdev->irq,
  859. NULL,
  860. mei_interrupt_thread_handler,
  861. IRQF_ONESHOT, mei_driver_name, dev);
  862. else
  863. err = request_threaded_irq(pdev->irq,
  864. mei_interrupt_quick_handler,
  865. mei_interrupt_thread_handler,
  866. IRQF_SHARED, mei_driver_name, dev);
  867. if (err) {
  868. dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
  869. pdev->irq);
  870. goto disable_msi;
  871. }
  872. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  873. if (mei_hw_init(dev)) {
  874. dev_err(&pdev->dev, "init hw failure.\n");
  875. err = -ENODEV;
  876. goto release_irq;
  877. }
  878. err = misc_register(&mei_misc_device);
  879. if (err)
  880. goto release_irq;
  881. mei_device = pdev;
  882. pci_set_drvdata(pdev, dev);
  883. schedule_delayed_work(&dev->timer_work, HZ);
  884. mutex_unlock(&mei_mutex);
  885. pr_debug("initialization successful.\n");
  886. return 0;
  887. release_irq:
  888. /* disable interrupts */
  889. dev->host_hw_state = mei_hcsr_read(dev);
  890. mei_disable_interrupts(dev);
  891. flush_scheduled_work();
  892. free_irq(pdev->irq, dev);
  893. disable_msi:
  894. pci_disable_msi(pdev);
  895. pci_iounmap(pdev, dev->mem_addr);
  896. free_device:
  897. kfree(dev);
  898. release_regions:
  899. pci_release_regions(pdev);
  900. disable_device:
  901. pci_disable_device(pdev);
  902. end:
  903. mutex_unlock(&mei_mutex);
  904. dev_err(&pdev->dev, "initialization failed.\n");
  905. return err;
  906. }
  907. /**
  908. * mei_remove - Device Removal Routine
  909. *
  910. * @pdev: PCI device structure
  911. *
  912. * mei_remove is called by the PCI subsystem to alert the driver
  913. * that it should release a PCI device.
  914. */
  915. static void __devexit mei_remove(struct pci_dev *pdev)
  916. {
  917. struct mei_device *dev;
  918. if (mei_device != pdev)
  919. return;
  920. dev = pci_get_drvdata(pdev);
  921. if (!dev)
  922. return;
  923. mutex_lock(&dev->device_lock);
  924. mei_wd_stop(dev, false);
  925. mei_device = NULL;
  926. if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
  927. dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
  928. mei_disconnect_host_client(dev, &dev->iamthif_cl);
  929. }
  930. if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
  931. dev->wd_cl.state = MEI_FILE_DISCONNECTING;
  932. mei_disconnect_host_client(dev, &dev->wd_cl);
  933. }
  934. /* Unregistering watchdog device */
  935. mei_watchdog_unregister(dev);
  936. /* remove entry if already in list */
  937. dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
  938. mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
  939. mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
  940. dev->iamthif_current_cb = NULL;
  941. dev->me_clients_num = 0;
  942. mutex_unlock(&dev->device_lock);
  943. flush_scheduled_work();
  944. /* disable interrupts */
  945. mei_disable_interrupts(dev);
  946. free_irq(pdev->irq, dev);
  947. pci_disable_msi(pdev);
  948. pci_set_drvdata(pdev, NULL);
  949. if (dev->mem_addr)
  950. pci_iounmap(pdev, dev->mem_addr);
  951. kfree(dev);
  952. pci_release_regions(pdev);
  953. pci_disable_device(pdev);
  954. misc_deregister(&mei_misc_device);
  955. }
  956. #ifdef CONFIG_PM
  957. static int mei_pci_suspend(struct device *device)
  958. {
  959. struct pci_dev *pdev = to_pci_dev(device);
  960. struct mei_device *dev = pci_get_drvdata(pdev);
  961. int err;
  962. if (!dev)
  963. return -ENODEV;
  964. mutex_lock(&dev->device_lock);
  965. /* Stop watchdog if exists */
  966. err = mei_wd_stop(dev, true);
  967. /* Set new mei state */
  968. if (dev->mei_state == MEI_ENABLED ||
  969. dev->mei_state == MEI_RECOVERING_FROM_RESET) {
  970. dev->mei_state = MEI_POWER_DOWN;
  971. mei_reset(dev, 0);
  972. }
  973. mutex_unlock(&dev->device_lock);
  974. free_irq(pdev->irq, dev);
  975. pci_disable_msi(pdev);
  976. return err;
  977. }
  978. static int mei_pci_resume(struct device *device)
  979. {
  980. struct pci_dev *pdev = to_pci_dev(device);
  981. struct mei_device *dev;
  982. int err;
  983. dev = pci_get_drvdata(pdev);
  984. if (!dev)
  985. return -ENODEV;
  986. pci_enable_msi(pdev);
  987. /* request and enable interrupt */
  988. if (pci_dev_msi_enabled(pdev))
  989. err = request_threaded_irq(pdev->irq,
  990. NULL,
  991. mei_interrupt_thread_handler,
  992. IRQF_ONESHOT, mei_driver_name, dev);
  993. else
  994. err = request_threaded_irq(pdev->irq,
  995. mei_interrupt_quick_handler,
  996. mei_interrupt_thread_handler,
  997. IRQF_SHARED, mei_driver_name, dev);
  998. if (err) {
  999. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  1000. pdev->irq);
  1001. return err;
  1002. }
  1003. mutex_lock(&dev->device_lock);
  1004. dev->mei_state = MEI_POWER_UP;
  1005. mei_reset(dev, 1);
  1006. mutex_unlock(&dev->device_lock);
  1007. /* Start timer if stopped in suspend */
  1008. schedule_delayed_work(&dev->timer_work, HZ);
  1009. return err;
  1010. }
  1011. static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
  1012. #define MEI_PM_OPS (&mei_pm_ops)
  1013. #else
  1014. #define MEI_PM_OPS NULL
  1015. #endif /* CONFIG_PM */
  1016. /*
  1017. * PCI driver structure
  1018. */
  1019. static struct pci_driver mei_driver = {
  1020. .name = mei_driver_name,
  1021. .id_table = mei_pci_tbl,
  1022. .probe = mei_probe,
  1023. .remove = __devexit_p(mei_remove),
  1024. .shutdown = __devexit_p(mei_remove),
  1025. .driver.pm = MEI_PM_OPS,
  1026. };
  1027. module_pci_driver(mei_driver);
  1028. MODULE_AUTHOR("Intel Corporation");
  1029. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  1030. MODULE_LICENSE("GPL v2");