cfmuxl.c 6.3 KB
Newer Older
1 2
/*
 * Copyright (C) ST-Ericsson AB 2010
3
 * Author:	Sjur Brendeland
4 5
 * License terms: GNU General Public License (GPL) version 2
 */
J
Joe Perches 已提交
6 7 8

#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__

9 10 11
#include <linux/stddef.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
12
#include <linux/rculist.h>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <net/caif/cfpkt.h>
#include <net/caif/cfmuxl.h>
#include <net/caif/cfsrvl.h>
#include <net/caif/cffrml.h>

#define container_obj(layr) container_of(layr, struct cfmuxl, layer)

#define CAIF_CTRL_CHANNEL 0
#define UP_CACHE_SIZE 8
#define DN_CACHE_SIZE 8

struct cfmuxl {
	struct cflayer layer;
	struct list_head srvl_list;
	struct list_head frml_list;
	struct cflayer *up_cache[UP_CACHE_SIZE];
	struct cflayer *dn_cache[DN_CACHE_SIZE];
	/*
	 * Set when inserting or removing downwards layers.
	 */
	spinlock_t transmit_lock;

	/*
	 * Set when inserting or removing upwards layers.
	 */
	spinlock_t receive_lock;

};

static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt);
static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt);
static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
45
			   int phyid);
46 47 48 49
static struct cflayer *get_up(struct cfmuxl *muxl, u16 id);

struct cflayer *cfmuxl_create(void)
{
50 51
	struct cfmuxl *this = kzalloc(sizeof(struct cfmuxl), GFP_ATOMIC);

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	if (!this)
		return NULL;
	this->layer.receive = cfmuxl_receive;
	this->layer.transmit = cfmuxl_transmit;
	this->layer.ctrlcmd = cfmuxl_ctrlcmd;
	INIT_LIST_HEAD(&this->srvl_list);
	INIT_LIST_HEAD(&this->frml_list);
	spin_lock_init(&this->transmit_lock);
	spin_lock_init(&this->receive_lock);
	snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux");
	return &this->layer;
}

int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid)
{
	struct cfmuxl *muxl = (struct cfmuxl *) layr;
68 69 70 71

	spin_lock_bh(&muxl->transmit_lock);
	list_add_rcu(&dn->node, &muxl->frml_list);
	spin_unlock_bh(&muxl->transmit_lock);
72 73 74 75 76
	return 0;
}

static struct cflayer *get_from_id(struct list_head *list, u16 id)
{
77 78 79 80
	struct cflayer *lyr;
	list_for_each_entry_rcu(lyr, list, node) {
		if (lyr->id == id)
			return lyr;
81
	}
82

83 84 85
	return NULL;
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid)
{
	struct cfmuxl *muxl = container_obj(layr);
	struct cflayer *old;

	spin_lock_bh(&muxl->receive_lock);

	/* Two entries with same id is wrong, so remove old layer from mux */
	old = get_from_id(&muxl->srvl_list, linkid);
	if (old != NULL)
		list_del_rcu(&old->node);

	list_add_rcu(&up->node, &muxl->srvl_list);
	spin_unlock_bh(&muxl->receive_lock);

	return 0;
}

104 105 106 107
struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid)
{
	struct cfmuxl *muxl = container_obj(layr);
	struct cflayer *dn;
108 109 110
	int idx = phyid % DN_CACHE_SIZE;

	spin_lock_bh(&muxl->transmit_lock);
111
	RCU_INIT_POINTER(muxl->dn_cache[idx], NULL);
112
	dn = get_from_id(&muxl->frml_list, phyid);
113 114 115 116
	if (dn == NULL)
		goto out;

	list_del_rcu(&dn->node);
117
	caif_assert(dn != NULL);
118 119
out:
	spin_unlock_bh(&muxl->transmit_lock);
120 121 122 123 124 125 126
	return dn;
}

static struct cflayer *get_up(struct cfmuxl *muxl, u16 id)
{
	struct cflayer *up;
	int idx = id % UP_CACHE_SIZE;
127
	up = rcu_dereference(muxl->up_cache[idx]);
128
	if (up == NULL || up->id != id) {
129
		spin_lock_bh(&muxl->receive_lock);
130
		up = get_from_id(&muxl->srvl_list, id);
131 132
		rcu_assign_pointer(muxl->up_cache[idx], up);
		spin_unlock_bh(&muxl->receive_lock);
133 134 135 136 137 138 139 140
	}
	return up;
}

