main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/kernel.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/poll.h>
  26. #include <linux/init.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/cdev.h>
  29. #include <linux/sched/signal.h>
  30. #include <linux/uuid.h>
  31. #include <linux/compat.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mei.h>
  35. #include "mei_dev.h"
  36. #include "client.h"
  37. /**
  38. * mei_open - the open function
  39. *
  40. * @inode: pointer to inode structure
  41. * @file: pointer to file structure
  42. *
  43. * Return: 0 on success, <0 on error
  44. */
  45. static int mei_open(struct inode *inode, struct file *file)
  46. {
  47. struct mei_device *dev;
  48. struct mei_cl *cl;
  49. int err;
  50. dev = container_of(inode->i_cdev, struct mei_device, cdev);
  51. if (!dev)
  52. return -ENODEV;
  53. mutex_lock(&dev->device_lock);
  54. if (dev->dev_state != MEI_DEV_ENABLED) {
  55. dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  56. mei_dev_state_str(dev->dev_state));
  57. err = -ENODEV;
  58. goto err_unlock;
  59. }
  60. cl = mei_cl_alloc_linked(dev);
  61. if (IS_ERR(cl)) {
  62. err = PTR_ERR(cl);
  63. goto err_unlock;
  64. }
  65. cl->fp = file;
  66. file->private_data = cl;
  67. mutex_unlock(&dev->device_lock);
  68. return nonseekable_open(inode, file);
  69. err_unlock:
  70. mutex_unlock(&dev->device_lock);
  71. return err;
  72. }
  73. /**
  74. * mei_release - the release function
  75. *
  76. * @inode: pointer to inode structure
  77. * @file: pointer to file structure
  78. *
  79. * Return: 0 on success, <0 on error
  80. */
  81. static int mei_release(struct inode *inode, struct file *file)
  82. {
  83. struct mei_cl *cl = file->private_data;
  84. struct mei_device *dev;
  85. int rets;
  86. if (WARN_ON(!cl || !cl->dev))
  87. return -ENODEV;
  88. dev = cl->dev;
  89. mutex_lock(&dev->device_lock);
  90. rets = mei_cl_disconnect(cl);
  91. mei_cl_flush_queues(cl, file);
  92. cl_dbg(dev, cl, "removing\n");
  93. mei_cl_unlink(cl);
  94. file->private_data = NULL;
  95. kfree(cl);
  96. mutex_unlock(&dev->device_lock);
  97. return rets;
  98. }
  99. /**
  100. * mei_read - the read function.
  101. *
  102. * @file: pointer to file structure
  103. * @ubuf: pointer to user buffer
  104. * @length: buffer length
  105. * @offset: data offset in buffer
  106. *
  107. * Return: >=0 data length on success , <0 on error
  108. */
  109. static ssize_t mei_read(struct file *file, char __user *ubuf,
  110. size_t length, loff_t *offset)
  111. {
  112. struct mei_cl *cl = file->private_data;
  113. struct mei_device *dev;
  114. struct mei_cl_cb *cb = NULL;
  115. bool nonblock = !!(file->f_flags & O_NONBLOCK);
  116. int rets;
  117. if (WARN_ON(!cl || !cl->dev))
  118. return -ENODEV;
  119. dev = cl->dev;
  120. mutex_lock(&dev->device_lock);
  121. if (dev->dev_state != MEI_DEV_ENABLED) {
  122. rets = -ENODEV;
  123. goto out;
  124. }
  125. if (length == 0) {
  126. rets = 0;
  127. goto out;
  128. }
  129. if (ubuf == NULL) {
  130. rets = -EMSGSIZE;
  131. goto out;
  132. }
  133. cb = mei_cl_read_cb(cl, file);
  134. if (cb)
  135. goto copy_buffer;
  136. if (*offset > 0)
  137. *offset = 0;
  138. rets = mei_cl_read_start(cl, length, file);
  139. if (rets && rets != -EBUSY) {
  140. cl_dbg(dev, cl, "mei start read failure status = %d\n", rets);
  141. goto out;
  142. }
  143. if (nonblock) {
  144. rets = -EAGAIN;
  145. goto out;
  146. }
  147. mutex_unlock(&dev->device_lock);
  148. if (wait_event_interruptible(cl->rx_wait,
  149. !list_empty(&cl->rd_completed) ||
  150. !mei_cl_is_connected(cl))) {
  151. if (signal_pending(current))
  152. return -EINTR;
  153. return -ERESTARTSYS;
  154. }
  155. mutex_lock(&dev->device_lock);
  156. if (!mei_cl_is_connected(cl)) {
  157. rets = -ENODEV;
  158. goto out;
  159. }
  160. cb = mei_cl_read_cb(cl, file);
  161. if (!cb) {
  162. rets = 0;
  163. goto out;
  164. }
  165. copy_buffer:
  166. /* now copy the data to user space */
  167. if (cb->status) {
  168. rets = cb->status;
  169. cl_dbg(dev, cl, "read operation failed %d\n", rets);
  170. goto free;
  171. }
  172. cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n",
  173. cb->buf.size, cb->buf_idx, *offset);
  174. if (*offset >= cb->buf_idx) {
  175. rets = 0;
  176. goto free;
  177. }
  178. /* length is being truncated to PAGE_SIZE,
  179. * however buf_idx may point beyond that */
  180. length = min_t(size_t, length, cb->buf_idx - *offset);
  181. if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
  182. dev_dbg(dev->dev, "failed to copy data to userland\n");
  183. rets = -EFAULT;
  184. goto free;
  185. }
  186. rets = length;
  187. *offset += length;
  188. /* not all data was read, keep the cb */
  189. if (*offset < cb->buf_idx)
  190. goto out;
  191. free:
  192. mei_io_cb_free(cb);
  193. *offset = 0;
  194. out:
  195. cl_dbg(dev, cl, "end mei read rets = %d\n", rets);
  196. mutex_unlock(&dev->device_lock);
  197. return rets;
  198. }
  199. /**
  200. * mei_write - the write function.
  201. *
  202. * @file: pointer to file structure
  203. * @ubuf: pointer to user buffer
  204. * @length: buffer length
  205. * @offset: data offset in buffer
  206. *
  207. * Return: >=0 data length on success , <0 on error
  208. */
  209. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  210. size_t length, loff_t *offset)
  211. {
  212. struct mei_cl *cl = file->private_data;
  213. struct mei_cl_cb *cb;
  214. struct mei_device *dev;
  215. int rets;
  216. if (WARN_ON(!cl || !cl->dev))
  217. return -ENODEV;
  218. dev = cl->dev;
  219. mutex_lock(&dev->device_lock);
  220. if (dev->dev_state != MEI_DEV_ENABLED) {
  221. rets = -ENODEV;
  222. goto out;
  223. }
  224. if (!mei_cl_is_connected(cl)) {
  225. cl_err(dev, cl, "is not connected");
  226. rets = -ENODEV;
  227. goto out;
  228. }
  229. if (!mei_me_cl_is_active(cl->me_cl)) {
  230. rets = -ENOTTY;
  231. goto out;
  232. }
  233. if (length > mei_cl_mtu(cl)) {
  234. rets = -EFBIG;
  235. goto out;
  236. }
  237. if (length == 0) {
  238. rets = 0;
  239. goto out;
  240. }
  241. while (cl->tx_cb_queued >= dev->tx_queue_limit) {
  242. if (file->f_flags & O_NONBLOCK) {
  243. rets = -EAGAIN;
  244. goto out;
  245. }
  246. mutex_unlock(&dev->device_lock);
  247. rets = wait_event_interruptible(cl->tx_wait,
  248. cl->writing_state == MEI_WRITE_COMPLETE ||
  249. (!mei_cl_is_connected(cl)));
  250. mutex_lock(&dev->device_lock);
  251. if (rets) {
  252. if (signal_pending(current))
  253. rets = -EINTR;
  254. goto out;
  255. }
  256. if (!mei_cl_is_connected(cl)) {
  257. rets = -ENODEV;
  258. goto out;
  259. }
  260. }
  261. *offset = 0;
  262. cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
  263. if (!cb) {
  264. rets = -ENOMEM;
  265. goto out;
  266. }
  267. rets = copy_from_user(cb->buf.data, ubuf, length);
  268. if (rets) {
  269. dev_dbg(dev->dev, "failed to copy data from userland\n");
  270. rets = -EFAULT;
  271. mei_io_cb_free(cb);
  272. goto out;
  273. }
  274. rets = mei_cl_write(cl, cb);
  275. out:
  276. mutex_unlock(&dev->device_lock);
  277. return rets;
  278. }
  279. /**
  280. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  281. *
  282. * @file: private data of the file object
  283. * @data: IOCTL connect data, input and output parameters
  284. *
  285. * Locking: called under "dev->device_lock" lock
  286. *
  287. * Return: 0 on success, <0 on failure.
  288. */
  289. static int mei_ioctl_connect_client(struct file *file,
  290. struct mei_connect_client_data *data)
  291. {
  292. struct mei_device *dev;
  293. struct mei_client *client;
  294. struct mei_me_client *me_cl;
  295. struct mei_cl *cl;
  296. int rets;
  297. cl = file->private_data;
  298. dev = cl->dev;
  299. if (dev->dev_state != MEI_DEV_ENABLED)
  300. return -ENODEV;
  301. if (cl->state != MEI_FILE_INITIALIZING &&
  302. cl->state != MEI_FILE_DISCONNECTED)
  303. return -EBUSY;
  304. /* find ME client we're trying to connect to */
  305. me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  306. if (!me_cl) {
  307. dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  308. &data->in_client_uuid);
  309. rets = -ENOTTY;
  310. goto end;
  311. }
  312. if (me_cl->props.fixed_address) {
  313. bool forbidden = dev->override_fixed_address ?
  314. !dev->allow_fixed_address : !dev->hbm_f_fa_supported;
  315. if (forbidden) {
  316. dev_dbg(dev->dev, "Connection forbidden to FW Client UUID = %pUl\n",
  317. &data->in_client_uuid);
  318. rets = -ENOTTY;
  319. goto end;
  320. }
  321. }
  322. dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
  323. me_cl->client_id);
  324. dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
  325. me_cl->props.protocol_version);
  326. dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
  327. me_cl->props.max_msg_length);
  328. /* prepare the output buffer */
  329. client = &data->out_client_properties;
  330. client->max_msg_length = me_cl->props.max_msg_length;
  331. client->protocol_version = me_cl->props.protocol_version;
  332. dev_dbg(dev->dev, "Can connect?\n");
  333. rets = mei_cl_connect(cl, me_cl, file);
  334. end:
  335. mei_me_cl_put(me_cl);
  336. return rets;
  337. }
  338. /**
  339. * mei_ioctl_client_notify_request -
  340. * propagate event notification request to client
  341. *
  342. * @file: pointer to file structure
  343. * @request: 0 - disable, 1 - enable
  344. *
  345. * Return: 0 on success , <0 on error
  346. */
  347. static int mei_ioctl_client_notify_request(const struct file *file, u32 request)
  348. {
  349. struct mei_cl *cl = file->private_data;
  350. if (request != MEI_HBM_NOTIFICATION_START &&
  351. request != MEI_HBM_NOTIFICATION_STOP)
  352. return -EINVAL;
  353. return mei_cl_notify_request(cl, file, (u8)request);
  354. }
  355. /**
  356. * mei_ioctl_client_notify_get - wait for notification request
  357. *
  358. * @file: pointer to file structure
  359. * @notify_get: 0 - disable, 1 - enable
  360. *
  361. * Return: 0 on success , <0 on error
  362. */
  363. static int mei_ioctl_client_notify_get(const struct file *file, u32 *notify_get)
  364. {
  365. struct mei_cl *cl = file->private_data;
  366. bool notify_ev;
  367. bool block = (file->f_flags & O_NONBLOCK) == 0;
  368. int rets;
  369. rets = mei_cl_notify_get(cl, block, &notify_ev);
  370. if (rets)
  371. return rets;
  372. *notify_get = notify_ev ? 1 : 0;
  373. return 0;
  374. }
  375. /**
  376. * mei_ioctl - the IOCTL function
  377. *
  378. * @file: pointer to file structure
  379. * @cmd: ioctl command
  380. * @data: pointer to mei message structure
  381. *
  382. * Return: 0 on success , <0 on error
  383. */
  384. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  385. {
  386. struct mei_device *dev;
  387. struct mei_cl *cl = file->private_data;
  388. struct mei_connect_client_data connect_data;
  389. u32 notify_get, notify_req;
  390. int rets;
  391. if (WARN_ON(!cl || !cl->dev))
  392. return -ENODEV;
  393. dev = cl->dev;
  394. dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
  395. mutex_lock(&dev->device_lock);
  396. if (dev->dev_state != MEI_DEV_ENABLED) {
  397. rets = -ENODEV;
  398. goto out;
  399. }
  400. switch (cmd) {
  401. case IOCTL_MEI_CONNECT_CLIENT:
  402. dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  403. if (copy_from_user(&connect_data, (char __user *)data,
  404. sizeof(struct mei_connect_client_data))) {
  405. dev_dbg(dev->dev, "failed to copy data from userland\n");
  406. rets = -EFAULT;
  407. goto out;
  408. }
  409. rets = mei_ioctl_connect_client(file, &connect_data);
  410. if (rets)
  411. goto out;
  412. /* if all is ok, copying the data back to user. */
  413. if (copy_to_user((char __user *)data, &connect_data,
  414. sizeof(struct mei_connect_client_data))) {
  415. dev_dbg(dev->dev, "failed to copy data to userland\n");
  416. rets = -EFAULT;
  417. goto out;
  418. }
  419. break;
  420. case IOCTL_MEI_NOTIFY_SET:
  421. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_SET.\n");
  422. if (copy_from_user(&notify_req,
  423. (char __user *)data, sizeof(notify_req))) {
  424. dev_dbg(dev->dev, "failed to copy data from userland\n");
  425. rets = -EFAULT;
  426. goto out;
  427. }
  428. rets = mei_ioctl_client_notify_request(file, notify_req);
  429. break;
  430. case IOCTL_MEI_NOTIFY_GET:
  431. dev_dbg(dev->dev, ": IOCTL_MEI_NOTIFY_GET.\n");
  432. rets = mei_ioctl_client_notify_get(file, &notify_get);
  433. if (rets)
  434. goto out;
  435. dev_dbg(dev->dev, "copy connect data to user\n");
  436. if (copy_to_user((char __user *)data,
  437. &notify_get, sizeof(notify_get))) {
  438. dev_dbg(dev->dev, "failed to copy data to userland\n");
  439. rets = -EFAULT;
  440. goto out;
  441. }
  442. break;
  443. default:
  444. rets = -ENOIOCTLCMD;
  445. }
  446. out:
  447. mutex_unlock(&dev->device_lock);
  448. return rets;
  449. }
  450. /**
  451. * mei_compat_ioctl - the compat IOCTL function
  452. *
  453. * @file: pointer to file structure
  454. * @cmd: ioctl command
  455. * @data: pointer to mei message structure
  456. *
  457. * Return: 0 on success , <0 on error
  458. */
  459. #ifdef CONFIG_COMPAT
  460. static long mei_compat_ioctl(struct file *file,
  461. unsigned int cmd, unsigned long data)
  462. {
  463. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  464. }
  465. #endif
  466. /**
  467. * mei_poll - the poll function
  468. *
  469. * @file: pointer to file structure
  470. * @wait: pointer to poll_table structure
  471. *
  472. * Return: poll mask
  473. */
  474. static __poll_t mei_poll(struct file *file, poll_table *wait)
  475. {
  476. __poll_t req_events = poll_requested_events(wait);
  477. struct mei_cl *cl = file->private_data;
  478. struct mei_device *dev;
  479. __poll_t mask = 0;
  480. bool notify_en;
  481. if (WARN_ON(!cl || !cl->dev))
  482. return EPOLLERR;
  483. dev = cl->dev;
  484. mutex_lock(&dev->device_lock);
  485. notify_en = cl->notify_en && (req_events & EPOLLPRI);
  486. if (dev->dev_state != MEI_DEV_ENABLED ||
  487. !mei_cl_is_connected(cl)) {
  488. mask = EPOLLERR;
  489. goto out;
  490. }
  491. if (notify_en) {
  492. poll_wait(file, &cl->ev_wait, wait);
  493. if (cl->notify_ev)
  494. mask |= EPOLLPRI;
  495. }
  496. if (req_events & (EPOLLIN | EPOLLRDNORM)) {
  497. poll_wait(file, &cl->rx_wait, wait);
  498. if (!list_empty(&cl->rd_completed))
  499. mask |= EPOLLIN | EPOLLRDNORM;
  500. else
  501. mei_cl_read_start(cl, mei_cl_mtu(cl), file);
  502. }
  503. if (req_events & (POLLOUT | POLLWRNORM)) {
  504. poll_wait(file, &cl->tx_wait, wait);
  505. if (cl->tx_cb_queued < dev->tx_queue_limit)
  506. mask |= POLLOUT | POLLWRNORM;
  507. }
  508. out:
  509. mutex_unlock(&dev->device_lock);
  510. return mask;
  511. }
  512. /**
  513. * mei_cl_is_write_queued - check if the client has pending writes.
  514. *
  515. * @cl: writing host client
  516. *
  517. * Return: true if client is writing, false otherwise.
  518. */
  519. static bool mei_cl_is_write_queued(struct mei_cl *cl)
  520. {
  521. struct mei_device *dev = cl->dev;
  522. struct mei_cl_cb *cb;
  523. list_for_each_entry(cb, &dev->write_list, list)
  524. if (cb->cl == cl)
  525. return true;
  526. list_for_each_entry(cb, &dev->write_waiting_list, list)
  527. if (cb->cl == cl)
  528. return true;
  529. return false;
  530. }
  531. /**
  532. * mei_fsync - the fsync handler
  533. *
  534. * @fp: pointer to file structure
  535. * @start: unused
  536. * @end: unused
  537. * @datasync: unused
  538. *
  539. * Return: 0 on success, -ENODEV if client is not connected
  540. */
  541. static int mei_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
  542. {
  543. struct mei_cl *cl = fp->private_data;
  544. struct mei_device *dev;
  545. int rets;
  546. if (WARN_ON(!cl || !cl->dev))
  547. return -ENODEV;
  548. dev = cl->dev;
  549. mutex_lock(&dev->device_lock);
  550. if (dev->dev_state != MEI_DEV_ENABLED || !mei_cl_is_connected(cl)) {
  551. rets = -ENODEV;
  552. goto out;
  553. }
  554. while (mei_cl_is_write_queued(cl)) {
  555. mutex_unlock(&dev->device_lock);
  556. rets = wait_event_interruptible(cl->tx_wait,
  557. cl->writing_state == MEI_WRITE_COMPLETE ||
  558. !mei_cl_is_connected(cl));
  559. mutex_lock(&dev->device_lock);
  560. if (rets) {
  561. if (signal_pending(current))
  562. rets = -EINTR;
  563. goto out;
  564. }
  565. if (!mei_cl_is_connected(cl)) {
  566. rets = -ENODEV;
  567. goto out;
  568. }
  569. }
  570. rets = 0;
  571. out:
  572. mutex_unlock(&dev->device_lock);
  573. return rets;
  574. }
  575. /**
  576. * mei_fasync - asynchronous io support
  577. *
  578. * @fd: file descriptor
  579. * @file: pointer to file structure
  580. * @band: band bitmap
  581. *
  582. * Return: negative on error,
  583. * 0 if it did no changes,
  584. * and positive a process was added or deleted
  585. */
  586. static int mei_fasync(int fd, struct file *file, int band)
  587. {
  588. struct mei_cl *cl = file->private_data;
  589. if (!mei_cl_is_connected(cl))
  590. return -ENODEV;
  591. return fasync_helper(fd, file, band, &cl->ev_async);
  592. }
  593. /**
  594. * fw_status_show - mei device fw_status attribute show method
  595. *
  596. * @device: device pointer
  597. * @attr: attribute pointer
  598. * @buf: char out buffer
  599. *
  600. * Return: number of the bytes printed into buf or error
  601. */
  602. static ssize_t fw_status_show(struct device *device,
  603. struct device_attribute *attr, char *buf)
  604. {
  605. struct mei_device *dev = dev_get_drvdata(device);
  606. struct mei_fw_status fw_status;
  607. int err, i;
  608. ssize_t cnt = 0;
  609. mutex_lock(&dev->device_lock);
  610. err = mei_fw_status(dev, &fw_status);
  611. mutex_unlock(&dev->device_lock);
  612. if (err) {
  613. dev_err(device, "read fw_status error = %d\n", err);
  614. return err;
  615. }
  616. for (i = 0; i < fw_status.count; i++)
  617. cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
  618. fw_status.status[i]);
  619. return cnt;
  620. }
  621. static DEVICE_ATTR_RO(fw_status);
  622. /**
  623. * hbm_ver_show - display HBM protocol version negotiated with FW
  624. *
  625. * @device: device pointer
  626. * @attr: attribute pointer
  627. * @buf: char out buffer
  628. *
  629. * Return: number of the bytes printed into buf or error
  630. */
  631. static ssize_t hbm_ver_show(struct device *device,
  632. struct device_attribute *attr, char *buf)
  633. {
  634. struct mei_device *dev = dev_get_drvdata(device);
  635. struct hbm_version ver;
  636. mutex_lock(&dev->device_lock);
  637. ver = dev->version;
  638. mutex_unlock(&dev->device_lock);
  639. return sprintf(buf, "%u.%u\n", ver.major_version, ver.minor_version);
  640. }
  641. static DEVICE_ATTR_RO(hbm_ver);
  642. /**
  643. * hbm_ver_drv_show - display HBM protocol version advertised by driver
  644. *
  645. * @device: device pointer
  646. * @attr: attribute pointer
  647. * @buf: char out buffer
  648. *
  649. * Return: number of the bytes printed into buf or error
  650. */
  651. static ssize_t hbm_ver_drv_show(struct device *device,
  652. struct device_attribute *attr, char *buf)
  653. {
  654. return sprintf(buf, "%u.%u\n", HBM_MAJOR_VERSION, HBM_MINOR_VERSION);
  655. }
  656. static DEVICE_ATTR_RO(hbm_ver_drv);
  657. static ssize_t tx_queue_limit_show(struct device *device,
  658. struct device_attribute *attr, char *buf)
  659. {
  660. struct mei_device *dev = dev_get_drvdata(device);
  661. u8 size = 0;
  662. mutex_lock(&dev->device_lock);
  663. size = dev->tx_queue_limit;
  664. mutex_unlock(&dev->device_lock);
  665. return snprintf(buf, PAGE_SIZE, "%u\n", size);
  666. }
  667. static ssize_t tx_queue_limit_store(struct device *device,
  668. struct device_attribute *attr,
  669. const char *buf, size_t count)
  670. {
  671. struct mei_device *dev = dev_get_drvdata(device);
  672. u8 limit;
  673. unsigned int inp;
  674. int err;
  675. err = kstrtouint(buf, 10, &inp);
  676. if (err)
  677. return err;
  678. if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
  679. return -EINVAL;
  680. limit = inp;
  681. mutex_lock(&dev->device_lock);
  682. dev->tx_queue_limit = limit;
  683. mutex_unlock(&dev->device_lock);
  684. return count;
  685. }
  686. static DEVICE_ATTR_RW(tx_queue_limit);
  687. static struct attribute *mei_attrs[] = {
  688. &dev_attr_fw_status.attr,
  689. &dev_attr_hbm_ver.attr,
  690. &dev_attr_hbm_ver_drv.attr,
  691. &dev_attr_tx_queue_limit.attr,
  692. NULL
  693. };
  694. ATTRIBUTE_GROUPS(mei);
  695. /*
  696. * file operations structure will be used for mei char device.
  697. */
  698. static const struct file_operations mei_fops = {
  699. .owner = THIS_MODULE,
  700. .read = mei_read,
  701. .unlocked_ioctl = mei_ioctl,
  702. #ifdef CONFIG_COMPAT
  703. .compat_ioctl = mei_compat_ioctl,
  704. #endif
  705. .open = mei_open,
  706. .release = mei_release,
  707. .write = mei_write,
  708. .poll = mei_poll,
  709. .fsync = mei_fsync,
  710. .fasync = mei_fasync,
  711. .llseek = no_llseek
  712. };
  713. static struct class *mei_class;
  714. static dev_t mei_devt;
  715. #define MEI_MAX_DEVS MINORMASK
  716. static DEFINE_MUTEX(mei_minor_lock);
  717. static DEFINE_IDR(mei_idr);
  718. /**
  719. * mei_minor_get - obtain next free device minor number
  720. *
  721. * @dev: device pointer
  722. *
  723. * Return: allocated minor, or -ENOSPC if no free minor left
  724. */
  725. static int mei_minor_get(struct mei_device *dev)
  726. {
  727. int ret;
  728. mutex_lock(&mei_minor_lock);
  729. ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
  730. if (ret >= 0)
  731. dev->minor = ret;
  732. else if (ret == -ENOSPC)
  733. dev_err(dev->dev, "too many mei devices\n");
  734. mutex_unlock(&mei_minor_lock);
  735. return ret;
  736. }
  737. /**
  738. * mei_minor_free - mark device minor number as free
  739. *
  740. * @dev: device pointer
  741. */
  742. static void mei_minor_free(struct mei_device *dev)
  743. {
  744. mutex_lock(&mei_minor_lock);
  745. idr_remove(&mei_idr, dev->minor);
  746. mutex_unlock(&mei_minor_lock);
  747. }
  748. int mei_register(struct mei_device *dev, struct device *parent)
  749. {
  750. struct device *clsdev; /* class device */
  751. int ret, devno;
  752. ret = mei_minor_get(dev);
  753. if (ret < 0)
  754. return ret;
  755. /* Fill in the data structures */
  756. devno = MKDEV(MAJOR(mei_devt), dev->minor);
  757. cdev_init(&dev->cdev, &mei_fops);
  758. dev->cdev.owner = parent->driver->owner;
  759. /* Add the device */
  760. ret = cdev_add(&dev->cdev, devno, 1);
  761. if (ret) {
  762. dev_err(parent, "unable to add device %d:%d\n",
  763. MAJOR(mei_devt), dev->minor);
  764. goto err_dev_add;
  765. }
  766. clsdev = device_create_with_groups(mei_class, parent, devno,
  767. dev, mei_groups,
  768. "mei%d", dev->minor);
  769. if (IS_ERR(clsdev)) {
  770. dev_err(parent, "unable to create device %d:%d\n",
  771. MAJOR(mei_devt), dev->minor);
  772. ret = PTR_ERR(clsdev);
  773. goto err_dev_create;
  774. }
  775. ret = mei_dbgfs_register(dev, dev_name(clsdev));
  776. if (ret) {
  777. dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
  778. goto err_dev_dbgfs;
  779. }
  780. return 0;
  781. err_dev_dbgfs:
  782. device_destroy(mei_class, devno);
  783. err_dev_create:
  784. cdev_del(&dev->cdev);
  785. err_dev_add:
  786. mei_minor_free(dev);
  787. return ret;
  788. }
  789. EXPORT_SYMBOL_GPL(mei_register);
  790. void mei_deregister(struct mei_device *dev)
  791. {
  792. int devno;
  793. devno = dev->cdev.dev;
  794. cdev_del(&dev->cdev);
  795. mei_dbgfs_deregister(dev);
  796. device_destroy(mei_class, devno);
  797. mei_minor_free(dev);
  798. }
  799. EXPORT_SYMBOL_GPL(mei_deregister);
  800. static int __init mei_init(void)
  801. {
  802. int ret;
  803. mei_class = class_create(THIS_MODULE, "mei");
  804. if (IS_ERR(mei_class)) {
  805. pr_err("couldn't create class\n");
  806. ret = PTR_ERR(mei_class);
  807. goto err;
  808. }
  809. ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
  810. if (ret < 0) {
  811. pr_err("unable to allocate char dev region\n");
  812. goto err_class;
  813. }
  814. ret = mei_cl_bus_init();
  815. if (ret < 0) {
  816. pr_err("unable to initialize bus\n");
  817. goto err_chrdev;
  818. }
  819. return 0;
  820. err_chrdev:
  821. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  822. err_class:
  823. class_destroy(mei_class);
  824. err:
  825. return ret;
  826. }
  827. static void __exit mei_exit(void)
  828. {
  829. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  830. class_destroy(mei_class);
  831. mei_cl_bus_exit();
  832. }
  833. module_init(mei_init);
  834. module_exit(mei_exit);
  835. MODULE_AUTHOR("Intel Corporation");
  836. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  837. MODULE_LICENSE("GPL v2");