Linux 3.2.102
[pandora-kernel.git] / net / caif / cfcnfg.c
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  */
6
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
9 #include <linux/kernel.h>
10 #include <linux/stddef.h>
11 #include <linux/slab.h>
12 #include <linux/netdevice.h>
13 #include <linux/module.h>
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>
22 #include <net/caif/caif_dev.h>
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 {
30         struct list_head node;
31         bool up;
32
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
42         /* Information about the physical device */
43         struct dev_info dev_info;
44
45         /* Interface index */
46         int ifindex;
47
48         /* Use Start of frame extension */
49         bool use_stx;
50
51         /* Use Start of frame checksum */
52         bool use_fcs;
53 };
54
55 struct cfcnfg {
56         struct cflayer layer;
57         struct cflayer *ctrl;
58         struct cflayer *mux;
59         struct list_head phys;
60         struct mutex lock;
61 };
62
63 static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
64                              enum cfctrl_srv serv, u8 phyid,
65                              struct cflayer *adapt_layer);
66 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id);
67 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
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;
76
77         might_sleep();
78
79         /* Initiate this layer */
80         this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
81         if (!this)
82                 return NULL;
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;
93         resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
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;
98         resp->linksetup_rsp = cfcnfg_linkup_rsp;
99         resp->reject_rsp = cfcnfg_reject_rsp;
100         INIT_LIST_HEAD(&this->phys);
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);
105         mutex_init(&this->lock);
106
107         return this;
108 out_of_mem:
109         synchronize_rcu();
110
111         kfree(this->mux);
112         kfree(this->ctrl);
113         kfree(this);
114         return NULL;
115 }
116
117 void cfcnfg_remove(struct cfcnfg *cfg)
118 {
119         might_sleep();
120         if (cfg) {
121                 synchronize_rcu();
122
123                 kfree(cfg->mux);
124                 cfctrl_remove(cfg->ctrl);
125                 kfree(cfg);
126         }
127 }
128
129 static void cfctrl_resp_func(void)
130 {
131 }
132
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
144 static void cfctrl_enum_resp(void)
145 {
146 }
147
148 static struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
149                                   enum cfcnfg_phy_preference phy_pref)
150 {
151         /* Try to match with specified preference */
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;
159         }
160
161         /* Otherwise just return something */
162         list_for_each_entry_rcu(phy, &cnfg->phys, node)
163                 if (phy->up)
164                         return &phy->dev_info;
165
166         return NULL;
167 }
168
169 static int cfcnfg_get_id_from_ifi(struct cfcnfg *cnfg, int ifi)
170 {
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;
176         return -ENODEV;
177 }
178
179 int caif_disconnect_client(struct net *net, struct cflayer *adap_layer)
180 {
181         u8 channel_id;
182         struct cfcnfg *cfg = get_cfcnfg(net);
183
184         caif_assert(adap_layer != NULL);
185         cfctrl_cancel_req(cfg->ctrl, adap_layer);
186         channel_id = adap_layer->id;
187         if (channel_id != 0) {
188                 struct cflayer *servl;
189                 servl = cfmuxl_remove_uplayer(cfg->mux, channel_id);
190                 if (servl != NULL)
191                         layer_set_up(servl, NULL);
192         } else
193                 pr_debug("nothing to disconnect\n");
194         cfctrl_linkdown_req(cfg->ctrl, channel_id, adap_layer);
195
196         /* Do RCU sync before initiating cleanup */
197         synchronize_rcu();
198         if (adap_layer->ctrlcmd != NULL)
199                 adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0);
200         return 0;
201
202 }
203 EXPORT_SYMBOL(caif_disconnect_client);
204
205 static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id)
206 {
207 }
208
209 static const int protohead[CFCTRL_SRV_MASK] = {
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
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,
304                                 int *proto_head,
305                                 int *proto_tail)
306 {
307         struct cflayer *frml;
308         struct cfcnfg_phyinfo *phy;
309         int err;
310         struct cfctrl_link_param param;
311         struct cfcnfg *cfg = get_cfcnfg(net);
312
313         rcu_read_lock();
314         err = caif_connect_req_to_link_param(cfg, conn_req, &param);
315         if (err)
316                 goto unlock;
317
318         phy = cfcnfg_get_phyinfo_rcu(cfg, param.phyid);
319         if (!phy) {
320                 err = -ENODEV;
321                 goto unlock;
322         }
323         err = -EINVAL;
324
325         if (adap_layer == NULL) {
326                 pr_err("adap_layer is zero\n");
327                 goto unlock;
328         }
329         if (adap_layer->receive == NULL) {
330                 pr_err("adap_layer->receive is NULL\n");
331                 goto unlock;
332         }
333         if (adap_layer->ctrlcmd == NULL) {
334                 pr_err("adap_layer->ctrlcmd == NULL\n");
335                 goto unlock;
336         }
337
338         err = -ENODEV;
339         frml = phy->frm_layer;
340         if (frml == NULL) {
341                 pr_err("Specified PHY type does not exist!\n");
342                 goto unlock;
343         }
344         caif_assert(param.phyid == phy->id);
345         caif_assert(phy->frm_layer->id ==
346                      param.phyid);
347         caif_assert(phy->phy_layer->id ==
348                      param.phyid);
349
350         *ifindex = phy->ifindex;
351         *proto_tail = 2;
352         *proto_head =
353
354         protohead[param.linktype] + (phy->use_stx ? 1 : 0);
355
356         rcu_read_unlock();
357
358         /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
359         cfctrl_enum_req(cfg->ctrl, param.phyid);
360         return cfctrl_linkup_request(cfg->ctrl, &param, adap_layer);
361
362 unlock:
363         rcu_read_unlock();
364         return err;
365 }
366 EXPORT_SYMBOL(caif_connect_client);
367
368 static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
369                              struct cflayer *adapt_layer)
370 {
371         if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
372                 adapt_layer->ctrlcmd(adapt_layer,
373                                      CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
374 }
375
376 static void
377 cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
378                   u8 phyid, struct cflayer *adapt_layer)
379 {
380         struct cfcnfg *cnfg = container_obj(layer);
381         struct cflayer *servicel = NULL;
382         struct cfcnfg_phyinfo *phyinfo;
383         struct net_device *netdev;
384
385         if (channel_id == 0) {
386                 pr_warn("received channel_id zero\n");
387                 if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
388                         adapt_layer->ctrlcmd(adapt_layer,
389                                                 CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
390                 return;
391         }
392
393         rcu_read_lock();
394
395         if (adapt_layer == NULL) {
396                 pr_debug("link setup response but no client exist,"
397                                 "send linkdown back\n");
398                 cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
399                 goto unlock;
400         }
401
402         caif_assert(cnfg != NULL);
403         caif_assert(phyid != 0);
404
405         phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phyid);
406         if (phyinfo == NULL) {
407                 pr_err("ERROR: Link Layer Device dissapeared"
408                                 "while connecting\n");
409                 goto unlock;
410         }
411
412         caif_assert(phyinfo != NULL);
413         caif_assert(phyinfo->id == phyid);
414         caif_assert(phyinfo->phy_layer != NULL);
415         caif_assert(phyinfo->phy_layer->id == phyid);
416
417         adapt_layer->id = channel_id;
418
419         switch (serv) {
420         case CFCTRL_SRV_VEI:
421                 servicel = cfvei_create(channel_id, &phyinfo->dev_info);
422                 break;
423         case CFCTRL_SRV_DATAGRAM:
424                 servicel = cfdgml_create(channel_id,
425                                         &phyinfo->dev_info);
426                 break;
427         case CFCTRL_SRV_RFM:
428                 netdev = phyinfo->dev_info.dev;
429                 servicel = cfrfml_create(channel_id, &phyinfo->dev_info,
430                                                 netdev->mtu);
431                 break;
432         case CFCTRL_SRV_UTIL:
433                 servicel = cfutill_create(channel_id, &phyinfo->dev_info);
434                 break;
435         case CFCTRL_SRV_VIDEO:
436                 servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
437                 break;
438         case CFCTRL_SRV_DBG:
439                 servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
440                 break;
441         default:
442                 pr_err("Protocol error. Link setup response "
443                                 "- unknown channel type\n");
444                 goto unlock;
445         }
446         if (!servicel)
447                 goto unlock;
448         layer_set_dn(servicel, cnfg->mux);
449         cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
450         layer_set_up(servicel, adapt_layer);
451         layer_set_dn(adapt_layer, servicel);
452
453         rcu_read_unlock();
454
455         servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
456         return;
457 unlock:
458         rcu_read_unlock();
459 }
460
461 void
462 cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
463                      struct net_device *dev, struct cflayer *phy_layer,
464                      enum cfcnfg_phy_preference pref,
465                      bool fcs, bool stx)
466 {
467         struct cflayer *frml;
468         struct cflayer *phy_driver = NULL;
469         struct cfcnfg_phyinfo *phyinfo = NULL;
470         int i;
471         u8 phyid;
472
473         mutex_lock(&cnfg->lock);
474
475         /* CAIF protocol allow maximum 6 link-layers */
476         for (i = 0; i < 7; i++) {
477                 phyid = (dev->ifindex + i) & 0x7;
478                 if (phyid == 0)
479                         continue;
480                 if (cfcnfg_get_phyinfo_rcu(cnfg, phyid) == NULL)
481                         goto got_phyid;
482         }
483         pr_warn("Too many CAIF Link Layers (max 6)\n");
484         goto out_err;
485
486 got_phyid:
487         phyinfo = kzalloc(sizeof(struct cfcnfg_phyinfo), GFP_ATOMIC);
488         if (!phyinfo)
489                 goto out_err;
490
491         switch (phy_type) {
492         case CFPHYTYPE_FRAG:
493                 phy_driver =
494                     cfserl_create(CFPHYTYPE_FRAG, phyid, stx);
495                 if (!phy_driver)
496                         goto out_err;
497                 break;
498         case CFPHYTYPE_CAIF:
499                 phy_driver = NULL;
500                 break;
501         default:
502                 goto out_err;
503         }
504         phy_layer->id = phyid;
505         phyinfo->pref = pref;
506         phyinfo->id = phyid;
507         phyinfo->dev_info.id = phyid;
508         phyinfo->dev_info.dev = dev;
509         phyinfo->phy_layer = phy_layer;
510         phyinfo->ifindex = dev->ifindex;
511         phyinfo->use_stx = stx;
512         phyinfo->use_fcs = fcs;
513
514         frml = cffrml_create(phyid, fcs);
515
516         if (!frml)
517                 goto out_err;
518         phyinfo->frm_layer = frml;
519         layer_set_up(frml, cnfg->mux);
520
521         if (phy_driver != NULL) {
522                 phy_driver->id = phyid;
523                 layer_set_dn(frml, phy_driver);
524                 layer_set_up(phy_driver, frml);
525                 layer_set_dn(phy_driver, phy_layer);
526                 layer_set_up(phy_layer, phy_driver);
527         } else {
528                 layer_set_dn(frml, phy_layer);
529                 layer_set_up(phy_layer, frml);
530         }
531
532         list_add_rcu(&phyinfo->node, &cnfg->phys);
533         mutex_unlock(&cnfg->lock);
534         return;
535
536 out_err:
537         kfree(phy_driver);
538         kfree(phyinfo);
539         mutex_unlock(&cnfg->lock);
540 }
541 EXPORT_SYMBOL(cfcnfg_add_phy_layer);
542
543 int cfcnfg_set_phy_state(struct cfcnfg *cnfg, struct cflayer *phy_layer,
544                 bool up)
545 {
546         struct cfcnfg_phyinfo *phyinfo;
547
548         rcu_read_lock();
549         phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phy_layer->id);
550         if (phyinfo == NULL) {
551                 rcu_read_unlock();
552                 return -ENODEV;
553         }
554
555         if (phyinfo->up == up) {
556                 rcu_read_unlock();
557                 return 0;
558         }
559         phyinfo->up = up;
560
561         if (up) {
562                 cffrml_hold(phyinfo->frm_layer);
563                 cfmuxl_set_dnlayer(cnfg->mux, phyinfo->frm_layer,
564                                         phy_layer->id);
565         } else {
566                 cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
567                 cffrml_put(phyinfo->frm_layer);
568         }
569
570         rcu_read_unlock();
571         return 0;
572 }
573 EXPORT_SYMBOL(cfcnfg_set_phy_state);
574
575 int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
576 {
577         struct cflayer *frml, *frml_dn;
578         u16 phyid;
579         struct cfcnfg_phyinfo *phyinfo;
580
581         might_sleep();
582
583         mutex_lock(&cnfg->lock);
584
585         phyid = phy_layer->id;
586         phyinfo = cfcnfg_get_phyinfo_rcu(cnfg, phyid);
587
588         if (phyinfo == NULL) {
589                 mutex_unlock(&cnfg->lock);
590                 return 0;
591         }
592         caif_assert(phyid == phyinfo->id);
593         caif_assert(phy_layer == phyinfo->phy_layer);
594         caif_assert(phy_layer->id == phyid);
595         caif_assert(phyinfo->frm_layer->id == phyid);
596
597         list_del_rcu(&phyinfo->node);
598         synchronize_rcu();
599
600         /* Fail if reference count is not zero */
601         if (cffrml_refcnt_read(phyinfo->frm_layer) != 0) {
602                 pr_info("Wait for device inuse\n");
603                 list_add_rcu(&phyinfo->node, &cnfg->phys);
604                 mutex_unlock(&cnfg->lock);
605                 return -EAGAIN;
606         }
607
608         frml = phyinfo->frm_layer;
609         frml_dn = frml->dn;
610         cffrml_set_uplayer(frml, NULL);
611         cffrml_set_dnlayer(frml, NULL);
612         if (phy_layer != frml_dn) {
613                 layer_set_up(frml_dn, NULL);
614                 layer_set_dn(frml_dn, NULL);
615         }
616         layer_set_up(phy_layer, NULL);
617
618         if (phyinfo->phy_layer != frml_dn)
619                 kfree(frml_dn);
620
621         cffrml_free(frml);
622         kfree(phyinfo);
623         mutex_unlock(&cnfg->lock);
624
625         return 0;
626 }
627 EXPORT_SYMBOL(cfcnfg_del_phy_layer);