Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 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> |
| 12 | #include <net/caif/cfpkt.h> |
| 13 | #include <net/caif/cfmuxl.h> |
| 14 | #include <net/caif/cfsrvl.h> |
| 15 | #include <net/caif/cffrml.h> |
| 16 | |
| 17 | #define container_obj(layr) container_of(layr, struct cfmuxl, layer) |
| 18 | |
| 19 | #define CAIF_CTRL_CHANNEL 0 |
| 20 | #define UP_CACHE_SIZE 8 |
| 21 | #define DN_CACHE_SIZE 8 |
| 22 | |
| 23 | struct cfmuxl { |
| 24 | struct cflayer layer; |
| 25 | struct list_head srvl_list; |
| 26 | struct list_head frml_list; |
| 27 | struct cflayer *up_cache[UP_CACHE_SIZE]; |
| 28 | struct cflayer *dn_cache[DN_CACHE_SIZE]; |
| 29 | /* |
| 30 | * Set when inserting or removing downwards layers. |
| 31 | */ |
| 32 | spinlock_t transmit_lock; |
| 33 | |
| 34 | /* |
| 35 | * Set when inserting or removing upwards layers. |
| 36 | */ |
| 37 | spinlock_t receive_lock; |
| 38 | |
| 39 | }; |
| 40 | |
| 41 | static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt); |
| 42 | static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt); |
| 43 | static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
| 44 | int phyid); |
| 45 | static struct cflayer *get_up(struct cfmuxl *muxl, u16 id); |
| 46 | |
| 47 | struct cflayer *cfmuxl_create(void) |
| 48 | { |
| 49 | struct cfmuxl *this = kmalloc(sizeof(struct cfmuxl), GFP_ATOMIC); |
| 50 | if (!this) |
| 51 | return NULL; |
| 52 | memset(this, 0, sizeof(*this)); |
| 53 | this->layer.receive = cfmuxl_receive; |
| 54 | this->layer.transmit = cfmuxl_transmit; |
| 55 | this->layer.ctrlcmd = cfmuxl_ctrlcmd; |
| 56 | INIT_LIST_HEAD(&this->srvl_list); |
| 57 | INIT_LIST_HEAD(&this->frml_list); |
| 58 | spin_lock_init(&this->transmit_lock); |
| 59 | spin_lock_init(&this->receive_lock); |
| 60 | snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux"); |
| 61 | return &this->layer; |
| 62 | } |
| 63 | |
| 64 | int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid) |
| 65 | { |
| 66 | struct cfmuxl *muxl = container_obj(layr); |
| 67 | spin_lock(&muxl->receive_lock); |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 68 | cfsrvl_get(up); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 69 | list_add(&up->node, &muxl->srvl_list); |
| 70 | spin_unlock(&muxl->receive_lock); |
| 71 | return 0; |
| 72 | } |
| 73 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 74 | int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid) |
| 75 | { |
| 76 | struct cfmuxl *muxl = (struct cfmuxl *) layr; |
| 77 | spin_lock(&muxl->transmit_lock); |
| 78 | list_add(&dn->node, &muxl->frml_list); |
| 79 | spin_unlock(&muxl->transmit_lock); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static struct cflayer *get_from_id(struct list_head *list, u16 id) |
| 84 | { |
| 85 | struct list_head *node; |
| 86 | struct cflayer *layer; |
| 87 | list_for_each(node, list) { |
| 88 | layer = list_entry(node, struct cflayer, node); |
| 89 | if (layer->id == id) |
| 90 | return layer; |
| 91 | } |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid) |
| 96 | { |
| 97 | struct cfmuxl *muxl = container_obj(layr); |
| 98 | struct cflayer *dn; |
| 99 | spin_lock(&muxl->transmit_lock); |
| 100 | memset(muxl->dn_cache, 0, sizeof(muxl->dn_cache)); |
| 101 | dn = get_from_id(&muxl->frml_list, phyid); |
| 102 | if (dn == NULL) { |
| 103 | spin_unlock(&muxl->transmit_lock); |
| 104 | return NULL; |
| 105 | } |
| 106 | list_del(&dn->node); |
| 107 | caif_assert(dn != NULL); |
| 108 | spin_unlock(&muxl->transmit_lock); |
| 109 | return dn; |
| 110 | } |
| 111 | |
| 112 | /* Invariant: lock is taken */ |
| 113 | static struct cflayer *get_up(struct cfmuxl *muxl, u16 id) |
| 114 | { |
| 115 | struct cflayer *up; |
| 116 | int idx = id % UP_CACHE_SIZE; |
| 117 | up = muxl->up_cache[idx]; |
| 118 | if (up == NULL || up->id != id) { |
| 119 | up = get_from_id(&muxl->srvl_list, id); |
| 120 | muxl->up_cache[idx] = up; |
| 121 | } |
| 122 | return up; |
| 123 | } |
| 124 | |
| 125 | /* Invariant: lock is taken */ |
| 126 | static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info) |
| 127 | { |
| 128 | struct cflayer *dn; |
| 129 | int idx = dev_info->id % DN_CACHE_SIZE; |
| 130 | dn = muxl->dn_cache[idx]; |
| 131 | if (dn == NULL || dn->id != dev_info->id) { |
| 132 | dn = get_from_id(&muxl->frml_list, dev_info->id); |
| 133 | muxl->dn_cache[idx] = dn; |
| 134 | } |
| 135 | return dn; |
| 136 | } |
| 137 | |
| 138 | struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id) |
| 139 | { |
| 140 | struct cflayer *up; |
| 141 | struct cfmuxl *muxl = container_obj(layr); |
| 142 | spin_lock(&muxl->receive_lock); |
| 143 | up = get_up(muxl, id); |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 144 | if (up == NULL) |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 145 | goto out; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 146 | memset(muxl->up_cache, 0, sizeof(muxl->up_cache)); |
| 147 | list_del(&up->node); |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 148 | cfsrvl_put(up); |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 149 | out: |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 150 | spin_unlock(&muxl->receive_lock); |
| 151 | return up; |
| 152 | } |
| 153 | |
| 154 | static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt) |
| 155 | { |
| 156 | int ret; |
| 157 | struct cfmuxl *muxl = container_obj(layr); |
| 158 | u8 id; |
| 159 | struct cflayer *up; |
| 160 | if (cfpkt_extr_head(pkt, &id, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 161 | pr_err("erroneous Caif Packet\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 162 | cfpkt_destroy(pkt); |
| 163 | return -EPROTO; |
| 164 | } |
| 165 | |
| 166 | spin_lock(&muxl->receive_lock); |
| 167 | up = get_up(muxl, id); |
| 168 | spin_unlock(&muxl->receive_lock); |
| 169 | if (up == NULL) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 170 | pr_info("Received data on unknown link ID = %d (0x%x) up == NULL", |
| 171 | id, id); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 172 | cfpkt_destroy(pkt); |
| 173 | /* |
| 174 | * Don't return ERROR, since modem misbehaves and sends out |
| 175 | * flow on before linksetup response. |
| 176 | */ |
| 177 | return /* CFGLU_EPROT; */ 0; |
| 178 | } |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 179 | cfsrvl_get(up); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 180 | ret = up->receive(up, pkt); |
Sjur Braendeland | 5b20865 | 2010-04-28 08:54:36 +0000 | [diff] [blame] | 181 | cfsrvl_put(up); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 186 | { |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 187 | struct cfmuxl *muxl = container_obj(layr); |
| 188 | u8 linkid; |
| 189 | struct cflayer *dn; |
| 190 | struct caif_payload_info *info = cfpkt_info(pkt); |
| 191 | dn = get_dn(muxl, cfpkt_info(pkt)->dev_info); |
| 192 | if (dn == NULL) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 193 | pr_warn("Send data on unknown phy ID = %d (0x%x)\n", |
| 194 | info->dev_info->id, info->dev_info->id); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 195 | return -ENOTCONN; |
| 196 | } |
| 197 | info->hdr_len += 1; |
| 198 | linkid = info->channel_id; |
| 199 | cfpkt_add_head(pkt, &linkid, 1); |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame^] | 200 | return dn->transmit(dn, pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl, |
| 204 | int phyid) |
| 205 | { |
| 206 | struct cfmuxl *muxl = container_obj(layr); |
Sjur Brændeland | 0c184ed | 2011-04-11 10:11:29 +0000 | [diff] [blame] | 207 | struct list_head *node, *next; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 208 | struct cflayer *layer; |
Sjur Brændeland | 0c184ed | 2011-04-11 10:11:29 +0000 | [diff] [blame] | 209 | list_for_each_safe(node, next, &muxl->srvl_list) { |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 210 | layer = list_entry(node, struct cflayer, node); |
| 211 | if (cfsrvl_phyid_match(layer, phyid)) |
| 212 | layer->ctrlcmd(layer, ctrl, phyid); |
| 213 | } |
| 214 | } |