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