1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0 3 * 4 * Copyright (c) 2004 Topspin Communications. All rights reserved. 5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 6 * Copyright (c) 2004 Voltaire, Inc. All rights reserved. 7 * 8 * This software is available to you under a choice of one of two 9 * licenses. You may choose to be licensed under the terms of the GNU 10 * General Public License (GPL) Version 2, available from the file 11 * COPYING in the main directory of this source tree, or the 12 * OpenIB.org BSD license below: 13 * 14 * Redistribution and use in source and binary forms, with or 15 * without modification, are permitted provided that the following 16 * conditions are met: 17 * 18 * - Redistributions of source code must retain the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer. 21 * 22 * - Redistributions in binary form must reproduce the above 23 * copyright notice, this list of conditions and the following 24 * disclaimer in the documentation and/or other materials 25 * provided with the distribution. 26 * 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 34 * SOFTWARE. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "ipoib.h" 41 #include <sys/eventhandler.h> 42 43 static int ipoib_resolvemulti(struct ifnet *, struct sockaddr **, 44 struct sockaddr *); 45 46 47 #include <linux/module.h> 48 49 #include <linux/slab.h> 50 #include <linux/kernel.h> 51 #include <linux/vmalloc.h> 52 53 #include <linux/if_arp.h> /* For ARPHRD_xxx */ 54 #include <linux/if_vlan.h> 55 #include <net/ip.h> 56 #include <net/ipv6.h> 57 58 #include <rdma/ib_cache.h> 59 60 MODULE_AUTHOR("Roland Dreier"); 61 MODULE_DESCRIPTION("IP-over-InfiniBand net driver"); 62 MODULE_LICENSE("Dual BSD/GPL"); 63 64 int ipoib_sendq_size = IPOIB_TX_RING_SIZE; 65 int ipoib_recvq_size = IPOIB_RX_RING_SIZE; 66 67 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444); 68 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue"); 69 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444); 70 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue"); 71 72 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 73 int ipoib_debug_level = 1; 74 75 module_param_named(debug_level, ipoib_debug_level, int, 0644); 76 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0"); 77 #endif 78 79 struct ipoib_path_iter { 80 struct ipoib_dev_priv *priv; 81 struct ipoib_path path; 82 }; 83 84 static const u8 ipv4_bcast_addr[] = { 85 0x00, 0xff, 0xff, 0xff, 86 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, 87 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff 88 }; 89 90 struct workqueue_struct *ipoib_workqueue; 91 92 struct ib_sa_client ipoib_sa_client; 93 94 static void ipoib_add_one(struct ib_device *device); 95 static void ipoib_remove_one(struct ib_device *device, void *client_data); 96 static struct net_device *ipoib_get_net_dev_by_params( 97 struct ib_device *dev, u8 port, u16 pkey, 98 const union ib_gid *gid, const struct sockaddr *addr, 99 void *client_data); 100 static void ipoib_start(struct ifnet *dev); 101 static int ipoib_output(struct ifnet *ifp, struct mbuf *m, 102 const struct sockaddr *dst, struct route *ro); 103 static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data); 104 static void ipoib_input(struct ifnet *ifp, struct mbuf *m); 105 106 #define IPOIB_MTAP(_ifp, _m) \ 107 do { \ 108 if (bpf_peers_present((_ifp)->if_bpf)) { \ 109 M_ASSERTVALID(_m); \ 110 ipoib_mtap_mb((_ifp), (_m)); \ 111 } \ 112 } while (0) 113 114 static struct unrhdr *ipoib_unrhdr; 115 116 static void 117 ipoib_unrhdr_init(void *arg) 118 { 119 120 ipoib_unrhdr = new_unrhdr(0, 65535, NULL); 121 } 122 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL); 123 124 static void 125 ipoib_unrhdr_uninit(void *arg) 126 { 127 128 if (ipoib_unrhdr != NULL) { 129 struct unrhdr *hdr; 130 131 hdr = ipoib_unrhdr; 132 ipoib_unrhdr = NULL; 133 134 delete_unrhdr(hdr); 135 } 136 } 137 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL); 138 139 /* 140 * This is for clients that have an ipoib_header in the mbuf. 141 */ 142 static void 143 ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb) 144 { 145 struct ipoib_header *ih; 146 struct ether_header eh; 147 148 ih = mtod(mb, struct ipoib_header *); 149 eh.ether_type = ih->proto; 150 bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN); 151 bzero(&eh.ether_shost, ETHER_ADDR_LEN); 152 mb->m_data += sizeof(struct ipoib_header); 153 mb->m_len -= sizeof(struct ipoib_header); 154 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb); 155 mb->m_data -= sizeof(struct ipoib_header); 156 mb->m_len += sizeof(struct ipoib_header); 157 } 158 159 void 160 ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto) 161 { 162 struct ether_header eh; 163 164 eh.ether_type = proto; 165 bzero(&eh.ether_shost, ETHER_ADDR_LEN); 166 bzero(&eh.ether_dhost, ETHER_ADDR_LEN); 167 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb); 168 } 169 170 static struct ib_client ipoib_client = { 171 .name = "ipoib", 172 .add = ipoib_add_one, 173 .remove = ipoib_remove_one, 174 .get_net_dev_by_params = ipoib_get_net_dev_by_params, 175 }; 176 177 int 178 ipoib_open(struct ipoib_dev_priv *priv) 179 { 180 struct ifnet *dev = priv->dev; 181 182 ipoib_dbg(priv, "bringing up interface\n"); 183 184 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 185 186 if (ipoib_pkey_dev_delay_open(priv)) 187 return 0; 188 189 if (ipoib_ib_dev_open(priv)) 190 goto err_disable; 191 192 if (ipoib_ib_dev_up(priv)) 193 goto err_stop; 194 195 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 196 struct ipoib_dev_priv *cpriv; 197 198 /* Bring up any child interfaces too */ 199 mutex_lock(&priv->vlan_mutex); 200 list_for_each_entry(cpriv, &priv->child_intfs, list) 201 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0) 202 ipoib_open(cpriv); 203 mutex_unlock(&priv->vlan_mutex); 204 } 205 dev->if_drv_flags |= IFF_DRV_RUNNING; 206 dev->if_drv_flags &= ~IFF_DRV_OACTIVE; 207 208 return 0; 209 210 err_stop: 211 ipoib_ib_dev_stop(priv, 1); 212 213 err_disable: 214 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 215 216 return -EINVAL; 217 } 218 219 static void 220 ipoib_init(void *arg) 221 { 222 struct ifnet *dev; 223 struct ipoib_dev_priv *priv; 224 225 priv = arg; 226 dev = priv->dev; 227 if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0) 228 ipoib_open(priv); 229 queue_work(ipoib_workqueue, &priv->flush_light); 230 } 231 232 233 static int 234 ipoib_stop(struct ipoib_dev_priv *priv) 235 { 236 struct ifnet *dev = priv->dev; 237 238 ipoib_dbg(priv, "stopping interface\n"); 239 240 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 241 242 dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 243 244 ipoib_ib_dev_down(priv, 0); 245 ipoib_ib_dev_stop(priv, 0); 246 247 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 248 struct ipoib_dev_priv *cpriv; 249 250 /* Bring down any child interfaces too */ 251 mutex_lock(&priv->vlan_mutex); 252 list_for_each_entry(cpriv, &priv->child_intfs, list) 253 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0) 254 ipoib_stop(cpriv); 255 mutex_unlock(&priv->vlan_mutex); 256 } 257 258 return 0; 259 } 260 261 static int 262 ipoib_propagate_ifnet_mtu(struct ipoib_dev_priv *priv, int new_mtu, 263 bool propagate) 264 { 265 struct ifnet *ifp; 266 struct ifreq ifr; 267 int error; 268 269 ifp = priv->dev; 270 if (ifp->if_mtu == new_mtu) 271 return (0); 272 if (propagate) { 273 strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ); 274 ifr.ifr_mtu = new_mtu; 275 CURVNET_SET(ifp->if_vnet); 276 error = ifhwioctl(SIOCSIFMTU, ifp, (caddr_t)&ifr, curthread); 277 CURVNET_RESTORE(); 278 } else { 279 ifp->if_mtu = new_mtu; 280 error = 0; 281 } 282 return (error); 283 } 284 285 int 286 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu, bool propagate) 287 { 288 int error, prev_admin_mtu; 289 290 /* dev->if_mtu > 2K ==> connected mode */ 291 if (ipoib_cm_admin_enabled(priv)) { 292 if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv))) 293 return -EINVAL; 294 295 if (new_mtu > priv->mcast_mtu) 296 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n", 297 priv->mcast_mtu); 298 299 return (ipoib_propagate_ifnet_mtu(priv, new_mtu, propagate)); 300 } 301 302 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu)) 303 return -EINVAL; 304 305 prev_admin_mtu = priv->admin_mtu; 306 priv->admin_mtu = new_mtu; 307 error = ipoib_propagate_ifnet_mtu(priv, min(priv->mcast_mtu, 308 priv->admin_mtu), propagate); 309 if (error == 0) { 310 /* check for MTU change to avoid infinite loop */ 311 if (prev_admin_mtu != new_mtu) 312 queue_work(ipoib_workqueue, &priv->flush_light); 313 } else 314 priv->admin_mtu = prev_admin_mtu; 315 return (error); 316 } 317 318 static int 319 ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 320 { 321 struct ipoib_dev_priv *priv = ifp->if_softc; 322 struct ifaddr *ifa = (struct ifaddr *) data; 323 struct ifreq *ifr = (struct ifreq *) data; 324 int error = 0; 325 326 /* check if detaching */ 327 if (priv == NULL || priv->gone != 0) 328 return (ENXIO); 329 330 switch (command) { 331 case SIOCSIFFLAGS: 332 if (ifp->if_flags & IFF_UP) { 333 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 334 error = -ipoib_open(priv); 335 } else 336 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 337 ipoib_stop(priv); 338 break; 339 case SIOCADDMULTI: 340 case SIOCDELMULTI: 341 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 342 queue_work(ipoib_workqueue, &priv->restart_task); 343 break; 344 case SIOCSIFADDR: 345 ifp->if_flags |= IFF_UP; 346 347 switch (ifa->ifa_addr->sa_family) { 348 #ifdef INET 349 case AF_INET: 350 ifp->if_init(ifp->if_softc); /* before arpwhohas */ 351 arp_ifinit(ifp, ifa); 352 break; 353 #endif 354 default: 355 ifp->if_init(ifp->if_softc); 356 break; 357 } 358 break; 359 360 case SIOCGIFADDR: 361 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0], 362 INFINIBAND_ALEN); 363 break; 364 365 case SIOCSIFMTU: 366 /* 367 * Set the interface MTU. 368 */ 369 error = -ipoib_change_mtu(priv, ifr->ifr_mtu, false); 370 break; 371 default: 372 error = EINVAL; 373 break; 374 } 375 return (error); 376 } 377 378 379 static struct ipoib_path * 380 __path_find(struct ipoib_dev_priv *priv, void *gid) 381 { 382 struct rb_node *n = priv->path_tree.rb_node; 383 struct ipoib_path *path; 384 int ret; 385 386 while (n) { 387 path = rb_entry(n, struct ipoib_path, rb_node); 388 389 ret = memcmp(gid, path->pathrec.dgid.raw, 390 sizeof (union ib_gid)); 391 392 if (ret < 0) 393 n = n->rb_left; 394 else if (ret > 0) 395 n = n->rb_right; 396 else 397 return path; 398 } 399 400 return NULL; 401 } 402 403 static int 404 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path) 405 { 406 struct rb_node **n = &priv->path_tree.rb_node; 407 struct rb_node *pn = NULL; 408 struct ipoib_path *tpath; 409 int ret; 410 411 while (*n) { 412 pn = *n; 413 tpath = rb_entry(pn, struct ipoib_path, rb_node); 414 415 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw, 416 sizeof (union ib_gid)); 417 if (ret < 0) 418 n = &pn->rb_left; 419 else if (ret > 0) 420 n = &pn->rb_right; 421 else 422 return -EEXIST; 423 } 424 425 rb_link_node(&path->rb_node, pn, n); 426 rb_insert_color(&path->rb_node, &priv->path_tree); 427 428 list_add_tail(&path->list, &priv->path_list); 429 430 return 0; 431 } 432 433 void 434 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path) 435 { 436 437 _IF_DRAIN(&path->queue); 438 439 if (path->ah) 440 ipoib_put_ah(path->ah); 441 if (ipoib_cm_get(path)) 442 ipoib_cm_destroy_tx(ipoib_cm_get(path)); 443 444 kfree(path); 445 } 446 447 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 448 449 struct ipoib_path_iter * 450 ipoib_path_iter_init(struct ipoib_dev_priv *priv) 451 { 452 struct ipoib_path_iter *iter; 453 454 iter = kmalloc(sizeof *iter, GFP_KERNEL); 455 if (!iter) 456 return NULL; 457 458 iter->priv = priv; 459 memset(iter->path.pathrec.dgid.raw, 0, 16); 460 461 if (ipoib_path_iter_next(iter)) { 462 kfree(iter); 463 return NULL; 464 } 465 466 return iter; 467 } 468 469 int 470 ipoib_path_iter_next(struct ipoib_path_iter *iter) 471 { 472 struct ipoib_dev_priv *priv = iter->priv; 473 struct rb_node *n; 474 struct ipoib_path *path; 475 int ret = 1; 476 477 spin_lock_irq(&priv->lock); 478 479 n = rb_first(&priv->path_tree); 480 481 while (n) { 482 path = rb_entry(n, struct ipoib_path, rb_node); 483 484 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw, 485 sizeof (union ib_gid)) < 0) { 486 iter->path = *path; 487 ret = 0; 488 break; 489 } 490 491 n = rb_next(n); 492 } 493 494 spin_unlock_irq(&priv->lock); 495 496 return ret; 497 } 498 499 void 500 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path) 501 { 502 *path = iter->path; 503 } 504 505 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */ 506 507 void 508 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv) 509 { 510 struct ipoib_path *path, *tp; 511 512 spin_lock_irq(&priv->lock); 513 514 list_for_each_entry_safe(path, tp, &priv->path_list, list) { 515 ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n", 516 be16_to_cpu(path->pathrec.dlid), 517 path->pathrec.dgid.raw, ":"); 518 path->valid = 0; 519 } 520 521 spin_unlock_irq(&priv->lock); 522 } 523 524 void 525 ipoib_flush_paths(struct ipoib_dev_priv *priv) 526 { 527 struct ipoib_path *path, *tp; 528 LIST_HEAD(remove_list); 529 unsigned long flags; 530 531 spin_lock_irqsave(&priv->lock, flags); 532 533 list_splice_init(&priv->path_list, &remove_list); 534 535 list_for_each_entry(path, &remove_list, list) 536 rb_erase(&path->rb_node, &priv->path_tree); 537 538 list_for_each_entry_safe(path, tp, &remove_list, list) { 539 if (path->query) 540 ib_sa_cancel_query(path->query_id, path->query); 541 spin_unlock_irqrestore(&priv->lock, flags); 542 wait_for_completion(&path->done); 543 ipoib_path_free(priv, path); 544 spin_lock_irqsave(&priv->lock, flags); 545 } 546 547 spin_unlock_irqrestore(&priv->lock, flags); 548 } 549 550 static void 551 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr) 552 { 553 struct ipoib_path *path = path_ptr; 554 struct ipoib_dev_priv *priv = path->priv; 555 struct ifnet *dev = priv->dev; 556 struct ipoib_ah *ah = NULL; 557 struct ipoib_ah *old_ah = NULL; 558 struct ifqueue mbqueue; 559 struct mbuf *mb; 560 unsigned long flags; 561 562 if (!status) 563 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n", 564 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":"); 565 else 566 ipoib_dbg(priv, "PathRec status %d for GID %16D\n", 567 status, path->pathrec.dgid.raw, ":"); 568 569 bzero(&mbqueue, sizeof(mbqueue)); 570 571 if (!status) { 572 struct ib_ah_attr av; 573 574 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av)) 575 ah = ipoib_create_ah(priv, priv->pd, &av); 576 } 577 578 spin_lock_irqsave(&priv->lock, flags); 579 580 if (ah) { 581 path->pathrec = *pathrec; 582 583 old_ah = path->ah; 584 path->ah = ah; 585 586 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n", 587 ah, be16_to_cpu(pathrec->dlid), pathrec->sl); 588 589 for (;;) { 590 _IF_DEQUEUE(&path->queue, mb); 591 if (mb == NULL) 592 break; 593 _IF_ENQUEUE(&mbqueue, mb); 594 } 595 596 #ifdef CONFIG_INFINIBAND_IPOIB_CM 597 if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path)) 598 ipoib_cm_set(path, ipoib_cm_create_tx(priv, path)); 599 #endif 600 601 path->valid = 1; 602 } 603 604 path->query = NULL; 605 complete(&path->done); 606 607 spin_unlock_irqrestore(&priv->lock, flags); 608 609 if (old_ah) 610 ipoib_put_ah(old_ah); 611 612 for (;;) { 613 _IF_DEQUEUE(&mbqueue, mb); 614 if (mb == NULL) 615 break; 616 mb->m_pkthdr.rcvif = dev; 617 if (dev->if_transmit(dev, mb)) 618 ipoib_warn(priv, "dev_queue_xmit failed " 619 "to requeue packet\n"); 620 } 621 } 622 623 static struct ipoib_path * 624 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr) 625 { 626 struct ipoib_path *path; 627 628 if (!priv->broadcast) 629 return NULL; 630 631 path = kzalloc(sizeof *path, GFP_ATOMIC); 632 if (!path) 633 return NULL; 634 635 path->priv = priv; 636 637 bzero(&path->queue, sizeof(path->queue)); 638 639 #ifdef CONFIG_INFINIBAND_IPOIB_CM 640 memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN); 641 #endif 642 memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid)); 643 path->pathrec.sgid = priv->local_gid; 644 path->pathrec.pkey = cpu_to_be16(priv->pkey); 645 path->pathrec.numb_path = 1; 646 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class; 647 648 return path; 649 } 650 651 static int 652 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path) 653 { 654 struct ifnet *dev = priv->dev; 655 656 ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU; 657 struct ib_sa_path_rec p_rec; 658 659 p_rec = path->pathrec; 660 p_rec.mtu_selector = IB_SA_GT; 661 662 switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) { 663 case 512: 664 p_rec.mtu = IB_MTU_256; 665 break; 666 case 1024: 667 p_rec.mtu = IB_MTU_512; 668 break; 669 case 2048: 670 p_rec.mtu = IB_MTU_1024; 671 break; 672 case 4096: 673 p_rec.mtu = IB_MTU_2048; 674 break; 675 default: 676 /* Wildcard everything */ 677 comp_mask = 0; 678 p_rec.mtu = 0; 679 p_rec.mtu_selector = 0; 680 } 681 682 ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n", 683 p_rec.dgid.raw, ":", 684 comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0); 685 686 init_completion(&path->done); 687 688 path->query_id = 689 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port, 690 &p_rec, comp_mask | 691 IB_SA_PATH_REC_DGID | 692 IB_SA_PATH_REC_SGID | 693 IB_SA_PATH_REC_NUMB_PATH | 694 IB_SA_PATH_REC_TRAFFIC_CLASS | 695 IB_SA_PATH_REC_PKEY, 696 1000, GFP_ATOMIC, 697 path_rec_completion, 698 path, &path->query); 699 if (path->query_id < 0) { 700 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id); 701 path->query = NULL; 702 complete(&path->done); 703 return path->query_id; 704 } 705 706 return 0; 707 } 708 709 static void 710 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh) 711 { 712 struct ipoib_path *path; 713 714 path = __path_find(priv, eh->hwaddr + 4); 715 if (!path || !path->valid) { 716 int new_path = 0; 717 718 if (!path) { 719 path = path_rec_create(priv, eh->hwaddr); 720 new_path = 1; 721 } 722 if (path) { 723 if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) 724 _IF_ENQUEUE(&path->queue, mb); 725 else { 726 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1); 727 m_freem(mb); 728 } 729 730 if (!path->query && path_rec_start(priv, path)) { 731 spin_unlock_irqrestore(&priv->lock, flags); 732 if (new_path) 733 ipoib_path_free(priv, path); 734 return; 735 } else 736 __path_add(priv, path); 737 } else { 738 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1); 739 m_freem(mb); 740 } 741 742 return; 743 } 744 745 if (ipoib_cm_get(path) && ipoib_cm_up(path)) { 746 ipoib_cm_send(priv, mb, ipoib_cm_get(path)); 747 } else if (path->ah) { 748 ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr)); 749 } else if ((path->query || !path_rec_start(priv, path)) && 750 path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) { 751 _IF_ENQUEUE(&path->queue, mb); 752 } else { 753 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1); 754 m_freem(mb); 755 } 756 } 757 758 static int 759 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb) 760 { 761 struct ipoib_header *eh; 762 763 eh = mtod(mb, struct ipoib_header *); 764 if (IPOIB_IS_MULTICAST(eh->hwaddr)) { 765 /* Add in the P_Key for multicast*/ 766 eh->hwaddr[8] = (priv->pkey >> 8) & 0xff; 767 eh->hwaddr[9] = priv->pkey & 0xff; 768 769 ipoib_mcast_send(priv, eh->hwaddr + 4, mb); 770 } else 771 ipoib_unicast_send(mb, priv, eh); 772 773 return 0; 774 } 775 776 777 static void 778 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv) 779 { 780 struct mbuf *mb; 781 782 if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) != 783 IFF_DRV_RUNNING) 784 return; 785 786 spin_lock(&priv->lock); 787 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) && 788 (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) { 789 IFQ_DRV_DEQUEUE(&dev->if_snd, mb); 790 if (mb == NULL) 791 break; 792 IPOIB_MTAP(dev, mb); 793 ipoib_send_one(priv, mb); 794 } 795 spin_unlock(&priv->lock); 796 } 797 798 static void 799 ipoib_start(struct ifnet *dev) 800 { 801 _ipoib_start(dev, dev->if_softc); 802 } 803 804 static void 805 ipoib_vlan_start(struct ifnet *dev) 806 { 807 struct ipoib_dev_priv *priv; 808 struct mbuf *mb; 809 810 priv = VLAN_COOKIE(dev); 811 if (priv != NULL) 812 return _ipoib_start(dev, priv); 813 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) { 814 IFQ_DRV_DEQUEUE(&dev->if_snd, mb); 815 if (mb == NULL) 816 break; 817 m_freem(mb); 818 if_inc_counter(dev, IFCOUNTER_OERRORS, 1); 819 } 820 } 821 822 int 823 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port) 824 { 825 826 /* Allocate RX/TX "rings" to hold queued mbs */ 827 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring, 828 GFP_KERNEL); 829 if (!priv->rx_ring) { 830 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n", 831 ca->name, ipoib_recvq_size); 832 goto out; 833 } 834 835 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL); 836 if (!priv->tx_ring) { 837 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n", 838 ca->name, ipoib_sendq_size); 839 goto out_rx_ring_cleanup; 840 } 841 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring); 842 843 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */ 844 845 if (ipoib_ib_dev_init(priv, ca, port)) 846 goto out_tx_ring_cleanup; 847 848 return 0; 849 850 out_tx_ring_cleanup: 851 kfree(priv->tx_ring); 852 853 out_rx_ring_cleanup: 854 kfree(priv->rx_ring); 855 856 out: 857 return -ENOMEM; 858 } 859 860 static void 861 ipoib_detach(struct ipoib_dev_priv *priv) 862 { 863 struct ifnet *dev; 864 865 dev = priv->dev; 866 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 867 priv->gone = 1; 868 bpfdetach(dev); 869 if_detach(dev); 870 if_free(dev); 871 free_unr(ipoib_unrhdr, priv->unit); 872 } else 873 VLAN_SETCOOKIE(priv->dev, NULL); 874 875 free(priv, M_TEMP); 876 } 877 878 void 879 ipoib_dev_cleanup(struct ipoib_dev_priv *priv) 880 { 881 struct ipoib_dev_priv *cpriv, *tcpriv; 882 883 /* Delete any child interfaces first */ 884 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) { 885 ipoib_dev_cleanup(cpriv); 886 ipoib_detach(cpriv); 887 } 888 889 ipoib_ib_dev_cleanup(priv); 890 891 kfree(priv->rx_ring); 892 kfree(priv->tx_ring); 893 894 priv->rx_ring = NULL; 895 priv->tx_ring = NULL; 896 } 897 898 static struct ipoib_dev_priv * 899 ipoib_priv_alloc(void) 900 { 901 struct ipoib_dev_priv *priv; 902 903 priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK); 904 spin_lock_init(&priv->lock); 905 spin_lock_init(&priv->drain_lock); 906 mutex_init(&priv->vlan_mutex); 907 INIT_LIST_HEAD(&priv->path_list); 908 INIT_LIST_HEAD(&priv->child_intfs); 909 INIT_LIST_HEAD(&priv->dead_ahs); 910 INIT_LIST_HEAD(&priv->multicast_list); 911 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll); 912 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task); 913 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task); 914 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light); 915 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal); 916 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy); 917 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task); 918 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah); 919 memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN); 920 921 return (priv); 922 } 923 924 struct ipoib_dev_priv * 925 ipoib_intf_alloc(const char *name) 926 { 927 struct ipoib_dev_priv *priv; 928 struct sockaddr_dl *sdl; 929 struct ifnet *dev; 930 931 priv = ipoib_priv_alloc(); 932 dev = priv->dev = if_alloc(IFT_INFINIBAND); 933 if (!dev) { 934 free(priv, M_TEMP); 935 return NULL; 936 } 937 dev->if_softc = priv; 938 priv->unit = alloc_unr(ipoib_unrhdr); 939 if (priv->unit == -1) { 940 if_free(dev); 941 free(priv, M_TEMP); 942 return NULL; 943 } 944 if_initname(dev, name, priv->unit); 945 dev->if_flags = IFF_BROADCAST | IFF_MULTICAST; 946 dev->if_addrlen = INFINIBAND_ALEN; 947 dev->if_hdrlen = IPOIB_HEADER_LEN; 948 if_attach(dev); 949 dev->if_init = ipoib_init; 950 dev->if_ioctl = ipoib_ioctl; 951 dev->if_start = ipoib_start; 952 dev->if_output = ipoib_output; 953 dev->if_input = ipoib_input; 954 dev->if_resolvemulti = ipoib_resolvemulti; 955 dev->if_baudrate = IF_Gbps(10); 956 dev->if_broadcastaddr = priv->broadcastaddr; 957 dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2; 958 sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr; 959 sdl->sdl_type = IFT_INFINIBAND; 960 sdl->sdl_alen = dev->if_addrlen; 961 priv->dev = dev; 962 if_link_state_change(dev, LINK_STATE_DOWN); 963 bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN); 964 965 return dev->if_softc; 966 } 967 968 int 969 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca) 970 { 971 struct ib_device_attr *device_attr = &hca->attrs; 972 973 priv->hca_caps = device_attr->device_cap_flags; 974 975 priv->dev->if_hwassist = 0; 976 priv->dev->if_capabilities = 0; 977 978 #ifndef CONFIG_INFINIBAND_IPOIB_CM 979 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) { 980 set_bit(IPOIB_FLAG_CSUM, &priv->flags); 981 priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP; 982 priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM; 983 } 984 985 #if 0 986 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) { 987 priv->dev->if_capabilities |= IFCAP_TSO4; 988 priv->dev->if_hwassist |= CSUM_TSO; 989 } 990 #endif 991 #endif 992 priv->dev->if_capabilities |= 993 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE; 994 priv->dev->if_capenable = priv->dev->if_capabilities; 995 996 return 0; 997 } 998 999 1000 static struct ifnet * 1001 ipoib_add_port(const char *format, struct ib_device *hca, u8 port) 1002 { 1003 struct ipoib_dev_priv *priv; 1004 struct ib_port_attr attr; 1005 int result = -ENOMEM; 1006 1007 priv = ipoib_intf_alloc(format); 1008 if (!priv) 1009 goto alloc_mem_failed; 1010 1011 if (!ib_query_port(hca, port, &attr)) 1012 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu); 1013 else { 1014 printk(KERN_WARNING "%s: ib_query_port %d failed\n", 1015 hca->name, port); 1016 goto device_init_failed; 1017 } 1018 1019 /* MTU will be reset when mcast join happens */ 1020 priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu); 1021 priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu; 1022 1023 result = ib_query_pkey(hca, port, 0, &priv->pkey); 1024 if (result) { 1025 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n", 1026 hca->name, port, result); 1027 goto device_init_failed; 1028 } 1029 1030 if (ipoib_set_dev_features(priv, hca)) 1031 goto device_init_failed; 1032 1033 /* 1034 * Set the full membership bit, so that we join the right 1035 * broadcast group, etc. 1036 */ 1037 priv->pkey |= 0x8000; 1038 1039 priv->broadcastaddr[8] = priv->pkey >> 8; 1040 priv->broadcastaddr[9] = priv->pkey & 0xff; 1041 1042 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL); 1043 if (result) { 1044 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n", 1045 hca->name, port, result); 1046 goto device_init_failed; 1047 } 1048 memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid)); 1049 1050 result = ipoib_dev_init(priv, hca, port); 1051 if (result < 0) { 1052 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n", 1053 hca->name, port, result); 1054 goto device_init_failed; 1055 } 1056 if (ipoib_cm_admin_enabled(priv)) 1057 priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)); 1058 1059 INIT_IB_EVENT_HANDLER(&priv->event_handler, 1060 priv->ca, ipoib_event); 1061 result = ib_register_event_handler(&priv->event_handler); 1062 if (result < 0) { 1063 printk(KERN_WARNING "%s: ib_register_event_handler failed for " 1064 "port %d (ret = %d)\n", 1065 hca->name, port, result); 1066 goto event_failed; 1067 } 1068 if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port); 1069 1070 return priv->dev; 1071 1072 event_failed: 1073 ipoib_dev_cleanup(priv); 1074 1075 device_init_failed: 1076 ipoib_detach(priv); 1077 1078 alloc_mem_failed: 1079 return ERR_PTR(result); 1080 } 1081 1082 static void 1083 ipoib_add_one(struct ib_device *device) 1084 { 1085 struct list_head *dev_list; 1086 struct ifnet *dev; 1087 struct ipoib_dev_priv *priv; 1088 int s, e, p; 1089 1090 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) 1091 return; 1092 1093 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL); 1094 if (!dev_list) 1095 return; 1096 1097 INIT_LIST_HEAD(dev_list); 1098 1099 if (device->node_type == RDMA_NODE_IB_SWITCH) { 1100 s = 0; 1101 e = 0; 1102 } else { 1103 s = 1; 1104 e = device->phys_port_cnt; 1105 } 1106 1107 for (p = s; p <= e; ++p) { 1108 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND) 1109 continue; 1110 dev = ipoib_add_port("ib", device, p); 1111 if (!IS_ERR(dev)) { 1112 priv = dev->if_softc; 1113 list_add_tail(&priv->list, dev_list); 1114 } 1115 } 1116 1117 ib_set_client_data(device, &ipoib_client, dev_list); 1118 } 1119 1120 static void 1121 ipoib_remove_one(struct ib_device *device, void *client_data) 1122 { 1123 struct ipoib_dev_priv *priv, *tmp; 1124 struct list_head *dev_list = client_data; 1125 1126 if (!dev_list) 1127 return; 1128 1129 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) 1130 return; 1131 1132 list_for_each_entry_safe(priv, tmp, dev_list, list) { 1133 if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND) 1134 continue; 1135 1136 ipoib_stop(priv); 1137 1138 ib_unregister_event_handler(&priv->event_handler); 1139 1140 /* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */ 1141 1142 flush_workqueue(ipoib_workqueue); 1143 1144 ipoib_dev_cleanup(priv); 1145 ipoib_detach(priv); 1146 } 1147 1148 kfree(dev_list); 1149 } 1150 1151 static int 1152 ipoib_match_dev_addr(const struct sockaddr *addr, struct net_device *dev) 1153 { 1154 struct epoch_tracker et; 1155 struct ifaddr *ifa; 1156 int retval = 0; 1157 1158 CURVNET_SET(dev->if_vnet); 1159 NET_EPOCH_ENTER(et); 1160 CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) { 1161 if (ifa->ifa_addr == NULL || 1162 ifa->ifa_addr->sa_family != addr->sa_family || 1163 ifa->ifa_addr->sa_len != addr->sa_len) { 1164 continue; 1165 } 1166 if (memcmp(ifa->ifa_addr, addr, addr->sa_len) == 0) { 1167 retval = 1; 1168 break; 1169 } 1170 } 1171 NET_EPOCH_EXIT(et); 1172 CURVNET_RESTORE(); 1173 1174 return (retval); 1175 } 1176 1177 /* 1178 * ipoib_match_gid_pkey_addr - returns the number of IPoIB netdevs on 1179 * top a given ipoib device matching a pkey_index and address, if one 1180 * exists. 1181 * 1182 * @found_net_dev: contains a matching net_device if the return value 1183 * >= 1, with a reference held. 1184 */ 1185 static int 1186 ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv, 1187 const union ib_gid *gid, u16 pkey_index, const struct sockaddr *addr, 1188 struct net_device **found_net_dev) 1189 { 1190 struct ipoib_dev_priv *child_priv; 1191 int matches = 0; 1192 1193 if (priv->pkey_index == pkey_index && 1194 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) { 1195 if (addr == NULL || ipoib_match_dev_addr(addr, priv->dev) != 0) { 1196 if (*found_net_dev == NULL) { 1197 struct net_device *net_dev; 1198 1199 if (priv->parent != NULL) 1200 net_dev = priv->parent; 1201 else 1202 net_dev = priv->dev; 1203 *found_net_dev = net_dev; 1204 dev_hold(net_dev); 1205 } 1206 matches++; 1207 } 1208 } 1209 1210 /* Check child interfaces */ 1211 mutex_lock(&priv->vlan_mutex); 1212 list_for_each_entry(child_priv, &priv->child_intfs, list) { 1213 matches += ipoib_match_gid_pkey_addr(child_priv, gid, 1214 pkey_index, addr, found_net_dev); 1215 if (matches > 1) 1216 break; 1217 } 1218 mutex_unlock(&priv->vlan_mutex); 1219 1220 return matches; 1221 } 1222 1223 /* 1224 * __ipoib_get_net_dev_by_params - returns the number of matching 1225 * net_devs found (between 0 and 2). Also return the matching 1226 * net_device in the @net_dev parameter, holding a reference to the 1227 * net_device, if the number of matches >= 1 1228 */ 1229 static int 1230 __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port, 1231 u16 pkey_index, const union ib_gid *gid, 1232 const struct sockaddr *addr, struct net_device **net_dev) 1233 { 1234 struct ipoib_dev_priv *priv; 1235 int matches = 0; 1236 1237 *net_dev = NULL; 1238 1239 list_for_each_entry(priv, dev_list, list) { 1240 if (priv->port != port) 1241 continue; 1242 1243 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index, 1244 addr, net_dev); 1245 1246 if (matches > 1) 1247 break; 1248 } 1249 1250 return matches; 1251 } 1252 1253 static struct net_device * 1254 ipoib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey, 1255 const union ib_gid *gid, const struct sockaddr *addr, void *client_data) 1256 { 1257 struct net_device *net_dev; 1258 struct list_head *dev_list = client_data; 1259 u16 pkey_index; 1260 int matches; 1261 int ret; 1262 1263 if (!rdma_protocol_ib(dev, port)) 1264 return NULL; 1265 1266 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index); 1267 if (ret) 1268 return NULL; 1269 1270 if (!dev_list) 1271 return NULL; 1272 1273 /* See if we can find a unique device matching the L2 parameters */ 1274 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, 1275 gid, NULL, &net_dev); 1276 1277 switch (matches) { 1278 case 0: 1279 return NULL; 1280 case 1: 1281 return net_dev; 1282 } 1283 1284 dev_put(net_dev); 1285 1286 /* Couldn't find a unique device with L2 parameters only. Use L3 1287 * address to uniquely match the net device */ 1288 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, 1289 gid, addr, &net_dev); 1290 switch (matches) { 1291 case 0: 1292 return NULL; 1293 default: 1294 dev_warn_ratelimited(&dev->dev, 1295 "duplicate IP address detected\n"); 1296 /* Fall through */ 1297 case 1: 1298 return net_dev; 1299 } 1300 } 1301 1302 static void 1303 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 1304 { 1305 struct ipoib_dev_priv *parent; 1306 struct ipoib_dev_priv *priv; 1307 struct ifnet *dev; 1308 uint16_t pkey; 1309 int error; 1310 1311 if (ifp->if_type != IFT_INFINIBAND) 1312 return; 1313 dev = VLAN_DEVAT(ifp, vtag); 1314 if (dev == NULL) 1315 return; 1316 priv = NULL; 1317 error = 0; 1318 parent = ifp->if_softc; 1319 /* We only support 15 bits of pkey. */ 1320 if (vtag & 0x8000) 1321 return; 1322 pkey = vtag | 0x8000; /* Set full membership bit. */ 1323 if (pkey == parent->pkey) 1324 return; 1325 /* Check for dups */ 1326 mutex_lock(&parent->vlan_mutex); 1327 list_for_each_entry(priv, &parent->child_intfs, list) { 1328 if (priv->pkey == pkey) { 1329 priv = NULL; 1330 error = EBUSY; 1331 goto out; 1332 } 1333 } 1334 priv = ipoib_priv_alloc(); 1335 priv->dev = dev; 1336 priv->max_ib_mtu = parent->max_ib_mtu; 1337 priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu; 1338 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags); 1339 error = ipoib_set_dev_features(priv, parent->ca); 1340 if (error) 1341 goto out; 1342 priv->pkey = pkey; 1343 priv->broadcastaddr[8] = pkey >> 8; 1344 priv->broadcastaddr[9] = pkey & 0xff; 1345 dev->if_broadcastaddr = priv->broadcastaddr; 1346 error = ipoib_dev_init(priv, parent->ca, parent->port); 1347 if (error) 1348 goto out; 1349 priv->parent = parent->dev; 1350 list_add_tail(&priv->list, &parent->child_intfs); 1351 VLAN_SETCOOKIE(dev, priv); 1352 dev->if_start = ipoib_vlan_start; 1353 dev->if_drv_flags &= ~IFF_DRV_RUNNING; 1354 dev->if_hdrlen = IPOIB_HEADER_LEN; 1355 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1356 ipoib_open(priv); 1357 mutex_unlock(&parent->vlan_mutex); 1358 return; 1359 out: 1360 mutex_unlock(&parent->vlan_mutex); 1361 if (priv) 1362 free(priv, M_TEMP); 1363 if (error) 1364 ipoib_warn(parent, 1365 "failed to initialize subinterface: device %s, port %d vtag 0x%X", 1366 parent->ca->name, parent->port, vtag); 1367 return; 1368 } 1369 1370 static void 1371 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag) 1372 { 1373 struct ipoib_dev_priv *parent; 1374 struct ipoib_dev_priv *priv; 1375 struct ifnet *dev; 1376 uint16_t pkey; 1377 1378 if (ifp->if_type != IFT_INFINIBAND) 1379 return; 1380 1381 dev = VLAN_DEVAT(ifp, vtag); 1382 if (dev) 1383 VLAN_SETCOOKIE(dev, NULL); 1384 pkey = vtag | 0x8000; 1385 parent = ifp->if_softc; 1386 mutex_lock(&parent->vlan_mutex); 1387 list_for_each_entry(priv, &parent->child_intfs, list) { 1388 if (priv->pkey == pkey) { 1389 ipoib_dev_cleanup(priv); 1390 list_del(&priv->list); 1391 break; 1392 } 1393 } 1394 mutex_unlock(&parent->vlan_mutex); 1395 } 1396 1397 eventhandler_tag ipoib_vlan_attach; 1398 eventhandler_tag ipoib_vlan_detach; 1399 1400 static int __init 1401 ipoib_init_module(void) 1402 { 1403 int ret; 1404 1405 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size); 1406 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE); 1407 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE); 1408 1409 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size); 1410 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE); 1411 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE, 1412 IPOIB_MIN_QUEUE_SIZE)); 1413 #ifdef CONFIG_INFINIBAND_IPOIB_CM 1414 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP); 1415 #endif 1416 1417 ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config, 1418 ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST); 1419 ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig, 1420 ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST); 1421 1422 /* 1423 * We create our own workqueue mainly because we want to be 1424 * able to flush it when devices are being removed. We can't 1425 * use schedule_work()/flush_scheduled_work() because both 1426 * unregister_netdev() and linkwatch_event take the rtnl lock, 1427 * so flush_scheduled_work() can deadlock during device 1428 * removal. 1429 */ 1430 ipoib_workqueue = create_singlethread_workqueue("ipoib"); 1431 if (!ipoib_workqueue) { 1432 ret = -ENOMEM; 1433 goto err_fs; 1434 } 1435 1436 ib_sa_register_client(&ipoib_sa_client); 1437 1438 ret = ib_register_client(&ipoib_client); 1439 if (ret) 1440 goto err_sa; 1441 1442 return 0; 1443 1444 err_sa: 1445 ib_sa_unregister_client(&ipoib_sa_client); 1446 destroy_workqueue(ipoib_workqueue); 1447 1448 err_fs: 1449 return ret; 1450 } 1451 1452 static void __exit 1453 ipoib_cleanup_module(void) 1454 { 1455 1456 EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach); 1457 EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach); 1458 ib_unregister_client(&ipoib_client); 1459 ib_sa_unregister_client(&ipoib_sa_client); 1460 destroy_workqueue(ipoib_workqueue); 1461 } 1462 1463 /* 1464 * Infiniband output routine. 1465 */ 1466 static int 1467 ipoib_output(struct ifnet *ifp, struct mbuf *m, 1468 const struct sockaddr *dst, struct route *ro) 1469 { 1470 u_char edst[INFINIBAND_ALEN]; 1471 #if defined(INET) || defined(INET6) 1472 struct llentry *lle = NULL; 1473 #endif 1474 struct ipoib_header *eh; 1475 int error = 0, is_gw = 0; 1476 short type; 1477 1478 if (ro != NULL) 1479 is_gw = (ro->ro_flags & RT_HAS_GW) != 0; 1480 #ifdef MAC 1481 error = mac_ifnet_check_transmit(ifp, m); 1482 if (error) 1483 goto bad; 1484 #endif 1485 1486 M_PROFILE(m); 1487 if (ifp->if_flags & IFF_MONITOR) { 1488 error = ENETDOWN; 1489 goto bad; 1490 } 1491 if (!((ifp->if_flags & IFF_UP) && 1492 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 1493 error = ENETDOWN; 1494 goto bad; 1495 } 1496 1497 switch (dst->sa_family) { 1498 #ifdef INET 1499 case AF_INET: 1500 if (lle != NULL && (lle->la_flags & LLE_VALID)) 1501 memcpy(edst, lle->ll_addr, sizeof(edst)); 1502 else if (m->m_flags & M_MCAST) 1503 ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst); 1504 else 1505 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL); 1506 if (error) 1507 return (error == EWOULDBLOCK ? 0 : error); 1508 type = htons(ETHERTYPE_IP); 1509 break; 1510 case AF_ARP: 1511 { 1512 struct arphdr *ah; 1513 ah = mtod(m, struct arphdr *); 1514 ah->ar_hrd = htons(ARPHRD_INFINIBAND); 1515 1516 switch(ntohs(ah->ar_op)) { 1517 case ARPOP_REVREQUEST: 1518 case ARPOP_REVREPLY: 1519 type = htons(ETHERTYPE_REVARP); 1520 break; 1521 case ARPOP_REQUEST: 1522 case ARPOP_REPLY: 1523 default: 1524 type = htons(ETHERTYPE_ARP); 1525 break; 1526 } 1527 1528 if (m->m_flags & M_BCAST) 1529 bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN); 1530 else 1531 bcopy(ar_tha(ah), edst, INFINIBAND_ALEN); 1532 1533 } 1534 break; 1535 #endif 1536 #ifdef INET6 1537 case AF_INET6: 1538 if (lle != NULL && (lle->la_flags & LLE_VALID)) 1539 memcpy(edst, lle->ll_addr, sizeof(edst)); 1540 else if (m->m_flags & M_MCAST) 1541 ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst); 1542 else 1543 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL); 1544 if (error) 1545 return error; 1546 type = htons(ETHERTYPE_IPV6); 1547 break; 1548 #endif 1549 1550 default: 1551 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 1552 error = EAFNOSUPPORT; 1553 goto bad; 1554 } 1555 1556 /* 1557 * Add local net header. If no space in first mbuf, 1558 * allocate another. 1559 */ 1560 M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT); 1561 if (m == NULL) { 1562 error = ENOBUFS; 1563 goto bad; 1564 } 1565 eh = mtod(m, struct ipoib_header *); 1566 (void)memcpy(&eh->proto, &type, sizeof(eh->proto)); 1567 (void)memcpy(&eh->hwaddr, edst, sizeof (edst)); 1568 1569 /* 1570 * Queue message on interface, update output statistics if 1571 * successful, and start output if interface not yet active. 1572 */ 1573 return ((ifp->if_transmit)(ifp, m)); 1574 bad: 1575 if (m != NULL) 1576 m_freem(m); 1577 return (error); 1578 } 1579 1580 /* 1581 * Upper layer processing for a received Infiniband packet. 1582 */ 1583 void 1584 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto) 1585 { 1586 int isr; 1587 1588 #ifdef MAC 1589 /* 1590 * Tag the mbuf with an appropriate MAC label before any other 1591 * consumers can get to it. 1592 */ 1593 mac_ifnet_create_mbuf(ifp, m); 1594 #endif 1595 /* Allow monitor mode to claim this frame, after stats are updated. */ 1596 if (ifp->if_flags & IFF_MONITOR) { 1597 if_printf(ifp, "discard frame at IFF_MONITOR\n"); 1598 m_freem(m); 1599 return; 1600 } 1601 /* 1602 * Dispatch frame to upper layer. 1603 */ 1604 switch (proto) { 1605 #ifdef INET 1606 case ETHERTYPE_IP: 1607 isr = NETISR_IP; 1608 break; 1609 1610 case ETHERTYPE_ARP: 1611 if (ifp->if_flags & IFF_NOARP) { 1612 /* Discard packet if ARP is disabled on interface */ 1613 m_freem(m); 1614 return; 1615 } 1616 isr = NETISR_ARP; 1617 break; 1618 #endif 1619 #ifdef INET6 1620 case ETHERTYPE_IPV6: 1621 isr = NETISR_IPV6; 1622 break; 1623 #endif 1624 default: 1625 goto discard; 1626 } 1627 netisr_dispatch(isr, m); 1628 return; 1629 1630 discard: 1631 m_freem(m); 1632 } 1633 1634 /* 1635 * Process a received Infiniband packet. 1636 */ 1637 static void 1638 ipoib_input(struct ifnet *ifp, struct mbuf *m) 1639 { 1640 struct ipoib_header *eh; 1641 1642 if ((ifp->if_flags & IFF_UP) == 0) { 1643 m_freem(m); 1644 return; 1645 } 1646 CURVNET_SET_QUIET(ifp->if_vnet); 1647 1648 /* Let BPF have it before we strip the header. */ 1649 IPOIB_MTAP(ifp, m); 1650 eh = mtod(m, struct ipoib_header *); 1651 /* 1652 * Reset layer specific mbuf flags to avoid confusing upper layers. 1653 * Strip off Infiniband header. 1654 */ 1655 m->m_flags &= ~M_VLANTAG; 1656 m_clrprotoflags(m); 1657 m_adj(m, IPOIB_HEADER_LEN); 1658 1659 if (IPOIB_IS_MULTICAST(eh->hwaddr)) { 1660 if (memcmp(eh->hwaddr, ifp->if_broadcastaddr, 1661 ifp->if_addrlen) == 0) 1662 m->m_flags |= M_BCAST; 1663 else 1664 m->m_flags |= M_MCAST; 1665 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 1666 } 1667 1668 ipoib_demux(ifp, m, ntohs(eh->proto)); 1669 CURVNET_RESTORE(); 1670 } 1671 1672 static int 1673 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 1674 struct sockaddr *sa) 1675 { 1676 struct sockaddr_dl *sdl; 1677 #ifdef INET 1678 struct sockaddr_in *sin; 1679 #endif 1680 #ifdef INET6 1681 struct sockaddr_in6 *sin6; 1682 #endif 1683 u_char *e_addr; 1684 1685 switch(sa->sa_family) { 1686 case AF_LINK: 1687 /* 1688 * No mapping needed. Just check that it's a valid MC address. 1689 */ 1690 sdl = (struct sockaddr_dl *)sa; 1691 e_addr = LLADDR(sdl); 1692 if (!IPOIB_IS_MULTICAST(e_addr)) 1693 return EADDRNOTAVAIL; 1694 *llsa = NULL; 1695 return 0; 1696 1697 #ifdef INET 1698 case AF_INET: 1699 sin = (struct sockaddr_in *)sa; 1700 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 1701 return EADDRNOTAVAIL; 1702 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND); 1703 sdl->sdl_alen = INFINIBAND_ALEN; 1704 e_addr = LLADDR(sdl); 1705 ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr, 1706 e_addr); 1707 *llsa = (struct sockaddr *)sdl; 1708 return 0; 1709 #endif 1710 #ifdef INET6 1711 case AF_INET6: 1712 sin6 = (struct sockaddr_in6 *)sa; 1713 /* 1714 * An IP6 address of 0 means listen to all 1715 * of the multicast address used for IP6. 1716 * This has no meaning in ipoib. 1717 */ 1718 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 1719 return EADDRNOTAVAIL; 1720 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 1721 return EADDRNOTAVAIL; 1722 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND); 1723 sdl->sdl_alen = INFINIBAND_ALEN; 1724 e_addr = LLADDR(sdl); 1725 ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr); 1726 *llsa = (struct sockaddr *)sdl; 1727 return 0; 1728 #endif 1729 1730 default: 1731 return EAFNOSUPPORT; 1732 } 1733 } 1734 1735 module_init(ipoib_init_module); 1736 module_exit(ipoib_cleanup_module); 1737 1738 static int 1739 ipoib_evhand(module_t mod, int event, void *arg) 1740 { 1741 return (0); 1742 } 1743 1744 static moduledata_t ipoib_mod = { 1745 .name = "ipoib", 1746 .evhand = ipoib_evhand, 1747 }; 1748 1749 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY); 1750 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1); 1751 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1); 1752