Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/net/caif/cffrml.c
49620 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* CAIF Framing Layer.
4
*
5
* Copyright (C) ST-Ericsson AB 2010
6
* Author: Sjur Brendeland
7
*/
8
9
#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
10
11
#include <linux/stddef.h>
12
#include <linux/spinlock.h>
13
#include <linux/slab.h>
14
#include <linux/crc-ccitt.h>
15
#include <linux/netdevice.h>
16
#include <net/caif/caif_layer.h>
17
#include <net/caif/cfpkt.h>
18
#include <net/caif/cffrml.h>
19
20
#define container_obj(layr) container_of(layr, struct cffrml, layer)
21
22
struct cffrml {
23
struct cflayer layer;
24
bool dofcs; /* !< FCS active */
25
int __percpu *pcpu_refcnt;
26
};
27
28
static int cffrml_receive(struct cflayer *layr, struct cfpkt *pkt);
29
static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt);
30
static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
31
int phyid);
32
33
static u32 cffrml_rcv_error;
34
static u32 cffrml_rcv_checsum_error;
35
struct cflayer *cffrml_create(u16 phyid, bool use_fcs)
36
{
37
struct cffrml *this = kzalloc(sizeof(struct cffrml), GFP_ATOMIC);
38
if (!this)
39
return NULL;
40
this->pcpu_refcnt = alloc_percpu(int);
41
if (this->pcpu_refcnt == NULL) {
42
kfree(this);
43
return NULL;
44
}
45
46
caif_assert(offsetof(struct cffrml, layer) == 0);
47
48
this->layer.receive = cffrml_receive;
49
this->layer.transmit = cffrml_transmit;
50
this->layer.ctrlcmd = cffrml_ctrlcmd;
51
snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "frm%d", phyid);
52
this->dofcs = use_fcs;
53
this->layer.id = phyid;
54
return (struct cflayer *) this;
55
}
56
57
void cffrml_free(struct cflayer *layer)
58
{
59
struct cffrml *this = container_obj(layer);
60
free_percpu(this->pcpu_refcnt);
61
kfree(layer);
62
}
63
64
void cffrml_set_uplayer(struct cflayer *this, struct cflayer *up)
65
{
66
this->up = up;
67
}
68
69
void cffrml_set_dnlayer(struct cflayer *this, struct cflayer *dn)
70
{
71
this->dn = dn;
72
}
73
74
static u16 cffrml_checksum(u16 chks, void *buf, u16 len)
75
{
76
/* FIXME: FCS should be moved to glue in order to use OS-Specific
77
* solutions
78
*/
79
return crc_ccitt(chks, buf, len);
80
}
81
82
static int cffrml_receive(struct cflayer *layr, struct cfpkt *pkt)
83
{
84
u16 tmp;
85
u16 len;
86
u16 hdrchks;
87
int pktchks;
88
struct cffrml *this;
89
this = container_obj(layr);
90
91
cfpkt_extr_head(pkt, &tmp, 2);
92
len = le16_to_cpu(tmp);
93
94
/* Subtract for FCS on length if FCS is not used. */
95
if (!this->dofcs) {
96
if (len < 2) {
97
++cffrml_rcv_error;
98
pr_err("Invalid frame length (%d)\n", len);
99
cfpkt_destroy(pkt);
100
return -EPROTO;
101
}
102
len -= 2;
103
}
104
105
if (cfpkt_setlen(pkt, len) < 0) {
106
++cffrml_rcv_error;
107
pr_err("Framing length error (%d)\n", len);
108
cfpkt_destroy(pkt);
109
return -EPROTO;
110
}
111
/*
112
* Don't do extract if FCS is false, rather do setlen - then we don't
113
* get a cache-miss.
114
*/
115
if (this->dofcs) {
116
cfpkt_extr_trail(pkt, &tmp, 2);
117
hdrchks = le16_to_cpu(tmp);
118
pktchks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff);
119
if (pktchks != hdrchks) {
120
cfpkt_add_trail(pkt, &tmp, 2);
121
++cffrml_rcv_error;
122
++cffrml_rcv_checsum_error;
123
pr_info("Frame checksum error (0x%x != 0x%x)\n",
124
hdrchks, pktchks);
125
return -EILSEQ;
126
}
127
}
128
if (cfpkt_erroneous(pkt)) {
129
++cffrml_rcv_error;
130
pr_err("Packet is erroneous!\n");
131
cfpkt_destroy(pkt);
132
return -EPROTO;
133
}
134
135
if (layr->up == NULL) {
136
pr_err("Layr up is missing!\n");
137
cfpkt_destroy(pkt);
138
return -EINVAL;
139
}
140
141
return layr->up->receive(layr->up, pkt);
142
}
143
144
static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt)
145
{
146
u16 chks;
147
u16 len;
148
__le16 data;
149
150
struct cffrml *this = container_obj(layr);
151
if (this->dofcs) {
152
chks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff);
153
data = cpu_to_le16(chks);
154
cfpkt_add_trail(pkt, &data, 2);
155
} else {
156
cfpkt_pad_trail(pkt, 2);
157
}
158
len = cfpkt_getlen(pkt);
159
data = cpu_to_le16(len);
160
cfpkt_add_head(pkt, &data, 2);
161
cfpkt_info(pkt)->hdr_len += 2;
162
if (cfpkt_erroneous(pkt)) {
163
pr_err("Packet is erroneous!\n");
164
cfpkt_destroy(pkt);
165
return -EPROTO;
166
}
167
168
if (layr->dn == NULL) {
169
cfpkt_destroy(pkt);
170
return -ENODEV;
171
172
}
173
return layr->dn->transmit(layr->dn, pkt);
174
}
175
176
static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
177
int phyid)
178
{
179
if (layr->up && layr->up->ctrlcmd)
180
layr->up->ctrlcmd(layr->up, ctrl, layr->id);
181
}
182
183
void cffrml_put(struct cflayer *layr)
184
{
185
struct cffrml *this = container_obj(layr);
186
if (layr != NULL && this->pcpu_refcnt != NULL)
187
this_cpu_dec(*this->pcpu_refcnt);
188
}
189
190
void cffrml_hold(struct cflayer *layr)
191
{
192
struct cffrml *this = container_obj(layr);
193
if (layr != NULL && this->pcpu_refcnt != NULL)
194
this_cpu_inc(*this->pcpu_refcnt);
195
}
196
197
int cffrml_refcnt_read(struct cflayer *layr)
198
{
199
int i, refcnt = 0;
200
struct cffrml *this = container_obj(layr);
201
for_each_possible_cpu(i)
202
refcnt += *per_cpu_ptr(this->pcpu_refcnt, i);
203
return refcnt;
204
}
205
206