Merge tag 'ntb-5.3' of git://github.com/jonmason/ntb
[linux-2.6-block.git] / net / lapb / lapb_out.c
CommitLineData
ee5d8f4d 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * LAPB release 002
4 *
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
6 *
1da177e4
LT
7 * History
8 * LAPB 001 Jonathan Naylor Started Coding
9 * LAPB 002 Jonathan Naylor New timer architecture.
10 */
11
a508da6c
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/types.h>
16#include <linux/socket.h>
17#include <linux/in.h>
18#include <linux/kernel.h>
1da177e4
LT
19#include <linux/timer.h>
20#include <linux/string.h>
21#include <linux/sockios.h>
22#include <linux/net.h>
23#include <linux/inet.h>
24#include <linux/skbuff.h>
5a0e3ad6 25#include <linux/slab.h>
1da177e4 26#include <net/sock.h>
7c0f6ba6 27#include <linux/uaccess.h>
1da177e4
LT
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/interrupt.h>
31#include <net/lapb.h>
32
56d6c3d7 33/*
1da177e4
LT
34 * This procedure is passed a buffer descriptor for an iframe. It builds
35 * the rest of the control part of the frame and then writes it out.
36 */
37static void lapb_send_iframe(struct lapb_cb *lapb, struct sk_buff *skb, int poll_bit)
38{
39 unsigned char *frame;
40
41 if (!skb)
42 return;
43
44 if (lapb->mode & LAPB_EXTENDED) {
45 frame = skb_push(skb, 2);
46
47 frame[0] = LAPB_I;
48 frame[0] |= lapb->vs << 1;
49 frame[1] = poll_bit ? LAPB_EPF : 0;
50 frame[1] |= lapb->vr << 1;
51 } else {
52 frame = skb_push(skb, 1);
53
54 *frame = LAPB_I;
55 *frame |= poll_bit ? LAPB_SPF : 0;
56 *frame |= lapb->vr << 5;
57 *frame |= lapb->vs << 1;
58 }
59
a508da6c
JP
60 lapb_dbg(1, "(%p) S%d TX I(%d) S%d R%d\n",
61 lapb->dev, lapb->state, poll_bit, lapb->vs, lapb->vr);
1da177e4 62
56d6c3d7 63 lapb_transmit_buffer(lapb, skb, LAPB_COMMAND);
1da177e4
LT
64}
65
66void lapb_kick(struct lapb_cb *lapb)
67{
68 struct sk_buff *skb, *skbn;
69 unsigned short modulus, start, end;
70
71 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
72 start = !skb_peek(&lapb->ack_queue) ? lapb->va : lapb->vs;
73 end = (lapb->va + lapb->window) % modulus;
74
75 if (!(lapb->condition & LAPB_PEER_RX_BUSY_CONDITION) &&
76 start != end && skb_peek(&lapb->write_queue)) {
77 lapb->vs = start;
78
79 /*
80 * Dequeue the frame and copy it.
81 */
82 skb = skb_dequeue(&lapb->write_queue);
83
84 do {
85 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
86 skb_queue_head(&lapb->write_queue, skb);
87 break;
88 }
89
90 if (skb->sk)
91 skb_set_owner_w(skbn, skb->sk);
92
93 /*
94 * Transmit the frame copy.
95 */
96 lapb_send_iframe(lapb, skbn, LAPB_POLLOFF);
97
98 lapb->vs = (lapb->vs + 1) % modulus;
99
100 /*
101 * Requeue the original data frame.
102 */
103 skb_queue_tail(&lapb->ack_queue, skb);
104
105 } while (lapb->vs != end && (skb = skb_dequeue(&lapb->write_queue)) != NULL);
106
107 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
108
109 if (!lapb_t1timer_running(lapb))
110 lapb_start_t1timer(lapb);
111 }
112}
113
114void lapb_transmit_buffer(struct lapb_cb *lapb, struct sk_buff *skb, int type)
115{
116 unsigned char *ptr;
117
118 ptr = skb_push(skb, 1);
119
120 if (lapb->mode & LAPB_MLP) {
121 if (lapb->mode & LAPB_DCE) {
122 if (type == LAPB_COMMAND)
123 *ptr = LAPB_ADDR_C;
124 if (type == LAPB_RESPONSE)
125 *ptr = LAPB_ADDR_D;
126 } else {
127 if (type == LAPB_COMMAND)
128 *ptr = LAPB_ADDR_D;
129 if (type == LAPB_RESPONSE)
130 *ptr = LAPB_ADDR_C;
131 }
132 } else {
133 if (lapb->mode & LAPB_DCE) {
134 if (type == LAPB_COMMAND)
135 *ptr = LAPB_ADDR_A;
136 if (type == LAPB_RESPONSE)
137 *ptr = LAPB_ADDR_B;
138 } else {
139 if (type == LAPB_COMMAND)
140 *ptr = LAPB_ADDR_B;
141 if (type == LAPB_RESPONSE)
142 *ptr = LAPB_ADDR_A;
143 }
144 }
145
0d08df6c 146 lapb_dbg(2, "(%p) S%d TX %3ph\n", lapb->dev, lapb->state, skb->data);
1da177e4
LT
147
148 if (!lapb_data_transmit(lapb, skb))
149 kfree_skb(skb);
150}
151
152void lapb_establish_data_link(struct lapb_cb *lapb)
153{
154 lapb->condition = 0x00;
155 lapb->n2count = 0;
156
157 if (lapb->mode & LAPB_EXTENDED) {
a508da6c 158 lapb_dbg(1, "(%p) S%d TX SABME(1)\n", lapb->dev, lapb->state);
1da177e4
LT
159 lapb_send_control(lapb, LAPB_SABME, LAPB_POLLON, LAPB_COMMAND);
160 } else {
a508da6c 161 lapb_dbg(1, "(%p) S%d TX SABM(1)\n", lapb->dev, lapb->state);
1da177e4
LT
162 lapb_send_control(lapb, LAPB_SABM, LAPB_POLLON, LAPB_COMMAND);
163 }
164
165 lapb_start_t1timer(lapb);
166 lapb_stop_t2timer(lapb);
167}
168
169void lapb_enquiry_response(struct lapb_cb *lapb)
170{
a508da6c
JP
171 lapb_dbg(1, "(%p) S%d TX RR(1) R%d\n",
172 lapb->dev, lapb->state, lapb->vr);
1da177e4
LT
173
174 lapb_send_control(lapb, LAPB_RR, LAPB_POLLON, LAPB_RESPONSE);
175
176 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
177}
178
179void lapb_timeout_response(struct lapb_cb *lapb)
180{
a508da6c
JP
181 lapb_dbg(1, "(%p) S%d TX RR(0) R%d\n",
182 lapb->dev, lapb->state, lapb->vr);
1da177e4
LT
183 lapb_send_control(lapb, LAPB_RR, LAPB_POLLOFF, LAPB_RESPONSE);
184
185 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
186}
187
188void lapb_check_iframes_acked(struct lapb_cb *lapb, unsigned short nr)
189{
190 if (lapb->vs == nr) {
191 lapb_frames_acked(lapb, nr);
192 lapb_stop_t1timer(lapb);
193 lapb->n2count = 0;
194 } else if (lapb->va != nr) {
195 lapb_frames_acked(lapb, nr);
196 lapb_start_t1timer(lapb);
197 }
198}
199
200void lapb_check_need_response(struct lapb_cb *lapb, int type, int pf)
201{
202 if (type == LAPB_COMMAND && pf)
203 lapb_enquiry_response(lapb);
204}