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