xref: /openbmc/linux/net/nfc/hci/llc_shdlc.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
146fe7771SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24a61cd66SEric Lapuyade /*
34a61cd66SEric Lapuyade  * shdlc Link Layer Control
44a61cd66SEric Lapuyade  *
54a61cd66SEric Lapuyade  * Copyright (C) 2012  Intel Corporation. All rights reserved.
64a61cd66SEric Lapuyade  */
74a61cd66SEric Lapuyade 
84a61cd66SEric Lapuyade #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
94a61cd66SEric Lapuyade 
104a61cd66SEric Lapuyade #include <linux/types.h>
114a61cd66SEric Lapuyade #include <linux/sched.h>
124a61cd66SEric Lapuyade #include <linux/wait.h>
134a61cd66SEric Lapuyade #include <linux/slab.h>
144a61cd66SEric Lapuyade #include <linux/skbuff.h>
154a61cd66SEric Lapuyade 
164a61cd66SEric Lapuyade #include "llc.h"
174a61cd66SEric Lapuyade 
184a61cd66SEric Lapuyade enum shdlc_state {
194a61cd66SEric Lapuyade 	SHDLC_DISCONNECTED = 0,
204a61cd66SEric Lapuyade 	SHDLC_CONNECTING = 1,
214c0ba9acSWaldemar Rymarkiewicz 	SHDLC_NEGOTIATING = 2,
2212bfd1e8SWaldemar Rymarkiewicz 	SHDLC_HALF_CONNECTED = 3,
2312bfd1e8SWaldemar Rymarkiewicz 	SHDLC_CONNECTED = 4
244a61cd66SEric Lapuyade };
254a61cd66SEric Lapuyade 
264a61cd66SEric Lapuyade struct llc_shdlc {
274a61cd66SEric Lapuyade 	struct nfc_hci_dev *hdev;
284a61cd66SEric Lapuyade 	xmit_to_drv_t xmit_to_drv;
294a61cd66SEric Lapuyade 	rcv_to_hci_t rcv_to_hci;
304a61cd66SEric Lapuyade 
314a61cd66SEric Lapuyade 	struct mutex state_mutex;
324a61cd66SEric Lapuyade 	enum shdlc_state state;
334a61cd66SEric Lapuyade 	int hard_fault;
344a61cd66SEric Lapuyade 
354a61cd66SEric Lapuyade 	wait_queue_head_t *connect_wq;
364a61cd66SEric Lapuyade 	int connect_tries;
374a61cd66SEric Lapuyade 	int connect_result;
384a61cd66SEric Lapuyade 	struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
394a61cd66SEric Lapuyade 
404a61cd66SEric Lapuyade 	u8 w;				/* window size */
414a61cd66SEric Lapuyade 	bool srej_support;
424a61cd66SEric Lapuyade 
434a61cd66SEric Lapuyade 	struct timer_list t1_timer;	/* send ack timeout */
444a61cd66SEric Lapuyade 	bool t1_active;
454a61cd66SEric Lapuyade 
464a61cd66SEric Lapuyade 	struct timer_list t2_timer;	/* guard/retransmit timeout */
474a61cd66SEric Lapuyade 	bool t2_active;
484a61cd66SEric Lapuyade 
494a61cd66SEric Lapuyade 	int ns;				/* next seq num for send */
504a61cd66SEric Lapuyade 	int nr;				/* next expected seq num for receive */
514a61cd66SEric Lapuyade 	int dnr;			/* oldest sent unacked seq num */
524a61cd66SEric Lapuyade 
534a61cd66SEric Lapuyade 	struct sk_buff_head rcv_q;
544a61cd66SEric Lapuyade 
554a61cd66SEric Lapuyade 	struct sk_buff_head send_q;
564a61cd66SEric Lapuyade 	bool rnr;			/* other side is not ready to receive */
574a61cd66SEric Lapuyade 
584a61cd66SEric Lapuyade 	struct sk_buff_head ack_pending_q;
594a61cd66SEric Lapuyade 
604a61cd66SEric Lapuyade 	struct work_struct sm_work;
614a61cd66SEric Lapuyade 
624a61cd66SEric Lapuyade 	int tx_headroom;
634a61cd66SEric Lapuyade 	int tx_tailroom;
644a61cd66SEric Lapuyade 
654a61cd66SEric Lapuyade 	llc_failure_t llc_failure;
664a61cd66SEric Lapuyade };
674a61cd66SEric Lapuyade 
684a61cd66SEric Lapuyade #define SHDLC_LLC_HEAD_ROOM	2
694a61cd66SEric Lapuyade 
704a61cd66SEric Lapuyade #define SHDLC_MAX_WINDOW	4
714a61cd66SEric Lapuyade #define SHDLC_SREJ_SUPPORT	false
724a61cd66SEric Lapuyade 
734a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_MASK	0xe0
744a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_I	0x80
754a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_I2	0xa0
764a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_S	0xc0
774a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_U	0xe0
784a61cd66SEric Lapuyade 
794a61cd66SEric Lapuyade #define SHDLC_CONTROL_NS_MASK	0x38
804a61cd66SEric Lapuyade #define SHDLC_CONTROL_NR_MASK	0x07
814a61cd66SEric Lapuyade #define SHDLC_CONTROL_TYPE_MASK	0x18
824a61cd66SEric Lapuyade 
834a61cd66SEric Lapuyade #define SHDLC_CONTROL_M_MASK	0x1f
844a61cd66SEric Lapuyade 
854a61cd66SEric Lapuyade enum sframe_type {
864a61cd66SEric Lapuyade 	S_FRAME_RR = 0x00,
874a61cd66SEric Lapuyade 	S_FRAME_REJ = 0x01,
884a61cd66SEric Lapuyade 	S_FRAME_RNR = 0x02,
894a61cd66SEric Lapuyade 	S_FRAME_SREJ = 0x03
904a61cd66SEric Lapuyade };
914a61cd66SEric Lapuyade 
924a61cd66SEric Lapuyade enum uframe_modifier {
934a61cd66SEric Lapuyade 	U_FRAME_UA = 0x06,
944a61cd66SEric Lapuyade 	U_FRAME_RSET = 0x19
954a61cd66SEric Lapuyade };
964a61cd66SEric Lapuyade 
974a61cd66SEric Lapuyade #define SHDLC_CONNECT_VALUE_MS	5
984a61cd66SEric Lapuyade #define SHDLC_T1_VALUE_MS(w)	((5 * w) / 4)
994a61cd66SEric Lapuyade #define SHDLC_T2_VALUE_MS	300
1004a61cd66SEric Lapuyade 
1014a61cd66SEric Lapuyade #define SHDLC_DUMP_SKB(info, skb)				  \
1024a61cd66SEric Lapuyade do {								  \
1034a61cd66SEric Lapuyade 	pr_debug("%s:\n", info);				  \
1044a61cd66SEric Lapuyade 	print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
1054a61cd66SEric Lapuyade 		       16, 1, skb->data, skb->len, 0);		  \
1064a61cd66SEric Lapuyade } while (0)
1074a61cd66SEric Lapuyade 
1084a61cd66SEric Lapuyade /* checks x < y <= z modulo 8 */
llc_shdlc_x_lt_y_lteq_z(int x,int y,int z)1094a61cd66SEric Lapuyade static bool llc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
1104a61cd66SEric Lapuyade {
1114a61cd66SEric Lapuyade 	if (x < z)
1124a61cd66SEric Lapuyade 		return ((x < y) && (y <= z)) ? true : false;
1134a61cd66SEric Lapuyade 	else
1144a61cd66SEric Lapuyade 		return ((y > x) || (y <= z)) ? true : false;
1154a61cd66SEric Lapuyade }
1164a61cd66SEric Lapuyade 
1174a61cd66SEric Lapuyade /* checks x <= y < z modulo 8 */
llc_shdlc_x_lteq_y_lt_z(int x,int y,int z)1184a61cd66SEric Lapuyade static bool llc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
1194a61cd66SEric Lapuyade {
1204a61cd66SEric Lapuyade 	if (x <= z)
1214a61cd66SEric Lapuyade 		return ((x <= y) && (y < z)) ? true : false;
1224a61cd66SEric Lapuyade 	else			/* x > z -> z+8 > x */
1234a61cd66SEric Lapuyade 		return ((y >= x) || (y < z)) ? true : false;
1244a61cd66SEric Lapuyade }
1254a61cd66SEric Lapuyade 
llc_shdlc_alloc_skb(const struct llc_shdlc * shdlc,int payload_len)1263df40eb3SKrzysztof Kozlowski static struct sk_buff *llc_shdlc_alloc_skb(const struct llc_shdlc *shdlc,
1274a61cd66SEric Lapuyade 					   int payload_len)
1284a61cd66SEric Lapuyade {
1294a61cd66SEric Lapuyade 	struct sk_buff *skb;
1304a61cd66SEric Lapuyade 
1314a61cd66SEric Lapuyade 	skb = alloc_skb(shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM +
1324a61cd66SEric Lapuyade 			shdlc->tx_tailroom + payload_len, GFP_KERNEL);
1334a61cd66SEric Lapuyade 	if (skb)
1344a61cd66SEric Lapuyade 		skb_reserve(skb, shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM);
1354a61cd66SEric Lapuyade 
1364a61cd66SEric Lapuyade 	return skb;
1374a61cd66SEric Lapuyade }
1384a61cd66SEric Lapuyade 
1394a61cd66SEric Lapuyade /* immediately sends an S frame. */
llc_shdlc_send_s_frame(const struct llc_shdlc * shdlc,enum sframe_type sframe_type,int nr)1403df40eb3SKrzysztof Kozlowski static int llc_shdlc_send_s_frame(const struct llc_shdlc *shdlc,
1414a61cd66SEric Lapuyade 				  enum sframe_type sframe_type, int nr)
1424a61cd66SEric Lapuyade {
1434a61cd66SEric Lapuyade 	int r;
1444a61cd66SEric Lapuyade 	struct sk_buff *skb;
1454a61cd66SEric Lapuyade 
1464a61cd66SEric Lapuyade 	pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
1474a61cd66SEric Lapuyade 
1484a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 0);
1494a61cd66SEric Lapuyade 	if (skb == NULL)
1504a61cd66SEric Lapuyade 		return -ENOMEM;
1514a61cd66SEric Lapuyade 
152d58ff351SJohannes Berg 	*(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
1534a61cd66SEric Lapuyade 
1544a61cd66SEric Lapuyade 	r = shdlc->xmit_to_drv(shdlc->hdev, skb);
1554a61cd66SEric Lapuyade 
1564a61cd66SEric Lapuyade 	kfree_skb(skb);
1574a61cd66SEric Lapuyade 
1584a61cd66SEric Lapuyade 	return r;
1594a61cd66SEric Lapuyade }
1604a61cd66SEric Lapuyade 
1614a61cd66SEric Lapuyade /* immediately sends an U frame. skb may contain optional payload */
llc_shdlc_send_u_frame(const struct llc_shdlc * shdlc,struct sk_buff * skb,enum uframe_modifier uframe_modifier)1623df40eb3SKrzysztof Kozlowski static int llc_shdlc_send_u_frame(const struct llc_shdlc *shdlc,
1634a61cd66SEric Lapuyade 				  struct sk_buff *skb,
1644a61cd66SEric Lapuyade 				  enum uframe_modifier uframe_modifier)
1654a61cd66SEric Lapuyade {
1664a61cd66SEric Lapuyade 	int r;
1674a61cd66SEric Lapuyade 
1684a61cd66SEric Lapuyade 	pr_debug("uframe_modifier=%d\n", uframe_modifier);
1694a61cd66SEric Lapuyade 
170d58ff351SJohannes Berg 	*(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
1714a61cd66SEric Lapuyade 
1724a61cd66SEric Lapuyade 	r = shdlc->xmit_to_drv(shdlc->hdev, skb);
1734a61cd66SEric Lapuyade 
1744a61cd66SEric Lapuyade 	kfree_skb(skb);
1754a61cd66SEric Lapuyade 
1764a61cd66SEric Lapuyade 	return r;
1774a61cd66SEric Lapuyade }
1784a61cd66SEric Lapuyade 
1794a61cd66SEric Lapuyade /*
1804a61cd66SEric Lapuyade  * Free ack_pending frames until y_nr - 1, and reset t2 according to
1814a61cd66SEric Lapuyade  * the remaining oldest ack_pending frame sent time
1824a61cd66SEric Lapuyade  */
llc_shdlc_reset_t2(struct llc_shdlc * shdlc,int y_nr)1834a61cd66SEric Lapuyade static void llc_shdlc_reset_t2(struct llc_shdlc *shdlc, int y_nr)
1844a61cd66SEric Lapuyade {
1854a61cd66SEric Lapuyade 	struct sk_buff *skb;
1864a61cd66SEric Lapuyade 	int dnr = shdlc->dnr;	/* MUST initially be < y_nr */
1874a61cd66SEric Lapuyade 
1884a61cd66SEric Lapuyade 	pr_debug("release ack pending up to frame %d excluded\n", y_nr);
1894a61cd66SEric Lapuyade 
1904a61cd66SEric Lapuyade 	while (dnr != y_nr) {
1914a61cd66SEric Lapuyade 		pr_debug("release ack pending frame %d\n", dnr);
1924a61cd66SEric Lapuyade 
1934a61cd66SEric Lapuyade 		skb = skb_dequeue(&shdlc->ack_pending_q);
1944a61cd66SEric Lapuyade 		kfree_skb(skb);
1954a61cd66SEric Lapuyade 
1964a61cd66SEric Lapuyade 		dnr = (dnr + 1) % 8;
1974a61cd66SEric Lapuyade 	}
1984a61cd66SEric Lapuyade 
1994a61cd66SEric Lapuyade 	if (skb_queue_empty(&shdlc->ack_pending_q)) {
2004a61cd66SEric Lapuyade 		if (shdlc->t2_active) {
2014a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t2_timer);
2024a61cd66SEric Lapuyade 			shdlc->t2_active = false;
2034a61cd66SEric Lapuyade 
204*3537e507SKrzysztof Kozlowski 			pr_debug("All sent frames acked. Stopped T2(retransmit)\n");
2054a61cd66SEric Lapuyade 		}
2064a61cd66SEric Lapuyade 	} else {
2074a61cd66SEric Lapuyade 		skb = skb_peek(&shdlc->ack_pending_q);
2084a61cd66SEric Lapuyade 
2094a61cd66SEric Lapuyade 		mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
2104a61cd66SEric Lapuyade 			  msecs_to_jiffies(SHDLC_T2_VALUE_MS));
2114a61cd66SEric Lapuyade 		shdlc->t2_active = true;
2124a61cd66SEric Lapuyade 
213*3537e507SKrzysztof Kozlowski 		pr_debug("Start T2(retransmit) for remaining unacked sent frames\n");
2144a61cd66SEric Lapuyade 	}
2154a61cd66SEric Lapuyade }
2164a61cd66SEric Lapuyade 
2174a61cd66SEric Lapuyade /*
2184a61cd66SEric Lapuyade  * Receive validated frames from lower layer. skb contains HCI payload only.
2194a61cd66SEric Lapuyade  * Handle according to algorithm at spec:10.8.2
2204a61cd66SEric Lapuyade  */
llc_shdlc_rcv_i_frame(struct llc_shdlc * shdlc,struct sk_buff * skb,int ns,int nr)2214a61cd66SEric Lapuyade static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
2224a61cd66SEric Lapuyade 				  struct sk_buff *skb, int ns, int nr)
2234a61cd66SEric Lapuyade {
2244a61cd66SEric Lapuyade 	int x_ns = ns;
2254a61cd66SEric Lapuyade 	int y_nr = nr;
2264a61cd66SEric Lapuyade 
2274a61cd66SEric Lapuyade 	pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
2284a61cd66SEric Lapuyade 
2294a61cd66SEric Lapuyade 	if (shdlc->state != SHDLC_CONNECTED)
2304a61cd66SEric Lapuyade 		goto exit;
2314a61cd66SEric Lapuyade 
2324a61cd66SEric Lapuyade 	if (x_ns != shdlc->nr) {
2334a61cd66SEric Lapuyade 		llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
2344a61cd66SEric Lapuyade 		goto exit;
2354a61cd66SEric Lapuyade 	}
2364a61cd66SEric Lapuyade 
237f50e2f9fSYANG LI 	if (!shdlc->t1_active) {
2384a61cd66SEric Lapuyade 		shdlc->t1_active = true;
2394a61cd66SEric Lapuyade 		mod_timer(&shdlc->t1_timer, jiffies +
2404a61cd66SEric Lapuyade 			  msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
2414a61cd66SEric Lapuyade 		pr_debug("(re)Start T1(send ack)\n");
2424a61cd66SEric Lapuyade 	}
2434a61cd66SEric Lapuyade 
2444a61cd66SEric Lapuyade 	if (skb->len) {
2454a61cd66SEric Lapuyade 		shdlc->rcv_to_hci(shdlc->hdev, skb);
2464a61cd66SEric Lapuyade 		skb = NULL;
2474a61cd66SEric Lapuyade 	}
2484a61cd66SEric Lapuyade 
2494a61cd66SEric Lapuyade 	shdlc->nr = (shdlc->nr + 1) % 8;
2504a61cd66SEric Lapuyade 
2514a61cd66SEric Lapuyade 	if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
2524a61cd66SEric Lapuyade 		llc_shdlc_reset_t2(shdlc, y_nr);
2534a61cd66SEric Lapuyade 
2544a61cd66SEric Lapuyade 		shdlc->dnr = y_nr;
2554a61cd66SEric Lapuyade 	}
2564a61cd66SEric Lapuyade 
2574a61cd66SEric Lapuyade exit:
2584a61cd66SEric Lapuyade 	kfree_skb(skb);
2594a61cd66SEric Lapuyade }
2604a61cd66SEric Lapuyade 
llc_shdlc_rcv_ack(struct llc_shdlc * shdlc,int y_nr)2614a61cd66SEric Lapuyade static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
2624a61cd66SEric Lapuyade {
2634a61cd66SEric Lapuyade 	pr_debug("remote acked up to frame %d excluded\n", y_nr);
2644a61cd66SEric Lapuyade 
2654a61cd66SEric Lapuyade 	if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
2664a61cd66SEric Lapuyade 		llc_shdlc_reset_t2(shdlc, y_nr);
2674a61cd66SEric Lapuyade 		shdlc->dnr = y_nr;
2684a61cd66SEric Lapuyade 	}
2694a61cd66SEric Lapuyade }
2704a61cd66SEric Lapuyade 
llc_shdlc_requeue_ack_pending(struct llc_shdlc * shdlc)2714a61cd66SEric Lapuyade static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
2724a61cd66SEric Lapuyade {
2734a61cd66SEric Lapuyade 	struct sk_buff *skb;
2744a61cd66SEric Lapuyade 
2754a61cd66SEric Lapuyade 	pr_debug("ns reset to %d\n", shdlc->dnr);
2764a61cd66SEric Lapuyade 
2774a61cd66SEric Lapuyade 	while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
2784a61cd66SEric Lapuyade 		skb_pull(skb, 1);	/* remove control field */
2794a61cd66SEric Lapuyade 		skb_queue_head(&shdlc->send_q, skb);
2804a61cd66SEric Lapuyade 	}
2814a61cd66SEric Lapuyade 	shdlc->ns = shdlc->dnr;
2824a61cd66SEric Lapuyade }
2834a61cd66SEric Lapuyade 
llc_shdlc_rcv_rej(struct llc_shdlc * shdlc,int y_nr)2844a61cd66SEric Lapuyade static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
2854a61cd66SEric Lapuyade {
2864a61cd66SEric Lapuyade 	struct sk_buff *skb;
2874a61cd66SEric Lapuyade 
28888bfe6eaSGeert Uytterhoeven 	pr_debug("remote asks retransmission from frame %d\n", y_nr);
2894a61cd66SEric Lapuyade 
2904a61cd66SEric Lapuyade 	if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
2914a61cd66SEric Lapuyade 		if (shdlc->t2_active) {
2924a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t2_timer);
2934a61cd66SEric Lapuyade 			shdlc->t2_active = false;
2944a61cd66SEric Lapuyade 			pr_debug("Stopped T2(retransmit)\n");
2954a61cd66SEric Lapuyade 		}
2964a61cd66SEric Lapuyade 
2974a61cd66SEric Lapuyade 		if (shdlc->dnr != y_nr) {
2984a61cd66SEric Lapuyade 			while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
2994a61cd66SEric Lapuyade 				skb = skb_dequeue(&shdlc->ack_pending_q);
3004a61cd66SEric Lapuyade 				kfree_skb(skb);
3014a61cd66SEric Lapuyade 			}
3024a61cd66SEric Lapuyade 		}
3034a61cd66SEric Lapuyade 
3044a61cd66SEric Lapuyade 		llc_shdlc_requeue_ack_pending(shdlc);
3054a61cd66SEric Lapuyade 	}
3064a61cd66SEric Lapuyade }
3074a61cd66SEric Lapuyade 
3084a61cd66SEric Lapuyade /* See spec RR:10.8.3 REJ:10.8.4 */
llc_shdlc_rcv_s_frame(struct llc_shdlc * shdlc,enum sframe_type s_frame_type,int nr)3094a61cd66SEric Lapuyade static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
3104a61cd66SEric Lapuyade 				  enum sframe_type s_frame_type, int nr)
3114a61cd66SEric Lapuyade {
3124a61cd66SEric Lapuyade 	struct sk_buff *skb;
3134a61cd66SEric Lapuyade 
3144a61cd66SEric Lapuyade 	if (shdlc->state != SHDLC_CONNECTED)
3154a61cd66SEric Lapuyade 		return;
3164a61cd66SEric Lapuyade 
3174a61cd66SEric Lapuyade 	switch (s_frame_type) {
3184a61cd66SEric Lapuyade 	case S_FRAME_RR:
3194a61cd66SEric Lapuyade 		llc_shdlc_rcv_ack(shdlc, nr);
3204a61cd66SEric Lapuyade 		if (shdlc->rnr == true) {	/* see SHDLC 10.7.7 */
3214a61cd66SEric Lapuyade 			shdlc->rnr = false;
3224a61cd66SEric Lapuyade 			if (shdlc->send_q.qlen == 0) {
3234a61cd66SEric Lapuyade 				skb = llc_shdlc_alloc_skb(shdlc, 0);
3244a61cd66SEric Lapuyade 				if (skb)
3254a61cd66SEric Lapuyade 					skb_queue_tail(&shdlc->send_q, skb);
3264a61cd66SEric Lapuyade 			}
3274a61cd66SEric Lapuyade 		}
3284a61cd66SEric Lapuyade 		break;
3294a61cd66SEric Lapuyade 	case S_FRAME_REJ:
3304a61cd66SEric Lapuyade 		llc_shdlc_rcv_rej(shdlc, nr);
3314a61cd66SEric Lapuyade 		break;
3324a61cd66SEric Lapuyade 	case S_FRAME_RNR:
3334a61cd66SEric Lapuyade 		llc_shdlc_rcv_ack(shdlc, nr);
3344a61cd66SEric Lapuyade 		shdlc->rnr = true;
3354a61cd66SEric Lapuyade 		break;
3364a61cd66SEric Lapuyade 	default:
3374a61cd66SEric Lapuyade 		break;
3384a61cd66SEric Lapuyade 	}
3394a61cd66SEric Lapuyade }
3404a61cd66SEric Lapuyade 
llc_shdlc_connect_complete(struct llc_shdlc * shdlc,int r)3414a61cd66SEric Lapuyade static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
3424a61cd66SEric Lapuyade {
3434a61cd66SEric Lapuyade 	pr_debug("result=%d\n", r);
3444a61cd66SEric Lapuyade 
3454a61cd66SEric Lapuyade 	del_timer_sync(&shdlc->connect_timer);
3464a61cd66SEric Lapuyade 
3474a61cd66SEric Lapuyade 	if (r == 0) {
3484a61cd66SEric Lapuyade 		shdlc->ns = 0;
3494a61cd66SEric Lapuyade 		shdlc->nr = 0;
3504a61cd66SEric Lapuyade 		shdlc->dnr = 0;
3514a61cd66SEric Lapuyade 
35212bfd1e8SWaldemar Rymarkiewicz 		shdlc->state = SHDLC_HALF_CONNECTED;
3534a61cd66SEric Lapuyade 	} else {
3544a61cd66SEric Lapuyade 		shdlc->state = SHDLC_DISCONNECTED;
3554a61cd66SEric Lapuyade 	}
3564a61cd66SEric Lapuyade 
3574a61cd66SEric Lapuyade 	shdlc->connect_result = r;
3584a61cd66SEric Lapuyade 
3594a61cd66SEric Lapuyade 	wake_up(shdlc->connect_wq);
3604a61cd66SEric Lapuyade }
3614a61cd66SEric Lapuyade 
llc_shdlc_connect_initiate(const struct llc_shdlc * shdlc)3623df40eb3SKrzysztof Kozlowski static int llc_shdlc_connect_initiate(const struct llc_shdlc *shdlc)
3634a61cd66SEric Lapuyade {
3644a61cd66SEric Lapuyade 	struct sk_buff *skb;
3654a61cd66SEric Lapuyade 
3664a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 2);
3674a61cd66SEric Lapuyade 	if (skb == NULL)
3684a61cd66SEric Lapuyade 		return -ENOMEM;
3694a61cd66SEric Lapuyade 
370634fef61SJohannes Berg 	skb_put_u8(skb, SHDLC_MAX_WINDOW);
371634fef61SJohannes Berg 	skb_put_u8(skb, SHDLC_SREJ_SUPPORT ? 1 : 0);
3724a61cd66SEric Lapuyade 
3734a61cd66SEric Lapuyade 	return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
3744a61cd66SEric Lapuyade }
3754a61cd66SEric Lapuyade 
llc_shdlc_connect_send_ua(const struct llc_shdlc * shdlc)3763df40eb3SKrzysztof Kozlowski static int llc_shdlc_connect_send_ua(const struct llc_shdlc *shdlc)
3774a61cd66SEric Lapuyade {
3784a61cd66SEric Lapuyade 	struct sk_buff *skb;
3794a61cd66SEric Lapuyade 
3804a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 0);
3814a61cd66SEric Lapuyade 	if (skb == NULL)
3824a61cd66SEric Lapuyade 		return -ENOMEM;
3834a61cd66SEric Lapuyade 
3844a61cd66SEric Lapuyade 	return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
3854a61cd66SEric Lapuyade }
3864a61cd66SEric Lapuyade 
llc_shdlc_rcv_u_frame(struct llc_shdlc * shdlc,struct sk_buff * skb,enum uframe_modifier u_frame_modifier)3874a61cd66SEric Lapuyade static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
3884a61cd66SEric Lapuyade 				  struct sk_buff *skb,
3894a61cd66SEric Lapuyade 				  enum uframe_modifier u_frame_modifier)
3904a61cd66SEric Lapuyade {
3914a61cd66SEric Lapuyade 	u8 w = SHDLC_MAX_WINDOW;
3924a61cd66SEric Lapuyade 	bool srej_support = SHDLC_SREJ_SUPPORT;
3934a61cd66SEric Lapuyade 	int r;
3944a61cd66SEric Lapuyade 
3954a61cd66SEric Lapuyade 	pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
3964a61cd66SEric Lapuyade 
3974a61cd66SEric Lapuyade 	switch (u_frame_modifier) {
3984a61cd66SEric Lapuyade 	case U_FRAME_RSET:
39912bfd1e8SWaldemar Rymarkiewicz 		switch (shdlc->state) {
4004c0ba9acSWaldemar Rymarkiewicz 		case SHDLC_NEGOTIATING:
40112bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_CONNECTING:
40212bfd1e8SWaldemar Rymarkiewicz 			/*
40301709d09SZheng Yongjun 			 * We sent RSET, but chip wants to negotiate or we
40412bfd1e8SWaldemar Rymarkiewicz 			 * got RSET before we managed to send out our.
40512bfd1e8SWaldemar Rymarkiewicz 			 */
4064a61cd66SEric Lapuyade 			if (skb->len > 0)
4074a61cd66SEric Lapuyade 				w = skb->data[0];
4084a61cd66SEric Lapuyade 
4094a61cd66SEric Lapuyade 			if (skb->len > 1)
4104a61cd66SEric Lapuyade 				srej_support = skb->data[1] & 0x01 ? true :
4114a61cd66SEric Lapuyade 					       false;
4124a61cd66SEric Lapuyade 
4134a61cd66SEric Lapuyade 			if ((w <= SHDLC_MAX_WINDOW) &&
4144a61cd66SEric Lapuyade 			    (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
4154a61cd66SEric Lapuyade 				shdlc->w = w;
4164a61cd66SEric Lapuyade 				shdlc->srej_support = srej_support;
4174a61cd66SEric Lapuyade 				r = llc_shdlc_connect_send_ua(shdlc);
4184a61cd66SEric Lapuyade 				llc_shdlc_connect_complete(shdlc, r);
4194a61cd66SEric Lapuyade 			}
42012bfd1e8SWaldemar Rymarkiewicz 			break;
42112bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_HALF_CONNECTED:
42212bfd1e8SWaldemar Rymarkiewicz 			/*
42312bfd1e8SWaldemar Rymarkiewicz 			 * Chip resent RSET due to its timeout - Ignote it
42412bfd1e8SWaldemar Rymarkiewicz 			 * as we already sent UA.
42512bfd1e8SWaldemar Rymarkiewicz 			 */
42612bfd1e8SWaldemar Rymarkiewicz 			break;
42712bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_CONNECTED:
4284a61cd66SEric Lapuyade 			/*
4294a61cd66SEric Lapuyade 			 * Chip wants to reset link. This is unexpected and
4304a61cd66SEric Lapuyade 			 * unsupported.
4314a61cd66SEric Lapuyade 			 */
4324a61cd66SEric Lapuyade 			shdlc->hard_fault = -ECONNRESET;
43312bfd1e8SWaldemar Rymarkiewicz 			break;
43412bfd1e8SWaldemar Rymarkiewicz 		default:
43512bfd1e8SWaldemar Rymarkiewicz 			break;
4364a61cd66SEric Lapuyade 		}
4374a61cd66SEric Lapuyade 		break;
4384a61cd66SEric Lapuyade 	case U_FRAME_UA:
4394a61cd66SEric Lapuyade 		if ((shdlc->state == SHDLC_CONNECTING &&
4404a61cd66SEric Lapuyade 		     shdlc->connect_tries > 0) ||
4414c0ba9acSWaldemar Rymarkiewicz 		    (shdlc->state == SHDLC_NEGOTIATING)) {
4424a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, 0);
44312bfd1e8SWaldemar Rymarkiewicz 			shdlc->state = SHDLC_CONNECTED;
44412bfd1e8SWaldemar Rymarkiewicz 		}
4454a61cd66SEric Lapuyade 		break;
4464a61cd66SEric Lapuyade 	default:
4474a61cd66SEric Lapuyade 		break;
4484a61cd66SEric Lapuyade 	}
4494a61cd66SEric Lapuyade 
4504a61cd66SEric Lapuyade 	kfree_skb(skb);
4514a61cd66SEric Lapuyade }
4524a61cd66SEric Lapuyade 
llc_shdlc_handle_rcv_queue(struct llc_shdlc * shdlc)4534a61cd66SEric Lapuyade static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
4544a61cd66SEric Lapuyade {
4554a61cd66SEric Lapuyade 	struct sk_buff *skb;
4564a61cd66SEric Lapuyade 	u8 control;
4574a61cd66SEric Lapuyade 	int nr;
4584a61cd66SEric Lapuyade 	int ns;
4594a61cd66SEric Lapuyade 	enum sframe_type s_frame_type;
4604a61cd66SEric Lapuyade 	enum uframe_modifier u_frame_modifier;
4614a61cd66SEric Lapuyade 
4624a61cd66SEric Lapuyade 	if (shdlc->rcv_q.qlen)
4634a61cd66SEric Lapuyade 		pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
4644a61cd66SEric Lapuyade 
4654a61cd66SEric Lapuyade 	while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
4664a61cd66SEric Lapuyade 		control = skb->data[0];
4674a61cd66SEric Lapuyade 		skb_pull(skb, 1);
4684a61cd66SEric Lapuyade 		switch (control & SHDLC_CONTROL_HEAD_MASK) {
4694a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_I:
4704a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_I2:
47112bfd1e8SWaldemar Rymarkiewicz 			if (shdlc->state == SHDLC_HALF_CONNECTED)
47212bfd1e8SWaldemar Rymarkiewicz 				shdlc->state = SHDLC_CONNECTED;
47312bfd1e8SWaldemar Rymarkiewicz 
4744a61cd66SEric Lapuyade 			ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
4754a61cd66SEric Lapuyade 			nr = control & SHDLC_CONTROL_NR_MASK;
4764a61cd66SEric Lapuyade 			llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
4774a61cd66SEric Lapuyade 			break;
4784a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_S:
47912bfd1e8SWaldemar Rymarkiewicz 			if (shdlc->state == SHDLC_HALF_CONNECTED)
48012bfd1e8SWaldemar Rymarkiewicz 				shdlc->state = SHDLC_CONNECTED;
48112bfd1e8SWaldemar Rymarkiewicz 
4824a61cd66SEric Lapuyade 			s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
4834a61cd66SEric Lapuyade 			nr = control & SHDLC_CONTROL_NR_MASK;
4844a61cd66SEric Lapuyade 			llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
4854a61cd66SEric Lapuyade 			kfree_skb(skb);
4864a61cd66SEric Lapuyade 			break;
4874a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_U:
4884a61cd66SEric Lapuyade 			u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
4894a61cd66SEric Lapuyade 			llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
4904a61cd66SEric Lapuyade 			break;
4914a61cd66SEric Lapuyade 		default:
4924a61cd66SEric Lapuyade 			pr_err("UNKNOWN Control=%d\n", control);
4934a61cd66SEric Lapuyade 			kfree_skb(skb);
4944a61cd66SEric Lapuyade 			break;
4954a61cd66SEric Lapuyade 		}
4964a61cd66SEric Lapuyade 	}
4974a61cd66SEric Lapuyade }
4984a61cd66SEric Lapuyade 
llc_shdlc_w_used(int ns,int dnr)4994a61cd66SEric Lapuyade static int llc_shdlc_w_used(int ns, int dnr)
5004a61cd66SEric Lapuyade {
5014a61cd66SEric Lapuyade 	int unack_count;
5024a61cd66SEric Lapuyade 
5034a61cd66SEric Lapuyade 	if (dnr <= ns)
5044a61cd66SEric Lapuyade 		unack_count = ns - dnr;
5054a61cd66SEric Lapuyade 	else
5064a61cd66SEric Lapuyade 		unack_count = 8 - dnr + ns;
5074a61cd66SEric Lapuyade 
5084a61cd66SEric Lapuyade 	return unack_count;
5094a61cd66SEric Lapuyade }
5104a61cd66SEric Lapuyade 
5114a61cd66SEric Lapuyade /* Send frames according to algorithm at spec:10.8.1 */
llc_shdlc_handle_send_queue(struct llc_shdlc * shdlc)5124a61cd66SEric Lapuyade static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
5134a61cd66SEric Lapuyade {
5144a61cd66SEric Lapuyade 	struct sk_buff *skb;
5154a61cd66SEric Lapuyade 	int r;
5164a61cd66SEric Lapuyade 	unsigned long time_sent;
5174a61cd66SEric Lapuyade 
5184a61cd66SEric Lapuyade 	if (shdlc->send_q.qlen)
519*3537e507SKrzysztof Kozlowski 		pr_debug("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
5204a61cd66SEric Lapuyade 			 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
5214a61cd66SEric Lapuyade 			 shdlc->rnr == false ? "false" : "true",
5224a61cd66SEric Lapuyade 			 shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
5234a61cd66SEric Lapuyade 			 shdlc->ack_pending_q.qlen);
5244a61cd66SEric Lapuyade 
5254a61cd66SEric Lapuyade 	while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
5264a61cd66SEric Lapuyade 	       (shdlc->rnr == false)) {
5274a61cd66SEric Lapuyade 
5284a61cd66SEric Lapuyade 		if (shdlc->t1_active) {
5294a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t1_timer);
5304a61cd66SEric Lapuyade 			shdlc->t1_active = false;
5314a61cd66SEric Lapuyade 			pr_debug("Stopped T1(send ack)\n");
5324a61cd66SEric Lapuyade 		}
5334a61cd66SEric Lapuyade 
5344a61cd66SEric Lapuyade 		skb = skb_dequeue(&shdlc->send_q);
5354a61cd66SEric Lapuyade 
536d58ff351SJohannes Berg 		*(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
5374a61cd66SEric Lapuyade 					shdlc->nr;
5384a61cd66SEric Lapuyade 
5394a61cd66SEric Lapuyade 		pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
5404a61cd66SEric Lapuyade 			 shdlc->nr);
541412fda53SEric Lapuyade 		SHDLC_DUMP_SKB("shdlc frame written", skb);
5424a61cd66SEric Lapuyade 
5434a61cd66SEric Lapuyade 		r = shdlc->xmit_to_drv(shdlc->hdev, skb);
5444a61cd66SEric Lapuyade 		if (r < 0) {
5454a61cd66SEric Lapuyade 			shdlc->hard_fault = r;
5464a61cd66SEric Lapuyade 			break;
5474a61cd66SEric Lapuyade 		}
5484a61cd66SEric Lapuyade 
5494a61cd66SEric Lapuyade 		shdlc->ns = (shdlc->ns + 1) % 8;
5504a61cd66SEric Lapuyade 
5514a61cd66SEric Lapuyade 		time_sent = jiffies;
5524a61cd66SEric Lapuyade 		*(unsigned long *)skb->cb = time_sent;
5534a61cd66SEric Lapuyade 
5544a61cd66SEric Lapuyade 		skb_queue_tail(&shdlc->ack_pending_q, skb);
5554a61cd66SEric Lapuyade 
5564a61cd66SEric Lapuyade 		if (shdlc->t2_active == false) {
5574a61cd66SEric Lapuyade 			shdlc->t2_active = true;
5584a61cd66SEric Lapuyade 			mod_timer(&shdlc->t2_timer, time_sent +
5594a61cd66SEric Lapuyade 				  msecs_to_jiffies(SHDLC_T2_VALUE_MS));
5604a61cd66SEric Lapuyade 			pr_debug("Started T2 (retransmit)\n");
5614a61cd66SEric Lapuyade 		}
5624a61cd66SEric Lapuyade 	}
5634a61cd66SEric Lapuyade }
5644a61cd66SEric Lapuyade 
llc_shdlc_connect_timeout(struct timer_list * t)5654b519bb4SAllen Pais static void llc_shdlc_connect_timeout(struct timer_list *t)
5664a61cd66SEric Lapuyade {
5674b519bb4SAllen Pais 	struct llc_shdlc *shdlc = from_timer(shdlc, t, connect_timer);
5684a61cd66SEric Lapuyade 
569916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5704a61cd66SEric Lapuyade }
5714a61cd66SEric Lapuyade 
llc_shdlc_t1_timeout(struct timer_list * t)5724b519bb4SAllen Pais static void llc_shdlc_t1_timeout(struct timer_list *t)
5734a61cd66SEric Lapuyade {
5744b519bb4SAllen Pais 	struct llc_shdlc *shdlc = from_timer(shdlc, t, t1_timer);
5754a61cd66SEric Lapuyade 
5764a61cd66SEric Lapuyade 	pr_debug("SoftIRQ: need to send ack\n");
5774a61cd66SEric Lapuyade 
578916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5794a61cd66SEric Lapuyade }
5804a61cd66SEric Lapuyade 
llc_shdlc_t2_timeout(struct timer_list * t)5814b519bb4SAllen Pais static void llc_shdlc_t2_timeout(struct timer_list *t)
5824a61cd66SEric Lapuyade {
5834b519bb4SAllen Pais 	struct llc_shdlc *shdlc = from_timer(shdlc, t, t2_timer);
5844a61cd66SEric Lapuyade 
5854a61cd66SEric Lapuyade 	pr_debug("SoftIRQ: need to retransmit\n");
5864a61cd66SEric Lapuyade 
587916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5884a61cd66SEric Lapuyade }
5894a61cd66SEric Lapuyade 
llc_shdlc_sm_work(struct work_struct * work)5904a61cd66SEric Lapuyade static void llc_shdlc_sm_work(struct work_struct *work)
5914a61cd66SEric Lapuyade {
5924a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
5934a61cd66SEric Lapuyade 	int r;
5944a61cd66SEric Lapuyade 
5954a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
5964a61cd66SEric Lapuyade 
5974a61cd66SEric Lapuyade 	switch (shdlc->state) {
5984a61cd66SEric Lapuyade 	case SHDLC_DISCONNECTED:
5994a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->rcv_q);
6004a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->send_q);
6014a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->ack_pending_q);
6024a61cd66SEric Lapuyade 		break;
6034a61cd66SEric Lapuyade 	case SHDLC_CONNECTING:
6044a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6054a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
6064a61cd66SEric Lapuyade 			break;
6074a61cd66SEric Lapuyade 		}
6084a61cd66SEric Lapuyade 
6094a61cd66SEric Lapuyade 		if (shdlc->connect_tries++ < 5)
6104a61cd66SEric Lapuyade 			r = llc_shdlc_connect_initiate(shdlc);
6114a61cd66SEric Lapuyade 		else
6124a61cd66SEric Lapuyade 			r = -ETIME;
6130f450772SSzymon Janc 		if (r < 0) {
6144a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, r);
6150f450772SSzymon Janc 		} else {
6164a61cd66SEric Lapuyade 			mod_timer(&shdlc->connect_timer, jiffies +
6174a61cd66SEric Lapuyade 				  msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
6184a61cd66SEric Lapuyade 
6194c0ba9acSWaldemar Rymarkiewicz 			shdlc->state = SHDLC_NEGOTIATING;
6204a61cd66SEric Lapuyade 		}
6214a61cd66SEric Lapuyade 		break;
6224c0ba9acSWaldemar Rymarkiewicz 	case SHDLC_NEGOTIATING:
6234a61cd66SEric Lapuyade 		if (timer_pending(&shdlc->connect_timer) == 0) {
6244a61cd66SEric Lapuyade 			shdlc->state = SHDLC_CONNECTING;
625916082b0SLinus Torvalds 			schedule_work(&shdlc->sm_work);
6264a61cd66SEric Lapuyade 		}
6274a61cd66SEric Lapuyade 
6284a61cd66SEric Lapuyade 		llc_shdlc_handle_rcv_queue(shdlc);
6294a61cd66SEric Lapuyade 
6304a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6314a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
6324a61cd66SEric Lapuyade 			break;
6334a61cd66SEric Lapuyade 		}
6344a61cd66SEric Lapuyade 		break;
63512bfd1e8SWaldemar Rymarkiewicz 	case SHDLC_HALF_CONNECTED:
6364a61cd66SEric Lapuyade 	case SHDLC_CONNECTED:
6374a61cd66SEric Lapuyade 		llc_shdlc_handle_rcv_queue(shdlc);
6384a61cd66SEric Lapuyade 		llc_shdlc_handle_send_queue(shdlc);
6394a61cd66SEric Lapuyade 
6404a61cd66SEric Lapuyade 		if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
641*3537e507SKrzysztof Kozlowski 			pr_debug("Handle T1(send ack) elapsed (T1 now inactive)\n");
6424a61cd66SEric Lapuyade 
6434a61cd66SEric Lapuyade 			shdlc->t1_active = false;
6444a61cd66SEric Lapuyade 			r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
6454a61cd66SEric Lapuyade 						   shdlc->nr);
6464a61cd66SEric Lapuyade 			if (r < 0)
6474a61cd66SEric Lapuyade 				shdlc->hard_fault = r;
6484a61cd66SEric Lapuyade 		}
6494a61cd66SEric Lapuyade 
6504a61cd66SEric Lapuyade 		if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
651*3537e507SKrzysztof Kozlowski 			pr_debug("Handle T2(retransmit) elapsed (T2 inactive)\n");
6524a61cd66SEric Lapuyade 
6534a61cd66SEric Lapuyade 			shdlc->t2_active = false;
6544a61cd66SEric Lapuyade 
6554a61cd66SEric Lapuyade 			llc_shdlc_requeue_ack_pending(shdlc);
6564a61cd66SEric Lapuyade 			llc_shdlc_handle_send_queue(shdlc);
6574a61cd66SEric Lapuyade 		}
6584a61cd66SEric Lapuyade 
6590f450772SSzymon Janc 		if (shdlc->hard_fault)
6604a61cd66SEric Lapuyade 			shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
6614a61cd66SEric Lapuyade 		break;
6624a61cd66SEric Lapuyade 	default:
6634a61cd66SEric Lapuyade 		break;
6644a61cd66SEric Lapuyade 	}
6654a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
6664a61cd66SEric Lapuyade }
6674a61cd66SEric Lapuyade 
6684a61cd66SEric Lapuyade /*
6694a61cd66SEric Lapuyade  * Called from syscall context to establish shdlc link. Sleeps until
6704a61cd66SEric Lapuyade  * link is ready or failure.
6714a61cd66SEric Lapuyade  */
llc_shdlc_connect(struct llc_shdlc * shdlc)6724a61cd66SEric Lapuyade static int llc_shdlc_connect(struct llc_shdlc *shdlc)
6734a61cd66SEric Lapuyade {
6744a61cd66SEric Lapuyade 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
6754a61cd66SEric Lapuyade 
6764a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
6774a61cd66SEric Lapuyade 
6784a61cd66SEric Lapuyade 	shdlc->state = SHDLC_CONNECTING;
6794a61cd66SEric Lapuyade 	shdlc->connect_wq = &connect_wq;
6804a61cd66SEric Lapuyade 	shdlc->connect_tries = 0;
6814a61cd66SEric Lapuyade 	shdlc->connect_result = 1;
6824a61cd66SEric Lapuyade 
6834a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
6844a61cd66SEric Lapuyade 
685916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
6864a61cd66SEric Lapuyade 
6874a61cd66SEric Lapuyade 	wait_event(connect_wq, shdlc->connect_result != 1);
6884a61cd66SEric Lapuyade 
6894a61cd66SEric Lapuyade 	return shdlc->connect_result;
6904a61cd66SEric Lapuyade }
6914a61cd66SEric Lapuyade 
llc_shdlc_disconnect(struct llc_shdlc * shdlc)6924a61cd66SEric Lapuyade static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
6934a61cd66SEric Lapuyade {
6944a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
6954a61cd66SEric Lapuyade 
6964a61cd66SEric Lapuyade 	shdlc->state = SHDLC_DISCONNECTED;
6974a61cd66SEric Lapuyade 
6984a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
6994a61cd66SEric Lapuyade 
700916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7014a61cd66SEric Lapuyade }
7024a61cd66SEric Lapuyade 
7034a61cd66SEric Lapuyade /*
7044a61cd66SEric Lapuyade  * Receive an incoming shdlc frame. Frame has already been crc-validated.
7054a61cd66SEric Lapuyade  * skb contains only LLC header and payload.
7064a61cd66SEric Lapuyade  * If skb == NULL, it is a notification that the link below is dead.
7074a61cd66SEric Lapuyade  */
llc_shdlc_recv_frame(struct llc_shdlc * shdlc,struct sk_buff * skb)7084a61cd66SEric Lapuyade static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
7094a61cd66SEric Lapuyade {
7104a61cd66SEric Lapuyade 	if (skb == NULL) {
7114a61cd66SEric Lapuyade 		pr_err("NULL Frame -> link is dead\n");
7124a61cd66SEric Lapuyade 		shdlc->hard_fault = -EREMOTEIO;
7134a61cd66SEric Lapuyade 	} else {
7144a61cd66SEric Lapuyade 		SHDLC_DUMP_SKB("incoming frame", skb);
7154a61cd66SEric Lapuyade 		skb_queue_tail(&shdlc->rcv_q, skb);
7164a61cd66SEric Lapuyade 	}
7174a61cd66SEric Lapuyade 
718916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7194a61cd66SEric Lapuyade }
7204a61cd66SEric Lapuyade 
llc_shdlc_init(struct nfc_hci_dev * hdev,xmit_to_drv_t xmit_to_drv,rcv_to_hci_t rcv_to_hci,int tx_headroom,int tx_tailroom,int * rx_headroom,int * rx_tailroom,llc_failure_t llc_failure)7214a61cd66SEric Lapuyade static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
7224a61cd66SEric Lapuyade 			    rcv_to_hci_t rcv_to_hci, int tx_headroom,
7234a61cd66SEric Lapuyade 			    int tx_tailroom, int *rx_headroom, int *rx_tailroom,
7244a61cd66SEric Lapuyade 			    llc_failure_t llc_failure)
7254a61cd66SEric Lapuyade {
7264a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc;
7274a61cd66SEric Lapuyade 
7284a61cd66SEric Lapuyade 	*rx_headroom = SHDLC_LLC_HEAD_ROOM;
7294a61cd66SEric Lapuyade 	*rx_tailroom = 0;
7304a61cd66SEric Lapuyade 
7314a61cd66SEric Lapuyade 	shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
7324a61cd66SEric Lapuyade 	if (shdlc == NULL)
7334a61cd66SEric Lapuyade 		return NULL;
7344a61cd66SEric Lapuyade 
7354a61cd66SEric Lapuyade 	mutex_init(&shdlc->state_mutex);
7364a61cd66SEric Lapuyade 	shdlc->state = SHDLC_DISCONNECTED;
7374a61cd66SEric Lapuyade 
7384b519bb4SAllen Pais 	timer_setup(&shdlc->connect_timer, llc_shdlc_connect_timeout, 0);
7394b519bb4SAllen Pais 	timer_setup(&shdlc->t1_timer, llc_shdlc_t1_timeout, 0);
7404b519bb4SAllen Pais 	timer_setup(&shdlc->t2_timer, llc_shdlc_t2_timeout, 0);
7414a61cd66SEric Lapuyade 
7424a61cd66SEric Lapuyade 	shdlc->w = SHDLC_MAX_WINDOW;
7434a61cd66SEric Lapuyade 	shdlc->srej_support = SHDLC_SREJ_SUPPORT;
7444a61cd66SEric Lapuyade 
7454a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->rcv_q);
7464a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->send_q);
7474a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->ack_pending_q);
7484a61cd66SEric Lapuyade 
7494a61cd66SEric Lapuyade 	INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
7504a61cd66SEric Lapuyade 
7514a61cd66SEric Lapuyade 	shdlc->hdev = hdev;
7524a61cd66SEric Lapuyade 	shdlc->xmit_to_drv = xmit_to_drv;
7534a61cd66SEric Lapuyade 	shdlc->rcv_to_hci = rcv_to_hci;
7544a61cd66SEric Lapuyade 	shdlc->tx_headroom = tx_headroom;
7554a61cd66SEric Lapuyade 	shdlc->tx_tailroom = tx_tailroom;
7564a61cd66SEric Lapuyade 	shdlc->llc_failure = llc_failure;
7574a61cd66SEric Lapuyade 
7584a61cd66SEric Lapuyade 	return shdlc;
7594a61cd66SEric Lapuyade }
7604a61cd66SEric Lapuyade 
llc_shdlc_deinit(struct nfc_llc * llc)7614a61cd66SEric Lapuyade static void llc_shdlc_deinit(struct nfc_llc *llc)
7624a61cd66SEric Lapuyade {
7634a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7644a61cd66SEric Lapuyade 
7654a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->rcv_q);
7664a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->send_q);
7674a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->ack_pending_q);
7684a61cd66SEric Lapuyade 
7694a61cd66SEric Lapuyade 	kfree(shdlc);
7704a61cd66SEric Lapuyade }
7714a61cd66SEric Lapuyade 
llc_shdlc_start(struct nfc_llc * llc)7724a61cd66SEric Lapuyade static int llc_shdlc_start(struct nfc_llc *llc)
7734a61cd66SEric Lapuyade {
7744a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7754a61cd66SEric Lapuyade 
7764a61cd66SEric Lapuyade 	return llc_shdlc_connect(shdlc);
7774a61cd66SEric Lapuyade }
7784a61cd66SEric Lapuyade 
llc_shdlc_stop(struct nfc_llc * llc)7794a61cd66SEric Lapuyade static int llc_shdlc_stop(struct nfc_llc *llc)
7804a61cd66SEric Lapuyade {
7814a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7824a61cd66SEric Lapuyade 
7834a61cd66SEric Lapuyade 	llc_shdlc_disconnect(shdlc);
7844a61cd66SEric Lapuyade 
7854a61cd66SEric Lapuyade 	return 0;
7864a61cd66SEric Lapuyade }
7874a61cd66SEric Lapuyade 
llc_shdlc_rcv_from_drv(struct nfc_llc * llc,struct sk_buff * skb)7884a61cd66SEric Lapuyade static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
7894a61cd66SEric Lapuyade {
7904a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7914a61cd66SEric Lapuyade 
7924a61cd66SEric Lapuyade 	llc_shdlc_recv_frame(shdlc, skb);
7934a61cd66SEric Lapuyade }
7944a61cd66SEric Lapuyade 
llc_shdlc_xmit_from_hci(struct nfc_llc * llc,struct sk_buff * skb)7954a61cd66SEric Lapuyade static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
7964a61cd66SEric Lapuyade {
7974a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7984a61cd66SEric Lapuyade 
7994a61cd66SEric Lapuyade 	skb_queue_tail(&shdlc->send_q, skb);
8004a61cd66SEric Lapuyade 
801916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
8024a61cd66SEric Lapuyade 
8034a61cd66SEric Lapuyade 	return 0;
8044a61cd66SEric Lapuyade }
8054a61cd66SEric Lapuyade 
80649545357SKrzysztof Kozlowski static const struct nfc_llc_ops llc_shdlc_ops = {
8074a61cd66SEric Lapuyade 	.init = llc_shdlc_init,
8084a61cd66SEric Lapuyade 	.deinit = llc_shdlc_deinit,
8094a61cd66SEric Lapuyade 	.start = llc_shdlc_start,
8104a61cd66SEric Lapuyade 	.stop = llc_shdlc_stop,
8114a61cd66SEric Lapuyade 	.rcv_from_drv = llc_shdlc_rcv_from_drv,
8124a61cd66SEric Lapuyade 	.xmit_from_hci = llc_shdlc_xmit_from_hci,
8134a61cd66SEric Lapuyade };
8144a61cd66SEric Lapuyade 
nfc_llc_shdlc_register(void)815a7d0281bSEric Lapuyade int nfc_llc_shdlc_register(void)
8164a61cd66SEric Lapuyade {
8174a61cd66SEric Lapuyade 	return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);
8184a61cd66SEric Lapuyade }
819