1
2
3
4
5
6
7
8
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/wait.h>
14#include <linux/workqueue.h>
15
16#include "greybus.h"
17#include "greybus_trace.h"
18
19static struct kmem_cache *gb_operation_cache;
20static struct kmem_cache *gb_message_cache;
21
22
23static struct workqueue_struct *gb_operation_completion_wq;
24
25
26static DECLARE_WAIT_QUEUE_HEAD(gb_operation_cancellation_queue);
27
28
29
30
31static DEFINE_SPINLOCK(gb_operations_lock);
32
33static int gb_operation_response_send(struct gb_operation *operation,
34 int errno);
35
36
37
38
39
40
41
42static 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 switch (connection->state) {
49 case GB_CONNECTION_STATE_ENABLED:
50 break;
51 case GB_CONNECTION_STATE_ENABLED_TX:
52 if (gb_operation_is_incoming(operation))
53 goto err_unlock;
54 break;
55 case GB_CONNECTION_STATE_DISCONNECTING:
56 if (!gb_operation_is_core(operation))
57 goto err_unlock;
58 break;
59 default:
60 goto err_unlock;
61 }
62
63 if (operation->active++ == 0)
64 list_add_tail(&operation->links, &connection->operations);
65
66 trace_gb_operation_get_active(operation);
67
68 spin_unlock_irqrestore(&connection->lock, flags);
69
70 return 0;
71
72err_unlock:
73 spin_unlock_irqrestore(&connection->lock, flags);
74
75 return -ENOTCONN;
76}
77
78
79static void gb_operation_put_active(struct gb_operation *operation)
80{
81 struct gb_connection *connection = operation->connection;
82 unsigned long flags;
83
84 spin_lock_irqsave(&connection->lock, flags);
85
86 trace_gb_operation_put_active(operation);
87
88 if (--operation->active == 0) {
89 list_del(&operation->links);
90 if (atomic_read(&operation->waiters))
91 wake_up(&gb_operation_cancellation_queue);
92 }
93 spin_unlock_irqrestore(&connection->lock, flags);
94}
95
96static bool gb_operation_is_active(struct gb_operation *operation)
97{
98 struct gb_connection *connection = operation->connection;
99 unsigned long flags;
100 bool ret;
101
102 spin_lock_irqsave(&connection->lock, flags);
103 ret = operation->active;
104 spin_unlock_irqrestore(&connection->lock, flags);
105
106 return ret;
107}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134static bool gb_operation_result_set(struct gb_operation *operation, int result)
135{
136 unsigned long flags;
137 int prev;
138
139 if (result == -EINPROGRESS) {
140
141
142
143
144
145
146
147 spin_lock_irqsave(&gb_operations_lock, flags);
148 prev = operation->errno;
149 if (prev == -EBADR)
150 operation->errno = result;
151 else
152 operation->errno = -EILSEQ;
153 spin_unlock_irqrestore(&gb_operations_lock, flags);
154 WARN_ON(prev != -EBADR);
155
156 return true;
157 }
158
159
160
161
162
163
164
165
166
167
168 if (WARN_ON(result == -EBADR))
169 result = -EILSEQ;
170
171 spin_lock_irqsave(&gb_operations_lock, flags);
172 prev = operation->errno;
173 if (prev == -EINPROGRESS)
174 operation->errno = result;
175 spin_unlock_irqrestore(&gb_operations_lock, flags);
176
177 return prev == -EINPROGRESS;
178}
179
180int gb_operation_result(struct gb_operation *operation)
181{
182 int result = operation->errno;
183
184 WARN_ON(result == -EBADR);
185 WARN_ON(result == -EINPROGRESS);
186
187 return result;
188}
189EXPORT_SYMBOL_GPL(gb_operation_result);
190
191
192
193
194
195static struct gb_operation *
196gb_operation_find_outgoing(struct gb_connection *connection, u16 operation_id)
197{
198 struct gb_operation *operation;
199 unsigned long flags;
200 bool found = false;
201
202 spin_lock_irqsave(&connection->lock, flags);
203 list_for_each_entry(operation, &connection->operations, links)
204 if (operation->id == operation_id &&
205 !gb_operation_is_incoming(operation)) {
206 gb_operation_get(operation);
207 found = true;
208 break;
209 }
210 spin_unlock_irqrestore(&connection->lock, flags);
211
212 return found ? operation : NULL;
213}
214
215static int gb_message_send(struct gb_message *message, gfp_t gfp)
216{
217 struct gb_connection *connection = message->operation->connection;
218
219 trace_gb_message_send(message);
220 return connection->hd->driver->message_send(connection->hd,
221 connection->hd_cport_id,
222 message,
223 gfp);
224}
225
226
227
228
229static void gb_message_cancel(struct gb_message *message)
230{
231 struct gb_host_device *hd = message->operation->connection->hd;
232
233 hd->driver->message_cancel(message);
234}
235
236static void gb_operation_request_handle(struct gb_operation *operation)
237{
238 struct gb_connection *connection = operation->connection;
239 int status;
240 int ret;
241
242 if (connection->handler) {
243 status = connection->handler(operation);
244 } else {
245 dev_err(&connection->hd->dev,
246 "%s: unexpected incoming request of type 0x%02x\n",
247 connection->name, operation->type);
248
249 status = -EPROTONOSUPPORT;
250 }
251
252 ret = gb_operation_response_send(operation, status);
253 if (ret) {
254 dev_err(&connection->hd->dev,
255 "%s: failed to send response %d for type 0x%02x: %d\n",
256 connection->name, status, operation->type, ret);
257 return;
258 }
259}
260
261
262
263
264
265
266
267
268
269
270
271
272static void gb_operation_work(struct work_struct *work)
273{
274 struct gb_operation *operation;
275 int ret;
276
277 operation = container_of(work, struct gb_operation, work);
278
279 if (gb_operation_is_incoming(operation)) {
280 gb_operation_request_handle(operation);
281 } else {
282 ret = del_timer_sync(&operation->timer);
283 if (!ret) {
284
285 if (gb_operation_result(operation) == -ETIMEDOUT)
286 gb_message_cancel(operation->request);
287 }
288
289 operation->callback(operation);
290 }
291
292 gb_operation_put_active(operation);
293 gb_operation_put(operation);
294}
295
296static void gb_operation_timeout(struct timer_list *t)
297{
298 struct gb_operation *operation = from_timer(operation, t, timer);
299
300 if (gb_operation_result_set(operation, -ETIMEDOUT)) {
301
302
303
304
305 queue_work(gb_operation_completion_wq, &operation->work);
306 }
307}
308
309static void gb_operation_message_init(struct gb_host_device *hd,
310 struct gb_message *message, u16 operation_id,
311 size_t payload_size, u8 type)
312{
313 struct gb_operation_msg_hdr *header;
314
315 header = message->buffer;
316
317 message->header = header;
318 message->payload = payload_size ? header + 1 : NULL;
319 message->payload_size = payload_size;
320
321
322
323
324
325
326 if (type != GB_REQUEST_TYPE_INVALID) {
327 u16 message_size = (u16)(sizeof(*header) + payload_size);
328
329
330
331
332
333
334
335
336
337
338 header->size = cpu_to_le16(message_size);
339 header->operation_id = 0;
340 header->type = type;
341 header->result = 0;
342 }
343}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359static struct gb_message *
360gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
361 size_t payload_size, gfp_t gfp_flags)
362{
363 struct gb_message *message;
364 struct gb_operation_msg_hdr *header;
365 size_t message_size = payload_size + sizeof(*header);
366
367 if (message_size > hd->buffer_size_max) {
368 dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
369 message_size, hd->buffer_size_max);
370 return NULL;
371 }
372
373
374 message = kmem_cache_zalloc(gb_message_cache, gfp_flags);
375 if (!message)
376 return NULL;
377
378 message->buffer = kzalloc(message_size, gfp_flags);
379 if (!message->buffer)
380 goto err_free_message;
381
382
383 gb_operation_message_init(hd, message, 0, payload_size, type);
384
385 return message;
386
387err_free_message:
388 kmem_cache_free(gb_message_cache, message);
389
390 return NULL;
391}
392
393static void gb_operation_message_free(struct gb_message *message)
394{
395 kfree(message->buffer);
396 kmem_cache_free(gb_message_cache, message);
397}
398
399
400
401
402
403static int gb_operation_status_map(u8 status)
404{
405 switch (status) {
406 case GB_OP_SUCCESS:
407 return 0;
408 case GB_OP_INTERRUPTED:
409 return -EINTR;
410 case GB_OP_TIMEOUT:
411 return -ETIMEDOUT;
412 case GB_OP_NO_MEMORY:
413 return -ENOMEM;
414 case GB_OP_PROTOCOL_BAD:
415 return -EPROTONOSUPPORT;
416 case GB_OP_OVERFLOW:
417 return -EMSGSIZE;
418 case GB_OP_INVALID:
419 return -EINVAL;
420 case GB_OP_RETRY:
421 return -EAGAIN;
422 case GB_OP_NONEXISTENT:
423 return -ENODEV;
424 case GB_OP_MALFUNCTION:
425 return -EILSEQ;
426 case GB_OP_UNKNOWN_ERROR:
427 default:
428 return -EIO;
429 }
430}
431
432
433
434
435
436
437
438static u8 gb_operation_errno_map(int errno)
439{
440 switch (errno) {
441 case 0:
442 return GB_OP_SUCCESS;
443 case -EINTR:
444 return GB_OP_INTERRUPTED;
445 case -ETIMEDOUT:
446 return GB_OP_TIMEOUT;
447 case -ENOMEM:
448 return GB_OP_NO_MEMORY;
449 case -EPROTONOSUPPORT:
450 return GB_OP_PROTOCOL_BAD;
451 case -EMSGSIZE:
452 return GB_OP_OVERFLOW;
453 case -EINVAL:
454 return GB_OP_INVALID;
455 case -EAGAIN:
456 return GB_OP_RETRY;
457 case -EILSEQ:
458 return GB_OP_MALFUNCTION;
459 case -ENODEV:
460 return GB_OP_NONEXISTENT;
461 case -EIO:
462 default:
463 return GB_OP_UNKNOWN_ERROR;
464 }
465}
466
467bool gb_operation_response_alloc(struct gb_operation *operation,
468 size_t response_size, gfp_t gfp)
469{
470 struct gb_host_device *hd = operation->connection->hd;
471 struct gb_operation_msg_hdr *request_header;
472 struct gb_message *response;
473 u8 type;
474
475 type = operation->type | GB_MESSAGE_TYPE_RESPONSE;
476 response = gb_operation_message_alloc(hd, type, response_size, gfp);
477 if (!response)
478 return false;
479 response->operation = operation;
480
481
482
483
484
485
486
487 request_header = operation->request->header;
488 response->header->operation_id = request_header->operation_id;
489 operation->response = response;
490
491 return true;
492}
493EXPORT_SYMBOL_GPL(gb_operation_response_alloc);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517static struct gb_operation *
518gb_operation_create_common(struct gb_connection *connection, u8 type,
519 size_t request_size, size_t response_size,
520 unsigned long op_flags, gfp_t gfp_flags)
521{
522 struct gb_host_device *hd = connection->hd;
523 struct gb_operation *operation;
524
525 operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
526 if (!operation)
527 return NULL;
528 operation->connection = connection;
529
530 operation->request = gb_operation_message_alloc(hd, type, request_size,
531 gfp_flags);
532 if (!operation->request)
533 goto err_cache;
534 operation->request->operation = operation;
535
536
537 if (!(op_flags & GB_OPERATION_FLAG_INCOMING)) {
538 if (!gb_operation_response_alloc(operation, response_size,
539 gfp_flags)) {
540 goto err_request;
541 }
542
543 timer_setup(&operation->timer, gb_operation_timeout, 0);
544 }
545
546 operation->flags = op_flags;
547 operation->type = type;
548 operation->errno = -EBADR;
549
550 INIT_WORK(&operation->work, gb_operation_work);
551 init_completion(&operation->completion);
552 kref_init(&operation->kref);
553 atomic_set(&operation->waiters, 0);
554
555 return operation;
556
557err_request:
558 gb_operation_message_free(operation->request);
559err_cache:
560 kmem_cache_free(gb_operation_cache, operation);
561
562 return NULL;
563}
564
565
566
567
568
569
570
571
572
573struct gb_operation *
574gb_operation_create_flags(struct gb_connection *connection,
575 u8 type, size_t request_size,
576 size_t response_size, unsigned long flags,
577 gfp_t gfp)
578{
579 struct gb_operation *operation;
580
581 if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
582 return NULL;
583 if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
584 type &= ~GB_MESSAGE_TYPE_RESPONSE;
585
586 if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
587 flags &= GB_OPERATION_FLAG_USER_MASK;
588
589 operation = gb_operation_create_common(connection, type,
590 request_size, response_size,
591 flags, gfp);
592 if (operation)
593 trace_gb_operation_create(operation);
594
595 return operation;
596}
597EXPORT_SYMBOL_GPL(gb_operation_create_flags);
598
599struct gb_operation *
600gb_operation_create_core(struct gb_connection *connection,
601 u8 type, size_t request_size,
602 size_t response_size, unsigned long flags,
603 gfp_t gfp)
604{
605 struct gb_operation *operation;
606
607 flags |= GB_OPERATION_FLAG_CORE;
608
609 operation = gb_operation_create_common(connection, type,
610 request_size, response_size,
611 flags, gfp);
612 if (operation)
613 trace_gb_operation_create_core(operation);
614
615 return operation;
616}
617
618
619size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
620{
621 struct gb_host_device *hd = connection->hd;
622
623 return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
624}
625EXPORT_SYMBOL_GPL(gb_operation_get_payload_size_max);
626
627static struct gb_operation *
628gb_operation_create_incoming(struct gb_connection *connection, u16 id,
629 u8 type, void *data, size_t size)
630{
631 struct gb_operation *operation;
632 size_t request_size;
633 unsigned long flags = GB_OPERATION_FLAG_INCOMING;
634
635
636 request_size = size - sizeof(struct gb_operation_msg_hdr);
637
638 if (!id)
639 flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
640
641 operation = gb_operation_create_common(connection, type,
642 request_size,
643 GB_REQUEST_TYPE_INVALID,
644 flags, GFP_ATOMIC);
645 if (!operation)
646 return NULL;
647
648 operation->id = id;
649 memcpy(operation->request->header, data, size);
650 trace_gb_operation_create_incoming(operation);
651
652 return operation;
653}
654
655
656
657
658void gb_operation_get(struct gb_operation *operation)
659{
660 kref_get(&operation->kref);
661}
662EXPORT_SYMBOL_GPL(gb_operation_get);
663
664
665
666
667static void _gb_operation_destroy(struct kref *kref)
668{
669 struct gb_operation *operation;
670
671 operation = container_of(kref, struct gb_operation, kref);
672
673 trace_gb_operation_destroy(operation);
674
675 if (operation->response)
676 gb_operation_message_free(operation->response);
677 gb_operation_message_free(operation->request);
678
679 kmem_cache_free(gb_operation_cache, operation);
680}
681
682
683
684
685
686void gb_operation_put(struct gb_operation *operation)
687{
688 if (WARN_ON(!operation))
689 return;
690
691 kref_put(&operation->kref, _gb_operation_destroy);
692}
693EXPORT_SYMBOL_GPL(gb_operation_put);
694
695
696static void gb_operation_sync_callback(struct gb_operation *operation)
697{
698 complete(&operation->completion);
699}
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718int gb_operation_request_send(struct gb_operation *operation,
719 gb_operation_callback callback,
720 unsigned int timeout,
721 gfp_t gfp)
722{
723 struct gb_connection *connection = operation->connection;
724 struct gb_operation_msg_hdr *header;
725 unsigned int cycle;
726 int ret;
727
728 if (gb_connection_is_offloaded(connection))
729 return -EBUSY;
730
731 if (!callback)
732 return -EINVAL;
733
734
735
736
737
738
739 operation->callback = callback;
740
741
742
743
744
745 if (gb_operation_is_unidirectional(operation)) {
746 operation->id = 0;
747 } else {
748 cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
749 operation->id = (u16)(cycle % U16_MAX + 1);
750 }
751
752 header = operation->request->header;
753 header->operation_id = cpu_to_le16(operation->id);
754
755 gb_operation_result_set(operation, -EINPROGRESS);
756
757
758
759
760
761 gb_operation_get(operation);
762 ret = gb_operation_get_active(operation);
763 if (ret)
764 goto err_put;
765
766 ret = gb_message_send(operation->request, gfp);
767 if (ret)
768 goto err_put_active;
769
770 if (timeout) {
771 operation->timer.expires = jiffies + msecs_to_jiffies(timeout);
772 add_timer(&operation->timer);
773 }
774
775 return 0;
776
777err_put_active:
778 gb_operation_put_active(operation);
779err_put:
780 gb_operation_put(operation);
781
782 return ret;
783}
784EXPORT_SYMBOL_GPL(gb_operation_request_send);
785
786
787
788
789
790
791
792int gb_operation_request_send_sync_timeout(struct gb_operation *operation,
793 unsigned int timeout)
794{
795 int ret;
796
797 ret = gb_operation_request_send(operation, gb_operation_sync_callback,
798 timeout, GFP_KERNEL);
799 if (ret)
800 return ret;
801
802 ret = wait_for_completion_interruptible(&operation->completion);
803 if (ret < 0) {
804
805 gb_operation_cancel(operation, -ECANCELED);
806 }
807
808 return gb_operation_result(operation);
809}
810EXPORT_SYMBOL_GPL(gb_operation_request_send_sync_timeout);
811
812
813
814
815
816
817
818
819
820
821static int gb_operation_response_send(struct gb_operation *operation,
822 int errno)
823{
824 struct gb_connection *connection = operation->connection;
825 int ret;
826
827 if (!operation->response &&
828 !gb_operation_is_unidirectional(operation)) {
829 if (!gb_operation_response_alloc(operation, 0, GFP_KERNEL))
830 return -ENOMEM;
831 }
832
833
834 if (!gb_operation_result_set(operation, errno)) {
835 dev_err(&connection->hd->dev, "request result already set\n");
836 return -EIO;
837 }
838
839
840 if (gb_operation_is_unidirectional(operation))
841 return 0;
842
843
844 gb_operation_get(operation);
845 ret = gb_operation_get_active(operation);
846 if (ret)
847 goto err_put;
848
849
850 operation->response->header->result = gb_operation_errno_map(errno);
851
852 ret = gb_message_send(operation->response, GFP_KERNEL);
853 if (ret)
854 goto err_put_active;
855
856 return 0;
857
858err_put_active:
859 gb_operation_put_active(operation);
860err_put:
861 gb_operation_put(operation);
862
863 return ret;
864}
865
866
867
868
869void greybus_message_sent(struct gb_host_device *hd,
870 struct gb_message *message, int status)
871{
872 struct gb_operation *operation = message->operation;
873 struct gb_connection *connection = operation->connection;
874
875
876
877
878
879
880
881
882
883
884
885
886 if (message == operation->response) {
887 if (status) {
888 dev_err(&connection->hd->dev,
889 "%s: error sending response 0x%02x: %d\n",
890 connection->name, operation->type, status);
891 }
892
893 gb_operation_put_active(operation);
894 gb_operation_put(operation);
895 } else if (status || gb_operation_is_unidirectional(operation)) {
896 if (gb_operation_result_set(operation, status)) {
897 queue_work(gb_operation_completion_wq,
898 &operation->work);
899 }
900 }
901}
902EXPORT_SYMBOL_GPL(greybus_message_sent);
903
904
905
906
907
908
909
910
911static void gb_connection_recv_request(struct gb_connection *connection,
912 const struct gb_operation_msg_hdr *header,
913 void *data, size_t size)
914{
915 struct gb_operation *operation;
916 u16 operation_id;
917 u8 type;
918 int ret;
919
920 operation_id = le16_to_cpu(header->operation_id);
921 type = header->type;
922
923 operation = gb_operation_create_incoming(connection, operation_id,
924 type, data, size);
925 if (!operation) {
926 dev_err(&connection->hd->dev,
927 "%s: can't create incoming operation\n",
928 connection->name);
929 return;
930 }
931
932 ret = gb_operation_get_active(operation);
933 if (ret) {
934 gb_operation_put(operation);
935 return;
936 }
937 trace_gb_message_recv_request(operation->request);
938
939
940
941
942
943 if (gb_operation_result_set(operation, -EINPROGRESS))
944 queue_work(connection->wq, &operation->work);
945}
946
947
948
949
950
951
952
953
954
955static void gb_connection_recv_response(struct gb_connection *connection,
956 const struct gb_operation_msg_hdr *header,
957 void *data, size_t size)
958{
959 struct gb_operation *operation;
960 struct gb_message *message;
961 size_t message_size;
962 u16 operation_id;
963 int errno;
964
965 operation_id = le16_to_cpu(header->operation_id);
966
967 if (!operation_id) {
968 dev_err_ratelimited(&connection->hd->dev,
969 "%s: invalid response id 0 received\n",
970 connection->name);
971 return;
972 }
973
974 operation = gb_operation_find_outgoing(connection, operation_id);
975 if (!operation) {
976 dev_err_ratelimited(&connection->hd->dev,
977 "%s: unexpected response id 0x%04x received\n",
978 connection->name, operation_id);
979 return;
980 }
981
982 errno = gb_operation_status_map(header->result);
983 message = operation->response;
984 message_size = sizeof(*header) + message->payload_size;
985 if (!errno && size > message_size) {
986 dev_err_ratelimited(&connection->hd->dev,
987 "%s: malformed response 0x%02x received (%zu > %zu)\n",
988 connection->name, header->type,
989 size, message_size);
990 errno = -EMSGSIZE;
991 } else if (!errno && size < message_size) {
992 if (gb_operation_short_response_allowed(operation)) {
993 message->payload_size = size - sizeof(*header);
994 } else {
995 dev_err_ratelimited(&connection->hd->dev,
996 "%s: short response 0x%02x received (%zu < %zu)\n",
997 connection->name, header->type,
998 size, message_size);
999 errno = -EMSGSIZE;
1000 }
1001 }
1002
1003
1004 if (errno)
1005 size = sizeof(*header);
1006
1007
1008 if (gb_operation_result_set(operation, errno)) {
1009 memcpy(message->buffer, data, size);
1010
1011 trace_gb_message_recv_response(message);
1012
1013 queue_work(gb_operation_completion_wq, &operation->work);
1014 }
1015
1016 gb_operation_put(operation);
1017}
1018
1019
1020
1021
1022
1023
1024void gb_connection_recv(struct gb_connection *connection,
1025 void *data, size_t size)
1026{
1027 struct gb_operation_msg_hdr header;
1028 struct device *dev = &connection->hd->dev;
1029 size_t msg_size;
1030
1031 if (connection->state == GB_CONNECTION_STATE_DISABLED ||
1032 gb_connection_is_offloaded(connection)) {
1033 dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
1034 connection->name, size);
1035 return;
1036 }
1037
1038 if (size < sizeof(header)) {
1039 dev_err_ratelimited(dev, "%s: short message received\n",
1040 connection->name);
1041 return;
1042 }
1043
1044
1045 memcpy(&header, data, sizeof(header));
1046 msg_size = le16_to_cpu(header.size);
1047 if (size < msg_size) {
1048 dev_err_ratelimited(dev,
1049 "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
1050 connection->name,
1051 le16_to_cpu(header.operation_id),
1052 header.type, size, msg_size);
1053 return;
1054 }
1055
1056 if (header.type & GB_MESSAGE_TYPE_RESPONSE) {
1057 gb_connection_recv_response(connection, &header, data,
1058 msg_size);
1059 } else {
1060 gb_connection_recv_request(connection, &header, data,
1061 msg_size);
1062 }
1063}
1064
1065
1066
1067
1068
1069void gb_operation_cancel(struct gb_operation *operation, int errno)
1070{
1071 if (WARN_ON(gb_operation_is_incoming(operation)))
1072 return;
1073
1074 if (gb_operation_result_set(operation, errno)) {
1075 gb_message_cancel(operation->request);
1076 queue_work(gb_operation_completion_wq, &operation->work);
1077 }
1078 trace_gb_message_cancel_outgoing(operation->request);
1079
1080 atomic_inc(&operation->waiters);
1081 wait_event(gb_operation_cancellation_queue,
1082 !gb_operation_is_active(operation));
1083 atomic_dec(&operation->waiters);
1084}
1085EXPORT_SYMBOL_GPL(gb_operation_cancel);
1086
1087
1088
1089
1090
1091void gb_operation_cancel_incoming(struct gb_operation *operation, int errno)
1092{
1093 if (WARN_ON(!gb_operation_is_incoming(operation)))
1094 return;
1095
1096 if (!gb_operation_is_unidirectional(operation)) {
1097
1098
1099
1100
1101 flush_work(&operation->work);
1102 if (!gb_operation_result_set(operation, errno))
1103 gb_message_cancel(operation->response);
1104 }
1105 trace_gb_message_cancel_incoming(operation->response);
1106
1107 atomic_inc(&operation->waiters);
1108 wait_event(gb_operation_cancellation_queue,
1109 !gb_operation_is_active(operation));
1110 atomic_dec(&operation->waiters);
1111}
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136int gb_operation_sync_timeout(struct gb_connection *connection, int type,
1137 void *request, int request_size,
1138 void *response, int response_size,
1139 unsigned int timeout)
1140{
1141 struct gb_operation *operation;
1142 int ret;
1143
1144 if ((response_size && !response) ||
1145 (request_size && !request))
1146 return -EINVAL;
1147
1148 operation = gb_operation_create(connection, type,
1149 request_size, response_size,
1150 GFP_KERNEL);
1151 if (!operation)
1152 return -ENOMEM;
1153
1154 if (request_size)
1155 memcpy(operation->request->payload, request, request_size);
1156
1157 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1158 if (ret) {
1159 dev_err(&connection->hd->dev,
1160 "%s: synchronous operation id 0x%04x of type 0x%02x failed: %d\n",
1161 connection->name, operation->id, type, ret);
1162 } else {
1163 if (response_size) {
1164 memcpy(response, operation->response->payload,
1165 response_size);
1166 }
1167 }
1168
1169 gb_operation_put(operation);
1170
1171 return ret;
1172}
1173EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189int gb_operation_unidirectional_timeout(struct gb_connection *connection,
1190 int type, void *request, int request_size,
1191 unsigned int timeout)
1192{
1193 struct gb_operation *operation;
1194 int ret;
1195
1196 if (request_size && !request)
1197 return -EINVAL;
1198
1199 operation = gb_operation_create_flags(connection, type,
1200 request_size, 0,
1201 GB_OPERATION_FLAG_UNIDIRECTIONAL,
1202 GFP_KERNEL);
1203 if (!operation)
1204 return -ENOMEM;
1205
1206 if (request_size)
1207 memcpy(operation->request->payload, request, request_size);
1208
1209 ret = gb_operation_request_send_sync_timeout(operation, timeout);
1210 if (ret) {
1211 dev_err(&connection->hd->dev,
1212 "%s: unidirectional operation of type 0x%02x failed: %d\n",
1213 connection->name, type, ret);
1214 }
1215
1216 gb_operation_put(operation);
1217
1218 return ret;
1219}
1220EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
1221
1222int __init gb_operation_init(void)
1223{
1224 gb_message_cache = kmem_cache_create("gb_message_cache",
1225 sizeof(struct gb_message), 0, 0, NULL);
1226 if (!gb_message_cache)
1227 return -ENOMEM;
1228
1229 gb_operation_cache = kmem_cache_create("gb_operation_cache",
1230 sizeof(struct gb_operation), 0, 0, NULL);
1231 if (!gb_operation_cache)
1232 goto err_destroy_message_cache;
1233
1234 gb_operation_completion_wq = alloc_workqueue("greybus_completion",
1235 0, 0);
1236 if (!gb_operation_completion_wq)
1237 goto err_destroy_operation_cache;
1238
1239 return 0;
1240
1241err_destroy_operation_cache:
1242 kmem_cache_destroy(gb_operation_cache);
1243 gb_operation_cache = NULL;
1244err_destroy_message_cache:
1245 kmem_cache_destroy(gb_message_cache);
1246 gb_message_cache = NULL;
1247
1248 return -ENOMEM;
1249}
1250
1251void gb_operation_exit(void)
1252{
1253 destroy_workqueue(gb_operation_completion_wq);
1254 gb_operation_completion_wq = NULL;
1255 kmem_cache_destroy(gb_operation_cache);
1256 gb_operation_cache = NULL;
1257 kmem_cache_destroy(gb_message_cache);
1258 gb_message_cache = NULL;
1259}
1260