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