1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */ 4 /* Copyright (c) 2008-2019, IBM Corporation */ 5 6 #include <linux/init.h> 7 #include <linux/errno.h> 8 #include <linux/netdevice.h> 9 #include <linux/inetdevice.h> 10 #include <net/net_namespace.h> 11 #include <linux/rtnetlink.h> 12 #include <linux/if_arp.h> 13 #include <linux/list.h> 14 #include <linux/kernel.h> 15 #include <linux/sched.h> 16 #include <linux/module.h> 17 #include <linux/dma-mapping.h> 18 19 #include <net/addrconf.h> 20 #include <rdma/ib_verbs.h> 21 #include <rdma/ib_user_verbs.h> 22 #include <rdma/rdma_netlink.h> 23 #include <linux/kthread.h> 24 25 #include "siw.h" 26 #include "siw_verbs.h" 27 28 MODULE_AUTHOR("Bernard Metzler"); 29 MODULE_DESCRIPTION("Software iWARP Driver"); 30 MODULE_LICENSE("Dual BSD/GPL"); 31 32 /* transmit from user buffer, if possible */ 33 const bool zcopy_tx = true; 34 35 /* Restrict usage of GSO, if hardware peer iwarp is unable to process 36 * large packets. try_gso = true lets siw try to use local GSO, 37 * if peer agrees. Not using GSO severly limits siw maximum tx bandwidth. 38 */ 39 const bool try_gso; 40 41 /* Attach siw also with loopback devices */ 42 const bool loopback_enabled = true; 43 44 /* We try to negotiate CRC on, if true */ 45 const bool mpa_crc_required; 46 47 /* MPA CRC on/off enforced */ 48 const bool mpa_crc_strict; 49 50 /* Control TCP_NODELAY socket option */ 51 const bool siw_tcp_nagle; 52 53 /* Select MPA version to be used during connection setup */ 54 u_char mpa_version = MPA_REVISION_2; 55 56 /* Selects MPA P2P mode (additional handshake during connection 57 * setup, if true. 58 */ 59 const bool peer_to_peer; 60 61 struct task_struct *siw_tx_thread[NR_CPUS]; 62 63 static int siw_device_register(struct siw_device *sdev, const char *name) 64 { 65 struct ib_device *base_dev = &sdev->base_dev; 66 static int dev_id = 1; 67 int rv; 68 69 sdev->vendor_part_id = dev_id++; 70 71 rv = ib_register_device(base_dev, name, NULL); 72 if (rv) { 73 pr_warn("siw: device registration error %d\n", rv); 74 return rv; 75 } 76 77 siw_dbg(base_dev, "HWaddr=%pM\n", sdev->raw_gid); 78 return 0; 79 } 80 81 static void siw_device_cleanup(struct ib_device *base_dev) 82 { 83 struct siw_device *sdev = to_siw_dev(base_dev); 84 85 xa_destroy(&sdev->qp_xa); 86 xa_destroy(&sdev->mem_xa); 87 } 88 89 static int siw_dev_qualified(struct net_device *netdev) 90 { 91 /* 92 * Additional hardware support can be added here 93 * (e.g. ARPHRD_FDDI, ARPHRD_ATM, ...) - see 94 * <linux/if_arp.h> for type identifiers. 95 */ 96 if (netdev->type == ARPHRD_ETHER || netdev->type == ARPHRD_IEEE802 || 97 netdev->type == ARPHRD_NONE || 98 (netdev->type == ARPHRD_LOOPBACK && loopback_enabled)) 99 return 1; 100 101 return 0; 102 } 103 104 static DEFINE_PER_CPU(atomic_t, siw_use_cnt); 105 106 static struct { 107 struct cpumask **tx_valid_cpus; 108 int num_nodes; 109 } siw_cpu_info; 110 111 static void siw_destroy_cpulist(int number) 112 { 113 int i = 0; 114 115 while (i < number) 116 kfree(siw_cpu_info.tx_valid_cpus[i++]); 117 118 kfree(siw_cpu_info.tx_valid_cpus); 119 siw_cpu_info.tx_valid_cpus = NULL; 120 } 121 122 static int siw_init_cpulist(void) 123 { 124 int i, num_nodes = nr_node_ids; 125 126 memset(siw_tx_thread, 0, sizeof(siw_tx_thread)); 127 128 siw_cpu_info.num_nodes = num_nodes; 129 130 siw_cpu_info.tx_valid_cpus = 131 kcalloc(num_nodes, sizeof(struct cpumask *), GFP_KERNEL); 132 if (!siw_cpu_info.tx_valid_cpus) { 133 siw_cpu_info.num_nodes = 0; 134 return -ENOMEM; 135 } 136 for (i = 0; i < siw_cpu_info.num_nodes; i++) { 137 siw_cpu_info.tx_valid_cpus[i] = 138 kzalloc(sizeof(struct cpumask), GFP_KERNEL); 139 if (!siw_cpu_info.tx_valid_cpus[i]) 140 goto out_err; 141 142 cpumask_clear(siw_cpu_info.tx_valid_cpus[i]); 143 } 144 for_each_possible_cpu(i) 145 cpumask_set_cpu(i, siw_cpu_info.tx_valid_cpus[cpu_to_node(i)]); 146 147 return 0; 148 149 out_err: 150 siw_cpu_info.num_nodes = 0; 151 siw_destroy_cpulist(i); 152 153 return -ENOMEM; 154 } 155 156 /* 157 * Choose CPU with least number of active QP's from NUMA node of 158 * TX interface. 159 */ 160 int siw_get_tx_cpu(struct siw_device *sdev) 161 { 162 const struct cpumask *tx_cpumask; 163 int i, num_cpus, cpu, min_use, node = sdev->numa_node, tx_cpu = -1; 164 165 if (node < 0) 166 tx_cpumask = cpu_online_mask; 167 else 168 tx_cpumask = siw_cpu_info.tx_valid_cpus[node]; 169 170 num_cpus = cpumask_weight(tx_cpumask); 171 if (!num_cpus) { 172 /* no CPU on this NUMA node */ 173 tx_cpumask = cpu_online_mask; 174 num_cpus = cpumask_weight(tx_cpumask); 175 } 176 if (!num_cpus) 177 goto out; 178 179 cpu = cpumask_first(tx_cpumask); 180 181 for (i = 0, min_use = SIW_MAX_QP; i < num_cpus; 182 i++, cpu = cpumask_next(cpu, tx_cpumask)) { 183 int usage; 184 185 /* Skip any cores which have no TX thread */ 186 if (!siw_tx_thread[cpu]) 187 continue; 188 189 usage = atomic_read(&per_cpu(siw_use_cnt, cpu)); 190 if (usage <= min_use) { 191 tx_cpu = cpu; 192 min_use = usage; 193 } 194 } 195 siw_dbg(&sdev->base_dev, 196 "tx cpu %d, node %d, %d qp's\n", tx_cpu, node, min_use); 197 198 out: 199 if (tx_cpu >= 0) 200 atomic_inc(&per_cpu(siw_use_cnt, tx_cpu)); 201 else 202 pr_warn("siw: no tx cpu found\n"); 203 204 return tx_cpu; 205 } 206 207 void siw_put_tx_cpu(int cpu) 208 { 209 atomic_dec(&per_cpu(siw_use_cnt, cpu)); 210 } 211 212 static struct ib_qp *siw_get_base_qp(struct ib_device *base_dev, int id) 213 { 214 struct siw_qp *qp = siw_qp_id2obj(to_siw_dev(base_dev), id); 215 216 if (qp) { 217 /* 218 * siw_qp_id2obj() increments object reference count 219 */ 220 siw_qp_put(qp); 221 return &qp->base_qp; 222 } 223 return NULL; 224 } 225 226 static const struct ib_device_ops siw_device_ops = { 227 .owner = THIS_MODULE, 228 .uverbs_abi_ver = SIW_ABI_VERSION, 229 .driver_id = RDMA_DRIVER_SIW, 230 231 .alloc_mr = siw_alloc_mr, 232 .alloc_pd = siw_alloc_pd, 233 .alloc_ucontext = siw_alloc_ucontext, 234 .create_cq = siw_create_cq, 235 .create_qp = siw_create_qp, 236 .create_srq = siw_create_srq, 237 .dealloc_driver = siw_device_cleanup, 238 .dealloc_pd = siw_dealloc_pd, 239 .dealloc_ucontext = siw_dealloc_ucontext, 240 .dereg_mr = siw_dereg_mr, 241 .destroy_cq = siw_destroy_cq, 242 .destroy_qp = siw_destroy_qp, 243 .destroy_srq = siw_destroy_srq, 244 .get_dma_mr = siw_get_dma_mr, 245 .get_port_immutable = siw_get_port_immutable, 246 .iw_accept = siw_accept, 247 .iw_add_ref = siw_qp_get_ref, 248 .iw_connect = siw_connect, 249 .iw_create_listen = siw_create_listen, 250 .iw_destroy_listen = siw_destroy_listen, 251 .iw_get_qp = siw_get_base_qp, 252 .iw_reject = siw_reject, 253 .iw_rem_ref = siw_qp_put_ref, 254 .map_mr_sg = siw_map_mr_sg, 255 .mmap = siw_mmap, 256 .mmap_free = siw_mmap_free, 257 .modify_qp = siw_verbs_modify_qp, 258 .modify_srq = siw_modify_srq, 259 .poll_cq = siw_poll_cq, 260 .post_recv = siw_post_receive, 261 .post_send = siw_post_send, 262 .post_srq_recv = siw_post_srq_recv, 263 .query_device = siw_query_device, 264 .query_gid = siw_query_gid, 265 .query_port = siw_query_port, 266 .query_qp = siw_query_qp, 267 .query_srq = siw_query_srq, 268 .req_notify_cq = siw_req_notify_cq, 269 .reg_user_mr = siw_reg_user_mr, 270 271 INIT_RDMA_OBJ_SIZE(ib_cq, siw_cq, base_cq), 272 INIT_RDMA_OBJ_SIZE(ib_pd, siw_pd, base_pd), 273 INIT_RDMA_OBJ_SIZE(ib_qp, siw_qp, base_qp), 274 INIT_RDMA_OBJ_SIZE(ib_srq, siw_srq, base_srq), 275 INIT_RDMA_OBJ_SIZE(ib_ucontext, siw_ucontext, base_ucontext), 276 }; 277 278 static struct siw_device *siw_device_create(struct net_device *netdev) 279 { 280 struct siw_device *sdev = NULL; 281 struct ib_device *base_dev; 282 int rv; 283 284 sdev = ib_alloc_device(siw_device, base_dev); 285 if (!sdev) 286 return NULL; 287 288 base_dev = &sdev->base_dev; 289 290 if (netdev->addr_len) { 291 memcpy(sdev->raw_gid, netdev->dev_addr, 292 min_t(unsigned int, netdev->addr_len, ETH_ALEN)); 293 } else { 294 /* 295 * This device does not have a HW address, but 296 * connection mangagement requires a unique gid. 297 */ 298 eth_random_addr(sdev->raw_gid); 299 } 300 addrconf_addr_eui48((u8 *)&base_dev->node_guid, sdev->raw_gid); 301 302 base_dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND); 303 304 base_dev->node_type = RDMA_NODE_RNIC; 305 memcpy(base_dev->node_desc, SIW_NODE_DESC_COMMON, 306 sizeof(SIW_NODE_DESC_COMMON)); 307 308 /* 309 * Current model (one-to-one device association): 310 * One Softiwarp device per net_device or, equivalently, 311 * per physical port. 312 */ 313 base_dev->phys_port_cnt = 1; 314 base_dev->num_comp_vectors = num_possible_cpus(); 315 316 xa_init_flags(&sdev->qp_xa, XA_FLAGS_ALLOC1); 317 xa_init_flags(&sdev->mem_xa, XA_FLAGS_ALLOC1); 318 319 ib_set_device_ops(base_dev, &siw_device_ops); 320 rv = ib_device_set_netdev(base_dev, netdev, 1); 321 if (rv) 322 goto error; 323 324 memcpy(base_dev->iw_ifname, netdev->name, 325 sizeof(base_dev->iw_ifname)); 326 327 /* Disable TCP port mapping */ 328 base_dev->iw_driver_flags = IW_F_NO_PORT_MAP; 329 330 sdev->attrs.max_qp = SIW_MAX_QP; 331 sdev->attrs.max_qp_wr = SIW_MAX_QP_WR; 332 sdev->attrs.max_ord = SIW_MAX_ORD_QP; 333 sdev->attrs.max_ird = SIW_MAX_IRD_QP; 334 sdev->attrs.max_sge = SIW_MAX_SGE; 335 sdev->attrs.max_sge_rd = SIW_MAX_SGE_RD; 336 sdev->attrs.max_cq = SIW_MAX_CQ; 337 sdev->attrs.max_cqe = SIW_MAX_CQE; 338 sdev->attrs.max_mr = SIW_MAX_MR; 339 sdev->attrs.max_pd = SIW_MAX_PD; 340 sdev->attrs.max_mw = SIW_MAX_MW; 341 sdev->attrs.max_srq = SIW_MAX_SRQ; 342 sdev->attrs.max_srq_wr = SIW_MAX_SRQ_WR; 343 sdev->attrs.max_srq_sge = SIW_MAX_SGE; 344 345 INIT_LIST_HEAD(&sdev->cep_list); 346 INIT_LIST_HEAD(&sdev->qp_list); 347 348 atomic_set(&sdev->num_ctx, 0); 349 atomic_set(&sdev->num_srq, 0); 350 atomic_set(&sdev->num_qp, 0); 351 atomic_set(&sdev->num_cq, 0); 352 atomic_set(&sdev->num_mr, 0); 353 atomic_set(&sdev->num_pd, 0); 354 355 sdev->numa_node = dev_to_node(&netdev->dev); 356 spin_lock_init(&sdev->lock); 357 358 return sdev; 359 error: 360 ib_dealloc_device(base_dev); 361 362 return NULL; 363 } 364 365 static int siw_netdev_event(struct notifier_block *nb, unsigned long event, 366 void *arg) 367 { 368 struct net_device *netdev = netdev_notifier_info_to_dev(arg); 369 struct ib_device *base_dev; 370 struct siw_device *sdev; 371 372 dev_dbg(&netdev->dev, "siw: event %lu\n", event); 373 374 base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW); 375 if (!base_dev) 376 return NOTIFY_OK; 377 378 sdev = to_siw_dev(base_dev); 379 380 switch (event) { 381 case NETDEV_REGISTER: 382 /* 383 * Device registration now handled only by 384 * rdma netlink commands. So it shall be impossible 385 * to end up here with a valid siw device. 386 */ 387 siw_dbg(base_dev, "unexpected NETDEV_REGISTER event\n"); 388 break; 389 390 case NETDEV_UNREGISTER: 391 ib_unregister_device_queued(&sdev->base_dev); 392 break; 393 394 case NETDEV_CHANGEADDR: 395 siw_port_event(sdev, 1, IB_EVENT_LID_CHANGE); 396 break; 397 /* 398 * All other events are not handled 399 */ 400 default: 401 break; 402 } 403 ib_device_put(&sdev->base_dev); 404 405 return NOTIFY_OK; 406 } 407 408 static struct notifier_block siw_netdev_nb = { 409 .notifier_call = siw_netdev_event, 410 }; 411 412 static int siw_newlink(const char *basedev_name, struct net_device *netdev) 413 { 414 struct ib_device *base_dev; 415 struct siw_device *sdev = NULL; 416 int rv = -ENOMEM; 417 418 if (!siw_dev_qualified(netdev)) 419 return -EINVAL; 420 421 base_dev = ib_device_get_by_netdev(netdev, RDMA_DRIVER_SIW); 422 if (base_dev) { 423 ib_device_put(base_dev); 424 return -EEXIST; 425 } 426 sdev = siw_device_create(netdev); 427 if (sdev) { 428 dev_dbg(&netdev->dev, "siw: new device\n"); 429 ib_mark_name_assigned_by_user(&sdev->base_dev); 430 rv = siw_device_register(sdev, basedev_name); 431 if (rv) 432 ib_dealloc_device(&sdev->base_dev); 433 } 434 return rv; 435 } 436 437 static struct rdma_link_ops siw_link_ops = { 438 .type = "siw", 439 .newlink = siw_newlink, 440 }; 441 442 /* 443 * siw_init_module - Initialize Softiwarp module and register with netdev 444 * subsystem. 445 */ 446 static __init int siw_init_module(void) 447 { 448 int rv; 449 450 if (SENDPAGE_THRESH < SIW_MAX_INLINE) { 451 pr_info("siw: sendpage threshold too small: %u\n", 452 (int)SENDPAGE_THRESH); 453 rv = -EINVAL; 454 goto out_error; 455 } 456 rv = siw_init_cpulist(); 457 if (rv) 458 goto out_error; 459 460 rv = siw_cm_init(); 461 if (rv) 462 goto out_error; 463 464 if (!siw_create_tx_threads()) { 465 pr_info("siw: Could not start any TX thread\n"); 466 rv = -ENOMEM; 467 goto out_error; 468 } 469 470 rv = register_netdevice_notifier(&siw_netdev_nb); 471 if (rv) 472 goto out_error; 473 474 rdma_link_register(&siw_link_ops); 475 476 pr_info("SoftiWARP attached\n"); 477 return 0; 478 479 out_error: 480 siw_stop_tx_threads(); 481 482 pr_info("SoftIWARP attach failed. Error: %d\n", rv); 483 484 siw_cm_exit(); 485 siw_destroy_cpulist(siw_cpu_info.num_nodes); 486 487 return rv; 488 } 489 490 static void __exit siw_exit_module(void) 491 { 492 siw_stop_tx_threads(); 493 494 unregister_netdevice_notifier(&siw_netdev_nb); 495 rdma_link_unregister(&siw_link_ops); 496 ib_unregister_driver(RDMA_DRIVER_SIW); 497 498 siw_cm_exit(); 499 500 siw_destroy_cpulist(siw_cpu_info.num_nodes); 501 502 pr_info("SoftiWARP detached\n"); 503 } 504 505 module_init(siw_init_module); 506 module_exit(siw_exit_module); 507 508 MODULE_ALIAS_RDMA_LINK("siw"); 509