xref: /openbmc/linux/net/nfc/hci/llc_shdlc.c (revision 49545357bf7e134a4012d4652c2df5f78f4485af)
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 */
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 */
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 
1264a61cd66SEric Lapuyade static struct sk_buff *llc_shdlc_alloc_skb(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. */
1404a61cd66SEric Lapuyade static int llc_shdlc_send_s_frame(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 */
1624a61cd66SEric Lapuyade static int llc_shdlc_send_u_frame(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  */
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 
2044a61cd66SEric Lapuyade 			pr_debug
2054a61cd66SEric Lapuyade 			    ("All sent frames acked. Stopped T2(retransmit)\n");
2064a61cd66SEric Lapuyade 		}
2074a61cd66SEric Lapuyade 	} else {
2084a61cd66SEric Lapuyade 		skb = skb_peek(&shdlc->ack_pending_q);
2094a61cd66SEric Lapuyade 
2104a61cd66SEric Lapuyade 		mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
2114a61cd66SEric Lapuyade 			  msecs_to_jiffies(SHDLC_T2_VALUE_MS));
2124a61cd66SEric Lapuyade 		shdlc->t2_active = true;
2134a61cd66SEric Lapuyade 
2144a61cd66SEric Lapuyade 		pr_debug
2154a61cd66SEric Lapuyade 		    ("Start T2(retransmit) for remaining unacked sent frames\n");
2164a61cd66SEric Lapuyade 	}
2174a61cd66SEric Lapuyade }
2184a61cd66SEric Lapuyade 
2194a61cd66SEric Lapuyade /*
2204a61cd66SEric Lapuyade  * Receive validated frames from lower layer. skb contains HCI payload only.
2214a61cd66SEric Lapuyade  * Handle according to algorithm at spec:10.8.2
2224a61cd66SEric Lapuyade  */
2234a61cd66SEric Lapuyade static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
2244a61cd66SEric Lapuyade 				  struct sk_buff *skb, int ns, int nr)
2254a61cd66SEric Lapuyade {
2264a61cd66SEric Lapuyade 	int x_ns = ns;
2274a61cd66SEric Lapuyade 	int y_nr = nr;
2284a61cd66SEric Lapuyade 
2294a61cd66SEric Lapuyade 	pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
2304a61cd66SEric Lapuyade 
2314a61cd66SEric Lapuyade 	if (shdlc->state != SHDLC_CONNECTED)
2324a61cd66SEric Lapuyade 		goto exit;
2334a61cd66SEric Lapuyade 
2344a61cd66SEric Lapuyade 	if (x_ns != shdlc->nr) {
2354a61cd66SEric Lapuyade 		llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
2364a61cd66SEric Lapuyade 		goto exit;
2374a61cd66SEric Lapuyade 	}
2384a61cd66SEric Lapuyade 
239f50e2f9fSYANG LI 	if (!shdlc->t1_active) {
2404a61cd66SEric Lapuyade 		shdlc->t1_active = true;
2414a61cd66SEric Lapuyade 		mod_timer(&shdlc->t1_timer, jiffies +
2424a61cd66SEric Lapuyade 			  msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
2434a61cd66SEric Lapuyade 		pr_debug("(re)Start T1(send ack)\n");
2444a61cd66SEric Lapuyade 	}
2454a61cd66SEric Lapuyade 
2464a61cd66SEric Lapuyade 	if (skb->len) {
2474a61cd66SEric Lapuyade 		shdlc->rcv_to_hci(shdlc->hdev, skb);
2484a61cd66SEric Lapuyade 		skb = NULL;
2494a61cd66SEric Lapuyade 	}
2504a61cd66SEric Lapuyade 
2514a61cd66SEric Lapuyade 	shdlc->nr = (shdlc->nr + 1) % 8;
2524a61cd66SEric Lapuyade 
2534a61cd66SEric Lapuyade 	if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
2544a61cd66SEric Lapuyade 		llc_shdlc_reset_t2(shdlc, y_nr);
2554a61cd66SEric Lapuyade 
2564a61cd66SEric Lapuyade 		shdlc->dnr = y_nr;
2574a61cd66SEric Lapuyade 	}
2584a61cd66SEric Lapuyade 
2594a61cd66SEric Lapuyade exit:
2604a61cd66SEric Lapuyade 	kfree_skb(skb);
2614a61cd66SEric Lapuyade }
2624a61cd66SEric Lapuyade 
2634a61cd66SEric Lapuyade static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
2644a61cd66SEric Lapuyade {
2654a61cd66SEric Lapuyade 	pr_debug("remote acked up to frame %d excluded\n", y_nr);
2664a61cd66SEric Lapuyade 
2674a61cd66SEric Lapuyade 	if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
2684a61cd66SEric Lapuyade 		llc_shdlc_reset_t2(shdlc, y_nr);
2694a61cd66SEric Lapuyade 		shdlc->dnr = y_nr;
2704a61cd66SEric Lapuyade 	}
2714a61cd66SEric Lapuyade }
2724a61cd66SEric Lapuyade 
2734a61cd66SEric Lapuyade static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
2744a61cd66SEric Lapuyade {
2754a61cd66SEric Lapuyade 	struct sk_buff *skb;
2764a61cd66SEric Lapuyade 
2774a61cd66SEric Lapuyade 	pr_debug("ns reset to %d\n", shdlc->dnr);
2784a61cd66SEric Lapuyade 
2794a61cd66SEric Lapuyade 	while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
2804a61cd66SEric Lapuyade 		skb_pull(skb, 1);	/* remove control field */
2814a61cd66SEric Lapuyade 		skb_queue_head(&shdlc->send_q, skb);
2824a61cd66SEric Lapuyade 	}
2834a61cd66SEric Lapuyade 	shdlc->ns = shdlc->dnr;
2844a61cd66SEric Lapuyade }
2854a61cd66SEric Lapuyade 
2864a61cd66SEric Lapuyade static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
2874a61cd66SEric Lapuyade {
2884a61cd66SEric Lapuyade 	struct sk_buff *skb;
2894a61cd66SEric Lapuyade 
29088bfe6eaSGeert Uytterhoeven 	pr_debug("remote asks retransmission from frame %d\n", y_nr);
2914a61cd66SEric Lapuyade 
2924a61cd66SEric Lapuyade 	if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
2934a61cd66SEric Lapuyade 		if (shdlc->t2_active) {
2944a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t2_timer);
2954a61cd66SEric Lapuyade 			shdlc->t2_active = false;
2964a61cd66SEric Lapuyade 			pr_debug("Stopped T2(retransmit)\n");
2974a61cd66SEric Lapuyade 		}
2984a61cd66SEric Lapuyade 
2994a61cd66SEric Lapuyade 		if (shdlc->dnr != y_nr) {
3004a61cd66SEric Lapuyade 			while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
3014a61cd66SEric Lapuyade 				skb = skb_dequeue(&shdlc->ack_pending_q);
3024a61cd66SEric Lapuyade 				kfree_skb(skb);
3034a61cd66SEric Lapuyade 			}
3044a61cd66SEric Lapuyade 		}
3054a61cd66SEric Lapuyade 
3064a61cd66SEric Lapuyade 		llc_shdlc_requeue_ack_pending(shdlc);
3074a61cd66SEric Lapuyade 	}
3084a61cd66SEric Lapuyade }
3094a61cd66SEric Lapuyade 
3104a61cd66SEric Lapuyade /* See spec RR:10.8.3 REJ:10.8.4 */
3114a61cd66SEric Lapuyade static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
3124a61cd66SEric Lapuyade 				  enum sframe_type s_frame_type, int nr)
3134a61cd66SEric Lapuyade {
3144a61cd66SEric Lapuyade 	struct sk_buff *skb;
3154a61cd66SEric Lapuyade 
3164a61cd66SEric Lapuyade 	if (shdlc->state != SHDLC_CONNECTED)
3174a61cd66SEric Lapuyade 		return;
3184a61cd66SEric Lapuyade 
3194a61cd66SEric Lapuyade 	switch (s_frame_type) {
3204a61cd66SEric Lapuyade 	case S_FRAME_RR:
3214a61cd66SEric Lapuyade 		llc_shdlc_rcv_ack(shdlc, nr);
3224a61cd66SEric Lapuyade 		if (shdlc->rnr == true) {	/* see SHDLC 10.7.7 */
3234a61cd66SEric Lapuyade 			shdlc->rnr = false;
3244a61cd66SEric Lapuyade 			if (shdlc->send_q.qlen == 0) {
3254a61cd66SEric Lapuyade 				skb = llc_shdlc_alloc_skb(shdlc, 0);
3264a61cd66SEric Lapuyade 				if (skb)
3274a61cd66SEric Lapuyade 					skb_queue_tail(&shdlc->send_q, skb);
3284a61cd66SEric Lapuyade 			}
3294a61cd66SEric Lapuyade 		}
3304a61cd66SEric Lapuyade 		break;
3314a61cd66SEric Lapuyade 	case S_FRAME_REJ:
3324a61cd66SEric Lapuyade 		llc_shdlc_rcv_rej(shdlc, nr);
3334a61cd66SEric Lapuyade 		break;
3344a61cd66SEric Lapuyade 	case S_FRAME_RNR:
3354a61cd66SEric Lapuyade 		llc_shdlc_rcv_ack(shdlc, nr);
3364a61cd66SEric Lapuyade 		shdlc->rnr = true;
3374a61cd66SEric Lapuyade 		break;
3384a61cd66SEric Lapuyade 	default:
3394a61cd66SEric Lapuyade 		break;
3404a61cd66SEric Lapuyade 	}
3414a61cd66SEric Lapuyade }
3424a61cd66SEric Lapuyade 
3434a61cd66SEric Lapuyade static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
3444a61cd66SEric Lapuyade {
3454a61cd66SEric Lapuyade 	pr_debug("result=%d\n", r);
3464a61cd66SEric Lapuyade 
3474a61cd66SEric Lapuyade 	del_timer_sync(&shdlc->connect_timer);
3484a61cd66SEric Lapuyade 
3494a61cd66SEric Lapuyade 	if (r == 0) {
3504a61cd66SEric Lapuyade 		shdlc->ns = 0;
3514a61cd66SEric Lapuyade 		shdlc->nr = 0;
3524a61cd66SEric Lapuyade 		shdlc->dnr = 0;
3534a61cd66SEric Lapuyade 
35412bfd1e8SWaldemar Rymarkiewicz 		shdlc->state = SHDLC_HALF_CONNECTED;
3554a61cd66SEric Lapuyade 	} else {
3564a61cd66SEric Lapuyade 		shdlc->state = SHDLC_DISCONNECTED;
3574a61cd66SEric Lapuyade 	}
3584a61cd66SEric Lapuyade 
3594a61cd66SEric Lapuyade 	shdlc->connect_result = r;
3604a61cd66SEric Lapuyade 
3614a61cd66SEric Lapuyade 	wake_up(shdlc->connect_wq);
3624a61cd66SEric Lapuyade }
3634a61cd66SEric Lapuyade 
3644a61cd66SEric Lapuyade static int llc_shdlc_connect_initiate(struct llc_shdlc *shdlc)
3654a61cd66SEric Lapuyade {
3664a61cd66SEric Lapuyade 	struct sk_buff *skb;
3674a61cd66SEric Lapuyade 
3684a61cd66SEric Lapuyade 	pr_debug("\n");
3694a61cd66SEric Lapuyade 
3704a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 2);
3714a61cd66SEric Lapuyade 	if (skb == NULL)
3724a61cd66SEric Lapuyade 		return -ENOMEM;
3734a61cd66SEric Lapuyade 
374634fef61SJohannes Berg 	skb_put_u8(skb, SHDLC_MAX_WINDOW);
375634fef61SJohannes Berg 	skb_put_u8(skb, SHDLC_SREJ_SUPPORT ? 1 : 0);
3764a61cd66SEric Lapuyade 
3774a61cd66SEric Lapuyade 	return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
3784a61cd66SEric Lapuyade }
3794a61cd66SEric Lapuyade 
3804a61cd66SEric Lapuyade static int llc_shdlc_connect_send_ua(struct llc_shdlc *shdlc)
3814a61cd66SEric Lapuyade {
3824a61cd66SEric Lapuyade 	struct sk_buff *skb;
3834a61cd66SEric Lapuyade 
3844a61cd66SEric Lapuyade 	pr_debug("\n");
3854a61cd66SEric Lapuyade 
3864a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 0);
3874a61cd66SEric Lapuyade 	if (skb == NULL)
3884a61cd66SEric Lapuyade 		return -ENOMEM;
3894a61cd66SEric Lapuyade 
3904a61cd66SEric Lapuyade 	return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
3914a61cd66SEric Lapuyade }
3924a61cd66SEric Lapuyade 
3934a61cd66SEric Lapuyade static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
3944a61cd66SEric Lapuyade 				  struct sk_buff *skb,
3954a61cd66SEric Lapuyade 				  enum uframe_modifier u_frame_modifier)
3964a61cd66SEric Lapuyade {
3974a61cd66SEric Lapuyade 	u8 w = SHDLC_MAX_WINDOW;
3984a61cd66SEric Lapuyade 	bool srej_support = SHDLC_SREJ_SUPPORT;
3994a61cd66SEric Lapuyade 	int r;
4004a61cd66SEric Lapuyade 
4014a61cd66SEric Lapuyade 	pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
4024a61cd66SEric Lapuyade 
4034a61cd66SEric Lapuyade 	switch (u_frame_modifier) {
4044a61cd66SEric Lapuyade 	case U_FRAME_RSET:
40512bfd1e8SWaldemar Rymarkiewicz 		switch (shdlc->state) {
4064c0ba9acSWaldemar Rymarkiewicz 		case SHDLC_NEGOTIATING:
40712bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_CONNECTING:
40812bfd1e8SWaldemar Rymarkiewicz 			/*
40901709d09SZheng Yongjun 			 * We sent RSET, but chip wants to negotiate or we
41012bfd1e8SWaldemar Rymarkiewicz 			 * got RSET before we managed to send out our.
41112bfd1e8SWaldemar Rymarkiewicz 			 */
4124a61cd66SEric Lapuyade 			if (skb->len > 0)
4134a61cd66SEric Lapuyade 				w = skb->data[0];
4144a61cd66SEric Lapuyade 
4154a61cd66SEric Lapuyade 			if (skb->len > 1)
4164a61cd66SEric Lapuyade 				srej_support = skb->data[1] & 0x01 ? true :
4174a61cd66SEric Lapuyade 					       false;
4184a61cd66SEric Lapuyade 
4194a61cd66SEric Lapuyade 			if ((w <= SHDLC_MAX_WINDOW) &&
4204a61cd66SEric Lapuyade 			    (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
4214a61cd66SEric Lapuyade 				shdlc->w = w;
4224a61cd66SEric Lapuyade 				shdlc->srej_support = srej_support;
4234a61cd66SEric Lapuyade 				r = llc_shdlc_connect_send_ua(shdlc);
4244a61cd66SEric Lapuyade 				llc_shdlc_connect_complete(shdlc, r);
4254a61cd66SEric Lapuyade 			}
42612bfd1e8SWaldemar Rymarkiewicz 			break;
42712bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_HALF_CONNECTED:
42812bfd1e8SWaldemar Rymarkiewicz 			/*
42912bfd1e8SWaldemar Rymarkiewicz 			 * Chip resent RSET due to its timeout - Ignote it
43012bfd1e8SWaldemar Rymarkiewicz 			 * as we already sent UA.
43112bfd1e8SWaldemar Rymarkiewicz 			 */
43212bfd1e8SWaldemar Rymarkiewicz 			break;
43312bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_CONNECTED:
4344a61cd66SEric Lapuyade 			/*
4354a61cd66SEric Lapuyade 			 * Chip wants to reset link. This is unexpected and
4364a61cd66SEric Lapuyade 			 * unsupported.
4374a61cd66SEric Lapuyade 			 */
4384a61cd66SEric Lapuyade 			shdlc->hard_fault = -ECONNRESET;
43912bfd1e8SWaldemar Rymarkiewicz 			break;
44012bfd1e8SWaldemar Rymarkiewicz 		default:
44112bfd1e8SWaldemar Rymarkiewicz 			break;
4424a61cd66SEric Lapuyade 		}
4434a61cd66SEric Lapuyade 		break;
4444a61cd66SEric Lapuyade 	case U_FRAME_UA:
4454a61cd66SEric Lapuyade 		if ((shdlc->state == SHDLC_CONNECTING &&
4464a61cd66SEric Lapuyade 		     shdlc->connect_tries > 0) ||
4474c0ba9acSWaldemar Rymarkiewicz 		    (shdlc->state == SHDLC_NEGOTIATING)) {
4484a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, 0);
44912bfd1e8SWaldemar Rymarkiewicz 			shdlc->state = SHDLC_CONNECTED;
45012bfd1e8SWaldemar Rymarkiewicz 		}
4514a61cd66SEric Lapuyade 		break;
4524a61cd66SEric Lapuyade 	default:
4534a61cd66SEric Lapuyade 		break;
4544a61cd66SEric Lapuyade 	}
4554a61cd66SEric Lapuyade 
4564a61cd66SEric Lapuyade 	kfree_skb(skb);
4574a61cd66SEric Lapuyade }
4584a61cd66SEric Lapuyade 
4594a61cd66SEric Lapuyade static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
4604a61cd66SEric Lapuyade {
4614a61cd66SEric Lapuyade 	struct sk_buff *skb;
4624a61cd66SEric Lapuyade 	u8 control;
4634a61cd66SEric Lapuyade 	int nr;
4644a61cd66SEric Lapuyade 	int ns;
4654a61cd66SEric Lapuyade 	enum sframe_type s_frame_type;
4664a61cd66SEric Lapuyade 	enum uframe_modifier u_frame_modifier;
4674a61cd66SEric Lapuyade 
4684a61cd66SEric Lapuyade 	if (shdlc->rcv_q.qlen)
4694a61cd66SEric Lapuyade 		pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
4704a61cd66SEric Lapuyade 
4714a61cd66SEric Lapuyade 	while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
4724a61cd66SEric Lapuyade 		control = skb->data[0];
4734a61cd66SEric Lapuyade 		skb_pull(skb, 1);
4744a61cd66SEric Lapuyade 		switch (control & SHDLC_CONTROL_HEAD_MASK) {
4754a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_I:
4764a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_I2:
47712bfd1e8SWaldemar Rymarkiewicz 			if (shdlc->state == SHDLC_HALF_CONNECTED)
47812bfd1e8SWaldemar Rymarkiewicz 				shdlc->state = SHDLC_CONNECTED;
47912bfd1e8SWaldemar Rymarkiewicz 
4804a61cd66SEric Lapuyade 			ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
4814a61cd66SEric Lapuyade 			nr = control & SHDLC_CONTROL_NR_MASK;
4824a61cd66SEric Lapuyade 			llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
4834a61cd66SEric Lapuyade 			break;
4844a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_S:
48512bfd1e8SWaldemar Rymarkiewicz 			if (shdlc->state == SHDLC_HALF_CONNECTED)
48612bfd1e8SWaldemar Rymarkiewicz 				shdlc->state = SHDLC_CONNECTED;
48712bfd1e8SWaldemar Rymarkiewicz 
4884a61cd66SEric Lapuyade 			s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
4894a61cd66SEric Lapuyade 			nr = control & SHDLC_CONTROL_NR_MASK;
4904a61cd66SEric Lapuyade 			llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
4914a61cd66SEric Lapuyade 			kfree_skb(skb);
4924a61cd66SEric Lapuyade 			break;
4934a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_U:
4944a61cd66SEric Lapuyade 			u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
4954a61cd66SEric Lapuyade 			llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
4964a61cd66SEric Lapuyade 			break;
4974a61cd66SEric Lapuyade 		default:
4984a61cd66SEric Lapuyade 			pr_err("UNKNOWN Control=%d\n", control);
4994a61cd66SEric Lapuyade 			kfree_skb(skb);
5004a61cd66SEric Lapuyade 			break;
5014a61cd66SEric Lapuyade 		}
5024a61cd66SEric Lapuyade 	}
5034a61cd66SEric Lapuyade }
5044a61cd66SEric Lapuyade 
5054a61cd66SEric Lapuyade static int llc_shdlc_w_used(int ns, int dnr)
5064a61cd66SEric Lapuyade {
5074a61cd66SEric Lapuyade 	int unack_count;
5084a61cd66SEric Lapuyade 
5094a61cd66SEric Lapuyade 	if (dnr <= ns)
5104a61cd66SEric Lapuyade 		unack_count = ns - dnr;
5114a61cd66SEric Lapuyade 	else
5124a61cd66SEric Lapuyade 		unack_count = 8 - dnr + ns;
5134a61cd66SEric Lapuyade 
5144a61cd66SEric Lapuyade 	return unack_count;
5154a61cd66SEric Lapuyade }
5164a61cd66SEric Lapuyade 
5174a61cd66SEric Lapuyade /* Send frames according to algorithm at spec:10.8.1 */
5184a61cd66SEric Lapuyade static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
5194a61cd66SEric Lapuyade {
5204a61cd66SEric Lapuyade 	struct sk_buff *skb;
5214a61cd66SEric Lapuyade 	int r;
5224a61cd66SEric Lapuyade 	unsigned long time_sent;
5234a61cd66SEric Lapuyade 
5244a61cd66SEric Lapuyade 	if (shdlc->send_q.qlen)
5254a61cd66SEric Lapuyade 		pr_debug
5264a61cd66SEric Lapuyade 		    ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
5274a61cd66SEric Lapuyade 		     shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
5284a61cd66SEric Lapuyade 		     shdlc->rnr == false ? "false" : "true",
5294a61cd66SEric Lapuyade 		     shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
5304a61cd66SEric Lapuyade 		     shdlc->ack_pending_q.qlen);
5314a61cd66SEric Lapuyade 
5324a61cd66SEric Lapuyade 	while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
5334a61cd66SEric Lapuyade 	       (shdlc->rnr == false)) {
5344a61cd66SEric Lapuyade 
5354a61cd66SEric Lapuyade 		if (shdlc->t1_active) {
5364a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t1_timer);
5374a61cd66SEric Lapuyade 			shdlc->t1_active = false;
5384a61cd66SEric Lapuyade 			pr_debug("Stopped T1(send ack)\n");
5394a61cd66SEric Lapuyade 		}
5404a61cd66SEric Lapuyade 
5414a61cd66SEric Lapuyade 		skb = skb_dequeue(&shdlc->send_q);
5424a61cd66SEric Lapuyade 
543d58ff351SJohannes Berg 		*(u8 *)skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
5444a61cd66SEric Lapuyade 					shdlc->nr;
5454a61cd66SEric Lapuyade 
5464a61cd66SEric Lapuyade 		pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
5474a61cd66SEric Lapuyade 			 shdlc->nr);
548412fda53SEric Lapuyade 		SHDLC_DUMP_SKB("shdlc frame written", skb);
5494a61cd66SEric Lapuyade 
5504a61cd66SEric Lapuyade 		r = shdlc->xmit_to_drv(shdlc->hdev, skb);
5514a61cd66SEric Lapuyade 		if (r < 0) {
5524a61cd66SEric Lapuyade 			shdlc->hard_fault = r;
5534a61cd66SEric Lapuyade 			break;
5544a61cd66SEric Lapuyade 		}
5554a61cd66SEric Lapuyade 
5564a61cd66SEric Lapuyade 		shdlc->ns = (shdlc->ns + 1) % 8;
5574a61cd66SEric Lapuyade 
5584a61cd66SEric Lapuyade 		time_sent = jiffies;
5594a61cd66SEric Lapuyade 		*(unsigned long *)skb->cb = time_sent;
5604a61cd66SEric Lapuyade 
5614a61cd66SEric Lapuyade 		skb_queue_tail(&shdlc->ack_pending_q, skb);
5624a61cd66SEric Lapuyade 
5634a61cd66SEric Lapuyade 		if (shdlc->t2_active == false) {
5644a61cd66SEric Lapuyade 			shdlc->t2_active = true;
5654a61cd66SEric Lapuyade 			mod_timer(&shdlc->t2_timer, time_sent +
5664a61cd66SEric Lapuyade 				  msecs_to_jiffies(SHDLC_T2_VALUE_MS));
5674a61cd66SEric Lapuyade 			pr_debug("Started T2 (retransmit)\n");
5684a61cd66SEric Lapuyade 		}
5694a61cd66SEric Lapuyade 	}
5704a61cd66SEric Lapuyade }
5714a61cd66SEric Lapuyade 
5724b519bb4SAllen Pais static void llc_shdlc_connect_timeout(struct timer_list *t)
5734a61cd66SEric Lapuyade {
5744b519bb4SAllen Pais 	struct llc_shdlc *shdlc = from_timer(shdlc, t, connect_timer);
5754a61cd66SEric Lapuyade 
5764a61cd66SEric Lapuyade 	pr_debug("\n");
5774a61cd66SEric Lapuyade 
578916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5794a61cd66SEric Lapuyade }
5804a61cd66SEric Lapuyade 
5814b519bb4SAllen Pais static void llc_shdlc_t1_timeout(struct timer_list *t)
5824a61cd66SEric Lapuyade {
5834b519bb4SAllen Pais 	struct llc_shdlc *shdlc = from_timer(shdlc, t, t1_timer);
5844a61cd66SEric Lapuyade 
5854a61cd66SEric Lapuyade 	pr_debug("SoftIRQ: need to send ack\n");
5864a61cd66SEric Lapuyade 
587916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5884a61cd66SEric Lapuyade }
5894a61cd66SEric Lapuyade 
5904b519bb4SAllen Pais static void llc_shdlc_t2_timeout(struct timer_list *t)
5914a61cd66SEric Lapuyade {
5924b519bb4SAllen Pais 	struct llc_shdlc *shdlc = from_timer(shdlc, t, t2_timer);
5934a61cd66SEric Lapuyade 
5944a61cd66SEric Lapuyade 	pr_debug("SoftIRQ: need to retransmit\n");
5954a61cd66SEric Lapuyade 
596916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5974a61cd66SEric Lapuyade }
5984a61cd66SEric Lapuyade 
5994a61cd66SEric Lapuyade static void llc_shdlc_sm_work(struct work_struct *work)
6004a61cd66SEric Lapuyade {
6014a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
6024a61cd66SEric Lapuyade 	int r;
6034a61cd66SEric Lapuyade 
6044a61cd66SEric Lapuyade 	pr_debug("\n");
6054a61cd66SEric Lapuyade 
6064a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
6074a61cd66SEric Lapuyade 
6084a61cd66SEric Lapuyade 	switch (shdlc->state) {
6094a61cd66SEric Lapuyade 	case SHDLC_DISCONNECTED:
6104a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->rcv_q);
6114a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->send_q);
6124a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->ack_pending_q);
6134a61cd66SEric Lapuyade 		break;
6144a61cd66SEric Lapuyade 	case SHDLC_CONNECTING:
6154a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6164a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
6174a61cd66SEric Lapuyade 			break;
6184a61cd66SEric Lapuyade 		}
6194a61cd66SEric Lapuyade 
6204a61cd66SEric Lapuyade 		if (shdlc->connect_tries++ < 5)
6214a61cd66SEric Lapuyade 			r = llc_shdlc_connect_initiate(shdlc);
6224a61cd66SEric Lapuyade 		else
6234a61cd66SEric Lapuyade 			r = -ETIME;
6240f450772SSzymon Janc 		if (r < 0) {
6254a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, r);
6260f450772SSzymon Janc 		} else {
6274a61cd66SEric Lapuyade 			mod_timer(&shdlc->connect_timer, jiffies +
6284a61cd66SEric Lapuyade 				  msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
6294a61cd66SEric Lapuyade 
6304c0ba9acSWaldemar Rymarkiewicz 			shdlc->state = SHDLC_NEGOTIATING;
6314a61cd66SEric Lapuyade 		}
6324a61cd66SEric Lapuyade 		break;
6334c0ba9acSWaldemar Rymarkiewicz 	case SHDLC_NEGOTIATING:
6344a61cd66SEric Lapuyade 		if (timer_pending(&shdlc->connect_timer) == 0) {
6354a61cd66SEric Lapuyade 			shdlc->state = SHDLC_CONNECTING;
636916082b0SLinus Torvalds 			schedule_work(&shdlc->sm_work);
6374a61cd66SEric Lapuyade 		}
6384a61cd66SEric Lapuyade 
6394a61cd66SEric Lapuyade 		llc_shdlc_handle_rcv_queue(shdlc);
6404a61cd66SEric Lapuyade 
6414a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6424a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
6434a61cd66SEric Lapuyade 			break;
6444a61cd66SEric Lapuyade 		}
6454a61cd66SEric Lapuyade 		break;
64612bfd1e8SWaldemar Rymarkiewicz 	case SHDLC_HALF_CONNECTED:
6474a61cd66SEric Lapuyade 	case SHDLC_CONNECTED:
6484a61cd66SEric Lapuyade 		llc_shdlc_handle_rcv_queue(shdlc);
6494a61cd66SEric Lapuyade 		llc_shdlc_handle_send_queue(shdlc);
6504a61cd66SEric Lapuyade 
6514a61cd66SEric Lapuyade 		if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
6524a61cd66SEric Lapuyade 			pr_debug
6534a61cd66SEric Lapuyade 			    ("Handle T1(send ack) elapsed (T1 now inactive)\n");
6544a61cd66SEric Lapuyade 
6554a61cd66SEric Lapuyade 			shdlc->t1_active = false;
6564a61cd66SEric Lapuyade 			r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
6574a61cd66SEric Lapuyade 						   shdlc->nr);
6584a61cd66SEric Lapuyade 			if (r < 0)
6594a61cd66SEric Lapuyade 				shdlc->hard_fault = r;
6604a61cd66SEric Lapuyade 		}
6614a61cd66SEric Lapuyade 
6624a61cd66SEric Lapuyade 		if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
6634a61cd66SEric Lapuyade 			pr_debug
6644a61cd66SEric Lapuyade 			    ("Handle T2(retransmit) elapsed (T2 inactive)\n");
6654a61cd66SEric Lapuyade 
6664a61cd66SEric Lapuyade 			shdlc->t2_active = false;
6674a61cd66SEric Lapuyade 
6684a61cd66SEric Lapuyade 			llc_shdlc_requeue_ack_pending(shdlc);
6694a61cd66SEric Lapuyade 			llc_shdlc_handle_send_queue(shdlc);
6704a61cd66SEric Lapuyade 		}
6714a61cd66SEric Lapuyade 
6720f450772SSzymon Janc 		if (shdlc->hard_fault)
6734a61cd66SEric Lapuyade 			shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
6744a61cd66SEric Lapuyade 		break;
6754a61cd66SEric Lapuyade 	default:
6764a61cd66SEric Lapuyade 		break;
6774a61cd66SEric Lapuyade 	}
6784a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
6794a61cd66SEric Lapuyade }
6804a61cd66SEric Lapuyade 
6814a61cd66SEric Lapuyade /*
6824a61cd66SEric Lapuyade  * Called from syscall context to establish shdlc link. Sleeps until
6834a61cd66SEric Lapuyade  * link is ready or failure.
6844a61cd66SEric Lapuyade  */
6854a61cd66SEric Lapuyade static int llc_shdlc_connect(struct llc_shdlc *shdlc)
6864a61cd66SEric Lapuyade {
6874a61cd66SEric Lapuyade 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
6884a61cd66SEric Lapuyade 
6894a61cd66SEric Lapuyade 	pr_debug("\n");
6904a61cd66SEric Lapuyade 
6914a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
6924a61cd66SEric Lapuyade 
6934a61cd66SEric Lapuyade 	shdlc->state = SHDLC_CONNECTING;
6944a61cd66SEric Lapuyade 	shdlc->connect_wq = &connect_wq;
6954a61cd66SEric Lapuyade 	shdlc->connect_tries = 0;
6964a61cd66SEric Lapuyade 	shdlc->connect_result = 1;
6974a61cd66SEric Lapuyade 
6984a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
6994a61cd66SEric Lapuyade 
700916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7014a61cd66SEric Lapuyade 
7024a61cd66SEric Lapuyade 	wait_event(connect_wq, shdlc->connect_result != 1);
7034a61cd66SEric Lapuyade 
7044a61cd66SEric Lapuyade 	return shdlc->connect_result;
7054a61cd66SEric Lapuyade }
7064a61cd66SEric Lapuyade 
7074a61cd66SEric Lapuyade static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
7084a61cd66SEric Lapuyade {
7094a61cd66SEric Lapuyade 	pr_debug("\n");
7104a61cd66SEric Lapuyade 
7114a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
7124a61cd66SEric Lapuyade 
7134a61cd66SEric Lapuyade 	shdlc->state = SHDLC_DISCONNECTED;
7144a61cd66SEric Lapuyade 
7154a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
7164a61cd66SEric Lapuyade 
717916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7184a61cd66SEric Lapuyade }
7194a61cd66SEric Lapuyade 
7204a61cd66SEric Lapuyade /*
7214a61cd66SEric Lapuyade  * Receive an incoming shdlc frame. Frame has already been crc-validated.
7224a61cd66SEric Lapuyade  * skb contains only LLC header and payload.
7234a61cd66SEric Lapuyade  * If skb == NULL, it is a notification that the link below is dead.
7244a61cd66SEric Lapuyade  */
7254a61cd66SEric Lapuyade static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
7264a61cd66SEric Lapuyade {
7274a61cd66SEric Lapuyade 	if (skb == NULL) {
7284a61cd66SEric Lapuyade 		pr_err("NULL Frame -> link is dead\n");
7294a61cd66SEric Lapuyade 		shdlc->hard_fault = -EREMOTEIO;
7304a61cd66SEric Lapuyade 	} else {
7314a61cd66SEric Lapuyade 		SHDLC_DUMP_SKB("incoming frame", skb);
7324a61cd66SEric Lapuyade 		skb_queue_tail(&shdlc->rcv_q, skb);
7334a61cd66SEric Lapuyade 	}
7344a61cd66SEric Lapuyade 
735916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7364a61cd66SEric Lapuyade }
7374a61cd66SEric Lapuyade 
7384a61cd66SEric Lapuyade static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
7394a61cd66SEric Lapuyade 			    rcv_to_hci_t rcv_to_hci, int tx_headroom,
7404a61cd66SEric Lapuyade 			    int tx_tailroom, int *rx_headroom, int *rx_tailroom,
7414a61cd66SEric Lapuyade 			    llc_failure_t llc_failure)
7424a61cd66SEric Lapuyade {
7434a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc;
7444a61cd66SEric Lapuyade 
7454a61cd66SEric Lapuyade 	*rx_headroom = SHDLC_LLC_HEAD_ROOM;
7464a61cd66SEric Lapuyade 	*rx_tailroom = 0;
7474a61cd66SEric Lapuyade 
7484a61cd66SEric Lapuyade 	shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
7494a61cd66SEric Lapuyade 	if (shdlc == NULL)
7504a61cd66SEric Lapuyade 		return NULL;
7514a61cd66SEric Lapuyade 
7524a61cd66SEric Lapuyade 	mutex_init(&shdlc->state_mutex);
7534a61cd66SEric Lapuyade 	shdlc->state = SHDLC_DISCONNECTED;
7544a61cd66SEric Lapuyade 
7554b519bb4SAllen Pais 	timer_setup(&shdlc->connect_timer, llc_shdlc_connect_timeout, 0);
7564b519bb4SAllen Pais 	timer_setup(&shdlc->t1_timer, llc_shdlc_t1_timeout, 0);
7574b519bb4SAllen Pais 	timer_setup(&shdlc->t2_timer, llc_shdlc_t2_timeout, 0);
7584a61cd66SEric Lapuyade 
7594a61cd66SEric Lapuyade 	shdlc->w = SHDLC_MAX_WINDOW;
7604a61cd66SEric Lapuyade 	shdlc->srej_support = SHDLC_SREJ_SUPPORT;
7614a61cd66SEric Lapuyade 
7624a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->rcv_q);
7634a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->send_q);
7644a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->ack_pending_q);
7654a61cd66SEric Lapuyade 
7664a61cd66SEric Lapuyade 	INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
7674a61cd66SEric Lapuyade 
7684a61cd66SEric Lapuyade 	shdlc->hdev = hdev;
7694a61cd66SEric Lapuyade 	shdlc->xmit_to_drv = xmit_to_drv;
7704a61cd66SEric Lapuyade 	shdlc->rcv_to_hci = rcv_to_hci;
7714a61cd66SEric Lapuyade 	shdlc->tx_headroom = tx_headroom;
7724a61cd66SEric Lapuyade 	shdlc->tx_tailroom = tx_tailroom;
7734a61cd66SEric Lapuyade 	shdlc->llc_failure = llc_failure;
7744a61cd66SEric Lapuyade 
7754a61cd66SEric Lapuyade 	return shdlc;
7764a61cd66SEric Lapuyade }
7774a61cd66SEric Lapuyade 
7784a61cd66SEric Lapuyade static void llc_shdlc_deinit(struct nfc_llc *llc)
7794a61cd66SEric Lapuyade {
7804a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7814a61cd66SEric Lapuyade 
7824a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->rcv_q);
7834a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->send_q);
7844a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->ack_pending_q);
7854a61cd66SEric Lapuyade 
7864a61cd66SEric Lapuyade 	kfree(shdlc);
7874a61cd66SEric Lapuyade }
7884a61cd66SEric Lapuyade 
7894a61cd66SEric Lapuyade static int llc_shdlc_start(struct nfc_llc *llc)
7904a61cd66SEric Lapuyade {
7914a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7924a61cd66SEric Lapuyade 
7934a61cd66SEric Lapuyade 	return llc_shdlc_connect(shdlc);
7944a61cd66SEric Lapuyade }
7954a61cd66SEric Lapuyade 
7964a61cd66SEric Lapuyade static int llc_shdlc_stop(struct nfc_llc *llc)
7974a61cd66SEric Lapuyade {
7984a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
7994a61cd66SEric Lapuyade 
8004a61cd66SEric Lapuyade 	llc_shdlc_disconnect(shdlc);
8014a61cd66SEric Lapuyade 
8024a61cd66SEric Lapuyade 	return 0;
8034a61cd66SEric Lapuyade }
8044a61cd66SEric Lapuyade 
8054a61cd66SEric Lapuyade static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
8064a61cd66SEric Lapuyade {
8074a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8084a61cd66SEric Lapuyade 
8094a61cd66SEric Lapuyade 	llc_shdlc_recv_frame(shdlc, skb);
8104a61cd66SEric Lapuyade }
8114a61cd66SEric Lapuyade 
8124a61cd66SEric Lapuyade static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
8134a61cd66SEric Lapuyade {
8144a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8154a61cd66SEric Lapuyade 
8164a61cd66SEric Lapuyade 	skb_queue_tail(&shdlc->send_q, skb);
8174a61cd66SEric Lapuyade 
818916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
8194a61cd66SEric Lapuyade 
8204a61cd66SEric Lapuyade 	return 0;
8214a61cd66SEric Lapuyade }
8224a61cd66SEric Lapuyade 
823*49545357SKrzysztof Kozlowski static const struct nfc_llc_ops llc_shdlc_ops = {
8244a61cd66SEric Lapuyade 	.init = llc_shdlc_init,
8254a61cd66SEric Lapuyade 	.deinit = llc_shdlc_deinit,
8264a61cd66SEric Lapuyade 	.start = llc_shdlc_start,
8274a61cd66SEric Lapuyade 	.stop = llc_shdlc_stop,
8284a61cd66SEric Lapuyade 	.rcv_from_drv = llc_shdlc_rcv_from_drv,
8294a61cd66SEric Lapuyade 	.xmit_from_hci = llc_shdlc_xmit_from_hci,
8304a61cd66SEric Lapuyade };
8314a61cd66SEric Lapuyade 
832a7d0281bSEric Lapuyade int nfc_llc_shdlc_register(void)
8334a61cd66SEric Lapuyade {
8344a61cd66SEric Lapuyade 	return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);
8354a61cd66SEric Lapuyade }
836