Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright Gavin Shan, IBM Corporation 2016. |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/netdevice.h> |
| 10 | #include <linux/skbuff.h> |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 11 | |
| 12 | #include <net/ncsi.h> |
| 13 | #include <net/net_namespace.h> |
| 14 | #include <net/sock.h> |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 15 | #include <net/addrconf.h> |
| 16 | #include <net/ipv6.h> |
| 17 | #include <net/if_inet6.h> |
Justin.Lee1@Dell.com | 9771b8c | 2018-10-11 18:07:37 +0000 | [diff] [blame] | 18 | #include <net/genetlink.h> |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 19 | |
| 20 | #include "internal.h" |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 21 | #include "ncsi-pkt.h" |
Samuel Mendoza-Jonas | 955dc68 | 2018-03-05 11:39:05 +1100 | [diff] [blame] | 22 | #include "ncsi-netlink.h" |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 23 | |
| 24 | LIST_HEAD(ncsi_dev_list); |
| 25 | DEFINE_SPINLOCK(ncsi_dev_lock); |
| 26 | |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 27 | bool ncsi_channel_has_link(struct ncsi_channel *channel) |
| 28 | { |
| 29 | return !!(channel->modes[NCSI_MODE_LINK].data[2] & 0x1); |
| 30 | } |
| 31 | |
| 32 | bool ncsi_channel_is_last(struct ncsi_dev_priv *ndp, |
| 33 | struct ncsi_channel *channel) |
| 34 | { |
| 35 | struct ncsi_package *np; |
| 36 | struct ncsi_channel *nc; |
| 37 | |
| 38 | NCSI_FOR_EACH_PACKAGE(ndp, np) |
| 39 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 40 | if (nc == channel) |
| 41 | continue; |
| 42 | if (nc->state == NCSI_CHANNEL_ACTIVE && |
| 43 | ncsi_channel_has_link(nc)) |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 50 | static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down) |
| 51 | { |
| 52 | struct ncsi_dev *nd = &ndp->ndev; |
| 53 | struct ncsi_package *np; |
| 54 | struct ncsi_channel *nc; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 55 | unsigned long flags; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 56 | |
| 57 | nd->state = ncsi_dev_state_functional; |
| 58 | if (force_down) { |
| 59 | nd->link_up = 0; |
| 60 | goto report; |
| 61 | } |
| 62 | |
| 63 | nd->link_up = 0; |
| 64 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 65 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 66 | spin_lock_irqsave(&nc->lock, flags); |
| 67 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 68 | if (!list_empty(&nc->link) || |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 69 | nc->state != NCSI_CHANNEL_ACTIVE) { |
| 70 | spin_unlock_irqrestore(&nc->lock, flags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 71 | continue; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 72 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 73 | |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 74 | if (ncsi_channel_has_link(nc)) { |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 75 | spin_unlock_irqrestore(&nc->lock, flags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 76 | nd->link_up = 1; |
| 77 | goto report; |
| 78 | } |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 79 | |
| 80 | spin_unlock_irqrestore(&nc->lock, flags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | report: |
| 85 | nd->handler(nd); |
| 86 | } |
| 87 | |
Kees Cook | 86cb30e | 2017-10-17 20:21:24 -0700 | [diff] [blame] | 88 | static void ncsi_channel_monitor(struct timer_list *t) |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 89 | { |
Kees Cook | 86cb30e | 2017-10-17 20:21:24 -0700 | [diff] [blame] | 90 | struct ncsi_channel *nc = from_timer(nc, t, monitor.timer); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 91 | struct ncsi_package *np = nc->package; |
| 92 | struct ncsi_dev_priv *ndp = np->ndp; |
Gavin Shan | 52b4c86 | 2017-10-19 13:43:08 +1100 | [diff] [blame] | 93 | struct ncsi_channel_mode *ncm; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 94 | struct ncsi_cmd_arg nca; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 95 | bool enabled, chained; |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 96 | unsigned int monitor_state; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 97 | unsigned long flags; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 98 | int state, ret; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 99 | |
| 100 | spin_lock_irqsave(&nc->lock, flags); |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 101 | state = nc->state; |
| 102 | chained = !list_empty(&nc->link); |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 103 | enabled = nc->monitor.enabled; |
| 104 | monitor_state = nc->monitor.state; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 105 | spin_unlock_irqrestore(&nc->lock, flags); |
| 106 | |
Samuel Mendoza-Jonas | 0795fb2 | 2017-10-19 13:43:06 +1100 | [diff] [blame] | 107 | if (!enabled || chained) { |
| 108 | ncsi_stop_channel_monitor(nc); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 109 | return; |
Samuel Mendoza-Jonas | 0795fb2 | 2017-10-19 13:43:06 +1100 | [diff] [blame] | 110 | } |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 111 | if (state != NCSI_CHANNEL_INACTIVE && |
Samuel Mendoza-Jonas | 0795fb2 | 2017-10-19 13:43:06 +1100 | [diff] [blame] | 112 | state != NCSI_CHANNEL_ACTIVE) { |
| 113 | ncsi_stop_channel_monitor(nc); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 114 | return; |
Samuel Mendoza-Jonas | 0795fb2 | 2017-10-19 13:43:06 +1100 | [diff] [blame] | 115 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 116 | |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 117 | switch (monitor_state) { |
| 118 | case NCSI_CHANNEL_MONITOR_START: |
| 119 | case NCSI_CHANNEL_MONITOR_RETRY: |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 120 | nca.ndp = ndp; |
| 121 | nca.package = np->id; |
| 122 | nca.channel = nc->id; |
| 123 | nca.type = NCSI_PKT_CMD_GLS; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 124 | nca.req_flags = 0; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 125 | ret = ncsi_xmit_cmd(&nca); |
Samuel Mendoza-Jonas | 0795fb2 | 2017-10-19 13:43:06 +1100 | [diff] [blame] | 126 | if (ret) |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 127 | netdev_err(ndp->ndev.dev, "Error %d sending GLS\n", |
| 128 | ret); |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 129 | break; |
| 130 | case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX: |
| 131 | break; |
| 132 | default: |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 133 | netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n", |
| 134 | nc->id); |
Samuel Mendoza-Jonas | 60ab49b | 2018-11-16 15:51:54 +1100 | [diff] [blame] | 135 | ncsi_report_link(ndp, true); |
| 136 | ndp->flags |= NCSI_DEV_RESHUFFLE; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 137 | |
Samuel Mendoza-Jonas | 0795fb2 | 2017-10-19 13:43:06 +1100 | [diff] [blame] | 138 | ncsi_stop_channel_monitor(nc); |
| 139 | |
Gavin Shan | 52b4c86 | 2017-10-19 13:43:08 +1100 | [diff] [blame] | 140 | ncm = &nc->modes[NCSI_MODE_LINK]; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 141 | spin_lock_irqsave(&nc->lock, flags); |
| 142 | nc->state = NCSI_CHANNEL_INVISIBLE; |
Gavin Shan | 52b4c86 | 2017-10-19 13:43:08 +1100 | [diff] [blame] | 143 | ncm->data[2] &= ~0x1; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 144 | spin_unlock_irqrestore(&nc->lock, flags); |
| 145 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 146 | spin_lock_irqsave(&ndp->lock, flags); |
Gavin Shan | 52b4c86 | 2017-10-19 13:43:08 +1100 | [diff] [blame] | 147 | nc->state = NCSI_CHANNEL_ACTIVE; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 148 | list_add_tail_rcu(&nc->link, &ndp->channel_queue); |
| 149 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 150 | ncsi_process_next_channel(ndp); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | spin_lock_irqsave(&nc->lock, flags); |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 155 | nc->monitor.state++; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 156 | spin_unlock_irqrestore(&nc->lock, flags); |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 157 | mod_timer(&nc->monitor.timer, jiffies + HZ); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void ncsi_start_channel_monitor(struct ncsi_channel *nc) |
| 161 | { |
| 162 | unsigned long flags; |
| 163 | |
| 164 | spin_lock_irqsave(&nc->lock, flags); |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 165 | WARN_ON_ONCE(nc->monitor.enabled); |
| 166 | nc->monitor.enabled = true; |
| 167 | nc->monitor.state = NCSI_CHANNEL_MONITOR_START; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 168 | spin_unlock_irqrestore(&nc->lock, flags); |
| 169 | |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 170 | mod_timer(&nc->monitor.timer, jiffies + HZ); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void ncsi_stop_channel_monitor(struct ncsi_channel *nc) |
| 174 | { |
| 175 | unsigned long flags; |
| 176 | |
| 177 | spin_lock_irqsave(&nc->lock, flags); |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 178 | if (!nc->monitor.enabled) { |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 179 | spin_unlock_irqrestore(&nc->lock, flags); |
| 180 | return; |
| 181 | } |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 182 | nc->monitor.enabled = false; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 183 | spin_unlock_irqrestore(&nc->lock, flags); |
| 184 | |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 185 | del_timer_sync(&nc->monitor.timer); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 186 | } |
| 187 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 188 | struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np, |
| 189 | unsigned char id) |
| 190 | { |
| 191 | struct ncsi_channel *nc; |
| 192 | |
| 193 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 194 | if (nc->id == id) |
| 195 | return nc; |
| 196 | } |
| 197 | |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id) |
| 202 | { |
| 203 | struct ncsi_channel *nc, *tmp; |
| 204 | int index; |
| 205 | unsigned long flags; |
| 206 | |
| 207 | nc = kzalloc(sizeof(*nc), GFP_ATOMIC); |
| 208 | if (!nc) |
| 209 | return NULL; |
| 210 | |
| 211 | nc->id = id; |
| 212 | nc->package = np; |
| 213 | nc->state = NCSI_CHANNEL_INACTIVE; |
Gavin Shan | 83afdc6 | 2016-10-04 11:25:52 +1100 | [diff] [blame] | 214 | nc->monitor.enabled = false; |
Kees Cook | 86cb30e | 2017-10-17 20:21:24 -0700 | [diff] [blame] | 215 | timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 216 | spin_lock_init(&nc->lock); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 217 | INIT_LIST_HEAD(&nc->link); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 218 | for (index = 0; index < NCSI_CAP_MAX; index++) |
| 219 | nc->caps[index].index = index; |
| 220 | for (index = 0; index < NCSI_MODE_MAX; index++) |
| 221 | nc->modes[index].index = index; |
| 222 | |
| 223 | spin_lock_irqsave(&np->lock, flags); |
| 224 | tmp = ncsi_find_channel(np, id); |
| 225 | if (tmp) { |
| 226 | spin_unlock_irqrestore(&np->lock, flags); |
| 227 | kfree(nc); |
| 228 | return tmp; |
| 229 | } |
| 230 | |
| 231 | list_add_tail_rcu(&nc->node, &np->channels); |
| 232 | np->channel_num++; |
| 233 | spin_unlock_irqrestore(&np->lock, flags); |
| 234 | |
| 235 | return nc; |
| 236 | } |
| 237 | |
| 238 | static void ncsi_remove_channel(struct ncsi_channel *nc) |
| 239 | { |
| 240 | struct ncsi_package *np = nc->package; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 241 | unsigned long flags; |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 242 | |
| 243 | spin_lock_irqsave(&nc->lock, flags); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 244 | |
| 245 | /* Release filters */ |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 246 | kfree(nc->mac_filter.addrs); |
| 247 | kfree(nc->vlan_filter.vids); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 248 | |
| 249 | nc->state = NCSI_CHANNEL_INACTIVE; |
| 250 | spin_unlock_irqrestore(&nc->lock, flags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 251 | ncsi_stop_channel_monitor(nc); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 252 | |
| 253 | /* Remove and free channel */ |
| 254 | spin_lock_irqsave(&np->lock, flags); |
| 255 | list_del_rcu(&nc->node); |
| 256 | np->channel_num--; |
| 257 | spin_unlock_irqrestore(&np->lock, flags); |
| 258 | |
| 259 | kfree(nc); |
| 260 | } |
| 261 | |
| 262 | struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp, |
| 263 | unsigned char id) |
| 264 | { |
| 265 | struct ncsi_package *np; |
| 266 | |
| 267 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 268 | if (np->id == id) |
| 269 | return np; |
| 270 | } |
| 271 | |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp, |
| 276 | unsigned char id) |
| 277 | { |
| 278 | struct ncsi_package *np, *tmp; |
| 279 | unsigned long flags; |
| 280 | |
| 281 | np = kzalloc(sizeof(*np), GFP_ATOMIC); |
| 282 | if (!np) |
| 283 | return NULL; |
| 284 | |
| 285 | np->id = id; |
| 286 | np->ndp = ndp; |
| 287 | spin_lock_init(&np->lock); |
| 288 | INIT_LIST_HEAD(&np->channels); |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 289 | np->channel_whitelist = UINT_MAX; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 290 | |
| 291 | spin_lock_irqsave(&ndp->lock, flags); |
| 292 | tmp = ncsi_find_package(ndp, id); |
| 293 | if (tmp) { |
| 294 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 295 | kfree(np); |
| 296 | return tmp; |
| 297 | } |
| 298 | |
| 299 | list_add_tail_rcu(&np->node, &ndp->packages); |
| 300 | ndp->package_num++; |
| 301 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 302 | |
| 303 | return np; |
| 304 | } |
| 305 | |
| 306 | void ncsi_remove_package(struct ncsi_package *np) |
| 307 | { |
| 308 | struct ncsi_dev_priv *ndp = np->ndp; |
| 309 | struct ncsi_channel *nc, *tmp; |
| 310 | unsigned long flags; |
| 311 | |
| 312 | /* Release all child channels */ |
| 313 | list_for_each_entry_safe(nc, tmp, &np->channels, node) |
| 314 | ncsi_remove_channel(nc); |
| 315 | |
| 316 | /* Remove and free package */ |
| 317 | spin_lock_irqsave(&ndp->lock, flags); |
| 318 | list_del_rcu(&np->node); |
| 319 | ndp->package_num--; |
| 320 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 321 | |
| 322 | kfree(np); |
| 323 | } |
| 324 | |
| 325 | void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp, |
| 326 | unsigned char id, |
| 327 | struct ncsi_package **np, |
| 328 | struct ncsi_channel **nc) |
| 329 | { |
| 330 | struct ncsi_package *p; |
| 331 | struct ncsi_channel *c; |
| 332 | |
| 333 | p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id)); |
| 334 | c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL; |
| 335 | |
| 336 | if (np) |
| 337 | *np = p; |
| 338 | if (nc) |
| 339 | *nc = c; |
| 340 | } |
| 341 | |
| 342 | /* For two consecutive NCSI commands, the packet IDs shouldn't |
| 343 | * be same. Otherwise, the bogus response might be replied. So |
| 344 | * the available IDs are allocated in round-robin fashion. |
| 345 | */ |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 346 | struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp, |
| 347 | unsigned int req_flags) |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 348 | { |
| 349 | struct ncsi_request *nr = NULL; |
| 350 | int i, limit = ARRAY_SIZE(ndp->requests); |
| 351 | unsigned long flags; |
| 352 | |
| 353 | /* Check if there is one available request until the ceiling */ |
| 354 | spin_lock_irqsave(&ndp->lock, flags); |
Gavin Shan | a15af54 | 2016-10-04 11:25:50 +1100 | [diff] [blame] | 355 | for (i = ndp->request_id; i < limit; i++) { |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 356 | if (ndp->requests[i].used) |
| 357 | continue; |
| 358 | |
| 359 | nr = &ndp->requests[i]; |
| 360 | nr->used = true; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 361 | nr->flags = req_flags; |
Gavin Shan | a15af54 | 2016-10-04 11:25:50 +1100 | [diff] [blame] | 362 | ndp->request_id = i + 1; |
| 363 | goto found; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* Fail back to check from the starting cursor */ |
Gavin Shan | a15af54 | 2016-10-04 11:25:50 +1100 | [diff] [blame] | 367 | for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) { |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 368 | if (ndp->requests[i].used) |
| 369 | continue; |
| 370 | |
| 371 | nr = &ndp->requests[i]; |
| 372 | nr->used = true; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 373 | nr->flags = req_flags; |
Gavin Shan | a15af54 | 2016-10-04 11:25:50 +1100 | [diff] [blame] | 374 | ndp->request_id = i + 1; |
| 375 | goto found; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 376 | } |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 377 | |
Gavin Shan | a15af54 | 2016-10-04 11:25:50 +1100 | [diff] [blame] | 378 | found: |
| 379 | spin_unlock_irqrestore(&ndp->lock, flags); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 380 | return nr; |
| 381 | } |
| 382 | |
| 383 | void ncsi_free_request(struct ncsi_request *nr) |
| 384 | { |
| 385 | struct ncsi_dev_priv *ndp = nr->ndp; |
| 386 | struct sk_buff *cmd, *rsp; |
| 387 | unsigned long flags; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 388 | bool driven; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 389 | |
| 390 | if (nr->enabled) { |
| 391 | nr->enabled = false; |
| 392 | del_timer_sync(&nr->timer); |
| 393 | } |
| 394 | |
| 395 | spin_lock_irqsave(&ndp->lock, flags); |
| 396 | cmd = nr->cmd; |
| 397 | rsp = nr->rsp; |
| 398 | nr->cmd = NULL; |
| 399 | nr->rsp = NULL; |
| 400 | nr->used = false; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 401 | driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 402 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 403 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 404 | if (driven && cmd && --ndp->pending_req_num == 0) |
| 405 | schedule_work(&ndp->work); |
| 406 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 407 | /* Release command and response */ |
| 408 | consume_skb(cmd); |
| 409 | consume_skb(rsp); |
| 410 | } |
| 411 | |
| 412 | struct ncsi_dev *ncsi_find_dev(struct net_device *dev) |
| 413 | { |
| 414 | struct ncsi_dev_priv *ndp; |
| 415 | |
| 416 | NCSI_FOR_EACH_DEV(ndp) { |
| 417 | if (ndp->ndev.dev == dev) |
| 418 | return &ndp->ndev; |
| 419 | } |
| 420 | |
| 421 | return NULL; |
| 422 | } |
| 423 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 424 | static void ncsi_request_timeout(struct timer_list *t) |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 425 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 426 | struct ncsi_request *nr = from_timer(nr, t, timer); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 427 | struct ncsi_dev_priv *ndp = nr->ndp; |
Justin.Lee1@Dell.com | 9771b8c | 2018-10-11 18:07:37 +0000 | [diff] [blame] | 428 | struct ncsi_cmd_pkt *cmd; |
| 429 | struct ncsi_package *np; |
| 430 | struct ncsi_channel *nc; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 431 | unsigned long flags; |
| 432 | |
| 433 | /* If the request already had associated response, |
| 434 | * let the response handler to release it. |
| 435 | */ |
| 436 | spin_lock_irqsave(&ndp->lock, flags); |
| 437 | nr->enabled = false; |
| 438 | if (nr->rsp || !nr->cmd) { |
| 439 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 440 | return; |
| 441 | } |
| 442 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 443 | |
Justin.Lee1@Dell.com | 9771b8c | 2018-10-11 18:07:37 +0000 | [diff] [blame] | 444 | if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) { |
| 445 | if (nr->cmd) { |
| 446 | /* Find the package */ |
| 447 | cmd = (struct ncsi_cmd_pkt *) |
| 448 | skb_network_header(nr->cmd); |
| 449 | ncsi_find_package_and_channel(ndp, |
| 450 | cmd->cmd.common.channel, |
| 451 | &np, &nc); |
| 452 | ncsi_send_netlink_timeout(nr, np, nc); |
| 453 | } |
| 454 | } |
| 455 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 456 | /* Release the request */ |
| 457 | ncsi_free_request(nr); |
| 458 | } |
| 459 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 460 | static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp) |
| 461 | { |
| 462 | struct ncsi_dev *nd = &ndp->ndev; |
Samuel Mendoza-Jonas | cd09ab0 | 2018-11-16 15:51:56 +1100 | [diff] [blame] | 463 | struct ncsi_package *np; |
| 464 | struct ncsi_channel *nc, *tmp; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 465 | struct ncsi_cmd_arg nca; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 466 | unsigned long flags; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 467 | int ret; |
| 468 | |
Samuel Mendoza-Jonas | cd09ab0 | 2018-11-16 15:51:56 +1100 | [diff] [blame] | 469 | np = ndp->active_package; |
| 470 | nc = ndp->active_channel; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 471 | nca.ndp = ndp; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 472 | nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 473 | switch (nd->state) { |
| 474 | case ncsi_dev_state_suspend: |
| 475 | nd->state = ncsi_dev_state_suspend_select; |
| 476 | /* Fall through */ |
| 477 | case ncsi_dev_state_suspend_select: |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 478 | ndp->pending_req_num = 1; |
| 479 | |
| 480 | nca.type = NCSI_PKT_CMD_SP; |
| 481 | nca.package = np->id; |
| 482 | nca.channel = NCSI_RESERVED_CHANNEL; |
| 483 | if (ndp->flags & NCSI_DEV_HWA) |
| 484 | nca.bytes[0] = 0; |
| 485 | else |
| 486 | nca.bytes[0] = 1; |
| 487 | |
Gavin Shan | 008a424 | 2016-10-20 11:45:50 +1100 | [diff] [blame] | 488 | /* To retrieve the last link states of channels in current |
| 489 | * package when current active channel needs fail over to |
| 490 | * another one. It means we will possibly select another |
| 491 | * channel as next active one. The link states of channels |
| 492 | * are most important factor of the selection. So we need |
| 493 | * accurate link states. Unfortunately, the link states on |
| 494 | * inactive channels can't be updated with LSC AEN in time. |
| 495 | */ |
| 496 | if (ndp->flags & NCSI_DEV_RESHUFFLE) |
| 497 | nd->state = ncsi_dev_state_suspend_gls; |
| 498 | else |
| 499 | nd->state = ncsi_dev_state_suspend_dcnt; |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 500 | ret = ncsi_xmit_cmd(&nca); |
| 501 | if (ret) |
| 502 | goto error; |
| 503 | |
| 504 | break; |
Gavin Shan | 008a424 | 2016-10-20 11:45:50 +1100 | [diff] [blame] | 505 | case ncsi_dev_state_suspend_gls: |
| 506 | ndp->pending_req_num = np->channel_num; |
| 507 | |
| 508 | nca.type = NCSI_PKT_CMD_GLS; |
| 509 | nca.package = np->id; |
| 510 | |
| 511 | nd->state = ncsi_dev_state_suspend_dcnt; |
| 512 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 513 | nca.channel = nc->id; |
| 514 | ret = ncsi_xmit_cmd(&nca); |
| 515 | if (ret) |
| 516 | goto error; |
| 517 | } |
| 518 | |
| 519 | break; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 520 | case ncsi_dev_state_suspend_dcnt: |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 521 | ndp->pending_req_num = 1; |
| 522 | |
| 523 | nca.type = NCSI_PKT_CMD_DCNT; |
| 524 | nca.package = np->id; |
| 525 | nca.channel = nc->id; |
| 526 | |
| 527 | nd->state = ncsi_dev_state_suspend_dc; |
| 528 | ret = ncsi_xmit_cmd(&nca); |
| 529 | if (ret) |
| 530 | goto error; |
| 531 | |
| 532 | break; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 533 | case ncsi_dev_state_suspend_dc: |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 534 | ndp->pending_req_num = 1; |
| 535 | |
| 536 | nca.type = NCSI_PKT_CMD_DC; |
| 537 | nca.package = np->id; |
| 538 | nca.channel = nc->id; |
| 539 | nca.bytes[0] = 1; |
| 540 | |
| 541 | nd->state = ncsi_dev_state_suspend_deselect; |
| 542 | ret = ncsi_xmit_cmd(&nca); |
| 543 | if (ret) |
| 544 | goto error; |
| 545 | |
Samuel Mendoza-Jonas | cd09ab0 | 2018-11-16 15:51:56 +1100 | [diff] [blame] | 546 | NCSI_FOR_EACH_CHANNEL(np, tmp) { |
| 547 | /* If there is another channel active on this package |
| 548 | * do not deselect the package. |
| 549 | */ |
| 550 | if (tmp != nc && tmp->state == NCSI_CHANNEL_ACTIVE) { |
| 551 | nd->state = ncsi_dev_state_suspend_done; |
| 552 | break; |
| 553 | } |
| 554 | } |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 555 | break; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 556 | case ncsi_dev_state_suspend_deselect: |
| 557 | ndp->pending_req_num = 1; |
| 558 | |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 559 | nca.type = NCSI_PKT_CMD_DP; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 560 | nca.package = np->id; |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 561 | nca.channel = NCSI_RESERVED_CHANNEL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 562 | |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 563 | nd->state = ncsi_dev_state_suspend_done; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 564 | ret = ncsi_xmit_cmd(&nca); |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 565 | if (ret) |
| 566 | goto error; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 567 | |
| 568 | break; |
| 569 | case ncsi_dev_state_suspend_done: |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 570 | spin_lock_irqsave(&nc->lock, flags); |
| 571 | nc->state = NCSI_CHANNEL_INACTIVE; |
| 572 | spin_unlock_irqrestore(&nc->lock, flags); |
Samuel Mendoza-Jonas | 2878a2c | 2018-11-16 15:51:58 +1100 | [diff] [blame] | 573 | if (ndp->flags & NCSI_DEV_RESET) |
| 574 | ncsi_reset_dev(nd); |
| 575 | else |
| 576 | ncsi_process_next_channel(ndp); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 577 | break; |
| 578 | default: |
| 579 | netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n", |
| 580 | nd->state); |
| 581 | } |
Gavin Shan | 7ba5c00 | 2016-10-20 11:45:49 +1100 | [diff] [blame] | 582 | |
| 583 | return; |
| 584 | error: |
| 585 | nd->state = ncsi_dev_state_functional; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 586 | } |
| 587 | |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 588 | /* Check the VLAN filter bitmap for a set filter, and construct a |
| 589 | * "Set VLAN Filter - Disable" packet if found. |
| 590 | */ |
| 591 | static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc, |
| 592 | struct ncsi_cmd_arg *nca) |
| 593 | { |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 594 | struct ncsi_channel_vlan_filter *ncf; |
| 595 | unsigned long flags; |
| 596 | void *bitmap; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 597 | int index; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 598 | u16 vid; |
| 599 | |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 600 | ncf = &nc->vlan_filter; |
| 601 | bitmap = &ncf->bitmap; |
| 602 | |
| 603 | spin_lock_irqsave(&nc->lock, flags); |
| 604 | index = find_next_bit(bitmap, ncf->n_vids, 0); |
| 605 | if (index >= ncf->n_vids) { |
| 606 | spin_unlock_irqrestore(&nc->lock, flags); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 607 | return -1; |
| 608 | } |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 609 | vid = ncf->vids[index]; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 610 | |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 611 | clear_bit(index, bitmap); |
| 612 | ncf->vids[index] = 0; |
| 613 | spin_unlock_irqrestore(&nc->lock, flags); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 614 | |
| 615 | nca->type = NCSI_PKT_CMD_SVF; |
| 616 | nca->words[1] = vid; |
| 617 | /* HW filter index starts at 1 */ |
| 618 | nca->bytes[6] = index + 1; |
| 619 | nca->bytes[7] = 0x00; |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable" |
| 624 | * packet. |
| 625 | */ |
| 626 | static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc, |
| 627 | struct ncsi_cmd_arg *nca) |
| 628 | { |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 629 | struct ncsi_channel_vlan_filter *ncf; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 630 | struct vlan_vid *vlan = NULL; |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 631 | unsigned long flags; |
| 632 | int i, index; |
| 633 | void *bitmap; |
| 634 | u16 vid; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 635 | |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 636 | if (list_empty(&ndp->vlan_vids)) |
| 637 | return -1; |
| 638 | |
| 639 | ncf = &nc->vlan_filter; |
| 640 | bitmap = &ncf->bitmap; |
| 641 | |
| 642 | spin_lock_irqsave(&nc->lock, flags); |
| 643 | |
| 644 | rcu_read_lock(); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 645 | list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) { |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 646 | vid = vlan->vid; |
| 647 | for (i = 0; i < ncf->n_vids; i++) |
| 648 | if (ncf->vids[i] == vid) { |
| 649 | vid = 0; |
| 650 | break; |
| 651 | } |
| 652 | if (vid) |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 653 | break; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 654 | } |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 655 | rcu_read_unlock(); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 656 | |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 657 | if (!vid) { |
| 658 | /* No VLAN ID is not set */ |
| 659 | spin_unlock_irqrestore(&nc->lock, flags); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 663 | index = find_next_zero_bit(bitmap, ncf->n_vids, 0); |
| 664 | if (index < 0 || index >= ncf->n_vids) { |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 665 | netdev_err(ndp->ndev.dev, |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 666 | "Channel %u already has all VLAN filters set\n", |
| 667 | nc->id); |
| 668 | spin_unlock_irqrestore(&nc->lock, flags); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 669 | return -1; |
| 670 | } |
| 671 | |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 672 | ncf->vids[index] = vid; |
| 673 | set_bit(index, bitmap); |
| 674 | spin_unlock_irqrestore(&nc->lock, flags); |
| 675 | |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 676 | nca->type = NCSI_PKT_CMD_SVF; |
Samuel Mendoza-Jonas | 062b3e1 | 2018-04-17 14:23:23 +1000 | [diff] [blame] | 677 | nca->words[1] = vid; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 678 | /* HW filter index starts at 1 */ |
| 679 | nca->bytes[6] = index + 1; |
| 680 | nca->bytes[7] = 0x01; |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Vijay Khemka | cb10c7c | 2018-10-16 12:13:19 -0700 | [diff] [blame] | 685 | #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC) |
| 686 | |
| 687 | /* NCSI OEM Command APIs */ |
| 688 | static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca) |
| 689 | { |
| 690 | unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN]; |
| 691 | int ret = 0; |
| 692 | |
| 693 | nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN; |
| 694 | |
| 695 | memset(data, 0, NCSI_OEM_BCM_CMD_GMA_LEN); |
| 696 | *(unsigned int *)data = ntohl(NCSI_OEM_MFR_BCM_ID); |
| 697 | data[5] = NCSI_OEM_BCM_CMD_GMA; |
| 698 | |
| 699 | nca->data = data; |
| 700 | |
| 701 | ret = ncsi_xmit_cmd(nca); |
| 702 | if (ret) |
| 703 | netdev_err(nca->ndp->ndev.dev, |
| 704 | "NCSI: Failed to transmit cmd 0x%x during configure\n", |
| 705 | nca->type); |
| 706 | return ret; |
| 707 | } |
| 708 | |
Vijay Khemka | 16e8c4c | 2018-11-26 13:49:04 -0800 | [diff] [blame] | 709 | static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg *nca) |
| 710 | { |
| 711 | union { |
| 712 | u8 data_u8[NCSI_OEM_MLX_CMD_GMA_LEN]; |
| 713 | u32 data_u32[NCSI_OEM_MLX_CMD_GMA_LEN / sizeof(u32)]; |
| 714 | } u; |
| 715 | int ret = 0; |
| 716 | |
| 717 | nca->payload = NCSI_OEM_MLX_CMD_GMA_LEN; |
| 718 | |
| 719 | memset(&u, 0, sizeof(u)); |
| 720 | u.data_u32[0] = ntohl(NCSI_OEM_MFR_MLX_ID); |
| 721 | u.data_u8[5] = NCSI_OEM_MLX_CMD_GMA; |
| 722 | u.data_u8[6] = NCSI_OEM_MLX_CMD_GMA_PARAM; |
| 723 | |
| 724 | nca->data = u.data_u8; |
| 725 | |
| 726 | ret = ncsi_xmit_cmd(nca); |
| 727 | if (ret) |
| 728 | netdev_err(nca->ndp->ndev.dev, |
| 729 | "NCSI: Failed to transmit cmd 0x%x during configure\n", |
| 730 | nca->type); |
| 731 | return ret; |
| 732 | } |
| 733 | |
Vijay Khemka | cb10c7c | 2018-10-16 12:13:19 -0700 | [diff] [blame] | 734 | /* OEM Command handlers initialization */ |
| 735 | static struct ncsi_oem_gma_handler { |
| 736 | unsigned int mfr_id; |
| 737 | int (*handler)(struct ncsi_cmd_arg *nca); |
| 738 | } ncsi_oem_gma_handlers[] = { |
Vijay Khemka | 16e8c4c | 2018-11-26 13:49:04 -0800 | [diff] [blame] | 739 | { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm }, |
| 740 | { NCSI_OEM_MFR_MLX_ID, ncsi_oem_gma_handler_mlx } |
Vijay Khemka | cb10c7c | 2018-10-16 12:13:19 -0700 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id) |
| 744 | { |
| 745 | struct ncsi_oem_gma_handler *nch = NULL; |
| 746 | int i; |
| 747 | |
| 748 | /* This function should only be called once, return if flag set */ |
| 749 | if (nca->ndp->gma_flag == 1) |
| 750 | return -1; |
| 751 | |
| 752 | /* Find gma handler for given manufacturer id */ |
| 753 | for (i = 0; i < ARRAY_SIZE(ncsi_oem_gma_handlers); i++) { |
| 754 | if (ncsi_oem_gma_handlers[i].mfr_id == mf_id) { |
| 755 | if (ncsi_oem_gma_handlers[i].handler) |
| 756 | nch = &ncsi_oem_gma_handlers[i]; |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (!nch) { |
| 762 | netdev_err(nca->ndp->ndev.dev, |
| 763 | "NCSI: No GMA handler available for MFR-ID (0x%x)\n", |
| 764 | mf_id); |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | /* Set the flag for GMA command which should only be called once */ |
| 769 | nca->ndp->gma_flag = 1; |
| 770 | |
| 771 | /* Get Mac address from NCSI device */ |
| 772 | return nch->handler(nca); |
| 773 | } |
| 774 | |
| 775 | #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */ |
| 776 | |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 777 | /* Determine if a given channel from the channel_queue should be used for Tx */ |
| 778 | static bool ncsi_channel_is_tx(struct ncsi_dev_priv *ndp, |
| 779 | struct ncsi_channel *nc) |
| 780 | { |
| 781 | struct ncsi_channel_mode *ncm; |
| 782 | struct ncsi_channel *channel; |
| 783 | struct ncsi_package *np; |
| 784 | |
| 785 | /* Check if any other channel has Tx enabled; a channel may have already |
| 786 | * been configured and removed from the channel queue. |
| 787 | */ |
| 788 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 789 | if (!ndp->multi_package && np != nc->package) |
| 790 | continue; |
| 791 | NCSI_FOR_EACH_CHANNEL(np, channel) { |
| 792 | ncm = &channel->modes[NCSI_MODE_TX_ENABLE]; |
| 793 | if (ncm->enable) |
| 794 | return false; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* This channel is the preferred channel and has link */ |
| 799 | list_for_each_entry_rcu(channel, &ndp->channel_queue, link) { |
| 800 | np = channel->package; |
| 801 | if (np->preferred_channel && |
| 802 | ncsi_channel_has_link(np->preferred_channel)) { |
| 803 | return np->preferred_channel == nc; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* This channel has link */ |
| 808 | if (ncsi_channel_has_link(nc)) |
| 809 | return true; |
| 810 | |
| 811 | list_for_each_entry_rcu(channel, &ndp->channel_queue, link) |
| 812 | if (ncsi_channel_has_link(channel)) |
| 813 | return false; |
| 814 | |
| 815 | /* No other channel has link; default to this one */ |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | /* Change the active Tx channel in a multi-channel setup */ |
| 820 | int ncsi_update_tx_channel(struct ncsi_dev_priv *ndp, |
| 821 | struct ncsi_package *package, |
| 822 | struct ncsi_channel *disable, |
| 823 | struct ncsi_channel *enable) |
| 824 | { |
| 825 | struct ncsi_cmd_arg nca; |
| 826 | struct ncsi_channel *nc; |
| 827 | struct ncsi_package *np; |
| 828 | int ret = 0; |
| 829 | |
| 830 | if (!package->multi_channel && !ndp->multi_package) |
| 831 | netdev_warn(ndp->ndev.dev, |
| 832 | "NCSI: Trying to update Tx channel in single-channel mode\n"); |
| 833 | nca.ndp = ndp; |
| 834 | nca.req_flags = 0; |
| 835 | |
| 836 | /* Find current channel with Tx enabled */ |
| 837 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 838 | if (disable) |
| 839 | break; |
| 840 | if (!ndp->multi_package && np != package) |
| 841 | continue; |
| 842 | |
| 843 | NCSI_FOR_EACH_CHANNEL(np, nc) |
| 844 | if (nc->modes[NCSI_MODE_TX_ENABLE].enable) { |
| 845 | disable = nc; |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* Find a suitable channel for Tx */ |
| 851 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 852 | if (enable) |
| 853 | break; |
| 854 | if (!ndp->multi_package && np != package) |
| 855 | continue; |
| 856 | if (!(ndp->package_whitelist & (0x1 << np->id))) |
| 857 | continue; |
| 858 | |
| 859 | if (np->preferred_channel && |
| 860 | ncsi_channel_has_link(np->preferred_channel)) { |
| 861 | enable = np->preferred_channel; |
| 862 | break; |
| 863 | } |
| 864 | |
| 865 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 866 | if (!(np->channel_whitelist & 0x1 << nc->id)) |
| 867 | continue; |
| 868 | if (nc->state != NCSI_CHANNEL_ACTIVE) |
| 869 | continue; |
| 870 | if (ncsi_channel_has_link(nc)) { |
| 871 | enable = nc; |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if (disable == enable) |
| 878 | return -1; |
| 879 | |
| 880 | if (!enable) |
| 881 | return -1; |
| 882 | |
| 883 | if (disable) { |
| 884 | nca.channel = disable->id; |
| 885 | nca.package = disable->package->id; |
| 886 | nca.type = NCSI_PKT_CMD_DCNT; |
| 887 | ret = ncsi_xmit_cmd(&nca); |
| 888 | if (ret) |
| 889 | netdev_err(ndp->ndev.dev, |
| 890 | "Error %d sending DCNT\n", |
| 891 | ret); |
| 892 | } |
| 893 | |
| 894 | netdev_info(ndp->ndev.dev, "NCSI: channel %u enables Tx\n", enable->id); |
| 895 | |
| 896 | nca.channel = enable->id; |
| 897 | nca.package = enable->package->id; |
| 898 | nca.type = NCSI_PKT_CMD_ECNT; |
| 899 | ret = ncsi_xmit_cmd(&nca); |
| 900 | if (ret) |
| 901 | netdev_err(ndp->ndev.dev, |
| 902 | "Error %d sending ECNT\n", |
| 903 | ret); |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 908 | static void ncsi_configure_channel(struct ncsi_dev_priv *ndp) |
| 909 | { |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 910 | struct ncsi_package *np = ndp->active_package; |
| 911 | struct ncsi_channel *nc = ndp->active_channel; |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 912 | struct ncsi_channel *hot_nc = NULL; |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 913 | struct ncsi_dev *nd = &ndp->ndev; |
| 914 | struct net_device *dev = nd->dev; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 915 | struct ncsi_cmd_arg nca; |
| 916 | unsigned char index; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 917 | unsigned long flags; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 918 | int ret; |
| 919 | |
| 920 | nca.ndp = ndp; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 921 | nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 922 | switch (nd->state) { |
| 923 | case ncsi_dev_state_config: |
| 924 | case ncsi_dev_state_config_sp: |
| 925 | ndp->pending_req_num = 1; |
| 926 | |
| 927 | /* Select the specific package */ |
| 928 | nca.type = NCSI_PKT_CMD_SP; |
| 929 | if (ndp->flags & NCSI_DEV_HWA) |
| 930 | nca.bytes[0] = 0; |
| 931 | else |
| 932 | nca.bytes[0] = 1; |
| 933 | nca.package = np->id; |
Gavin Shan | bc7e0f5 | 2016-10-04 11:25:48 +1100 | [diff] [blame] | 934 | nca.channel = NCSI_RESERVED_CHANNEL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 935 | ret = ncsi_xmit_cmd(&nca); |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 936 | if (ret) { |
| 937 | netdev_err(ndp->ndev.dev, |
| 938 | "NCSI: Failed to transmit CMD_SP\n"); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 939 | goto error; |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 940 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 941 | |
| 942 | nd->state = ncsi_dev_state_config_cis; |
| 943 | break; |
| 944 | case ncsi_dev_state_config_cis: |
| 945 | ndp->pending_req_num = 1; |
| 946 | |
| 947 | /* Clear initial state */ |
| 948 | nca.type = NCSI_PKT_CMD_CIS; |
| 949 | nca.package = np->id; |
| 950 | nca.channel = nc->id; |
| 951 | ret = ncsi_xmit_cmd(&nca); |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 952 | if (ret) { |
| 953 | netdev_err(ndp->ndev.dev, |
| 954 | "NCSI: Failed to transmit CMD_CIS\n"); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 955 | goto error; |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 956 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 957 | |
Vijay Khemka | cb10c7c | 2018-10-16 12:13:19 -0700 | [diff] [blame] | 958 | nd->state = ncsi_dev_state_config_oem_gma; |
| 959 | break; |
| 960 | case ncsi_dev_state_config_oem_gma: |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 961 | nd->state = ncsi_dev_state_config_clear_vids; |
Vijay Khemka | cb10c7c | 2018-10-16 12:13:19 -0700 | [diff] [blame] | 962 | ret = -1; |
| 963 | |
| 964 | #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC) |
| 965 | nca.type = NCSI_PKT_CMD_OEM; |
| 966 | nca.package = np->id; |
| 967 | nca.channel = nc->id; |
| 968 | ndp->pending_req_num = 1; |
| 969 | ret = ncsi_gma_handler(&nca, nc->version.mf_id); |
| 970 | #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */ |
| 971 | |
| 972 | if (ret < 0) |
| 973 | schedule_work(&ndp->work); |
| 974 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 975 | break; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 976 | case ncsi_dev_state_config_clear_vids: |
| 977 | case ncsi_dev_state_config_svf: |
| 978 | case ncsi_dev_state_config_ev: |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 979 | case ncsi_dev_state_config_sma: |
| 980 | case ncsi_dev_state_config_ebf: |
| 981 | #if IS_ENABLED(CONFIG_IPV6) |
| 982 | case ncsi_dev_state_config_egmf: |
| 983 | #endif |
| 984 | case ncsi_dev_state_config_ecnt: |
| 985 | case ncsi_dev_state_config_ec: |
| 986 | case ncsi_dev_state_config_ae: |
| 987 | case ncsi_dev_state_config_gls: |
| 988 | ndp->pending_req_num = 1; |
| 989 | |
| 990 | nca.package = np->id; |
| 991 | nca.channel = nc->id; |
| 992 | |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 993 | /* Clear any active filters on the channel before setting */ |
| 994 | if (nd->state == ncsi_dev_state_config_clear_vids) { |
| 995 | ret = clear_one_vid(ndp, nc, &nca); |
| 996 | if (ret) { |
| 997 | nd->state = ncsi_dev_state_config_svf; |
| 998 | schedule_work(&ndp->work); |
| 999 | break; |
| 1000 | } |
| 1001 | /* Repeat */ |
| 1002 | nd->state = ncsi_dev_state_config_clear_vids; |
| 1003 | /* Add known VLAN tags to the filter */ |
| 1004 | } else if (nd->state == ncsi_dev_state_config_svf) { |
| 1005 | ret = set_one_vid(ndp, nc, &nca); |
| 1006 | if (ret) { |
| 1007 | nd->state = ncsi_dev_state_config_ev; |
| 1008 | schedule_work(&ndp->work); |
| 1009 | break; |
| 1010 | } |
| 1011 | /* Repeat */ |
| 1012 | nd->state = ncsi_dev_state_config_svf; |
| 1013 | /* Enable/Disable the VLAN filter */ |
| 1014 | } else if (nd->state == ncsi_dev_state_config_ev) { |
| 1015 | if (list_empty(&ndp->vlan_vids)) { |
| 1016 | nca.type = NCSI_PKT_CMD_DV; |
| 1017 | } else { |
| 1018 | nca.type = NCSI_PKT_CMD_EV; |
| 1019 | nca.bytes[3] = NCSI_CAP_VLAN_NO; |
| 1020 | } |
| 1021 | nd->state = ncsi_dev_state_config_sma; |
| 1022 | } else if (nd->state == ncsi_dev_state_config_sma) { |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1023 | /* Use first entry in unicast filter table. Note that |
| 1024 | * the MAC filter table starts from entry 1 instead of |
| 1025 | * 0. |
| 1026 | */ |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1027 | nca.type = NCSI_PKT_CMD_SMA; |
| 1028 | for (index = 0; index < 6; index++) |
| 1029 | nca.bytes[index] = dev->dev_addr[index]; |
| 1030 | nca.bytes[6] = 0x1; |
| 1031 | nca.bytes[7] = 0x1; |
| 1032 | nd->state = ncsi_dev_state_config_ebf; |
| 1033 | } else if (nd->state == ncsi_dev_state_config_ebf) { |
| 1034 | nca.type = NCSI_PKT_CMD_EBF; |
| 1035 | nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap; |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1036 | if (ncsi_channel_is_tx(ndp, nc)) |
| 1037 | nd->state = ncsi_dev_state_config_ecnt; |
| 1038 | else |
| 1039 | nd->state = ncsi_dev_state_config_ec; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1040 | #if IS_ENABLED(CONFIG_IPV6) |
| 1041 | if (ndp->inet6_addr_num > 0 && |
| 1042 | (nc->caps[NCSI_CAP_GENERIC].cap & |
| 1043 | NCSI_CAP_GENERIC_MC)) |
| 1044 | nd->state = ncsi_dev_state_config_egmf; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1045 | } else if (nd->state == ncsi_dev_state_config_egmf) { |
| 1046 | nca.type = NCSI_PKT_CMD_EGMF; |
| 1047 | nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap; |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1048 | if (ncsi_channel_is_tx(ndp, nc)) |
| 1049 | nd->state = ncsi_dev_state_config_ecnt; |
| 1050 | else |
| 1051 | nd->state = ncsi_dev_state_config_ec; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1052 | #endif /* CONFIG_IPV6 */ |
| 1053 | } else if (nd->state == ncsi_dev_state_config_ecnt) { |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1054 | if (np->preferred_channel && |
| 1055 | nc != np->preferred_channel) |
| 1056 | netdev_info(ndp->ndev.dev, |
| 1057 | "NCSI: Tx failed over to channel %u\n", |
| 1058 | nc->id); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1059 | nca.type = NCSI_PKT_CMD_ECNT; |
| 1060 | nd->state = ncsi_dev_state_config_ec; |
| 1061 | } else if (nd->state == ncsi_dev_state_config_ec) { |
| 1062 | /* Enable AEN if it's supported */ |
| 1063 | nca.type = NCSI_PKT_CMD_EC; |
| 1064 | nd->state = ncsi_dev_state_config_ae; |
| 1065 | if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK)) |
| 1066 | nd->state = ncsi_dev_state_config_gls; |
| 1067 | } else if (nd->state == ncsi_dev_state_config_ae) { |
| 1068 | nca.type = NCSI_PKT_CMD_AE; |
| 1069 | nca.bytes[0] = 0; |
| 1070 | nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap; |
| 1071 | nd->state = ncsi_dev_state_config_gls; |
| 1072 | } else if (nd->state == ncsi_dev_state_config_gls) { |
| 1073 | nca.type = NCSI_PKT_CMD_GLS; |
| 1074 | nd->state = ncsi_dev_state_config_done; |
| 1075 | } |
| 1076 | |
| 1077 | ret = ncsi_xmit_cmd(&nca); |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1078 | if (ret) { |
| 1079 | netdev_err(ndp->ndev.dev, |
| 1080 | "NCSI: Failed to transmit CMD %x\n", |
| 1081 | nca.type); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1082 | goto error; |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1083 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1084 | break; |
| 1085 | case ncsi_dev_state_config_done: |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1086 | netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n", |
| 1087 | nc->id); |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1088 | spin_lock_irqsave(&nc->lock, flags); |
Samuel Mendoza-Jonas | 2878a2c | 2018-11-16 15:51:58 +1100 | [diff] [blame] | 1089 | nc->state = NCSI_CHANNEL_ACTIVE; |
| 1090 | |
| 1091 | if (ndp->flags & NCSI_DEV_RESET) { |
| 1092 | /* A reset event happened during config, start it now */ |
| 1093 | nc->reconfigure_needed = false; |
| 1094 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1095 | ncsi_reset_dev(nd); |
| 1096 | break; |
| 1097 | } |
| 1098 | |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1099 | if (nc->reconfigure_needed) { |
| 1100 | /* This channel's configuration has been updated |
| 1101 | * part-way during the config state - start the |
| 1102 | * channel configuration over |
| 1103 | */ |
| 1104 | nc->reconfigure_needed = false; |
| 1105 | nc->state = NCSI_CHANNEL_INACTIVE; |
| 1106 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1107 | |
| 1108 | spin_lock_irqsave(&ndp->lock, flags); |
| 1109 | list_add_tail_rcu(&nc->link, &ndp->channel_queue); |
| 1110 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1111 | |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1112 | netdev_dbg(dev, "Dirty NCSI channel state reset\n"); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1113 | ncsi_process_next_channel(ndp); |
| 1114 | break; |
| 1115 | } |
| 1116 | |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 1117 | if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) { |
| 1118 | hot_nc = nc; |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 1119 | } else { |
| 1120 | hot_nc = NULL; |
Joel Stanley | 87975a0 | 2018-06-19 15:08:31 +0930 | [diff] [blame] | 1121 | netdev_dbg(ndp->ndev.dev, |
| 1122 | "NCSI: channel %u link down after config\n", |
| 1123 | nc->id); |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 1124 | } |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1125 | spin_unlock_irqrestore(&nc->lock, flags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1126 | |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 1127 | /* Update the hot channel */ |
| 1128 | spin_lock_irqsave(&ndp->lock, flags); |
| 1129 | ndp->hot_channel = hot_nc; |
| 1130 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1131 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1132 | ncsi_start_channel_monitor(nc); |
| 1133 | ncsi_process_next_channel(ndp); |
| 1134 | break; |
| 1135 | default: |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1136 | netdev_alert(dev, "Wrong NCSI state 0x%x in config\n", |
| 1137 | nd->state); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | return; |
| 1141 | |
| 1142 | error: |
| 1143 | ncsi_report_link(ndp, true); |
| 1144 | } |
| 1145 | |
| 1146 | static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp) |
| 1147 | { |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1148 | struct ncsi_channel *nc, *found, *hot_nc; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1149 | struct ncsi_channel_mode *ncm; |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1150 | unsigned long flags, cflags; |
| 1151 | struct ncsi_package *np; |
| 1152 | bool with_link; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1153 | |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 1154 | spin_lock_irqsave(&ndp->lock, flags); |
| 1155 | hot_nc = ndp->hot_channel; |
| 1156 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1157 | |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1158 | /* By default the search is done once an inactive channel with up |
| 1159 | * link is found, unless a preferred channel is set. |
| 1160 | * If multi_package or multi_channel are configured all channels in the |
| 1161 | * whitelist are added to the channel queue. |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1162 | */ |
| 1163 | found = NULL; |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1164 | with_link = false; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1165 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1166 | if (!(ndp->package_whitelist & (0x1 << np->id))) |
Samuel Mendoza-Jonas | 955dc68 | 2018-03-05 11:39:05 +1100 | [diff] [blame] | 1167 | continue; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1168 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1169 | if (!(np->channel_whitelist & (0x1 << nc->id))) |
| 1170 | continue; |
| 1171 | |
| 1172 | spin_lock_irqsave(&nc->lock, cflags); |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1173 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1174 | if (!list_empty(&nc->link) || |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1175 | nc->state != NCSI_CHANNEL_INACTIVE) { |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1176 | spin_unlock_irqrestore(&nc->lock, cflags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1177 | continue; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1178 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1179 | |
| 1180 | if (!found) |
| 1181 | found = nc; |
| 1182 | |
Gavin Shan | bbc7c01 | 2016-10-20 11:45:51 +1100 | [diff] [blame] | 1183 | if (nc == hot_nc) |
| 1184 | found = nc; |
| 1185 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1186 | ncm = &nc->modes[NCSI_MODE_LINK]; |
| 1187 | if (ncm->data[2] & 0x1) { |
| 1188 | found = nc; |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1189 | with_link = true; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1190 | } |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1191 | |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1192 | /* If multi_channel is enabled configure all valid |
| 1193 | * channels whether or not they currently have link |
| 1194 | * so they will have AENs enabled. |
| 1195 | */ |
| 1196 | if (with_link || np->multi_channel) { |
| 1197 | spin_lock_irqsave(&ndp->lock, flags); |
| 1198 | list_add_tail_rcu(&nc->link, |
| 1199 | &ndp->channel_queue); |
| 1200 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1201 | |
| 1202 | netdev_dbg(ndp->ndev.dev, |
| 1203 | "NCSI: Channel %u added to queue (link %s)\n", |
| 1204 | nc->id, |
| 1205 | ncm->data[2] & 0x1 ? "up" : "down"); |
| 1206 | } |
| 1207 | |
| 1208 | spin_unlock_irqrestore(&nc->lock, cflags); |
| 1209 | |
| 1210 | if (with_link && !np->multi_channel) |
| 1211 | break; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1212 | } |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1213 | if (with_link && !ndp->multi_package) |
| 1214 | break; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1215 | } |
| 1216 | |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1217 | if (list_empty(&ndp->channel_queue) && found) { |
| 1218 | netdev_info(ndp->ndev.dev, |
| 1219 | "NCSI: No channel with link found, configuring channel %u\n", |
| 1220 | found->id); |
| 1221 | spin_lock_irqsave(&ndp->lock, flags); |
| 1222 | list_add_tail_rcu(&found->link, &ndp->channel_queue); |
| 1223 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1224 | } else if (!found) { |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1225 | netdev_warn(ndp->ndev.dev, |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1226 | "NCSI: No channel found to configure!\n"); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1227 | ncsi_report_link(ndp, true); |
| 1228 | return -ENODEV; |
| 1229 | } |
| 1230 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1231 | return ncsi_process_next_channel(ndp); |
| 1232 | } |
| 1233 | |
| 1234 | static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp) |
| 1235 | { |
| 1236 | struct ncsi_package *np; |
| 1237 | struct ncsi_channel *nc; |
| 1238 | unsigned int cap; |
Gavin Shan | 100ef01 | 2017-10-19 13:43:07 +1100 | [diff] [blame] | 1239 | bool has_channel = false; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1240 | |
| 1241 | /* The hardware arbitration is disabled if any one channel |
| 1242 | * doesn't support explicitly. |
| 1243 | */ |
| 1244 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 1245 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
Gavin Shan | 100ef01 | 2017-10-19 13:43:07 +1100 | [diff] [blame] | 1246 | has_channel = true; |
| 1247 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1248 | cap = nc->caps[NCSI_CAP_GENERIC].cap; |
| 1249 | if (!(cap & NCSI_CAP_GENERIC_HWA) || |
| 1250 | (cap & NCSI_CAP_GENERIC_HWA_MASK) != |
| 1251 | NCSI_CAP_GENERIC_HWA_SUPPORT) { |
| 1252 | ndp->flags &= ~NCSI_DEV_HWA; |
| 1253 | return false; |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
Gavin Shan | 100ef01 | 2017-10-19 13:43:07 +1100 | [diff] [blame] | 1258 | if (has_channel) { |
| 1259 | ndp->flags |= NCSI_DEV_HWA; |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | ndp->flags &= ~NCSI_DEV_HWA; |
| 1264 | return false; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1265 | } |
| 1266 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1267 | static void ncsi_probe_channel(struct ncsi_dev_priv *ndp) |
| 1268 | { |
| 1269 | struct ncsi_dev *nd = &ndp->ndev; |
| 1270 | struct ncsi_package *np; |
| 1271 | struct ncsi_channel *nc; |
| 1272 | struct ncsi_cmd_arg nca; |
| 1273 | unsigned char index; |
| 1274 | int ret; |
| 1275 | |
| 1276 | nca.ndp = ndp; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 1277 | nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1278 | switch (nd->state) { |
| 1279 | case ncsi_dev_state_probe: |
| 1280 | nd->state = ncsi_dev_state_probe_deselect; |
| 1281 | /* Fall through */ |
| 1282 | case ncsi_dev_state_probe_deselect: |
| 1283 | ndp->pending_req_num = 8; |
| 1284 | |
| 1285 | /* Deselect all possible packages */ |
| 1286 | nca.type = NCSI_PKT_CMD_DP; |
Gavin Shan | bc7e0f5 | 2016-10-04 11:25:48 +1100 | [diff] [blame] | 1287 | nca.channel = NCSI_RESERVED_CHANNEL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1288 | for (index = 0; index < 8; index++) { |
| 1289 | nca.package = index; |
| 1290 | ret = ncsi_xmit_cmd(&nca); |
| 1291 | if (ret) |
| 1292 | goto error; |
| 1293 | } |
| 1294 | |
| 1295 | nd->state = ncsi_dev_state_probe_package; |
| 1296 | break; |
| 1297 | case ncsi_dev_state_probe_package: |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1298 | ndp->pending_req_num = 1; |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1299 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1300 | nca.type = NCSI_PKT_CMD_SP; |
| 1301 | nca.bytes[0] = 1; |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1302 | nca.package = ndp->package_probe_id; |
Gavin Shan | bc7e0f5 | 2016-10-04 11:25:48 +1100 | [diff] [blame] | 1303 | nca.channel = NCSI_RESERVED_CHANNEL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1304 | ret = ncsi_xmit_cmd(&nca); |
| 1305 | if (ret) |
| 1306 | goto error; |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1307 | nd->state = ncsi_dev_state_probe_channel; |
| 1308 | break; |
| 1309 | case ncsi_dev_state_probe_channel: |
| 1310 | ndp->active_package = ncsi_find_package(ndp, |
| 1311 | ndp->package_probe_id); |
| 1312 | if (!ndp->active_package) { |
| 1313 | /* No response */ |
| 1314 | nd->state = ncsi_dev_state_probe_dp; |
| 1315 | schedule_work(&ndp->work); |
| 1316 | break; |
| 1317 | } |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1318 | nd->state = ncsi_dev_state_probe_cis; |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1319 | schedule_work(&ndp->work); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1320 | break; |
| 1321 | case ncsi_dev_state_probe_cis: |
Gavin Shan | 55e02d0 | 2016-10-04 11:25:49 +1100 | [diff] [blame] | 1322 | ndp->pending_req_num = NCSI_RESERVED_CHANNEL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1323 | |
| 1324 | /* Clear initial state */ |
| 1325 | nca.type = NCSI_PKT_CMD_CIS; |
| 1326 | nca.package = ndp->active_package->id; |
Gavin Shan | 55e02d0 | 2016-10-04 11:25:49 +1100 | [diff] [blame] | 1327 | for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) { |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1328 | nca.channel = index; |
| 1329 | ret = ncsi_xmit_cmd(&nca); |
| 1330 | if (ret) |
| 1331 | goto error; |
| 1332 | } |
| 1333 | |
| 1334 | nd->state = ncsi_dev_state_probe_gvi; |
| 1335 | break; |
| 1336 | case ncsi_dev_state_probe_gvi: |
| 1337 | case ncsi_dev_state_probe_gc: |
| 1338 | case ncsi_dev_state_probe_gls: |
| 1339 | np = ndp->active_package; |
| 1340 | ndp->pending_req_num = np->channel_num; |
| 1341 | |
| 1342 | /* Retrieve version, capability or link status */ |
| 1343 | if (nd->state == ncsi_dev_state_probe_gvi) |
| 1344 | nca.type = NCSI_PKT_CMD_GVI; |
| 1345 | else if (nd->state == ncsi_dev_state_probe_gc) |
| 1346 | nca.type = NCSI_PKT_CMD_GC; |
| 1347 | else |
| 1348 | nca.type = NCSI_PKT_CMD_GLS; |
| 1349 | |
| 1350 | nca.package = np->id; |
| 1351 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 1352 | nca.channel = nc->id; |
| 1353 | ret = ncsi_xmit_cmd(&nca); |
| 1354 | if (ret) |
| 1355 | goto error; |
| 1356 | } |
| 1357 | |
| 1358 | if (nd->state == ncsi_dev_state_probe_gvi) |
| 1359 | nd->state = ncsi_dev_state_probe_gc; |
| 1360 | else if (nd->state == ncsi_dev_state_probe_gc) |
| 1361 | nd->state = ncsi_dev_state_probe_gls; |
| 1362 | else |
| 1363 | nd->state = ncsi_dev_state_probe_dp; |
| 1364 | break; |
| 1365 | case ncsi_dev_state_probe_dp: |
| 1366 | ndp->pending_req_num = 1; |
| 1367 | |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1368 | /* Deselect the current package */ |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1369 | nca.type = NCSI_PKT_CMD_DP; |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1370 | nca.package = ndp->package_probe_id; |
Gavin Shan | bc7e0f5 | 2016-10-04 11:25:48 +1100 | [diff] [blame] | 1371 | nca.channel = NCSI_RESERVED_CHANNEL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1372 | ret = ncsi_xmit_cmd(&nca); |
| 1373 | if (ret) |
| 1374 | goto error; |
| 1375 | |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1376 | /* Probe next package */ |
| 1377 | ndp->package_probe_id++; |
| 1378 | if (ndp->package_probe_id >= 8) { |
| 1379 | /* Probe finished */ |
| 1380 | ndp->flags |= NCSI_DEV_PROBED; |
| 1381 | break; |
| 1382 | } |
| 1383 | nd->state = ncsi_dev_state_probe_package; |
| 1384 | ndp->active_package = NULL; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1385 | break; |
| 1386 | default: |
| 1387 | netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n", |
| 1388 | nd->state); |
| 1389 | } |
| 1390 | |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1391 | if (ndp->flags & NCSI_DEV_PROBED) { |
| 1392 | /* Check if all packages have HWA support */ |
| 1393 | ncsi_check_hwa(ndp); |
| 1394 | ncsi_choose_active_channel(ndp); |
| 1395 | } |
| 1396 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1397 | return; |
| 1398 | error: |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1399 | netdev_err(ndp->ndev.dev, |
| 1400 | "NCSI: Failed to transmit cmd 0x%x during probe\n", |
| 1401 | nca.type); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1402 | ncsi_report_link(ndp, true); |
| 1403 | } |
| 1404 | |
| 1405 | static void ncsi_dev_work(struct work_struct *work) |
| 1406 | { |
| 1407 | struct ncsi_dev_priv *ndp = container_of(work, |
| 1408 | struct ncsi_dev_priv, work); |
| 1409 | struct ncsi_dev *nd = &ndp->ndev; |
| 1410 | |
| 1411 | switch (nd->state & ncsi_dev_state_major) { |
| 1412 | case ncsi_dev_state_probe: |
| 1413 | ncsi_probe_channel(ndp); |
| 1414 | break; |
| 1415 | case ncsi_dev_state_suspend: |
| 1416 | ncsi_suspend_channel(ndp); |
| 1417 | break; |
| 1418 | case ncsi_dev_state_config: |
| 1419 | ncsi_configure_channel(ndp); |
| 1420 | break; |
| 1421 | default: |
| 1422 | netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n", |
| 1423 | nd->state); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | int ncsi_process_next_channel(struct ncsi_dev_priv *ndp) |
| 1428 | { |
| 1429 | struct ncsi_channel *nc; |
| 1430 | int old_state; |
| 1431 | unsigned long flags; |
| 1432 | |
| 1433 | spin_lock_irqsave(&ndp->lock, flags); |
| 1434 | nc = list_first_or_null_rcu(&ndp->channel_queue, |
| 1435 | struct ncsi_channel, link); |
Arnd Bergmann | a1b43ed | 2016-07-21 21:28:34 +0200 | [diff] [blame] | 1436 | if (!nc) { |
| 1437 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1438 | goto out; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1439 | } |
Arnd Bergmann | a1b43ed | 2016-07-21 21:28:34 +0200 | [diff] [blame] | 1440 | |
Arnd Bergmann | a1b43ed | 2016-07-21 21:28:34 +0200 | [diff] [blame] | 1441 | list_del_init(&nc->link); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1442 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1443 | |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1444 | spin_lock_irqsave(&nc->lock, flags); |
| 1445 | old_state = nc->state; |
| 1446 | nc->state = NCSI_CHANNEL_INVISIBLE; |
| 1447 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1448 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1449 | ndp->active_channel = nc; |
Arnd Bergmann | a1b43ed | 2016-07-21 21:28:34 +0200 | [diff] [blame] | 1450 | ndp->active_package = nc->package; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1451 | |
| 1452 | switch (old_state) { |
| 1453 | case NCSI_CHANNEL_INACTIVE: |
| 1454 | ndp->ndev.state = ncsi_dev_state_config; |
Joel Stanley | 87975a0 | 2018-06-19 15:08:31 +0930 | [diff] [blame] | 1455 | netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n", |
| 1456 | nc->id); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1457 | ncsi_configure_channel(ndp); |
| 1458 | break; |
| 1459 | case NCSI_CHANNEL_ACTIVE: |
| 1460 | ndp->ndev.state = ncsi_dev_state_suspend; |
Joel Stanley | 87975a0 | 2018-06-19 15:08:31 +0930 | [diff] [blame] | 1461 | netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n", |
| 1462 | nc->id); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1463 | ncsi_suspend_channel(ndp); |
| 1464 | break; |
| 1465 | default: |
| 1466 | netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n", |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1467 | old_state, nc->package->id, nc->id); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1468 | ncsi_report_link(ndp, false); |
| 1469 | return -EINVAL; |
| 1470 | } |
| 1471 | |
| 1472 | return 0; |
Arnd Bergmann | a1b43ed | 2016-07-21 21:28:34 +0200 | [diff] [blame] | 1473 | |
| 1474 | out: |
| 1475 | ndp->active_channel = NULL; |
| 1476 | ndp->active_package = NULL; |
| 1477 | if (ndp->flags & NCSI_DEV_RESHUFFLE) { |
| 1478 | ndp->flags &= ~NCSI_DEV_RESHUFFLE; |
| 1479 | return ncsi_choose_active_channel(ndp); |
| 1480 | } |
| 1481 | |
| 1482 | ncsi_report_link(ndp, false); |
| 1483 | return -ENODEV; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | #if IS_ENABLED(CONFIG_IPV6) |
| 1487 | static int ncsi_inet6addr_event(struct notifier_block *this, |
| 1488 | unsigned long event, void *data) |
| 1489 | { |
| 1490 | struct inet6_ifaddr *ifa = data; |
| 1491 | struct net_device *dev = ifa->idev->dev; |
| 1492 | struct ncsi_dev *nd = ncsi_find_dev(dev); |
| 1493 | struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL; |
| 1494 | struct ncsi_package *np; |
| 1495 | struct ncsi_channel *nc; |
| 1496 | struct ncsi_cmd_arg nca; |
| 1497 | bool action; |
| 1498 | int ret; |
| 1499 | |
| 1500 | if (!ndp || (ipv6_addr_type(&ifa->addr) & |
| 1501 | (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK))) |
| 1502 | return NOTIFY_OK; |
| 1503 | |
| 1504 | switch (event) { |
| 1505 | case NETDEV_UP: |
| 1506 | action = (++ndp->inet6_addr_num) == 1; |
| 1507 | nca.type = NCSI_PKT_CMD_EGMF; |
| 1508 | break; |
| 1509 | case NETDEV_DOWN: |
| 1510 | action = (--ndp->inet6_addr_num == 0); |
| 1511 | nca.type = NCSI_PKT_CMD_DGMF; |
| 1512 | break; |
| 1513 | default: |
| 1514 | return NOTIFY_OK; |
| 1515 | } |
| 1516 | |
| 1517 | /* We might not have active channel or packages. The IPv6 |
| 1518 | * required multicast will be enabled when active channel |
| 1519 | * or packages are chosen. |
| 1520 | */ |
| 1521 | np = ndp->active_package; |
| 1522 | nc = ndp->active_channel; |
| 1523 | if (!action || !np || !nc) |
| 1524 | return NOTIFY_OK; |
| 1525 | |
| 1526 | /* We needn't enable or disable it if the function isn't supported */ |
| 1527 | if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC)) |
| 1528 | return NOTIFY_OK; |
| 1529 | |
| 1530 | nca.ndp = ndp; |
Gavin Shan | a0509cb | 2016-10-04 11:25:51 +1100 | [diff] [blame] | 1531 | nca.req_flags = 0; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1532 | nca.package = np->id; |
| 1533 | nca.channel = nc->id; |
| 1534 | nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap; |
| 1535 | ret = ncsi_xmit_cmd(&nca); |
| 1536 | if (ret) { |
| 1537 | netdev_warn(dev, "Fail to %s global multicast filter (%d)\n", |
| 1538 | (event == NETDEV_UP) ? "enable" : "disable", ret); |
| 1539 | return NOTIFY_DONE; |
| 1540 | } |
| 1541 | |
| 1542 | return NOTIFY_OK; |
| 1543 | } |
| 1544 | |
| 1545 | static struct notifier_block ncsi_inet6addr_notifier = { |
| 1546 | .notifier_call = ncsi_inet6addr_event, |
| 1547 | }; |
| 1548 | #endif /* CONFIG_IPV6 */ |
| 1549 | |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1550 | static int ncsi_kick_channels(struct ncsi_dev_priv *ndp) |
| 1551 | { |
| 1552 | struct ncsi_dev *nd = &ndp->ndev; |
| 1553 | struct ncsi_channel *nc; |
| 1554 | struct ncsi_package *np; |
| 1555 | unsigned long flags; |
| 1556 | unsigned int n = 0; |
| 1557 | |
| 1558 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 1559 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 1560 | spin_lock_irqsave(&nc->lock, flags); |
| 1561 | |
| 1562 | /* Channels may be busy, mark dirty instead of |
| 1563 | * kicking if; |
| 1564 | * a) not ACTIVE (configured) |
| 1565 | * b) in the channel_queue (to be configured) |
| 1566 | * c) it's ndev is in the config state |
| 1567 | */ |
| 1568 | if (nc->state != NCSI_CHANNEL_ACTIVE) { |
| 1569 | if ((ndp->ndev.state & 0xff00) == |
| 1570 | ncsi_dev_state_config || |
| 1571 | !list_empty(&nc->link)) { |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1572 | netdev_dbg(nd->dev, |
| 1573 | "NCSI: channel %p marked dirty\n", |
| 1574 | nc); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1575 | nc->reconfigure_needed = true; |
| 1576 | } |
| 1577 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1578 | continue; |
| 1579 | } |
| 1580 | |
| 1581 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1582 | |
| 1583 | ncsi_stop_channel_monitor(nc); |
| 1584 | spin_lock_irqsave(&nc->lock, flags); |
| 1585 | nc->state = NCSI_CHANNEL_INACTIVE; |
| 1586 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1587 | |
| 1588 | spin_lock_irqsave(&ndp->lock, flags); |
| 1589 | list_add_tail_rcu(&nc->link, &ndp->channel_queue); |
| 1590 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1591 | |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1592 | netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1593 | n++; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | return n; |
| 1598 | } |
| 1599 | |
| 1600 | int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid) |
| 1601 | { |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1602 | struct ncsi_dev_priv *ndp; |
| 1603 | unsigned int n_vids = 0; |
| 1604 | struct vlan_vid *vlan; |
| 1605 | struct ncsi_dev *nd; |
| 1606 | bool found = false; |
| 1607 | |
| 1608 | if (vid == 0) |
| 1609 | return 0; |
| 1610 | |
| 1611 | nd = ncsi_find_dev(dev); |
| 1612 | if (!nd) { |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1613 | netdev_warn(dev, "NCSI: No net_device?\n"); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | ndp = TO_NCSI_DEV_PRIV(nd); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1618 | |
| 1619 | /* Add the VLAN id to our internal list */ |
| 1620 | list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) { |
| 1621 | n_vids++; |
| 1622 | if (vlan->vid == vid) { |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1623 | netdev_dbg(dev, "NCSI: vid %u already registered\n", |
| 1624 | vid); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1625 | return 0; |
| 1626 | } |
| 1627 | } |
Samuel Mendoza-Jonas | 6e9c007 | 2017-10-11 16:54:27 +1100 | [diff] [blame] | 1628 | if (n_vids >= NCSI_MAX_VLAN_VIDS) { |
| 1629 | netdev_warn(dev, |
| 1630 | "tried to add vlan id %u but NCSI max already registered (%u)\n", |
| 1631 | vid, NCSI_MAX_VLAN_VIDS); |
| 1632 | return -ENOSPC; |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 1636 | if (!vlan) |
| 1637 | return -ENOMEM; |
| 1638 | |
| 1639 | vlan->proto = proto; |
| 1640 | vlan->vid = vid; |
| 1641 | list_add_rcu(&vlan->list, &ndp->vlan_vids); |
| 1642 | |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1643 | netdev_dbg(dev, "NCSI: Added new vid %u\n", vid); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1644 | |
| 1645 | found = ncsi_kick_channels(ndp) != 0; |
| 1646 | |
| 1647 | return found ? ncsi_process_next_channel(ndp) : 0; |
| 1648 | } |
Arnd Bergmann | fd0c88b | 2017-09-05 10:05:47 +0200 | [diff] [blame] | 1649 | EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1650 | |
| 1651 | int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid) |
| 1652 | { |
| 1653 | struct vlan_vid *vlan, *tmp; |
| 1654 | struct ncsi_dev_priv *ndp; |
| 1655 | struct ncsi_dev *nd; |
| 1656 | bool found = false; |
| 1657 | |
| 1658 | if (vid == 0) |
| 1659 | return 0; |
| 1660 | |
| 1661 | nd = ncsi_find_dev(dev); |
| 1662 | if (!nd) { |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1663 | netdev_warn(dev, "NCSI: no net_device?\n"); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | ndp = TO_NCSI_DEV_PRIV(nd); |
| 1668 | |
| 1669 | /* Remove the VLAN id from our internal list */ |
| 1670 | list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list) |
| 1671 | if (vlan->vid == vid) { |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1672 | netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1673 | list_del_rcu(&vlan->list); |
| 1674 | found = true; |
| 1675 | kfree(vlan); |
| 1676 | } |
| 1677 | |
| 1678 | if (!found) { |
Samuel Mendoza-Jonas | 9ef8690 | 2017-11-08 16:30:44 +1100 | [diff] [blame] | 1679 | netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1680 | return -EINVAL; |
| 1681 | } |
| 1682 | |
| 1683 | found = ncsi_kick_channels(ndp) != 0; |
| 1684 | |
| 1685 | return found ? ncsi_process_next_channel(ndp) : 0; |
| 1686 | } |
Arnd Bergmann | fd0c88b | 2017-09-05 10:05:47 +0200 | [diff] [blame] | 1687 | EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1688 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1689 | struct ncsi_dev *ncsi_register_dev(struct net_device *dev, |
| 1690 | void (*handler)(struct ncsi_dev *ndev)) |
| 1691 | { |
| 1692 | struct ncsi_dev_priv *ndp; |
| 1693 | struct ncsi_dev *nd; |
| 1694 | unsigned long flags; |
| 1695 | int i; |
| 1696 | |
| 1697 | /* Check if the device has been registered or not */ |
| 1698 | nd = ncsi_find_dev(dev); |
| 1699 | if (nd) |
| 1700 | return nd; |
| 1701 | |
| 1702 | /* Create NCSI device */ |
| 1703 | ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC); |
| 1704 | if (!ndp) |
| 1705 | return NULL; |
| 1706 | |
| 1707 | nd = &ndp->ndev; |
| 1708 | nd->state = ncsi_dev_state_registered; |
| 1709 | nd->dev = dev; |
| 1710 | nd->handler = handler; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1711 | ndp->pending_req_num = 0; |
| 1712 | INIT_LIST_HEAD(&ndp->channel_queue); |
Samuel Mendoza-Jonas | 21acf63 | 2017-08-28 16:18:42 +1000 | [diff] [blame] | 1713 | INIT_LIST_HEAD(&ndp->vlan_vids); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1714 | INIT_WORK(&ndp->work, ncsi_dev_work); |
Samuel Mendoza-Jonas | 8d951a7 | 2018-11-16 15:51:59 +1100 | [diff] [blame] | 1715 | ndp->package_whitelist = UINT_MAX; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1716 | |
| 1717 | /* Initialize private NCSI device */ |
| 1718 | spin_lock_init(&ndp->lock); |
| 1719 | INIT_LIST_HEAD(&ndp->packages); |
Gavin Shan | a15af54 | 2016-10-04 11:25:50 +1100 | [diff] [blame] | 1720 | ndp->request_id = NCSI_REQ_START_IDX; |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1721 | for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) { |
| 1722 | ndp->requests[i].id = i; |
| 1723 | ndp->requests[i].ndp = ndp; |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 1724 | timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0); |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | spin_lock_irqsave(&ncsi_dev_lock, flags); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1728 | #if IS_ENABLED(CONFIG_IPV6) |
| 1729 | ndp->inet6_addr_num = 0; |
| 1730 | if (list_empty(&ncsi_dev_list)) |
| 1731 | register_inet6addr_notifier(&ncsi_inet6addr_notifier); |
| 1732 | #endif |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1733 | list_add_tail_rcu(&ndp->node, &ncsi_dev_list); |
| 1734 | spin_unlock_irqrestore(&ncsi_dev_lock, flags); |
| 1735 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1736 | /* Register NCSI packet Rx handler */ |
| 1737 | ndp->ptype.type = cpu_to_be16(ETH_P_NCSI); |
| 1738 | ndp->ptype.func = ncsi_rcv_rsp; |
| 1739 | ndp->ptype.dev = dev; |
| 1740 | dev_add_pack(&ndp->ptype); |
| 1741 | |
Samuel Mendoza-Jonas | 955dc68 | 2018-03-05 11:39:05 +1100 | [diff] [blame] | 1742 | /* Set up generic netlink interface */ |
| 1743 | ncsi_init_netlink(dev); |
| 1744 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1745 | return nd; |
| 1746 | } |
| 1747 | EXPORT_SYMBOL_GPL(ncsi_register_dev); |
| 1748 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1749 | int ncsi_start_dev(struct ncsi_dev *nd) |
| 1750 | { |
| 1751 | struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1752 | |
| 1753 | if (nd->state != ncsi_dev_state_registered && |
| 1754 | nd->state != ncsi_dev_state_functional) |
| 1755 | return -ENOTTY; |
| 1756 | |
| 1757 | if (!(ndp->flags & NCSI_DEV_PROBED)) { |
Samuel Mendoza-Jonas | 8e13f70 | 2018-11-16 15:51:55 +1100 | [diff] [blame] | 1758 | ndp->package_probe_id = 0; |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1759 | nd->state = ncsi_dev_state_probe; |
| 1760 | schedule_work(&ndp->work); |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
Samuel Mendoza-Jonas | 2878a2c | 2018-11-16 15:51:58 +1100 | [diff] [blame] | 1764 | return ncsi_reset_dev(nd); |
Gavin Shan | c0cd1ba | 2016-10-04 11:25:53 +1100 | [diff] [blame] | 1765 | } |
| 1766 | EXPORT_SYMBOL_GPL(ncsi_start_dev); |
| 1767 | |
| 1768 | void ncsi_stop_dev(struct ncsi_dev *nd) |
| 1769 | { |
| 1770 | struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd); |
| 1771 | struct ncsi_package *np; |
| 1772 | struct ncsi_channel *nc; |
| 1773 | bool chained; |
| 1774 | int old_state; |
| 1775 | unsigned long flags; |
| 1776 | |
Samuel Mendoza-Jonas | 2878a2c | 2018-11-16 15:51:58 +1100 | [diff] [blame] | 1777 | /* Stop the channel monitor on any active channels. Don't reset the |
| 1778 | * channel state so we know which were active when ncsi_start_dev() |
| 1779 | * is next called. |
| 1780 | */ |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1781 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 1782 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
Gavin Shan | c0cd1ba | 2016-10-04 11:25:53 +1100 | [diff] [blame] | 1783 | ncsi_stop_channel_monitor(nc); |
| 1784 | |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1785 | spin_lock_irqsave(&nc->lock, flags); |
| 1786 | chained = !list_empty(&nc->link); |
| 1787 | old_state = nc->state; |
Gavin Shan | d8cedaa | 2016-10-04 11:25:47 +1100 | [diff] [blame] | 1788 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1789 | |
| 1790 | WARN_ON_ONCE(chained || |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1791 | old_state == NCSI_CHANNEL_INVISIBLE); |
| 1792 | } |
| 1793 | } |
| 1794 | |
Joel Stanley | 6e42a3f | 2018-06-19 15:08:33 +0930 | [diff] [blame] | 1795 | netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n"); |
Gavin Shan | c0cd1ba | 2016-10-04 11:25:53 +1100 | [diff] [blame] | 1796 | ncsi_report_link(ndp, true); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1797 | } |
Gavin Shan | c0cd1ba | 2016-10-04 11:25:53 +1100 | [diff] [blame] | 1798 | EXPORT_SYMBOL_GPL(ncsi_stop_dev); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1799 | |
Samuel Mendoza-Jonas | 2878a2c | 2018-11-16 15:51:58 +1100 | [diff] [blame] | 1800 | int ncsi_reset_dev(struct ncsi_dev *nd) |
| 1801 | { |
| 1802 | struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd); |
| 1803 | struct ncsi_channel *nc, *active, *tmp; |
| 1804 | struct ncsi_package *np; |
| 1805 | unsigned long flags; |
| 1806 | |
| 1807 | spin_lock_irqsave(&ndp->lock, flags); |
| 1808 | |
| 1809 | if (!(ndp->flags & NCSI_DEV_RESET)) { |
| 1810 | /* Haven't been called yet, check states */ |
| 1811 | switch (nd->state & ncsi_dev_state_major) { |
| 1812 | case ncsi_dev_state_registered: |
| 1813 | case ncsi_dev_state_probe: |
| 1814 | /* Not even probed yet - do nothing */ |
| 1815 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1816 | return 0; |
| 1817 | case ncsi_dev_state_suspend: |
| 1818 | case ncsi_dev_state_config: |
| 1819 | /* Wait for the channel to finish its suspend/config |
| 1820 | * operation; once it finishes it will check for |
| 1821 | * NCSI_DEV_RESET and reset the state. |
| 1822 | */ |
| 1823 | ndp->flags |= NCSI_DEV_RESET; |
| 1824 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1825 | return 0; |
| 1826 | } |
| 1827 | } else { |
| 1828 | switch (nd->state) { |
| 1829 | case ncsi_dev_state_suspend_done: |
| 1830 | case ncsi_dev_state_config_done: |
| 1831 | case ncsi_dev_state_functional: |
| 1832 | /* Ok */ |
| 1833 | break; |
| 1834 | default: |
| 1835 | /* Current reset operation happening */ |
| 1836 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1837 | return 0; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | if (!list_empty(&ndp->channel_queue)) { |
| 1842 | /* Clear any channel queue we may have interrupted */ |
| 1843 | list_for_each_entry_safe(nc, tmp, &ndp->channel_queue, link) |
| 1844 | list_del_init(&nc->link); |
| 1845 | } |
| 1846 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1847 | |
| 1848 | active = NULL; |
| 1849 | NCSI_FOR_EACH_PACKAGE(ndp, np) { |
| 1850 | NCSI_FOR_EACH_CHANNEL(np, nc) { |
| 1851 | spin_lock_irqsave(&nc->lock, flags); |
| 1852 | |
| 1853 | if (nc->state == NCSI_CHANNEL_ACTIVE) { |
| 1854 | active = nc; |
| 1855 | nc->state = NCSI_CHANNEL_INVISIBLE; |
| 1856 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1857 | ncsi_stop_channel_monitor(nc); |
| 1858 | break; |
| 1859 | } |
| 1860 | |
| 1861 | spin_unlock_irqrestore(&nc->lock, flags); |
| 1862 | } |
| 1863 | if (active) |
| 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | if (!active) { |
| 1868 | /* Done */ |
| 1869 | spin_lock_irqsave(&ndp->lock, flags); |
| 1870 | ndp->flags &= ~NCSI_DEV_RESET; |
| 1871 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1872 | return ncsi_choose_active_channel(ndp); |
| 1873 | } |
| 1874 | |
| 1875 | spin_lock_irqsave(&ndp->lock, flags); |
| 1876 | ndp->flags |= NCSI_DEV_RESET; |
| 1877 | ndp->active_channel = active; |
| 1878 | ndp->active_package = active->package; |
| 1879 | spin_unlock_irqrestore(&ndp->lock, flags); |
| 1880 | |
| 1881 | nd->state = ncsi_dev_state_suspend; |
| 1882 | schedule_work(&ndp->work); |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1886 | void ncsi_unregister_dev(struct ncsi_dev *nd) |
| 1887 | { |
| 1888 | struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd); |
| 1889 | struct ncsi_package *np, *tmp; |
| 1890 | unsigned long flags; |
| 1891 | |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1892 | dev_remove_pack(&ndp->ptype); |
| 1893 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1894 | list_for_each_entry_safe(np, tmp, &ndp->packages, node) |
| 1895 | ncsi_remove_package(np); |
| 1896 | |
| 1897 | spin_lock_irqsave(&ncsi_dev_lock, flags); |
| 1898 | list_del_rcu(&ndp->node); |
Gavin Shan | e6f44ed | 2016-07-19 11:54:19 +1000 | [diff] [blame] | 1899 | #if IS_ENABLED(CONFIG_IPV6) |
| 1900 | if (list_empty(&ncsi_dev_list)) |
| 1901 | unregister_inet6addr_notifier(&ncsi_inet6addr_notifier); |
| 1902 | #endif |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1903 | spin_unlock_irqrestore(&ncsi_dev_lock, flags); |
| 1904 | |
Samuel Mendoza-Jonas | 955dc68 | 2018-03-05 11:39:05 +1100 | [diff] [blame] | 1905 | ncsi_unregister_netlink(nd->dev); |
| 1906 | |
Gavin Shan | 2d283bd | 2016-07-19 11:54:16 +1000 | [diff] [blame] | 1907 | kfree(ndp); |
| 1908 | } |
| 1909 | EXPORT_SYMBOL_GPL(ncsi_unregister_dev); |