1 // SPDX-License-Identifier: GPL-2.0-only
3 * shdlc Link Layer Control
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
8 #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/wait.h>
13 #include <linux/slab.h>
14 #include <linux/skbuff.h>
19 SHDLC_DISCONNECTED = 0,
21 SHDLC_NEGOTIATING = 2,
22 SHDLC_HALF_CONNECTED = 3,
27 struct nfc_hci_dev *hdev;
28 xmit_to_drv_t xmit_to_drv;
29 rcv_to_hci_t rcv_to_hci;
31 struct mutex state_mutex;
32 enum shdlc_state state;
35 wait_queue_head_t *connect_wq;
38 struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
40 u8 w; /* window size */
43 struct timer_list t1_timer; /* send ack timeout */
46 struct timer_list t2_timer; /* guard/retransmit timeout */
49 int ns; /* next seq num for send */
50 int nr; /* next expected seq num for receive */
51 int dnr; /* oldest sent unacked seq num */
53 struct sk_buff_head rcv_q;
55 struct sk_buff_head send_q;
56 bool rnr; /* other side is not ready to receive */
58 struct sk_buff_head ack_pending_q;
60 struct work_struct sm_work;
65 llc_failure_t llc_failure;
68 #define SHDLC_LLC_HEAD_ROOM 2
70 #define SHDLC_MAX_WINDOW 4
71 #define SHDLC_SREJ_SUPPORT false
73 #define SHDLC_CONTROL_HEAD_MASK 0xe0
74 #define SHDLC_CONTROL_HEAD_I 0x80
75 #define SHDLC_CONTROL_HEAD_I2 0xa0
76 #define SHDLC_CONTROL_HEAD_S 0xc0
77 #define SHDLC_CONTROL_HEAD_U 0xe0
79 #define SHDLC_CONTROL_NS_MASK 0x38
80 #define SHDLC_CONTROL_NR_MASK 0x07
81 #define SHDLC_CONTROL_TYPE_MASK 0x18
83 #define SHDLC_CONTROL_M_MASK 0x1f
92 enum uframe_modifier {
97 #define SHDLC_CONNECT_VALUE_MS 5
98 #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
99 #define SHDLC_T2_VALUE_MS 300
101 #define SHDLC_DUMP_SKB(info, skb) \
103 pr_debug("%s:\n", info); \
104 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
105 16, 1, skb->data, skb->len, 0); \
108 /* checks x < y <= z modulo 8 */
109 static bool llc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
112 return ((x < y) && (y <= z)) ? true : false;
114 return ((y > x) || (y <= z)) ? true : false;
117 /* checks x <= y < z modulo 8 */
118 static bool llc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
121 return ((x <= y) && (y < z)) ? true : false;
122 else /* x > z -> z+8 > x */
123 return ((y >= x) || (y < z)) ? true : false;
126 static struct sk_buff *llc_shdlc_alloc_skb(const struct llc_shdlc *shdlc,
131 skb = alloc_skb(shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM +
132 shdlc->tx_tailroom + payload_len, GFP_KERNEL);
134 skb_reserve(skb, shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM);
139 /* immediately sends an S frame. */
140 static int llc_shdlc_send_s_frame(const struct llc_shdlc *shdlc,
141 enum sframe_type sframe_type, int nr)
146 pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
148 skb = llc_shdlc_alloc_skb(shdlc, 0);
152 *(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
154 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
161 /* immediately sends an U frame. skb may contain optional payload */
162 static int llc_shdlc_send_u_frame(const struct llc_shdlc *shdlc,
164 enum uframe_modifier uframe_modifier)
168 pr_debug("uframe_modifier=%d\n", uframe_modifier);
170 *(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
172 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
180 * Free ack_pending frames until y_nr - 1, and reset t2 according to
181 * the remaining oldest ack_pending frame sent time
183 static void llc_shdlc_reset_t2(struct llc_shdlc *shdlc, int y_nr)
186 int dnr = shdlc->dnr; /* MUST initially be < y_nr */
188 pr_debug("release ack pending up to frame %d excluded\n", y_nr);
190 while (dnr != y_nr) {
191 pr_debug("release ack pending frame %d\n", dnr);
193 skb = skb_dequeue(&shdlc->ack_pending_q);
199 if (skb_queue_empty(&shdlc->ack_pending_q)) {
200 if (shdlc->t2_active) {
201 del_timer_sync(&shdlc->t2_timer);
202 shdlc->t2_active = false;
205 ("All sent frames acked. Stopped T2(retransmit)\n");
208 skb = skb_peek(&shdlc->ack_pending_q);
210 mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
211 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
212 shdlc->t2_active = true;
215 ("Start T2(retransmit) for remaining unacked sent frames\n");
220 * Receive validated frames from lower layer. skb contains HCI payload only.
221 * Handle according to algorithm at spec:10.8.2
223 static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
224 struct sk_buff *skb, int ns, int nr)
229 pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
231 if (shdlc->state != SHDLC_CONNECTED)
234 if (x_ns != shdlc->nr) {
235 llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
239 if (shdlc->t1_active == false) {
240 shdlc->t1_active = true;
241 mod_timer(&shdlc->t1_timer, jiffies +
242 msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
243 pr_debug("(re)Start T1(send ack)\n");
247 shdlc->rcv_to_hci(shdlc->hdev, skb);
251 shdlc->nr = (shdlc->nr + 1) % 8;
253 if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
254 llc_shdlc_reset_t2(shdlc, y_nr);
263 static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
265 pr_debug("remote acked up to frame %d excluded\n", y_nr);
267 if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
268 llc_shdlc_reset_t2(shdlc, y_nr);
273 static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
277 pr_debug("ns reset to %d\n", shdlc->dnr);
279 while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
280 skb_pull(skb, 1); /* remove control field */
281 skb_queue_head(&shdlc->send_q, skb);
283 shdlc->ns = shdlc->dnr;
286 static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
290 pr_debug("remote asks retransmission from frame %d\n", y_nr);
292 if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
293 if (shdlc->t2_active) {
294 del_timer_sync(&shdlc->t2_timer);
295 shdlc->t2_active = false;
296 pr_debug("Stopped T2(retransmit)\n");
299 if (shdlc->dnr != y_nr) {
300 while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
301 skb = skb_dequeue(&shdlc->ack_pending_q);
306 llc_shdlc_requeue_ack_pending(shdlc);
310 /* See spec RR:10.8.3 REJ:10.8.4 */
311 static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
312 enum sframe_type s_frame_type, int nr)
316 if (shdlc->state != SHDLC_CONNECTED)
319 switch (s_frame_type) {
321 llc_shdlc_rcv_ack(shdlc, nr);
322 if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
324 if (shdlc->send_q.qlen == 0) {
325 skb = llc_shdlc_alloc_skb(shdlc, 0);
327 skb_queue_tail(&shdlc->send_q, skb);
332 llc_shdlc_rcv_rej(shdlc, nr);
335 llc_shdlc_rcv_ack(shdlc, nr);
343 static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
345 pr_debug("result=%d\n", r);
347 del_timer_sync(&shdlc->connect_timer);
354 shdlc->state = SHDLC_HALF_CONNECTED;
356 shdlc->state = SHDLC_DISCONNECTED;
359 shdlc->connect_result = r;
361 wake_up(shdlc->connect_wq);
364 static int llc_shdlc_connect_initiate(const struct llc_shdlc *shdlc)
370 skb = llc_shdlc_alloc_skb(shdlc, 2);
374 skb_put_u8(skb, SHDLC_MAX_WINDOW);
375 skb_put_u8(skb, SHDLC_SREJ_SUPPORT ? 1 : 0);
377 return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
380 static int llc_shdlc_connect_send_ua(const struct llc_shdlc *shdlc)
386 skb = llc_shdlc_alloc_skb(shdlc, 0);
390 return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
393 static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
395 enum uframe_modifier u_frame_modifier)
397 u8 w = SHDLC_MAX_WINDOW;
398 bool srej_support = SHDLC_SREJ_SUPPORT;
401 pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
403 switch (u_frame_modifier) {
405 switch (shdlc->state) {
406 case SHDLC_NEGOTIATING:
407 case SHDLC_CONNECTING:
409 * We sent RSET, but chip wants to negociate or we
410 * got RSET before we managed to send out our.
416 srej_support = skb->data[1] & 0x01 ? true :
419 if ((w <= SHDLC_MAX_WINDOW) &&
420 (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
422 shdlc->srej_support = srej_support;
423 r = llc_shdlc_connect_send_ua(shdlc);
424 llc_shdlc_connect_complete(shdlc, r);
427 case SHDLC_HALF_CONNECTED:
429 * Chip resent RSET due to its timeout - Ignote it
430 * as we already sent UA.
433 case SHDLC_CONNECTED:
435 * Chip wants to reset link. This is unexpected and
438 shdlc->hard_fault = -ECONNRESET;
445 if ((shdlc->state == SHDLC_CONNECTING &&
446 shdlc->connect_tries > 0) ||
447 (shdlc->state == SHDLC_NEGOTIATING)) {
448 llc_shdlc_connect_complete(shdlc, 0);
449 shdlc->state = SHDLC_CONNECTED;
459 static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
465 enum sframe_type s_frame_type;
466 enum uframe_modifier u_frame_modifier;
468 if (shdlc->rcv_q.qlen)
469 pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
471 while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
472 control = skb->data[0];
474 switch (control & SHDLC_CONTROL_HEAD_MASK) {
475 case SHDLC_CONTROL_HEAD_I:
476 case SHDLC_CONTROL_HEAD_I2:
477 if (shdlc->state == SHDLC_HALF_CONNECTED)
478 shdlc->state = SHDLC_CONNECTED;
480 ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
481 nr = control & SHDLC_CONTROL_NR_MASK;
482 llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
484 case SHDLC_CONTROL_HEAD_S:
485 if (shdlc->state == SHDLC_HALF_CONNECTED)
486 shdlc->state = SHDLC_CONNECTED;
488 s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
489 nr = control & SHDLC_CONTROL_NR_MASK;
490 llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
493 case SHDLC_CONTROL_HEAD_U:
494 u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
495 llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
498 pr_err("UNKNOWN Control=%d\n", control);
505 static int llc_shdlc_w_used(int ns, int dnr)
510 unack_count = ns - dnr;
512 unack_count = 8 - dnr + ns;
517 /* Send frames according to algorithm at spec:10.8.1 */
518 static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
522 unsigned long time_sent;
524 if (shdlc->send_q.qlen)
526 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
527 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
528 shdlc->rnr == false ? "false" : "true",
529 shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
530 shdlc->ack_pending_q.qlen);
532 while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
533 (shdlc->rnr == false)) {
535 if (shdlc->t1_active) {
536 del_timer_sync(&shdlc->t1_timer);
537 shdlc->t1_active = false;
538 pr_debug("Stopped T1(send ack)\n");
541 skb = skb_dequeue(&shdlc->send_q);
543 *(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
546 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
548 SHDLC_DUMP_SKB("shdlc frame written", skb);
550 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
552 shdlc->hard_fault = r;
556 shdlc->ns = (shdlc->ns + 1) % 8;
559 *(unsigned long *)skb->cb = time_sent;
561 skb_queue_tail(&shdlc->ack_pending_q, skb);
563 if (shdlc->t2_active == false) {
564 shdlc->t2_active = true;
565 mod_timer(&shdlc->t2_timer, time_sent +
566 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
567 pr_debug("Started T2 (retransmit)\n");
572 static void llc_shdlc_connect_timeout(struct timer_list *t)
574 struct llc_shdlc *shdlc = from_timer(shdlc, t, connect_timer);
578 schedule_work(&shdlc->sm_work);
581 static void llc_shdlc_t1_timeout(struct timer_list *t)
583 struct llc_shdlc *shdlc = from_timer(shdlc, t, t1_timer);
585 pr_debug("SoftIRQ: need to send ack\n");
587 schedule_work(&shdlc->sm_work);
590 static void llc_shdlc_t2_timeout(struct timer_list *t)
592 struct llc_shdlc *shdlc = from_timer(shdlc, t, t2_timer);
594 pr_debug("SoftIRQ: need to retransmit\n");
596 schedule_work(&shdlc->sm_work);
599 static void llc_shdlc_sm_work(struct work_struct *work)
601 struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
606 mutex_lock(&shdlc->state_mutex);
608 switch (shdlc->state) {
609 case SHDLC_DISCONNECTED:
610 skb_queue_purge(&shdlc->rcv_q);
611 skb_queue_purge(&shdlc->send_q);
612 skb_queue_purge(&shdlc->ack_pending_q);
614 case SHDLC_CONNECTING:
615 if (shdlc->hard_fault) {
616 llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
620 if (shdlc->connect_tries++ < 5)
621 r = llc_shdlc_connect_initiate(shdlc);
625 llc_shdlc_connect_complete(shdlc, r);
627 mod_timer(&shdlc->connect_timer, jiffies +
628 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
630 shdlc->state = SHDLC_NEGOTIATING;
633 case SHDLC_NEGOTIATING:
634 if (timer_pending(&shdlc->connect_timer) == 0) {
635 shdlc->state = SHDLC_CONNECTING;
636 schedule_work(&shdlc->sm_work);
639 llc_shdlc_handle_rcv_queue(shdlc);
641 if (shdlc->hard_fault) {
642 llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
646 case SHDLC_HALF_CONNECTED:
647 case SHDLC_CONNECTED:
648 llc_shdlc_handle_rcv_queue(shdlc);
649 llc_shdlc_handle_send_queue(shdlc);
651 if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
653 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
655 shdlc->t1_active = false;
656 r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
659 shdlc->hard_fault = r;
662 if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
664 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
666 shdlc->t2_active = false;
668 llc_shdlc_requeue_ack_pending(shdlc);
669 llc_shdlc_handle_send_queue(shdlc);
672 if (shdlc->hard_fault)
673 shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
678 mutex_unlock(&shdlc->state_mutex);
682 * Called from syscall context to establish shdlc link. Sleeps until
683 * link is ready or failure.
685 static int llc_shdlc_connect(struct llc_shdlc *shdlc)
687 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
691 mutex_lock(&shdlc->state_mutex);
693 shdlc->state = SHDLC_CONNECTING;
694 shdlc->connect_wq = &connect_wq;
695 shdlc->connect_tries = 0;
696 shdlc->connect_result = 1;
698 mutex_unlock(&shdlc->state_mutex);
700 schedule_work(&shdlc->sm_work);
702 wait_event(connect_wq, shdlc->connect_result != 1);
704 return shdlc->connect_result;
707 static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
711 mutex_lock(&shdlc->state_mutex);
713 shdlc->state = SHDLC_DISCONNECTED;
715 mutex_unlock(&shdlc->state_mutex);
717 schedule_work(&shdlc->sm_work);
721 * Receive an incoming shdlc frame. Frame has already been crc-validated.
722 * skb contains only LLC header and payload.
723 * If skb == NULL, it is a notification that the link below is dead.
725 static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
728 pr_err("NULL Frame -> link is dead\n");
729 shdlc->hard_fault = -EREMOTEIO;
731 SHDLC_DUMP_SKB("incoming frame", skb);
732 skb_queue_tail(&shdlc->rcv_q, skb);
735 schedule_work(&shdlc->sm_work);
738 static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
739 rcv_to_hci_t rcv_to_hci, int tx_headroom,
740 int tx_tailroom, int *rx_headroom, int *rx_tailroom,
741 llc_failure_t llc_failure)
743 struct llc_shdlc *shdlc;
745 *rx_headroom = SHDLC_LLC_HEAD_ROOM;
748 shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
752 mutex_init(&shdlc->state_mutex);
753 shdlc->state = SHDLC_DISCONNECTED;
755 timer_setup(&shdlc->connect_timer, llc_shdlc_connect_timeout, 0);
756 timer_setup(&shdlc->t1_timer, llc_shdlc_t1_timeout, 0);
757 timer_setup(&shdlc->t2_timer, llc_shdlc_t2_timeout, 0);
759 shdlc->w = SHDLC_MAX_WINDOW;
760 shdlc->srej_support = SHDLC_SREJ_SUPPORT;
762 skb_queue_head_init(&shdlc->rcv_q);
763 skb_queue_head_init(&shdlc->send_q);
764 skb_queue_head_init(&shdlc->ack_pending_q);
766 INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
769 shdlc->xmit_to_drv = xmit_to_drv;
770 shdlc->rcv_to_hci = rcv_to_hci;
771 shdlc->tx_headroom = tx_headroom;
772 shdlc->tx_tailroom = tx_tailroom;
773 shdlc->llc_failure = llc_failure;
778 static void llc_shdlc_deinit(struct nfc_llc *llc)
780 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
782 skb_queue_purge(&shdlc->rcv_q);
783 skb_queue_purge(&shdlc->send_q);
784 skb_queue_purge(&shdlc->ack_pending_q);
789 static int llc_shdlc_start(struct nfc_llc *llc)
791 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
793 return llc_shdlc_connect(shdlc);
796 static int llc_shdlc_stop(struct nfc_llc *llc)
798 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
800 llc_shdlc_disconnect(shdlc);
805 static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
807 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
809 llc_shdlc_recv_frame(shdlc, skb);
812 static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
814 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
816 skb_queue_tail(&shdlc->send_q, skb);
818 schedule_work(&shdlc->sm_work);
823 static struct nfc_llc_ops llc_shdlc_ops = {
824 .init = llc_shdlc_init,
825 .deinit = llc_shdlc_deinit,
826 .start = llc_shdlc_start,
827 .stop = llc_shdlc_stop,
828 .rcv_from_drv = llc_shdlc_rcv_from_drv,
829 .xmit_from_hci = llc_shdlc_xmit_from_hci,
832 int nfc_llc_shdlc_register(void)
834 return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);