]>
Commit | Line | Data |
---|---|---|
15c9ac0c SB |
1 | /* |
2 | * Copyright (C) ST-Ericsson AB 2010 | |
3 | * Author: Sjur Brendeland/[email protected] | |
4 | * License terms: GNU General Public License (GPL) version 2 | |
5 | */ | |
b31fa5ba JP |
6 | |
7 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ | |
8 | ||
15c9ac0c SB |
9 | #include <linux/kernel.h> |
10 | #include <linux/stddef.h> | |
6c579906 | 11 | #include <linux/slab.h> |
2aa40aef | 12 | #include <linux/netdevice.h> |
f3621440 | 13 | #include <linux/module.h> |
15c9ac0c SB |
14 | #include <net/caif/caif_layer.h> |
15 | #include <net/caif/cfpkt.h> | |
16 | #include <net/caif/cfcnfg.h> | |
17 | #include <net/caif/cfctrl.h> | |
18 | #include <net/caif/cfmuxl.h> | |
19 | #include <net/caif/cffrml.h> | |
20 | #include <net/caif/cfserl.h> | |
21 | #include <net/caif/cfsrvl.h> | |
f3621440 | 22 | #include <net/caif/caif_dev.h> |
15c9ac0c SB |
23 | |
24 | #define container_obj(layr) container_of(layr, struct cfcnfg, layer) | |
25 | ||
26 | /* Information about CAIF physical interfaces held by Config Module in order | |
27 | * to manage physical interfaces | |
28 | */ | |
29 | struct cfcnfg_phyinfo { | |
f3621440 | 30 | struct list_head node; |
31 | bool up; | |
32 | ||
15c9ac0c SB |
33 | /* Pointer to the layer below the MUX (framing layer) */ |
34 | struct cflayer *frm_layer; | |
35 | /* Pointer to the lowest actual physical layer */ | |
36 | struct cflayer *phy_layer; | |
37 | /* Unique identifier of the physical interface */ | |
38 | unsigned int id; | |
39 | /* Preference of the physical in interface */ | |
40 | enum cfcnfg_phy_preference pref; | |
41 | ||
15c9ac0c SB |
42 | /* Information about the physical device */ |
43 | struct dev_info dev_info; | |
2aa40aef SB |
44 | |
45 | /* Interface index */ | |
46 | int ifindex; | |
47 | ||
7c18d220 | 48 | /* Protocol head room added for CAIF link layer */ |
49 | int head_room; | |
2aa40aef SB |
50 | |
51 | /* Use Start of frame checksum */ | |
52 | bool use_fcs; | |
15c9ac0c SB |
53 | }; |
54 | ||
55 | struct cfcnfg { | |
56 | struct cflayer layer; | |
57 | struct cflayer *ctrl; | |
58 | struct cflayer *mux; | |
f3621440 | 59 | struct list_head phys; |
60 | struct mutex lock; | |
15c9ac0c SB |
61 | }; |
62 | ||
e539d83c | 63 | static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, |
15c9ac0c SB |
64 | enum cfctrl_srv serv, u8 phyid, |
65 | struct cflayer *adapt_layer); | |
8d545c8f | 66 | static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id); |
e539d83c | 67 | static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id, |
15c9ac0c SB |
68 | struct cflayer *adapt_layer); |
69 | static void cfctrl_resp_func(void); | |
70 | static void cfctrl_enum_resp(void); | |
71 | ||
72 | struct cfcnfg *cfcnfg_create(void) | |
73 | { | |
74 | struct cfcnfg *this; | |
75 | struct cfctrl_rsp *resp; | |
f3621440 | 76 | |
77 | might_sleep(); | |
78 | ||
15c9ac0c | 79 | /* Initiate this layer */ |
49afa55b | 80 | this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC); |
7ac2ed0c | 81 | if (!this) |
15c9ac0c | 82 | return NULL; |
15c9ac0c SB |
83 | this->mux = cfmuxl_create(); |
84 | if (!this->mux) | |
85 | goto out_of_mem; | |
86 | this->ctrl = cfctrl_create(); | |
87 | if (!this->ctrl) | |
88 | goto out_of_mem; | |
89 | /* Initiate response functions */ | |
90 | resp = cfctrl_get_respfuncs(this->ctrl); | |
91 | resp->enum_rsp = cfctrl_enum_resp; | |
92 | resp->linkerror_ind = cfctrl_resp_func; | |
e539d83c | 93 | resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp; |
15c9ac0c SB |
94 | resp->sleep_rsp = cfctrl_resp_func; |
95 | resp->wake_rsp = cfctrl_resp_func; | |
96 | resp->restart_rsp = cfctrl_resp_func; | |
97 | resp->radioset_rsp = cfctrl_resp_func; | |
e539d83c SB |
98 | resp->linksetup_rsp = cfcnfg_linkup_rsp; |
99 | resp->reject_rsp = cfcnfg_reject_rsp; | |
f3621440 | 100 | INIT_LIST_HEAD(&this->phys); |
15c9ac0c SB |
101 | |
102 | cfmuxl_set_uplayer(this->mux, this->ctrl, 0); | |
103 | layer_set_dn(this->ctrl, this->mux); | |
104 | layer_set_up(this->ctrl, this); | |
f3621440 | 105 | mutex_init(&this->lock); |
106 | ||
15c9ac0c SB |
107 | return this; |
108 | out_of_mem: | |
f3621440 | 109 | synchronize_rcu(); |
110 | ||
15c9ac0c SB |
111 | kfree(this->mux); |
112 | kfree(this->ctrl); | |
113 | kfree(this); | |
114 | return NULL; | |
115 | } | |
15c9ac0c SB |
116 | |
117 | void cfcnfg_remove(struct cfcnfg *cfg) | |
118 | { | |
f3621440 | 119 | might_sleep(); |
15c9ac0c | 120 | if (cfg) { |
f3621440 | 121 | synchronize_rcu(); |
122 | ||
15c9ac0c | 123 | kfree(cfg->mux); |
c85c2951 | 124 | cfctrl_remove(cfg->ctrl); |
15c9ac0c SB |
125 | kfree(cfg); |
126 | } | |
127 | } | |
128 | ||
129 | static void cfctrl_resp_func(void) | |
130 | { | |
131 | } | |
132 | ||
f3621440 | 133 | static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo_rcu(struct cfcnfg *cnfg, |
134 | u8 phyid) | |
135 | { | |
136 | struct cfcnfg_phyinfo *phy; | |
137 | ||
138 | list_for_each_entry_rcu(phy, &cnfg->phys, node) | |
139 | if (phy->id == phyid) | |
140 | return phy; | |
141 | return NULL; | |
142 | } | |
143 | ||
15c9ac0c SB |
144 | static void cfctrl_enum_resp(void) |
145 | { | |
146 | } | |
147 | ||
bee925db | 148 | static struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg, |
15c9ac0c SB |
149 | enum cfcnfg_phy_preference phy_pref) |
150 | { | |
15c9ac0c | 151 | /* Try to match with specified preference */ |
f3621440 | 152 | struct cfcnfg_phyinfo *phy; |
153 | ||
154 | list_for_each_entry_rcu(phy, &cnfg->phys, node) { | |
155 | if (phy->up && phy->pref == phy_pref && | |
156 | phy->frm_layer != NULL) | |
157 | ||
158 | return &phy->dev_info; | |
15c9ac0c SB |
159 | } |
160 | ||
f3621440 | 161 | /* Otherwise just return something */ |
162 | list_for_each_entry_rcu(phy, &cnfg->phys, node) | |
163 | if (phy->up) | |
164 | return &phy->dev_info; | |
15c9ac0c | 165 | |
15c9ac0c SB |
166 | return NULL; |
167 | } | |
168 | ||
bee925db | 169 | static int cfcnfg_get_id_from_ifi(struct cfcnfg *cnfg, int ifi) |
15c9ac0c | 170 | { |
f3621440 | 171 | struct cfcnfg_phyinfo *phy; |
172 | ||
173 | list_for_each_entry_rcu(phy, &cnfg->phys, node) | |
174 | if (phy->ifindex == ifi && phy->up) | |
175 | return phy->id; | |
f2527ec4 | 176 | return -ENODEV; |
15c9ac0c SB |
177 | } |
178 | ||
bee925db | 179 | int caif_disconnect_client(struct net *net, struct cflayer *adap_layer) |
15c9ac0c | 180 | { |
54e90fb5 | 181 | u8 channel_id; |
bee925db | 182 | struct cfcnfg *cfg = get_cfcnfg(net); |
01a85901 | 183 | |
15c9ac0c | 184 | caif_assert(adap_layer != NULL); |
f3621440 | 185 | cfctrl_cancel_req(cfg->ctrl, adap_layer); |
54e90fb5 | 186 | channel_id = adap_layer->id; |
187 | if (channel_id != 0) { | |
188 | struct cflayer *servl; | |
189 | servl = cfmuxl_remove_uplayer(cfg->mux, channel_id); | |
7c18d220 | 190 | cfctrl_linkdown_req(cfg->ctrl, channel_id, adap_layer); |
54e90fb5 | 191 | if (servl != NULL) |
192 | layer_set_up(servl, NULL); | |
193 | } else | |
194 | pr_debug("nothing to disconnect\n"); | |
f3621440 | 195 | |
196 | /* Do RCU sync before initiating cleanup */ | |
197 | synchronize_rcu(); | |
8d545c8f SB |
198 | if (adap_layer->ctrlcmd != NULL) |
199 | adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0); | |
54e90fb5 | 200 | return 0; |
15c9ac0c SB |
201 | |
202 | } | |
bee925db | 203 | EXPORT_SYMBOL(caif_disconnect_client); |
5b208656 | 204 | |
8d545c8f | 205 | static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id) |
15c9ac0c | 206 | { |
15c9ac0c SB |
207 | } |
208 | ||
73d6ac63 | 209 | static const int protohead[CFCTRL_SRV_MASK] = { |
2aa40aef SB |
210 | [CFCTRL_SRV_VEI] = 4, |
211 | [CFCTRL_SRV_DATAGRAM] = 7, | |
212 | [CFCTRL_SRV_UTIL] = 4, | |
213 | [CFCTRL_SRV_RFM] = 3, | |
214 | [CFCTRL_SRV_DBG] = 3, | |
215 | }; | |
216 | ||
bee925db | 217 | |
218 | static int caif_connect_req_to_link_param(struct cfcnfg *cnfg, | |
219 | struct caif_connect_request *s, | |
220 | struct cfctrl_link_param *l) | |
221 | { | |
222 | struct dev_info *dev_info; | |
223 | enum cfcnfg_phy_preference pref; | |
224 | int res; | |
225 | ||
226 | memset(l, 0, sizeof(*l)); | |
227 | /* In caif protocol low value is high priority */ | |
228 | l->priority = CAIF_PRIO_MAX - s->priority + 1; | |
229 | ||
230 | if (s->ifindex != 0) { | |
231 | res = cfcnfg_get_id_from_ifi(cnfg, s->ifindex); | |
232 | if (res < 0) | |
233 | return res; | |
234 | l->phyid = res; | |
235 | } else { | |
236 | switch (s->link_selector) { | |
237 | case CAIF_LINK_HIGH_BANDW: | |
238 | pref = CFPHYPREF_HIGH_BW; | |
239 | break; | |
240 | case CAIF_LINK_LOW_LATENCY: | |
241 | pref = CFPHYPREF_LOW_LAT; | |
242 | break; | |
243 | default: | |
244 | return -EINVAL; | |
245 | } | |
246 | dev_info = cfcnfg_get_phyid(cnfg, pref); | |
247 | if (dev_info == NULL) | |
248 | return -ENODEV; | |
249 | l->phyid = dev_info->id; | |
250 | } | |
251 | switch (s->protocol) { | |
252 | case CAIFPROTO_AT: | |
253 | l->linktype = CFCTRL_SRV_VEI; | |
254 | l->endpoint = (s->sockaddr.u.at.type >> 2) & 0x3; | |
255 | l->chtype = s->sockaddr.u.at.type & 0x3; | |
256 | break; | |
257 | case CAIFPROTO_DATAGRAM: | |
258 | l->linktype = CFCTRL_SRV_DATAGRAM; | |
259 | l->chtype = 0x00; | |
260 | l->u.datagram.connid = s->sockaddr.u.dgm.connection_id; | |
261 | break; | |
262 | case CAIFPROTO_DATAGRAM_LOOP: | |
263 | l->linktype = CFCTRL_SRV_DATAGRAM; | |
264 | l->chtype = 0x03; | |
265 | l->endpoint = 0x00; | |
266 | l->u.datagram.connid = s->sockaddr.u.dgm.connection_id; | |
267 | break; | |
268 | case CAIFPROTO_RFM: | |
269 | l->linktype = CFCTRL_SRV_RFM; | |
270 | l->u.datagram.connid = s->sockaddr.u.rfm.connection_id; | |
271 | strncpy(l->u.rfm.volume, s->sockaddr.u.rfm.volume, | |
272 | sizeof(l->u.rfm.volume)-1); | |
273 | l->u.rfm.volume[sizeof(l->u.rfm.volume)-1] = 0; | |
274 | break; | |
275 | case CAIFPROTO_UTIL: | |
276 | l->linktype = CFCTRL_SRV_UTIL; | |
277 | l->endpoint = 0x00; | |
278 | l->chtype = 0x00; | |
279 | strncpy(l->u.utility.name, s->sockaddr.u.util.service, | |
280 | sizeof(l->u.utility.name)-1); | |
281 | l->u.utility.name[sizeof(l->u.utility.name)-1] = 0; | |
282 | caif_assert(sizeof(l->u.utility.name) > 10); | |
283 | l->u.utility.paramlen = s->param.size; | |
284 | if (l->u.utility.paramlen > sizeof(l->u.utility.params)) | |
285 | l->u.utility.paramlen = sizeof(l->u.utility.params); | |
286 | ||
287 | memcpy(l->u.utility.params, s->param.data, | |
288 | l->u.utility.paramlen); | |
289 | ||
290 | break; | |
291 | case CAIFPROTO_DEBUG: | |
292 | l->linktype = CFCTRL_SRV_DBG; | |
293 | l->endpoint = s->sockaddr.u.dbg.service; | |
294 | l->chtype = s->sockaddr.u.dbg.type; | |
295 | break; | |
296 | default: | |
297 | return -EINVAL; | |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
302 | int caif_connect_client(struct net *net, struct caif_connect_request *conn_req, | |
303 | struct cflayer *adap_layer, int *ifindex, | |
2aa40aef SB |
304 | int *proto_head, |
305 | int *proto_tail) | |
15c9ac0c SB |
306 | { |
307 | struct cflayer *frml; | |
f3621440 | 308 | struct cfcnfg_phyinfo *phy; |
309 | int err; | |
bee925db | 310 | struct cfctrl_link_param param; |
311 | struct cfcnfg *cfg = get_cfcnfg(net); | |
f3621440 | 312 | |
313 | rcu_read_lock(); | |
bee925db | 314 | err = caif_connect_req_to_link_param(cfg, conn_req, ¶m); |
315 | if (err) | |
316 | goto unlock; | |
317 | ||
318 | phy = cfcnfg_get_phyinfo_rcu(cfg, param.phyid); | |
f3621440 | 319 | if (!phy) { |
320 | err = -ENODEV; | |
321 | goto unlock; | |
322 | } | |
323 | err = -EINVAL; | |
324 | ||
15c9ac0c | 325 | if (adap_layer == NULL) { |
b31fa5ba | 326 | pr_err("adap_layer is zero\n"); |
f3621440 | 327 | goto unlock; |
15c9ac0c SB |
328 | } |
329 | if (adap_layer->receive == NULL) { | |
b31fa5ba | 330 | pr_err("adap_layer->receive is NULL\n"); |
f3621440 | 331 | goto unlock; |
15c9ac0c SB |
332 | } |
333 | if (adap_layer->ctrlcmd == NULL) { | |
b31fa5ba | 334 | pr_err("adap_layer->ctrlcmd == NULL\n"); |
f3621440 | 335 | goto unlock; |
15c9ac0c | 336 | } |
f3621440 | 337 | |
338 | err = -ENODEV; | |
339 | frml = phy->frm_layer; | |
15c9ac0c | 340 | if (frml == NULL) { |
b31fa5ba | 341 | pr_err("Specified PHY type does not exist!\n"); |
f3621440 | 342 | goto unlock; |
15c9ac0c | 343 | } |
bee925db | 344 | caif_assert(param.phyid == phy->id); |
f3621440 | 345 | caif_assert(phy->frm_layer->id == |
bee925db | 346 | param.phyid); |
f3621440 | 347 | caif_assert(phy->phy_layer->id == |
bee925db | 348 | param.phyid); |
2aa40aef | 349 | |
f3621440 | 350 | *ifindex = phy->ifindex; |
351 | *proto_tail = 2; | |
7c18d220 | 352 | *proto_head = protohead[param.linktype] + phy->head_room; |
2aa40aef | 353 | |
f3621440 | 354 | rcu_read_unlock(); |
2aa40aef | 355 | |
15c9ac0c | 356 | /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */ |
bee925db | 357 | cfctrl_enum_req(cfg->ctrl, param.phyid); |
358 | return cfctrl_linkup_request(cfg->ctrl, ¶m, adap_layer); | |
f3621440 | 359 | |
360 | unlock: | |
361 | rcu_read_unlock(); | |
362 | return err; | |
15c9ac0c | 363 | } |
bee925db | 364 | EXPORT_SYMBOL(caif_connect_client); |
15c9ac0c | 365 | |
e539d83c | 366 | static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id, |
15c9ac0c SB |
367 | struct cflayer *adapt_layer) |
368 | { | |
369 | if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL) | |
370 | adapt_layer->ctrlcmd(adapt_layer, | |
371 | CAIF_CTRLCMD_INIT_FAIL_RSP, 0); | |
372 | } | |
373 | ||
374 | static void | |
e539d83c | 375 | cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv, |
f3621440 | 376 | u8 phyid, struct cflayer *adapt_layer) |
15c9ac0c SB |
377 | { |
378 | struct cfcnfg *cnfg = container_obj(layer); | |
379 | struct cflayer *servicel = NULL; | |
380 | struct cfcnfg_phyinfo *phyinfo; | |
2aa40aef SB |
381 | struct net_device *netdev; |
382 | ||
54e90fb5 | 383 | if (channel_id == 0) { |
384 | pr_warn("received channel_id zero\n"); | |
385 | if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL) | |
386 | adapt_layer->ctrlcmd(adapt_layer, | |
387 | CAIF_CTRLCMD_INIT_FAIL_RSP, 0); | |
388 | return; | |
389 | } | |
390 | ||
f3621440 | 391 | rcu_read_lock(); |
392 | ||
15c9ac0c | 393 | if (adapt_layer == NULL) { |
f3621440 | 394 | pr_debug("link setup response but no client exist," |
395 | "send linkdown back\n"); | |
8d545c8f | 396 | cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL); |
f3621440 | 397 | goto unlock; |
15c9ac0c SB |
398 | } |
399 | ||
400 | caif_assert(cnfg != NULL); | |
401 | caif_assert(phyid != 0); | |
f3621440 | 402 | |
403 | phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phyid); | |
404 | if (phyinfo == NULL) { | |
8a168ca7 | 405 | pr_err("ERROR: Link Layer Device disappeared" |
f3621440 | 406 | "while connecting\n"); |
407 | goto unlock; | |
408 | } | |
409 | ||
410 | caif_assert(phyinfo != NULL); | |
15c9ac0c SB |
411 | caif_assert(phyinfo->id == phyid); |
412 | caif_assert(phyinfo->phy_layer != NULL); | |
413 | caif_assert(phyinfo->phy_layer->id == phyid); | |
414 | ||
e539d83c | 415 | adapt_layer->id = channel_id; |
15c9ac0c SB |
416 | |
417 | switch (serv) { | |
418 | case CFCTRL_SRV_VEI: | |
e539d83c | 419 | servicel = cfvei_create(channel_id, &phyinfo->dev_info); |
15c9ac0c SB |
420 | break; |
421 | case CFCTRL_SRV_DATAGRAM: | |
f3621440 | 422 | servicel = cfdgml_create(channel_id, |
423 | &phyinfo->dev_info); | |
15c9ac0c SB |
424 | break; |
425 | case CFCTRL_SRV_RFM: | |
2aa40aef | 426 | netdev = phyinfo->dev_info.dev; |
a7da1f55 | 427 | servicel = cfrfml_create(channel_id, &phyinfo->dev_info, |
2aa40aef | 428 | netdev->mtu); |
15c9ac0c SB |
429 | break; |
430 | case CFCTRL_SRV_UTIL: | |
e539d83c | 431 | servicel = cfutill_create(channel_id, &phyinfo->dev_info); |
15c9ac0c SB |
432 | break; |
433 | case CFCTRL_SRV_VIDEO: | |
e539d83c | 434 | servicel = cfvidl_create(channel_id, &phyinfo->dev_info); |
15c9ac0c SB |
435 | break; |
436 | case CFCTRL_SRV_DBG: | |
e539d83c | 437 | servicel = cfdbgl_create(channel_id, &phyinfo->dev_info); |
15c9ac0c SB |
438 | break; |
439 | default: | |
f3621440 | 440 | pr_err("Protocol error. Link setup response " |
441 | "- unknown channel type\n"); | |
442 | goto unlock; | |
15c9ac0c | 443 | } |
7ac2ed0c | 444 | if (!servicel) |
f3621440 | 445 | goto unlock; |
15c9ac0c | 446 | layer_set_dn(servicel, cnfg->mux); |
e539d83c | 447 | cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id); |
15c9ac0c SB |
448 | layer_set_up(servicel, adapt_layer); |
449 | layer_set_dn(adapt_layer, servicel); | |
f3621440 | 450 | |
451 | rcu_read_unlock(); | |
452 | ||
15c9ac0c | 453 | servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0); |
f3621440 | 454 | return; |
455 | unlock: | |
456 | rcu_read_unlock(); | |
15c9ac0c SB |
457 | } |
458 | ||
459 | void | |
7c18d220 | 460 | cfcnfg_add_phy_layer(struct cfcnfg *cnfg, |
2aa40aef | 461 | struct net_device *dev, struct cflayer *phy_layer, |
bee925db | 462 | enum cfcnfg_phy_preference pref, |
7c18d220 | 463 | struct cflayer *link_support, |
464 | bool fcs, int head_room) | |
15c9ac0c SB |
465 | { |
466 | struct cflayer *frml; | |
5bb20ed8 | 467 | struct cfcnfg_phyinfo *phyinfo = NULL; |
15c9ac0c | 468 | int i; |
f3621440 | 469 | u8 phyid; |
15c9ac0c | 470 | |
f3621440 | 471 | mutex_lock(&cnfg->lock); |
15c9ac0c | 472 | |
f3621440 | 473 | /* CAIF protocol allow maximum 6 link-layers */ |
474 | for (i = 0; i < 7; i++) { | |
475 | phyid = (dev->ifindex + i) & 0x7; | |
476 | if (phyid == 0) | |
477 | continue; | |
478 | if (cfcnfg_get_phyinfo_rcu(cnfg, phyid) == NULL) | |
479 | goto got_phyid; | |
15c9ac0c | 480 | } |
f3621440 | 481 | pr_warn("Too many CAIF Link Layers (max 6)\n"); |
7c18d220 | 482 | goto out; |
f3621440 | 483 | |
484 | got_phyid: | |
485 | phyinfo = kzalloc(sizeof(struct cfcnfg_phyinfo), GFP_ATOMIC); | |
5bb20ed8 DC |
486 | if (!phyinfo) |
487 | goto out_err; | |
15c9ac0c | 488 | |
f3621440 | 489 | phy_layer->id = phyid; |
490 | phyinfo->pref = pref; | |
491 | phyinfo->id = phyid; | |
492 | phyinfo->dev_info.id = phyid; | |
493 | phyinfo->dev_info.dev = dev; | |
494 | phyinfo->phy_layer = phy_layer; | |
495 | phyinfo->ifindex = dev->ifindex; | |
7c18d220 | 496 | phyinfo->head_room = head_room; |
f3621440 | 497 | phyinfo->use_fcs = fcs; |
2aa40aef | 498 | |
f3621440 | 499 | frml = cffrml_create(phyid, fcs); |
500 | ||
5bb20ed8 DC |
501 | if (!frml) |
502 | goto out_err; | |
f3621440 | 503 | phyinfo->frm_layer = frml; |
15c9ac0c SB |
504 | layer_set_up(frml, cnfg->mux); |
505 | ||
7c18d220 | 506 | if (link_support != NULL) { |
507 | link_support->id = phyid; | |
508 | layer_set_dn(frml, link_support); | |
509 | layer_set_up(link_support, frml); | |
510 | layer_set_dn(link_support, phy_layer); | |
511 | layer_set_up(phy_layer, link_support); | |
15c9ac0c SB |
512 | } else { |
513 | layer_set_dn(frml, phy_layer); | |
514 | layer_set_up(phy_layer, frml); | |
515 | } | |
f3621440 | 516 | |
517 | list_add_rcu(&phyinfo->node, &cnfg->phys); | |
7c18d220 | 518 | out: |
5bb20ed8 DC |
519 | mutex_unlock(&cnfg->lock); |
520 | return; | |
521 | ||
522 | out_err: | |
5bb20ed8 | 523 | kfree(phyinfo); |
f3621440 | 524 | mutex_unlock(&cnfg->lock); |
15c9ac0c SB |
525 | } |
526 | EXPORT_SYMBOL(cfcnfg_add_phy_layer); | |
527 | ||
f3621440 | 528 | int cfcnfg_set_phy_state(struct cfcnfg *cnfg, struct cflayer *phy_layer, |
529 | bool up) | |
530 | { | |
531 | struct cfcnfg_phyinfo *phyinfo; | |
532 | ||
533 | rcu_read_lock(); | |
534 | phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phy_layer->id); | |
535 | if (phyinfo == NULL) { | |
536 | rcu_read_unlock(); | |
537 | return -ENODEV; | |
538 | } | |
539 | ||
540 | if (phyinfo->up == up) { | |
541 | rcu_read_unlock(); | |
542 | return 0; | |
543 | } | |
544 | phyinfo->up = up; | |
545 | ||
546 | if (up) { | |
547 | cffrml_hold(phyinfo->frm_layer); | |
548 | cfmuxl_set_dnlayer(cnfg->mux, phyinfo->frm_layer, | |
549 | phy_layer->id); | |
550 | } else { | |
551 | cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id); | |
552 | cffrml_put(phyinfo->frm_layer); | |
553 | } | |
554 | ||
555 | rcu_read_unlock(); | |
556 | return 0; | |
557 | } | |
558 | EXPORT_SYMBOL(cfcnfg_set_phy_state); | |
559 | ||
15c9ac0c SB |
560 | int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer) |
561 | { | |
562 | struct cflayer *frml, *frml_dn; | |
563 | u16 phyid; | |
f3621440 | 564 | struct cfcnfg_phyinfo *phyinfo; |
565 | ||
566 | might_sleep(); | |
567 | ||
568 | mutex_lock(&cnfg->lock); | |
569 | ||
15c9ac0c | 570 | phyid = phy_layer->id; |
f3621440 | 571 | phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phyid); |
572 | ||
bee925db | 573 | if (phyinfo == NULL) { |
574 | mutex_unlock(&cnfg->lock); | |
f3621440 | 575 | return 0; |
bee925db | 576 | } |
f3621440 | 577 | caif_assert(phyid == phyinfo->id); |
578 | caif_assert(phy_layer == phyinfo->phy_layer); | |
15c9ac0c | 579 | caif_assert(phy_layer->id == phyid); |
f3621440 | 580 | caif_assert(phyinfo->frm_layer->id == phyid); |
581 | ||
bee925db | 582 | list_del_rcu(&phyinfo->node); |
583 | synchronize_rcu(); | |
584 | ||
cb3cb423 | 585 | /* Fail if reference count is not zero */ |
586 | if (cffrml_refcnt_read(phyinfo->frm_layer) != 0) { | |
587 | pr_info("Wait for device inuse\n"); | |
bee925db | 588 | list_add_rcu(&phyinfo->node, &cnfg->phys); |
cb3cb423 | 589 | mutex_unlock(&cnfg->lock); |
590 | return -EAGAIN; | |
591 | } | |
592 | ||
f3621440 | 593 | frml = phyinfo->frm_layer; |
15c9ac0c SB |
594 | frml_dn = frml->dn; |
595 | cffrml_set_uplayer(frml, NULL); | |
596 | cffrml_set_dnlayer(frml, NULL); | |
15c9ac0c SB |
597 | if (phy_layer != frml_dn) { |
598 | layer_set_up(frml_dn, NULL); | |
599 | layer_set_dn(frml_dn, NULL); | |
15c9ac0c SB |
600 | } |
601 | layer_set_up(phy_layer, NULL); | |
f3621440 | 602 | |
f3621440 | 603 | if (phyinfo->phy_layer != frml_dn) |
604 | kfree(frml_dn); | |
605 | ||
cb3cb423 | 606 | cffrml_free(frml); |
f3621440 | 607 | kfree(phyinfo); |
608 | mutex_unlock(&cnfg->lock); | |
609 | ||
15c9ac0c SB |
610 | return 0; |
611 | } | |
612 | EXPORT_SYMBOL(cfcnfg_del_phy_layer); |