main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2003-2022, Intel Corporation. All rights reserved.
  4. * Intel Management Engine Interface (Intel MEI) Linux driver
  5. */
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/slab.h>
  11. #include <linux/fs.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/poll.h>
  16. #include <linux/init.h>
  17. #include <linux/ioctl.h>
  18. #include <linux/cdev.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/compat.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mei.h>
  24. #include "mei_dev.h"
  25. #include "client.h"
  26. static const struct class mei_class = {
  27. .name = "mei",
  28. };
  29. static dev_t mei_devt;
  30. #define MEI_MAX_DEVS MINORMASK
  31. static DEFINE_MUTEX(mei_minor_lock);
  32. static DEFINE_IDR(mei_idr);
  33. /**
  34. * mei_open - the open function
  35. *
  36. * @inode: pointer to inode structure
  37. * @file: pointer to file structure
  38. *
  39. * Return: 0 on success, <0 on error
  40. */
  41. static int mei_open(struct inode *inode, struct file *file)
  42. {
  43. struct mei_device *dev;
  44. struct mei_cl *cl;
  45. int err;
  46. dev = container_of(inode->i_cdev, struct mei_device, cdev);
  47. mutex_lock(&dev->device_lock);
  48. if (dev->dev_state != MEI_DEV_ENABLED) {
  49. dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  50. mei_dev_state_str(dev->dev_state));
  51. err = -ENODEV;
  52. goto err_unlock;
  53. }
  54. cl = mei_cl_alloc_linked(dev);
  55. if (IS_ERR(cl)) {
  56. err = PTR_ERR(cl);
  57. goto err_unlock;
  58. }
  59. cl->fp = file;
  60. file->private_data = cl;
  61. mutex_unlock(&dev->device_lock);
  62. return nonseekable_open(inode, file);
  63. err_unlock:
  64. mutex_unlock(&dev->device_lock);
  65. return err;
  66. }
  67. /**
  68. * mei_cl_vtag_remove_by_fp - remove vtag that corresponds to fp from list
  69. *
  70. * @cl: host client
  71. * @fp: pointer to file structure
  72. *
  73. */
  74. static void mei_cl_vtag_remove_by_fp(const struct mei_cl *cl,
  75. const struct file *fp)
  76. {
  77. struct mei_cl_vtag *vtag_l, *next;
  78. list_for_each_entry_safe(vtag_l, next, &cl->vtag_map, list) {
  79. if (vtag_l->fp == fp) {
  80. list_del(&vtag_l->list);
  81. kfree(vtag_l);
  82. return;
  83. }
  84. }
  85. }
  86. /**
  87. * mei_release - the release function
  88. *
  89. * @inode: pointer to inode structure
  90. * @file: pointer to file structure
  91. *
  92. * Return: 0 on success, <0 on error
  93. */
  94. static int mei_release(struct inode *inode, struct file *file)
  95. {
  96. struct mei_cl *cl = file->private_data;
  97. struct mei_device *dev;
  98. int rets;
  99. if (WARN_ON(!cl || !cl->dev))
  100. return -ENODEV;
  101. dev = cl->dev;
  102. mutex_lock(&dev->device_lock);
  103. mei_cl_vtag_remove_by_fp(cl, file);
  104. if (!list_empty(&cl->vtag_map)) {
  105. cl_dbg(dev, cl, "not the last vtag\n");
  106. mei_cl_flush_queues(cl, file);
  107. rets = 0;
  108. goto out;
  109. }
  110. rets = mei_cl_disconnect(cl);
  111. /*
  112. * Check again: This is necessary since disconnect releases the lock
  113. * and another client can connect in the meantime.
  114. */
  115. if (!list_empty(&cl->vtag_map)) {
  116. cl_dbg(dev, cl, "not the last vtag after disconnect\n");
  117. mei_cl_flush_queues(cl, file);
  118. goto out;
  119. }
  120. mei_cl_flush_queues(cl, NULL);
  121. cl_dbg(dev, cl, "removing\n");
  122. mei_cl_unlink(cl);
  123. kfree(cl);
  124. out:
  125. file->private_data = NULL;
  126. mutex_unlock(&dev->device_lock);
  127. return rets;
  128. }
  129. /**
  130. * mei_read - the read function.
  131. *
  132. * @file: pointer to file structure
  133. * @ubuf: pointer to user buffer
  134. * @length: buffer length
  135. * @offset: data offset in buffer
  136. *
  137. * Return: >=0 data length on success , <0 on error
  138. */
  139. static ssize_t mei_read(struct file *file, char __user *ubuf,
  140. size_t length, loff_t *offset)
  141. {
  142. struct mei_cl *cl = file->private_data;
  143. struct mei_device *dev;
  144. struct mei_cl_cb *cb = NULL;
  145. bool nonblock = !!(file->f_flags & O_NONBLOCK);
  146. ssize_t rets;
  147. if (WARN_ON(!cl || !cl->dev))
  148. return -ENODEV;
  149. dev = cl->dev;
  150. mutex_lock(&dev->device_lock);
  151. if (dev->dev_state != MEI_DEV_ENABLED) {
  152. rets = -ENODEV;
  153. goto out;
  154. }
  155. if (length == 0) {
  156. rets = 0;
  157. goto out;
  158. }
  159. if (ubuf == NULL) {
  160. rets = -EMSGSIZE;
  161. goto out;
  162. }
  163. cb = mei_cl_read_cb(cl, file);
  164. if (cb)
  165. goto copy_buffer;
  166. if (*offset > 0)
  167. *offset = 0;
  168. rets = mei_cl_read_start(cl, length, file);
  169. if (rets && rets != -EBUSY) {
  170. cl_dbg(dev, cl, "mei start read failure status = %zd\n", rets);
  171. goto out;
  172. }
  173. if (nonblock) {
  174. rets = -EAGAIN;
  175. goto out;
  176. }
  177. mutex_unlock(&dev->device_lock);
  178. if (wait_event_interruptible(cl->rx_wait,
  179. mei_cl_read_cb(cl, file) ||
  180. !mei_cl_is_connected(cl))) {
  181. if (signal_pending(current))
  182. return -EINTR;
  183. return -ERESTARTSYS;
  184. }
  185. mutex_lock(&dev->device_lock);
  186. if (!mei_cl_is_connected(cl)) {
  187. rets = -ENODEV;
  188. goto out;
  189. }
  190. cb = mei_cl_read_cb(cl, file);
  191. if (!cb) {
  192. rets = 0;
  193. goto out;
  194. }
  195. copy_buffer:
  196. /* now copy the data to user space */
  197. if (cb->status) {
  198. rets = cb->status;
  199. cl_dbg(dev, cl, "read operation failed %zd\n", rets);
  200. goto free;
  201. }
  202. cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
  203. cb->buf.size, cb->buf_idx, *offset);
  204. if (*offset >= cb->buf_idx) {
  205. rets = 0;
  206. goto free;
  207. }
  208. /* length is being truncated to PAGE_SIZE,
  209. * however buf_idx may point beyond that */
  210. length = min_t(size_t, length, cb->buf_idx - *offset);
  211. if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
  212. dev_dbg(dev->dev, "failed to copy data to userland\n");
  213. rets = -EFAULT;
  214. goto free;
  215. }
  216. rets = length;
  217. *offset += length;
  218. /* not all data was read, keep the cb */
  219. if (*offset < cb->buf_idx)
  220. goto out;
  221. free:
  222. mei_cl_del_rd_completed(cl, cb);
  223. *offset = 0;
  224. out:
  225. cl_dbg(dev, cl, "end mei read rets = %zd\n", rets);
  226. mutex_unlock(&dev->device_lock);
  227. return rets;
  228. }
  229. /**
  230. * mei_cl_vtag_by_fp - obtain the vtag by file pointer
  231. *
  232. * @cl: host client
  233. * @fp: pointer to file structure
  234. *
  235. * Return: vtag value on success, otherwise 0
  236. */
  237. static u8 mei_cl_vtag_by_fp(const struct mei_cl *cl, const struct file *fp)
  238. {
  239. struct mei_cl_vtag *cl_vtag;
  240. if (!fp)
  241. return 0;
  242. list_for_each_entry(cl_vtag, &cl->vtag_map, list)
  243. if (cl_vtag->fp == fp)
  244. return cl_vtag->vtag;
  245. return 0;
  246. }
  247. /**
  248. * mei_write - the write function.
  249. *
  250. * @file: pointer to file structure
  251. * @ubuf: pointer to user buffer
  252. * @length: buffer length
  253. * @offset: data offset in buffer
  254. *
  255. * Return: >=0 data length on success , <0 on error
  256. */
  257. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  258. size_t length, loff_t *offset)
  259. {
  260. struct mei_cl *cl = file->private_data;
  261. struct mei_cl_cb *cb;
  262. struct mei_device *dev;
  263. ssize_t rets;
  264. if (WARN_ON(!cl || !cl->dev))
  265. return -ENODEV;
  266. dev = cl->dev;
  267. mutex_lock(&dev->device_lock);
  268. if (dev->dev_state != MEI_DEV_ENABLED) {
  269. rets = -ENODEV;
  270. goto out;
  271. }
  272. if (!mei_cl_is_connected(cl)) {
  273. cl_dbg(dev, cl, "is not connected");
  274. rets = -ENODEV;
  275. goto out;
  276. }
  277. if (!mei_me_cl_is_active(cl->me_cl)) {
  278. rets = -ENOTTY;
  279. goto out;
  280. }
  281. if (length > mei_cl_mtu(cl)) {
  282. rets = -EFBIG;
  283. goto out;
  284. }
  285. if (length == 0) {
  286. rets = 0;
  287. goto out;
  288. }
  289. while (cl->tx_cb_queued >= dev->tx_queue_limit) {
  290. if (file->f_flags & O_NONBLOCK) {
  291. rets = -EAGAIN;
  292. goto out;
  293. }
  294. mutex_unlock(&dev->device_lock);
  295. rets = wait_event_interruptible(cl->tx_wait,
  296. cl->writing_state == MEI_WRITE_COMPLETE ||
  297. (!mei_cl_is_connected(cl)));
  298. mutex_lock(&dev->device_lock);
  299. if (rets) {
  300. if (signal_pending(current))
  301. rets = -EINTR;
  302. goto out;
  303. }
  304. if (!mei_cl_is_connected(cl)) {
  305. rets = -ENODEV;
  306. goto out;
  307. }
  308. }
  309. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
  310. if (!cb) {
  311. rets = -ENOMEM;
  312. goto out;
  313. }
  314. cb->vtag = mei_cl_vtag_by_fp(cl, file);
  315. rets = copy_from_user(cb->buf.data, ubuf, length);
  316. if (rets) {
  317. dev_dbg(dev->dev, "failed to copy data from userland\n");
  318. rets = -EFAULT;
  319. mei_io_cb_free(cb);
  320. goto out;
  321. }
  322. rets = mei_cl_write(cl, cb, MAX_SCHEDULE_TIMEOUT);
  323. out:
  324. mutex_unlock(&dev->device_lock);
  325. return rets;
  326. }
  327. /**
  328. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  329. *
  330. * @file: private data of the file object
  331. * @in_client_uuid: requested UUID for connection
  332. * @client: IOCTL connect data, output parameters
  333. *
  334. * Locking: called under "dev->device_lock" lock
  335. *
  336. * Return: 0 on success, <0 on failure.
  337. */
  338. static int mei_ioctl_connect_client(struct file *file,
  339. const uuid_le *in_client_uuid,
  340. struct mei_client *client)
  341. {
  342. struct mei_device *dev;
  343. struct mei_me_client *me_cl;
  344. struct mei_cl *cl;
  345. int rets;
  346. cl = file->private_data;
  347. dev = cl->dev;
  348. if (cl->state != MEI_FILE_INITIALIZING &&
  349. cl->state != MEI_FILE_DISCONNECTED)
  350. return -EBUSY;
  351. /* find ME client we're trying to connect to */
  352. me_cl = mei_me_cl_by_uuid(dev, in_client_uuid);
  353. if (!me_cl) {
  354. dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  355. in_client_uuid);
  356. rets = -ENOTTY;
  357. goto end;
  358. }
  359. if (me_cl->props.fixed_address) {
  360. bool forbidden = dev->override_fixed_address ?
  361. !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
  362. if (forbidden) {
  363. dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
  364. in_client_uuid);
  365. rets = -ENOTTY;
  366. goto end;
  367. }
  368. }
  369. dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
  370. me_cl->client_id);
  371. dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
  372. me_cl->props.protocol_version);
  373. dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
  374. me_cl->props.max_msg_length);
  375. /* prepare the output buffer */
  376. client->max_msg_length = me_cl->props.max_msg_length;
  377. client->protocol_version = me_cl->props.protocol_version;
  378. dev_dbg(dev->dev, "Can connect?\n");
  379. rets = mei_cl_connect(cl, me_cl, file);
  380. end:
  381. mei_me_cl_put(me_cl);
  382. return rets;
  383. }
  384. /**
  385. * mei_vt_support_check - check if client support vtags
  386. *
  387. * @dev: mei_device
  388. * @uuid: client UUID
  389. *
  390. * Locking: called under "dev->device_lock" lock
  391. *
  392. * Return:
  393. * 0 - supported
  394. * -ENOTTY - no such client
  395. * -EOPNOTSUPP - vtags are not supported by client
  396. */
  397. static int mei_vt_support_check(struct mei_device *dev, const uuid_le *uuid)
  398. {
  399. struct mei_me_client *me_cl;
  400. int ret;
  401. if (!dev->hbm_f_vt_supported)
  402. return -EOPNOTSUPP;
  403. me_cl = mei_me_cl_by_uuid(dev, uuid);
  404. if (!me_cl) {
  405. dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  406. uuid);
  407. return -ENOTTY;
  408. }
  409. ret = me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
  410. mei_me_cl_put(me_cl);
  411. return ret;
  412. }
  413. /**
  414. * mei_ioctl_connect_vtag - connect to fw client with vtag IOCTL function
  415. *
  416. * @file: private data of the file object
  417. * @in_client_uuid: requested UUID for connection
  418. * @client: IOCTL connect data, output parameters
  419. * @vtag: vm tag
  420. *
  421. * Locking: called under "dev->device_lock" lock
  422. *
  423. * Return: 0 on success, <0 on failure.
  424. */
  425. static int mei_ioctl_connect_vtag(struct file *file,
  426. const uuid_le *in_client_uuid,
  427. struct mei_client *client,
  428. u8 vtag)
  429. {
  430. struct mei_device *dev;
  431. struct mei_cl *cl;
  432. struct mei_cl *pos;
  433. struct mei_cl_vtag *cl_vtag;
  434. cl = file->private_data;
  435. dev = cl->dev;
  436. dev_dbg(dev->dev, "FW Client %pUl vtag %d\n", in_client_uuid, vtag);
  437. switch (cl->state) {
  438. case MEI_FILE_DISCONNECTED:
  439. if (mei_cl_vtag_by_fp(cl, file) != vtag) {
  440. dev_err(dev->dev, "reconnect with different vtag\n");
  441. return -EINVAL;
  442. }
  443. break;
  444. case MEI_FILE_INITIALIZING:
  445. /* malicious connect from another thread may push vtag */
  446. if (!IS_ERR(mei_cl_fp_by_vtag(cl, vtag))) {
  447. dev_err(dev->dev, "vtag already filled\n");
  448. return -EINVAL;
  449. }
  450. list_for_each_entry(pos, &dev->file_list, link) {
  451. if (pos == cl)
  452. continue;
  453. if (!pos->me_cl)
  454. continue;
  455. /* only search for same UUID */
  456. if (uuid_le_cmp(*mei_cl_uuid(pos), *in_client_uuid))
  457. continue;
  458. /* if tag already exist try another fp */
  459. if (!IS_ERR(mei_cl_fp_by_vtag(pos, vtag)))
  460. continue;
  461. /* replace cl with acquired one */
  462. dev_dbg(dev->dev, "replacing with existing cl\n");
  463. mei_cl_unlink(cl);
  464. kfree(cl);
  465. file->private_data = pos;
  466. cl = pos;
  467. break;
  468. }
  469. cl_vtag = mei_cl_vtag_alloc(file, vtag);
  470. if (IS_ERR(cl_vtag))
  471. return -ENOMEM;
  472. list_add_tail(&cl_vtag->list, &cl->vtag_map);
  473. break;
  474. default:
  475. return -EBUSY;
  476. }
  477. while (cl->state != MEI_FILE_INITIALIZING &&
  478. cl->state != MEI_FILE_DISCONNECTED &&
  479. cl->state != MEI_FILE_CONNECTED) {
  480. mutex_unlock(&dev->device_lock);
  481. wait_event_timeout(cl->wait,
  482. (cl->state == MEI_FILE_CONNECTED ||
  483. cl->state == MEI_FILE_DISCONNECTED ||
  484. cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
  485. cl->state == MEI_FILE_DISCONNECT_REPLY),
  486. dev->timeouts.cl_connect);
  487. mutex_lock(&dev->device_lock);
  488. }
  489. if (!mei_cl_is_connected(cl))
  490. return mei_ioctl_connect_client(file, in_client_uuid, client);
  491. client->max_msg_length = cl->me_cl->props.max_msg_length;
  492. client->protocol_version = cl->me_cl->props.protocol_version;
  493. return 0;
  494. }
  495. /**
  496. * mei_ioctl_client_notify_request - propagate event notification
  497. * request to client
  498. *
  499. * @file: pointer to file structure
  500. * @request: 0 - disable, 1 - enable
  501. *
  502. * Return: 0 on success , <0 on error
  503. */
  504. static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
  505. {
  506. struct mei_cl *cl = file->private_data;
  507. if (request != MEI_HBM_NOTIFICATION_START &&
  508. request != MEI_HBM_NOTIFICATION_STOP)
  509. return -EINVAL;
  510. return mei_cl_notify_request(cl, file, (u8)request);
  511. }
  512. /**
  513. * mei_ioctl_client_notify_get - wait for notification request
  514. *
  515. * @file: pointer to file structure
  516. * @notify_get: 0 - disable, 1 - enable
  517. *
  518. * Return: 0 on success , <0 on error
  519. */
  520. static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
  521. {
  522. struct mei_cl *cl = file->private_data;
  523. bool notify_ev;
  524. bool block = (file->f_flags & O_NONBLOCK) == 0;
  525. int rets;
  526. rets = mei_cl_notify_get(cl, block, &notify_ev);
  527. if (rets)
  528. return rets;
  529. *notify_get = notify_ev ? 1 : 0;
  530. return 0;
  531. }
  532. /**
  533. * mei_ioctl - the IOCTL function
  534. *
  535. * @file: pointer to file structure
  536. * @cmd: ioctl command
  537. * @data: pointer to mei message structure
  538. *
  539. * Return: 0 on success , <0 on error
  540. */
  541. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  542. {
  543. struct mei_device *dev;
  544. struct mei_cl *cl = file->private_data;
  545. struct mei_connect_client_data conn;
  546. struct mei_connect_client_data_vtag conn_vtag;
  547. const uuid_le *cl_uuid;
  548. struct mei_client *props;
  549. u8 vtag;
  550. u32 notify_get, notify_req;
  551. int rets;
  552. if (WARN_ON(!cl || !cl->dev))
  553. return -ENODEV;
  554. dev = cl->dev;
  555. dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
  556. mutex_lock(&dev->device_lock);
  557. if (dev->dev_state != MEI_DEV_ENABLED) {
  558. rets = -ENODEV;
  559. goto out;
  560. }
  561. switch (cmd) {
  562. case IOCTL_MEI_CONNECT_CLIENT:
  563. dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  564. if (copy_from_user(&conn, (char __user *)data, sizeof(conn))) {
  565. dev_dbg(dev->dev, "failed to copy data from userland\n");
  566. rets = -EFAULT;
  567. goto out;
  568. }
  569. cl_uuid = &conn.in_client_uuid;
  570. props = &conn.out_client_properties;
  571. vtag = 0;
  572. rets = mei_vt_support_check(dev, cl_uuid);
  573. if (rets == -ENOTTY)
  574. goto out;
  575. if (!rets)
  576. rets = mei_ioctl_connect_vtag(file, cl_uuid, props,
  577. vtag);
  578. else
  579. rets = mei_ioctl_connect_client(file, cl_uuid, props);
  580. if (rets)
  581. goto out;
  582. /* if all is ok, copying the data back to user. */
  583. if (copy_to_user((char __user *)data, &conn, sizeof(conn))) {
  584. dev_dbg(dev->dev, "failed to copy data to userland\n");
  585. rets = -EFAULT;
  586. goto out;
  587. }
  588. break;
  589. case IOCTL_MEI_CONNECT_CLIENT_VTAG:
  590. dev_dbg(dev->dev, "IOCTL_MEI_CONNECT_CLIENT_VTAG\n");
  591. if (copy_from_user(&conn_vtag, (char __user *)data,
  592. sizeof(conn_vtag))) {
  593. dev_dbg(dev->dev, "failed to copy data from userland\n");
  594. rets = -EFAULT;
  595. goto out;
  596. }
  597. cl_uuid = &conn_vtag.connect.in_client_uuid;
  598. props = &conn_vtag.out_client_properties;
  599. vtag = conn_vtag.connect.vtag;
  600. rets = mei_vt_support_check(dev, cl_uuid);
  601. if (rets == -EOPNOTSUPP)
  602. dev_dbg(dev->dev, "FW Client %pUl does not support vtags\n",
  603. cl_uuid);
  604. if (rets)
  605. goto out;
  606. if (!vtag) {
  607. dev_dbg(dev->dev, "vtag can't be zero\n");
  608. rets = -EINVAL;
  609. goto out;
  610. }
  611. rets = mei_ioctl_connect_vtag(file, cl_uuid, props, vtag);
  612. if (rets)
  613. goto out;
  614. /* if all is ok, copying the data back to user. */
  615. if (copy_to_user((char __user *)data, &conn_vtag,
  616. sizeof(conn_vtag))) {
  617. dev_dbg(dev->dev, "failed to copy data to userland\n");
  618. rets = -EFAULT;
  619. goto out;
  620. }
  621. break;
  622. case IOCTL_MEI_NOTIFY_SET:
  623. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
  624. if (copy_from_user(&notify_req,
  625. (char __user *)data, sizeof(notify_req))) {
  626. dev_dbg(dev->dev, "failed to copy data from userland\n");
  627. rets = -EFAULT;
  628. goto out;
  629. }
  630. rets = mei_ioctl_client_notify_request(file, notify_req);
  631. break;
  632. case IOCTL_MEI_NOTIFY_GET:
  633. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
  634. rets = mei_ioctl_client_notify_get(file, &notify_get);
  635. if (rets)
  636. goto out;
  637. dev_dbg(dev->dev, "copy connect data to user\n");
  638. if (copy_to_user((char __user *)data,
  639. &notify_get, sizeof(notify_get))) {
  640. dev_dbg(dev->dev, "failed to copy data to userland\n");
  641. rets = -EFAULT;
  642. goto out;
  643. }
  644. break;
  645. default:
  646. rets = -ENOIOCTLCMD;
  647. }
  648. out:
  649. mutex_unlock(&dev->device_lock);
  650. return rets;
  651. }
  652. /**
  653. * mei_poll - the poll function
  654. *
  655. * @file: pointer to file structure
  656. * @wait: pointer to poll_table structure
  657. *
  658. * Return: poll mask
  659. */
  660. static __poll_t mei_poll(struct file *file, poll_table *wait)
  661. {
  662. __poll_t req_events = poll_requested_events(wait);
  663. struct mei_cl *cl = file->private_data;
  664. struct mei_device *dev;
  665. __poll_t mask = 0;
  666. bool notify_en;
  667. if (WARN_ON(!cl || !cl->dev))
  668. return EPOLLERR;
  669. dev = cl->dev;
  670. mutex_lock(&dev->device_lock);
  671. notify_en = cl->notify_en && (req_events & EPOLLPRI);
  672. if (dev->dev_state != MEI_DEV_ENABLED ||
  673. !mei_cl_is_connected(cl)) {
  674. mask = EPOLLERR;
  675. goto out;
  676. }
  677. if (notify_en) {
  678. poll_wait(file, &cl->ev_wait, wait);
  679. if (cl->notify_ev)
  680. mask |= EPOLLPRI;
  681. }
  682. if (req_events & (EPOLLIN | EPOLLRDNORM)) {
  683. poll_wait(file, &cl->rx_wait, wait);
  684. if (mei_cl_read_cb(cl, file))
  685. mask |= EPOLLIN | EPOLLRDNORM;
  686. else
  687. mei_cl_read_start(cl, mei_cl_mtu(cl), file);
  688. }
  689. if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
  690. poll_wait(file, &cl->tx_wait, wait);
  691. if (cl->tx_cb_queued < dev->tx_queue_limit)
  692. mask |= EPOLLOUT | EPOLLWRNORM;
  693. }
  694. out:
  695. mutex_unlock(&dev->device_lock);
  696. return mask;
  697. }
  698. /**
  699. * mei_cl_is_write_queued - check if the client has pending writes.
  700. *
  701. * @cl: writing host client
  702. *
  703. * Return: true if client is writing, false otherwise.
  704. */
  705. static bool mei_cl_is_write_queued(struct mei_cl *cl)
  706. {
  707. struct mei_device *dev = cl->dev;
  708. struct mei_cl_cb *cb;
  709. list_for_each_entry(cb, &dev->write_list, list)
  710. if (cb->cl == cl)
  711. return true;
  712. list_for_each_entry(cb, &dev->write_waiting_list, list)
  713. if (cb->cl == cl)
  714. return true;
  715. return false;
  716. }
  717. /**
  718. * mei_fsync - the fsync handler
  719. *
  720. * @fp: pointer to file structure
  721. * @start: unused
  722. * @end: unused
  723. * @datasync: unused
  724. *
  725. * Return: 0 on success, -ENODEV if client is not connected
  726. */
  727. static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
  728. {
  729. struct mei_cl *cl = fp->private_data;
  730. struct mei_device *dev;
  731. int rets;
  732. if (WARN_ON(!cl || !cl->dev))
  733. return -ENODEV;
  734. dev = cl->dev;
  735. mutex_lock(&dev->device_lock);
  736. if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
  737. rets = -ENODEV;
  738. goto out;
  739. }
  740. while (mei_cl_is_write_queued(cl)) {
  741. mutex_unlock(&dev->device_lock);
  742. rets = wait_event_interruptible(cl->tx_wait,
  743. cl->writing_state == MEI_WRITE_COMPLETE ||
  744. !mei_cl_is_connected(cl));
  745. mutex_lock(&dev->device_lock);
  746. if (rets) {
  747. if (signal_pending(current))
  748. rets = -EINTR;
  749. goto out;
  750. }
  751. if (!mei_cl_is_connected(cl)) {
  752. rets = -ENODEV;
  753. goto out;
  754. }
  755. }
  756. rets = 0;
  757. out:
  758. mutex_unlock(&dev->device_lock);
  759. return rets;
  760. }
  761. /**
  762. * mei_fasync - asynchronous io support
  763. *
  764. * @fd: file descriptor
  765. * @file: pointer to file structure
  766. * @band: band bitmap
  767. *
  768. * Return: negative on error,
  769. * 0 if it did no changes,
  770. * and positive a process was added or deleted
  771. */
  772. static int mei_fasync(int fd, struct file *file, int band)
  773. {
  774. struct mei_cl *cl = file->private_data;
  775. if (!mei_cl_is_connected(cl))
  776. return -ENODEV;
  777. return fasync_helper(fd, file, band, &cl->ev_async);
  778. }
  779. /**
  780. * trc_show - mei device trc attribute show method
  781. *
  782. * @device: device pointer
  783. * @attr: attribute pointer
  784. * @buf: char out buffer
  785. *
  786. * Return: number of the bytes printed into buf or error
  787. */
  788. static ssize_t trc_show(struct device *device,
  789. struct device_attribute *attr, char *buf)
  790. {
  791. struct mei_device *dev = dev_get_drvdata(device);
  792. u32 trc;
  793. int ret;
  794. ret = mei_trc_status(dev, &trc);
  795. if (ret)
  796. return ret;
  797. return sprintf(buf, "%08X\n", trc);
  798. }
  799. static DEVICE_ATTR_RO(trc);
  800. /**
  801. * fw_status_show - mei device fw_status attribute show method
  802. *
  803. * @device: device pointer
  804. * @attr: attribute pointer
  805. * @buf: char out buffer
  806. *
  807. * Return: number of the bytes printed into buf or error
  808. */
  809. static ssize_t fw_status_show(struct device *device,
  810. struct device_attribute *attr, char *buf)
  811. {
  812. struct mei_device *dev = dev_get_drvdata(device);
  813. struct mei_fw_status fw_status;
  814. int err, i;
  815. ssize_t cnt = 0;
  816. mutex_lock(&dev->device_lock);
  817. err = mei_fw_status(dev, &fw_status);
  818. mutex_unlock(&dev->device_lock);
  819. if (err) {
  820. dev_err(device, "read fw_status error = %d\n", err);
  821. return err;
  822. }
  823. for (i = 0; i < fw_status.count; i++)
  824. cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
  825. fw_status.status[i]);
  826. return cnt;
  827. }
  828. static DEVICE_ATTR_RO(fw_status);
  829. /**
  830. * hbm_ver_show - display HBM protocol version negotiated with FW
  831. *
  832. * @device: device pointer
  833. * @attr: attribute pointer
  834. * @buf: char out buffer
  835. *
  836. * Return: number of the bytes printed into buf or error
  837. */
  838. static ssize_t hbm_ver_show(struct device *device,
  839. struct device_attribute *attr, char *buf)
  840. {
  841. struct mei_device *dev = dev_get_drvdata(device);
  842. struct hbm_version ver;
  843. mutex_lock(&dev->device_lock);
  844. ver = dev->version;
  845. mutex_unlock(&dev->device_lock);
  846. return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
  847. }
  848. static DEVICE_ATTR_RO(hbm_ver);
  849. /**
  850. * hbm_ver_drv_show - display HBM protocol version advertised by driver
  851. *
  852. * @device: device pointer
  853. * @attr: attribute pointer
  854. * @buf: char out buffer
  855. *
  856. * Return: number of the bytes printed into buf or error
  857. */
  858. static ssize_t hbm_ver_drv_show(struct device *device,
  859. struct device_attribute *attr, char *buf)
  860. {
  861. return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
  862. }
  863. static DEVICE_ATTR_RO(hbm_ver_drv);
  864. static ssize_t tx_queue_limit_show(struct device *device,
  865. struct device_attribute *attr, char *buf)
  866. {
  867. struct mei_device *dev = dev_get_drvdata(device);
  868. u8 size = 0;
  869. mutex_lock(&dev->device_lock);
  870. size = dev->tx_queue_limit;
  871. mutex_unlock(&dev->device_lock);
  872. return sysfs_emit(buf, "%u\n", size);
  873. }
  874. static ssize_t tx_queue_limit_store(struct device *device,
  875. struct device_attribute *attr,
  876. const char *buf, size_t count)
  877. {
  878. struct mei_device *dev = dev_get_drvdata(device);
  879. u8 limit;
  880. unsigned int inp;
  881. int err;
  882. err = kstrtouint(buf, 10, &inp);
  883. if (err)
  884. return err;
  885. if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
  886. return -EINVAL;
  887. limit = inp;
  888. mutex_lock(&dev->device_lock);
  889. dev->tx_queue_limit = limit;
  890. mutex_unlock(&dev->device_lock);
  891. return count;
  892. }
  893. static DEVICE_ATTR_RW(tx_queue_limit);
  894. /**
  895. * fw_ver_show - display ME FW version
  896. *
  897. * @device: device pointer
  898. * @attr: attribute pointer
  899. * @buf: char out buffer
  900. *
  901. * Return: number of the bytes printed into buf or error
  902. */
  903. static ssize_t fw_ver_show(struct device *device,
  904. struct device_attribute *attr, char *buf)
  905. {
  906. struct mei_device *dev = dev_get_drvdata(device);
  907. struct mei_fw_version *ver;
  908. ssize_t cnt = 0;
  909. int i;
  910. ver = dev->fw_ver;
  911. for (i = 0; i < MEI_MAX_FW_VER_BLOCKS; i++)
  912. cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%u:%u.%u.%u.%u\n",
  913. ver[i].platform, ver[i].major, ver[i].minor,
  914. ver[i].hotfix, ver[i].buildno);
  915. return cnt;
  916. }
  917. static DEVICE_ATTR_RO(fw_ver);
  918. /**
  919. * dev_state_show - display device state
  920. *
  921. * @device: device pointer
  922. * @attr: attribute pointer
  923. * @buf: char out buffer
  924. *
  925. * Return: number of the bytes printed into buf or error
  926. */
  927. static ssize_t dev_state_show(struct device *device,
  928. struct device_attribute *attr, char *buf)
  929. {
  930. struct mei_device *dev = dev_get_drvdata(device);
  931. enum mei_dev_state dev_state;
  932. mutex_lock(&dev->device_lock);
  933. dev_state = dev->dev_state;
  934. mutex_unlock(&dev->device_lock);
  935. return sprintf(buf, "%s", mei_dev_state_str(dev_state));
  936. }
  937. static DEVICE_ATTR_RO(dev_state);
  938. /**
  939. * mei_set_devstate: set to new device state and notify sysfs file.
  940. *
  941. * @dev: mei_device
  942. * @state: new device state
  943. */
  944. void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
  945. {
  946. struct device *clsdev;
  947. if (dev->dev_state == state)
  948. return;
  949. dev->dev_state = state;
  950. clsdev = class_find_device_by_devt(&mei_class, dev->cdev.dev);
  951. if (clsdev) {
  952. sysfs_notify(&clsdev->kobj, NULL, "dev_state");
  953. put_device(clsdev);
  954. }
  955. }
  956. /**
  957. * kind_show - display device kind
  958. *
  959. * @device: device pointer
  960. * @attr: attribute pointer
  961. * @buf: char out buffer
  962. *
  963. * Return: number of the bytes printed into buf or error
  964. */
  965. static ssize_t kind_show(struct device *device,
  966. struct device_attribute *attr, char *buf)
  967. {
  968. struct mei_device *dev = dev_get_drvdata(device);
  969. ssize_t ret;
  970. if (dev->kind)
  971. ret = sprintf(buf, "%s\n", dev->kind);
  972. else
  973. ret = sprintf(buf, "%s\n", "mei");
  974. return ret;
  975. }
  976. static DEVICE_ATTR_RO(kind);
  977. static struct attribute *mei_attrs[] = {
  978. &dev_attr_fw_status.attr,
  979. &dev_attr_hbm_ver.attr,
  980. &dev_attr_hbm_ver_drv.attr,
  981. &dev_attr_tx_queue_limit.attr,
  982. &dev_attr_fw_ver.attr,
  983. &dev_attr_dev_state.attr,
  984. &dev_attr_trc.attr,
  985. &dev_attr_kind.attr,
  986. NULL
  987. };
  988. ATTRIBUTE_GROUPS(mei);
  989. /*
  990. * file operations structure will be used for mei char device.
  991. */
  992. static const struct file_operations mei_fops = {
  993. .owner = THIS_MODULE,
  994. .read = mei_read,
  995. .unlocked_ioctl = mei_ioctl,
  996. .compat_ioctl = compat_ptr_ioctl,
  997. .open = mei_open,
  998. .release = mei_release,
  999. .write = mei_write,
  1000. .poll = mei_poll,
  1001. .fsync = mei_fsync,
  1002. .fasync = mei_fasync,
  1003. };
  1004. /**
  1005. * mei_minor_get - obtain next free device minor number
  1006. *
  1007. * @dev: device pointer
  1008. *
  1009. * Return: allocated minor, or -ENOSPC if no free minor left
  1010. */
  1011. static int mei_minor_get(struct mei_device *dev)
  1012. {
  1013. int ret;
  1014. mutex_lock(&mei_minor_lock);
  1015. ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
  1016. if (ret >= 0)
  1017. dev->minor = ret;
  1018. else if (ret == -ENOSPC)
  1019. dev_err(dev->dev, "too many mei devices\n");
  1020. mutex_unlock(&mei_minor_lock);
  1021. return ret;
  1022. }
  1023. /**
  1024. * mei_minor_free - mark device minor number as free
  1025. *
  1026. * @dev: device pointer
  1027. */
  1028. static void mei_minor_free(struct mei_device *dev)
  1029. {
  1030. mutex_lock(&mei_minor_lock);
  1031. idr_remove(&mei_idr, dev->minor);
  1032. mutex_unlock(&mei_minor_lock);
  1033. }
  1034. int mei_register(struct mei_device *dev, struct device *parent)
  1035. {
  1036. struct device *clsdev; /* class device */
  1037. int ret, devno;
  1038. ret = mei_minor_get(dev);
  1039. if (ret < 0)
  1040. return ret;
  1041. /* Fill in the data structures */
  1042. devno = MKDEV(MAJOR(mei_devt), dev->minor);
  1043. cdev_init(&dev->cdev, &mei_fops);
  1044. dev->cdev.owner = parent->driver->owner;
  1045. /* Add the device */
  1046. ret = cdev_add(&dev->cdev, devno, 1);
  1047. if (ret) {
  1048. dev_err(parent, "unable to add device %d:%d\n",
  1049. MAJOR(mei_devt), dev->minor);
  1050. goto err_dev_add;
  1051. }
  1052. clsdev = device_create_with_groups(&mei_class, parent, devno,
  1053. dev, mei_groups,
  1054. "mei%d", dev->minor);
  1055. if (IS_ERR(clsdev)) {
  1056. dev_err(parent, "unable to create device %d:%d\n",
  1057. MAJOR(mei_devt), dev->minor);
  1058. ret = PTR_ERR(clsdev);
  1059. goto err_dev_create;
  1060. }
  1061. mei_dbgfs_register(dev, dev_name(clsdev));
  1062. return 0;
  1063. err_dev_create:
  1064. cdev_del(&dev->cdev);
  1065. err_dev_add:
  1066. mei_minor_free(dev);
  1067. return ret;
  1068. }
  1069. EXPORT_SYMBOL_GPL(mei_register);
  1070. void mei_deregister(struct mei_device *dev)
  1071. {
  1072. int devno;
  1073. devno = dev->cdev.dev;
  1074. cdev_del(&dev->cdev);
  1075. mei_dbgfs_deregister(dev);
  1076. device_destroy(&mei_class, devno);
  1077. mei_minor_free(dev);
  1078. }
  1079. EXPORT_SYMBOL_GPL(mei_deregister);
  1080. static int __init mei_init(void)
  1081. {
  1082. int ret;
  1083. ret = class_register(&mei_class);
  1084. if (ret)
  1085. return ret;
  1086. ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
  1087. if (ret < 0) {
  1088. pr_err("unable to allocate char dev region\n");
  1089. goto err_class;
  1090. }
  1091. ret = mei_cl_bus_init();
  1092. if (ret < 0) {
  1093. pr_err("unable to initialize bus\n");
  1094. goto err_chrdev;
  1095. }
  1096. return 0;
  1097. err_chrdev:
  1098. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  1099. err_class:
  1100. class_unregister(&mei_class);
  1101. return ret;
  1102. }
  1103. static void __exit mei_exit(void)
  1104. {
  1105. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  1106. class_unregister(&mei_class);
  1107. mei_cl_bus_exit();
  1108. }
  1109. module_init(mei_init);
  1110. module_exit(mei_exit);
  1111. MODULE_AUTHOR("Intel Corporation");
  1112. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  1113. MODULE_LICENSE("GPL v2");