2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
4 * License terms: GNU General Public License (GPL) version 2
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
9 #include <linux/stddef.h>
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <asm/unaligned.h>
13 #include <net/caif/caif_layer.h>
14 #include <net/caif/cfsrvl.h>
15 #include <net/caif/cfpkt.h>
17 #define container_obj(layr) container_of(layr, struct cfrfml, serv.layer)
18 #define RFM_SEGMENTATION_BIT 0x01
19 #define RFM_HEAD_SIZE 7
21 static int cfrfml_receive(struct cflayer
*layr
, struct cfpkt
*pkt
);
22 static int cfrfml_transmit(struct cflayer
*layr
, struct cfpkt
*pkt
);
26 struct cfpkt
*incomplete_frm
;
30 /* Protects serialized processing of packets */
34 static void cfrfml_release(struct cflayer
*layer
)
36 struct cfsrvl
*srvl
= container_of(layer
, struct cfsrvl
, layer
);
37 struct cfrfml
*rfml
= container_obj(&srvl
->layer
);
39 if (rfml
->incomplete_frm
)
40 cfpkt_destroy(rfml
->incomplete_frm
);
45 struct cflayer
*cfrfml_create(u8 channel_id
, struct dev_info
*dev_info
,
49 struct cfrfml
*this = kzalloc(sizeof(struct cfrfml
), GFP_ATOMIC
);
54 cfsrvl_init(&this->serv
, channel_id
, dev_info
, false);
55 this->serv
.release
= cfrfml_release
;
56 this->serv
.layer
.receive
= cfrfml_receive
;
57 this->serv
.layer
.transmit
= cfrfml_transmit
;
59 /* Round down to closest multiple of 16 */
60 tmp
= (mtu_size
- RFM_HEAD_SIZE
- 6) / 16;
63 this->fragment_size
= tmp
;
64 spin_lock_init(&this->sync
);
65 snprintf(this->serv
.layer
.name
, CAIF_LAYER_NAME_SZ
,
68 return &this->serv
.layer
;
71 static struct cfpkt
*rfm_append(struct cfrfml
*rfml
, char *seghead
,
72 struct cfpkt
*pkt
, int *err
)
76 /* n-th but not last segment */
78 if (cfpkt_extr_head(pkt
, seghead
, 6) < 0)
81 /* Verify correct header */
82 if (memcmp(seghead
, rfml
->seghead
, 6) != 0)
85 tmppkt
= cfpkt_append(rfml
->incomplete_frm
, pkt
,
86 rfml
->pdu_size
+ RFM_HEAD_SIZE
);
88 /* If cfpkt_append failes input pkts are not freed */
97 static int cfrfml_receive(struct cflayer
*layr
, struct cfpkt
*pkt
)
104 struct cfpkt
*tmppkt
= NULL
;
106 caif_assert(layr
->up
!= NULL
);
107 caif_assert(layr
->receive
!= NULL
);
108 rfml
= container_obj(layr
);
109 spin_lock(&rfml
->sync
);
112 if (cfpkt_extr_head(pkt
, &tmp
, 1) < 0)
114 segmented
= tmp
& RFM_SEGMENTATION_BIT
;
117 if (rfml
->incomplete_frm
== NULL
) {
118 /* Initial Segment */
119 if (cfpkt_peek_head(pkt
, rfml
->seghead
, 6) < 0)
122 rfml
->pdu_size
= get_unaligned_le16(rfml
->seghead
+4);
124 if (cfpkt_erroneous(pkt
))
126 rfml
->incomplete_frm
= pkt
;
130 tmppkt
= rfm_append(rfml
, seghead
, pkt
, &err
);
134 if (cfpkt_erroneous(tmppkt
))
137 rfml
->incomplete_frm
= tmppkt
;
140 if (cfpkt_erroneous(tmppkt
))
147 if (rfml
->incomplete_frm
) {
150 tmppkt
= rfm_append(rfml
, seghead
, pkt
, &err
);
154 if (cfpkt_erroneous(tmppkt
))
157 rfml
->incomplete_frm
= NULL
;
161 /* Verify that length is correct */
163 if (rfml
->pdu_size
!= cfpkt_getlen(pkt
) - RFM_HEAD_SIZE
+ 1)
167 err
= rfml
->serv
.layer
.up
->receive(rfml
->serv
.layer
.up
, pkt
);
173 cfpkt_destroy(tmppkt
);
176 if (rfml
->incomplete_frm
)
177 cfpkt_destroy(rfml
->incomplete_frm
);
178 rfml
->incomplete_frm
= NULL
;
180 pr_info("Connection error %d triggered on RFM link\n", err
);
182 /* Trigger connection error upon failure.*/
183 layr
->up
->ctrlcmd(layr
->up
, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
,
184 rfml
->serv
.dev_info
.id
);
186 spin_unlock(&rfml
->sync
);
191 static int cfrfml_transmit_segment(struct cfrfml
*rfml
, struct cfpkt
*pkt
)
193 caif_assert(cfpkt_getlen(pkt
) < rfml
->fragment_size
+ RFM_HEAD_SIZE
);
195 /* Add info for MUX-layer to route the packet out. */
196 cfpkt_info(pkt
)->channel_id
= rfml
->serv
.layer
.id
;
199 * To optimize alignment, we add up the size of CAIF header before
202 cfpkt_info(pkt
)->hdr_len
= RFM_HEAD_SIZE
;
203 cfpkt_info(pkt
)->dev_info
= &rfml
->serv
.dev_info
;
205 return rfml
->serv
.layer
.dn
->transmit(rfml
->serv
.layer
.dn
, pkt
);
208 static int cfrfml_transmit(struct cflayer
*layr
, struct cfpkt
*pkt
)
213 struct cfpkt
*rearpkt
= NULL
;
214 struct cfpkt
*frontpkt
= pkt
;
215 struct cfrfml
*rfml
= container_obj(layr
);
217 caif_assert(layr
->dn
!= NULL
);
218 caif_assert(layr
->dn
->transmit
!= NULL
);
220 if (!cfsrvl_ready(&rfml
->serv
, &err
))
224 if (cfpkt_getlen(pkt
) <= RFM_HEAD_SIZE
-1)
228 if (cfpkt_getlen(pkt
) > rfml
->fragment_size
+ RFM_HEAD_SIZE
)
229 err
= cfpkt_peek_head(pkt
, head
, 6);
234 while (cfpkt_getlen(frontpkt
) > rfml
->fragment_size
+ RFM_HEAD_SIZE
) {
239 if (cfpkt_add_head(frontpkt
, &seg
, 1) < 0)
242 * On OOM error cfpkt_split returns NULL.
244 * NOTE: Segmented pdu is not correctly aligned.
245 * This has negative performance impact.
248 rearpkt
= cfpkt_split(frontpkt
, rfml
->fragment_size
);
252 err
= cfrfml_transmit_segment(rfml
, frontpkt
);
260 if (frontpkt
== NULL
)
263 if (cfpkt_add_head(frontpkt
, head
, 6) < 0)
271 if (cfpkt_add_head(frontpkt
, &seg
, 1) < 0)
274 err
= cfrfml_transmit_segment(rfml
, frontpkt
);
280 pr_info("Connection error %d triggered on RFM link\n", err
);
281 /* Trigger connection error upon failure.*/
283 layr
->up
->ctrlcmd(layr
->up
, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
,
284 rfml
->serv
.dev_info
.id
);
287 cfpkt_destroy(rearpkt
);
289 if (frontpkt
&& frontpkt
!= pkt
) {
291 cfpkt_destroy(frontpkt
);
293 * Socket layer will free the original packet,
294 * but this packet may already be sent and
295 * freed. So we have to return 0 in this case
296 * to avoid socket layer to re-free this packet.
297 * The return of shutdown indication will
298 * cause connection to be invalidated anyhow.