0001
0002
0003
0004
0005
0006
0007
0008 #include <asm/unaligned.h>
0009 #include <linux/atomic.h>
0010 #include <linux/error-injection.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/kfifo.h>
0013 #include <linux/kref.h>
0014 #include <linux/kthread.h>
0015 #include <linux/ktime.h>
0016 #include <linux/limits.h>
0017 #include <linux/list.h>
0018 #include <linux/lockdep.h>
0019 #include <linux/serdev.h>
0020 #include <linux/slab.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/workqueue.h>
0023
0024 #include <linux/surface_aggregator/serial_hub.h>
0025
0026 #include "ssh_msgb.h"
0027 #include "ssh_packet_layer.h"
0028 #include "ssh_parser.h"
0029
0030 #include "trace.h"
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 #define SSH_PTL_MAX_PACKET_TRIES 3
0185
0186
0187
0188
0189
0190
0191
0192
0193 #define SSH_PTL_TX_TIMEOUT HZ
0194
0195
0196
0197
0198
0199
0200
0201 #define SSH_PTL_PACKET_TIMEOUT ms_to_ktime(1000)
0202
0203
0204
0205
0206
0207
0208
0209 #define SSH_PTL_PACKET_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
0210
0211
0212
0213
0214
0215
0216
0217
0218 #define SSH_PTL_MAX_PENDING 1
0219
0220
0221
0222
0223 #define SSH_PTL_RX_BUF_LEN 4096
0224
0225
0226
0227
0228 #define SSH_PTL_RX_FIFO_LEN 4096
0229
0230 #ifdef CONFIG_SURFACE_AGGREGATOR_ERROR_INJECTION
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 static noinline bool ssh_ptl_should_drop_ack_packet(void)
0243 {
0244 return false;
0245 }
0246 ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_ack_packet, TRUE);
0247
0248
0249
0250
0251
0252
0253
0254
0255 static noinline bool ssh_ptl_should_drop_nak_packet(void)
0256 {
0257 return false;
0258 }
0259 ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_nak_packet, TRUE);
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269 static noinline bool ssh_ptl_should_drop_dsq_packet(void)
0270 {
0271 return false;
0272 }
0273 ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_dsq_packet, TRUE);
0274
0275
0276
0277
0278
0279
0280
0281 static noinline int ssh_ptl_should_fail_write(void)
0282 {
0283 return 0;
0284 }
0285 ALLOW_ERROR_INJECTION(ssh_ptl_should_fail_write, ERRNO);
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297 static noinline bool ssh_ptl_should_corrupt_tx_data(void)
0298 {
0299 return false;
0300 }
0301 ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_tx_data, TRUE);
0302
0303
0304
0305
0306
0307
0308
0309
0310 static noinline bool ssh_ptl_should_corrupt_rx_syn(void)
0311 {
0312 return false;
0313 }
0314 ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_syn, TRUE);
0315
0316
0317
0318
0319
0320
0321
0322
0323 static noinline bool ssh_ptl_should_corrupt_rx_data(void)
0324 {
0325 return false;
0326 }
0327 ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_data, TRUE);
0328
0329 static bool __ssh_ptl_should_drop_ack_packet(struct ssh_packet *packet)
0330 {
0331 if (likely(!ssh_ptl_should_drop_ack_packet()))
0332 return false;
0333
0334 trace_ssam_ei_tx_drop_ack_packet(packet);
0335 ptl_info(packet->ptl, "packet error injection: dropping ACK packet %p\n",
0336 packet);
0337
0338 return true;
0339 }
0340
0341 static bool __ssh_ptl_should_drop_nak_packet(struct ssh_packet *packet)
0342 {
0343 if (likely(!ssh_ptl_should_drop_nak_packet()))
0344 return false;
0345
0346 trace_ssam_ei_tx_drop_nak_packet(packet);
0347 ptl_info(packet->ptl, "packet error injection: dropping NAK packet %p\n",
0348 packet);
0349
0350 return true;
0351 }
0352
0353 static bool __ssh_ptl_should_drop_dsq_packet(struct ssh_packet *packet)
0354 {
0355 if (likely(!ssh_ptl_should_drop_dsq_packet()))
0356 return false;
0357
0358 trace_ssam_ei_tx_drop_dsq_packet(packet);
0359 ptl_info(packet->ptl,
0360 "packet error injection: dropping sequenced data packet %p\n",
0361 packet);
0362
0363 return true;
0364 }
0365
0366 static bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
0367 {
0368
0369 if (!packet->data.ptr || !packet->data.len)
0370 return false;
0371
0372 switch (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)]) {
0373 case SSH_FRAME_TYPE_ACK:
0374 return __ssh_ptl_should_drop_ack_packet(packet);
0375
0376 case SSH_FRAME_TYPE_NAK:
0377 return __ssh_ptl_should_drop_nak_packet(packet);
0378
0379 case SSH_FRAME_TYPE_DATA_SEQ:
0380 return __ssh_ptl_should_drop_dsq_packet(packet);
0381
0382 default:
0383 return false;
0384 }
0385 }
0386
0387 static int ssh_ptl_write_buf(struct ssh_ptl *ptl, struct ssh_packet *packet,
0388 const unsigned char *buf, size_t count)
0389 {
0390 int status;
0391
0392 status = ssh_ptl_should_fail_write();
0393 if (unlikely(status)) {
0394 trace_ssam_ei_tx_fail_write(packet, status);
0395 ptl_info(packet->ptl,
0396 "packet error injection: simulating transmit error %d, packet %p\n",
0397 status, packet);
0398
0399 return status;
0400 }
0401
0402 return serdev_device_write_buf(ptl->serdev, buf, count);
0403 }
0404
0405 static void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
0406 {
0407
0408 if (!packet->data.ptr || !packet->data.len)
0409 return;
0410
0411
0412 if (packet->data.ptr[SSH_MSGOFFSET_FRAME(type)] != SSH_FRAME_TYPE_DATA_SEQ)
0413 return;
0414
0415 if (likely(!ssh_ptl_should_corrupt_tx_data()))
0416 return;
0417
0418 trace_ssam_ei_tx_corrupt_data(packet);
0419 ptl_info(packet->ptl,
0420 "packet error injection: simulating invalid transmit data on packet %p\n",
0421 packet);
0422
0423
0424
0425
0426
0427
0428 memset(packet->data.ptr, 0xb3, packet->data.len);
0429 }
0430
0431 static void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
0432 struct ssam_span *data)
0433 {
0434 struct ssam_span frame;
0435
0436
0437 if (!sshp_find_syn(data, &frame))
0438 return;
0439
0440 if (likely(!ssh_ptl_should_corrupt_rx_syn()))
0441 return;
0442
0443 trace_ssam_ei_rx_corrupt_syn(data->len);
0444
0445 data->ptr[1] = 0xb3;
0446 }
0447
0448 static void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
0449 struct ssam_span *frame)
0450 {
0451 size_t payload_len, message_len;
0452 struct ssh_frame *sshf;
0453
0454
0455 if (frame->len < SSH_MESSAGE_LENGTH(0))
0456 return;
0457
0458
0459 payload_len = get_unaligned_le16(&frame->ptr[SSH_MSGOFFSET_FRAME(len)]);
0460 message_len = SSH_MESSAGE_LENGTH(payload_len);
0461 if (frame->len < message_len)
0462 return;
0463
0464 if (likely(!ssh_ptl_should_corrupt_rx_data()))
0465 return;
0466
0467 sshf = (struct ssh_frame *)&frame->ptr[SSH_MSGOFFSET_FRAME(type)];
0468 trace_ssam_ei_rx_corrupt_data(sshf);
0469
0470
0471
0472
0473
0474
0475
0476 frame->ptr[frame->len - 2] = ~frame->ptr[frame->len - 2];
0477 }
0478
0479 #else
0480
0481 static inline bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
0482 {
0483 return false;
0484 }
0485
0486 static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
0487 struct ssh_packet *packet,
0488 const unsigned char *buf,
0489 size_t count)
0490 {
0491 return serdev_device_write_buf(ptl->serdev, buf, count);
0492 }
0493
0494 static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
0495 {
0496 }
0497
0498 static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
0499 struct ssam_span *data)
0500 {
0501 }
0502
0503 static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
0504 struct ssam_span *frame)
0505 {
0506 }
0507
0508 #endif
0509
0510 static void __ssh_ptl_packet_release(struct kref *kref)
0511 {
0512 struct ssh_packet *p = container_of(kref, struct ssh_packet, refcnt);
0513
0514 trace_ssam_packet_release(p);
0515
0516 ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
0517 p->ops->release(p);
0518 }
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529 struct ssh_packet *ssh_packet_get(struct ssh_packet *packet)
0530 {
0531 if (packet)
0532 kref_get(&packet->refcnt);
0533 return packet;
0534 }
0535 EXPORT_SYMBOL_GPL(ssh_packet_get);
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547 void ssh_packet_put(struct ssh_packet *packet)
0548 {
0549 if (packet)
0550 kref_put(&packet->refcnt, __ssh_ptl_packet_release);
0551 }
0552 EXPORT_SYMBOL_GPL(ssh_packet_put);
0553
0554 static u8 ssh_packet_get_seq(struct ssh_packet *packet)
0555 {
0556 return packet->data.ptr[SSH_MSGOFFSET_FRAME(seq)];
0557 }
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572 void ssh_packet_init(struct ssh_packet *packet, unsigned long type,
0573 u8 priority, const struct ssh_packet_ops *ops)
0574 {
0575 kref_init(&packet->refcnt);
0576
0577 packet->ptl = NULL;
0578 INIT_LIST_HEAD(&packet->queue_node);
0579 INIT_LIST_HEAD(&packet->pending_node);
0580
0581 packet->state = type & SSH_PACKET_FLAGS_TY_MASK;
0582 packet->priority = priority;
0583 packet->timestamp = KTIME_MAX;
0584
0585 packet->data.ptr = NULL;
0586 packet->data.len = 0;
0587
0588 packet->ops = ops;
0589 }
0590
0591 static struct kmem_cache *ssh_ctrl_packet_cache;
0592
0593
0594
0595
0596 int ssh_ctrl_packet_cache_init(void)
0597 {
0598 const unsigned int size = sizeof(struct ssh_packet) + SSH_MSG_LEN_CTRL;
0599 const unsigned int align = __alignof__(struct ssh_packet);
0600 struct kmem_cache *cache;
0601
0602 cache = kmem_cache_create("ssam_ctrl_packet", size, align, 0, NULL);
0603 if (!cache)
0604 return -ENOMEM;
0605
0606 ssh_ctrl_packet_cache = cache;
0607 return 0;
0608 }
0609
0610
0611
0612
0613 void ssh_ctrl_packet_cache_destroy(void)
0614 {
0615 kmem_cache_destroy(ssh_ctrl_packet_cache);
0616 ssh_ctrl_packet_cache = NULL;
0617 }
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634 static int ssh_ctrl_packet_alloc(struct ssh_packet **packet,
0635 struct ssam_span *buffer, gfp_t flags)
0636 {
0637 *packet = kmem_cache_alloc(ssh_ctrl_packet_cache, flags);
0638 if (!*packet)
0639 return -ENOMEM;
0640
0641 buffer->ptr = (u8 *)(*packet + 1);
0642 buffer->len = SSH_MSG_LEN_CTRL;
0643
0644 trace_ssam_ctrl_packet_alloc(*packet, buffer->len);
0645 return 0;
0646 }
0647
0648
0649
0650
0651
0652 static void ssh_ctrl_packet_free(struct ssh_packet *p)
0653 {
0654 trace_ssam_ctrl_packet_free(p);
0655 kmem_cache_free(ssh_ctrl_packet_cache, p);
0656 }
0657
0658 static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
0659 .complete = NULL,
0660 .release = ssh_ctrl_packet_free,
0661 };
0662
0663 static void ssh_ptl_timeout_reaper_mod(struct ssh_ptl *ptl, ktime_t now,
0664 ktime_t expires)
0665 {
0666 unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
0667 ktime_t aexp = ktime_add(expires, SSH_PTL_PACKET_TIMEOUT_RESOLUTION);
0668
0669 spin_lock(&ptl->rtx_timeout.lock);
0670
0671
0672 if (ktime_before(aexp, ptl->rtx_timeout.expires)) {
0673 ptl->rtx_timeout.expires = expires;
0674 mod_delayed_work(system_wq, &ptl->rtx_timeout.reaper, delta);
0675 }
0676
0677 spin_unlock(&ptl->rtx_timeout.lock);
0678 }
0679
0680
0681 static void ssh_packet_next_try(struct ssh_packet *p)
0682 {
0683 u8 base = ssh_packet_priority_get_base(p->priority);
0684 u8 try = ssh_packet_priority_get_try(p->priority);
0685
0686 lockdep_assert_held(&p->ptl->queue.lock);
0687
0688
0689
0690
0691
0692
0693 WRITE_ONCE(p->priority, __SSH_PACKET_PRIORITY(base, try + 1));
0694 }
0695
0696
0697 static struct list_head *__ssh_ptl_queue_find_entrypoint(struct ssh_packet *p)
0698 {
0699 struct list_head *head;
0700 struct ssh_packet *q;
0701
0702 lockdep_assert_held(&p->ptl->queue.lock);
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717 if (p->priority > SSH_PACKET_PRIORITY(DATA, 0)) {
0718 list_for_each(head, &p->ptl->queue.head) {
0719 q = list_entry(head, struct ssh_packet, queue_node);
0720
0721 if (q->priority < p->priority)
0722 break;
0723 }
0724 } else {
0725 list_for_each_prev(head, &p->ptl->queue.head) {
0726 q = list_entry(head, struct ssh_packet, queue_node);
0727
0728 if (q->priority >= p->priority) {
0729 head = head->next;
0730 break;
0731 }
0732 }
0733 }
0734
0735 return head;
0736 }
0737
0738
0739 static int __ssh_ptl_queue_push(struct ssh_packet *packet)
0740 {
0741 struct ssh_ptl *ptl = packet->ptl;
0742 struct list_head *head;
0743
0744 lockdep_assert_held(&ptl->queue.lock);
0745
0746 if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
0747 return -ESHUTDOWN;
0748
0749
0750 if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state))
0751 return -EINVAL;
0752
0753
0754 if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state))
0755 return -EALREADY;
0756
0757 head = __ssh_ptl_queue_find_entrypoint(packet);
0758
0759 list_add_tail(&ssh_packet_get(packet)->queue_node, head);
0760 return 0;
0761 }
0762
0763 static int ssh_ptl_queue_push(struct ssh_packet *packet)
0764 {
0765 int status;
0766
0767 spin_lock(&packet->ptl->queue.lock);
0768 status = __ssh_ptl_queue_push(packet);
0769 spin_unlock(&packet->ptl->queue.lock);
0770
0771 return status;
0772 }
0773
0774 static void ssh_ptl_queue_remove(struct ssh_packet *packet)
0775 {
0776 struct ssh_ptl *ptl = packet->ptl;
0777
0778 spin_lock(&ptl->queue.lock);
0779
0780 if (!test_and_clear_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
0781 spin_unlock(&ptl->queue.lock);
0782 return;
0783 }
0784
0785 list_del(&packet->queue_node);
0786
0787 spin_unlock(&ptl->queue.lock);
0788 ssh_packet_put(packet);
0789 }
0790
0791 static void ssh_ptl_pending_push(struct ssh_packet *p)
0792 {
0793 struct ssh_ptl *ptl = p->ptl;
0794 const ktime_t timestamp = ktime_get_coarse_boottime();
0795 const ktime_t timeout = ptl->rtx_timeout.timeout;
0796
0797
0798
0799
0800
0801
0802
0803
0804 spin_lock(&ptl->pending.lock);
0805
0806
0807 if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state)) {
0808 spin_unlock(&ptl->pending.lock);
0809 return;
0810 }
0811
0812
0813
0814
0815
0816
0817 p->timestamp = timestamp;
0818
0819
0820 if (!test_and_set_bit(SSH_PACKET_SF_PENDING_BIT, &p->state)) {
0821 atomic_inc(&ptl->pending.count);
0822 list_add_tail(&ssh_packet_get(p)->pending_node, &ptl->pending.head);
0823 }
0824
0825 spin_unlock(&ptl->pending.lock);
0826
0827
0828 ssh_ptl_timeout_reaper_mod(ptl, timestamp, timestamp + timeout);
0829 }
0830
0831 static void ssh_ptl_pending_remove(struct ssh_packet *packet)
0832 {
0833 struct ssh_ptl *ptl = packet->ptl;
0834
0835 spin_lock(&ptl->pending.lock);
0836
0837 if (!test_and_clear_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state)) {
0838 spin_unlock(&ptl->pending.lock);
0839 return;
0840 }
0841
0842 list_del(&packet->pending_node);
0843 atomic_dec(&ptl->pending.count);
0844
0845 spin_unlock(&ptl->pending.lock);
0846
0847 ssh_packet_put(packet);
0848 }
0849
0850
0851 static void __ssh_ptl_complete(struct ssh_packet *p, int status)
0852 {
0853 struct ssh_ptl *ptl = READ_ONCE(p->ptl);
0854
0855 trace_ssam_packet_complete(p, status);
0856 ptl_dbg_cond(ptl, "ptl: completing packet %p (status: %d)\n", p, status);
0857
0858 if (p->ops->complete)
0859 p->ops->complete(p, status);
0860 }
0861
0862 static void ssh_ptl_remove_and_complete(struct ssh_packet *p, int status)
0863 {
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874 if (test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
0875 return;
0876
0877 ssh_ptl_queue_remove(p);
0878 ssh_ptl_pending_remove(p);
0879
0880 __ssh_ptl_complete(p, status);
0881 }
0882
0883 static bool ssh_ptl_tx_can_process(struct ssh_packet *packet)
0884 {
0885 struct ssh_ptl *ptl = packet->ptl;
0886
0887 if (test_bit(SSH_PACKET_TY_FLUSH_BIT, &packet->state))
0888 return !atomic_read(&ptl->pending.count);
0889
0890
0891 if (!test_bit(SSH_PACKET_TY_BLOCKING_BIT, &packet->state))
0892 return true;
0893
0894
0895 if (test_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state))
0896 return true;
0897
0898
0899 return atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING;
0900 }
0901
0902 static struct ssh_packet *ssh_ptl_tx_pop(struct ssh_ptl *ptl)
0903 {
0904 struct ssh_packet *packet = ERR_PTR(-ENOENT);
0905 struct ssh_packet *p, *n;
0906
0907 spin_lock(&ptl->queue.lock);
0908 list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
0909
0910
0911
0912
0913 if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
0914 continue;
0915
0916
0917
0918
0919
0920
0921 if (!ssh_ptl_tx_can_process(p)) {
0922 packet = ERR_PTR(-EBUSY);
0923 break;
0924 }
0925
0926
0927
0928
0929
0930
0931 list_del(&p->queue_node);
0932
0933 set_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state);
0934
0935 smp_mb__before_atomic();
0936 clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
0937
0938
0939
0940
0941
0942
0943
0944
0945 ssh_packet_next_try(p);
0946
0947 packet = p;
0948 break;
0949 }
0950 spin_unlock(&ptl->queue.lock);
0951
0952 return packet;
0953 }
0954
0955 static struct ssh_packet *ssh_ptl_tx_next(struct ssh_ptl *ptl)
0956 {
0957 struct ssh_packet *p;
0958
0959 p = ssh_ptl_tx_pop(ptl);
0960 if (IS_ERR(p))
0961 return p;
0962
0963 if (test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &p->state)) {
0964 ptl_dbg(ptl, "ptl: transmitting sequenced packet %p\n", p);
0965 ssh_ptl_pending_push(p);
0966 } else {
0967 ptl_dbg(ptl, "ptl: transmitting non-sequenced packet %p\n", p);
0968 }
0969
0970 return p;
0971 }
0972
0973 static void ssh_ptl_tx_compl_success(struct ssh_packet *packet)
0974 {
0975 struct ssh_ptl *ptl = packet->ptl;
0976
0977 ptl_dbg(ptl, "ptl: successfully transmitted packet %p\n", packet);
0978
0979
0980 set_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state);
0981
0982 smp_mb__before_atomic();
0983 clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
0984
0985
0986 if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &packet->state)) {
0987 set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
0988 ssh_ptl_remove_and_complete(packet, 0);
0989 }
0990
0991
0992
0993
0994
0995 wake_up_all(&ptl->tx.packet_wq);
0996 }
0997
0998 static void ssh_ptl_tx_compl_error(struct ssh_packet *packet, int status)
0999 {
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
1018 static 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
1034 static 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
1051 static 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
1099 static 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
1138 static 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
1152 int 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
1169 int 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
1193 static 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
1237 static 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
1244 static 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
1312 int 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
1363 static 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
1398 static 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
1450 void 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
1490 static 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
1500 static 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
1599 static 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
1623 static 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
1633 static 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
1657 static 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
1681 static 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
1778 static 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
1818 static void ssh_ptl_rx_wakeup(struct ssh_ptl *ptl)
1819 {
1820 wake_up(&ptl->rx.wq);
1821 }
1822
1823
1824
1825
1826
1827
1828
1829 int 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
1848 int 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
1874 int 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
1901 void 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
2010 int 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
2066 void ssh_ptl_destroy(struct ssh_ptl *ptl)
2067 {
2068 kfifo_free(&ptl->rx.fifo);
2069 sshp_buf_free(&ptl->rx.buf);
2070 }