Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CAIF Interface registration. |
| 3 | * Copyright (C) ST-Ericsson AB 2010 |
| 4 | * Author: Sjur Brendeland/sjur.brandeland@stericsson.com |
| 5 | * License terms: GNU General Public License (GPL) version 2 |
| 6 | * |
| 7 | * Borrowed heavily from file: pn_dev.c. Thanks to |
| 8 | * Remi Denis-Courmont <remi.denis-courmont@nokia.com> |
| 9 | * and Sakari Ailus <sakari.ailus@nokia.com> |
| 10 | */ |
| 11 | |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 13 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 14 | #include <linux/version.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/if_arp.h> |
| 18 | #include <linux/net.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <net/netns/generic.h> |
| 24 | #include <net/net_namespace.h> |
| 25 | #include <net/pkt_sched.h> |
| 26 | #include <net/caif/caif_device.h> |
| 27 | #include <net/caif/caif_dev.h> |
| 28 | #include <net/caif/caif_layer.h> |
| 29 | #include <net/caif/cfpkt.h> |
| 30 | #include <net/caif/cfcnfg.h> |
| 31 | |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | #define TIMEOUT (HZ*5) |
| 34 | |
| 35 | /* Used for local tracking of the CAIF net devices */ |
| 36 | struct caif_device_entry { |
| 37 | struct cflayer layer; |
| 38 | struct list_head list; |
| 39 | atomic_t in_use; |
| 40 | atomic_t state; |
| 41 | u16 phyid; |
| 42 | struct net_device *netdev; |
| 43 | wait_queue_head_t event; |
| 44 | }; |
| 45 | |
| 46 | struct caif_device_entry_list { |
| 47 | struct list_head list; |
| 48 | /* Protects simulanous deletes in list */ |
| 49 | spinlock_t lock; |
| 50 | }; |
| 51 | |
| 52 | struct caif_net { |
| 53 | struct caif_device_entry_list caifdevs; |
| 54 | }; |
| 55 | |
| 56 | static int caif_net_id; |
| 57 | static struct cfcnfg *cfg; |
| 58 | |
| 59 | static struct caif_device_entry_list *caif_device_list(struct net *net) |
| 60 | { |
| 61 | struct caif_net *caifn; |
| 62 | BUG_ON(!net); |
| 63 | caifn = net_generic(net, caif_net_id); |
| 64 | BUG_ON(!caifn); |
| 65 | return &caifn->caifdevs; |
| 66 | } |
| 67 | |
| 68 | /* Allocate new CAIF device. */ |
| 69 | static struct caif_device_entry *caif_device_alloc(struct net_device *dev) |
| 70 | { |
| 71 | struct caif_device_entry_list *caifdevs; |
| 72 | struct caif_device_entry *caifd; |
| 73 | caifdevs = caif_device_list(dev_net(dev)); |
| 74 | BUG_ON(!caifdevs); |
| 75 | caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC); |
| 76 | if (!caifd) |
| 77 | return NULL; |
| 78 | caifd->netdev = dev; |
| 79 | list_add(&caifd->list, &caifdevs->list); |
| 80 | init_waitqueue_head(&caifd->event); |
| 81 | return caifd; |
| 82 | } |
| 83 | |
| 84 | static struct caif_device_entry *caif_get(struct net_device *dev) |
| 85 | { |
| 86 | struct caif_device_entry_list *caifdevs = |
| 87 | caif_device_list(dev_net(dev)); |
| 88 | struct caif_device_entry *caifd; |
| 89 | BUG_ON(!caifdevs); |
| 90 | list_for_each_entry(caifd, &caifdevs->list, list) { |
| 91 | if (caifd->netdev == dev) |
| 92 | return caifd; |
| 93 | } |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | static void caif_device_destroy(struct net_device *dev) |
| 98 | { |
| 99 | struct caif_device_entry_list *caifdevs = |
| 100 | caif_device_list(dev_net(dev)); |
| 101 | struct caif_device_entry *caifd; |
| 102 | ASSERT_RTNL(); |
| 103 | if (dev->type != ARPHRD_CAIF) |
| 104 | return; |
| 105 | |
| 106 | spin_lock_bh(&caifdevs->lock); |
| 107 | caifd = caif_get(dev); |
| 108 | if (caifd == NULL) { |
| 109 | spin_unlock_bh(&caifdevs->lock); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | list_del(&caifd->list); |
| 114 | spin_unlock_bh(&caifdevs->lock); |
| 115 | |
| 116 | kfree(caifd); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int transmit(struct cflayer *layer, struct cfpkt *pkt) |
| 120 | { |
| 121 | struct caif_device_entry *caifd = |
| 122 | container_of(layer, struct caif_device_entry, layer); |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame^] | 123 | struct sk_buff *skb; |
| 124 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 125 | skb = cfpkt_tonative(pkt); |
| 126 | skb->dev = caifd->netdev; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 127 | |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame^] | 128 | dev_queue_xmit(skb); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl) |
| 134 | { |
| 135 | struct caif_device_entry *caifd; |
| 136 | struct caif_dev_common *caifdev; |
| 137 | caifd = container_of(layr, struct caif_device_entry, layer); |
| 138 | caifdev = netdev_priv(caifd->netdev); |
| 139 | if (ctrl == _CAIF_MODEMCMD_PHYIF_USEFULL) { |
| 140 | atomic_set(&caifd->in_use, 1); |
| 141 | wake_up_interruptible(&caifd->event); |
| 142 | |
| 143 | } else if (ctrl == _CAIF_MODEMCMD_PHYIF_USELESS) { |
| 144 | atomic_set(&caifd->in_use, 0); |
| 145 | wake_up_interruptible(&caifd->event); |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Stuff received packets to associated sockets. |
| 152 | * On error, returns non-zero and releases the skb. |
| 153 | */ |
| 154 | static int receive(struct sk_buff *skb, struct net_device *dev, |
| 155 | struct packet_type *pkttype, struct net_device *orig_dev) |
| 156 | { |
| 157 | struct net *net; |
| 158 | struct cfpkt *pkt; |
| 159 | struct caif_device_entry *caifd; |
| 160 | net = dev_net(dev); |
| 161 | pkt = cfpkt_fromnative(CAIF_DIR_IN, skb); |
| 162 | caifd = caif_get(dev); |
Sjur Braendeland | e5e03ce | 2010-09-21 11:44:45 +0000 | [diff] [blame] | 163 | if (!caifd || !caifd->layer.up || !caifd->layer.up->receive) |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 164 | return NET_RX_DROP; |
| 165 | |
| 166 | if (caifd->layer.up->receive(caifd->layer.up, pkt)) |
| 167 | return NET_RX_DROP; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static struct packet_type caif_packet_type __read_mostly = { |
| 173 | .type = cpu_to_be16(ETH_P_CAIF), |
| 174 | .func = receive, |
| 175 | }; |
| 176 | |
| 177 | static void dev_flowctrl(struct net_device *dev, int on) |
| 178 | { |
| 179 | struct caif_device_entry *caifd = caif_get(dev); |
| 180 | if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) |
| 181 | return; |
| 182 | |
| 183 | caifd->layer.up->ctrlcmd(caifd->layer.up, |
| 184 | on ? |
| 185 | _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND : |
| 186 | _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND, |
| 187 | caifd->layer.id); |
| 188 | } |
| 189 | |
| 190 | /* notify Caif of device events */ |
| 191 | static int caif_device_notify(struct notifier_block *me, unsigned long what, |
| 192 | void *arg) |
| 193 | { |
| 194 | struct net_device *dev = arg; |
| 195 | struct caif_device_entry *caifd = NULL; |
| 196 | struct caif_dev_common *caifdev; |
| 197 | enum cfcnfg_phy_preference pref; |
| 198 | int res = -EINVAL; |
| 199 | enum cfcnfg_phy_type phy_type; |
| 200 | |
| 201 | if (dev->type != ARPHRD_CAIF) |
| 202 | return 0; |
| 203 | |
| 204 | switch (what) { |
| 205 | case NETDEV_REGISTER: |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 206 | netdev_info(dev, "register\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 207 | caifd = caif_device_alloc(dev); |
| 208 | if (caifd == NULL) |
| 209 | break; |
| 210 | caifdev = netdev_priv(dev); |
| 211 | caifdev->flowctrl = dev_flowctrl; |
| 212 | atomic_set(&caifd->state, what); |
| 213 | res = 0; |
| 214 | break; |
| 215 | |
| 216 | case NETDEV_UP: |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 217 | netdev_info(dev, "up\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 218 | caifd = caif_get(dev); |
| 219 | if (caifd == NULL) |
| 220 | break; |
| 221 | caifdev = netdev_priv(dev); |
| 222 | if (atomic_read(&caifd->state) == NETDEV_UP) { |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 223 | netdev_info(dev, "already up\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | atomic_set(&caifd->state, what); |
| 227 | caifd->layer.transmit = transmit; |
| 228 | caifd->layer.modemcmd = modemcmd; |
| 229 | |
| 230 | if (caifdev->use_frag) |
| 231 | phy_type = CFPHYTYPE_FRAG; |
| 232 | else |
| 233 | phy_type = CFPHYTYPE_CAIF; |
| 234 | |
| 235 | switch (caifdev->link_select) { |
| 236 | case CAIF_LINK_HIGH_BANDW: |
Sjur Braendeland | 2c48520 | 2010-04-28 08:54:40 +0000 | [diff] [blame] | 237 | pref = CFPHYPREF_HIGH_BW; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 238 | break; |
| 239 | case CAIF_LINK_LOW_LATENCY: |
Sjur Braendeland | 2c48520 | 2010-04-28 08:54:40 +0000 | [diff] [blame] | 240 | pref = CFPHYPREF_LOW_LAT; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 241 | break; |
| 242 | default: |
| 243 | pref = CFPHYPREF_HIGH_BW; |
| 244 | break; |
| 245 | } |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 246 | dev_hold(dev); |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 247 | cfcnfg_add_phy_layer(cfg, |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 248 | phy_type, |
| 249 | dev, |
| 250 | &caifd->layer, |
| 251 | &caifd->phyid, |
| 252 | pref, |
| 253 | caifdev->use_fcs, |
| 254 | caifdev->use_stx); |
| 255 | strncpy(caifd->layer.name, dev->name, |
| 256 | sizeof(caifd->layer.name) - 1); |
| 257 | caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0; |
| 258 | break; |
| 259 | |
| 260 | case NETDEV_GOING_DOWN: |
| 261 | caifd = caif_get(dev); |
| 262 | if (caifd == NULL) |
| 263 | break; |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 264 | netdev_info(dev, "going down\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 265 | |
| 266 | if (atomic_read(&caifd->state) == NETDEV_GOING_DOWN || |
| 267 | atomic_read(&caifd->state) == NETDEV_DOWN) |
| 268 | break; |
| 269 | |
| 270 | atomic_set(&caifd->state, what); |
| 271 | if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) |
| 272 | return -EINVAL; |
| 273 | caifd->layer.up->ctrlcmd(caifd->layer.up, |
| 274 | _CAIF_CTRLCMD_PHYIF_DOWN_IND, |
| 275 | caifd->layer.id); |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 276 | might_sleep(); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 277 | res = wait_event_interruptible_timeout(caifd->event, |
| 278 | atomic_read(&caifd->in_use) == 0, |
| 279 | TIMEOUT); |
| 280 | break; |
| 281 | |
| 282 | case NETDEV_DOWN: |
| 283 | caifd = caif_get(dev); |
| 284 | if (caifd == NULL) |
| 285 | break; |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 286 | netdev_info(dev, "down\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 287 | if (atomic_read(&caifd->in_use)) |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 288 | netdev_warn(dev, |
| 289 | "Unregistering an active CAIF device\n"); |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 290 | cfcnfg_del_phy_layer(cfg, &caifd->layer); |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 291 | dev_put(dev); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 292 | atomic_set(&caifd->state, what); |
| 293 | break; |
| 294 | |
| 295 | case NETDEV_UNREGISTER: |
| 296 | caifd = caif_get(dev); |
André Carvalho de Matos | f2527ec | 2010-11-01 11:52:47 +0000 | [diff] [blame] | 297 | if (caifd == NULL) |
| 298 | break; |
Joe Perches | f6c9322 | 2010-09-05 22:08:09 +0000 | [diff] [blame] | 299 | netdev_info(dev, "unregister\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 300 | atomic_set(&caifd->state, what); |
| 301 | caif_device_destroy(dev); |
| 302 | break; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static struct notifier_block caif_device_notifier = { |
| 308 | .notifier_call = caif_device_notify, |
| 309 | .priority = 0, |
| 310 | }; |
| 311 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 312 | int caif_connect_client(struct caif_connect_request *conn_req, |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 313 | struct cflayer *client_layer, int *ifindex, |
| 314 | int *headroom, int *tailroom) |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 315 | { |
| 316 | struct cfctrl_link_param param; |
Sjur Braendeland | e539d83 | 2010-04-28 08:54:35 +0000 | [diff] [blame] | 317 | int ret; |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 318 | |
| 319 | ret = caif_connect_req_to_link_param(cfg, conn_req, ¶m); |
Sjur Braendeland | e539d83 | 2010-04-28 08:54:35 +0000 | [diff] [blame] | 320 | if (ret) |
| 321 | return ret; |
| 322 | /* Hook up the adaptation layer. */ |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 323 | return cfcnfg_add_adaptation_layer(cfg, ¶m, |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 324 | client_layer, ifindex, |
| 325 | headroom, tailroom); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 326 | } |
| 327 | EXPORT_SYMBOL(caif_connect_client); |
| 328 | |
| 329 | int caif_disconnect_client(struct cflayer *adap_layer) |
| 330 | { |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 331 | return cfcnfg_disconn_adapt_layer(cfg, adap_layer); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 332 | } |
| 333 | EXPORT_SYMBOL(caif_disconnect_client); |
| 334 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 335 | /* Per-namespace Caif devices handling */ |
| 336 | static int caif_init_net(struct net *net) |
| 337 | { |
| 338 | struct caif_net *caifn = net_generic(net, caif_net_id); |
| 339 | INIT_LIST_HEAD(&caifn->caifdevs.list); |
| 340 | spin_lock_init(&caifn->caifdevs.lock); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static void caif_exit_net(struct net *net) |
| 345 | { |
| 346 | struct net_device *dev; |
| 347 | int res; |
| 348 | rtnl_lock(); |
| 349 | for_each_netdev(net, dev) { |
| 350 | if (dev->type != ARPHRD_CAIF) |
| 351 | continue; |
| 352 | res = dev_close(dev); |
| 353 | caif_device_destroy(dev); |
| 354 | } |
| 355 | rtnl_unlock(); |
| 356 | } |
| 357 | |
| 358 | static struct pernet_operations caif_net_ops = { |
| 359 | .init = caif_init_net, |
| 360 | .exit = caif_exit_net, |
| 361 | .id = &caif_net_id, |
| 362 | .size = sizeof(struct caif_net), |
| 363 | }; |
| 364 | |
| 365 | /* Initialize Caif devices list */ |
| 366 | static int __init caif_device_init(void) |
| 367 | { |
| 368 | int result; |
| 369 | cfg = cfcnfg_create(); |
| 370 | if (!cfg) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 371 | pr_warn("can't create cfcnfg\n"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 372 | goto err_cfcnfg_create_failed; |
| 373 | } |
| 374 | result = register_pernet_device(&caif_net_ops); |
| 375 | |
| 376 | if (result) { |
| 377 | kfree(cfg); |
| 378 | cfg = NULL; |
| 379 | return result; |
| 380 | } |
| 381 | dev_add_pack(&caif_packet_type); |
| 382 | register_netdevice_notifier(&caif_device_notifier); |
| 383 | |
| 384 | return result; |
| 385 | err_cfcnfg_create_failed: |
| 386 | return -ENODEV; |
| 387 | } |
| 388 | |
| 389 | static void __exit caif_device_exit(void) |
| 390 | { |
| 391 | dev_remove_pack(&caif_packet_type); |
| 392 | unregister_pernet_device(&caif_net_ops); |
| 393 | unregister_netdevice_notifier(&caif_device_notifier); |
| 394 | cfcnfg_remove(cfg); |
| 395 | } |
| 396 | |
| 397 | module_init(caif_device_init); |
| 398 | module_exit(caif_device_exit); |