operation.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Greybus operations
  4. *
  5. * Copyright 2014-2015 Google Inc.
  6. * Copyright 2014-2015 Linaro Ltd.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/greybus.h>
  15. #include "greybus_trace.h"
  16. static struct kmem_cache *gb_operation_cache;
  17. static struct kmem_cache *gb_message_cache;
  18. /* Workqueue to handle Greybus operation completions. */
  19. static struct workqueue_struct *gb_operation_completion_wq;
  20. /* Wait queue for synchronous cancellations. */
  21. static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue);
  22. /*
  23. * Protects updates to operation->errno.
  24. */
  25. static DEFINE_SPINLOCK(gb_operations_lock);
  26. static int gb_operation_response_send(struct gb_operation *operation,
  27. int errno);
  28. /*
  29. * Increment operation active count and add to connection list unless the
  30. * connection is going away.
  31. *
  32. * Caller holds operation reference.
  33. */
  34. static int gb_operation_get_active(struct gb_operation *operation)
  35. {
  36. struct gb_connection *connection = operation->connection;
  37. unsigned long flags;
  38. spin_lock_irqsave(&connection->lock, flags);
  39. switch (connection->state) {
  40. case GB_CONNECTION_STATE_ENABLED:
  41. break;
  42. case GB_CONNECTION_STATE_ENABLED_TX:
  43. if (gb_operation_is_incoming(operation))
  44. goto err_unlock;
  45. break;
  46. case GB_CONNECTION_STATE_DISCONNECTING:
  47. if (!gb_operation_is_core(operation))
  48. goto err_unlock;
  49. break;
  50. default:
  51. goto err_unlock;
  52. }
  53. if (operation->active++ == 0)
  54. list_add_tail(&operation->links, &connection->operations);
  55. trace_gb_operation_get_active(operation);
  56. spin_unlock_irqrestore(&connection->lock, flags);
  57. return 0;
  58. err_unlock:
  59. spin_unlock_irqrestore(&connection->lock, flags);
  60. return -ENOTCONN;
  61. }
  62. /* Caller holds operation reference. */
  63. static void gb_operation_put_active(struct gb_operation *operation)
  64. {
  65. struct gb_connection *connection = operation->connection;
  66. unsigned long flags;
  67. spin_lock_irqsave(&connection->lock, flags);
  68. trace_gb_operation_put_active(operation);
  69. if (--operation->active == 0) {
  70. list_del(&operation->links);
  71. if (atomic_read(&operation->waiters))
  72. wake_up(&gb_operation_cancellation_queue);
  73. }
  74. spin_unlock_irqrestore(&connection->lock, flags);
  75. }
  76. static bool gb_operation_is_active(struct gb_operation *operation)
  77. {
  78. struct gb_connection *connection = operation->connection;
  79. unsigned long flags;
  80. bool ret;
  81. spin_lock_irqsave(&connection->lock, flags);
  82. ret = operation->active;
  83. spin_unlock_irqrestore(&connection->lock, flags);
  84. return ret;
  85. }
  86. /*
  87. * Set an operation's result.
  88. *
  89. * Initially an outgoing operation's errno value is -EBADR.
  90. * If no error occurs before sending the request message the only
  91. * valid value operation->errno can be set to is -EINPROGRESS,
  92. * indicating the request has been (or rather is about to be) sent.
  93. * At that point nobody should be looking at the result until the
  94. * response arrives.
  95. *
  96. * The first time the result gets set after the request has been
  97. * sent, that result "sticks." That is, if two concurrent threads
  98. * race to set the result, the first one wins. The return value
  99. * tells the caller whether its result was recorded; if not the
  100. * caller has nothing more to do.
  101. *
  102. * The result value -EILSEQ is reserved to signal an implementation
  103. * error; if it's ever observed, the code performing the request has
  104. * done something fundamentally wrong. It is an error to try to set
  105. * the result to -EBADR, and attempts to do so result in a warning,
  106. * and -EILSEQ is used instead. Similarly, the only valid result
  107. * value to set for an operation in initial state is -EINPROGRESS.
  108. * Attempts to do otherwise will also record a (successful) -EILSEQ
  109. * operation result.
  110. */
  111. static bool gb_operation_result_set(struct gb_operation *operation, int result)
  112. {
  113. unsigned long flags;
  114. int prev;
  115. if (result == -EINPROGRESS) {
  116. /*
  117. * -EINPROGRESS is used to indicate the request is
  118. * in flight. It should be the first result value
  119. * set after the initial -EBADR. Issue a warning
  120. * and record an implementation error if it's
  121. * set at any other time.
  122. */
  123. spin_lock_irqsave(&gb_operations_lock, flags);
  124. prev = operation->errno;
  125. if (prev == -EBADR)
  126. operation->errno = result;
  127. else
  128. operation->errno = -EILSEQ;
  129. spin_unlock_irqrestore(&gb_operations_lock, flags);
  130. WARN_ON(prev != -EBADR);
  131. return true;
  132. }
  133. /*
  134. * The first result value set after a request has been sent
  135. * will be the final result of the operation. Subsequent
  136. * attempts to set the result are ignored.
  137. *
  138. * Note that -EBADR is a reserved "initial state" result
  139. * value. Attempts to set this value result in a warning,
  140. * and the result code is set to -EILSEQ instead.
  141. */
  142. if (WARN_ON(result == -EBADR))
  143. result = -EILSEQ; /* Nobody should be setting -EBADR */
  144. spin_lock_irqsave(&gb_operations_lock, flags);
  145. prev = operation->errno;
  146. if (prev == -EINPROGRESS)
  147. operation->errno = result; /* First and final result */
  148. spin_unlock_irqrestore(&gb_operations_lock, flags);
  149. return prev == -EINPROGRESS;
  150. }
  151. int gb_operation_result(struct gb_operation *operation)
  152. {
  153. int result = operation->errno;
  154. WARN_ON(result == -EBADR);
  155. WARN_ON(result == -EINPROGRESS);
  156. return result;
  157. }
  158. EXPORT_SYMBOL_GPL(gb_operation_result);
  159. /*
  160. * Looks up an outgoing operation on a connection and returns a refcounted
  161. * pointer if found, or NULL otherwise.
  162. */
  163. static struct gb_operation *
  164. gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
  165. {
  166. struct gb_operation *operation;
  167. unsigned long flags;
  168. bool found = false;
  169. spin_lock_irqsave(&connection->lock, flags);
  170. list_for_each_entry(operation, &connection->operations, links)
  171. if (operation->id == operation_id &&
  172. !gb_operation_is_incoming(operation)) {
  173. gb_operation_get(operation);
  174. found = true;
  175. break;
  176. }
  177. spin_unlock_irqrestore(&connection->lock, flags);
  178. return found ? operation : NULL;
  179. }
  180. static int gb_message_send(struct gb_message *message, gfp_t gfp)
  181. {
  182. struct gb_connection *connection = message->operation->connection;
  183. trace_gb_message_send(message);
  184. return connection->hd->driver->message_send(connection->hd,
  185. connection->hd_cport_id,
  186. message,
  187. gfp);
  188. }
  189. /*
  190. * Cancel a message we have passed to the host device layer to be sent.
  191. */
  192. static void gb_message_cancel(struct gb_message *message)
  193. {
  194. struct gb_host_device *hd = message->operation->connection->hd;
  195. hd->driver->message_cancel(message);
  196. }
  197. static void gb_operation_request_handle(struct gb_operation *operation)
  198. {
  199. struct gb_connection *connection = operation->connection;
  200. int status;
  201. int ret;
  202. if (connection->handler) {
  203. status = connection->handler(operation);
  204. } else {
  205. dev_err(&connection->hd->dev,
  206. "%s: unexpected incoming request of type 0x%02x\n",
  207. connection->name, operation->type);
  208. status = -EPROTONOSUPPORT;
  209. }
  210. ret = gb_operation_response_send(operation, status);
  211. if (ret) {
  212. dev_err(&connection->hd->dev,
  213. "%s: failed to send response %d for type 0x%02x: %d\n",
  214. connection->name, status, operation->type, ret);
  215. return;
  216. }
  217. }
  218. /*
  219. * Process operation work.
  220. *
  221. * For incoming requests, call the protocol request handler. The operation
  222. * result should be -EINPROGRESS at this point.
  223. *
  224. * For outgoing requests, the operation result value should have
  225. * been set before queueing this. The operation callback function
  226. * allows the original requester to know the request has completed
  227. * and its result is available.
  228. */
  229. static void gb_operation_work(struct work_struct *work)
  230. {
  231. struct gb_operation *operation;
  232. int ret;
  233. operation = container_of(work, struct gb_operation, work);
  234. if (gb_operation_is_incoming(operation)) {
  235. gb_operation_request_handle(operation);
  236. } else {
  237. ret = timer_delete_sync(&operation->timer);
  238. if (!ret) {
  239. /* Cancel request message if scheduled by timeout. */
  240. if (gb_operation_result(operation) == -ETIMEDOUT)
  241. gb_message_cancel(operation->request);
  242. }
  243. operation->callback(operation);
  244. }
  245. gb_operation_put_active(operation);
  246. gb_operation_put(operation);
  247. }
  248. static void gb_operation_timeout(struct timer_list *t)
  249. {
  250. struct gb_operation *operation = timer_container_of(operation, t,
  251. timer);
  252. if (gb_operation_result_set(operation, -ETIMEDOUT)) {
  253. /*
  254. * A stuck request message will be cancelled from the
  255. * workqueue.
  256. */
  257. queue_work(gb_operation_completion_wq, &operation->work);
  258. }
  259. }
  260. static void gb_operation_message_init(struct gb_host_device *hd,
  261. struct gb_message *message,
  262. u16 operation_id,
  263. size_t payload_size, u8 type)
  264. {
  265. struct gb_operation_msg_hdr *header;
  266. header = message->buffer;
  267. message->header = header;
  268. message->payload = payload_size ? header + 1 : NULL;
  269. message->payload_size = payload_size;
  270. /*
  271. * The type supplied for incoming message buffers will be
  272. * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
  273. * arriving data so there's no need to initialize the message header.
  274. */
  275. if (type != GB_REQUEST_TYPE_INVALID) {
  276. u16 message_size = (u16)(sizeof(*header) + payload_size);
  277. /*
  278. * For a request, the operation id gets filled in
  279. * when the message is sent. For a response, it
  280. * will be copied from the request by the caller.
  281. *
  282. * The result field in a request message must be
  283. * zero. It will be set just prior to sending for
  284. * a response.
  285. */
  286. header->size = cpu_to_le16(message_size);
  287. header->operation_id = 0;
  288. header->type = type;
  289. header->result = 0;
  290. }
  291. }
  292. /*
  293. * Allocate a message to be used for an operation request or response.
  294. * Both types of message contain a common header. The request message
  295. * for an outgoing operation is outbound, as is the response message
  296. * for an incoming operation. The message header for an outbound
  297. * message is partially initialized here.
  298. *
  299. * The headers for inbound messages don't need to be initialized;
  300. * they'll be filled in by arriving data.
  301. *
  302. * Our message buffers have the following layout:
  303. * message header \_ these combined are
  304. * message payload / the message size
  305. */
  306. static struct gb_message *
  307. gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
  308. size_t payload_size, gfp_t gfp_flags)
  309. {
  310. struct gb_message *message;
  311. struct gb_operation_msg_hdr *header;
  312. size_t message_size = payload_size + sizeof(*header);
  313. if (message_size > hd->buffer_size_max) {
  314. dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
  315. message_size, hd->buffer_size_max);
  316. return NULL;
  317. }
  318. /* Allocate the message structure and buffer. */
  319. message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
  320. if (!message)
  321. return NULL;
  322. message->buffer = kzalloc(message_size, gfp_flags);
  323. if (!message->buffer)
  324. goto err_free_message;
  325. /* Initialize the message. Operation id is filled in later. */
  326. gb_operation_message_init(hd, message, 0, payload_size, type);
  327. return message;
  328. err_free_message:
  329. kmem_cache_free(gb_message_cache, message);
  330. return NULL;
  331. }
  332. static void gb_operation_message_free(struct gb_message *message)
  333. {
  334. kfree(message->buffer);
  335. kmem_cache_free(gb_message_cache, message);
  336. }
  337. /*
  338. * Map an enum gb_operation_status value (which is represented in a
  339. * message as a single byte) to an appropriate Linux negative errno.
  340. */
  341. static int gb_operation_status_map(u8 status)
  342. {
  343. switch (status) {
  344. case GB_OP_SUCCESS:
  345. return 0;
  346. case GB_OP_INTERRUPTED:
  347. return -EINTR;
  348. case GB_OP_TIMEOUT:
  349. return -ETIMEDOUT;
  350. case GB_OP_NO_MEMORY:
  351. return -ENOMEM;
  352. case GB_OP_PROTOCOL_BAD:
  353. return -EPROTONOSUPPORT;
  354. case GB_OP_OVERFLOW:
  355. return -EMSGSIZE;
  356. case GB_OP_INVALID:
  357. return -EINVAL;
  358. case GB_OP_RETRY:
  359. return -EAGAIN;
  360. case GB_OP_NONEXISTENT:
  361. return -ENODEV;
  362. case GB_OP_MALFUNCTION:
  363. return -EILSEQ;
  364. case GB_OP_UNKNOWN_ERROR:
  365. default:
  366. return -EIO;
  367. }
  368. }
  369. /*
  370. * Map a Linux errno value (from operation->errno) into the value
  371. * that should represent it in a response message status sent
  372. * over the wire. Returns an enum gb_operation_status value (which
  373. * is represented in a message as a single byte).
  374. */
  375. static u8 gb_operation_errno_map(int errno)
  376. {
  377. switch (errno) {
  378. case 0:
  379. return GB_OP_SUCCESS;
  380. case -EINTR:
  381. return GB_OP_INTERRUPTED;
  382. case -ETIMEDOUT:
  383. return GB_OP_TIMEOUT;
  384. case -ENOMEM:
  385. return GB_OP_NO_MEMORY;
  386. case -EPROTONOSUPPORT:
  387. return GB_OP_PROTOCOL_BAD;
  388. case -EMSGSIZE:
  389. return GB_OP_OVERFLOW; /* Could be underflow too */
  390. case -EINVAL:
  391. return GB_OP_INVALID;
  392. case -EAGAIN:
  393. return GB_OP_RETRY;
  394. case -EILSEQ:
  395. return GB_OP_MALFUNCTION;
  396. case -ENODEV:
  397. return GB_OP_NONEXISTENT;
  398. case -EIO:
  399. default:
  400. return GB_OP_UNKNOWN_ERROR;
  401. }
  402. }
  403. bool gb_operation_response_alloc(struct gb_operation *operation,
  404. size_t response_size, gfp_t gfp)
  405. {
  406. struct gb_host_device *hd = operation->connection->hd;
  407. struct gb_operation_msg_hdr *request_header;
  408. struct gb_message *response;
  409. u8 type;
  410. type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
  411. response = gb_operation_message_alloc(hd, type, response_size, gfp);
  412. if (!response)
  413. return false;
  414. response->operation = operation;
  415. /*
  416. * Size and type get initialized when the message is
  417. * allocated. The errno will be set before sending. All
  418. * that's left is the operation id, which we copy from the
  419. * request message header (as-is, in little-endian order).
  420. */
  421. request_header = operation->request->header;
  422. response->header->operation_id = request_header->operation_id;
  423. operation->response = response;
  424. return true;
  425. }
  426. EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
  427. /*
  428. * Create a Greybus operation to be sent over the given connection.
  429. * The request buffer will be big enough for a payload of the given
  430. * size.
  431. *
  432. * For outgoing requests, the request message's header will be
  433. * initialized with the type of the request and the message size.
  434. * Outgoing operations must also specify the response buffer size,
  435. * which must be sufficient to hold all expected response data. The
  436. * response message header will eventually be overwritten, so there's
  437. * no need to initialize it here.
  438. *
  439. * Request messages for incoming operations can arrive in interrupt
  440. * context, so they must be allocated with GFP_ATOMIC. In this case
  441. * the request buffer will be immediately overwritten, so there is
  442. * no need to initialize the message header. Responsibility for
  443. * allocating a response buffer lies with the incoming request
  444. * handler for a protocol. So we don't allocate that here.
  445. *
  446. * Returns a pointer to the new operation or a null pointer if an
  447. * error occurs.
  448. */
  449. static struct gb_operation *
  450. gb_operation_create_common(struct gb_connection *connection, u8 type,
  451. size_t request_size, size_t response_size,
  452. unsigned long op_flags, gfp_t gfp_flags)
  453. {
  454. struct gb_host_device *hd = connection->hd;
  455. struct gb_operation *operation;
  456. operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
  457. if (!operation)
  458. return NULL;
  459. operation->connection = connection;
  460. operation->request = gb_operation_message_alloc(hd, type, request_size,
  461. gfp_flags);
  462. if (!operation->request)
  463. goto err_cache;
  464. operation->request->operation = operation;
  465. /* Allocate the response buffer for outgoing operations */
  466. if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
  467. if (!gb_operation_response_alloc(operation, response_size,
  468. gfp_flags)) {
  469. goto err_request;
  470. }
  471. timer_setup(&operation->timer, gb_operation_timeout, 0);
  472. }
  473. operation->flags = op_flags;
  474. operation->type = type;
  475. operation->errno = -EBADR; /* Initial value--means "never set" */
  476. INIT_WORK(&operation->work, gb_operation_work);
  477. init_completion(&operation->completion);
  478. kref_init(&operation->kref);
  479. atomic_set(&operation->waiters, 0);
  480. return operation;
  481. err_request:
  482. gb_operation_message_free(operation->request);
  483. err_cache:
  484. kmem_cache_free(gb_operation_cache, operation);
  485. return NULL;
  486. }
  487. /*
  488. * Create a new operation associated with the given connection. The
  489. * request and response sizes provided are the number of bytes
  490. * required to hold the request/response payload only. Both of
  491. * these are allowed to be 0. Note that 0x00 is reserved as an
  492. * invalid operation type for all protocols, and this is enforced
  493. * here.
  494. */
  495. struct gb_operation *
  496. gb_operation_create_flags(struct gb_connection *connection,
  497. u8 type, size_t request_size,
  498. size_t response_size, unsigned long flags,
  499. gfp_t gfp)
  500. {
  501. struct gb_operation *operation;
  502. if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
  503. return NULL;
  504. if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
  505. type &= ~GB_MESSAGE_TYPE_RESPONSE;
  506. if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
  507. flags &= GB_OPERATION_FLAG_USER_MASK;
  508. operation = gb_operation_create_common(connection, type,
  509. request_size, response_size,
  510. flags, gfp);
  511. if (operation)
  512. trace_gb_operation_create(operation);
  513. return operation;
  514. }
  515. EXPORT_SYMBOL_GPL(gb_operation_create_flags);
  516. struct gb_operation *
  517. gb_operation_create_core(struct gb_connection *connection,
  518. u8 type, size_t request_size,
  519. size_t response_size, unsigned long flags,
  520. gfp_t gfp)
  521. {
  522. struct gb_operation *operation;
  523. flags |= GB_OPERATION_FLAG_CORE;
  524. operation = gb_operation_create_common(connection, type,
  525. request_size, response_size,
  526. flags, gfp);
  527. if (operation)
  528. trace_gb_operation_create_core(operation);
  529. return operation;
  530. }
  531. /* Do not export this function. */
  532. size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
  533. {
  534. struct gb_host_device *hd = connection->hd;
  535. return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
  536. }
  537. EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
  538. static struct gb_operation *
  539. gb_operation_create_incoming(struct gb_connection *connection, u16 id,
  540. u8 type, void *data, size_t size)
  541. {
  542. struct gb_operation *operation;
  543. size_t request_size;
  544. unsigned long flags = GB_OPERATION_FLAG_INCOMING;
  545. /* Caller has made sure we at least have a message header. */
  546. request_size = size - sizeof(struct gb_operation_msg_hdr);
  547. if (!id)
  548. flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
  549. operation = gb_operation_create_common(connection, type,
  550. request_size,
  551. GB_REQUEST_TYPE_INVALID,
  552. flags, GFP_ATOMIC);
  553. if (!operation)
  554. return NULL;
  555. operation->id = id;
  556. memcpy(operation->request->header, data, size);
  557. trace_gb_operation_create_incoming(operation);
  558. return operation;
  559. }
  560. /*
  561. * Get an additional reference on an operation.
  562. */
  563. void gb_operation_get(struct gb_operation *operation)
  564. {
  565. kref_get(&operation->kref);
  566. }
  567. EXPORT_SYMBOL_GPL(gb_operation_get);
  568. /*
  569. * Destroy a previously created operation.
  570. */
  571. static void _gb_operation_destroy(struct kref *kref)
  572. {
  573. struct gb_operation *operation;
  574. operation = container_of(kref, struct gb_operation, kref);
  575. trace_gb_operation_destroy(operation);
  576. if (operation->response)
  577. gb_operation_message_free(operation->response);
  578. gb_operation_message_free(operation->request);
  579. kmem_cache_free(gb_operation_cache, operation);
  580. }
  581. /*
  582. * Drop a reference on an operation, and destroy it when the last
  583. * one is gone.
  584. */
  585. void gb_operation_put(struct gb_operation *operation)
  586. {
  587. if (WARN_ON(!operation))
  588. return;
  589. kref_put(&operation->kref, _gb_operation_destroy);
  590. }
  591. EXPORT_SYMBOL_GPL(gb_operation_put);
  592. /* Tell the requester we're done */
  593. static void gb_operation_sync_callback(struct gb_operation *operation)
  594. {
  595. complete(&operation->completion);
  596. }
  597. /**
  598. * gb_operation_request_send() - send an operation request message
  599. * @operation: the operation to initiate
  600. * @callback: the operation completion callback
  601. * @timeout: operation timeout in milliseconds, or zero for no timeout
  602. * @gfp: the memory flags to use for any allocations
  603. *
  604. * The caller has filled in any payload so the request message is ready to go.
  605. * The callback function supplied will be called when the response message has
  606. * arrived, a unidirectional request has been sent, or the operation is
  607. * cancelled, indicating that the operation is complete. The callback function
  608. * can fetch the result of the operation using gb_operation_result() if
  609. * desired.
  610. *
  611. * Return: 0 if the request was successfully queued in the host-driver queues,
  612. * or a negative errno.
  613. */
  614. int gb_operation_request_send(struct gb_operation *operation,
  615. gb_operation_callback callback,
  616. unsigned int timeout,
  617. gfp_t gfp)
  618. {
  619. struct gb_connection *connection = operation->connection;
  620. struct gb_operation_msg_hdr *header;
  621. unsigned int cycle;
  622. int ret;
  623. if (gb_connection_is_offloaded(connection))
  624. return -EBUSY;
  625. if (!callback)
  626. return -EINVAL;
  627. /*
  628. * Record the callback function, which is executed in
  629. * non-atomic (workqueue) context when the final result
  630. * of an operation has been set.
  631. */
  632. operation->callback = callback;
  633. /*
  634. * Assign the operation's id, and store it in the request header.
  635. * Zero is a reserved operation id for unidirectional operations.
  636. */
  637. if (gb_operation_is_unidirectional(operation)) {
  638. operation->id = 0;
  639. } else {
  640. cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
  641. operation->id = (u16)(cycle % U16_MAX + 1);
  642. }
  643. header = operation->request->header;
  644. header->operation_id = cpu_to_le16(operation->id);
  645. gb_operation_result_set(operation, -EINPROGRESS);
  646. /*
  647. * Get an extra reference on the operation. It'll be dropped when the
  648. * operation completes.
  649. */
  650. gb_operation_get(operation);
  651. ret = gb_operation_get_active(operation);
  652. if (ret)
  653. goto err_put;
  654. ret = gb_message_send(operation->request, gfp);
  655. if (ret)
  656. goto err_put_active;
  657. if (timeout) {
  658. operation->timer.expires = jiffies + msecs_to_jiffies(timeout);
  659. add_timer(&operation->timer);
  660. }
  661. return 0;
  662. err_put_active:
  663. gb_operation_put_active(operation);
  664. err_put:
  665. gb_operation_put(operation);
  666. return ret;
  667. }
  668. EXPORT_SYMBOL_GPL(gb_operation_request_send);
  669. /*
  670. * Send a synchronous operation. This function is expected to
  671. * block, returning only when the response has arrived, (or when an
  672. * error is detected. The return value is the result of the
  673. * operation.
  674. */
  675. int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
  676. unsigned int timeout)
  677. {
  678. int ret;
  679. ret = gb_operation_request_send(operation, gb_operation_sync_callback,
  680. timeout, GFP_KERNEL);
  681. if (ret)
  682. return ret;
  683. ret = wait_for_completion_interruptible(&operation->completion);
  684. if (ret < 0) {
  685. /* Cancel the operation if interrupted */
  686. gb_operation_cancel(operation, -ECANCELED);
  687. }
  688. return gb_operation_result(operation);
  689. }
  690. EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
  691. /*
  692. * Send a response for an incoming operation request. A non-zero
  693. * errno indicates a failed operation.
  694. *
  695. * If there is any response payload, the incoming request handler is
  696. * responsible for allocating the response message. Otherwise the
  697. * it can simply supply the result errno; this function will
  698. * allocate the response message if necessary.
  699. */
  700. static int gb_operation_response_send(struct gb_operation *operation,
  701. int errno)
  702. {
  703. struct gb_connection *connection = operation->connection;
  704. int ret;
  705. if (!operation->response &&
  706. !gb_operation_is_unidirectional(operation)) {
  707. if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
  708. return -ENOMEM;
  709. }
  710. /* Record the result */
  711. if (!gb_operation_result_set(operation, errno)) {
  712. dev_err(&connection->hd->dev, "request result already set\n");
  713. return -EIO; /* Shouldn't happen */
  714. }
  715. /* Sender of request does not care about response. */
  716. if (gb_operation_is_unidirectional(operation))
  717. return 0;
  718. /* Reference will be dropped when message has been sent. */
  719. gb_operation_get(operation);
  720. ret = gb_operation_get_active(operation);
  721. if (ret)
  722. goto err_put;
  723. /* Fill in the response header and send it */
  724. operation->response->header->result = gb_operation_errno_map(errno);
  725. ret = gb_message_send(operation->response, GFP_KERNEL);
  726. if (ret)
  727. goto err_put_active;
  728. return 0;
  729. err_put_active:
  730. gb_operation_put_active(operation);
  731. err_put:
  732. gb_operation_put(operation);
  733. return ret;
  734. }
  735. /*
  736. * This function is called when a message send request has completed.
  737. */
  738. void greybus_message_sent(struct gb_host_device *hd,
  739. struct gb_message *message, int status)
  740. {
  741. struct gb_operation *operation = message->operation;
  742. struct gb_connection *connection = operation->connection;
  743. /*
  744. * If the message was a response, we just need to drop our
  745. * reference to the operation. If an error occurred, report
  746. * it.
  747. *
  748. * For requests, if there's no error and the operation in not
  749. * unidirectional, there's nothing more to do until the response
  750. * arrives. If an error occurred attempting to send it, or if the
  751. * operation is unidrectional, record the result of the operation and
  752. * schedule its completion.
  753. */
  754. if (message == operation->response) {
  755. if (status) {
  756. dev_err(&connection->hd->dev,
  757. "%s: error sending response 0x%02x: %d\n",
  758. connection->name, operation->type, status);
  759. }
  760. gb_operation_put_active(operation);
  761. gb_operation_put(operation);
  762. } else if (status || gb_operation_is_unidirectional(operation)) {
  763. if (gb_operation_result_set(operation, status)) {
  764. queue_work(gb_operation_completion_wq,
  765. &operation->work);
  766. }
  767. }
  768. }
  769. EXPORT_SYMBOL_GPL(greybus_message_sent);
  770. /*
  771. * We've received data on a connection, and it doesn't look like a
  772. * response, so we assume it's a request.
  773. *
  774. * This is called in interrupt context, so just copy the incoming
  775. * data into the request buffer and handle the rest via workqueue.
  776. */
  777. static void gb_connection_recv_request(struct gb_connection *connection,
  778. const struct gb_operation_msg_hdr *header,
  779. void *data, size_t size)
  780. {
  781. struct gb_operation *operation;
  782. u16 operation_id;
  783. u8 type;
  784. int ret;
  785. operation_id = le16_to_cpu(header->operation_id);
  786. type = header->type;
  787. operation = gb_operation_create_incoming(connection, operation_id,
  788. type, data, size);
  789. if (!operation) {
  790. dev_err(&connection->hd->dev,
  791. "%s: can't create incoming operation\n",
  792. connection->name);
  793. return;
  794. }
  795. ret = gb_operation_get_active(operation);
  796. if (ret) {
  797. gb_operation_put(operation);
  798. return;
  799. }
  800. trace_gb_message_recv_request(operation->request);
  801. /*
  802. * The initial reference to the operation will be dropped when the
  803. * request handler returns.
  804. */
  805. if (gb_operation_result_set(operation, -EINPROGRESS))
  806. queue_work(connection->wq, &operation->work);
  807. }
  808. /*
  809. * We've received data that appears to be an operation response
  810. * message. Look up the operation, and record that we've received
  811. * its response.
  812. *
  813. * This is called in interrupt context, so just copy the incoming
  814. * data into the response buffer and handle the rest via workqueue.
  815. */
  816. static void gb_connection_recv_response(struct gb_connection *connection,
  817. const struct gb_operation_msg_hdr *header,
  818. void *data, size_t size)
  819. {
  820. struct gb_operation *operation;
  821. struct gb_message *message;
  822. size_t message_size;
  823. u16 operation_id;
  824. int errno;
  825. operation_id = le16_to_cpu(header->operation_id);
  826. if (!operation_id) {
  827. dev_err_ratelimited(&connection->hd->dev,
  828. "%s: invalid response id 0 received\n",
  829. connection->name);
  830. return;
  831. }
  832. operation = gb_operation_find_outgoing(connection, operation_id);
  833. if (!operation) {
  834. dev_err_ratelimited(&connection->hd->dev,
  835. "%s: unexpected response id 0x%04x received\n",
  836. connection->name, operation_id);
  837. return;
  838. }
  839. errno = gb_operation_status_map(header->result);
  840. message = operation->response;
  841. message_size = sizeof(*header) + message->payload_size;
  842. if (!errno && size > message_size) {
  843. dev_err_ratelimited(&connection->hd->dev,
  844. "%s: malformed response 0x%02x received (%zu > %zu)\n",
  845. connection->name, header->type,
  846. size, message_size);
  847. errno = -EMSGSIZE;
  848. } else if (!errno && size < message_size) {
  849. if (gb_operation_short_response_allowed(operation)) {
  850. message->payload_size = size - sizeof(*header);
  851. } else {
  852. dev_err_ratelimited(&connection->hd->dev,
  853. "%s: short response 0x%02x received (%zu < %zu)\n",
  854. connection->name, header->type,
  855. size, message_size);
  856. errno = -EMSGSIZE;
  857. }
  858. }
  859. /* We must ignore the payload if a bad status is returned */
  860. if (errno)
  861. size = sizeof(*header);
  862. /* The rest will be handled in work queue context */
  863. if (gb_operation_result_set(operation, errno)) {
  864. memcpy(message->buffer, data, size);
  865. trace_gb_message_recv_response(message);
  866. queue_work(gb_operation_completion_wq, &operation->work);
  867. }
  868. gb_operation_put(operation);
  869. }
  870. /*
  871. * Handle data arriving on a connection. As soon as we return the
  872. * supplied data buffer will be reused (so unless we do something
  873. * with, it's effectively dropped).
  874. */
  875. void gb_connection_recv(struct gb_connection *connection,
  876. void *data, size_t size)
  877. {
  878. struct gb_operation_msg_hdr header;
  879. struct device *dev = &connection->hd->dev;
  880. size_t msg_size;
  881. if (connection->state == GB_CONNECTION_STATE_DISABLED ||
  882. gb_connection_is_offloaded(connection)) {
  883. dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
  884. connection->name, size);
  885. return;
  886. }
  887. if (size < sizeof(header)) {
  888. dev_err_ratelimited(dev, "%s: short message received\n",
  889. connection->name);
  890. return;
  891. }
  892. /* Use memcpy as data may be unaligned */
  893. memcpy(&header, data, sizeof(header));
  894. msg_size = le16_to_cpu(header.size);
  895. if (size < msg_size) {
  896. dev_err_ratelimited(dev,
  897. "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
  898. connection->name,
  899. le16_to_cpu(header.operation_id),
  900. header.type, size, msg_size);
  901. return; /* XXX Should still complete operation */
  902. }
  903. if (header.type & GB_MESSAGE_TYPE_RESPONSE) {
  904. gb_connection_recv_response(connection, &header, data,
  905. msg_size);
  906. } else {
  907. gb_connection_recv_request(connection, &header, data,
  908. msg_size);
  909. }
  910. }
  911. /*
  912. * Cancel an outgoing operation synchronously, and record the given error to
  913. * indicate why.
  914. */
  915. void gb_operation_cancel(struct gb_operation *operation, int errno)
  916. {
  917. if (WARN_ON(gb_operation_is_incoming(operation)))
  918. return;
  919. if (gb_operation_result_set(operation, errno)) {
  920. gb_message_cancel(operation->request);
  921. queue_work(gb_operation_completion_wq, &operation->work);
  922. }
  923. trace_gb_message_cancel_outgoing(operation->request);
  924. atomic_inc(&operation->waiters);
  925. wait_event(gb_operation_cancellation_queue,
  926. !gb_operation_is_active(operation));
  927. atomic_dec(&operation->waiters);
  928. }
  929. EXPORT_SYMBOL_GPL(gb_operation_cancel);
  930. /*
  931. * Cancel an incoming operation synchronously. Called during connection tear
  932. * down.
  933. */
  934. void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
  935. {
  936. if (WARN_ON(!gb_operation_is_incoming(operation)))
  937. return;
  938. if (!gb_operation_is_unidirectional(operation)) {
  939. /*
  940. * Make sure the request handler has submitted the response
  941. * before cancelling it.
  942. */
  943. flush_work(&operation->work);
  944. if (!gb_operation_result_set(operation, errno))
  945. gb_message_cancel(operation->response);
  946. }
  947. trace_gb_message_cancel_incoming(operation->response);
  948. atomic_inc(&operation->waiters);
  949. wait_event(gb_operation_cancellation_queue,
  950. !gb_operation_is_active(operation));
  951. atomic_dec(&operation->waiters);
  952. }
  953. /**
  954. * gb_operation_sync_timeout() - implement a "simple" synchronous operation
  955. * @connection: the Greybus connection to send this to
  956. * @type: the type of operation to send
  957. * @request: pointer to a memory buffer to copy the request from
  958. * @request_size: size of @request
  959. * @response: pointer to a memory buffer to copy the response to
  960. * @response_size: the size of @response.
  961. * @timeout: operation timeout in milliseconds
  962. *
  963. * This function implements a simple synchronous Greybus operation. It sends
  964. * the provided operation request and waits (sleeps) until the corresponding
  965. * operation response message has been successfully received, or an error
  966. * occurs. @request and @response are buffers to hold the request and response
  967. * data respectively, and if they are not NULL, their size must be specified in
  968. * @request_size and @response_size.
  969. *
  970. * If a response payload is to come back, and @response is not NULL,
  971. * @response_size number of bytes will be copied into @response if the operation
  972. * is successful.
  973. *
  974. * If there is an error, the response buffer is left alone.
  975. */
  976. int gb_operation_sync_timeout(struct gb_connection *connection, int type,
  977. void *request, int request_size,
  978. void *response, int response_size,
  979. unsigned int timeout)
  980. {
  981. struct gb_operation *operation;
  982. int ret;
  983. if ((response_size && !response) ||
  984. (request_size && !request))
  985. return -EINVAL;
  986. operation = gb_operation_create(connection, type,
  987. request_size, response_size,
  988. GFP_KERNEL);
  989. if (!operation)
  990. return -ENOMEM;
  991. if (request_size)
  992. memcpy(operation->request->payload, request, request_size);
  993. ret = gb_operation_request_send_sync_timeout(operation, timeout);
  994. if (ret) {
  995. dev_err(&connection->hd->dev,
  996. "%s: synchronous operation id 0x%04x of type 0x%02x failed: %d\n",
  997. connection->name, operation->id, type, ret);
  998. } else {
  999. if (response_size) {
  1000. memcpy(response, operation->response->payload,
  1001. response_size);
  1002. }
  1003. }
  1004. gb_operation_put(operation);
  1005. return ret;
  1006. }
  1007. EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
  1008. /**
  1009. * gb_operation_unidirectional_timeout() - initiate a unidirectional operation
  1010. * @connection: connection to use
  1011. * @type: type of operation to send
  1012. * @request: memory buffer to copy the request from
  1013. * @request_size: size of @request
  1014. * @timeout: send timeout in milliseconds
  1015. *
  1016. * Initiate a unidirectional operation by sending a request message and
  1017. * waiting for it to be acknowledged as sent by the host device.
  1018. *
  1019. * Note that successful send of a unidirectional operation does not imply that
  1020. * the request as actually reached the remote end of the connection.
  1021. */
  1022. int gb_operation_unidirectional_timeout(struct gb_connection *connection,
  1023. int type, void *request,
  1024. int request_size,
  1025. unsigned int timeout)
  1026. {
  1027. struct gb_operation *operation;
  1028. int ret;
  1029. if (request_size && !request)
  1030. return -EINVAL;
  1031. operation = gb_operation_create_flags(connection, type,
  1032. request_size, 0,
  1033. GB_OPERATION_FLAG_UNIDIRECTIONAL,
  1034. GFP_KERNEL);
  1035. if (!operation)
  1036. return -ENOMEM;
  1037. if (request_size)
  1038. memcpy(operation->request->payload, request, request_size);
  1039. ret = gb_operation_request_send_sync_timeout(operation, timeout);
  1040. if (ret) {
  1041. dev_err(&connection->hd->dev,
  1042. "%s: unidirectional operation of type 0x%02x failed: %d\n",
  1043. connection->name, type, ret);
  1044. }
  1045. gb_operation_put(operation);
  1046. return ret;
  1047. }
  1048. EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
  1049. int __init gb_operation_init(void)
  1050. {
  1051. gb_message_cache = kmem_cache_create("gb_message_cache",
  1052. sizeof(struct gb_message), 0, 0,
  1053. NULL);
  1054. if (!gb_message_cache)
  1055. return -ENOMEM;
  1056. gb_operation_cache = kmem_cache_create("gb_operation_cache",
  1057. sizeof(struct gb_operation), 0,
  1058. 0, NULL);
  1059. if (!gb_operation_cache)
  1060. goto err_destroy_message_cache;
  1061. gb_operation_completion_wq = alloc_workqueue("greybus_completion",
  1062. WQ_PERCPU, 0);
  1063. if (!gb_operation_completion_wq)
  1064. goto err_destroy_operation_cache;
  1065. return 0;
  1066. err_destroy_operation_cache:
  1067. kmem_cache_destroy(gb_operation_cache);
  1068. gb_operation_cache = NULL;
  1069. err_destroy_message_cache:
  1070. kmem_cache_destroy(gb_message_cache);
  1071. gb_message_cache = NULL;
  1072. return -ENOMEM;
  1073. }
  1074. void gb_operation_exit(void)
  1075. {
  1076. destroy_workqueue(gb_operation_completion_wq);
  1077. gb_operation_completion_wq = NULL;
  1078. kmem_cache_destroy(gb_operation_cache);
  1079. gb_operation_cache = NULL;
  1080. kmem_cache_destroy(gb_message_cache);
  1081. gb_message_cache = NULL;
  1082. }