main.c 28 KB

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