Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CAIF Framing Layer. |
| 3 | * |
| 4 | * Copyright (C) ST-Ericsson AB 2010 |
| 5 | * Author: Sjur Brendeland/sjur.brandeland@stericsson.com |
| 6 | * License terms: GNU General Public License (GPL) version 2 |
| 7 | */ |
| 8 | |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 10 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 11 | #include <linux/stddef.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/crc-ccitt.h> |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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 */ |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 25 | int __percpu *pcpu_refcnt; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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 | { |
Joe Perches | 7ac2ed0 | 2011-08-25 13:22:24 +0000 | [diff] [blame] | 37 | struct cffrml *this = kzalloc(sizeof(struct cffrml), GFP_ATOMIC); |
| 38 | if (!this) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 39 | return NULL; |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 40 | this->pcpu_refcnt = alloc_percpu(int); |
| 41 | if (this->pcpu_refcnt == NULL) { |
| 42 | kfree(this); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 46 | caif_assert(offsetof(struct cffrml, layer) == 0); |
| 47 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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 | |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 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 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 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 | u16 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 | len -= 2; |
| 97 | |
| 98 | if (cfpkt_setlen(pkt, len) < 0) { |
| 99 | ++cffrml_rcv_error; |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 100 | pr_err("Framing length error (%d)\n", len); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 101 | cfpkt_destroy(pkt); |
| 102 | return -EPROTO; |
| 103 | } |
| 104 | /* |
| 105 | * Don't do extract if FCS is false, rather do setlen - then we don't |
| 106 | * get a cache-miss. |
| 107 | */ |
| 108 | if (this->dofcs) { |
| 109 | cfpkt_extr_trail(pkt, &tmp, 2); |
| 110 | hdrchks = le16_to_cpu(tmp); |
| 111 | pktchks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff); |
| 112 | if (pktchks != hdrchks) { |
| 113 | cfpkt_add_trail(pkt, &tmp, 2); |
| 114 | ++cffrml_rcv_error; |
| 115 | ++cffrml_rcv_checsum_error; |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 116 | pr_info("Frame checksum error (0x%x != 0x%x)\n", |
| 117 | hdrchks, pktchks); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 118 | return -EILSEQ; |
| 119 | } |
| 120 | } |
| 121 | if (cfpkt_erroneous(pkt)) { |
| 122 | ++cffrml_rcv_error; |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 123 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 124 | cfpkt_destroy(pkt); |
| 125 | return -EPROTO; |
| 126 | } |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 127 | |
| 128 | if (layr->up == NULL) { |
| 129 | pr_err("Layr up is missing!\n"); |
| 130 | cfpkt_destroy(pkt); |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 134 | return layr->up->receive(layr->up, pkt); |
| 135 | } |
| 136 | |
| 137 | static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 138 | { |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 139 | u16 chks; |
| 140 | u16 len; |
Dan Carpenter | f23aa62 | 2011-11-21 16:46:24 -0500 | [diff] [blame] | 141 | __le16 data; |
| 142 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 143 | struct cffrml *this = container_obj(layr); |
| 144 | if (this->dofcs) { |
| 145 | chks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff); |
Dan Carpenter | f23aa62 | 2011-11-21 16:46:24 -0500 | [diff] [blame] | 146 | data = cpu_to_le16(chks); |
| 147 | cfpkt_add_trail(pkt, &data, 2); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 148 | } else { |
| 149 | cfpkt_pad_trail(pkt, 2); |
| 150 | } |
| 151 | len = cfpkt_getlen(pkt); |
Dan Carpenter | f23aa62 | 2011-11-21 16:46:24 -0500 | [diff] [blame] | 152 | data = cpu_to_le16(len); |
| 153 | cfpkt_add_head(pkt, &data, 2); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 154 | cfpkt_info(pkt)->hdr_len += 2; |
| 155 | if (cfpkt_erroneous(pkt)) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 156 | pr_err("Packet is erroneous!\n"); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 157 | cfpkt_destroy(pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 158 | return -EPROTO; |
| 159 | } |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 160 | |
| 161 | if (layr->dn == NULL) { |
| 162 | cfpkt_destroy(pkt); |
| 163 | return -ENODEV; |
| 164 | |
| 165 | } |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame] | 166 | return layr->dn->transmit(layr->dn, pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
| 170 | int phyid) |
| 171 | { |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 172 | if (layr->up && layr->up->ctrlcmd) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 173 | layr->up->ctrlcmd(layr->up, ctrl, layr->id); |
| 174 | } |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 175 | |
| 176 | void cffrml_put(struct cflayer *layr) |
| 177 | { |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 178 | struct cffrml *this = container_obj(layr); |
| 179 | if (layr != NULL && this->pcpu_refcnt != NULL) |
Christoph Lameter | 933393f | 2011-12-22 11:58:51 -0600 | [diff] [blame] | 180 | this_cpu_dec(*this->pcpu_refcnt); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void cffrml_hold(struct cflayer *layr) |
| 184 | { |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 185 | struct cffrml *this = container_obj(layr); |
| 186 | if (layr != NULL && this->pcpu_refcnt != NULL) |
Christoph Lameter | 933393f | 2011-12-22 11:58:51 -0600 | [diff] [blame] | 187 | this_cpu_inc(*this->pcpu_refcnt); |
sjur.brandeland@stericsson.com | cb3cb42 | 2011-05-13 02:44:02 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int cffrml_refcnt_read(struct cflayer *layr) |
| 191 | { |
| 192 | int i, refcnt = 0; |
| 193 | struct cffrml *this = container_obj(layr); |
| 194 | for_each_possible_cpu(i) |
| 195 | refcnt += *per_cpu_ptr(this->pcpu_refcnt, i); |
| 196 | return refcnt; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 197 | } |