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