0001
0002
0003
0004
0005
0006 #include <linux/module.h>
0007 #include <linux/kernel.h>
0008 #include <linux/init.h>
0009 #include <linux/netdevice.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013
0014 #include <net/ncsi.h>
0015 #include <net/net_namespace.h>
0016 #include <net/sock.h>
0017 #include <net/addrconf.h>
0018 #include <net/ipv6.h>
0019 #include <net/genetlink.h>
0020
0021 #include "internal.h"
0022 #include "ncsi-pkt.h"
0023 #include "ncsi-netlink.h"
0024
0025 LIST_HEAD(ncsi_dev_list);
0026 DEFINE_SPINLOCK(ncsi_dev_lock);
0027
0028 bool ncsi_channel_has_link(struct ncsi_channel *channel)
0029 {
0030 return !!(channel->modes[NCSI_MODE_LINK].data[2] & 0x1);
0031 }
0032
0033 bool ncsi_channel_is_last(struct ncsi_dev_priv *ndp,
0034 struct ncsi_channel *channel)
0035 {
0036 struct ncsi_package *np;
0037 struct ncsi_channel *nc;
0038
0039 NCSI_FOR_EACH_PACKAGE(ndp, np)
0040 NCSI_FOR_EACH_CHANNEL(np, nc) {
0041 if (nc == channel)
0042 continue;
0043 if (nc->state == NCSI_CHANNEL_ACTIVE &&
0044 ncsi_channel_has_link(nc))
0045 return false;
0046 }
0047
0048 return true;
0049 }
0050
0051 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
0052 {
0053 struct ncsi_dev *nd = &ndp->ndev;
0054 struct ncsi_package *np;
0055 struct ncsi_channel *nc;
0056 unsigned long flags;
0057
0058 nd->state = ncsi_dev_state_functional;
0059 if (force_down) {
0060 nd->link_up = 0;
0061 goto report;
0062 }
0063
0064 nd->link_up = 0;
0065 NCSI_FOR_EACH_PACKAGE(ndp, np) {
0066 NCSI_FOR_EACH_CHANNEL(np, nc) {
0067 spin_lock_irqsave(&nc->lock, flags);
0068
0069 if (!list_empty(&nc->link) ||
0070 nc->state != NCSI_CHANNEL_ACTIVE) {
0071 spin_unlock_irqrestore(&nc->lock, flags);
0072 continue;
0073 }
0074
0075 if (ncsi_channel_has_link(nc)) {
0076 spin_unlock_irqrestore(&nc->lock, flags);
0077 nd->link_up = 1;
0078 goto report;
0079 }
0080
0081 spin_unlock_irqrestore(&nc->lock, flags);
0082 }
0083 }
0084
0085 report:
0086 nd->handler(nd);
0087 }
0088
0089 static void ncsi_channel_monitor(struct timer_list *t)
0090 {
0091 struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
0092 struct ncsi_package *np = nc->package;
0093 struct ncsi_dev_priv *ndp = np->ndp;
0094 struct ncsi_channel_mode *ncm;
0095 struct ncsi_cmd_arg nca;
0096 bool enabled, chained;
0097 unsigned int monitor_state;
0098 unsigned long flags;
0099 int state, ret;
0100
0101 spin_lock_irqsave(&nc->lock, flags);
0102 state = nc->state;
0103 chained = !list_empty(&nc->link);
0104 enabled = nc->monitor.enabled;
0105 monitor_state = nc->monitor.state;
0106 spin_unlock_irqrestore(&nc->lock, flags);
0107
0108 if (!enabled)
0109 return;
0110 if (WARN_ON_ONCE(chained))
0111 goto bad_state;
0112
0113 if (state != NCSI_CHANNEL_INACTIVE &&
0114 state != NCSI_CHANNEL_ACTIVE) {
0115 bad_state:
0116 netdev_warn(ndp->ndev.dev,
0117 "Bad NCSI monitor state channel %d 0x%x %s queue\n",
0118 nc->id, state, chained ? "on" : "off");
0119 spin_lock_irqsave(&nc->lock, flags);
0120 nc->monitor.enabled = false;
0121 spin_unlock_irqrestore(&nc->lock, flags);
0122 return;
0123 }
0124
0125 switch (monitor_state) {
0126 case NCSI_CHANNEL_MONITOR_START:
0127 case NCSI_CHANNEL_MONITOR_RETRY:
0128 nca.ndp = ndp;
0129 nca.package = np->id;
0130 nca.channel = nc->id;
0131 nca.type = NCSI_PKT_CMD_GLS;
0132 nca.req_flags = 0;
0133 ret = ncsi_xmit_cmd(&nca);
0134 if (ret)
0135 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
0136 ret);
0137 break;
0138 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
0139 break;
0140 default:
0141 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
0142 nc->id);
0143 ncsi_report_link(ndp, true);
0144 ndp->flags |= NCSI_DEV_RESHUFFLE;
0145
0146 ncm = &nc->modes[NCSI_MODE_LINK];
0147 spin_lock_irqsave(&nc->lock, flags);
0148 nc->monitor.enabled = false;
0149 nc->state = NCSI_CHANNEL_INVISIBLE;
0150 ncm->data[2] &= ~0x1;
0151 spin_unlock_irqrestore(&nc->lock, flags);
0152
0153 spin_lock_irqsave(&ndp->lock, flags);
0154 nc->state = NCSI_CHANNEL_ACTIVE;
0155 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
0156 spin_unlock_irqrestore(&ndp->lock, flags);
0157 ncsi_process_next_channel(ndp);
0158 return;
0159 }
0160
0161 spin_lock_irqsave(&nc->lock, flags);
0162 nc->monitor.state++;
0163 spin_unlock_irqrestore(&nc->lock, flags);
0164 mod_timer(&nc->monitor.timer, jiffies + HZ);
0165 }
0166
0167 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
0168 {
0169 unsigned long flags;
0170
0171 spin_lock_irqsave(&nc->lock, flags);
0172 WARN_ON_ONCE(nc->monitor.enabled);
0173 nc->monitor.enabled = true;
0174 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
0175 spin_unlock_irqrestore(&nc->lock, flags);
0176
0177 mod_timer(&nc->monitor.timer, jiffies + HZ);
0178 }
0179
0180 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
0181 {
0182 unsigned long flags;
0183
0184 spin_lock_irqsave(&nc->lock, flags);
0185 if (!nc->monitor.enabled) {
0186 spin_unlock_irqrestore(&nc->lock, flags);
0187 return;
0188 }
0189 nc->monitor.enabled = false;
0190 spin_unlock_irqrestore(&nc->lock, flags);
0191
0192 del_timer_sync(&nc->monitor.timer);
0193 }
0194
0195 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
0196 unsigned char id)
0197 {
0198 struct ncsi_channel *nc;
0199
0200 NCSI_FOR_EACH_CHANNEL(np, nc) {
0201 if (nc->id == id)
0202 return nc;
0203 }
0204
0205 return NULL;
0206 }
0207
0208 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
0209 {
0210 struct ncsi_channel *nc, *tmp;
0211 int index;
0212 unsigned long flags;
0213
0214 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
0215 if (!nc)
0216 return NULL;
0217
0218 nc->id = id;
0219 nc->package = np;
0220 nc->state = NCSI_CHANNEL_INACTIVE;
0221 nc->monitor.enabled = false;
0222 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
0223 spin_lock_init(&nc->lock);
0224 INIT_LIST_HEAD(&nc->link);
0225 for (index = 0; index < NCSI_CAP_MAX; index++)
0226 nc->caps[index].index = index;
0227 for (index = 0; index < NCSI_MODE_MAX; index++)
0228 nc->modes[index].index = index;
0229
0230 spin_lock_irqsave(&np->lock, flags);
0231 tmp = ncsi_find_channel(np, id);
0232 if (tmp) {
0233 spin_unlock_irqrestore(&np->lock, flags);
0234 kfree(nc);
0235 return tmp;
0236 }
0237
0238 list_add_tail_rcu(&nc->node, &np->channels);
0239 np->channel_num++;
0240 spin_unlock_irqrestore(&np->lock, flags);
0241
0242 return nc;
0243 }
0244
0245 static void ncsi_remove_channel(struct ncsi_channel *nc)
0246 {
0247 struct ncsi_package *np = nc->package;
0248 unsigned long flags;
0249
0250 spin_lock_irqsave(&nc->lock, flags);
0251
0252
0253 kfree(nc->mac_filter.addrs);
0254 kfree(nc->vlan_filter.vids);
0255
0256 nc->state = NCSI_CHANNEL_INACTIVE;
0257 spin_unlock_irqrestore(&nc->lock, flags);
0258 ncsi_stop_channel_monitor(nc);
0259
0260
0261 spin_lock_irqsave(&np->lock, flags);
0262 list_del_rcu(&nc->node);
0263 np->channel_num--;
0264 spin_unlock_irqrestore(&np->lock, flags);
0265
0266 kfree(nc);
0267 }
0268
0269 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
0270 unsigned char id)
0271 {
0272 struct ncsi_package *np;
0273
0274 NCSI_FOR_EACH_PACKAGE(ndp, np) {
0275 if (np->id == id)
0276 return np;
0277 }
0278
0279 return NULL;
0280 }
0281
0282 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
0283 unsigned char id)
0284 {
0285 struct ncsi_package *np, *tmp;
0286 unsigned long flags;
0287
0288 np = kzalloc(sizeof(*np), GFP_ATOMIC);
0289 if (!np)
0290 return NULL;
0291
0292 np->id = id;
0293 np->ndp = ndp;
0294 spin_lock_init(&np->lock);
0295 INIT_LIST_HEAD(&np->channels);
0296 np->channel_whitelist = UINT_MAX;
0297
0298 spin_lock_irqsave(&ndp->lock, flags);
0299 tmp = ncsi_find_package(ndp, id);
0300 if (tmp) {
0301 spin_unlock_irqrestore(&ndp->lock, flags);
0302 kfree(np);
0303 return tmp;
0304 }
0305
0306 list_add_tail_rcu(&np->node, &ndp->packages);
0307 ndp->package_num++;
0308 spin_unlock_irqrestore(&ndp->lock, flags);
0309
0310 return np;
0311 }
0312
0313 void ncsi_remove_package(struct ncsi_package *np)
0314 {
0315 struct ncsi_dev_priv *ndp = np->ndp;
0316 struct ncsi_channel *nc, *tmp;
0317 unsigned long flags;
0318
0319
0320 list_for_each_entry_safe(nc, tmp, &np->channels, node)
0321 ncsi_remove_channel(nc);
0322
0323
0324 spin_lock_irqsave(&ndp->lock, flags);
0325 list_del_rcu(&np->node);
0326 ndp->package_num--;
0327 spin_unlock_irqrestore(&ndp->lock, flags);
0328
0329 kfree(np);
0330 }
0331
0332 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
0333 unsigned char id,
0334 struct ncsi_package **np,
0335 struct ncsi_channel **nc)
0336 {
0337 struct ncsi_package *p;
0338 struct ncsi_channel *c;
0339
0340 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
0341 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
0342
0343 if (np)
0344 *np = p;
0345 if (nc)
0346 *nc = c;
0347 }
0348
0349
0350
0351
0352
0353 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
0354 unsigned int req_flags)
0355 {
0356 struct ncsi_request *nr = NULL;
0357 int i, limit = ARRAY_SIZE(ndp->requests);
0358 unsigned long flags;
0359
0360
0361 spin_lock_irqsave(&ndp->lock, flags);
0362 for (i = ndp->request_id; i < limit; i++) {
0363 if (ndp->requests[i].used)
0364 continue;
0365
0366 nr = &ndp->requests[i];
0367 nr->used = true;
0368 nr->flags = req_flags;
0369 ndp->request_id = i + 1;
0370 goto found;
0371 }
0372
0373
0374 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
0375 if (ndp->requests[i].used)
0376 continue;
0377
0378 nr = &ndp->requests[i];
0379 nr->used = true;
0380 nr->flags = req_flags;
0381 ndp->request_id = i + 1;
0382 goto found;
0383 }
0384
0385 found:
0386 spin_unlock_irqrestore(&ndp->lock, flags);
0387 return nr;
0388 }
0389
0390 void ncsi_free_request(struct ncsi_request *nr)
0391 {
0392 struct ncsi_dev_priv *ndp = nr->ndp;
0393 struct sk_buff *cmd, *rsp;
0394 unsigned long flags;
0395 bool driven;
0396
0397 if (nr->enabled) {
0398 nr->enabled = false;
0399 del_timer_sync(&nr->timer);
0400 }
0401
0402 spin_lock_irqsave(&ndp->lock, flags);
0403 cmd = nr->cmd;
0404 rsp = nr->rsp;
0405 nr->cmd = NULL;
0406 nr->rsp = NULL;
0407 nr->used = false;
0408 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
0409 spin_unlock_irqrestore(&ndp->lock, flags);
0410
0411 if (driven && cmd && --ndp->pending_req_num == 0)
0412 schedule_work(&ndp->work);
0413
0414
0415 consume_skb(cmd);
0416 consume_skb(rsp);
0417 }
0418
0419 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
0420 {
0421 struct ncsi_dev_priv *ndp;
0422
0423 NCSI_FOR_EACH_DEV(ndp) {
0424 if (ndp->ndev.dev == dev)
0425 return &ndp->ndev;
0426 }
0427
0428 return NULL;
0429 }
0430
0431 static void ncsi_request_timeout(struct timer_list *t)
0432 {
0433 struct ncsi_request *nr = from_timer(nr, t, timer);
0434 struct ncsi_dev_priv *ndp = nr->ndp;
0435 struct ncsi_cmd_pkt *cmd;
0436 struct ncsi_package *np;
0437 struct ncsi_channel *nc;
0438 unsigned long flags;
0439
0440
0441
0442
0443 spin_lock_irqsave(&ndp->lock, flags);
0444 nr->enabled = false;
0445 if (nr->rsp || !nr->cmd) {
0446 spin_unlock_irqrestore(&ndp->lock, flags);
0447 return;
0448 }
0449 spin_unlock_irqrestore(&ndp->lock, flags);
0450
0451 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
0452 if (nr->cmd) {
0453
0454 cmd = (struct ncsi_cmd_pkt *)
0455 skb_network_header(nr->cmd);
0456 ncsi_find_package_and_channel(ndp,
0457 cmd->cmd.common.channel,
0458 &np, &nc);
0459 ncsi_send_netlink_timeout(nr, np, nc);
0460 }
0461 }
0462
0463
0464 ncsi_free_request(nr);
0465 }
0466
0467 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
0468 {
0469 struct ncsi_dev *nd = &ndp->ndev;
0470 struct ncsi_package *np;
0471 struct ncsi_channel *nc, *tmp;
0472 struct ncsi_cmd_arg nca;
0473 unsigned long flags;
0474 int ret;
0475
0476 np = ndp->active_package;
0477 nc = ndp->active_channel;
0478 nca.ndp = ndp;
0479 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
0480 switch (nd->state) {
0481 case ncsi_dev_state_suspend:
0482 nd->state = ncsi_dev_state_suspend_select;
0483 fallthrough;
0484 case ncsi_dev_state_suspend_select:
0485 ndp->pending_req_num = 1;
0486
0487 nca.type = NCSI_PKT_CMD_SP;
0488 nca.package = np->id;
0489 nca.channel = NCSI_RESERVED_CHANNEL;
0490 if (ndp->flags & NCSI_DEV_HWA)
0491 nca.bytes[0] = 0;
0492 else
0493 nca.bytes[0] = 1;
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 if (ndp->flags & NCSI_DEV_RESHUFFLE)
0504 nd->state = ncsi_dev_state_suspend_gls;
0505 else
0506 nd->state = ncsi_dev_state_suspend_dcnt;
0507 ret = ncsi_xmit_cmd(&nca);
0508 if (ret)
0509 goto error;
0510
0511 break;
0512 case ncsi_dev_state_suspend_gls:
0513 ndp->pending_req_num = np->channel_num;
0514
0515 nca.type = NCSI_PKT_CMD_GLS;
0516 nca.package = np->id;
0517
0518 nd->state = ncsi_dev_state_suspend_dcnt;
0519 NCSI_FOR_EACH_CHANNEL(np, nc) {
0520 nca.channel = nc->id;
0521 ret = ncsi_xmit_cmd(&nca);
0522 if (ret)
0523 goto error;
0524 }
0525
0526 break;
0527 case ncsi_dev_state_suspend_dcnt:
0528 ndp->pending_req_num = 1;
0529
0530 nca.type = NCSI_PKT_CMD_DCNT;
0531 nca.package = np->id;
0532 nca.channel = nc->id;
0533
0534 nd->state = ncsi_dev_state_suspend_dc;
0535 ret = ncsi_xmit_cmd(&nca);
0536 if (ret)
0537 goto error;
0538
0539 break;
0540 case ncsi_dev_state_suspend_dc:
0541 ndp->pending_req_num = 1;
0542
0543 nca.type = NCSI_PKT_CMD_DC;
0544 nca.package = np->id;
0545 nca.channel = nc->id;
0546 nca.bytes[0] = 1;
0547
0548 nd->state = ncsi_dev_state_suspend_deselect;
0549 ret = ncsi_xmit_cmd(&nca);
0550 if (ret)
0551 goto error;
0552
0553 NCSI_FOR_EACH_CHANNEL(np, tmp) {
0554
0555
0556
0557 if (tmp != nc && tmp->state == NCSI_CHANNEL_ACTIVE) {
0558 nd->state = ncsi_dev_state_suspend_done;
0559 break;
0560 }
0561 }
0562 break;
0563 case ncsi_dev_state_suspend_deselect:
0564 ndp->pending_req_num = 1;
0565
0566 nca.type = NCSI_PKT_CMD_DP;
0567 nca.package = np->id;
0568 nca.channel = NCSI_RESERVED_CHANNEL;
0569
0570 nd->state = ncsi_dev_state_suspend_done;
0571 ret = ncsi_xmit_cmd(&nca);
0572 if (ret)
0573 goto error;
0574
0575 break;
0576 case ncsi_dev_state_suspend_done:
0577 spin_lock_irqsave(&nc->lock, flags);
0578 nc->state = NCSI_CHANNEL_INACTIVE;
0579 spin_unlock_irqrestore(&nc->lock, flags);
0580 if (ndp->flags & NCSI_DEV_RESET)
0581 ncsi_reset_dev(nd);
0582 else
0583 ncsi_process_next_channel(ndp);
0584 break;
0585 default:
0586 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
0587 nd->state);
0588 }
0589
0590 return;
0591 error:
0592 nd->state = ncsi_dev_state_functional;
0593 }
0594
0595
0596
0597
0598 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
0599 struct ncsi_cmd_arg *nca)
0600 {
0601 struct ncsi_channel_vlan_filter *ncf;
0602 unsigned long flags;
0603 void *bitmap;
0604 int index;
0605 u16 vid;
0606
0607 ncf = &nc->vlan_filter;
0608 bitmap = &ncf->bitmap;
0609
0610 spin_lock_irqsave(&nc->lock, flags);
0611 index = find_first_bit(bitmap, ncf->n_vids);
0612 if (index >= ncf->n_vids) {
0613 spin_unlock_irqrestore(&nc->lock, flags);
0614 return -1;
0615 }
0616 vid = ncf->vids[index];
0617
0618 clear_bit(index, bitmap);
0619 ncf->vids[index] = 0;
0620 spin_unlock_irqrestore(&nc->lock, flags);
0621
0622 nca->type = NCSI_PKT_CMD_SVF;
0623 nca->words[1] = vid;
0624
0625 nca->bytes[6] = index + 1;
0626 nca->bytes[7] = 0x00;
0627 return 0;
0628 }
0629
0630
0631
0632
0633 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
0634 struct ncsi_cmd_arg *nca)
0635 {
0636 struct ncsi_channel_vlan_filter *ncf;
0637 struct vlan_vid *vlan = NULL;
0638 unsigned long flags;
0639 int i, index;
0640 void *bitmap;
0641 u16 vid;
0642
0643 if (list_empty(&ndp->vlan_vids))
0644 return -1;
0645
0646 ncf = &nc->vlan_filter;
0647 bitmap = &ncf->bitmap;
0648
0649 spin_lock_irqsave(&nc->lock, flags);
0650
0651 rcu_read_lock();
0652 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
0653 vid = vlan->vid;
0654 for (i = 0; i < ncf->n_vids; i++)
0655 if (ncf->vids[i] == vid) {
0656 vid = 0;
0657 break;
0658 }
0659 if (vid)
0660 break;
0661 }
0662 rcu_read_unlock();
0663
0664 if (!vid) {
0665
0666 spin_unlock_irqrestore(&nc->lock, flags);
0667 return -1;
0668 }
0669
0670 index = find_first_zero_bit(bitmap, ncf->n_vids);
0671 if (index < 0 || index >= ncf->n_vids) {
0672 netdev_err(ndp->ndev.dev,
0673 "Channel %u already has all VLAN filters set\n",
0674 nc->id);
0675 spin_unlock_irqrestore(&nc->lock, flags);
0676 return -1;
0677 }
0678
0679 ncf->vids[index] = vid;
0680 set_bit(index, bitmap);
0681 spin_unlock_irqrestore(&nc->lock, flags);
0682
0683 nca->type = NCSI_PKT_CMD_SVF;
0684 nca->words[1] = vid;
0685
0686 nca->bytes[6] = index + 1;
0687 nca->bytes[7] = 0x01;
0688
0689 return 0;
0690 }
0691
0692 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY)
0693
0694 static int ncsi_oem_keep_phy_intel(struct ncsi_cmd_arg *nca)
0695 {
0696 unsigned char data[NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN];
0697 int ret = 0;
0698
0699 nca->payload = NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN;
0700
0701 memset(data, 0, NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN);
0702 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_INTEL_ID);
0703
0704 data[4] = NCSI_OEM_INTEL_CMD_KEEP_PHY;
0705
0706
0707 data[6] = 0x1;
0708
0709 nca->data = data;
0710
0711 ret = ncsi_xmit_cmd(nca);
0712 if (ret)
0713 netdev_err(nca->ndp->ndev.dev,
0714 "NCSI: Failed to transmit cmd 0x%x during configure\n",
0715 nca->type);
0716 return ret;
0717 }
0718
0719 #endif
0720
0721 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
0722
0723
0724 static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca)
0725 {
0726 unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN];
0727 int ret = 0;
0728
0729 nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN;
0730
0731 memset(data, 0, NCSI_OEM_BCM_CMD_GMA_LEN);
0732 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_BCM_ID);
0733 data[5] = NCSI_OEM_BCM_CMD_GMA;
0734
0735 nca->data = data;
0736
0737 ret = ncsi_xmit_cmd(nca);
0738 if (ret)
0739 netdev_err(nca->ndp->ndev.dev,
0740 "NCSI: Failed to transmit cmd 0x%x during configure\n",
0741 nca->type);
0742 return ret;
0743 }
0744
0745 static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg *nca)
0746 {
0747 union {
0748 u8 data_u8[NCSI_OEM_MLX_CMD_GMA_LEN];
0749 u32 data_u32[NCSI_OEM_MLX_CMD_GMA_LEN / sizeof(u32)];
0750 } u;
0751 int ret = 0;
0752
0753 nca->payload = NCSI_OEM_MLX_CMD_GMA_LEN;
0754
0755 memset(&u, 0, sizeof(u));
0756 u.data_u32[0] = ntohl((__force __be32)NCSI_OEM_MFR_MLX_ID);
0757 u.data_u8[5] = NCSI_OEM_MLX_CMD_GMA;
0758 u.data_u8[6] = NCSI_OEM_MLX_CMD_GMA_PARAM;
0759
0760 nca->data = u.data_u8;
0761
0762 ret = ncsi_xmit_cmd(nca);
0763 if (ret)
0764 netdev_err(nca->ndp->ndev.dev,
0765 "NCSI: Failed to transmit cmd 0x%x during configure\n",
0766 nca->type);
0767 return ret;
0768 }
0769
0770 static int ncsi_oem_smaf_mlx(struct ncsi_cmd_arg *nca)
0771 {
0772 union {
0773 u8 data_u8[NCSI_OEM_MLX_CMD_SMAF_LEN];
0774 u32 data_u32[NCSI_OEM_MLX_CMD_SMAF_LEN / sizeof(u32)];
0775 } u;
0776 int ret = 0;
0777
0778 memset(&u, 0, sizeof(u));
0779 u.data_u32[0] = ntohl((__force __be32)NCSI_OEM_MFR_MLX_ID);
0780 u.data_u8[5] = NCSI_OEM_MLX_CMD_SMAF;
0781 u.data_u8[6] = NCSI_OEM_MLX_CMD_SMAF_PARAM;
0782 memcpy(&u.data_u8[MLX_SMAF_MAC_ADDR_OFFSET],
0783 nca->ndp->ndev.dev->dev_addr, ETH_ALEN);
0784 u.data_u8[MLX_SMAF_MED_SUPPORT_OFFSET] =
0785 (MLX_MC_RBT_AVL | MLX_MC_RBT_SUPPORT);
0786
0787 nca->payload = NCSI_OEM_MLX_CMD_SMAF_LEN;
0788 nca->data = u.data_u8;
0789
0790 ret = ncsi_xmit_cmd(nca);
0791 if (ret)
0792 netdev_err(nca->ndp->ndev.dev,
0793 "NCSI: Failed to transmit cmd 0x%x during probe\n",
0794 nca->type);
0795 return ret;
0796 }
0797
0798 static int ncsi_oem_gma_handler_intel(struct ncsi_cmd_arg *nca)
0799 {
0800 unsigned char data[NCSI_OEM_INTEL_CMD_GMA_LEN];
0801 int ret = 0;
0802
0803 nca->payload = NCSI_OEM_INTEL_CMD_GMA_LEN;
0804
0805 memset(data, 0, NCSI_OEM_INTEL_CMD_GMA_LEN);
0806 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_INTEL_ID);
0807 data[4] = NCSI_OEM_INTEL_CMD_GMA;
0808
0809 nca->data = data;
0810
0811 ret = ncsi_xmit_cmd(nca);
0812 if (ret)
0813 netdev_err(nca->ndp->ndev.dev,
0814 "NCSI: Failed to transmit cmd 0x%x during configure\n",
0815 nca->type);
0816
0817 return ret;
0818 }
0819
0820
0821 static struct ncsi_oem_gma_handler {
0822 unsigned int mfr_id;
0823 int (*handler)(struct ncsi_cmd_arg *nca);
0824 } ncsi_oem_gma_handlers[] = {
0825 { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm },
0826 { NCSI_OEM_MFR_MLX_ID, ncsi_oem_gma_handler_mlx },
0827 { NCSI_OEM_MFR_INTEL_ID, ncsi_oem_gma_handler_intel }
0828 };
0829
0830 static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id)
0831 {
0832 struct ncsi_oem_gma_handler *nch = NULL;
0833 int i;
0834
0835
0836 if (nca->ndp->gma_flag == 1)
0837 return -1;
0838
0839
0840 for (i = 0; i < ARRAY_SIZE(ncsi_oem_gma_handlers); i++) {
0841 if (ncsi_oem_gma_handlers[i].mfr_id == mf_id) {
0842 if (ncsi_oem_gma_handlers[i].handler)
0843 nch = &ncsi_oem_gma_handlers[i];
0844 break;
0845 }
0846 }
0847
0848 if (!nch) {
0849 netdev_err(nca->ndp->ndev.dev,
0850 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
0851 mf_id);
0852 return -1;
0853 }
0854
0855
0856 return nch->handler(nca);
0857 }
0858
0859 #endif
0860
0861
0862 static bool ncsi_channel_is_tx(struct ncsi_dev_priv *ndp,
0863 struct ncsi_channel *nc)
0864 {
0865 struct ncsi_channel_mode *ncm;
0866 struct ncsi_channel *channel;
0867 struct ncsi_package *np;
0868
0869
0870
0871
0872 NCSI_FOR_EACH_PACKAGE(ndp, np) {
0873 if (!ndp->multi_package && np != nc->package)
0874 continue;
0875 NCSI_FOR_EACH_CHANNEL(np, channel) {
0876 ncm = &channel->modes[NCSI_MODE_TX_ENABLE];
0877 if (ncm->enable)
0878 return false;
0879 }
0880 }
0881
0882
0883 list_for_each_entry_rcu(channel, &ndp->channel_queue, link) {
0884 np = channel->package;
0885 if (np->preferred_channel &&
0886 ncsi_channel_has_link(np->preferred_channel)) {
0887 return np->preferred_channel == nc;
0888 }
0889 }
0890
0891
0892 if (ncsi_channel_has_link(nc))
0893 return true;
0894
0895 list_for_each_entry_rcu(channel, &ndp->channel_queue, link)
0896 if (ncsi_channel_has_link(channel))
0897 return false;
0898
0899
0900 return true;
0901 }
0902
0903
0904 int ncsi_update_tx_channel(struct ncsi_dev_priv *ndp,
0905 struct ncsi_package *package,
0906 struct ncsi_channel *disable,
0907 struct ncsi_channel *enable)
0908 {
0909 struct ncsi_cmd_arg nca;
0910 struct ncsi_channel *nc;
0911 struct ncsi_package *np;
0912 int ret = 0;
0913
0914 if (!package->multi_channel && !ndp->multi_package)
0915 netdev_warn(ndp->ndev.dev,
0916 "NCSI: Trying to update Tx channel in single-channel mode\n");
0917 nca.ndp = ndp;
0918 nca.req_flags = 0;
0919
0920
0921 NCSI_FOR_EACH_PACKAGE(ndp, np) {
0922 if (disable)
0923 break;
0924 if (!ndp->multi_package && np != package)
0925 continue;
0926
0927 NCSI_FOR_EACH_CHANNEL(np, nc)
0928 if (nc->modes[NCSI_MODE_TX_ENABLE].enable) {
0929 disable = nc;
0930 break;
0931 }
0932 }
0933
0934
0935 NCSI_FOR_EACH_PACKAGE(ndp, np) {
0936 if (enable)
0937 break;
0938 if (!ndp->multi_package && np != package)
0939 continue;
0940 if (!(ndp->package_whitelist & (0x1 << np->id)))
0941 continue;
0942
0943 if (np->preferred_channel &&
0944 ncsi_channel_has_link(np->preferred_channel)) {
0945 enable = np->preferred_channel;
0946 break;
0947 }
0948
0949 NCSI_FOR_EACH_CHANNEL(np, nc) {
0950 if (!(np->channel_whitelist & 0x1 << nc->id))
0951 continue;
0952 if (nc->state != NCSI_CHANNEL_ACTIVE)
0953 continue;
0954 if (ncsi_channel_has_link(nc)) {
0955 enable = nc;
0956 break;
0957 }
0958 }
0959 }
0960
0961 if (disable == enable)
0962 return -1;
0963
0964 if (!enable)
0965 return -1;
0966
0967 if (disable) {
0968 nca.channel = disable->id;
0969 nca.package = disable->package->id;
0970 nca.type = NCSI_PKT_CMD_DCNT;
0971 ret = ncsi_xmit_cmd(&nca);
0972 if (ret)
0973 netdev_err(ndp->ndev.dev,
0974 "Error %d sending DCNT\n",
0975 ret);
0976 }
0977
0978 netdev_info(ndp->ndev.dev, "NCSI: channel %u enables Tx\n", enable->id);
0979
0980 nca.channel = enable->id;
0981 nca.package = enable->package->id;
0982 nca.type = NCSI_PKT_CMD_ECNT;
0983 ret = ncsi_xmit_cmd(&nca);
0984 if (ret)
0985 netdev_err(ndp->ndev.dev,
0986 "Error %d sending ECNT\n",
0987 ret);
0988
0989 return ret;
0990 }
0991
0992 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
0993 {
0994 struct ncsi_package *np = ndp->active_package;
0995 struct ncsi_channel *nc = ndp->active_channel;
0996 struct ncsi_channel *hot_nc = NULL;
0997 struct ncsi_dev *nd = &ndp->ndev;
0998 struct net_device *dev = nd->dev;
0999 struct ncsi_cmd_arg nca;
1000 unsigned char index;
1001 unsigned long flags;
1002 int ret;
1003
1004 nca.ndp = ndp;
1005 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1006 switch (nd->state) {
1007 case ncsi_dev_state_config:
1008 case ncsi_dev_state_config_sp:
1009 ndp->pending_req_num = 1;
1010
1011
1012 nca.type = NCSI_PKT_CMD_SP;
1013 if (ndp->flags & NCSI_DEV_HWA)
1014 nca.bytes[0] = 0;
1015 else
1016 nca.bytes[0] = 1;
1017 nca.package = np->id;
1018 nca.channel = NCSI_RESERVED_CHANNEL;
1019 ret = ncsi_xmit_cmd(&nca);
1020 if (ret) {
1021 netdev_err(ndp->ndev.dev,
1022 "NCSI: Failed to transmit CMD_SP\n");
1023 goto error;
1024 }
1025
1026 nd->state = ncsi_dev_state_config_cis;
1027 break;
1028 case ncsi_dev_state_config_cis:
1029 ndp->pending_req_num = 1;
1030
1031
1032 nca.type = NCSI_PKT_CMD_CIS;
1033 nca.package = np->id;
1034 nca.channel = nc->id;
1035 ret = ncsi_xmit_cmd(&nca);
1036 if (ret) {
1037 netdev_err(ndp->ndev.dev,
1038 "NCSI: Failed to transmit CMD_CIS\n");
1039 goto error;
1040 }
1041
1042 nd->state = ncsi_dev_state_config_oem_gma;
1043 break;
1044 case ncsi_dev_state_config_oem_gma:
1045 nd->state = ncsi_dev_state_config_clear_vids;
1046 ret = -1;
1047
1048 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
1049 nca.type = NCSI_PKT_CMD_OEM;
1050 nca.package = np->id;
1051 nca.channel = nc->id;
1052 ndp->pending_req_num = 1;
1053 ret = ncsi_gma_handler(&nca, nc->version.mf_id);
1054 #endif
1055
1056 if (ret < 0)
1057 schedule_work(&ndp->work);
1058
1059 break;
1060 case ncsi_dev_state_config_clear_vids:
1061 case ncsi_dev_state_config_svf:
1062 case ncsi_dev_state_config_ev:
1063 case ncsi_dev_state_config_sma:
1064 case ncsi_dev_state_config_ebf:
1065 case ncsi_dev_state_config_dgmf:
1066 case ncsi_dev_state_config_ecnt:
1067 case ncsi_dev_state_config_ec:
1068 case ncsi_dev_state_config_ae:
1069 case ncsi_dev_state_config_gls:
1070 ndp->pending_req_num = 1;
1071
1072 nca.package = np->id;
1073 nca.channel = nc->id;
1074
1075
1076 if (nd->state == ncsi_dev_state_config_clear_vids) {
1077 ret = clear_one_vid(ndp, nc, &nca);
1078 if (ret) {
1079 nd->state = ncsi_dev_state_config_svf;
1080 schedule_work(&ndp->work);
1081 break;
1082 }
1083
1084 nd->state = ncsi_dev_state_config_clear_vids;
1085
1086 } else if (nd->state == ncsi_dev_state_config_svf) {
1087 ret = set_one_vid(ndp, nc, &nca);
1088 if (ret) {
1089 nd->state = ncsi_dev_state_config_ev;
1090 schedule_work(&ndp->work);
1091 break;
1092 }
1093
1094 nd->state = ncsi_dev_state_config_svf;
1095
1096 } else if (nd->state == ncsi_dev_state_config_ev) {
1097 if (list_empty(&ndp->vlan_vids)) {
1098 nca.type = NCSI_PKT_CMD_DV;
1099 } else {
1100 nca.type = NCSI_PKT_CMD_EV;
1101 nca.bytes[3] = NCSI_CAP_VLAN_NO;
1102 }
1103 nd->state = ncsi_dev_state_config_sma;
1104 } else if (nd->state == ncsi_dev_state_config_sma) {
1105
1106
1107
1108
1109 nca.type = NCSI_PKT_CMD_SMA;
1110 for (index = 0; index < 6; index++)
1111 nca.bytes[index] = dev->dev_addr[index];
1112 nca.bytes[6] = 0x1;
1113 nca.bytes[7] = 0x1;
1114 nd->state = ncsi_dev_state_config_ebf;
1115 } else if (nd->state == ncsi_dev_state_config_ebf) {
1116 nca.type = NCSI_PKT_CMD_EBF;
1117 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
1118
1119
1120
1121
1122 if (nc->caps[NCSI_CAP_GENERIC].cap &
1123 NCSI_CAP_GENERIC_MC)
1124 nd->state = ncsi_dev_state_config_dgmf;
1125 else if (ncsi_channel_is_tx(ndp, nc))
1126 nd->state = ncsi_dev_state_config_ecnt;
1127 else
1128 nd->state = ncsi_dev_state_config_ec;
1129 } else if (nd->state == ncsi_dev_state_config_dgmf) {
1130 nca.type = NCSI_PKT_CMD_DGMF;
1131 if (ncsi_channel_is_tx(ndp, nc))
1132 nd->state = ncsi_dev_state_config_ecnt;
1133 else
1134 nd->state = ncsi_dev_state_config_ec;
1135 } else if (nd->state == ncsi_dev_state_config_ecnt) {
1136 if (np->preferred_channel &&
1137 nc != np->preferred_channel)
1138 netdev_info(ndp->ndev.dev,
1139 "NCSI: Tx failed over to channel %u\n",
1140 nc->id);
1141 nca.type = NCSI_PKT_CMD_ECNT;
1142 nd->state = ncsi_dev_state_config_ec;
1143 } else if (nd->state == ncsi_dev_state_config_ec) {
1144
1145 nca.type = NCSI_PKT_CMD_EC;
1146 nd->state = ncsi_dev_state_config_ae;
1147 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
1148 nd->state = ncsi_dev_state_config_gls;
1149 } else if (nd->state == ncsi_dev_state_config_ae) {
1150 nca.type = NCSI_PKT_CMD_AE;
1151 nca.bytes[0] = 0;
1152 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
1153 nd->state = ncsi_dev_state_config_gls;
1154 } else if (nd->state == ncsi_dev_state_config_gls) {
1155 nca.type = NCSI_PKT_CMD_GLS;
1156 nd->state = ncsi_dev_state_config_done;
1157 }
1158
1159 ret = ncsi_xmit_cmd(&nca);
1160 if (ret) {
1161 netdev_err(ndp->ndev.dev,
1162 "NCSI: Failed to transmit CMD %x\n",
1163 nca.type);
1164 goto error;
1165 }
1166 break;
1167 case ncsi_dev_state_config_done:
1168 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
1169 nc->id);
1170 spin_lock_irqsave(&nc->lock, flags);
1171 nc->state = NCSI_CHANNEL_ACTIVE;
1172
1173 if (ndp->flags & NCSI_DEV_RESET) {
1174
1175 nc->reconfigure_needed = false;
1176 spin_unlock_irqrestore(&nc->lock, flags);
1177 ncsi_reset_dev(nd);
1178 break;
1179 }
1180
1181 if (nc->reconfigure_needed) {
1182
1183
1184
1185
1186 nc->reconfigure_needed = false;
1187 nc->state = NCSI_CHANNEL_INACTIVE;
1188 spin_unlock_irqrestore(&nc->lock, flags);
1189
1190 spin_lock_irqsave(&ndp->lock, flags);
1191 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1192 spin_unlock_irqrestore(&ndp->lock, flags);
1193
1194 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
1195 ncsi_process_next_channel(ndp);
1196 break;
1197 }
1198
1199 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
1200 hot_nc = nc;
1201 } else {
1202 hot_nc = NULL;
1203 netdev_dbg(ndp->ndev.dev,
1204 "NCSI: channel %u link down after config\n",
1205 nc->id);
1206 }
1207 spin_unlock_irqrestore(&nc->lock, flags);
1208
1209
1210 spin_lock_irqsave(&ndp->lock, flags);
1211 ndp->hot_channel = hot_nc;
1212 spin_unlock_irqrestore(&ndp->lock, flags);
1213
1214 ncsi_start_channel_monitor(nc);
1215 ncsi_process_next_channel(ndp);
1216 break;
1217 default:
1218 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
1219 nd->state);
1220 }
1221
1222 return;
1223
1224 error:
1225 ncsi_report_link(ndp, true);
1226 }
1227
1228 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
1229 {
1230 struct ncsi_channel *nc, *found, *hot_nc;
1231 struct ncsi_channel_mode *ncm;
1232 unsigned long flags, cflags;
1233 struct ncsi_package *np;
1234 bool with_link;
1235
1236 spin_lock_irqsave(&ndp->lock, flags);
1237 hot_nc = ndp->hot_channel;
1238 spin_unlock_irqrestore(&ndp->lock, flags);
1239
1240
1241
1242
1243
1244
1245 found = NULL;
1246 with_link = false;
1247 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1248 if (!(ndp->package_whitelist & (0x1 << np->id)))
1249 continue;
1250 NCSI_FOR_EACH_CHANNEL(np, nc) {
1251 if (!(np->channel_whitelist & (0x1 << nc->id)))
1252 continue;
1253
1254 spin_lock_irqsave(&nc->lock, cflags);
1255
1256 if (!list_empty(&nc->link) ||
1257 nc->state != NCSI_CHANNEL_INACTIVE) {
1258 spin_unlock_irqrestore(&nc->lock, cflags);
1259 continue;
1260 }
1261
1262 if (!found)
1263 found = nc;
1264
1265 if (nc == hot_nc)
1266 found = nc;
1267
1268 ncm = &nc->modes[NCSI_MODE_LINK];
1269 if (ncm->data[2] & 0x1) {
1270 found = nc;
1271 with_link = true;
1272 }
1273
1274
1275
1276
1277
1278 if (with_link || np->multi_channel) {
1279 spin_lock_irqsave(&ndp->lock, flags);
1280 list_add_tail_rcu(&nc->link,
1281 &ndp->channel_queue);
1282 spin_unlock_irqrestore(&ndp->lock, flags);
1283
1284 netdev_dbg(ndp->ndev.dev,
1285 "NCSI: Channel %u added to queue (link %s)\n",
1286 nc->id,
1287 ncm->data[2] & 0x1 ? "up" : "down");
1288 }
1289
1290 spin_unlock_irqrestore(&nc->lock, cflags);
1291
1292 if (with_link && !np->multi_channel)
1293 break;
1294 }
1295 if (with_link && !ndp->multi_package)
1296 break;
1297 }
1298
1299 if (list_empty(&ndp->channel_queue) && found) {
1300 netdev_info(ndp->ndev.dev,
1301 "NCSI: No channel with link found, configuring channel %u\n",
1302 found->id);
1303 spin_lock_irqsave(&ndp->lock, flags);
1304 list_add_tail_rcu(&found->link, &ndp->channel_queue);
1305 spin_unlock_irqrestore(&ndp->lock, flags);
1306 } else if (!found) {
1307 netdev_warn(ndp->ndev.dev,
1308 "NCSI: No channel found to configure!\n");
1309 ncsi_report_link(ndp, true);
1310 return -ENODEV;
1311 }
1312
1313 return ncsi_process_next_channel(ndp);
1314 }
1315
1316 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
1317 {
1318 struct ncsi_package *np;
1319 struct ncsi_channel *nc;
1320 unsigned int cap;
1321 bool has_channel = false;
1322
1323
1324
1325
1326 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1327 NCSI_FOR_EACH_CHANNEL(np, nc) {
1328 has_channel = true;
1329
1330 cap = nc->caps[NCSI_CAP_GENERIC].cap;
1331 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
1332 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
1333 NCSI_CAP_GENERIC_HWA_SUPPORT) {
1334 ndp->flags &= ~NCSI_DEV_HWA;
1335 return false;
1336 }
1337 }
1338 }
1339
1340 if (has_channel) {
1341 ndp->flags |= NCSI_DEV_HWA;
1342 return true;
1343 }
1344
1345 ndp->flags &= ~NCSI_DEV_HWA;
1346 return false;
1347 }
1348
1349 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1350 {
1351 struct ncsi_dev *nd = &ndp->ndev;
1352 struct ncsi_package *np;
1353 struct ncsi_channel *nc;
1354 struct ncsi_cmd_arg nca;
1355 unsigned char index;
1356 int ret;
1357
1358 nca.ndp = ndp;
1359 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1360 switch (nd->state) {
1361 case ncsi_dev_state_probe:
1362 nd->state = ncsi_dev_state_probe_deselect;
1363 fallthrough;
1364 case ncsi_dev_state_probe_deselect:
1365 ndp->pending_req_num = 8;
1366
1367
1368 nca.type = NCSI_PKT_CMD_DP;
1369 nca.channel = NCSI_RESERVED_CHANNEL;
1370 for (index = 0; index < 8; index++) {
1371 nca.package = index;
1372 ret = ncsi_xmit_cmd(&nca);
1373 if (ret)
1374 goto error;
1375 }
1376
1377 nd->state = ncsi_dev_state_probe_package;
1378 break;
1379 case ncsi_dev_state_probe_package:
1380 ndp->pending_req_num = 1;
1381
1382 nca.type = NCSI_PKT_CMD_SP;
1383 nca.bytes[0] = 1;
1384 nca.package = ndp->package_probe_id;
1385 nca.channel = NCSI_RESERVED_CHANNEL;
1386 ret = ncsi_xmit_cmd(&nca);
1387 if (ret)
1388 goto error;
1389 nd->state = ncsi_dev_state_probe_channel;
1390 break;
1391 case ncsi_dev_state_probe_channel:
1392 ndp->active_package = ncsi_find_package(ndp,
1393 ndp->package_probe_id);
1394 if (!ndp->active_package) {
1395
1396 nd->state = ncsi_dev_state_probe_dp;
1397 schedule_work(&ndp->work);
1398 break;
1399 }
1400 nd->state = ncsi_dev_state_probe_cis;
1401 if (IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC) &&
1402 ndp->mlx_multi_host)
1403 nd->state = ncsi_dev_state_probe_mlx_gma;
1404
1405 schedule_work(&ndp->work);
1406 break;
1407 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
1408 case ncsi_dev_state_probe_mlx_gma:
1409 ndp->pending_req_num = 1;
1410
1411 nca.type = NCSI_PKT_CMD_OEM;
1412 nca.package = ndp->active_package->id;
1413 nca.channel = 0;
1414 ret = ncsi_oem_gma_handler_mlx(&nca);
1415 if (ret)
1416 goto error;
1417
1418 nd->state = ncsi_dev_state_probe_mlx_smaf;
1419 break;
1420 case ncsi_dev_state_probe_mlx_smaf:
1421 ndp->pending_req_num = 1;
1422
1423 nca.type = NCSI_PKT_CMD_OEM;
1424 nca.package = ndp->active_package->id;
1425 nca.channel = 0;
1426 ret = ncsi_oem_smaf_mlx(&nca);
1427 if (ret)
1428 goto error;
1429
1430 nd->state = ncsi_dev_state_probe_cis;
1431 break;
1432 #endif
1433 case ncsi_dev_state_probe_cis:
1434 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1435
1436
1437 nca.type = NCSI_PKT_CMD_CIS;
1438 nca.package = ndp->active_package->id;
1439 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
1440 nca.channel = index;
1441 ret = ncsi_xmit_cmd(&nca);
1442 if (ret)
1443 goto error;
1444 }
1445
1446 nd->state = ncsi_dev_state_probe_gvi;
1447 if (IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY))
1448 nd->state = ncsi_dev_state_probe_keep_phy;
1449 break;
1450 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY)
1451 case ncsi_dev_state_probe_keep_phy:
1452 ndp->pending_req_num = 1;
1453
1454 nca.type = NCSI_PKT_CMD_OEM;
1455 nca.package = ndp->active_package->id;
1456 nca.channel = 0;
1457 ret = ncsi_oem_keep_phy_intel(&nca);
1458 if (ret)
1459 goto error;
1460
1461 nd->state = ncsi_dev_state_probe_gvi;
1462 break;
1463 #endif
1464 case ncsi_dev_state_probe_gvi:
1465 case ncsi_dev_state_probe_gc:
1466 case ncsi_dev_state_probe_gls:
1467 np = ndp->active_package;
1468 ndp->pending_req_num = np->channel_num;
1469
1470
1471 if (nd->state == ncsi_dev_state_probe_gvi)
1472 nca.type = NCSI_PKT_CMD_GVI;
1473 else if (nd->state == ncsi_dev_state_probe_gc)
1474 nca.type = NCSI_PKT_CMD_GC;
1475 else
1476 nca.type = NCSI_PKT_CMD_GLS;
1477
1478 nca.package = np->id;
1479 NCSI_FOR_EACH_CHANNEL(np, nc) {
1480 nca.channel = nc->id;
1481 ret = ncsi_xmit_cmd(&nca);
1482 if (ret)
1483 goto error;
1484 }
1485
1486 if (nd->state == ncsi_dev_state_probe_gvi)
1487 nd->state = ncsi_dev_state_probe_gc;
1488 else if (nd->state == ncsi_dev_state_probe_gc)
1489 nd->state = ncsi_dev_state_probe_gls;
1490 else
1491 nd->state = ncsi_dev_state_probe_dp;
1492 break;
1493 case ncsi_dev_state_probe_dp:
1494 ndp->pending_req_num = 1;
1495
1496
1497 nca.type = NCSI_PKT_CMD_DP;
1498 nca.package = ndp->package_probe_id;
1499 nca.channel = NCSI_RESERVED_CHANNEL;
1500 ret = ncsi_xmit_cmd(&nca);
1501 if (ret)
1502 goto error;
1503
1504
1505 ndp->package_probe_id++;
1506 if (ndp->package_probe_id >= 8) {
1507
1508 ndp->flags |= NCSI_DEV_PROBED;
1509 break;
1510 }
1511 nd->state = ncsi_dev_state_probe_package;
1512 ndp->active_package = NULL;
1513 break;
1514 default:
1515 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1516 nd->state);
1517 }
1518
1519 if (ndp->flags & NCSI_DEV_PROBED) {
1520
1521 ncsi_check_hwa(ndp);
1522 ncsi_choose_active_channel(ndp);
1523 }
1524
1525 return;
1526 error:
1527 netdev_err(ndp->ndev.dev,
1528 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1529 nca.type);
1530 ncsi_report_link(ndp, true);
1531 }
1532
1533 static void ncsi_dev_work(struct work_struct *work)
1534 {
1535 struct ncsi_dev_priv *ndp = container_of(work,
1536 struct ncsi_dev_priv, work);
1537 struct ncsi_dev *nd = &ndp->ndev;
1538
1539 switch (nd->state & ncsi_dev_state_major) {
1540 case ncsi_dev_state_probe:
1541 ncsi_probe_channel(ndp);
1542 break;
1543 case ncsi_dev_state_suspend:
1544 ncsi_suspend_channel(ndp);
1545 break;
1546 case ncsi_dev_state_config:
1547 ncsi_configure_channel(ndp);
1548 break;
1549 default:
1550 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1551 nd->state);
1552 }
1553 }
1554
1555 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1556 {
1557 struct ncsi_channel *nc;
1558 int old_state;
1559 unsigned long flags;
1560
1561 spin_lock_irqsave(&ndp->lock, flags);
1562 nc = list_first_or_null_rcu(&ndp->channel_queue,
1563 struct ncsi_channel, link);
1564 if (!nc) {
1565 spin_unlock_irqrestore(&ndp->lock, flags);
1566 goto out;
1567 }
1568
1569 list_del_init(&nc->link);
1570 spin_unlock_irqrestore(&ndp->lock, flags);
1571
1572 spin_lock_irqsave(&nc->lock, flags);
1573 old_state = nc->state;
1574 nc->state = NCSI_CHANNEL_INVISIBLE;
1575 spin_unlock_irqrestore(&nc->lock, flags);
1576
1577 ndp->active_channel = nc;
1578 ndp->active_package = nc->package;
1579
1580 switch (old_state) {
1581 case NCSI_CHANNEL_INACTIVE:
1582 ndp->ndev.state = ncsi_dev_state_config;
1583 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1584 nc->id);
1585 ncsi_configure_channel(ndp);
1586 break;
1587 case NCSI_CHANNEL_ACTIVE:
1588 ndp->ndev.state = ncsi_dev_state_suspend;
1589 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1590 nc->id);
1591 ncsi_suspend_channel(ndp);
1592 break;
1593 default:
1594 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1595 old_state, nc->package->id, nc->id);
1596 ncsi_report_link(ndp, false);
1597 return -EINVAL;
1598 }
1599
1600 return 0;
1601
1602 out:
1603 ndp->active_channel = NULL;
1604 ndp->active_package = NULL;
1605 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1606 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1607 return ncsi_choose_active_channel(ndp);
1608 }
1609
1610 ncsi_report_link(ndp, false);
1611 return -ENODEV;
1612 }
1613
1614 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1615 {
1616 struct ncsi_dev *nd = &ndp->ndev;
1617 struct ncsi_channel *nc;
1618 struct ncsi_package *np;
1619 unsigned long flags;
1620 unsigned int n = 0;
1621
1622 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1623 NCSI_FOR_EACH_CHANNEL(np, nc) {
1624 spin_lock_irqsave(&nc->lock, flags);
1625
1626
1627
1628
1629
1630
1631
1632 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1633 if ((ndp->ndev.state & 0xff00) ==
1634 ncsi_dev_state_config ||
1635 !list_empty(&nc->link)) {
1636 netdev_dbg(nd->dev,
1637 "NCSI: channel %p marked dirty\n",
1638 nc);
1639 nc->reconfigure_needed = true;
1640 }
1641 spin_unlock_irqrestore(&nc->lock, flags);
1642 continue;
1643 }
1644
1645 spin_unlock_irqrestore(&nc->lock, flags);
1646
1647 ncsi_stop_channel_monitor(nc);
1648 spin_lock_irqsave(&nc->lock, flags);
1649 nc->state = NCSI_CHANNEL_INACTIVE;
1650 spin_unlock_irqrestore(&nc->lock, flags);
1651
1652 spin_lock_irqsave(&ndp->lock, flags);
1653 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1654 spin_unlock_irqrestore(&ndp->lock, flags);
1655
1656 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
1657 n++;
1658 }
1659 }
1660
1661 return n;
1662 }
1663
1664 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1665 {
1666 struct ncsi_dev_priv *ndp;
1667 unsigned int n_vids = 0;
1668 struct vlan_vid *vlan;
1669 struct ncsi_dev *nd;
1670 bool found = false;
1671
1672 if (vid == 0)
1673 return 0;
1674
1675 nd = ncsi_find_dev(dev);
1676 if (!nd) {
1677 netdev_warn(dev, "NCSI: No net_device?\n");
1678 return 0;
1679 }
1680
1681 ndp = TO_NCSI_DEV_PRIV(nd);
1682
1683
1684 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1685 n_vids++;
1686 if (vlan->vid == vid) {
1687 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1688 vid);
1689 return 0;
1690 }
1691 }
1692 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1693 netdev_warn(dev,
1694 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1695 vid, NCSI_MAX_VLAN_VIDS);
1696 return -ENOSPC;
1697 }
1698
1699 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1700 if (!vlan)
1701 return -ENOMEM;
1702
1703 vlan->proto = proto;
1704 vlan->vid = vid;
1705 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1706
1707 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
1708
1709 found = ncsi_kick_channels(ndp) != 0;
1710
1711 return found ? ncsi_process_next_channel(ndp) : 0;
1712 }
1713 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1714
1715 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1716 {
1717 struct vlan_vid *vlan, *tmp;
1718 struct ncsi_dev_priv *ndp;
1719 struct ncsi_dev *nd;
1720 bool found = false;
1721
1722 if (vid == 0)
1723 return 0;
1724
1725 nd = ncsi_find_dev(dev);
1726 if (!nd) {
1727 netdev_warn(dev, "NCSI: no net_device?\n");
1728 return 0;
1729 }
1730
1731 ndp = TO_NCSI_DEV_PRIV(nd);
1732
1733
1734 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1735 if (vlan->vid == vid) {
1736 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
1737 list_del_rcu(&vlan->list);
1738 found = true;
1739 kfree(vlan);
1740 }
1741
1742 if (!found) {
1743 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1744 return -EINVAL;
1745 }
1746
1747 found = ncsi_kick_channels(ndp) != 0;
1748
1749 return found ? ncsi_process_next_channel(ndp) : 0;
1750 }
1751 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1752
1753 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1754 void (*handler)(struct ncsi_dev *ndev))
1755 {
1756 struct ncsi_dev_priv *ndp;
1757 struct ncsi_dev *nd;
1758 struct platform_device *pdev;
1759 struct device_node *np;
1760 unsigned long flags;
1761 int i;
1762
1763
1764 nd = ncsi_find_dev(dev);
1765 if (nd)
1766 return nd;
1767
1768
1769 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1770 if (!ndp)
1771 return NULL;
1772
1773 nd = &ndp->ndev;
1774 nd->state = ncsi_dev_state_registered;
1775 nd->dev = dev;
1776 nd->handler = handler;
1777 ndp->pending_req_num = 0;
1778 INIT_LIST_HEAD(&ndp->channel_queue);
1779 INIT_LIST_HEAD(&ndp->vlan_vids);
1780 INIT_WORK(&ndp->work, ncsi_dev_work);
1781 ndp->package_whitelist = UINT_MAX;
1782
1783
1784 spin_lock_init(&ndp->lock);
1785 INIT_LIST_HEAD(&ndp->packages);
1786 ndp->request_id = NCSI_REQ_START_IDX;
1787 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1788 ndp->requests[i].id = i;
1789 ndp->requests[i].ndp = ndp;
1790 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
1791 }
1792
1793 spin_lock_irqsave(&ncsi_dev_lock, flags);
1794 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1795 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1796
1797
1798 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1799 ndp->ptype.func = ncsi_rcv_rsp;
1800 ndp->ptype.dev = dev;
1801 dev_add_pack(&ndp->ptype);
1802
1803 pdev = to_platform_device(dev->dev.parent);
1804 if (pdev) {
1805 np = pdev->dev.of_node;
1806 if (np && (of_get_property(np, "mellanox,multi-host", NULL) ||
1807 of_get_property(np, "mlx,multi-host", NULL)))
1808 ndp->mlx_multi_host = true;
1809 }
1810
1811 return nd;
1812 }
1813 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1814
1815 int ncsi_start_dev(struct ncsi_dev *nd)
1816 {
1817 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1818
1819 if (nd->state != ncsi_dev_state_registered &&
1820 nd->state != ncsi_dev_state_functional)
1821 return -ENOTTY;
1822
1823 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1824 ndp->package_probe_id = 0;
1825 nd->state = ncsi_dev_state_probe;
1826 schedule_work(&ndp->work);
1827 return 0;
1828 }
1829
1830 return ncsi_reset_dev(nd);
1831 }
1832 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1833
1834 void ncsi_stop_dev(struct ncsi_dev *nd)
1835 {
1836 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1837 struct ncsi_package *np;
1838 struct ncsi_channel *nc;
1839 bool chained;
1840 int old_state;
1841 unsigned long flags;
1842
1843
1844
1845
1846
1847 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1848 NCSI_FOR_EACH_CHANNEL(np, nc) {
1849 ncsi_stop_channel_monitor(nc);
1850
1851 spin_lock_irqsave(&nc->lock, flags);
1852 chained = !list_empty(&nc->link);
1853 old_state = nc->state;
1854 spin_unlock_irqrestore(&nc->lock, flags);
1855
1856 WARN_ON_ONCE(chained ||
1857 old_state == NCSI_CHANNEL_INVISIBLE);
1858 }
1859 }
1860
1861 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
1862 ncsi_report_link(ndp, true);
1863 }
1864 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1865
1866 int ncsi_reset_dev(struct ncsi_dev *nd)
1867 {
1868 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1869 struct ncsi_channel *nc, *active, *tmp;
1870 struct ncsi_package *np;
1871 unsigned long flags;
1872
1873 spin_lock_irqsave(&ndp->lock, flags);
1874
1875 if (!(ndp->flags & NCSI_DEV_RESET)) {
1876
1877 switch (nd->state & ncsi_dev_state_major) {
1878 case ncsi_dev_state_registered:
1879 case ncsi_dev_state_probe:
1880
1881 spin_unlock_irqrestore(&ndp->lock, flags);
1882 return 0;
1883 case ncsi_dev_state_suspend:
1884 case ncsi_dev_state_config:
1885
1886
1887
1888
1889 ndp->flags |= NCSI_DEV_RESET;
1890 spin_unlock_irqrestore(&ndp->lock, flags);
1891 return 0;
1892 }
1893 } else {
1894 switch (nd->state) {
1895 case ncsi_dev_state_suspend_done:
1896 case ncsi_dev_state_config_done:
1897 case ncsi_dev_state_functional:
1898
1899 break;
1900 default:
1901
1902 spin_unlock_irqrestore(&ndp->lock, flags);
1903 return 0;
1904 }
1905 }
1906
1907 if (!list_empty(&ndp->channel_queue)) {
1908
1909 list_for_each_entry_safe(nc, tmp, &ndp->channel_queue, link)
1910 list_del_init(&nc->link);
1911 }
1912 spin_unlock_irqrestore(&ndp->lock, flags);
1913
1914 active = NULL;
1915 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1916 NCSI_FOR_EACH_CHANNEL(np, nc) {
1917 spin_lock_irqsave(&nc->lock, flags);
1918
1919 if (nc->state == NCSI_CHANNEL_ACTIVE) {
1920 active = nc;
1921 nc->state = NCSI_CHANNEL_INVISIBLE;
1922 spin_unlock_irqrestore(&nc->lock, flags);
1923 ncsi_stop_channel_monitor(nc);
1924 break;
1925 }
1926
1927 spin_unlock_irqrestore(&nc->lock, flags);
1928 }
1929 if (active)
1930 break;
1931 }
1932
1933 if (!active) {
1934
1935 spin_lock_irqsave(&ndp->lock, flags);
1936 ndp->flags &= ~NCSI_DEV_RESET;
1937 spin_unlock_irqrestore(&ndp->lock, flags);
1938 return ncsi_choose_active_channel(ndp);
1939 }
1940
1941 spin_lock_irqsave(&ndp->lock, flags);
1942 ndp->flags |= NCSI_DEV_RESET;
1943 ndp->active_channel = active;
1944 ndp->active_package = active->package;
1945 spin_unlock_irqrestore(&ndp->lock, flags);
1946
1947 nd->state = ncsi_dev_state_suspend;
1948 schedule_work(&ndp->work);
1949 return 0;
1950 }
1951
1952 void ncsi_unregister_dev(struct ncsi_dev *nd)
1953 {
1954 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1955 struct ncsi_package *np, *tmp;
1956 unsigned long flags;
1957
1958 dev_remove_pack(&ndp->ptype);
1959
1960 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1961 ncsi_remove_package(np);
1962
1963 spin_lock_irqsave(&ncsi_dev_lock, flags);
1964 list_del_rcu(&ndp->node);
1965 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1966
1967 kfree(ndp);
1968 }
1969 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);