static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info)
{
	struct cflayer *dn;
	int idx = dev_info->id % DN_CACHE_SIZE;
141
	dn = rcu_dereference(muxl->dn_cache[idx]);
142
	if (dn == NULL || dn->id != dev_info->id) {
143
		spin_lock_bh(&muxl->transmit_lock);
144
		dn = get_from_id(&muxl->frml_list, dev_info->id);
145 146
		rcu_assign_pointer(muxl->dn_cache[idx], dn);
		spin_unlock_bh(&muxl->transmit_lock);
147 148 149 150 151 152 153 154
	}
	return dn;
}

struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id)
{
	struct cflayer *up;
	struct cfmuxl *muxl = container_obj(layr);
155 156
	int idx = id % UP_CACHE_SIZE;

157 158 159 160 161
	if (id == 0) {
		pr_warn("Trying to remove control layer\n");
		return NULL;
	}

162 163
	spin_lock_bh(&muxl->receive_lock);
	up = get_from_id(&muxl->srvl_list, id);
164
	if (up == NULL)
165
		goto out;
166

167
	RCU_INIT_POINTER(muxl->up_cache[idx], NULL);
168
	list_del_rcu(&up->node);
169
out:
170
	spin_unlock_bh(&muxl->receive_lock);
171 172 173 174 175 176 177 178 179 180
	return up;
}

static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt)
{
	int ret;
	struct cfmuxl *muxl = container_obj(layr);
	u8 id;
	struct cflayer *up;
	if (cfpkt_extr_head(pkt, &id, 1) < 0) {
J
Joe Perches 已提交
181
		pr_err("erroneous Caif Packet\n");
182 183 184
		cfpkt_destroy(pkt);
		return -EPROTO;
	}
185
	rcu_read_lock();
186
	up = get_up(muxl, id);
187

188
	if (up == NULL) {
189 190
		pr_debug("Received data on unknown link ID = %d (0x%x)"
			" up == NULL", id, id);
191 192 193 194 195
		cfpkt_destroy(pkt);
		/*
		 * Don't return ERROR, since modem misbehaves and sends out
		 * flow on before linksetup response.
		 */
196 197

		rcu_read_unlock();
198 199
		return /* CFGLU_EPROT; */ 0;
	}
200 201

	/* We can't hold rcu_lock during receive, so take a ref count instead */
202
	cfsrvl_get(up);
203 204
	rcu_read_unlock();

205
	ret = up->receive(up, pkt);
206

207
	cfsrvl_put(up);
208 209 210 211 212 213
	return ret;
}

static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt)
{
	struct cfmuxl *muxl = container_obj(layr);
214
	int err;
215 216 217
	u8 linkid;
	struct cflayer *dn;
	struct caif_payload_info *info = cfpkt_info(pkt);
218
	BUG_ON(!info);
219 220 221

	rcu_read_lock();

222
	dn = get_dn(muxl, info->dev_info);
223
	if (dn == NULL) {
224
		pr_debug("Send data on unknown phy ID = %d (0x%x)\n",
J
Joe Perches 已提交
225
			info->dev_info->id, info->dev_info->id);
226 227
		rcu_read_unlock();
		cfpkt_destroy(pkt);
228 229
		return -ENOTCONN;
	}
230

231 232 233
	info->hdr_len += 1;
	linkid = info->channel_id;
	cfpkt_add_head(pkt, &linkid, 1);
234 235 236 237 238 239 240 241 242 243

	/* We can't hold rcu_lock during receive, so take a ref count instead */
	cffrml_hold(dn);

	rcu_read_unlock();

	err = dn->transmit(dn, pkt);

	cffrml_put(dn);
	return err;
244 245 246
}

static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
247
			   int phyid)
248 249 250
{
	struct cfmuxl *muxl = container_obj(layr);
	struct cflayer *layer;
251 252 253

	rcu_read_lock();
	list_for_each_entry_rcu(layer, &muxl->srvl_list, node) {
254 255 256

		if (cfsrvl_phyid_match(layer, phyid) && layer->ctrlcmd) {

257
			if ((ctrl == _CAIF_CTRLCMD_PHYIF_DOWN_IND ||
258
				ctrl == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND) &&
259 260 261
					layer->id != 0)
				cfmuxl_remove_uplayer(layr, layer->id);

262
			/* NOTE: ctrlcmd is not allowed to block */
263
			layer->ctrlcmd(layer, ctrl, phyid);
264
		}
265
	}
266
	rcu_read_unlock();
267
}
新手
引导
客服 返回
顶部