Thomas Gleixner | af873fc | 2019-05-28 09:57:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) ST-Ericsson AB 2010 |
sjur.brandeland@stericsson.com | 26ee65e | 2013-04-22 23:57:01 +0000 | [diff] [blame] | 4 | * Author: Sjur Brendeland |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 5 | */ |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 6 | |
| 7 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 8 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 9 | #include <linux/stddef.h> |
| 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/slab.h> |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 12 | #include <linux/rculist.h> |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 13 | #include <net/caif/cfpkt.h> |
| 14 | #include <net/caif/cfmuxl.h> |
| 15 | #include <net/caif/cfsrvl.h> |
| 16 | #include <net/caif/cffrml.h> |
| 17 | |
| 18 | #define container_obj(layr) container_of(layr, struct cfmuxl, layer) |
| 19 | |
| 20 | #define CAIF_CTRL_CHANNEL 0 |
| 21 | #define UP_CACHE_SIZE 8 |
| 22 | #define DN_CACHE_SIZE 8 |
| 23 | |
| 24 | struct cfmuxl { |
| 25 | struct cflayer layer; |
| 26 | struct list_head srvl_list; |
| 27 | struct list_head frml_list; |
| 28 | struct cflayer *up_cache[UP_CACHE_SIZE]; |
| 29 | struct cflayer *dn_cache[DN_CACHE_SIZE]; |
| 30 | /* |
| 31 | * Set when inserting or removing downwards layers. |
| 32 | */ |
| 33 | spinlock_t transmit_lock; |
| 34 | |
| 35 | /* |
| 36 | * Set when inserting or removing upwards layers. |
| 37 | */ |
| 38 | spinlock_t receive_lock; |
| 39 | |
| 40 | }; |
| 41 | |
| 42 | static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt); |
| 43 | static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt); |
| 44 | static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
Silviu-Mihai Popescu | 3bffc47 | 2013-03-06 19:39:57 +0000 | [diff] [blame] | 45 | int phyid); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 46 | static struct cflayer *get_up(struct cfmuxl *muxl, u16 id); |
| 47 | |
| 48 | struct cflayer *cfmuxl_create(void) |
| 49 | { |
Fabian Frederick | 6ff1e1e3 | 2014-10-13 22:21:46 +0200 | [diff] [blame] | 50 | struct cfmuxl *this = kzalloc(sizeof(struct cfmuxl), GFP_ATOMIC); |
| 51 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 52 | if (!this) |
| 53 | return NULL; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 54 | this->layer.receive = cfmuxl_receive; |
| 55 | this->layer.transmit = cfmuxl_transmit; |
| 56 | this->layer.ctrlcmd = cfmuxl_ctrlcmd; |
| 57 | INIT_LIST_HEAD(&this->srvl_list); |
| 58 | INIT_LIST_HEAD(&this->frml_list); |
| 59 | spin_lock_init(&this->transmit_lock); |
| 60 | spin_lock_init(&this->receive_lock); |
| 61 | snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux"); |
| 62 | return &this->layer; |
| 63 | } |
| 64 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 65 | int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid) |
| 66 | { |
| 67 | struct cfmuxl *muxl = (struct cfmuxl *) layr; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 68 | |
| 69 | spin_lock_bh(&muxl->transmit_lock); |
| 70 | list_add_rcu(&dn->node, &muxl->frml_list); |
| 71 | spin_unlock_bh(&muxl->transmit_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static struct cflayer *get_from_id(struct list_head *list, u16 id) |
| 76 | { |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 77 | struct cflayer *lyr; |
| 78 | list_for_each_entry_rcu(lyr, list, node) { |
| 79 | if (lyr->id == id) |
| 80 | return lyr; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 81 | } |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 82 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
| 85 | |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 86 | int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid) |
| 87 | { |
| 88 | struct cfmuxl *muxl = container_obj(layr); |
| 89 | struct cflayer *old; |
| 90 | |
| 91 | spin_lock_bh(&muxl->receive_lock); |
| 92 | |
| 93 | /* Two entries with same id is wrong, so remove old layer from mux */ |
| 94 | old = get_from_id(&muxl->srvl_list, linkid); |
| 95 | if (old != NULL) |
| 96 | list_del_rcu(&old->node); |
| 97 | |
| 98 | list_add_rcu(&up->node, &muxl->srvl_list); |
| 99 | spin_unlock_bh(&muxl->receive_lock); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 104 | struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid) |
| 105 | { |
| 106 | struct cfmuxl *muxl = container_obj(layr); |
| 107 | struct cflayer *dn; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 108 | int idx = phyid % DN_CACHE_SIZE; |
| 109 | |
| 110 | spin_lock_bh(&muxl->transmit_lock); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 111 | RCU_INIT_POINTER(muxl->dn_cache[idx], NULL); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 112 | dn = get_from_id(&muxl->frml_list, phyid); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 113 | if (dn == NULL) |
| 114 | goto out; |
| 115 | |
| 116 | list_del_rcu(&dn->node); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 117 | caif_assert(dn != NULL); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 118 | out: |
| 119 | spin_unlock_bh(&muxl->transmit_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 120 | return dn; |
| 121 | } |
| 122 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 123 | static struct cflayer *get_up(struct cfmuxl *muxl, u16 id) |
| 124 | { |
| 125 | struct cflayer *up; |
| 126 | int idx = id % UP_CACHE_SIZE; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 127 | up = rcu_dereference(muxl->up_cache[idx]); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 128 | if (up == NULL || up->id != id) { |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 129 | spin_lock_bh(&muxl->receive_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 130 | up = get_from_id(&muxl->srvl_list, id); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 131 | rcu_assign_pointer(muxl->up_cache[idx], up); |
| 132 | spin_unlock_bh(&muxl->receive_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 133 | } |
| 134 | return up; |
| 135 | } |
| 136 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 137 | static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info) |
| 138 | { |
| 139 | struct cflayer *dn; |
| 140 | int idx = dev_info->id % DN_CACHE_SIZE; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 141 | dn = rcu_dereference(muxl->dn_cache[idx]); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 142 | if (dn == NULL || dn->id != dev_info->id) { |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 143 | spin_lock_bh(&muxl->transmit_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 144 | dn = get_from_id(&muxl->frml_list, dev_info->id); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 145 | rcu_assign_pointer(muxl->dn_cache[idx], dn); |
| 146 | spin_unlock_bh(&muxl->transmit_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 147 | } |
| 148 | return dn; |
| 149 | } |
| 150 | |
| 151 | struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id) |
| 152 | { |
| 153 | struct cflayer *up; |
| 154 | struct cfmuxl *muxl = container_obj(layr); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 155 | int idx = id % UP_CACHE_SIZE; |
| 156 | |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 157 | if (id == 0) { |
| 158 | pr_warn("Trying to remove control layer\n"); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 162 | spin_lock_bh(&muxl->receive_lock); |
| 163 | up = get_from_id(&muxl->srvl_list, id); |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 164 | if (up == NULL) |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 165 | goto out; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 166 | |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 167 | RCU_INIT_POINTER(muxl->up_cache[idx], NULL); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 168 | list_del_rcu(&up->node); |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 169 | out: |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 170 | spin_unlock_bh(&muxl->receive_lock); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 171 | return up; |
| 172 | } |
| 173 | |
| 174 | static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt) |
| 175 | { |
| 176 | int ret; |
| 177 | struct cfmuxl *muxl = container_obj(layr); |
| 178 | u8 id; |
| 179 | struct cflayer *up; |
| 180 | if (cfpkt_extr_head(pkt, &id, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 181 | pr_err("erroneous Caif Packet\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 182 | cfpkt_destroy(pkt); |
| 183 | return -EPROTO; |
| 184 | } |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 185 | rcu_read_lock(); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 186 | up = get_up(muxl, id); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 187 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 188 | if (up == NULL) { |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 189 | pr_debug("Received data on unknown link ID = %d (0x%x)" |
| 190 | " up == NULL", id, id); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 191 | cfpkt_destroy(pkt); |
| 192 | /* |
| 193 | * Don't return ERROR, since modem misbehaves and sends out |
| 194 | * flow on before linksetup response. |
| 195 | */ |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 196 | |
| 197 | rcu_read_unlock(); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 198 | return /* CFGLU_EPROT; */ 0; |
| 199 | } |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 200 | |
| 201 | /* We can't hold rcu_lock during receive, so take a ref count instead */ |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 202 | cfsrvl_get(up); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 203 | rcu_read_unlock(); |
| 204 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 205 | ret = up->receive(up, pkt); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 206 | |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 207 | cfsrvl_put(up); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 212 | { |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 213 | struct cfmuxl *muxl = container_obj(layr); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 214 | int err; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 215 | u8 linkid; |
| 216 | struct cflayer *dn; |
| 217 | struct caif_payload_info *info = cfpkt_info(pkt); |
Sjur Brændeland | 39b9afb | 2011-04-11 10:43:52 +0000 | [diff] [blame] | 218 | BUG_ON(!info); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 219 | |
| 220 | rcu_read_lock(); |
| 221 | |
Sjur Brændeland | 39b9afb | 2011-04-11 10:43:52 +0000 | [diff] [blame] | 222 | dn = get_dn(muxl, info->dev_info); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 223 | if (dn == NULL) { |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 224 | pr_debug("Send data on unknown phy ID = %d (0x%x)\n", |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 225 | info->dev_info->id, info->dev_info->id); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 226 | rcu_read_unlock(); |
| 227 | cfpkt_destroy(pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 228 | return -ENOTCONN; |
| 229 | } |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 230 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 231 | info->hdr_len += 1; |
| 232 | linkid = info->channel_id; |
| 233 | cfpkt_add_head(pkt, &linkid, 1); |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 234 | |
| 235 | /* We can't hold rcu_lock during receive, so take a ref count instead */ |
| 236 | cffrml_hold(dn); |
| 237 | |
| 238 | rcu_read_unlock(); |
| 239 | |
| 240 | err = dn->transmit(dn, pkt); |
| 241 | |
| 242 | cffrml_put(dn); |
| 243 | return err; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
Silviu-Mihai Popescu | 3bffc47 | 2013-03-06 19:39:57 +0000 | [diff] [blame] | 247 | int phyid) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 248 | { |
| 249 | struct cfmuxl *muxl = container_obj(layr); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 250 | struct cflayer *layer; |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 251 | |
| 252 | rcu_read_lock(); |
| 253 | list_for_each_entry_rcu(layer, &muxl->srvl_list, node) { |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 254 | |
| 255 | if (cfsrvl_phyid_match(layer, phyid) && layer->ctrlcmd) { |
| 256 | |
sjur.brandeland@stericsson.com | a1b7f85 | 2011-06-15 12:38:25 +0000 | [diff] [blame] | 257 | if ((ctrl == _CAIF_CTRLCMD_PHYIF_DOWN_IND || |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 258 | ctrl == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND) && |
sjur.brandeland@stericsson.com | b01377a | 2012-02-02 01:21:02 +0000 | [diff] [blame] | 259 | layer->id != 0) |
| 260 | cfmuxl_remove_uplayer(layr, layer->id); |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 261 | |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 262 | /* NOTE: ctrlcmd is not allowed to block */ |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 263 | layer->ctrlcmd(layer, ctrl, phyid); |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 264 | } |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 265 | } |
sjur.brandeland@stericsson.com | 0b1e973 | 2011-05-13 02:43:59 +0000 | [diff] [blame] | 266 | rcu_read_unlock(); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 267 | } |