1
2
3
4
5
6
7
8#include <asm/unaligned.h>
9#include <linux/atomic.h>
10#include <linux/completion.h>
11#include <linux/error-injection.h>
12#include <linux/ktime.h>
13#include <linux/limits.h>
14#include <linux/list.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/types.h>
18#include <linux/workqueue.h>
19
20#include <linux/surface_aggregator/serial_hub.h>
21#include <linux/surface_aggregator/controller.h>
22
23#include "ssh_packet_layer.h"
24#include "ssh_request_layer.h"
25
26#include "trace.h"
27
28
29
30
31
32
33
34
35
36#define SSH_RTL_REQUEST_TIMEOUT ms_to_ktime(3000)
37
38
39
40
41
42
43
44#define SSH_RTL_REQUEST_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
45
46
47
48
49
50
51
52
53#define SSH_RTL_MAX_PENDING 3
54
55
56
57
58
59
60#define SSH_RTL_TX_BATCH 10
61
62#ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
63
64
65
66
67
68
69
70
71static noinline bool ssh_rtl_should_drop_response(void)
72{
73 return false;
74}
75ALLOW_ERROR_INJECTION(ssh_rtl_should_drop_response, TRUE);
76
77#else
78
79static inline bool ssh_rtl_should_drop_response(void)
80{
81 return false;
82}
83
84#endif
85
86static u16 ssh_request_get_rqid(struct ssh_request *rqst)
87{
88 return get_unaligned_le16(rqst->packet.data.ptr
89 + SSH_MSGOFFSET_COMMAND(rqid));
90}
91
92static u32 ssh_request_get_rqid_safe(struct ssh_request *rqst)
93{
94 if (!rqst->packet.data.ptr)
95 return U32_MAX;
96
97 return ssh_request_get_rqid(rqst);
98}
99
100static void ssh_rtl_queue_remove(struct ssh_request *rqst)
101{
102 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
103
104 spin_lock(&rtl->queue.lock);
105
106 if (!test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state)) {
107 spin_unlock(&rtl->queue.lock);
108 return;
109 }
110
111 list_del(&rqst->node);
112
113 spin_unlock(&rtl->queue.lock);
114 ssh_request_put(rqst);
115}
116
117static bool ssh_rtl_queue_empty(struct ssh_rtl *rtl)
118{
119 bool empty;
120
121 spin_lock(&rtl->queue.lock);
122 empty = list_empty(&rtl->queue.head);
123 spin_unlock(&rtl->queue.lock);
124
125 return empty;
126}
127
128static void ssh_rtl_pending_remove(struct ssh_request *rqst)
129{
130 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
131
132 spin_lock(&rtl->pending.lock);
133
134 if (!test_and_clear_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
135 spin_unlock(&rtl->pending.lock);
136 return;
137 }
138
139 atomic_dec(&rtl->pending.count);
140 list_del(&rqst->node);
141
142 spin_unlock(&rtl->pending.lock);
143
144 ssh_request_put(rqst);
145}
146
147static int ssh_rtl_tx_pending_push(struct ssh_request *rqst)
148{
149 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
150
151 spin_lock(&rtl->pending.lock);
152
153 if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
154 spin_unlock(&rtl->pending.lock);
155 return -EINVAL;
156 }
157
158 if (test_and_set_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
159 spin_unlock(&rtl->pending.lock);
160 return -EALREADY;
161 }
162
163 atomic_inc(&rtl->pending.count);
164 list_add_tail(&ssh_request_get(rqst)->node, &rtl->pending.head);
165
166 spin_unlock(&rtl->pending.lock);
167 return 0;
168}
169
170static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
171{
172 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
173
174 trace_ssam_request_complete(rqst, status);
175
176
177 rtl_dbg_cond(rtl, "rtl: completing request (rqid: %#06x, status: %d)\n",
178 ssh_request_get_rqid_safe(rqst), status);
179
180 rqst->ops->complete(rqst, NULL, NULL, status);
181}
182
183static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
184 const struct ssh_command *cmd,
185 const struct ssam_span *data)
186{
187 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
188
189 trace_ssam_request_complete(rqst, 0);
190
191 rtl_dbg(rtl, "rtl: completing request with response (rqid: %#06x)\n",
192 ssh_request_get_rqid(rqst));
193
194 rqst->ops->complete(rqst, cmd, data, 0);
195}
196
197static bool ssh_rtl_tx_can_process(struct ssh_request *rqst)
198{
199 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
200
201 if (test_bit(SSH_REQUEST_TY_FLUSH_BIT, &rqst->state))
202 return !atomic_read(&rtl->pending.count);
203
204 return atomic_read(&rtl->pending.count) < SSH_RTL_MAX_PENDING;
205}
206
207static struct ssh_request *ssh_rtl_tx_next(struct ssh_rtl *rtl)
208{
209 struct ssh_request *rqst = ERR_PTR(-ENOENT);
210 struct ssh_request *p, *n;
211
212 spin_lock(&rtl->queue.lock);
213
214
215 list_for_each_entry_safe(p, n, &rtl->queue.head, node) {
216 if (unlikely(test_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state)))
217 continue;
218
219 if (!ssh_rtl_tx_can_process(p)) {
220 rqst = ERR_PTR(-EBUSY);
221 break;
222 }
223
224
225 set_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &p->state);
226
227 smp_mb__before_atomic();
228 clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &p->state);
229
230 list_del(&p->node);
231
232 rqst = p;
233 break;
234 }
235
236 spin_unlock(&rtl->queue.lock);
237 return rqst;
238}
239
240static int ssh_rtl_tx_try_process_one(struct ssh_rtl *rtl)
241{
242 struct ssh_request *rqst;
243 int status;
244
245
246 rqst = ssh_rtl_tx_next(rtl);
247 if (IS_ERR(rqst))
248 return PTR_ERR(rqst);
249
250
251 status = ssh_rtl_tx_pending_push(rqst);
252 if (status) {
253 ssh_request_put(rqst);
254 return -EAGAIN;
255 }
256
257
258 status = ssh_ptl_submit(&rtl->ptl, &rqst->packet);
259 if (status == -ESHUTDOWN) {
260
261
262
263
264 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state);
265
266
267
268
269
270
271
272
273
274
275
276 ssh_rtl_pending_remove(rqst);
277 ssh_rtl_complete_with_status(rqst, -ESHUTDOWN);
278
279 ssh_request_put(rqst);
280 return -ESHUTDOWN;
281
282 } else if (status) {
283
284
285
286
287
288
289
290
291
292
293
294
295 WARN_ON(status != -EINVAL);
296
297 ssh_request_put(rqst);
298 return -EAGAIN;
299 }
300
301 ssh_request_put(rqst);
302 return 0;
303}
304
305static bool ssh_rtl_tx_schedule(struct ssh_rtl *rtl)
306{
307 if (atomic_read(&rtl->pending.count) >= SSH_RTL_MAX_PENDING)
308 return false;
309
310 if (ssh_rtl_queue_empty(rtl))
311 return false;
312
313 return schedule_work(&rtl->tx.work);
314}
315
316static void ssh_rtl_tx_work_fn(struct work_struct *work)
317{
318 struct ssh_rtl *rtl = to_ssh_rtl(work, tx.work);
319 unsigned int iterations = SSH_RTL_TX_BATCH;
320 int status;
321
322
323
324
325
326
327 do {
328 status = ssh_rtl_tx_try_process_one(rtl);
329 if (status == -ENOENT || status == -EBUSY)
330 return;
331
332 if (status == -ESHUTDOWN) {
333
334
335
336
337
338 return;
339 }
340
341 WARN_ON(status != 0 && status != -EAGAIN);
342 } while (--iterations);
343
344
345 ssh_rtl_tx_schedule(rtl);
346}
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
362{
363 trace_ssam_request_submit(rqst);
364
365
366
367
368
369
370 if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &rqst->state))
371 if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &rqst->packet.state))
372 return -EINVAL;
373
374 spin_lock(&rtl->queue.lock);
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391 if (cmpxchg(&rqst->packet.ptl, NULL, &rtl->ptl)) {
392 spin_unlock(&rtl->queue.lock);
393 return -EALREADY;
394 }
395
396
397
398
399
400
401
402
403
404
405 smp_mb__after_atomic();
406
407 if (test_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state)) {
408 spin_unlock(&rtl->queue.lock);
409 return -ESHUTDOWN;
410 }
411
412 if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
413 spin_unlock(&rtl->queue.lock);
414 return -EINVAL;
415 }
416
417 set_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
418 list_add_tail(&ssh_request_get(rqst)->node, &rtl->queue.head);
419
420 spin_unlock(&rtl->queue.lock);
421
422 ssh_rtl_tx_schedule(rtl);
423 return 0;
424}
425
426static void ssh_rtl_timeout_reaper_mod(struct ssh_rtl *rtl, ktime_t now,
427 ktime_t expires)
428{
429 unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
430 ktime_t aexp = ktime_add(expires, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION);
431
432 spin_lock(&rtl->rtx_timeout.lock);
433
434
435 if (ktime_before(aexp, rtl->rtx_timeout.expires)) {
436 rtl->rtx_timeout.expires = expires;
437 mod_delayed_work(system_wq, &rtl->rtx_timeout.reaper, delta);
438 }
439
440 spin_unlock(&rtl->rtx_timeout.lock);
441}
442
443static void ssh_rtl_timeout_start(struct ssh_request *rqst)
444{
445 struct ssh_rtl *rtl = ssh_request_rtl(rqst);
446 ktime_t timestamp = ktime_get_coarse_boottime();
447 ktime_t timeout = rtl->rtx_timeout.timeout;
448
449 if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state))
450 return;
451
452
453
454
455
456 WRITE_ONCE(rqst->timestamp, timestamp);
457
458
459
460
461
462 smp_mb__after_atomic();
463
464 ssh_rtl_timeout_reaper_mod(rtl, timestamp, timestamp + timeout);
465}
466
467static void ssh_rtl_complete(struct ssh_rtl *rtl,
468 const struct ssh_command *command,
469 const struct ssam_span *command_data)
470{
471 struct ssh_request *r = NULL;
472 struct ssh_request *p, *n;
473 u16 rqid = get_unaligned_le16(&command->rqid);
474
475 trace_ssam_rx_response_received(command, command_data->len);
476
477
478
479
480
481 spin_lock(&rtl->pending.lock);
482 list_for_each_entry_safe(p, n, &rtl->pending.head, node) {
483
484 if (unlikely(ssh_request_get_rqid(p) != rqid))
485 continue;
486
487
488 if (ssh_rtl_should_drop_response()) {
489 spin_unlock(&rtl->pending.lock);
490
491 trace_ssam_ei_rx_drop_response(p);
492 rtl_info(rtl, "request error injection: dropping response for request %p\n",
493 &p->packet);
494 return;
495 }
496
497
498
499
500
501 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state);
502 set_bit(SSH_REQUEST_SF_RSPRCVD_BIT, &p->state);
503
504 smp_mb__before_atomic();
505 clear_bit(SSH_REQUEST_SF_PENDING_BIT, &p->state);
506
507 atomic_dec(&rtl->pending.count);
508 list_del(&p->node);
509
510 r = p;
511 break;
512 }
513 spin_unlock(&rtl->pending.lock);
514
515 if (!r) {
516 rtl_warn(rtl, "rtl: dropping unexpected command message (rqid = %#06x)\n",
517 rqid);
518 return;
519 }
520
521
522 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state)) {
523 ssh_request_put(r);
524 ssh_rtl_tx_schedule(rtl);
525 return;
526 }
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548 if (!test_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state)) {
549 rtl_err(rtl, "rtl: received response before ACK for request (rqid = %#06x)\n",
550 rqid);
551
552
553
554
555
556
557
558 ssh_rtl_queue_remove(r);
559
560 ssh_rtl_complete_with_status(r, -EREMOTEIO);
561 ssh_request_put(r);
562
563 ssh_rtl_tx_schedule(rtl);
564 return;
565 }
566
567
568
569
570
571
572
573
574
575 ssh_rtl_complete_with_rsp(r, command, command_data);
576 ssh_request_put(r);
577
578 ssh_rtl_tx_schedule(rtl);
579}
580
581static bool ssh_rtl_cancel_nonpending(struct ssh_request *r)
582{
583 struct ssh_rtl *rtl;
584 unsigned long flags, fixed;
585 bool remove;
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607 fixed = READ_ONCE(r->state) & SSH_REQUEST_FLAGS_TY_MASK;
608 flags = cmpxchg(&r->state, fixed, SSH_REQUEST_SF_LOCKED_BIT);
609
610
611
612
613
614
615
616
617 smp_mb__after_atomic();
618
619 if (flags == fixed && !READ_ONCE(r->packet.ptl)) {
620 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
621 return true;
622
623 ssh_rtl_complete_with_status(r, -ECANCELED);
624 return true;
625 }
626
627 rtl = ssh_request_rtl(r);
628 spin_lock(&rtl->queue.lock);
629
630
631
632
633
634
635
636
637 remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
638 if (!remove) {
639 spin_unlock(&rtl->queue.lock);
640 return false;
641 }
642
643 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
644 list_del(&r->node);
645
646 spin_unlock(&rtl->queue.lock);
647
648 ssh_request_put(r);
649
650 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
651 return true;
652
653 ssh_rtl_complete_with_status(r, -ECANCELED);
654 return true;
655}
656
657static bool ssh_rtl_cancel_pending(struct ssh_request *r)
658{
659
660 if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
661 return true;
662
663
664
665
666
667
668
669
670
671
672
673
674 if (!READ_ONCE(r->packet.ptl)) {
675 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
676 return true;
677
678 ssh_rtl_complete_with_status(r, -ECANCELED);
679 return true;
680 }
681
682
683
684
685
686
687 ssh_ptl_cancel(&r->packet);
688
689
690
691
692
693
694
695 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
696 return true;
697
698 ssh_rtl_queue_remove(r);
699 ssh_rtl_pending_remove(r);
700 ssh_rtl_complete_with_status(r, -ECANCELED);
701
702 return true;
703}
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
729{
730 struct ssh_rtl *rtl;
731 bool canceled;
732
733 if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
734 return true;
735
736 trace_ssam_request_cancel(rqst);
737
738 if (pending)
739 canceled = ssh_rtl_cancel_pending(rqst);
740 else
741 canceled = ssh_rtl_cancel_nonpending(rqst);
742
743
744 rtl = ssh_request_rtl(rqst);
745 if (canceled && rtl)
746 ssh_rtl_tx_schedule(rtl);
747
748 return canceled;
749}
750
751static void ssh_rtl_packet_callback(struct ssh_packet *p, int status)
752{
753 struct ssh_request *r = to_ssh_request(p);
754
755 if (unlikely(status)) {
756 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
757
758 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
759 return;
760
761
762
763
764
765
766
767
768 ssh_rtl_queue_remove(r);
769 ssh_rtl_pending_remove(r);
770 ssh_rtl_complete_with_status(r, status);
771
772 ssh_rtl_tx_schedule(ssh_request_rtl(r));
773 return;
774 }
775
776
777 set_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state);
778
779 smp_mb__before_atomic();
780 clear_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &r->state);
781
782
783 if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &r->state)) {
784
785
786
787
788 ssh_rtl_timeout_start(r);
789 return;
790 }
791
792
793
794
795
796
797
798
799
800 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
801 if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
802 return;
803
804 ssh_rtl_pending_remove(r);
805 ssh_rtl_complete_with_status(r, 0);
806
807 ssh_rtl_tx_schedule(ssh_request_rtl(r));
808}
809
810static ktime_t ssh_request_get_expiration(struct ssh_request *r, ktime_t timeout)
811{
812 ktime_t timestamp = READ_ONCE(r->timestamp);
813
814 if (timestamp != KTIME_MAX)
815 return ktime_add(timestamp, timeout);
816 else
817 return KTIME_MAX;
818}
819
820static void ssh_rtl_timeout_reap(struct work_struct *work)
821{
822 struct ssh_rtl *rtl = to_ssh_rtl(work, rtx_timeout.reaper.work);
823 struct ssh_request *r, *n;
824 LIST_HEAD(claimed);
825 ktime_t now = ktime_get_coarse_boottime();
826 ktime_t timeout = rtl->rtx_timeout.timeout;
827 ktime_t next = KTIME_MAX;
828
829 trace_ssam_rtl_timeout_reap(atomic_read(&rtl->pending.count));
830
831
832
833
834
835 spin_lock(&rtl->rtx_timeout.lock);
836 rtl->rtx_timeout.expires = KTIME_MAX;
837 spin_unlock(&rtl->rtx_timeout.lock);
838
839 spin_lock(&rtl->pending.lock);
840 list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
841 ktime_t expires = ssh_request_get_expiration(r, timeout);
842
843
844
845
846
847 if (ktime_after(expires, now)) {
848 next = ktime_before(expires, next) ? expires : next;
849 continue;
850 }
851
852
853 if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
854 continue;
855
856
857
858
859
860
861
862
863 clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
864
865 atomic_dec(&rtl->pending.count);
866 list_move_tail(&r->node, &claimed);
867 }
868 spin_unlock(&rtl->pending.lock);
869
870
871 list_for_each_entry_safe(r, n, &claimed, node) {
872 trace_ssam_request_timeout(r);
873
874
875
876
877
878
879 if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
880 ssh_rtl_complete_with_status(r, -ETIMEDOUT);
881
882
883
884
885
886 list_del(&r->node);
887 ssh_request_put(r);
888 }
889
890
891 next = max(next, ktime_add(now, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION));
892 if (next != KTIME_MAX)
893 ssh_rtl_timeout_reaper_mod(rtl, now, next);
894
895 ssh_rtl_tx_schedule(rtl);
896}
897
898static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
899 const struct ssam_span *data)
900{
901 trace_ssam_rx_event_received(cmd, data->len);
902
903 rtl_dbg(rtl, "rtl: handling event (rqid: %#06x)\n",
904 get_unaligned_le16(&cmd->rqid));
905
906 rtl->ops.handle_event(rtl, cmd, data);
907}
908
909static void ssh_rtl_rx_command(struct ssh_ptl *p, const struct ssam_span *data)
910{
911 struct ssh_rtl *rtl = to_ssh_rtl(p, ptl);
912 struct device *dev = &p->serdev->dev;
913 struct ssh_command *command;
914 struct ssam_span command_data;
915
916 if (sshp_parse_command(dev, data, &command, &command_data))
917 return;
918
919 if (ssh_rqid_is_event(get_unaligned_le16(&command->rqid)))
920 ssh_rtl_rx_event(rtl, command, &command_data);
921 else
922 ssh_rtl_complete(rtl, command, &command_data);
923}
924
925static void ssh_rtl_rx_data(struct ssh_ptl *p, const struct ssam_span *data)
926{
927 if (!data->len) {
928 ptl_err(p, "rtl: rx: no data frame payload\n");
929 return;
930 }
931
932 switch (data->ptr[0]) {
933 case SSH_PLD_TYPE_CMD:
934 ssh_rtl_rx_command(p, data);
935 break;
936
937 default:
938 ptl_err(p, "rtl: rx: unknown frame payload type (type: %#04x)\n",
939 data->ptr[0]);
940 break;
941 }
942}
943
944static void ssh_rtl_packet_release(struct ssh_packet *p)
945{
946 struct ssh_request *rqst;
947
948 rqst = to_ssh_request(p);
949 rqst->ops->release(rqst);
950}
951
952static const struct ssh_packet_ops ssh_rtl_packet_ops = {
953 .complete = ssh_rtl_packet_callback,
954 .release = ssh_rtl_packet_release,
955};
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
971 const struct ssh_request_ops *ops)
972{
973 unsigned long type = BIT(SSH_PACKET_TY_BLOCKING_BIT);
974
975
976 if (flags & SSAM_REQUEST_UNSEQUENCED && flags & SSAM_REQUEST_HAS_RESPONSE)
977 return -EINVAL;
978
979 if (!(flags & SSAM_REQUEST_UNSEQUENCED))
980 type |= BIT(SSH_PACKET_TY_SEQUENCED_BIT);
981
982 ssh_packet_init(&rqst->packet, type, SSH_PACKET_PRIORITY(DATA, 0),
983 &ssh_rtl_packet_ops);
984
985 INIT_LIST_HEAD(&rqst->node);
986
987 rqst->state = 0;
988 if (flags & SSAM_REQUEST_HAS_RESPONSE)
989 rqst->state |= BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
990
991 rqst->timestamp = KTIME_MAX;
992 rqst->ops = ops;
993
994 return 0;
995}
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
1011 const struct ssh_rtl_ops *ops)
1012{
1013 struct ssh_ptl_ops ptl_ops;
1014 int status;
1015
1016 ptl_ops.data_received = ssh_rtl_rx_data;
1017
1018 status = ssh_ptl_init(&rtl->ptl, serdev, &ptl_ops);
1019 if (status)
1020 return status;
1021
1022 spin_lock_init(&rtl->queue.lock);
1023 INIT_LIST_HEAD(&rtl->queue.head);
1024
1025 spin_lock_init(&rtl->pending.lock);
1026 INIT_LIST_HEAD(&rtl->pending.head);
1027 atomic_set_release(&rtl->pending.count, 0);
1028
1029 INIT_WORK(&rtl->tx.work, ssh_rtl_tx_work_fn);
1030
1031 spin_lock_init(&rtl->rtx_timeout.lock);
1032 rtl->rtx_timeout.timeout = SSH_RTL_REQUEST_TIMEOUT;
1033 rtl->rtx_timeout.expires = KTIME_MAX;
1034 INIT_DELAYED_WORK(&rtl->rtx_timeout.reaper, ssh_rtl_timeout_reap);
1035
1036 rtl->ops = *ops;
1037
1038 return 0;
1039}
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050void ssh_rtl_destroy(struct ssh_rtl *rtl)
1051{
1052 ssh_ptl_destroy(&rtl->ptl);
1053}
1054
1055
1056
1057
1058
1059
1060
1061int ssh_rtl_start(struct ssh_rtl *rtl)
1062{
1063 int status;
1064
1065 status = ssh_ptl_tx_start(&rtl->ptl);
1066 if (status)
1067 return status;
1068
1069 ssh_rtl_tx_schedule(rtl);
1070
1071 status = ssh_ptl_rx_start(&rtl->ptl);
1072 if (status) {
1073 ssh_rtl_flush(rtl, msecs_to_jiffies(5000));
1074 ssh_ptl_tx_stop(&rtl->ptl);
1075 return status;
1076 }
1077
1078 return 0;
1079}
1080
1081struct ssh_flush_request {
1082 struct ssh_request base;
1083 struct completion completion;
1084 int status;
1085};
1086
1087static void ssh_rtl_flush_request_complete(struct ssh_request *r,
1088 const struct ssh_command *cmd,
1089 const struct ssam_span *data,
1090 int status)
1091{
1092 struct ssh_flush_request *rqst;
1093
1094 rqst = container_of(r, struct ssh_flush_request, base);
1095 rqst->status = status;
1096}
1097
1098static void ssh_rtl_flush_request_release(struct ssh_request *r)
1099{
1100 struct ssh_flush_request *rqst;
1101
1102 rqst = container_of(r, struct ssh_flush_request, base);
1103 complete_all(&rqst->completion);
1104}
1105
1106static const struct ssh_request_ops ssh_rtl_flush_request_ops = {
1107 .complete = ssh_rtl_flush_request_complete,
1108 .release = ssh_rtl_flush_request_release,
1109};
1110
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
1136
1137
1138
1139int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout)
1140{
1141 const unsigned int init_flags = SSAM_REQUEST_UNSEQUENCED;
1142 struct ssh_flush_request rqst;
1143 int status;
1144
1145 ssh_request_init(&rqst.base, init_flags, &ssh_rtl_flush_request_ops);
1146 rqst.base.packet.state |= BIT(SSH_PACKET_TY_FLUSH_BIT);
1147 rqst.base.packet.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
1148 rqst.base.state |= BIT(SSH_REQUEST_TY_FLUSH_BIT);
1149
1150 init_completion(&rqst.completion);
1151
1152 status = ssh_rtl_submit(rtl, &rqst.base);
1153 if (status)
1154 return status;
1155
1156 ssh_request_put(&rqst.base);
1157
1158 if (!wait_for_completion_timeout(&rqst.completion, timeout)) {
1159 ssh_rtl_cancel(&rqst.base, true);
1160 wait_for_completion(&rqst.completion);
1161 }
1162
1163 WARN_ON(rqst.status != 0 && rqst.status != -ECANCELED &&
1164 rqst.status != -ESHUTDOWN && rqst.status != -EINTR);
1165
1166 return rqst.status == -ECANCELED ? -ETIMEDOUT : rqst.status;
1167}
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182void ssh_rtl_shutdown(struct ssh_rtl *rtl)
1183{
1184 struct ssh_request *r, *n;
1185 LIST_HEAD(claimed);
1186 int pending;
1187
1188 set_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state);
1189
1190
1191
1192
1193
1194
1195 smp_mb__after_atomic();
1196
1197
1198 spin_lock(&rtl->queue.lock);
1199 list_for_each_entry_safe(r, n, &rtl->queue.head, node) {
1200 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
1201
1202 smp_mb__before_atomic();
1203 clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
1204
1205 list_move_tail(&r->node, &claimed);
1206 }
1207 spin_unlock(&rtl->queue.lock);
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219 cancel_work_sync(&rtl->tx.work);
1220 ssh_ptl_shutdown(&rtl->ptl);
1221 cancel_delayed_work_sync(&rtl->rtx_timeout.reaper);
1222
1223
1224
1225
1226
1227
1228
1229 pending = atomic_read(&rtl->pending.count);
1230 if (WARN_ON(pending)) {
1231 spin_lock(&rtl->pending.lock);
1232 list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
1233 set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
1234
1235 smp_mb__before_atomic();
1236 clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
1237
1238 list_move_tail(&r->node, &claimed);
1239 }
1240 spin_unlock(&rtl->pending.lock);
1241 }
1242
1243
1244 list_for_each_entry_safe(r, n, &claimed, node) {
1245
1246
1247
1248
1249 if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
1250 ssh_rtl_complete_with_status(r, -ESHUTDOWN);
1251
1252
1253
1254
1255
1256 list_del(&r->node);
1257 ssh_request_put(r);
1258 }
1259}
1260