client.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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/pci.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/delay.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/mei.h>
  22. #include "mei_dev.h"
  23. #include "hbm.h"
  24. #include "client.h"
  25. /**
  26. * mei_me_cl_by_uuid - locate index of me client
  27. *
  28. * @dev: mei device
  29. *
  30. * Locking: called under "dev->device_lock" lock
  31. *
  32. * returns me client index or -ENOENT if not found
  33. */
  34. int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *uuid)
  35. {
  36. int i;
  37. for (i = 0; i < dev->me_clients_num; ++i)
  38. if (uuid_le_cmp(*uuid,
  39. dev->me_clients[i].props.protocol_name) == 0)
  40. return i;
  41. return -ENOENT;
  42. }
  43. /**
  44. * mei_me_cl_by_id return index to me_clients for client_id
  45. *
  46. * @dev: the device structure
  47. * @client_id: me client id
  48. *
  49. * Locking: called under "dev->device_lock" lock
  50. *
  51. * returns index on success, -ENOENT on failure.
  52. */
  53. int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  54. {
  55. int i;
  56. for (i = 0; i < dev->me_clients_num; i++)
  57. if (dev->me_clients[i].client_id == client_id)
  58. return i;
  59. return -ENOENT;
  60. }
  61. /**
  62. * mei_cl_cmp_id - tells if the clients are the same
  63. *
  64. * @cl1: host client 1
  65. * @cl2: host client 2
  66. *
  67. * returns true - if the clients has same host and me ids
  68. * false - otherwise
  69. */
  70. static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
  71. const struct mei_cl *cl2)
  72. {
  73. return cl1 && cl2 &&
  74. (cl1->host_client_id == cl2->host_client_id) &&
  75. (cl1->me_client_id == cl2->me_client_id);
  76. }
  77. /**
  78. * mei_io_list_flush - removes cbs belonging to cl.
  79. *
  80. * @list: an instance of our list structure
  81. * @cl: host client, can be NULL for flushing the whole list
  82. * @free: whether to free the cbs
  83. */
  84. static void __mei_io_list_flush(struct mei_cl_cb *list,
  85. struct mei_cl *cl, bool free)
  86. {
  87. struct mei_cl_cb *cb;
  88. struct mei_cl_cb *next;
  89. /* enable removing everything if no cl is specified */
  90. list_for_each_entry_safe(cb, next, &list->list, list) {
  91. if (!cl || (cb->cl && mei_cl_cmp_id(cl, cb->cl))) {
  92. list_del(&cb->list);
  93. if (free)
  94. mei_io_cb_free(cb);
  95. }
  96. }
  97. }
  98. /**
  99. * mei_io_list_flush - removes list entry belonging to cl.
  100. *
  101. * @list: An instance of our list structure
  102. * @cl: host client
  103. */
  104. static inline void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
  105. {
  106. __mei_io_list_flush(list, cl, false);
  107. }
  108. /**
  109. * mei_io_list_free - removes cb belonging to cl and free them
  110. *
  111. * @list: An instance of our list structure
  112. * @cl: host client
  113. */
  114. static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
  115. {
  116. __mei_io_list_flush(list, cl, true);
  117. }
  118. /**
  119. * mei_io_cb_free - free mei_cb_private related memory
  120. *
  121. * @cb: mei callback struct
  122. */
  123. void mei_io_cb_free(struct mei_cl_cb *cb)
  124. {
  125. if (cb == NULL)
  126. return;
  127. kfree(cb->request_buffer.data);
  128. kfree(cb->response_buffer.data);
  129. kfree(cb);
  130. }
  131. /**
  132. * mei_io_cb_init - allocate and initialize io callback
  133. *
  134. * @cl - mei client
  135. * @fp: pointer to file structure
  136. *
  137. * returns mei_cl_cb pointer or NULL;
  138. */
  139. struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
  140. {
  141. struct mei_cl_cb *cb;
  142. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  143. if (!cb)
  144. return NULL;
  145. mei_io_list_init(cb);
  146. cb->file_object = fp;
  147. cb->cl = cl;
  148. cb->buf_idx = 0;
  149. return cb;
  150. }
  151. /**
  152. * mei_io_cb_alloc_req_buf - allocate request buffer
  153. *
  154. * @cb: io callback structure
  155. * @length: size of the buffer
  156. *
  157. * returns 0 on success
  158. * -EINVAL if cb is NULL
  159. * -ENOMEM if allocation failed
  160. */
  161. int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
  162. {
  163. if (!cb)
  164. return -EINVAL;
  165. if (length == 0)
  166. return 0;
  167. cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
  168. if (!cb->request_buffer.data)
  169. return -ENOMEM;
  170. cb->request_buffer.size = length;
  171. return 0;
  172. }
  173. /**
  174. * mei_io_cb_alloc_resp_buf - allocate response buffer
  175. *
  176. * @cb: io callback structure
  177. * @length: size of the buffer
  178. *
  179. * returns 0 on success
  180. * -EINVAL if cb is NULL
  181. * -ENOMEM if allocation failed
  182. */
  183. int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
  184. {
  185. if (!cb)
  186. return -EINVAL;
  187. if (length == 0)
  188. return 0;
  189. cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
  190. if (!cb->response_buffer.data)
  191. return -ENOMEM;
  192. cb->response_buffer.size = length;
  193. return 0;
  194. }
  195. /**
  196. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  197. *
  198. * @cl: host client
  199. */
  200. int mei_cl_flush_queues(struct mei_cl *cl)
  201. {
  202. struct mei_device *dev;
  203. if (WARN_ON(!cl || !cl->dev))
  204. return -EINVAL;
  205. dev = cl->dev;
  206. cl_dbg(dev, cl, "remove list entry belonging to cl\n");
  207. mei_io_list_flush(&cl->dev->read_list, cl);
  208. mei_io_list_free(&cl->dev->write_list, cl);
  209. mei_io_list_free(&cl->dev->write_waiting_list, cl);
  210. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  211. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  212. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  213. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  214. return 0;
  215. }
  216. /**
  217. * mei_cl_init - initializes cl.
  218. *
  219. * @cl: host client to be initialized
  220. * @dev: mei device
  221. */
  222. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
  223. {
  224. memset(cl, 0, sizeof(struct mei_cl));
  225. init_waitqueue_head(&cl->wait);
  226. init_waitqueue_head(&cl->rx_wait);
  227. init_waitqueue_head(&cl->tx_wait);
  228. INIT_LIST_HEAD(&cl->link);
  229. INIT_LIST_HEAD(&cl->device_link);
  230. cl->reading_state = MEI_IDLE;
  231. cl->writing_state = MEI_IDLE;
  232. cl->dev = dev;
  233. }
  234. /**
  235. * mei_cl_allocate - allocates cl structure and sets it up.
  236. *
  237. * @dev: mei device
  238. * returns The allocated file or NULL on failure
  239. */
  240. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  241. {
  242. struct mei_cl *cl;
  243. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  244. if (!cl)
  245. return NULL;
  246. mei_cl_init(cl, dev);
  247. return cl;
  248. }
  249. /**
  250. * mei_cl_find_read_cb - find this cl's callback in the read list
  251. *
  252. * @cl: host client
  253. *
  254. * returns cb on success, NULL on error
  255. */
  256. struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
  257. {
  258. struct mei_device *dev = cl->dev;
  259. struct mei_cl_cb *cb;
  260. list_for_each_entry(cb, &dev->read_list.list, list)
  261. if (mei_cl_cmp_id(cl, cb->cl))
  262. return cb;
  263. return NULL;
  264. }
  265. /** mei_cl_link: allocate host id in the host map
  266. *
  267. * @cl - host client
  268. * @id - fixed host id or -1 for generic one
  269. *
  270. * returns 0 on success
  271. * -EINVAL on incorrect values
  272. * -ENONET if client not found
  273. */
  274. int mei_cl_link(struct mei_cl *cl, int id)
  275. {
  276. struct mei_device *dev;
  277. long open_handle_count;
  278. if (WARN_ON(!cl || !cl->dev))
  279. return -EINVAL;
  280. dev = cl->dev;
  281. /* If Id is not assigned get one*/
  282. if (id == MEI_HOST_CLIENT_ID_ANY)
  283. id = find_first_zero_bit(dev->host_clients_map,
  284. MEI_CLIENTS_MAX);
  285. if (id >= MEI_CLIENTS_MAX) {
  286. dev_err(&dev->pdev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
  287. return -EMFILE;
  288. }
  289. open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
  290. if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
  291. dev_err(&dev->pdev->dev, "open_handle_count exceeded %d",
  292. MEI_MAX_OPEN_HANDLE_COUNT);
  293. return -EMFILE;
  294. }
  295. dev->open_handle_count++;
  296. cl->host_client_id = id;
  297. list_add_tail(&cl->link, &dev->file_list);
  298. set_bit(id, dev->host_clients_map);
  299. cl->state = MEI_FILE_INITIALIZING;
  300. cl_dbg(dev, cl, "link cl\n");
  301. return 0;
  302. }
  303. /**
  304. * mei_cl_unlink - remove me_cl from the list
  305. *
  306. * @cl: host client
  307. */
  308. int mei_cl_unlink(struct mei_cl *cl)
  309. {
  310. struct mei_device *dev;
  311. /* don't shout on error exit path */
  312. if (!cl)
  313. return 0;
  314. /* wd and amthif might not be initialized */
  315. if (!cl->dev)
  316. return 0;
  317. dev = cl->dev;
  318. cl_dbg(dev, cl, "unlink client");
  319. if (dev->open_handle_count > 0)
  320. dev->open_handle_count--;
  321. /* never clear the 0 bit */
  322. if (cl->host_client_id)
  323. clear_bit(cl->host_client_id, dev->host_clients_map);
  324. list_del_init(&cl->link);
  325. cl->state = MEI_FILE_INITIALIZING;
  326. return 0;
  327. }
  328. void mei_host_client_init(struct work_struct *work)
  329. {
  330. struct mei_device *dev = container_of(work,
  331. struct mei_device, init_work);
  332. struct mei_client_properties *client_props;
  333. int i;
  334. mutex_lock(&dev->device_lock);
  335. for (i = 0; i < dev->me_clients_num; i++) {
  336. client_props = &dev->me_clients[i].props;
  337. if (!uuid_le_cmp(client_props->protocol_name, mei_amthif_guid))
  338. mei_amthif_host_init(dev);
  339. else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
  340. mei_wd_host_init(dev);
  341. else if (!uuid_le_cmp(client_props->protocol_name, mei_nfc_guid))
  342. mei_nfc_host_init(dev);
  343. }
  344. dev->dev_state = MEI_DEV_ENABLED;
  345. dev->reset_count = 0;
  346. mutex_unlock(&dev->device_lock);
  347. pm_runtime_mark_last_busy(&dev->pdev->dev);
  348. dev_dbg(&dev->pdev->dev, "rpm: autosuspend\n");
  349. pm_runtime_autosuspend(&dev->pdev->dev);
  350. }
  351. /**
  352. * mei_hbuf_acquire: try to acquire host buffer
  353. *
  354. * @dev: the device structure
  355. * returns true if host buffer was acquired
  356. */
  357. bool mei_hbuf_acquire(struct mei_device *dev)
  358. {
  359. if (mei_pg_state(dev) == MEI_PG_ON ||
  360. dev->pg_event == MEI_PG_EVENT_WAIT) {
  361. dev_dbg(&dev->pdev->dev, "device is in pg\n");
  362. return false;
  363. }
  364. if (!dev->hbuf_is_ready) {
  365. dev_dbg(&dev->pdev->dev, "hbuf is not ready\n");
  366. return false;
  367. }
  368. dev->hbuf_is_ready = false;
  369. return true;
  370. }
  371. /**
  372. * mei_cl_disconnect - disconnect host client from the me one
  373. *
  374. * @cl: host client
  375. *
  376. * Locking: called under "dev->device_lock" lock
  377. *
  378. * returns 0 on success, <0 on failure.
  379. */
  380. int mei_cl_disconnect(struct mei_cl *cl)
  381. {
  382. struct mei_device *dev;
  383. struct mei_cl_cb *cb;
  384. int rets, err;
  385. if (WARN_ON(!cl || !cl->dev))
  386. return -ENODEV;
  387. dev = cl->dev;
  388. cl_dbg(dev, cl, "disconnecting");
  389. if (cl->state != MEI_FILE_DISCONNECTING)
  390. return 0;
  391. rets = pm_runtime_get(&dev->pdev->dev);
  392. if (rets < 0 && rets != -EINPROGRESS) {
  393. pm_runtime_put_noidle(&dev->pdev->dev);
  394. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  395. return rets;
  396. }
  397. cb = mei_io_cb_init(cl, NULL);
  398. if (!cb) {
  399. rets = -ENOMEM;
  400. goto free;
  401. }
  402. cb->fop_type = MEI_FOP_CLOSE;
  403. if (mei_hbuf_acquire(dev)) {
  404. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  405. rets = -ENODEV;
  406. cl_err(dev, cl, "failed to disconnect.\n");
  407. goto free;
  408. }
  409. mdelay(10); /* Wait for hardware disconnection ready */
  410. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  411. } else {
  412. cl_dbg(dev, cl, "add disconnect cb to control write list\n");
  413. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  414. }
  415. mutex_unlock(&dev->device_lock);
  416. err = wait_event_timeout(dev->wait_recvd_msg,
  417. MEI_FILE_DISCONNECTED == cl->state,
  418. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  419. mutex_lock(&dev->device_lock);
  420. if (MEI_FILE_DISCONNECTED == cl->state) {
  421. rets = 0;
  422. cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
  423. } else {
  424. rets = -ENODEV;
  425. if (MEI_FILE_DISCONNECTED != cl->state)
  426. cl_err(dev, cl, "wrong status client disconnect.\n");
  427. if (err)
  428. cl_dbg(dev, cl, "wait failed disconnect err=%d\n", err);
  429. cl_err(dev, cl, "failed to disconnect from FW client.\n");
  430. }
  431. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  432. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  433. free:
  434. cl_dbg(dev, cl, "rpm: autosuspend\n");
  435. pm_runtime_mark_last_busy(&dev->pdev->dev);
  436. pm_runtime_put_autosuspend(&dev->pdev->dev);
  437. mei_io_cb_free(cb);
  438. return rets;
  439. }
  440. /**
  441. * mei_cl_is_other_connecting - checks if other
  442. * client with the same me client id is connecting
  443. *
  444. * @cl: private data of the file object
  445. *
  446. * returns true if other client is connected, false - otherwise.
  447. */
  448. bool mei_cl_is_other_connecting(struct mei_cl *cl)
  449. {
  450. struct mei_device *dev;
  451. struct mei_cl *ocl; /* the other client */
  452. if (WARN_ON(!cl || !cl->dev))
  453. return false;
  454. dev = cl->dev;
  455. list_for_each_entry(ocl, &dev->file_list, link) {
  456. if (ocl->state == MEI_FILE_CONNECTING &&
  457. ocl != cl &&
  458. cl->me_client_id == ocl->me_client_id)
  459. return true;
  460. }
  461. return false;
  462. }
  463. /**
  464. * mei_cl_connect - connect host client to the me one
  465. *
  466. * @cl: host client
  467. *
  468. * Locking: called under "dev->device_lock" lock
  469. *
  470. * returns 0 on success, <0 on failure.
  471. */
  472. int mei_cl_connect(struct mei_cl *cl, struct file *file)
  473. {
  474. struct mei_device *dev;
  475. struct mei_cl_cb *cb;
  476. int rets;
  477. if (WARN_ON(!cl || !cl->dev))
  478. return -ENODEV;
  479. dev = cl->dev;
  480. rets = pm_runtime_get(&dev->pdev->dev);
  481. if (rets < 0 && rets != -EINPROGRESS) {
  482. pm_runtime_put_noidle(&dev->pdev->dev);
  483. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  484. return rets;
  485. }
  486. cb = mei_io_cb_init(cl, file);
  487. if (!cb) {
  488. rets = -ENOMEM;
  489. goto out;
  490. }
  491. cb->fop_type = MEI_FOP_CONNECT;
  492. /* run hbuf acquire last so we don't have to undo */
  493. if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
  494. cl->state = MEI_FILE_CONNECTING;
  495. if (mei_hbm_cl_connect_req(dev, cl)) {
  496. rets = -ENODEV;
  497. goto out;
  498. }
  499. cl->timer_count = MEI_CONNECT_TIMEOUT;
  500. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  501. } else {
  502. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  503. }
  504. mutex_unlock(&dev->device_lock);
  505. wait_event_timeout(dev->wait_recvd_msg,
  506. (cl->state == MEI_FILE_CONNECTED ||
  507. cl->state == MEI_FILE_DISCONNECTED),
  508. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  509. mutex_lock(&dev->device_lock);
  510. if (cl->state != MEI_FILE_CONNECTED) {
  511. /* something went really wrong */
  512. if (!cl->status)
  513. cl->status = -EFAULT;
  514. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  515. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  516. }
  517. rets = cl->status;
  518. out:
  519. cl_dbg(dev, cl, "rpm: autosuspend\n");
  520. pm_runtime_mark_last_busy(&dev->pdev->dev);
  521. pm_runtime_put_autosuspend(&dev->pdev->dev);
  522. mei_io_cb_free(cb);
  523. return rets;
  524. }
  525. /**
  526. * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
  527. *
  528. * @cl: private data of the file object
  529. *
  530. * returns 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
  531. * -ENOENT if mei_cl is not present
  532. * -EINVAL if single_recv_buf == 0
  533. */
  534. int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
  535. {
  536. struct mei_device *dev;
  537. struct mei_me_client *me_cl;
  538. int id;
  539. if (WARN_ON(!cl || !cl->dev))
  540. return -EINVAL;
  541. dev = cl->dev;
  542. if (!dev->me_clients_num)
  543. return 0;
  544. if (cl->mei_flow_ctrl_creds > 0)
  545. return 1;
  546. id = mei_me_cl_by_id(dev, cl->me_client_id);
  547. if (id < 0) {
  548. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  549. return id;
  550. }
  551. me_cl = &dev->me_clients[id];
  552. if (me_cl->mei_flow_ctrl_creds) {
  553. if (WARN_ON(me_cl->props.single_recv_buf == 0))
  554. return -EINVAL;
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. /**
  560. * mei_cl_flow_ctrl_reduce - reduces flow_control.
  561. *
  562. * @cl: private data of the file object
  563. *
  564. * @returns
  565. * 0 on success
  566. * -ENOENT when me client is not found
  567. * -EINVAL when ctrl credits are <= 0
  568. */
  569. int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
  570. {
  571. struct mei_device *dev;
  572. struct mei_me_client *me_cl;
  573. int id;
  574. if (WARN_ON(!cl || !cl->dev))
  575. return -EINVAL;
  576. dev = cl->dev;
  577. id = mei_me_cl_by_id(dev, cl->me_client_id);
  578. if (id < 0) {
  579. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  580. return id;
  581. }
  582. me_cl = &dev->me_clients[id];
  583. if (me_cl->props.single_recv_buf != 0) {
  584. if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0))
  585. return -EINVAL;
  586. me_cl->mei_flow_ctrl_creds--;
  587. } else {
  588. if (WARN_ON(cl->mei_flow_ctrl_creds <= 0))
  589. return -EINVAL;
  590. cl->mei_flow_ctrl_creds--;
  591. }
  592. return 0;
  593. }
  594. /**
  595. * mei_cl_read_start - the start read client message function.
  596. *
  597. * @cl: host client
  598. *
  599. * returns 0 on success, <0 on failure.
  600. */
  601. int mei_cl_read_start(struct mei_cl *cl, size_t length)
  602. {
  603. struct mei_device *dev;
  604. struct mei_cl_cb *cb;
  605. int rets;
  606. int i;
  607. if (WARN_ON(!cl || !cl->dev))
  608. return -ENODEV;
  609. dev = cl->dev;
  610. if (!mei_cl_is_connected(cl))
  611. return -ENODEV;
  612. if (cl->read_cb) {
  613. cl_dbg(dev, cl, "read is pending.\n");
  614. return -EBUSY;
  615. }
  616. i = mei_me_cl_by_id(dev, cl->me_client_id);
  617. if (i < 0) {
  618. cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
  619. return -ENOTTY;
  620. }
  621. rets = pm_runtime_get(&dev->pdev->dev);
  622. if (rets < 0 && rets != -EINPROGRESS) {
  623. pm_runtime_put_noidle(&dev->pdev->dev);
  624. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  625. return rets;
  626. }
  627. cb = mei_io_cb_init(cl, NULL);
  628. if (!cb) {
  629. rets = -ENOMEM;
  630. goto out;
  631. }
  632. /* always allocate at least client max message */
  633. length = max_t(size_t, length, dev->me_clients[i].props.max_msg_length);
  634. rets = mei_io_cb_alloc_resp_buf(cb, length);
  635. if (rets)
  636. goto out;
  637. cb->fop_type = MEI_FOP_READ;
  638. if (mei_hbuf_acquire(dev)) {
  639. rets = mei_hbm_cl_flow_control_req(dev, cl);
  640. if (rets < 0)
  641. goto out;
  642. list_add_tail(&cb->list, &dev->read_list.list);
  643. } else {
  644. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  645. }
  646. cl->read_cb = cb;
  647. out:
  648. cl_dbg(dev, cl, "rpm: autosuspend\n");
  649. pm_runtime_mark_last_busy(&dev->pdev->dev);
  650. pm_runtime_put_autosuspend(&dev->pdev->dev);
  651. if (rets)
  652. mei_io_cb_free(cb);
  653. return rets;
  654. }
  655. /**
  656. * mei_cl_irq_write - write a message to device
  657. * from the interrupt thread context
  658. *
  659. * @cl: client
  660. * @cb: callback block.
  661. * @cmpl_list: complete list.
  662. *
  663. * returns 0, OK; otherwise error.
  664. */
  665. int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  666. struct mei_cl_cb *cmpl_list)
  667. {
  668. struct mei_device *dev;
  669. struct mei_msg_data *buf;
  670. struct mei_msg_hdr mei_hdr;
  671. size_t len;
  672. u32 msg_slots;
  673. int slots;
  674. int rets;
  675. if (WARN_ON(!cl || !cl->dev))
  676. return -ENODEV;
  677. dev = cl->dev;
  678. buf = &cb->request_buffer;
  679. rets = mei_cl_flow_ctrl_creds(cl);
  680. if (rets < 0)
  681. return rets;
  682. if (rets == 0) {
  683. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  684. return 0;
  685. }
  686. slots = mei_hbuf_empty_slots(dev);
  687. len = buf->size - cb->buf_idx;
  688. msg_slots = mei_data2slots(len);
  689. mei_hdr.host_addr = cl->host_client_id;
  690. mei_hdr.me_addr = cl->me_client_id;
  691. mei_hdr.reserved = 0;
  692. mei_hdr.internal = cb->internal;
  693. if (slots >= msg_slots) {
  694. mei_hdr.length = len;
  695. mei_hdr.msg_complete = 1;
  696. /* Split the message only if we can write the whole host buffer */
  697. } else if (slots == dev->hbuf_depth) {
  698. msg_slots = slots;
  699. len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
  700. mei_hdr.length = len;
  701. mei_hdr.msg_complete = 0;
  702. } else {
  703. /* wait for next time the host buffer is empty */
  704. return 0;
  705. }
  706. cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
  707. cb->request_buffer.size, cb->buf_idx);
  708. rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
  709. if (rets) {
  710. cl->status = rets;
  711. list_move_tail(&cb->list, &cmpl_list->list);
  712. return rets;
  713. }
  714. cl->status = 0;
  715. cl->writing_state = MEI_WRITING;
  716. cb->buf_idx += mei_hdr.length;
  717. if (mei_hdr.msg_complete) {
  718. if (mei_cl_flow_ctrl_reduce(cl))
  719. return -EIO;
  720. list_move_tail(&cb->list, &dev->write_waiting_list.list);
  721. }
  722. return 0;
  723. }
  724. /**
  725. * mei_cl_write - submit a write cb to mei device
  726. assumes device_lock is locked
  727. *
  728. * @cl: host client
  729. * @cl: write callback with filled data
  730. *
  731. * returns number of bytes sent on success, <0 on failure.
  732. */
  733. int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
  734. {
  735. struct mei_device *dev;
  736. struct mei_msg_data *buf;
  737. struct mei_msg_hdr mei_hdr;
  738. int rets;
  739. if (WARN_ON(!cl || !cl->dev))
  740. return -ENODEV;
  741. if (WARN_ON(!cb))
  742. return -EINVAL;
  743. dev = cl->dev;
  744. buf = &cb->request_buffer;
  745. cl_dbg(dev, cl, "mei_cl_write %d\n", buf->size);
  746. rets = pm_runtime_get(&dev->pdev->dev);
  747. if (rets < 0 && rets != -EINPROGRESS) {
  748. pm_runtime_put_noidle(&dev->pdev->dev);
  749. cl_err(dev, cl, "rpm: get failed %d\n", rets);
  750. return rets;
  751. }
  752. cb->fop_type = MEI_FOP_WRITE;
  753. cb->buf_idx = 0;
  754. cl->writing_state = MEI_IDLE;
  755. mei_hdr.host_addr = cl->host_client_id;
  756. mei_hdr.me_addr = cl->me_client_id;
  757. mei_hdr.reserved = 0;
  758. mei_hdr.msg_complete = 0;
  759. mei_hdr.internal = cb->internal;
  760. rets = mei_cl_flow_ctrl_creds(cl);
  761. if (rets < 0)
  762. goto err;
  763. if (rets == 0) {
  764. cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
  765. rets = buf->size;
  766. goto out;
  767. }
  768. if (!mei_hbuf_acquire(dev)) {
  769. cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
  770. rets = buf->size;
  771. goto out;
  772. }
  773. /* Check for a maximum length */
  774. if (buf->size > mei_hbuf_max_len(dev)) {
  775. mei_hdr.length = mei_hbuf_max_len(dev);
  776. mei_hdr.msg_complete = 0;
  777. } else {
  778. mei_hdr.length = buf->size;
  779. mei_hdr.msg_complete = 1;
  780. }
  781. rets = mei_write_message(dev, &mei_hdr, buf->data);
  782. if (rets)
  783. goto err;
  784. cl->writing_state = MEI_WRITING;
  785. cb->buf_idx = mei_hdr.length;
  786. out:
  787. if (mei_hdr.msg_complete) {
  788. rets = mei_cl_flow_ctrl_reduce(cl);
  789. if (rets < 0)
  790. goto err;
  791. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  792. } else {
  793. list_add_tail(&cb->list, &dev->write_list.list);
  794. }
  795. if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
  796. mutex_unlock(&dev->device_lock);
  797. rets = wait_event_interruptible(cl->tx_wait,
  798. cl->writing_state == MEI_WRITE_COMPLETE);
  799. mutex_lock(&dev->device_lock);
  800. /* wait_event_interruptible returns -ERESTARTSYS */
  801. if (rets) {
  802. if (signal_pending(current))
  803. rets = -EINTR;
  804. goto err;
  805. }
  806. }
  807. rets = buf->size;
  808. err:
  809. cl_dbg(dev, cl, "rpm: autosuspend\n");
  810. pm_runtime_mark_last_busy(&dev->pdev->dev);
  811. pm_runtime_put_autosuspend(&dev->pdev->dev);
  812. return rets;
  813. }
  814. /**
  815. * mei_cl_complete - processes completed operation for a client
  816. *
  817. * @cl: private data of the file object.
  818. * @cb: callback block.
  819. */
  820. void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  821. {
  822. if (cb->fop_type == MEI_FOP_WRITE) {
  823. mei_io_cb_free(cb);
  824. cb = NULL;
  825. cl->writing_state = MEI_WRITE_COMPLETE;
  826. if (waitqueue_active(&cl->tx_wait))
  827. wake_up_interruptible(&cl->tx_wait);
  828. } else if (cb->fop_type == MEI_FOP_READ &&
  829. MEI_READING == cl->reading_state) {
  830. cl->reading_state = MEI_READ_COMPLETE;
  831. if (waitqueue_active(&cl->rx_wait))
  832. wake_up_interruptible(&cl->rx_wait);
  833. else
  834. mei_cl_bus_rx_event(cl);
  835. }
  836. }
  837. /**
  838. * mei_cl_all_disconnect - disconnect forcefully all connected clients
  839. *
  840. * @dev - mei device
  841. */
  842. void mei_cl_all_disconnect(struct mei_device *dev)
  843. {
  844. struct mei_cl *cl;
  845. list_for_each_entry(cl, &dev->file_list, link) {
  846. cl->state = MEI_FILE_DISCONNECTED;
  847. cl->mei_flow_ctrl_creds = 0;
  848. cl->timer_count = 0;
  849. }
  850. }
  851. /**
  852. * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
  853. *
  854. * @dev - mei device
  855. */
  856. void mei_cl_all_wakeup(struct mei_device *dev)
  857. {
  858. struct mei_cl *cl;
  859. list_for_each_entry(cl, &dev->file_list, link) {
  860. if (waitqueue_active(&cl->rx_wait)) {
  861. cl_dbg(dev, cl, "Waking up reading client!\n");
  862. wake_up_interruptible(&cl->rx_wait);
  863. }
  864. if (waitqueue_active(&cl->tx_wait)) {
  865. cl_dbg(dev, cl, "Waking up writing client!\n");
  866. wake_up_interruptible(&cl->tx_wait);
  867. }
  868. }
  869. }
  870. /**
  871. * mei_cl_all_write_clear - clear all pending writes
  872. * @dev - mei device
  873. */
  874. void mei_cl_all_write_clear(struct mei_device *dev)
  875. {
  876. mei_io_list_free(&dev->write_list, NULL);
  877. mei_io_list_free(&dev->write_waiting_list, NULL);
  878. }