1
2
3
4
5
6
7
8#include <asm/unaligned.h>
9#include <linux/atomic.h>
10#include <linux/error-injection.h>
11#include <linux/jiffies.h>
12#include <linux/kfifo.h>
13#include <linux/kref.h>
14#include <linux/kthread.h>
15#include <linux/ktime.h>
16#include <linux/limits.h>
17#include <linux/list.h>
18#include <linux/lockdep.h>
19#include <linux/serdev.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/workqueue.h>
23
24#include <linux/surface_aggregator/serial_hub.h>
25
26#include "ssh_msgb.h"
27#include "ssh_packet_layer.h"
28#include "ssh_parser.h"
29
30#include "trace.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184#define SSH_PTL_MAX_PACKET_TRIES 3
185
186
187
188
189
190
191
192
193#define SSH_PTL_TX_TIMEOUT HZ
194
195
196
197
198
199
200
201#define SSH_PTL_PACKET_TIMEOUT ms_to_ktime(1000)
202
203
204
205
206
207
208
209#define SSH_PTL_PACKET_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
210
211
212
213
214
215
216
217
218#define SSH_PTL_MAX_PENDING 1
219
220
221
222
223#define SSH_PTL_RX_BUF_LEN 4096
224
225
226
227
228#define SSH_PTL_RX_FIFO_LEN 4096
229
230#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
231
232
233
234
235
236
237
238
239
240
241
242static noinline bool ssh_ptl_should_drop_ack_packet(void)
243{
244 return false;
245}
246ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_ack_packet, TRUE);
247
248
249
250
251
252
253
254
255static noinline bool ssh_ptl_should_drop_nak_packet(void)
256{
257 return false;
258}
259ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_nak_packet, TRUE);
260
261
262
263
264
265
266
267
268
269static noinline bool ssh_ptl_should_drop_dsq_packet(void)
270{
271 return false;
272}
273ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_dsq_packet, TRUE);
274
275
276
277
278
279
280
281static noinline int ssh_ptl_should_fail_write(void)
282{
283 return 0;
284}
285ALLOW_ERROR_INJECTION(ssh_ptl_should_fail_write, ERRNO);
286
287
288
289
290
291
292
293
294
295
296
297static noinline bool ssh_ptl_should_corrupt_tx_data(void)
298{
299 return false;
300}
301ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_tx_data, TRUE);
302
303
304
305
306
307
308
309
310static noinline bool ssh_ptl_should_corrupt_rx_syn(void)
311{
312 return false;
313}
314ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_syn, TRUE);
315
316
317
318
319
320
321
322
323static noinline bool ssh_ptl_should_corrupt_rx_data(void)
324{
325 return false;
326}
327ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_data, TRUE);
328
329static bool __ssh_ptl_should_drop_ack_packet(struct ssh_packet *packet)
330{
331 if (likely(!ssh_ptl_should_drop_ack_packet()))
332 return false;
333
334 trace_ssam_ei_tx_drop_ack_packet(packet);
335 ptl_info(packet->ptl, "packet error injection: dropping ACK packet %p\n",
336 packet);
337
338 return true;
339}
340
341static bool __ssh_ptl_should_drop_nak_packet(struct ssh_packet *packet)
342{
343 if (likely(!ssh_ptl_should_drop_nak_packet()))
344 return false;
345
346 trace_ssam_ei_tx_drop_nak_packet(packet);
347 ptl_info(packet->ptl, "packet error injection: dropping NAK packet %p\n",
348 packet);
349
350 return true;
351}
352
353static bool __ssh_ptl_should_drop_dsq_packet(struct ssh_packet *packet)
354{
355 if (likely(!ssh_ptl_should_drop_dsq_packet()))
356 return false;
357
358 trace_ssam_ei_tx_drop_dsq_packet(packet);
359 ptl_info(packet->ptl,
360 "packet error injection: dropping sequenced data packet %p\n",
361 packet);
362
363 return true;
364}
365
366static bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
367{
368
369 if (!packet->data.ptr || !packet->data.len)
370 return false;
371
372 switch (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)]) {
373 case SSH_FRAME_TYPE_ACK:
374 return __ssh_ptl_should_drop_ack_packet(packet);
375
376 case SSH_FRAME_TYPE_NAK:
377 return __ssh_ptl_should_drop_nak_packet(packet);
378
379 case SSH_FRAME_TYPE_DATA_SEQ:
380 return __ssh_ptl_should_drop_dsq_packet(packet);
381
382 default:
383 return false;
384 }
385}
386
387static int ssh_ptl_write_buf(struct ssh_ptl *ptl, struct ssh_packet *packet,
388 const unsigned char *buf, size_t count)
389{
390 int status;
391
392 status = ssh_ptl_should_fail_write();
393 if (unlikely(status)) {
394 trace_ssam_ei_tx_fail_write(packet, status);
395 ptl_info(packet->ptl,
396 "packet error injection: simulating transmit error %d, packet %p\n",
397 status, packet);
398
399 return status;
400 }
401
402 return serdev_device_write_buf(ptl->serdev, buf, count);
403}
404
405static void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
406{
407
408 if (!packet->data.ptr || !packet->data.len)
409 return;
410
411
412 if (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)] != SSH_FRAME_TYPE_DATA_SEQ)
413 return;
414
415 if (likely(!ssh_ptl_should_corrupt_tx_data()))
416 return;
417
418 trace_ssam_ei_tx_corrupt_data(packet);
419 ptl_info(packet->ptl,
420 "packet error injection: simulating invalid transmit data on packet %p\n",
421 packet);
422
423
424
425
426
427
428 memset(packet->data.ptr, 0xb3, packet->data.len);
429}
430
431static void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
432 struct ssam_span *data)
433{
434 struct ssam_span frame;
435
436
437 if (!sshp_find_syn(data, &frame))
438 return;
439
440 if (likely(!ssh_ptl_should_corrupt_rx_syn()))
441 return;
442
443 trace_ssam_ei_rx_corrupt_syn(data->len);
444
445 data->ptr[1] = 0xb3;
446}
447
448static void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
449 struct ssam_span *frame)
450{
451 size_t payload_len, message_len;
452 struct ssh_frame *sshf;
453
454
455 if (frame->len < SSH_MESSAGE_LENGTH(0))
456 return;
457
458
459 payload_len = get_unaligned_le16(&frame->ptr[SSH_MSGOFFSET_FRAME(len)]);
460 message_len = SSH_MESSAGE_LENGTH(payload_len);
461 if (frame->len < message_len)
462 return;
463
464 if (likely(!ssh_ptl_should_corrupt_rx_data()))
465 return;
466
467 sshf = (struct ssh_frame *)&frame->ptr[SSH_MSGOFFSET_FRAME(type)];
468 trace_ssam_ei_rx_corrupt_data(sshf);
469
470
471
472
473
474
475
476 frame->ptr[frame->len - 2] = ~frame->ptr[frame->len - 2];
477}
478
479#else
480
481static inline bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
482{
483 return false;
484}
485
486static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
487 struct ssh_packet *packet,
488 const unsigned char *buf,
489 size_t count)
490{
491 return serdev_device_write_buf(ptl->serdev, buf, count);
492}
493
494static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
495{
496}
497
498static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
499 struct ssam_span *data)
500{
501}
502
503static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
504 struct ssam_span *frame)
505{
506}
507
508#endif
509
510static void __ssh_ptl_packet_release(struct kref *kref)
511{
512 struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
513
514 trace_ssam_packet_release(p);
515
516 ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
517 p->ops->release(p);
518}
519
520
521
522
523
524
525
526
527
528
529struct ssh_packet *ssh_packet_get(struct ssh_packet *packet)
530{
531 if (packet)
532 kref_get(&packet->refcnt);
533 return packet;
534}
535EXPORT_SYMBOL_GPL(ssh_packet_get);
536
537
538
539
540
541
542
543
544
545
546
547void ssh_packet_put(struct ssh_packet *packet)
548{
549 if (packet)
550 kref_put(&packet->refcnt, __ssh_ptl_packet_release);
551}
552EXPORT_SYMBOL_GPL(ssh_packet_put);
553
554static u8 ssh_packet_get_seq(struct ssh_packet *packet)
555{
556 return packet->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
557}
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
573 u8 priority, const struct ssh_packet_ops *ops)
574{
575 kref_init(&packet->refcnt);
576
577 packet->ptl = NULL;
578 INIT_LIST_HEAD(&packet->queue_node);
579 INIT_LIST_HEAD(&packet->pending_node);
580
581 packet->state = type & SSH_PACKET_FLAGS_TY_MASK;
582 packet->priority = priority;
583 packet->timestamp = KTIME_MAX;
584
585 packet->data.ptr = NULL;
586 packet->data.len = 0;
587
588 packet->ops = ops;
589}
590
591static struct kmem_cache *ssh_ctrl_packet_cache;
592
593
594
595
596int ssh_ctrl_packet_cache_init(void)
597{
598 const unsigned int size = sizeof(struct ssh_packet) + SSH_MSG_LEN_CTRL;
599 const unsigned int align = __alignof__(struct ssh_packet);
600 struct kmem_cache *cache;
601
602 cache = kmem_cache_create("ssam_ctrl_packet", size, align, 0, NULL);
603 if (!cache)
604 return -ENOMEM;
605
606 ssh_ctrl_packet_cache = cache;
607 return 0;
608}
609
610
611
612
613void ssh_ctrl_packet_cache_destroy(void)
614{
615 kmem_cache_destroy(ssh_ctrl_packet_cache);
616 ssh_ctrl_packet_cache = NULL;
617}
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
635 struct ssam_span *buffer, gfp_t flags)
636{
637 *packet = kmem_cache_alloc(ssh_ctrl_packet_cache, flags);
638 if (!*packet)
639 return -ENOMEM;
640
641 buffer->ptr = (u8 *)(*packet + 1);
642 buffer->len = SSH_MSG_LEN_CTRL;
643
644 trace_ssam_ctrl_packet_alloc(*packet, buffer->len);
645 return 0;
646}
647
648
649
650
651
652static void ssh_ctrl_packet_free(struct ssh_packet *p)
653{
654 trace_ssam_ctrl_packet_free(p);
655 kmem_cache_free(ssh_ctrl_packet_cache, p);
656}
657
658static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
659 .complete = NULL,
660 .release = ssh_ctrl_packet_free,
661};
662
663static void ssh_ptl_timeout_reaper_mod(struct ssh_ptl *ptl, ktime_t now,
664 ktime_t expires)
665{
666 unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
667 ktime_t aexp = ktime_add(expires, SSH_PTL_PACKET_TIMEOUT_RESOLUTION);
668
669 spin_lock(&ptl->rtx_timeout.lock);
670
671
672 if (ktime_before(aexp, ptl->rtx_timeout.expires)) {
673 ptl->rtx_timeout.expires = expires;
674 mod_delayed_work(system_wq, &ptl->rtx_timeout.reaper, delta);
675 }
676
677 spin_unlock(&ptl->rtx_timeout.lock);
678}
679
680
681static void ssh_packet_next_try(struct ssh_packet *p)
682{
683 u8 base = ssh_packet_priority_get_base(p->priority);
684 u8 try = ssh_packet_priority_get_try(p->priority);
685
686 lockdep_assert_held(&p->ptl->queue.lock);
687
688
689
690
691
692
693 WRITE_ONCE(p->priority, __SSH_PACKET_PRIORITY(base, try + 1));
694}
695
696
697static struct list_head *__ssh_ptl_queue_find_entrypoint(struct ssh_packet *p)
698{
699 struct list_head *head;
700 struct ssh_packet *q;
701
702 lockdep_assert_held(&p->ptl->queue.lock);
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717 if (p->priority > SSH_PACKET_PRIORITY(DATA, 0)) {
718 list_for_each(head, &p->ptl->queue.head) {
719 q = list_entry(head, struct ssh_packet, queue_node);
720
721 if (q->priority < p->priority)
722 break;
723 }
724 } else {
725 list_for_each_prev(head, &p->ptl->queue.head) {
726 q = list_entry(head, struct ssh_packet, queue_node);
727
728 if (q->priority >= p->priority) {
729 head = head->next;
730 break;
731 }
732 }
733 }
734
735 return head;
736}
737
738
739static int __ssh_ptl_queue_push(struct ssh_packet *packet)
740{
741 struct ssh_ptl *ptl = packet->ptl;
742 struct list_head *head;
743
744 lockdep_assert_held(&ptl->queue.lock);
745
746 if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
747 return -ESHUTDOWN;
748
749
750 if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state))
751 return -EINVAL;
752
753
754 if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state))
755 return -EALREADY;
756
757 head = __ssh_ptl_queue_find_entrypoint(packet);
758
759 list_add_tail(&ssh_packet_get(packet)->queue_node, head);
760 return 0;
761}
762
763static int ssh_ptl_queue_push(struct ssh_packet *packet)
764{
765 int status;
766
767 spin_lock(&packet->ptl->queue.lock);
768 status = __ssh_ptl_queue_push(packet);
769 spin_unlock(&packet->ptl->queue.lock);
770
771 return status;
772}
773
774static void ssh_ptl_queue_remove(struct ssh_packet *packet)
775{
776 struct ssh_ptl *ptl = packet->ptl;
777
778 spin_lock(&ptl->queue.lock);
779
780 if (!test_and_clear_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
781 spin_unlock(&ptl->queue.lock);
782 return;
783 }
784
785 list_del(&packet->queue_node);
786
787 spin_unlock(&ptl->queue.lock);
788 ssh_packet_put(packet);
789}
790
791static void ssh_ptl_pending_push(struct ssh_packet *p)
792{
793 struct ssh_ptl *ptl = p->ptl;
794 const ktime_t timestamp = ktime_get_coarse_boottime();
795 const ktime_t timeout = ptl->rtx_timeout.timeout;
796
797
798
799
800
801
802
803
804 spin_lock(&ptl->pending.lock);
805
806
807 if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state)) {
808 spin_unlock(&ptl->pending.lock);
809 return;
810 }
811
812
813
814
815
816
817 p->timestamp = timestamp;
818
819
820 if (!test_and_set_bit(SSH_PACKET_SF_PENDING_BIT, &p->state)) {
821 atomic_inc(&ptl->pending.count);
822 list_add_tail(&ssh_packet_get(p)->pending_node, &ptl->pending.head);
823 }
824
825 spin_unlock(&ptl->pending.lock);
826
827
828 ssh_ptl_timeout_reaper_mod(ptl, timestamp, timestamp + timeout);
829}
830
831static void ssh_ptl_pending_remove(struct ssh_packet *packet)
832{
833 struct ssh_ptl *ptl = packet->ptl;
834
835 spin_lock(&ptl->pending.lock);
836
837 if (!test_and_clear_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state)) {
838 spin_unlock(&ptl->pending.lock);
839 return;
840 }
841
842 list_del(&packet->pending_node);
843 atomic_dec(&ptl->pending.count);
844
845 spin_unlock(&ptl->pending.lock);
846
847 ssh_packet_put(packet);
848}
849
850
851static void __ssh_ptl_complete(struct ssh_packet *p, int status)
852{
853 struct ssh_ptl *ptl = READ_ONCE(p->ptl);
854
855 trace_ssam_packet_complete(p, status);
856 ptl_dbg_cond(ptl, "ptl: completing packet %p (status: %d)\n", p, status);
857
858 if (p->ops->complete)
859 p->ops->complete(p, status);
860}
861
862static void ssh_ptl_remove_and_complete(struct ssh_packet *p, int status)
863{
864
865
866
867
868
869
870
871
872
873
874 if (test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
875 return;
876
877 ssh_ptl_queue_remove(p);
878 ssh_ptl_pending_remove(p);
879
880 __ssh_ptl_complete(p, status);
881}
882
883static bool ssh_ptl_tx_can_process(struct ssh_packet *packet)
884{
885 struct ssh_ptl *ptl = packet->ptl;
886
887 if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &packet->state))
888 return !atomic_read(&ptl->pending.count);
889
890
891 if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &packet->state))
892 return true;
893
894
895 if (test_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state))
896 return true;
897
898
899 return atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING;
900}
901
902static struct ssh_packet *ssh_ptl_tx_pop(struct ssh_ptl *ptl)
903{
904 struct ssh_packet *packet = ERR_PTR(-ENOENT);
905 struct ssh_packet *p, *n;
906
907 spin_lock(&ptl->queue.lock);
908 list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
909
910
911
912
913 if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
914 continue;
915
916
917
918
919
920
921 if (!ssh_ptl_tx_can_process(p)) {
922 packet = ERR_PTR(-EBUSY);
923 break;
924 }
925
926
927
928
929
930
931 list_del(&p->queue_node);
932
933 set_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state);
934
935 smp_mb__before_atomic();
936 clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
937
938
939
940
941
942
943
944
945 ssh_packet_next_try(p);
946
947 packet = p;
948 break;
949 }
950 spin_unlock(&ptl->queue.lock);
951
952 return packet;
953}
954
955static struct ssh_packet *ssh_ptl_tx_next(struct ssh_ptl *ptl)
956{
957 struct ssh_packet *p;
958
959 p = ssh_ptl_tx_pop(ptl);
960 if (IS_ERR(p))
961 return p;
962
963 if (test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state)) {
964 ptl_dbg(ptl, "ptl: transmitting sequenced packet %p\n", p);
965 ssh_ptl_pending_push(p);
966 } else {
967 ptl_dbg(ptl, "ptl: transmitting non-sequenced packet %p\n", p);
968 }
969
970 return p;
971}
972
973static void ssh_ptl_tx_compl_success(struct ssh_packet *packet)
974{
975 struct ssh_ptl *ptl = packet->ptl;
976
977 ptl_dbg(ptl, "ptl: successfully transmitted packet %p\n", packet);
978
979
980 set_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state);
981
982 smp_mb__before_atomic();
983 clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
984
985
986 if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &packet->state)) {
987 set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
988 ssh_ptl_remove_and_complete(packet, 0);
989 }
990
991
992
993
994
995 wake_up_all(&ptl->tx.packet_wq);
996}
997
998static void ssh_ptl_tx_compl_error(struct ssh_packet *packet, int status)
999{
1000
1001 set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
1002
1003 smp_mb__before_atomic();
1004 clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
1005
1006 ptl_err(packet->ptl, "ptl: transmission error: %d\n", status);
1007 ptl_dbg(packet->ptl, "ptl: failed to transmit packet: %p\n", packet);
1008
1009 ssh_ptl_remove_and_complete(packet, status);
1010
1011
1012
1013
1014
1015 wake_up_all(&packet->ptl->tx.packet_wq);
1016}
1017
1018static long ssh_ptl_tx_wait_packet(struct ssh_ptl *ptl)
1019{
1020 int status;
1021
1022 status = wait_for_completion_interruptible(&ptl->tx.thread_cplt_pkt);
1023 reinit_completion(&ptl->tx.thread_cplt_pkt);
1024
1025
1026
1027
1028
1029 smp_mb__after_atomic();
1030
1031 return status;
1032}
1033
1034static long ssh_ptl_tx_wait_transfer(struct ssh_ptl *ptl, long timeout)
1035{
1036 long status;
1037
1038 status = wait_for_completion_interruptible_timeout(&ptl->tx.thread_cplt_tx,
1039 timeout);
1040 reinit_completion(&ptl->tx.thread_cplt_tx);
1041
1042
1043
1044
1045
1046 smp_mb__after_atomic();
1047
1048 return status;
1049}
1050
1051static int ssh_ptl_tx_packet(struct ssh_ptl *ptl, struct ssh_packet *packet)
1052{
1053 long timeout = SSH_PTL_TX_TIMEOUT;
1054 size_t offset = 0;
1055
1056
1057 if (unlikely(!packet->data.ptr))
1058 return 0;
1059
1060
1061 if (ssh_ptl_should_drop_packet(packet))
1062 return 0;
1063
1064
1065 ssh_ptl_tx_inject_invalid_data(packet);
1066
1067 ptl_dbg(ptl, "tx: sending data (length: %zu)\n", packet->data.len);
1068 print_hex_dump_debug("tx: ", DUMP_PREFIX_OFFSET, 16, 1,
1069 packet->data.ptr, packet->data.len, false);
1070
1071 do {
1072 ssize_t status, len;
1073 u8 *buf;
1074
1075 buf = packet->data.ptr + offset;
1076 len = packet->data.len - offset;
1077
1078 status = ssh_ptl_write_buf(ptl, packet, buf, len);
1079 if (status < 0)
1080 return status;
1081
1082 if (status == len)
1083 return 0;
1084
1085 offset += status;
1086
1087 timeout = ssh_ptl_tx_wait_transfer(ptl, timeout);
1088 if (kthread_should_stop() || !atomic_read(&ptl->tx.running))
1089 return -ESHUTDOWN;
1090
1091 if (timeout < 0)
1092 return -EINTR;
1093
1094 if (timeout == 0)
1095 return -ETIMEDOUT;
1096 } while (true);
1097}
1098
1099static int ssh_ptl_tx_threadfn(void *data)
1100{
1101 struct ssh_ptl *ptl = data;
1102
1103 while (!kthread_should_stop() && atomic_read(&ptl->tx.running)) {
1104 struct ssh_packet *packet;
1105 int status;
1106
1107
1108 packet = ssh_ptl_tx_next(ptl);
1109
1110
1111 if (IS_ERR(packet)) {
1112 ssh_ptl_tx_wait_packet(ptl);
1113 continue;
1114 }
1115
1116
1117 status = ssh_ptl_tx_packet(ptl, packet);
1118 if (status)
1119 ssh_ptl_tx_compl_error(packet, status);
1120 else
1121 ssh_ptl_tx_compl_success(packet);
1122
1123 ssh_packet_put(packet);
1124 }
1125
1126 return 0;
1127}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138static void ssh_ptl_tx_wakeup_packet(struct ssh_ptl *ptl)
1139{
1140 if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
1141 return;
1142
1143 complete(&ptl->tx.thread_cplt_pkt);
1144}
1145
1146
1147
1148
1149
1150
1151
1152int ssh_ptl_tx_start(struct ssh_ptl *ptl)
1153{
1154 atomic_set_release(&ptl->tx.running, 1);
1155
1156 ptl->tx.thread = kthread_run(ssh_ptl_tx_threadfn, ptl, "ssam_serial_hub-tx");
1157 if (IS_ERR(ptl->tx.thread))
1158 return PTR_ERR(ptl->tx.thread);
1159
1160 return 0;
1161}
1162
1163
1164
1165
1166
1167
1168
1169int ssh_ptl_tx_stop(struct ssh_ptl *ptl)
1170{
1171 int status = 0;
1172
1173 if (!IS_ERR_OR_NULL(ptl->tx.thread)) {
1174
1175 atomic_set_release(&ptl->tx.running, 0);
1176
1177
1178
1179
1180
1181
1182 complete(&ptl->tx.thread_cplt_pkt);
1183 complete(&ptl->tx.thread_cplt_tx);
1184
1185
1186 status = kthread_stop(ptl->tx.thread);
1187 ptl->tx.thread = NULL;
1188 }
1189
1190 return status;
1191}
1192
1193static struct ssh_packet *ssh_ptl_ack_pop(struct ssh_ptl *ptl, u8 seq_id)
1194{
1195 struct ssh_packet *packet = ERR_PTR(-ENOENT);
1196 struct ssh_packet *p, *n;
1197
1198 spin_lock(&ptl->pending.lock);
1199 list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
1200
1201
1202
1203
1204
1205 if (unlikely(ssh_packet_get_seq(p) != seq_id))
1206 continue;
1207
1208
1209
1210
1211
1212 if (unlikely(test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
1213 packet = ERR_PTR(-EPERM);
1214 break;
1215 }
1216
1217
1218
1219
1220
1221 set_bit(SSH_PACKET_SF_ACKED_BIT, &p->state);
1222
1223 smp_mb__before_atomic();
1224 clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
1225
1226 atomic_dec(&ptl->pending.count);
1227 list_del(&p->pending_node);
1228 packet = p;
1229
1230 break;
1231 }
1232 spin_unlock(&ptl->pending.lock);
1233
1234 return packet;
1235}
1236
1237static void ssh_ptl_wait_until_transmitted(struct ssh_packet *packet)
1238{
1239 wait_event(packet->ptl->tx.packet_wq,
1240 test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state) ||
1241 test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state));
1242}
1243
1244static void ssh_ptl_acknowledge(struct ssh_ptl *ptl, u8 seq)
1245{
1246 struct ssh_packet *p;
1247
1248 p = ssh_ptl_ack_pop(ptl, seq);
1249 if (IS_ERR(p)) {
1250 if (PTR_ERR(p) == -ENOENT) {
1251
1252
1253
1254
1255 ptl_warn(ptl, "ptl: received ACK for non-pending packet\n");
1256 } else {
1257
1258
1259
1260
1261 WARN_ON(PTR_ERR(p) != -EPERM);
1262 }
1263 return;
1264 }
1265
1266 ptl_dbg(ptl, "ptl: received ACK for packet %p\n", p);
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 ssh_ptl_wait_until_transmitted(p);
1278
1279
1280
1281
1282
1283
1284 if (unlikely(test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
1285 if (unlikely(!test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &p->state)))
1286 ptl_err(ptl, "ptl: received ACK before packet had been fully transmitted\n");
1287
1288 ssh_packet_put(p);
1289 return;
1290 }
1291
1292 ssh_ptl_remove_and_complete(p, 0);
1293 ssh_packet_put(p);
1294
1295 if (atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING)
1296 ssh_ptl_tx_wakeup_packet(ptl);
1297}
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *p)
1313{
1314 struct ssh_ptl *ptl_old;
1315 int status;
1316
1317 trace_ssam_packet_submit(p);
1318
1319
1320 if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &p->state)) {
1321 if (p->data.ptr || test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state))
1322 return -EINVAL;
1323 } else if (!p->data.ptr) {
1324 return -EINVAL;
1325 }
1326
1327
1328
1329
1330
1331
1332
1333
1334 ptl_old = READ_ONCE(p->ptl);
1335 if (!ptl_old)
1336 WRITE_ONCE(p->ptl, ptl);
1337 else if (WARN_ON(ptl_old != ptl))
1338 return -EALREADY;
1339
1340 status = ssh_ptl_queue_push(p);
1341 if (status)
1342 return status;
1343
1344 if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &p->state) ||
1345 (atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING))
1346 ssh_ptl_tx_wakeup_packet(ptl);
1347
1348 return 0;
1349}
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363static int __ssh_ptl_resubmit(struct ssh_packet *packet)
1364{
1365 int status;
1366 u8 try;
1367
1368 lockdep_assert_held(&packet->ptl->pending.lock);
1369
1370 trace_ssam_packet_resubmit(packet);
1371
1372 spin_lock(&packet->ptl->queue.lock);
1373
1374
1375 try = ssh_packet_priority_get_try(packet->priority);
1376 if (try >= SSH_PTL_MAX_PACKET_TRIES) {
1377 spin_unlock(&packet->ptl->queue.lock);
1378 return -ECANCELED;
1379 }
1380
1381 status = __ssh_ptl_queue_push(packet);
1382 if (status) {
1383
1384
1385
1386
1387
1388 spin_unlock(&packet->ptl->queue.lock);
1389 return status;
1390 }
1391
1392 packet->timestamp = KTIME_MAX;
1393
1394 spin_unlock(&packet->ptl->queue.lock);
1395 return 0;
1396}
1397
1398static void ssh_ptl_resubmit_pending(struct ssh_ptl *ptl)
1399{
1400 struct ssh_packet *p;
1401 bool resub = false;
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414 spin_lock(&ptl->pending.lock);
1415
1416
1417 list_for_each_entry(p, &ptl->pending.head, pending_node) {
1418
1419
1420
1421
1422
1423 if (!__ssh_ptl_resubmit(p))
1424 resub = true;
1425 }
1426
1427 spin_unlock(&ptl->pending.lock);
1428
1429 if (resub)
1430 ssh_ptl_tx_wakeup_packet(ptl);
1431}
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450void ssh_ptl_cancel(struct ssh_packet *p)
1451{
1452 if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
1453 return;
1454
1455 trace_ssam_packet_cancel(p);
1456
1457
1458
1459
1460
1461
1462 if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
1463 return;
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478 if (READ_ONCE(p->ptl)) {
1479 ssh_ptl_remove_and_complete(p, -ECANCELED);
1480
1481 if (atomic_read(&p->ptl->pending.count) < SSH_PTL_MAX_PENDING)
1482 ssh_ptl_tx_wakeup_packet(p->ptl);
1483
1484 } else if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
1485 __ssh_ptl_complete(p, -ECANCELED);
1486 }
1487}
1488
1489
1490static ktime_t ssh_packet_get_expiration(struct ssh_packet *p, ktime_t timeout)
1491{
1492 lockdep_assert_held(&p->ptl->pending.lock);
1493
1494 if (p->timestamp != KTIME_MAX)
1495 return ktime_add(p->timestamp, timeout);
1496 else
1497 return KTIME_MAX;
1498}
1499
1500static void ssh_ptl_timeout_reap(struct work_struct *work)
1501{
1502 struct ssh_ptl *ptl = to_ssh_ptl(work, rtx_timeout.reaper.work);
1503 struct ssh_packet *p, *n;
1504 LIST_HEAD(claimed);
1505 ktime_t now = ktime_get_coarse_boottime();
1506 ktime_t timeout = ptl->rtx_timeout.timeout;
1507 ktime_t next = KTIME_MAX;
1508 bool resub = false;
1509 int status;
1510
1511 trace_ssam_ptl_timeout_reap(atomic_read(&ptl->pending.count));
1512
1513
1514
1515
1516
1517 spin_lock(&ptl->rtx_timeout.lock);
1518 ptl->rtx_timeout.expires = KTIME_MAX;
1519 spin_unlock(&ptl->rtx_timeout.lock);
1520
1521 spin_lock(&ptl->pending.lock);
1522
1523 list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
1524 ktime_t expires = ssh_packet_get_expiration(p, timeout);
1525
1526
1527
1528
1529
1530 if (ktime_after(expires, now)) {
1531 next = ktime_before(expires, next) ? expires : next;
1532 continue;
1533 }
1534
1535 trace_ssam_packet_timeout(p);
1536
1537 status = __ssh_ptl_resubmit(p);
1538
1539
1540
1541
1542
1543
1544 if (!status)
1545 resub = true;
1546
1547
1548 if (status != -ECANCELED)
1549 continue;
1550
1551
1552
1553
1554
1555
1556
1557 if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
1558 continue;
1559
1560
1561
1562
1563
1564
1565
1566
1567 clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
1568
1569 atomic_dec(&ptl->pending.count);
1570 list_move_tail(&p->pending_node, &claimed);
1571 }
1572
1573 spin_unlock(&ptl->pending.lock);
1574
1575
1576 list_for_each_entry_safe(p, n, &claimed, pending_node) {
1577 if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
1578 ssh_ptl_queue_remove(p);
1579 __ssh_ptl_complete(p, -ETIMEDOUT);
1580 }
1581
1582
1583
1584
1585
1586 list_del(&p->pending_node);
1587 ssh_packet_put(p);
1588 }
1589
1590
1591 next = max(next, ktime_add(now, SSH_PTL_PACKET_TIMEOUT_RESOLUTION));
1592 if (next != KTIME_MAX)
1593 ssh_ptl_timeout_reaper_mod(ptl, now, next);
1594
1595 if (resub)
1596 ssh_ptl_tx_wakeup_packet(ptl);
1597}
1598
1599static bool ssh_ptl_rx_retransmit_check(struct ssh_ptl *ptl, u8 seq)
1600{
1601 int i;
1602
1603
1604
1605
1606
1607 for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++) {
1608 if (likely(ptl->rx.blocked.seqs[i] != seq))
1609 continue;
1610
1611 ptl_dbg(ptl, "ptl: ignoring repeated data packet\n");
1612 return true;
1613 }
1614
1615
1616 ptl->rx.blocked.seqs[ptl->rx.blocked.offset] = seq;
1617 ptl->rx.blocked.offset = (ptl->rx.blocked.offset + 1)
1618 % ARRAY_SIZE(ptl->rx.blocked.seqs);
1619
1620 return false;
1621}
1622
1623static void ssh_ptl_rx_dataframe(struct ssh_ptl *ptl,
1624 const struct ssh_frame *frame,
1625 const struct ssam_span *payload)
1626{
1627 if (ssh_ptl_rx_retransmit_check(ptl, frame->seq))
1628 return;
1629
1630 ptl->ops.data_received(ptl, payload);
1631}
1632
1633static void ssh_ptl_send_ack(struct ssh_ptl *ptl, u8 seq)
1634{
1635 struct ssh_packet *packet;
1636 struct ssam_span buf;
1637 struct msgbuf msgb;
1638 int status;
1639
1640 status = ssh_ctrl_packet_alloc(&packet, &buf, GFP_KERNEL);
1641 if (status) {
1642 ptl_err(ptl, "ptl: failed to allocate ACK packet\n");
1643 return;
1644 }
1645
1646 ssh_packet_init(packet, 0, SSH_PACKET_PRIORITY(ACK, 0),
1647 &ssh_ptl_ctrl_packet_ops);
1648
1649 msgb_init(&msgb, buf.ptr, buf.len);
1650 msgb_push_ack(&msgb, seq);
1651 ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
1652
1653 ssh_ptl_submit(ptl, packet);
1654 ssh_packet_put(packet);
1655}
1656
1657static void ssh_ptl_send_nak(struct ssh_ptl *ptl)
1658{
1659 struct ssh_packet *packet;
1660 struct ssam_span buf;
1661 struct msgbuf msgb;
1662 int status;
1663
1664 status = ssh_ctrl_packet_alloc(&packet, &buf, GFP_KERNEL);
1665 if (status) {
1666 ptl_err(ptl, "ptl: failed to allocate NAK packet\n");
1667 return;
1668 }
1669
1670 ssh_packet_init(packet, 0, SSH_PACKET_PRIORITY(NAK, 0),
1671 &ssh_ptl_ctrl_packet_ops);
1672
1673 msgb_init(&msgb, buf.ptr, buf.len);
1674 msgb_push_nak(&msgb);
1675 ssh_packet_set_data(packet, msgb.begin, msgb_bytes_used(&msgb));
1676
1677 ssh_ptl_submit(ptl, packet);
1678 ssh_packet_put(packet);
1679}
1680
1681static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct ssam_span *source)
1682{
1683 struct ssh_frame *frame;
1684 struct ssam_span payload;
1685 struct ssam_span aligned;
1686 bool syn_found;
1687 int status;
1688
1689
1690 ssh_ptl_rx_inject_invalid_syn(ptl, source);
1691
1692
1693 syn_found = sshp_find_syn(source, &aligned);
1694
1695 if (unlikely(aligned.ptr != source->ptr)) {
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713 ptl_warn(ptl, "rx: parser: invalid start of frame, skipping\n");
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733 ssh_ptl_send_nak(ptl);
1734 }
1735
1736 if (unlikely(!syn_found))
1737 return aligned.ptr - source->ptr;
1738
1739
1740 ssh_ptl_rx_inject_invalid_data(ptl, &aligned);
1741
1742
1743 status = sshp_parse_frame(&ptl->serdev->dev, &aligned, &frame, &payload,
1744 SSH_PTL_RX_BUF_LEN);
1745 if (status)
1746 return aligned.ptr - source->ptr + sizeof(u16);
1747 if (!frame)
1748 return aligned.ptr - source->ptr;
1749
1750 trace_ssam_rx_frame_received(frame);
1751
1752 switch (frame->type) {
1753 case SSH_FRAME_TYPE_ACK:
1754 ssh_ptl_acknowledge(ptl, frame->seq);
1755 break;
1756
1757 case SSH_FRAME_TYPE_NAK:
1758 ssh_ptl_resubmit_pending(ptl);
1759 break;
1760
1761 case SSH_FRAME_TYPE_DATA_SEQ:
1762 ssh_ptl_send_ack(ptl, frame->seq);
1763 fallthrough;
1764
1765 case SSH_FRAME_TYPE_DATA_NSQ:
1766 ssh_ptl_rx_dataframe(ptl, frame, &payload);
1767 break;
1768
1769 default:
1770 ptl_warn(ptl, "ptl: received frame with unknown type %#04x\n",
1771 frame->type);
1772 break;
1773 }
1774
1775 return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(payload.len);
1776}
1777
1778static int ssh_ptl_rx_threadfn(void *data)
1779{
1780 struct ssh_ptl *ptl = data;
1781
1782 while (true) {
1783 struct ssam_span span;
1784 size_t offs = 0;
1785 size_t n;
1786
1787 wait_event_interruptible(ptl->rx.wq,
1788 !kfifo_is_empty(&ptl->rx.fifo) ||
1789 kthread_should_stop());
1790 if (kthread_should_stop())
1791 break;
1792
1793
1794 n = sshp_buf_read_from_fifo(&ptl->rx.buf, &ptl->rx.fifo);
1795
1796 ptl_dbg(ptl, "rx: received data (size: %zu)\n", n);
1797 print_hex_dump_debug("rx: ", DUMP_PREFIX_OFFSET, 16, 1,
1798 ptl->rx.buf.ptr + ptl->rx.buf.len - n,
1799 n, false);
1800
1801
1802 while (offs < ptl->rx.buf.len) {
1803 sshp_buf_span_from(&ptl->rx.buf, offs, &span);
1804 n = ssh_ptl_rx_eval(ptl, &span);
1805 if (n == 0)
1806 break;
1807
1808 offs += n;
1809 }
1810
1811
1812 sshp_buf_drop(&ptl->rx.buf, offs);
1813 }
1814
1815 return 0;
1816}
1817
1818static void ssh_ptl_rx_wakeup(struct ssh_ptl *ptl)
1819{
1820 wake_up(&ptl->rx.wq);
1821}
1822
1823
1824
1825
1826
1827
1828
1829int ssh_ptl_rx_start(struct ssh_ptl *ptl)
1830{
1831 if (ptl->rx.thread)
1832 return 0;
1833
1834 ptl->rx.thread = kthread_run(ssh_ptl_rx_threadfn, ptl,
1835 "ssam_serial_hub-rx");
1836 if (IS_ERR(ptl->rx.thread))
1837 return PTR_ERR(ptl->rx.thread);
1838
1839 return 0;
1840}
1841
1842
1843
1844
1845
1846
1847
1848int ssh_ptl_rx_stop(struct ssh_ptl *ptl)
1849{
1850 int status = 0;
1851
1852 if (ptl->rx.thread) {
1853 status = kthread_stop(ptl->rx.thread);
1854 ptl->rx.thread = NULL;
1855 }
1856
1857 return status;
1858}
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
1875{
1876 int used;
1877
1878 if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
1879 return -ESHUTDOWN;
1880
1881 used = kfifo_in(&ptl->rx.fifo, buf, n);
1882 if (used)
1883 ssh_ptl_rx_wakeup(ptl);
1884
1885 return used;
1886}
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901void ssh_ptl_shutdown(struct ssh_ptl *ptl)
1902{
1903 LIST_HEAD(complete_q);
1904 LIST_HEAD(complete_p);
1905 struct ssh_packet *p, *n;
1906 int status;
1907
1908
1909 set_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state);
1910
1911
1912
1913
1914
1915
1916
1917
1918 smp_mb__after_atomic();
1919
1920 status = ssh_ptl_rx_stop(ptl);
1921 if (status)
1922 ptl_err(ptl, "ptl: failed to stop receiver thread\n");
1923
1924 status = ssh_ptl_tx_stop(ptl);
1925 if (status)
1926 ptl_err(ptl, "ptl: failed to stop transmitter thread\n");
1927
1928 cancel_delayed_work_sync(&ptl->rtx_timeout.reaper);
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951 spin_lock(&ptl->queue.lock);
1952 list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
1953 set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
1954
1955 smp_mb__before_atomic();
1956 clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
1957
1958 list_move_tail(&p->queue_node, &complete_q);
1959 }
1960 spin_unlock(&ptl->queue.lock);
1961
1962
1963 spin_lock(&ptl->pending.lock);
1964 list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
1965 set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
1966
1967 smp_mb__before_atomic();
1968 clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
1969
1970 list_move_tail(&p->pending_node, &complete_q);
1971 }
1972 atomic_set(&ptl->pending.count, 0);
1973 spin_unlock(&ptl->pending.lock);
1974
1975
1976 list_for_each_entry(p, &complete_q, queue_node) {
1977 if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
1978 __ssh_ptl_complete(p, -ESHUTDOWN);
1979
1980 ssh_packet_put(p);
1981 }
1982
1983
1984 list_for_each_entry(p, &complete_p, pending_node) {
1985 if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
1986 __ssh_ptl_complete(p, -ESHUTDOWN);
1987
1988 ssh_packet_put(p);
1989 }
1990
1991
1992
1993
1994
1995}
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
2011 struct ssh_ptl_ops *ops)
2012{
2013 int i, status;
2014
2015 ptl->serdev = serdev;
2016 ptl->state = 0;
2017
2018 spin_lock_init(&ptl->queue.lock);
2019 INIT_LIST_HEAD(&ptl->queue.head);
2020
2021 spin_lock_init(&ptl->pending.lock);
2022 INIT_LIST_HEAD(&ptl->pending.head);
2023 atomic_set_release(&ptl->pending.count, 0);
2024
2025 ptl->tx.thread = NULL;
2026 atomic_set(&ptl->tx.running, 0);
2027 init_completion(&ptl->tx.thread_cplt_pkt);
2028 init_completion(&ptl->tx.thread_cplt_tx);
2029 init_waitqueue_head(&ptl->tx.packet_wq);
2030
2031 ptl->rx.thread = NULL;
2032 init_waitqueue_head(&ptl->rx.wq);
2033
2034 spin_lock_init(&ptl->rtx_timeout.lock);
2035 ptl->rtx_timeout.timeout = SSH_PTL_PACKET_TIMEOUT;
2036 ptl->rtx_timeout.expires = KTIME_MAX;
2037 INIT_DELAYED_WORK(&ptl->rtx_timeout.reaper, ssh_ptl_timeout_reap);
2038
2039 ptl->ops = *ops;
2040
2041
2042 for (i = 0; i < ARRAY_SIZE(ptl->rx.blocked.seqs); i++)
2043 ptl->rx.blocked.seqs[i] = U16_MAX;
2044 ptl->rx.blocked.offset = 0;
2045
2046 status = kfifo_alloc(&ptl->rx.fifo, SSH_PTL_RX_FIFO_LEN, GFP_KERNEL);
2047 if (status)
2048 return status;
2049
2050 status = sshp_buf_alloc(&ptl->rx.buf, SSH_PTL_RX_BUF_LEN, GFP_KERNEL);
2051 if (status)
2052 kfifo_free(&ptl->rx.fifo);
2053
2054 return status;
2055}
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066void ssh_ptl_destroy(struct ssh_ptl *ptl)
2067{
2068 kfifo_free(&ptl->rx.fifo);
2069 sshp_buf_free(&ptl->rx.buf);
2070}
2071