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> |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/if_arp.h> |
| 17 | #include <linux/net.h> |
| 18 | #include <linux/netdevice.h> |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 20 | #include <net/netns/generic.h> |
| 21 | #include <net/net_namespace.h> |
| 22 | #include <net/pkt_sched.h> |
| 23 | #include <net/caif/caif_device.h> |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 24 | #include <net/caif/caif_layer.h> |
| 25 | #include <net/caif/cfpkt.h> |
| 26 | #include <net/caif/cfcnfg.h> |
| 27 | |
| 28 | MODULE_LICENSE("GPL"); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 29 | |
| 30 | /* Used for local tracking of the CAIF net devices */ |
| 31 | struct caif_device_entry { |
| 32 | struct cflayer layer; |
| 33 | struct list_head list; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 34 | struct net_device *netdev; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 35 | int __percpu *pcpu_refcnt; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct caif_device_entry_list { |
| 39 | struct list_head list; |
| 40 | /* Protects simulanous deletes in list */ |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 41 | struct mutex lock; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct caif_net { |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 45 | struct cfcnfg *cfg; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 46 | struct caif_device_entry_list caifdevs; |
| 47 | }; |
| 48 | |
| 49 | static int caif_net_id; |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 50 | |
| 51 | struct cfcnfg *get_cfcnfg(struct net *net) |
| 52 | { |
| 53 | struct caif_net *caifn; |
| 54 | BUG_ON(!net); |
| 55 | caifn = net_generic(net, caif_net_id); |
| 56 | BUG_ON(!caifn); |
| 57 | return caifn->cfg; |
| 58 | } |
| 59 | EXPORT_SYMBOL(get_cfcnfg); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 60 | |
| 61 | static struct caif_device_entry_list *caif_device_list(struct net *net) |
| 62 | { |
| 63 | struct caif_net *caifn; |
| 64 | BUG_ON(!net); |
| 65 | caifn = net_generic(net, caif_net_id); |
| 66 | BUG_ON(!caifn); |
| 67 | return &caifn->caifdevs; |
| 68 | } |
| 69 | |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 70 | static void caifd_put(struct caif_device_entry *e) |
| 71 | { |
| 72 | irqsafe_cpu_dec(*e->pcpu_refcnt); |
| 73 | } |
| 74 | |
| 75 | static void caifd_hold(struct caif_device_entry *e) |
| 76 | { |
| 77 | irqsafe_cpu_inc(*e->pcpu_refcnt); |
| 78 | } |
| 79 | |
| 80 | static int caifd_refcnt_read(struct caif_device_entry *e) |
| 81 | { |
| 82 | int i, refcnt = 0; |
| 83 | for_each_possible_cpu(i) |
| 84 | refcnt += *per_cpu_ptr(e->pcpu_refcnt, i); |
| 85 | return refcnt; |
| 86 | } |
| 87 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 88 | /* Allocate new CAIF device. */ |
| 89 | static struct caif_device_entry *caif_device_alloc(struct net_device *dev) |
| 90 | { |
| 91 | struct caif_device_entry_list *caifdevs; |
| 92 | struct caif_device_entry *caifd; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 93 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 94 | caifdevs = caif_device_list(dev_net(dev)); |
| 95 | BUG_ON(!caifdevs); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 96 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 97 | caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC); |
| 98 | if (!caifd) |
| 99 | return NULL; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 100 | caifd->pcpu_refcnt = alloc_percpu(int); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 101 | caifd->netdev = dev; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 102 | dev_hold(dev); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 103 | return caifd; |
| 104 | } |
| 105 | |
| 106 | static struct caif_device_entry *caif_get(struct net_device *dev) |
| 107 | { |
| 108 | struct caif_device_entry_list *caifdevs = |
| 109 | caif_device_list(dev_net(dev)); |
| 110 | struct caif_device_entry *caifd; |
| 111 | BUG_ON(!caifdevs); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 112 | list_for_each_entry_rcu(caifd, &caifdevs->list, list) { |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 113 | if (caifd->netdev == dev) |
| 114 | return caifd; |
| 115 | } |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 119 | static int transmit(struct cflayer *layer, struct cfpkt *pkt) |
| 120 | { |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 121 | int err; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 122 | struct caif_device_entry *caifd = |
| 123 | container_of(layer, struct caif_device_entry, layer); |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame] | 124 | struct sk_buff *skb; |
| 125 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 126 | skb = cfpkt_tonative(pkt); |
| 127 | skb->dev = caifd->netdev; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 128 | |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 129 | err = dev_queue_xmit(skb); |
| 130 | if (err > 0) |
| 131 | err = -EIO; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 132 | |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 133 | return err; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 136 | /* |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 137 | * Stuff received packets into the CAIF stack. |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 138 | * On error, returns non-zero and releases the skb. |
| 139 | */ |
| 140 | static int receive(struct sk_buff *skb, struct net_device *dev, |
| 141 | struct packet_type *pkttype, struct net_device *orig_dev) |
| 142 | { |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 143 | struct cfpkt *pkt; |
| 144 | struct caif_device_entry *caifd; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 145 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 146 | pkt = cfpkt_fromnative(CAIF_DIR_IN, skb); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 147 | |
| 148 | rcu_read_lock(); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 149 | caifd = caif_get(dev); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 150 | |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 151 | if (!caifd || !caifd->layer.up || !caifd->layer.up->receive || |
| 152 | !netif_oper_up(caifd->netdev)) { |
| 153 | rcu_read_unlock(); |
| 154 | kfree_skb(skb); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 155 | return NET_RX_DROP; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 156 | } |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 157 | |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 158 | /* Hold reference to netdevice while using CAIF stack */ |
| 159 | caifd_hold(caifd); |
| 160 | rcu_read_unlock(); |
| 161 | |
| 162 | caifd->layer.up->receive(caifd->layer.up, pkt); |
| 163 | |
| 164 | /* Release reference to stack upwards */ |
| 165 | caifd_put(caifd); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static struct packet_type caif_packet_type __read_mostly = { |
| 170 | .type = cpu_to_be16(ETH_P_CAIF), |
| 171 | .func = receive, |
| 172 | }; |
| 173 | |
| 174 | static void dev_flowctrl(struct net_device *dev, int on) |
| 175 | { |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 176 | struct caif_device_entry *caifd; |
| 177 | |
| 178 | rcu_read_lock(); |
| 179 | |
| 180 | caifd = caif_get(dev); |
| 181 | if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) { |
| 182 | rcu_read_unlock(); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 183 | return; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | caifd_hold(caifd); |
| 187 | rcu_read_unlock(); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 188 | |
| 189 | caifd->layer.up->ctrlcmd(caifd->layer.up, |
| 190 | on ? |
| 191 | _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND : |
| 192 | _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND, |
| 193 | caifd->layer.id); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 194 | caifd_put(caifd); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* notify Caif of device events */ |
| 198 | static int caif_device_notify(struct notifier_block *me, unsigned long what, |
| 199 | void *arg) |
| 200 | { |
| 201 | struct net_device *dev = arg; |
| 202 | struct caif_device_entry *caifd = NULL; |
| 203 | struct caif_dev_common *caifdev; |
| 204 | enum cfcnfg_phy_preference pref; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 205 | enum cfcnfg_phy_type phy_type; |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 206 | struct cfcnfg *cfg; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 207 | struct caif_device_entry_list *caifdevs = |
| 208 | caif_device_list(dev_net(dev)); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 209 | |
| 210 | if (dev->type != ARPHRD_CAIF) |
| 211 | return 0; |
| 212 | |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 213 | cfg = get_cfcnfg(dev_net(dev)); |
| 214 | if (cfg == NULL) |
| 215 | return 0; |
| 216 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 217 | switch (what) { |
| 218 | case NETDEV_REGISTER: |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 219 | caifd = caif_device_alloc(dev); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 220 | if (!caifd) |
| 221 | return 0; |
| 222 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 223 | caifdev = netdev_priv(dev); |
| 224 | caifdev->flowctrl = dev_flowctrl; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 225 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 226 | caifd->layer.transmit = transmit; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 227 | |
| 228 | if (caifdev->use_frag) |
| 229 | phy_type = CFPHYTYPE_FRAG; |
| 230 | else |
| 231 | phy_type = CFPHYTYPE_CAIF; |
| 232 | |
| 233 | switch (caifdev->link_select) { |
| 234 | case CAIF_LINK_HIGH_BANDW: |
Sjur Braendeland | 2c48520 | 2010-04-28 08:54:40 +0000 | [diff] [blame] | 235 | pref = CFPHYPREF_HIGH_BW; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 236 | break; |
| 237 | case CAIF_LINK_LOW_LATENCY: |
Sjur Braendeland | 2c48520 | 2010-04-28 08:54:40 +0000 | [diff] [blame] | 238 | pref = CFPHYPREF_LOW_LAT; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | pref = CFPHYPREF_HIGH_BW; |
| 242 | break; |
| 243 | } |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 244 | strncpy(caifd->layer.name, dev->name, |
| 245 | sizeof(caifd->layer.name) - 1); |
| 246 | caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0; |
| 247 | |
| 248 | mutex_lock(&caifdevs->lock); |
| 249 | list_add_rcu(&caifd->list, &caifdevs->list); |
| 250 | |
Stephen Hemminger | 73d6ac6 | 2011-04-11 10:43:50 +0000 | [diff] [blame] | 251 | cfcnfg_add_phy_layer(cfg, |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 252 | phy_type, |
| 253 | dev, |
| 254 | &caifd->layer, |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 255 | pref, |
| 256 | caifdev->use_fcs, |
| 257 | caifdev->use_stx); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 258 | mutex_unlock(&caifdevs->lock); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 259 | break; |
| 260 | |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 261 | case NETDEV_UP: |
| 262 | rcu_read_lock(); |
| 263 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 264 | caifd = caif_get(dev); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 265 | if (caifd == NULL) { |
| 266 | rcu_read_unlock(); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 267 | break; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 268 | } |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 269 | |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 270 | cfcnfg_set_phy_state(cfg, &caifd->layer, true); |
| 271 | rcu_read_unlock(); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 272 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 273 | break; |
| 274 | |
| 275 | case NETDEV_DOWN: |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 276 | rcu_read_lock(); |
| 277 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 278 | caifd = caif_get(dev); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 279 | if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) { |
| 280 | rcu_read_unlock(); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
| 284 | cfcnfg_set_phy_state(cfg, &caifd->layer, false); |
| 285 | caifd_hold(caifd); |
| 286 | rcu_read_unlock(); |
| 287 | |
| 288 | caifd->layer.up->ctrlcmd(caifd->layer.up, |
| 289 | _CAIF_CTRLCMD_PHYIF_DOWN_IND, |
| 290 | caifd->layer.id); |
| 291 | caifd_put(caifd); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 292 | break; |
| 293 | |
| 294 | case NETDEV_UNREGISTER: |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 295 | mutex_lock(&caifdevs->lock); |
| 296 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 297 | caifd = caif_get(dev); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 298 | if (caifd == NULL) { |
| 299 | mutex_unlock(&caifdevs->lock); |
André Carvalho de Matos | f2527ec | 2010-11-01 11:52:47 +0000 | [diff] [blame] | 300 | break; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 301 | } |
| 302 | list_del_rcu(&caifd->list); |
| 303 | |
| 304 | /* |
| 305 | * NETDEV_UNREGISTER is called repeatedly until all reference |
| 306 | * counts for the net-device are released. If references to |
| 307 | * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for |
| 308 | * the next call to NETDEV_UNREGISTER. |
| 309 | * |
| 310 | * If any packets are in flight down the CAIF Stack, |
| 311 | * cfcnfg_del_phy_layer will return nonzero. |
| 312 | * If no packets are in flight, the CAIF Stack associated |
| 313 | * with the net-device un-registering is freed. |
| 314 | */ |
| 315 | |
| 316 | if (caifd_refcnt_read(caifd) != 0 || |
| 317 | cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) { |
| 318 | |
| 319 | pr_info("Wait for device inuse\n"); |
| 320 | /* Enrole device if CAIF Stack is still in use */ |
| 321 | list_add_rcu(&caifd->list, &caifdevs->list); |
| 322 | mutex_unlock(&caifdevs->lock); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | synchronize_rcu(); |
| 327 | dev_put(caifd->netdev); |
| 328 | free_percpu(caifd->pcpu_refcnt); |
| 329 | kfree(caifd); |
| 330 | |
| 331 | mutex_unlock(&caifdevs->lock); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static struct notifier_block caif_device_notifier = { |
| 338 | .notifier_call = caif_device_notify, |
| 339 | .priority = 0, |
| 340 | }; |
| 341 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 342 | /* Per-namespace Caif devices handling */ |
| 343 | static int caif_init_net(struct net *net) |
| 344 | { |
| 345 | struct caif_net *caifn = net_generic(net, caif_net_id); |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 346 | BUG_ON(!caifn); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 347 | INIT_LIST_HEAD(&caifn->caifdevs.list); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 348 | mutex_init(&caifn->caifdevs.lock); |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 349 | |
| 350 | caifn->cfg = cfcnfg_create(); |
| 351 | if (!caifn->cfg) { |
| 352 | pr_warn("can't create cfcnfg\n"); |
| 353 | return -ENOMEM; |
| 354 | } |
| 355 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static void caif_exit_net(struct net *net) |
| 360 | { |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 361 | struct caif_device_entry *caifd, *tmp; |
| 362 | struct caif_device_entry_list *caifdevs = |
| 363 | caif_device_list(net); |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 364 | struct cfcnfg *cfg; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 365 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 366 | rtnl_lock(); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 367 | mutex_lock(&caifdevs->lock); |
| 368 | |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 369 | cfg = get_cfcnfg(net); |
| 370 | if (cfg == NULL) { |
| 371 | mutex_unlock(&caifdevs->lock); |
| 372 | return; |
| 373 | } |
| 374 | |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 375 | list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) { |
| 376 | int i = 0; |
| 377 | list_del_rcu(&caifd->list); |
| 378 | cfcnfg_set_phy_state(cfg, &caifd->layer, false); |
| 379 | |
| 380 | while (i < 10 && |
| 381 | (caifd_refcnt_read(caifd) != 0 || |
| 382 | cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) { |
| 383 | |
| 384 | pr_info("Wait for device inuse\n"); |
| 385 | msleep(250); |
| 386 | i++; |
| 387 | } |
| 388 | synchronize_rcu(); |
| 389 | dev_put(caifd->netdev); |
| 390 | free_percpu(caifd->pcpu_refcnt); |
| 391 | kfree(caifd); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 392 | } |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 393 | cfcnfg_remove(cfg); |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 394 | |
| 395 | mutex_unlock(&caifdevs->lock); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 396 | rtnl_unlock(); |
| 397 | } |
| 398 | |
| 399 | static struct pernet_operations caif_net_ops = { |
| 400 | .init = caif_init_net, |
| 401 | .exit = caif_exit_net, |
| 402 | .id = &caif_net_id, |
| 403 | .size = sizeof(struct caif_net), |
| 404 | }; |
| 405 | |
| 406 | /* Initialize Caif devices list */ |
| 407 | static int __init caif_device_init(void) |
| 408 | { |
| 409 | int result; |
sjur.brandeland@stericsson.com | bd30ce4 | 2011-05-13 02:44:00 +0000 | [diff] [blame] | 410 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 411 | result = register_pernet_device(&caif_net_ops); |
| 412 | |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 413 | if (result) |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 414 | return result; |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 415 | |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 416 | register_netdevice_notifier(&caif_device_notifier); |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 417 | dev_add_pack(&caif_packet_type); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 418 | |
| 419 | return result; |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void __exit caif_device_exit(void) |
| 423 | { |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 424 | unregister_pernet_device(&caif_net_ops); |
| 425 | unregister_netdevice_notifier(&caif_device_notifier); |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 426 | dev_remove_pack(&caif_packet_type); |
Sjur Braendeland | c72dfae | 2010-03-30 13:56:25 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | module_init(caif_device_init); |
| 430 | module_exit(caif_device_exit); |