1 /* 2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved. 4 * Copyright (c) 2006 Intel Corporation. All rights reserved. 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenIB.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 */ 34 35 #include <linux/init.h> 36 #include <linux/err.h> 37 #include <linux/random.h> 38 #include <linux/spinlock.h> 39 #include <linux/slab.h> 40 #include <linux/dma-mapping.h> 41 #include <linux/kref.h> 42 #include <linux/xarray.h> 43 #include <linux/workqueue.h> 44 #include <uapi/linux/if_ether.h> 45 #include <rdma/ib_pack.h> 46 #include <rdma/ib_cache.h> 47 #include <rdma/rdma_netlink.h> 48 #include <net/netlink.h> 49 #include <uapi/rdma/ib_user_sa.h> 50 #include <rdma/ib_marshall.h> 51 #include <rdma/ib_addr.h> 52 #include <rdma/opa_addr.h> 53 #include <rdma/rdma_cm.h> 54 #include "sa.h" 55 #include "core_priv.h" 56 57 #define IB_SA_LOCAL_SVC_TIMEOUT_MIN 100 58 #define IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT 2000 59 #define IB_SA_LOCAL_SVC_TIMEOUT_MAX 200000 60 #define IB_SA_CPI_MAX_RETRY_CNT 3 61 #define IB_SA_CPI_RETRY_WAIT 1000 /*msecs */ 62 static int sa_local_svc_timeout_ms = IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT; 63 64 struct ib_sa_sm_ah { 65 struct ib_ah *ah; 66 struct kref ref; 67 u16 pkey_index; 68 u8 src_path_mask; 69 }; 70 71 enum rdma_class_port_info_type { 72 RDMA_CLASS_PORT_INFO_IB, 73 RDMA_CLASS_PORT_INFO_OPA 74 }; 75 76 struct rdma_class_port_info { 77 enum rdma_class_port_info_type type; 78 union { 79 struct ib_class_port_info ib; 80 struct opa_class_port_info opa; 81 }; 82 }; 83 84 struct ib_sa_classport_cache { 85 bool valid; 86 int retry_cnt; 87 struct rdma_class_port_info data; 88 }; 89 90 struct ib_sa_port { 91 struct ib_mad_agent *agent; 92 struct ib_sa_sm_ah *sm_ah; 93 struct work_struct update_task; 94 struct ib_sa_classport_cache classport_info; 95 struct delayed_work ib_cpi_work; 96 spinlock_t classport_lock; /* protects class port info set */ 97 spinlock_t ah_lock; 98 u32 port_num; 99 }; 100 101 struct ib_sa_device { 102 int start_port, end_port; 103 struct ib_event_handler event_handler; 104 struct ib_sa_port port[]; 105 }; 106 107 struct ib_sa_query { 108 void (*callback)(struct ib_sa_query *sa_query, int status, 109 struct ib_sa_mad *mad); 110 void (*rmpp_callback)(struct ib_sa_query *sa_query, int status, 111 struct ib_mad_recv_wc *mad); 112 void (*release)(struct ib_sa_query *); 113 struct ib_sa_client *client; 114 struct ib_sa_port *port; 115 struct ib_mad_send_buf *mad_buf; 116 struct ib_sa_sm_ah *sm_ah; 117 int id; 118 u32 flags; 119 struct list_head list; /* Local svc request list */ 120 u32 seq; /* Local svc request sequence number */ 121 unsigned long timeout; /* Local svc timeout */ 122 u8 path_use; /* How will the pathrecord be used */ 123 }; 124 125 #define IB_SA_ENABLE_LOCAL_SERVICE 0x00000001 126 #define IB_SA_CANCEL 0x00000002 127 #define IB_SA_QUERY_OPA 0x00000004 128 129 struct ib_sa_path_query { 130 void (*callback)(int status, struct sa_path_rec *rec, 131 unsigned int num_paths, void *context); 132 void *context; 133 struct ib_sa_query sa_query; 134 struct sa_path_rec *conv_pr; 135 }; 136 137 struct ib_sa_guidinfo_query { 138 void (*callback)(int, struct ib_sa_guidinfo_rec *, void *); 139 void *context; 140 struct ib_sa_query sa_query; 141 }; 142 143 struct ib_sa_classport_info_query { 144 void (*callback)(void *); 145 void *context; 146 struct ib_sa_query sa_query; 147 }; 148 149 struct ib_sa_mcmember_query { 150 void (*callback)(int, struct ib_sa_mcmember_rec *, void *); 151 void *context; 152 struct ib_sa_query sa_query; 153 }; 154 155 struct ib_sa_service_query { 156 void (*callback)(int status, struct sa_service_rec *rec, 157 unsigned int num_services, void *context); 158 void *context; 159 struct ib_sa_query sa_query; 160 }; 161 162 static LIST_HEAD(ib_nl_request_list); 163 static DEFINE_SPINLOCK(ib_nl_request_lock); 164 static atomic_t ib_nl_sa_request_seq; 165 static struct workqueue_struct *ib_nl_wq; 166 static struct delayed_work ib_nl_timed_work; 167 static const struct nla_policy ib_nl_policy[LS_NLA_TYPE_MAX] = { 168 [LS_NLA_TYPE_PATH_RECORD] = {.type = NLA_BINARY, 169 .len = sizeof(struct ib_path_rec_data)}, 170 [LS_NLA_TYPE_TIMEOUT] = {.type = NLA_U32}, 171 [LS_NLA_TYPE_SERVICE_ID] = {.type = NLA_U64}, 172 [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY, 173 .len = sizeof(struct rdma_nla_ls_gid)}, 174 [LS_NLA_TYPE_SGID] = {.type = NLA_BINARY, 175 .len = sizeof(struct rdma_nla_ls_gid)}, 176 [LS_NLA_TYPE_TCLASS] = {.type = NLA_U8}, 177 [LS_NLA_TYPE_PKEY] = {.type = NLA_U16}, 178 [LS_NLA_TYPE_QOS_CLASS] = {.type = NLA_U16}, 179 }; 180 181 182 static int ib_sa_add_one(struct ib_device *device); 183 static void ib_sa_remove_one(struct ib_device *device, void *client_data); 184 185 static struct ib_client sa_client = { 186 .name = "sa", 187 .add = ib_sa_add_one, 188 .remove = ib_sa_remove_one 189 }; 190 191 static DEFINE_XARRAY_FLAGS(queries, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ); 192 193 static DEFINE_SPINLOCK(tid_lock); 194 static u32 tid; 195 196 #define PATH_REC_FIELD(field) \ 197 .struct_offset_bytes = offsetof(struct sa_path_rec, field), \ 198 .struct_size_bytes = sizeof_field(struct sa_path_rec, field), \ 199 .field_name = "sa_path_rec:" #field 200 201 static const struct ib_field path_rec_table[] = { 202 { PATH_REC_FIELD(service_id), 203 .offset_words = 0, 204 .offset_bits = 0, 205 .size_bits = 64 }, 206 { PATH_REC_FIELD(dgid), 207 .offset_words = 2, 208 .offset_bits = 0, 209 .size_bits = 128 }, 210 { PATH_REC_FIELD(sgid), 211 .offset_words = 6, 212 .offset_bits = 0, 213 .size_bits = 128 }, 214 { PATH_REC_FIELD(ib.dlid), 215 .offset_words = 10, 216 .offset_bits = 0, 217 .size_bits = 16 }, 218 { PATH_REC_FIELD(ib.slid), 219 .offset_words = 10, 220 .offset_bits = 16, 221 .size_bits = 16 }, 222 { PATH_REC_FIELD(ib.raw_traffic), 223 .offset_words = 11, 224 .offset_bits = 0, 225 .size_bits = 1 }, 226 { RESERVED, 227 .offset_words = 11, 228 .offset_bits = 1, 229 .size_bits = 3 }, 230 { PATH_REC_FIELD(flow_label), 231 .offset_words = 11, 232 .offset_bits = 4, 233 .size_bits = 20 }, 234 { PATH_REC_FIELD(hop_limit), 235 .offset_words = 11, 236 .offset_bits = 24, 237 .size_bits = 8 }, 238 { PATH_REC_FIELD(traffic_class), 239 .offset_words = 12, 240 .offset_bits = 0, 241 .size_bits = 8 }, 242 { PATH_REC_FIELD(reversible), 243 .offset_words = 12, 244 .offset_bits = 8, 245 .size_bits = 1 }, 246 { PATH_REC_FIELD(numb_path), 247 .offset_words = 12, 248 .offset_bits = 9, 249 .size_bits = 7 }, 250 { PATH_REC_FIELD(pkey), 251 .offset_words = 12, 252 .offset_bits = 16, 253 .size_bits = 16 }, 254 { PATH_REC_FIELD(qos_class), 255 .offset_words = 13, 256 .offset_bits = 0, 257 .size_bits = 12 }, 258 { PATH_REC_FIELD(sl), 259 .offset_words = 13, 260 .offset_bits = 12, 261 .size_bits = 4 }, 262 { PATH_REC_FIELD(mtu_selector), 263 .offset_words = 13, 264 .offset_bits = 16, 265 .size_bits = 2 }, 266 { PATH_REC_FIELD(mtu), 267 .offset_words = 13, 268 .offset_bits = 18, 269 .size_bits = 6 }, 270 { PATH_REC_FIELD(rate_selector), 271 .offset_words = 13, 272 .offset_bits = 24, 273 .size_bits = 2 }, 274 { PATH_REC_FIELD(rate), 275 .offset_words = 13, 276 .offset_bits = 26, 277 .size_bits = 6 }, 278 { PATH_REC_FIELD(packet_life_time_selector), 279 .offset_words = 14, 280 .offset_bits = 0, 281 .size_bits = 2 }, 282 { PATH_REC_FIELD(packet_life_time), 283 .offset_words = 14, 284 .offset_bits = 2, 285 .size_bits = 6 }, 286 { PATH_REC_FIELD(preference), 287 .offset_words = 14, 288 .offset_bits = 8, 289 .size_bits = 8 }, 290 { RESERVED, 291 .offset_words = 14, 292 .offset_bits = 16, 293 .size_bits = 48 }, 294 }; 295 296 #define OPA_PATH_REC_FIELD(field) \ 297 .struct_offset_bytes = \ 298 offsetof(struct sa_path_rec, field), \ 299 .struct_size_bytes = \ 300 sizeof_field(struct sa_path_rec, field), \ 301 .field_name = "sa_path_rec:" #field 302 303 static const struct ib_field opa_path_rec_table[] = { 304 { OPA_PATH_REC_FIELD(service_id), 305 .offset_words = 0, 306 .offset_bits = 0, 307 .size_bits = 64 }, 308 { OPA_PATH_REC_FIELD(dgid), 309 .offset_words = 2, 310 .offset_bits = 0, 311 .size_bits = 128 }, 312 { OPA_PATH_REC_FIELD(sgid), 313 .offset_words = 6, 314 .offset_bits = 0, 315 .size_bits = 128 }, 316 { OPA_PATH_REC_FIELD(opa.dlid), 317 .offset_words = 10, 318 .offset_bits = 0, 319 .size_bits = 32 }, 320 { OPA_PATH_REC_FIELD(opa.slid), 321 .offset_words = 11, 322 .offset_bits = 0, 323 .size_bits = 32 }, 324 { OPA_PATH_REC_FIELD(opa.raw_traffic), 325 .offset_words = 12, 326 .offset_bits = 0, 327 .size_bits = 1 }, 328 { RESERVED, 329 .offset_words = 12, 330 .offset_bits = 1, 331 .size_bits = 3 }, 332 { OPA_PATH_REC_FIELD(flow_label), 333 .offset_words = 12, 334 .offset_bits = 4, 335 .size_bits = 20 }, 336 { OPA_PATH_REC_FIELD(hop_limit), 337 .offset_words = 12, 338 .offset_bits = 24, 339 .size_bits = 8 }, 340 { OPA_PATH_REC_FIELD(traffic_class), 341 .offset_words = 13, 342 .offset_bits = 0, 343 .size_bits = 8 }, 344 { OPA_PATH_REC_FIELD(reversible), 345 .offset_words = 13, 346 .offset_bits = 8, 347 .size_bits = 1 }, 348 { OPA_PATH_REC_FIELD(numb_path), 349 .offset_words = 13, 350 .offset_bits = 9, 351 .size_bits = 7 }, 352 { OPA_PATH_REC_FIELD(pkey), 353 .offset_words = 13, 354 .offset_bits = 16, 355 .size_bits = 16 }, 356 { OPA_PATH_REC_FIELD(opa.l2_8B), 357 .offset_words = 14, 358 .offset_bits = 0, 359 .size_bits = 1 }, 360 { OPA_PATH_REC_FIELD(opa.l2_10B), 361 .offset_words = 14, 362 .offset_bits = 1, 363 .size_bits = 1 }, 364 { OPA_PATH_REC_FIELD(opa.l2_9B), 365 .offset_words = 14, 366 .offset_bits = 2, 367 .size_bits = 1 }, 368 { OPA_PATH_REC_FIELD(opa.l2_16B), 369 .offset_words = 14, 370 .offset_bits = 3, 371 .size_bits = 1 }, 372 { RESERVED, 373 .offset_words = 14, 374 .offset_bits = 4, 375 .size_bits = 2 }, 376 { OPA_PATH_REC_FIELD(opa.qos_type), 377 .offset_words = 14, 378 .offset_bits = 6, 379 .size_bits = 2 }, 380 { OPA_PATH_REC_FIELD(opa.qos_priority), 381 .offset_words = 14, 382 .offset_bits = 8, 383 .size_bits = 8 }, 384 { RESERVED, 385 .offset_words = 14, 386 .offset_bits = 16, 387 .size_bits = 3 }, 388 { OPA_PATH_REC_FIELD(sl), 389 .offset_words = 14, 390 .offset_bits = 19, 391 .size_bits = 5 }, 392 { RESERVED, 393 .offset_words = 14, 394 .offset_bits = 24, 395 .size_bits = 8 }, 396 { OPA_PATH_REC_FIELD(mtu_selector), 397 .offset_words = 15, 398 .offset_bits = 0, 399 .size_bits = 2 }, 400 { OPA_PATH_REC_FIELD(mtu), 401 .offset_words = 15, 402 .offset_bits = 2, 403 .size_bits = 6 }, 404 { OPA_PATH_REC_FIELD(rate_selector), 405 .offset_words = 15, 406 .offset_bits = 8, 407 .size_bits = 2 }, 408 { OPA_PATH_REC_FIELD(rate), 409 .offset_words = 15, 410 .offset_bits = 10, 411 .size_bits = 6 }, 412 { OPA_PATH_REC_FIELD(packet_life_time_selector), 413 .offset_words = 15, 414 .offset_bits = 16, 415 .size_bits = 2 }, 416 { OPA_PATH_REC_FIELD(packet_life_time), 417 .offset_words = 15, 418 .offset_bits = 18, 419 .size_bits = 6 }, 420 { OPA_PATH_REC_FIELD(preference), 421 .offset_words = 15, 422 .offset_bits = 24, 423 .size_bits = 8 }, 424 }; 425 426 #define MCMEMBER_REC_FIELD(field) \ 427 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \ 428 .struct_size_bytes = sizeof_field(struct ib_sa_mcmember_rec, field), \ 429 .field_name = "sa_mcmember_rec:" #field 430 431 static const struct ib_field mcmember_rec_table[] = { 432 { MCMEMBER_REC_FIELD(mgid), 433 .offset_words = 0, 434 .offset_bits = 0, 435 .size_bits = 128 }, 436 { MCMEMBER_REC_FIELD(port_gid), 437 .offset_words = 4, 438 .offset_bits = 0, 439 .size_bits = 128 }, 440 { MCMEMBER_REC_FIELD(qkey), 441 .offset_words = 8, 442 .offset_bits = 0, 443 .size_bits = 32 }, 444 { MCMEMBER_REC_FIELD(mlid), 445 .offset_words = 9, 446 .offset_bits = 0, 447 .size_bits = 16 }, 448 { MCMEMBER_REC_FIELD(mtu_selector), 449 .offset_words = 9, 450 .offset_bits = 16, 451 .size_bits = 2 }, 452 { MCMEMBER_REC_FIELD(mtu), 453 .offset_words = 9, 454 .offset_bits = 18, 455 .size_bits = 6 }, 456 { MCMEMBER_REC_FIELD(traffic_class), 457 .offset_words = 9, 458 .offset_bits = 24, 459 .size_bits = 8 }, 460 { MCMEMBER_REC_FIELD(pkey), 461 .offset_words = 10, 462 .offset_bits = 0, 463 .size_bits = 16 }, 464 { MCMEMBER_REC_FIELD(rate_selector), 465 .offset_words = 10, 466 .offset_bits = 16, 467 .size_bits = 2 }, 468 { MCMEMBER_REC_FIELD(rate), 469 .offset_words = 10, 470 .offset_bits = 18, 471 .size_bits = 6 }, 472 { MCMEMBER_REC_FIELD(packet_life_time_selector), 473 .offset_words = 10, 474 .offset_bits = 24, 475 .size_bits = 2 }, 476 { MCMEMBER_REC_FIELD(packet_life_time), 477 .offset_words = 10, 478 .offset_bits = 26, 479 .size_bits = 6 }, 480 { MCMEMBER_REC_FIELD(sl), 481 .offset_words = 11, 482 .offset_bits = 0, 483 .size_bits = 4 }, 484 { MCMEMBER_REC_FIELD(flow_label), 485 .offset_words = 11, 486 .offset_bits = 4, 487 .size_bits = 20 }, 488 { MCMEMBER_REC_FIELD(hop_limit), 489 .offset_words = 11, 490 .offset_bits = 24, 491 .size_bits = 8 }, 492 { MCMEMBER_REC_FIELD(scope), 493 .offset_words = 12, 494 .offset_bits = 0, 495 .size_bits = 4 }, 496 { MCMEMBER_REC_FIELD(join_state), 497 .offset_words = 12, 498 .offset_bits = 4, 499 .size_bits = 4 }, 500 { MCMEMBER_REC_FIELD(proxy_join), 501 .offset_words = 12, 502 .offset_bits = 8, 503 .size_bits = 1 }, 504 { RESERVED, 505 .offset_words = 12, 506 .offset_bits = 9, 507 .size_bits = 23 }, 508 }; 509 510 #define CLASSPORTINFO_REC_FIELD(field) \ 511 .struct_offset_bytes = offsetof(struct ib_class_port_info, field), \ 512 .struct_size_bytes = sizeof_field(struct ib_class_port_info, field), \ 513 .field_name = "ib_class_port_info:" #field 514 515 static const struct ib_field ib_classport_info_rec_table[] = { 516 { CLASSPORTINFO_REC_FIELD(base_version), 517 .offset_words = 0, 518 .offset_bits = 0, 519 .size_bits = 8 }, 520 { CLASSPORTINFO_REC_FIELD(class_version), 521 .offset_words = 0, 522 .offset_bits = 8, 523 .size_bits = 8 }, 524 { CLASSPORTINFO_REC_FIELD(capability_mask), 525 .offset_words = 0, 526 .offset_bits = 16, 527 .size_bits = 16 }, 528 { CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time), 529 .offset_words = 1, 530 .offset_bits = 0, 531 .size_bits = 32 }, 532 { CLASSPORTINFO_REC_FIELD(redirect_gid), 533 .offset_words = 2, 534 .offset_bits = 0, 535 .size_bits = 128 }, 536 { CLASSPORTINFO_REC_FIELD(redirect_tcslfl), 537 .offset_words = 6, 538 .offset_bits = 0, 539 .size_bits = 32 }, 540 { CLASSPORTINFO_REC_FIELD(redirect_lid), 541 .offset_words = 7, 542 .offset_bits = 0, 543 .size_bits = 16 }, 544 { CLASSPORTINFO_REC_FIELD(redirect_pkey), 545 .offset_words = 7, 546 .offset_bits = 16, 547 .size_bits = 16 }, 548 549 { CLASSPORTINFO_REC_FIELD(redirect_qp), 550 .offset_words = 8, 551 .offset_bits = 0, 552 .size_bits = 32 }, 553 { CLASSPORTINFO_REC_FIELD(redirect_qkey), 554 .offset_words = 9, 555 .offset_bits = 0, 556 .size_bits = 32 }, 557 558 { CLASSPORTINFO_REC_FIELD(trap_gid), 559 .offset_words = 10, 560 .offset_bits = 0, 561 .size_bits = 128 }, 562 { CLASSPORTINFO_REC_FIELD(trap_tcslfl), 563 .offset_words = 14, 564 .offset_bits = 0, 565 .size_bits = 32 }, 566 567 { CLASSPORTINFO_REC_FIELD(trap_lid), 568 .offset_words = 15, 569 .offset_bits = 0, 570 .size_bits = 16 }, 571 { CLASSPORTINFO_REC_FIELD(trap_pkey), 572 .offset_words = 15, 573 .offset_bits = 16, 574 .size_bits = 16 }, 575 576 { CLASSPORTINFO_REC_FIELD(trap_hlqp), 577 .offset_words = 16, 578 .offset_bits = 0, 579 .size_bits = 32 }, 580 { CLASSPORTINFO_REC_FIELD(trap_qkey), 581 .offset_words = 17, 582 .offset_bits = 0, 583 .size_bits = 32 }, 584 }; 585 586 #define OPA_CLASSPORTINFO_REC_FIELD(field) \ 587 .struct_offset_bytes =\ 588 offsetof(struct opa_class_port_info, field), \ 589 .struct_size_bytes = \ 590 sizeof_field(struct opa_class_port_info, field), \ 591 .field_name = "opa_class_port_info:" #field 592 593 static const struct ib_field opa_classport_info_rec_table[] = { 594 { OPA_CLASSPORTINFO_REC_FIELD(base_version), 595 .offset_words = 0, 596 .offset_bits = 0, 597 .size_bits = 8 }, 598 { OPA_CLASSPORTINFO_REC_FIELD(class_version), 599 .offset_words = 0, 600 .offset_bits = 8, 601 .size_bits = 8 }, 602 { OPA_CLASSPORTINFO_REC_FIELD(cap_mask), 603 .offset_words = 0, 604 .offset_bits = 16, 605 .size_bits = 16 }, 606 { OPA_CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time), 607 .offset_words = 1, 608 .offset_bits = 0, 609 .size_bits = 32 }, 610 { OPA_CLASSPORTINFO_REC_FIELD(redirect_gid), 611 .offset_words = 2, 612 .offset_bits = 0, 613 .size_bits = 128 }, 614 { OPA_CLASSPORTINFO_REC_FIELD(redirect_tc_fl), 615 .offset_words = 6, 616 .offset_bits = 0, 617 .size_bits = 32 }, 618 { OPA_CLASSPORTINFO_REC_FIELD(redirect_lid), 619 .offset_words = 7, 620 .offset_bits = 0, 621 .size_bits = 32 }, 622 { OPA_CLASSPORTINFO_REC_FIELD(redirect_sl_qp), 623 .offset_words = 8, 624 .offset_bits = 0, 625 .size_bits = 32 }, 626 { OPA_CLASSPORTINFO_REC_FIELD(redirect_qkey), 627 .offset_words = 9, 628 .offset_bits = 0, 629 .size_bits = 32 }, 630 { OPA_CLASSPORTINFO_REC_FIELD(trap_gid), 631 .offset_words = 10, 632 .offset_bits = 0, 633 .size_bits = 128 }, 634 { OPA_CLASSPORTINFO_REC_FIELD(trap_tc_fl), 635 .offset_words = 14, 636 .offset_bits = 0, 637 .size_bits = 32 }, 638 { OPA_CLASSPORTINFO_REC_FIELD(trap_lid), 639 .offset_words = 15, 640 .offset_bits = 0, 641 .size_bits = 32 }, 642 { OPA_CLASSPORTINFO_REC_FIELD(trap_hl_qp), 643 .offset_words = 16, 644 .offset_bits = 0, 645 .size_bits = 32 }, 646 { OPA_CLASSPORTINFO_REC_FIELD(trap_qkey), 647 .offset_words = 17, 648 .offset_bits = 0, 649 .size_bits = 32 }, 650 { OPA_CLASSPORTINFO_REC_FIELD(trap_pkey), 651 .offset_words = 18, 652 .offset_bits = 0, 653 .size_bits = 16 }, 654 { OPA_CLASSPORTINFO_REC_FIELD(redirect_pkey), 655 .offset_words = 18, 656 .offset_bits = 16, 657 .size_bits = 16 }, 658 { OPA_CLASSPORTINFO_REC_FIELD(trap_sl_rsvd), 659 .offset_words = 19, 660 .offset_bits = 0, 661 .size_bits = 8 }, 662 { RESERVED, 663 .offset_words = 19, 664 .offset_bits = 8, 665 .size_bits = 24 }, 666 }; 667 668 #define GUIDINFO_REC_FIELD(field) \ 669 .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field), \ 670 .struct_size_bytes = sizeof_field(struct ib_sa_guidinfo_rec, field), \ 671 .field_name = "sa_guidinfo_rec:" #field 672 673 static const struct ib_field guidinfo_rec_table[] = { 674 { GUIDINFO_REC_FIELD(lid), 675 .offset_words = 0, 676 .offset_bits = 0, 677 .size_bits = 16 }, 678 { GUIDINFO_REC_FIELD(block_num), 679 .offset_words = 0, 680 .offset_bits = 16, 681 .size_bits = 8 }, 682 { GUIDINFO_REC_FIELD(res1), 683 .offset_words = 0, 684 .offset_bits = 24, 685 .size_bits = 8 }, 686 { GUIDINFO_REC_FIELD(res2), 687 .offset_words = 1, 688 .offset_bits = 0, 689 .size_bits = 32 }, 690 { GUIDINFO_REC_FIELD(guid_info_list), 691 .offset_words = 2, 692 .offset_bits = 0, 693 .size_bits = 512 }, 694 }; 695 696 #define SERVICE_REC_FIELD(field) \ 697 .struct_offset_bytes = offsetof(struct sa_service_rec, field), \ 698 .struct_size_bytes = sizeof_field(struct sa_service_rec, field), \ 699 .field_name = "sa_service_rec:" #field 700 701 static const struct ib_field service_rec_table[] = { 702 { SERVICE_REC_FIELD(id), 703 .offset_words = 0, 704 .offset_bits = 0, 705 .size_bits = 64 }, 706 { SERVICE_REC_FIELD(gid), 707 .offset_words = 2, 708 .offset_bits = 0, 709 .size_bits = 128 }, 710 { SERVICE_REC_FIELD(pkey), 711 .offset_words = 6, 712 .offset_bits = 0, 713 .size_bits = 16 }, 714 { RESERVED, 715 .offset_words = 6, 716 .offset_bits = 16, 717 .size_bits = 16 }, 718 { SERVICE_REC_FIELD(lease), 719 .offset_words = 7, 720 .offset_bits = 0, 721 .size_bits = 32 }, 722 { SERVICE_REC_FIELD(key), 723 .offset_words = 8, 724 .offset_bits = 0, 725 .size_bits = 128 }, 726 { SERVICE_REC_FIELD(name), 727 .offset_words = 12, 728 .offset_bits = 0, 729 .size_bits = 512 }, 730 { SERVICE_REC_FIELD(data_8), 731 .offset_words = 28, 732 .offset_bits = 0, 733 .size_bits = 128 }, 734 { SERVICE_REC_FIELD(data_16), 735 .offset_words = 32, 736 .offset_bits = 0, 737 .size_bits = 128 }, 738 { SERVICE_REC_FIELD(data_32), 739 .offset_words = 36, 740 .offset_bits = 0, 741 .size_bits = 128 }, 742 { SERVICE_REC_FIELD(data_64), 743 .offset_words = 40, 744 .offset_bits = 0, 745 .size_bits = 128 }, 746 }; 747 748 #define RDMA_PRIMARY_PATH_MAX_REC_NUM 3 749 750 static inline void ib_sa_disable_local_svc(struct ib_sa_query *query) 751 { 752 query->flags &= ~IB_SA_ENABLE_LOCAL_SERVICE; 753 } 754 755 static inline int ib_sa_query_cancelled(struct ib_sa_query *query) 756 { 757 return (query->flags & IB_SA_CANCEL); 758 } 759 760 static void ib_nl_set_path_rec_attrs(struct sk_buff *skb, 761 struct ib_sa_query *query) 762 { 763 struct sa_path_rec *sa_rec = query->mad_buf->context[1]; 764 struct ib_sa_mad *mad = query->mad_buf->mad; 765 ib_sa_comp_mask comp_mask = mad->sa_hdr.comp_mask; 766 u16 val16; 767 u64 val64; 768 struct rdma_ls_resolve_header *header; 769 770 query->mad_buf->context[1] = NULL; 771 772 /* Construct the family header first */ 773 header = skb_put(skb, NLMSG_ALIGN(sizeof(*header))); 774 strscpy_pad(header->device_name, 775 dev_name(&query->port->agent->device->dev), 776 LS_DEVICE_NAME_MAX); 777 header->port_num = query->port->port_num; 778 779 if ((comp_mask & IB_SA_PATH_REC_REVERSIBLE) && 780 sa_rec->reversible != 0) 781 query->path_use = LS_RESOLVE_PATH_USE_ALL; 782 else 783 query->path_use = LS_RESOLVE_PATH_USE_UNIDIRECTIONAL; 784 header->path_use = query->path_use; 785 786 /* Now build the attributes */ 787 if (comp_mask & IB_SA_PATH_REC_SERVICE_ID) { 788 val64 = be64_to_cpu(sa_rec->service_id); 789 nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SERVICE_ID, 790 sizeof(val64), &val64); 791 } 792 if (comp_mask & IB_SA_PATH_REC_DGID) 793 nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_DGID, 794 sizeof(sa_rec->dgid), &sa_rec->dgid); 795 if (comp_mask & IB_SA_PATH_REC_SGID) 796 nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SGID, 797 sizeof(sa_rec->sgid), &sa_rec->sgid); 798 if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS) 799 nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_TCLASS, 800 sizeof(sa_rec->traffic_class), &sa_rec->traffic_class); 801 802 if (comp_mask & IB_SA_PATH_REC_PKEY) { 803 val16 = be16_to_cpu(sa_rec->pkey); 804 nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_PKEY, 805 sizeof(val16), &val16); 806 } 807 if (comp_mask & IB_SA_PATH_REC_QOS_CLASS) { 808 val16 = be16_to_cpu(sa_rec->qos_class); 809 nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_QOS_CLASS, 810 sizeof(val16), &val16); 811 } 812 } 813 814 static int ib_nl_get_path_rec_attrs_len(ib_sa_comp_mask comp_mask) 815 { 816 int len = 0; 817 818 if (comp_mask & IB_SA_PATH_REC_SERVICE_ID) 819 len += nla_total_size(sizeof(u64)); 820 if (comp_mask & IB_SA_PATH_REC_DGID) 821 len += nla_total_size(sizeof(struct rdma_nla_ls_gid)); 822 if (comp_mask & IB_SA_PATH_REC_SGID) 823 len += nla_total_size(sizeof(struct rdma_nla_ls_gid)); 824 if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS) 825 len += nla_total_size(sizeof(u8)); 826 if (comp_mask & IB_SA_PATH_REC_PKEY) 827 len += nla_total_size(sizeof(u16)); 828 if (comp_mask & IB_SA_PATH_REC_QOS_CLASS) 829 len += nla_total_size(sizeof(u16)); 830 831 /* 832 * Make sure that at least some of the required comp_mask bits are 833 * set. 834 */ 835 if (WARN_ON(len == 0)) 836 return len; 837 838 /* Add the family header */ 839 len += NLMSG_ALIGN(sizeof(struct rdma_ls_resolve_header)); 840 841 return len; 842 } 843 844 static int ib_nl_make_request(struct ib_sa_query *query, gfp_t gfp_mask) 845 { 846 struct sk_buff *skb = NULL; 847 struct nlmsghdr *nlh; 848 void *data; 849 struct ib_sa_mad *mad; 850 int len; 851 unsigned long flags; 852 unsigned long delay; 853 gfp_t gfp_flag; 854 int ret; 855 856 INIT_LIST_HEAD(&query->list); 857 query->seq = (u32)atomic_inc_return(&ib_nl_sa_request_seq); 858 859 mad = query->mad_buf->mad; 860 len = ib_nl_get_path_rec_attrs_len(mad->sa_hdr.comp_mask); 861 if (len <= 0) 862 return -EMSGSIZE; 863 864 skb = nlmsg_new(len, gfp_mask); 865 if (!skb) 866 return -ENOMEM; 867 868 /* Put nlmsg header only for now */ 869 data = ibnl_put_msg(skb, &nlh, query->seq, 0, RDMA_NL_LS, 870 RDMA_NL_LS_OP_RESOLVE, NLM_F_REQUEST); 871 if (!data) { 872 nlmsg_free(skb); 873 return -EMSGSIZE; 874 } 875 876 /* Add attributes */ 877 ib_nl_set_path_rec_attrs(skb, query); 878 879 /* Repair the nlmsg header length */ 880 nlmsg_end(skb, nlh); 881 882 gfp_flag = ((gfp_mask & GFP_ATOMIC) == GFP_ATOMIC) ? GFP_ATOMIC : 883 GFP_NOWAIT; 884 885 spin_lock_irqsave(&ib_nl_request_lock, flags); 886 ret = rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_LS, gfp_flag); 887 888 if (ret) 889 goto out; 890 891 /* Put the request on the list.*/ 892 delay = msecs_to_jiffies(sa_local_svc_timeout_ms); 893 query->timeout = delay + jiffies; 894 list_add_tail(&query->list, &ib_nl_request_list); 895 /* Start the timeout if this is the only request */ 896 if (ib_nl_request_list.next == &query->list) 897 queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay); 898 899 out: 900 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 901 902 return ret; 903 } 904 905 static int ib_nl_cancel_request(struct ib_sa_query *query) 906 { 907 unsigned long flags; 908 struct ib_sa_query *wait_query; 909 int found = 0; 910 911 spin_lock_irqsave(&ib_nl_request_lock, flags); 912 list_for_each_entry(wait_query, &ib_nl_request_list, list) { 913 /* Let the timeout to take care of the callback */ 914 if (query == wait_query) { 915 query->flags |= IB_SA_CANCEL; 916 query->timeout = jiffies; 917 list_move(&query->list, &ib_nl_request_list); 918 found = 1; 919 mod_delayed_work(ib_nl_wq, &ib_nl_timed_work, 1); 920 break; 921 } 922 } 923 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 924 925 return found; 926 } 927 928 static void send_handler(struct ib_mad_agent *agent, 929 struct ib_mad_send_wc *mad_send_wc); 930 931 static void ib_nl_process_good_resolve_rsp(struct ib_sa_query *query, 932 const struct nlmsghdr *nlh) 933 { 934 struct sa_path_rec recs[RDMA_PRIMARY_PATH_MAX_REC_NUM]; 935 struct ib_sa_path_query *path_query; 936 struct ib_path_rec_data *rec_data; 937 struct ib_mad_send_wc mad_send_wc; 938 const struct nlattr *head, *curr; 939 struct ib_sa_mad *mad = NULL; 940 int len, rem, status = -EIO; 941 unsigned int num_prs = 0; 942 u32 mask = 0; 943 944 if (!query->callback) 945 goto out; 946 947 path_query = container_of(query, struct ib_sa_path_query, sa_query); 948 mad = query->mad_buf->mad; 949 950 head = (const struct nlattr *) nlmsg_data(nlh); 951 len = nlmsg_len(nlh); 952 switch (query->path_use) { 953 case LS_RESOLVE_PATH_USE_UNIDIRECTIONAL: 954 mask = IB_PATH_PRIMARY | IB_PATH_OUTBOUND; 955 break; 956 957 case LS_RESOLVE_PATH_USE_ALL: 958 mask = IB_PATH_PRIMARY; 959 break; 960 961 case LS_RESOLVE_PATH_USE_GMP: 962 default: 963 mask = IB_PATH_PRIMARY | IB_PATH_GMP | 964 IB_PATH_BIDIRECTIONAL; 965 break; 966 } 967 968 nla_for_each_attr(curr, head, len, rem) { 969 if (curr->nla_type != LS_NLA_TYPE_PATH_RECORD) 970 continue; 971 972 rec_data = nla_data(curr); 973 if ((rec_data->flags & mask) != mask) 974 continue; 975 976 if ((query->flags & IB_SA_QUERY_OPA) || 977 path_query->conv_pr) { 978 mad->mad_hdr.method |= IB_MGMT_METHOD_RESP; 979 memcpy(mad->data, rec_data->path_rec, 980 sizeof(rec_data->path_rec)); 981 query->callback(query, 0, mad); 982 goto out; 983 } 984 985 status = 0; 986 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), 987 rec_data->path_rec, &recs[num_prs]); 988 recs[num_prs].flags = rec_data->flags; 989 recs[num_prs].rec_type = SA_PATH_REC_TYPE_IB; 990 sa_path_set_dmac_zero(&recs[num_prs]); 991 992 num_prs++; 993 if (num_prs >= RDMA_PRIMARY_PATH_MAX_REC_NUM) 994 break; 995 } 996 997 if (!status) { 998 mad->mad_hdr.method |= IB_MGMT_METHOD_RESP; 999 path_query->callback(status, recs, num_prs, 1000 path_query->context); 1001 } else 1002 query->callback(query, status, mad); 1003 1004 out: 1005 mad_send_wc.send_buf = query->mad_buf; 1006 mad_send_wc.status = IB_WC_SUCCESS; 1007 send_handler(query->mad_buf->mad_agent, &mad_send_wc); 1008 } 1009 1010 static void ib_nl_request_timeout(struct work_struct *work) 1011 { 1012 unsigned long flags; 1013 struct ib_sa_query *query; 1014 unsigned long delay; 1015 struct ib_mad_send_wc mad_send_wc; 1016 int ret; 1017 1018 spin_lock_irqsave(&ib_nl_request_lock, flags); 1019 while (!list_empty(&ib_nl_request_list)) { 1020 query = list_entry(ib_nl_request_list.next, 1021 struct ib_sa_query, list); 1022 1023 if (time_after(query->timeout, jiffies)) { 1024 delay = query->timeout - jiffies; 1025 if ((long)delay <= 0) 1026 delay = 1; 1027 queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay); 1028 break; 1029 } 1030 1031 list_del(&query->list); 1032 ib_sa_disable_local_svc(query); 1033 /* Hold the lock to protect against query cancellation */ 1034 if (ib_sa_query_cancelled(query)) 1035 ret = -1; 1036 else 1037 ret = ib_post_send_mad(query->mad_buf, NULL); 1038 if (ret) { 1039 mad_send_wc.send_buf = query->mad_buf; 1040 mad_send_wc.status = IB_WC_WR_FLUSH_ERR; 1041 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 1042 send_handler(query->port->agent, &mad_send_wc); 1043 spin_lock_irqsave(&ib_nl_request_lock, flags); 1044 } 1045 } 1046 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 1047 } 1048 1049 int ib_nl_handle_set_timeout(struct sk_buff *skb, 1050 struct nlmsghdr *nlh, 1051 struct netlink_ext_ack *extack) 1052 { 1053 int timeout, delta, abs_delta; 1054 const struct nlattr *attr; 1055 unsigned long flags; 1056 struct ib_sa_query *query; 1057 long delay = 0; 1058 struct nlattr *tb[LS_NLA_TYPE_MAX]; 1059 int ret; 1060 1061 if (!(nlh->nlmsg_flags & NLM_F_REQUEST) || 1062 !(NETLINK_CB(skb).sk)) 1063 return -EPERM; 1064 1065 ret = nla_parse_deprecated(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh), 1066 nlmsg_len(nlh), ib_nl_policy, NULL); 1067 attr = (const struct nlattr *)tb[LS_NLA_TYPE_TIMEOUT]; 1068 if (ret || !attr) 1069 goto settimeout_out; 1070 1071 timeout = *(int *) nla_data(attr); 1072 if (timeout < IB_SA_LOCAL_SVC_TIMEOUT_MIN) 1073 timeout = IB_SA_LOCAL_SVC_TIMEOUT_MIN; 1074 if (timeout > IB_SA_LOCAL_SVC_TIMEOUT_MAX) 1075 timeout = IB_SA_LOCAL_SVC_TIMEOUT_MAX; 1076 1077 delta = timeout - sa_local_svc_timeout_ms; 1078 if (delta < 0) 1079 abs_delta = -delta; 1080 else 1081 abs_delta = delta; 1082 1083 if (delta != 0) { 1084 spin_lock_irqsave(&ib_nl_request_lock, flags); 1085 sa_local_svc_timeout_ms = timeout; 1086 list_for_each_entry(query, &ib_nl_request_list, list) { 1087 if (delta < 0 && abs_delta > query->timeout) 1088 query->timeout = 0; 1089 else 1090 query->timeout += delta; 1091 1092 /* Get the new delay from the first entry */ 1093 if (!delay) { 1094 delay = query->timeout - jiffies; 1095 if (delay <= 0) 1096 delay = 1; 1097 } 1098 } 1099 if (delay) 1100 mod_delayed_work(ib_nl_wq, &ib_nl_timed_work, 1101 (unsigned long)delay); 1102 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 1103 } 1104 1105 settimeout_out: 1106 return 0; 1107 } 1108 1109 static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh) 1110 { 1111 struct nlattr *tb[LS_NLA_TYPE_MAX]; 1112 int ret; 1113 1114 if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR) 1115 return 0; 1116 1117 ret = nla_parse_deprecated(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh), 1118 nlmsg_len(nlh), ib_nl_policy, NULL); 1119 if (ret) 1120 return 0; 1121 1122 return 1; 1123 } 1124 1125 int ib_nl_handle_resolve_resp(struct sk_buff *skb, 1126 struct nlmsghdr *nlh, 1127 struct netlink_ext_ack *extack) 1128 { 1129 unsigned long flags; 1130 struct ib_sa_query *query = NULL, *iter; 1131 struct ib_mad_send_buf *send_buf; 1132 struct ib_mad_send_wc mad_send_wc; 1133 int ret; 1134 1135 if ((nlh->nlmsg_flags & NLM_F_REQUEST) || 1136 !(NETLINK_CB(skb).sk)) 1137 return -EPERM; 1138 1139 spin_lock_irqsave(&ib_nl_request_lock, flags); 1140 list_for_each_entry(iter, &ib_nl_request_list, list) { 1141 /* 1142 * If the query is cancelled, let the timeout routine 1143 * take care of it. 1144 */ 1145 if (nlh->nlmsg_seq == iter->seq) { 1146 if (!ib_sa_query_cancelled(iter)) { 1147 list_del(&iter->list); 1148 query = iter; 1149 } 1150 break; 1151 } 1152 } 1153 1154 if (!query) { 1155 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 1156 goto resp_out; 1157 } 1158 1159 send_buf = query->mad_buf; 1160 1161 if (!ib_nl_is_good_resolve_resp(nlh)) { 1162 /* if the result is a failure, send out the packet via IB */ 1163 ib_sa_disable_local_svc(query); 1164 ret = ib_post_send_mad(query->mad_buf, NULL); 1165 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 1166 if (ret) { 1167 mad_send_wc.send_buf = send_buf; 1168 mad_send_wc.status = IB_WC_GENERAL_ERR; 1169 send_handler(query->port->agent, &mad_send_wc); 1170 } 1171 } else { 1172 spin_unlock_irqrestore(&ib_nl_request_lock, flags); 1173 ib_nl_process_good_resolve_rsp(query, nlh); 1174 } 1175 1176 resp_out: 1177 return 0; 1178 } 1179 1180 static void free_sm_ah(struct kref *kref) 1181 { 1182 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref); 1183 1184 rdma_destroy_ah(sm_ah->ah, 0); 1185 kfree(sm_ah); 1186 } 1187 1188 void ib_sa_register_client(struct ib_sa_client *client) 1189 { 1190 atomic_set(&client->users, 1); 1191 init_completion(&client->comp); 1192 } 1193 EXPORT_SYMBOL(ib_sa_register_client); 1194 1195 void ib_sa_unregister_client(struct ib_sa_client *client) 1196 { 1197 ib_sa_client_put(client); 1198 wait_for_completion(&client->comp); 1199 } 1200 EXPORT_SYMBOL(ib_sa_unregister_client); 1201 1202 /** 1203 * ib_sa_cancel_query - try to cancel an SA query 1204 * @id:ID of query to cancel 1205 * @query:query pointer to cancel 1206 * 1207 * Try to cancel an SA query. If the id and query don't match up or 1208 * the query has already completed, nothing is done. Otherwise the 1209 * query is canceled and will complete with a status of -EINTR. 1210 */ 1211 void ib_sa_cancel_query(int id, struct ib_sa_query *query) 1212 { 1213 unsigned long flags; 1214 struct ib_mad_send_buf *mad_buf; 1215 1216 xa_lock_irqsave(&queries, flags); 1217 if (xa_load(&queries, id) != query) { 1218 xa_unlock_irqrestore(&queries, flags); 1219 return; 1220 } 1221 mad_buf = query->mad_buf; 1222 xa_unlock_irqrestore(&queries, flags); 1223 1224 /* 1225 * If the query is still on the netlink request list, schedule 1226 * it to be cancelled by the timeout routine. Otherwise, it has been 1227 * sent to the MAD layer and has to be cancelled from there. 1228 */ 1229 if (!ib_nl_cancel_request(query)) 1230 ib_cancel_mad(mad_buf); 1231 } 1232 EXPORT_SYMBOL(ib_sa_cancel_query); 1233 1234 static u8 get_src_path_mask(struct ib_device *device, u32 port_num) 1235 { 1236 struct ib_sa_device *sa_dev; 1237 struct ib_sa_port *port; 1238 unsigned long flags; 1239 u8 src_path_mask; 1240 1241 sa_dev = ib_get_client_data(device, &sa_client); 1242 if (!sa_dev) 1243 return 0x7f; 1244 1245 port = &sa_dev->port[port_num - sa_dev->start_port]; 1246 spin_lock_irqsave(&port->ah_lock, flags); 1247 src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f; 1248 spin_unlock_irqrestore(&port->ah_lock, flags); 1249 1250 return src_path_mask; 1251 } 1252 1253 static int init_ah_attr_grh_fields(struct ib_device *device, u32 port_num, 1254 struct sa_path_rec *rec, 1255 struct rdma_ah_attr *ah_attr, 1256 const struct ib_gid_attr *gid_attr) 1257 { 1258 enum ib_gid_type type = sa_conv_pathrec_to_gid_type(rec); 1259 1260 if (!gid_attr) { 1261 gid_attr = rdma_find_gid_by_port(device, &rec->sgid, type, 1262 port_num, NULL); 1263 if (IS_ERR(gid_attr)) 1264 return PTR_ERR(gid_attr); 1265 } else 1266 rdma_hold_gid_attr(gid_attr); 1267 1268 rdma_move_grh_sgid_attr(ah_attr, &rec->dgid, 1269 be32_to_cpu(rec->flow_label), 1270 rec->hop_limit, rec->traffic_class, 1271 gid_attr); 1272 return 0; 1273 } 1274 1275 /** 1276 * ib_init_ah_attr_from_path - Initialize address handle attributes based on 1277 * an SA path record. 1278 * @device: Device associated ah attributes initialization. 1279 * @port_num: Port on the specified device. 1280 * @rec: path record entry to use for ah attributes initialization. 1281 * @ah_attr: address handle attributes to initialization from path record. 1282 * @gid_attr: SGID attribute to consider during initialization. 1283 * 1284 * When ib_init_ah_attr_from_path() returns success, 1285 * (a) for IB link layer it optionally contains a reference to SGID attribute 1286 * when GRH is present for IB link layer. 1287 * (b) for RoCE link layer it contains a reference to SGID attribute. 1288 * User must invoke rdma_destroy_ah_attr() to release reference to SGID 1289 * attributes which are initialized using ib_init_ah_attr_from_path(). 1290 */ 1291 int ib_init_ah_attr_from_path(struct ib_device *device, u32 port_num, 1292 struct sa_path_rec *rec, 1293 struct rdma_ah_attr *ah_attr, 1294 const struct ib_gid_attr *gid_attr) 1295 { 1296 int ret = 0; 1297 1298 memset(ah_attr, 0, sizeof(*ah_attr)); 1299 ah_attr->type = rdma_ah_find_type(device, port_num); 1300 rdma_ah_set_sl(ah_attr, rec->sl); 1301 rdma_ah_set_port_num(ah_attr, port_num); 1302 rdma_ah_set_static_rate(ah_attr, rec->rate); 1303 1304 if (sa_path_is_roce(rec)) { 1305 ret = roce_resolve_route_from_path(rec, gid_attr); 1306 if (ret) 1307 return ret; 1308 1309 memcpy(ah_attr->roce.dmac, sa_path_get_dmac(rec), ETH_ALEN); 1310 } else { 1311 rdma_ah_set_dlid(ah_attr, be32_to_cpu(sa_path_get_dlid(rec))); 1312 if (sa_path_is_opa(rec) && 1313 rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) 1314 rdma_ah_set_make_grd(ah_attr, true); 1315 1316 rdma_ah_set_path_bits(ah_attr, 1317 be32_to_cpu(sa_path_get_slid(rec)) & 1318 get_src_path_mask(device, port_num)); 1319 } 1320 1321 if (rec->hop_limit > 0 || sa_path_is_roce(rec)) 1322 ret = init_ah_attr_grh_fields(device, port_num, 1323 rec, ah_attr, gid_attr); 1324 return ret; 1325 } 1326 EXPORT_SYMBOL(ib_init_ah_attr_from_path); 1327 1328 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask) 1329 { 1330 struct rdma_ah_attr ah_attr; 1331 unsigned long flags; 1332 1333 spin_lock_irqsave(&query->port->ah_lock, flags); 1334 if (!query->port->sm_ah) { 1335 spin_unlock_irqrestore(&query->port->ah_lock, flags); 1336 return -EAGAIN; 1337 } 1338 kref_get(&query->port->sm_ah->ref); 1339 query->sm_ah = query->port->sm_ah; 1340 spin_unlock_irqrestore(&query->port->ah_lock, flags); 1341 1342 /* 1343 * Always check if sm_ah has valid dlid assigned, 1344 * before querying for class port info 1345 */ 1346 if ((rdma_query_ah(query->sm_ah->ah, &ah_attr) < 0) || 1347 !rdma_is_valid_unicast_lid(&ah_attr)) { 1348 kref_put(&query->sm_ah->ref, free_sm_ah); 1349 return -EAGAIN; 1350 } 1351 query->mad_buf = ib_create_send_mad(query->port->agent, 1, 1352 query->sm_ah->pkey_index, 1353 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA, 1354 gfp_mask, 1355 ((query->flags & IB_SA_QUERY_OPA) ? 1356 OPA_MGMT_BASE_VERSION : 1357 IB_MGMT_BASE_VERSION)); 1358 if (IS_ERR(query->mad_buf)) { 1359 kref_put(&query->sm_ah->ref, free_sm_ah); 1360 return -ENOMEM; 1361 } 1362 1363 query->mad_buf->ah = query->sm_ah->ah; 1364 1365 return 0; 1366 } 1367 1368 static void free_mad(struct ib_sa_query *query) 1369 { 1370 ib_free_send_mad(query->mad_buf); 1371 kref_put(&query->sm_ah->ref, free_sm_ah); 1372 } 1373 1374 static void init_mad(struct ib_sa_query *query, struct ib_mad_agent *agent) 1375 { 1376 struct ib_sa_mad *mad = query->mad_buf->mad; 1377 unsigned long flags; 1378 1379 memset(mad, 0, sizeof *mad); 1380 1381 if (query->flags & IB_SA_QUERY_OPA) { 1382 mad->mad_hdr.base_version = OPA_MGMT_BASE_VERSION; 1383 mad->mad_hdr.class_version = OPA_SA_CLASS_VERSION; 1384 } else { 1385 mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION; 1386 mad->mad_hdr.class_version = IB_SA_CLASS_VERSION; 1387 } 1388 mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM; 1389 spin_lock_irqsave(&tid_lock, flags); 1390 mad->mad_hdr.tid = 1391 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++); 1392 spin_unlock_irqrestore(&tid_lock, flags); 1393 } 1394 1395 static int send_mad(struct ib_sa_query *query, unsigned long timeout_ms, 1396 gfp_t gfp_mask) 1397 { 1398 unsigned long flags; 1399 int ret, id; 1400 const int nmbr_sa_query_retries = 10; 1401 1402 xa_lock_irqsave(&queries, flags); 1403 ret = __xa_alloc(&queries, &id, query, xa_limit_32b, gfp_mask); 1404 xa_unlock_irqrestore(&queries, flags); 1405 if (ret < 0) 1406 return ret; 1407 1408 query->mad_buf->timeout_ms = timeout_ms / nmbr_sa_query_retries; 1409 query->mad_buf->retries = nmbr_sa_query_retries; 1410 if (!query->mad_buf->timeout_ms) { 1411 /* Special case, very small timeout_ms */ 1412 query->mad_buf->timeout_ms = 1; 1413 query->mad_buf->retries = timeout_ms; 1414 } 1415 query->mad_buf->context[0] = query; 1416 query->id = id; 1417 1418 if ((query->flags & IB_SA_ENABLE_LOCAL_SERVICE) && 1419 (!(query->flags & IB_SA_QUERY_OPA))) { 1420 if (rdma_nl_chk_listeners(RDMA_NL_GROUP_LS)) { 1421 if (!ib_nl_make_request(query, gfp_mask)) 1422 return id; 1423 } 1424 ib_sa_disable_local_svc(query); 1425 } 1426 1427 ret = ib_post_send_mad(query->mad_buf, NULL); 1428 if (ret) { 1429 xa_lock_irqsave(&queries, flags); 1430 __xa_erase(&queries, id); 1431 xa_unlock_irqrestore(&queries, flags); 1432 } 1433 1434 /* 1435 * It's not safe to dereference query any more, because the 1436 * send may already have completed and freed the query in 1437 * another context. 1438 */ 1439 return ret ? ret : id; 1440 } 1441 1442 void ib_sa_unpack_path(void *attribute, struct sa_path_rec *rec) 1443 { 1444 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec); 1445 } 1446 EXPORT_SYMBOL(ib_sa_unpack_path); 1447 1448 void ib_sa_pack_path(struct sa_path_rec *rec, void *attribute) 1449 { 1450 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute); 1451 } 1452 EXPORT_SYMBOL(ib_sa_pack_path); 1453 1454 void ib_sa_pack_service(struct sa_service_rec *rec, void *attribute) 1455 { 1456 ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table), rec, 1457 attribute); 1458 } 1459 EXPORT_SYMBOL(ib_sa_pack_service); 1460 1461 void ib_sa_unpack_service(void *attribute, struct sa_service_rec *rec) 1462 { 1463 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table), attribute, 1464 rec); 1465 } 1466 EXPORT_SYMBOL(ib_sa_unpack_service); 1467 1468 static bool ib_sa_opa_pathrecord_support(struct ib_sa_client *client, 1469 struct ib_sa_device *sa_dev, 1470 u32 port_num) 1471 { 1472 struct ib_sa_port *port; 1473 unsigned long flags; 1474 bool ret = false; 1475 1476 port = &sa_dev->port[port_num - sa_dev->start_port]; 1477 spin_lock_irqsave(&port->classport_lock, flags); 1478 if (!port->classport_info.valid) 1479 goto ret; 1480 1481 if (port->classport_info.data.type == RDMA_CLASS_PORT_INFO_OPA) 1482 ret = opa_get_cpi_capmask2(&port->classport_info.data.opa) & 1483 OPA_CLASS_PORT_INFO_PR_SUPPORT; 1484 ret: 1485 spin_unlock_irqrestore(&port->classport_lock, flags); 1486 return ret; 1487 } 1488 1489 enum opa_pr_supported { 1490 PR_NOT_SUPPORTED, 1491 PR_OPA_SUPPORTED, 1492 PR_IB_SUPPORTED 1493 }; 1494 1495 /* 1496 * opa_pr_query_possible - Check if current PR query can be an OPA query. 1497 * 1498 * Returns PR_NOT_SUPPORTED if a path record query is not 1499 * possible, PR_OPA_SUPPORTED if an OPA path record query 1500 * is possible and PR_IB_SUPPORTED if an IB path record 1501 * query is possible. 1502 */ 1503 static int opa_pr_query_possible(struct ib_sa_client *client, 1504 struct ib_sa_device *sa_dev, 1505 struct ib_device *device, u32 port_num) 1506 { 1507 struct ib_port_attr port_attr; 1508 1509 if (ib_query_port(device, port_num, &port_attr)) 1510 return PR_NOT_SUPPORTED; 1511 1512 if (ib_sa_opa_pathrecord_support(client, sa_dev, port_num)) 1513 return PR_OPA_SUPPORTED; 1514 1515 if (port_attr.lid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) 1516 return PR_NOT_SUPPORTED; 1517 else 1518 return PR_IB_SUPPORTED; 1519 } 1520 1521 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query, 1522 int status, struct ib_sa_mad *mad) 1523 { 1524 struct ib_sa_path_query *query = 1525 container_of(sa_query, struct ib_sa_path_query, sa_query); 1526 struct sa_path_rec rec = {}; 1527 1528 if (!mad) { 1529 query->callback(status, NULL, 0, query->context); 1530 return; 1531 } 1532 1533 if (sa_query->flags & IB_SA_QUERY_OPA) { 1534 ib_unpack(opa_path_rec_table, ARRAY_SIZE(opa_path_rec_table), 1535 mad->data, &rec); 1536 rec.rec_type = SA_PATH_REC_TYPE_OPA; 1537 query->callback(status, &rec, 1, query->context); 1538 return; 1539 } 1540 1541 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), 1542 mad->data, &rec); 1543 rec.rec_type = SA_PATH_REC_TYPE_IB; 1544 sa_path_set_dmac_zero(&rec); 1545 1546 if (query->conv_pr) { 1547 struct sa_path_rec opa; 1548 1549 memset(&opa, 0, sizeof(struct sa_path_rec)); 1550 sa_convert_path_ib_to_opa(&opa, &rec); 1551 query->callback(status, &opa, 1, query->context); 1552 } else { 1553 query->callback(status, &rec, 1, query->context); 1554 } 1555 } 1556 1557 #define IB_SA_DATA_OFFS 56 1558 #define IB_SERVICE_REC_SZ 176 1559 1560 static void ib_unpack_service_rmpp(struct sa_service_rec *rec, 1561 struct ib_mad_recv_wc *mad_wc, 1562 int num_services) 1563 { 1564 unsigned int cp_sz, data_i, data_size, rec_i = 0, buf_i = 0; 1565 struct ib_mad_recv_buf *mad_buf; 1566 u8 buf[IB_SERVICE_REC_SZ]; 1567 u8 *data; 1568 1569 data_size = sizeof(((struct ib_sa_mad *) mad_buf->mad)->data); 1570 1571 list_for_each_entry(mad_buf, &mad_wc->rmpp_list, list) { 1572 data = ((struct ib_sa_mad *) mad_buf->mad)->data; 1573 data_i = 0; 1574 while (data_i < data_size && rec_i < num_services) { 1575 cp_sz = min(IB_SERVICE_REC_SZ - buf_i, 1576 data_size - data_i); 1577 memcpy(buf + buf_i, data + data_i, cp_sz); 1578 data_i += cp_sz; 1579 buf_i += cp_sz; 1580 if (buf_i == IB_SERVICE_REC_SZ) { 1581 ib_sa_unpack_service(buf, rec + rec_i); 1582 buf_i = 0; 1583 rec_i++; 1584 } 1585 } 1586 } 1587 } 1588 1589 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query, int status, 1590 struct ib_mad_recv_wc *mad_wc) 1591 { 1592 struct ib_sa_service_query *query = 1593 container_of(sa_query, struct ib_sa_service_query, sa_query); 1594 struct sa_service_rec *rec; 1595 int num_services; 1596 1597 if (!mad_wc || !mad_wc->recv_buf.mad) { 1598 query->callback(status, NULL, 0, query->context); 1599 return; 1600 } 1601 1602 num_services = (mad_wc->mad_len - IB_SA_DATA_OFFS) / IB_SERVICE_REC_SZ; 1603 if (!num_services) { 1604 query->callback(-ENODATA, NULL, 0, query->context); 1605 return; 1606 } 1607 1608 rec = kmalloc_array(num_services, sizeof(*rec), GFP_KERNEL); 1609 if (!rec) { 1610 query->callback(-ENOMEM, NULL, 0, query->context); 1611 return; 1612 } 1613 1614 ib_unpack_service_rmpp(rec, mad_wc, num_services); 1615 query->callback(status, rec, num_services, query->context); 1616 kfree(rec); 1617 } 1618 1619 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query) 1620 { 1621 struct ib_sa_path_query *query = 1622 container_of(sa_query, struct ib_sa_path_query, sa_query); 1623 1624 kfree(query->conv_pr); 1625 kfree(query); 1626 } 1627 1628 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query) 1629 { 1630 struct ib_sa_service_query *query = 1631 container_of(sa_query, struct ib_sa_service_query, sa_query); 1632 1633 kfree(query); 1634 } 1635 1636 /** 1637 * ib_sa_path_rec_get - Start a Path get query 1638 * @client:SA client 1639 * @device:device to send query on 1640 * @port_num: port number to send query on 1641 * @rec:Path Record to send in query 1642 * @comp_mask:component mask to send in query 1643 * @timeout_ms:time to wait for response 1644 * @gfp_mask:GFP mask to use for internal allocations 1645 * @callback:function called when query completes, times out or is 1646 * canceled 1647 * @context:opaque user context passed to callback 1648 * @sa_query:query context, used to cancel query 1649 * 1650 * Send a Path Record Get query to the SA to look up a path. The 1651 * callback function will be called when the query completes (or 1652 * fails); status is 0 for a successful response, -EINTR if the query 1653 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error 1654 * occurred sending the query. The resp parameter of the callback is 1655 * only valid if status is 0. 1656 * 1657 * If the return value of ib_sa_path_rec_get() is negative, it is an 1658 * error code. Otherwise it is a query ID that can be used to cancel 1659 * the query. 1660 */ 1661 int ib_sa_path_rec_get(struct ib_sa_client *client, 1662 struct ib_device *device, u32 port_num, 1663 struct sa_path_rec *rec, 1664 ib_sa_comp_mask comp_mask, 1665 unsigned long timeout_ms, gfp_t gfp_mask, 1666 void (*callback)(int status, 1667 struct sa_path_rec *resp, 1668 unsigned int num_paths, void *context), 1669 void *context, 1670 struct ib_sa_query **sa_query) 1671 { 1672 struct ib_sa_path_query *query; 1673 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client); 1674 struct ib_sa_port *port; 1675 struct ib_mad_agent *agent; 1676 struct ib_sa_mad *mad; 1677 enum opa_pr_supported status; 1678 int ret; 1679 1680 if (!sa_dev) 1681 return -ENODEV; 1682 1683 if ((rec->rec_type != SA_PATH_REC_TYPE_IB) && 1684 (rec->rec_type != SA_PATH_REC_TYPE_OPA)) 1685 return -EINVAL; 1686 1687 port = &sa_dev->port[port_num - sa_dev->start_port]; 1688 agent = port->agent; 1689 1690 query = kzalloc(sizeof(*query), gfp_mask); 1691 if (!query) 1692 return -ENOMEM; 1693 1694 query->sa_query.port = port; 1695 if (rec->rec_type == SA_PATH_REC_TYPE_OPA) { 1696 status = opa_pr_query_possible(client, sa_dev, device, port_num); 1697 if (status == PR_NOT_SUPPORTED) { 1698 ret = -EINVAL; 1699 goto err1; 1700 } else if (status == PR_OPA_SUPPORTED) { 1701 query->sa_query.flags |= IB_SA_QUERY_OPA; 1702 } else { 1703 query->conv_pr = 1704 kmalloc(sizeof(*query->conv_pr), gfp_mask); 1705 if (!query->conv_pr) { 1706 ret = -ENOMEM; 1707 goto err1; 1708 } 1709 } 1710 } 1711 1712 ret = alloc_mad(&query->sa_query, gfp_mask); 1713 if (ret) 1714 goto err2; 1715 1716 ib_sa_client_get(client); 1717 query->sa_query.client = client; 1718 query->callback = callback; 1719 query->context = context; 1720 1721 mad = query->sa_query.mad_buf->mad; 1722 init_mad(&query->sa_query, agent); 1723 1724 query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL; 1725 query->sa_query.release = ib_sa_path_rec_release; 1726 mad->mad_hdr.method = IB_MGMT_METHOD_GET; 1727 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC); 1728 mad->sa_hdr.comp_mask = comp_mask; 1729 1730 if (query->sa_query.flags & IB_SA_QUERY_OPA) { 1731 ib_pack(opa_path_rec_table, ARRAY_SIZE(opa_path_rec_table), 1732 rec, mad->data); 1733 } else if (query->conv_pr) { 1734 sa_convert_path_opa_to_ib(query->conv_pr, rec); 1735 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), 1736 query->conv_pr, mad->data); 1737 } else { 1738 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), 1739 rec, mad->data); 1740 } 1741 1742 *sa_query = &query->sa_query; 1743 1744 query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE; 1745 query->sa_query.mad_buf->context[1] = (query->conv_pr) ? 1746 query->conv_pr : rec; 1747 1748 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask); 1749 if (ret < 0) 1750 goto err3; 1751 1752 return ret; 1753 1754 err3: 1755 *sa_query = NULL; 1756 ib_sa_client_put(query->sa_query.client); 1757 free_mad(&query->sa_query); 1758 err2: 1759 kfree(query->conv_pr); 1760 err1: 1761 kfree(query); 1762 return ret; 1763 } 1764 EXPORT_SYMBOL(ib_sa_path_rec_get); 1765 1766 /** 1767 * ib_sa_service_rec_get - Start a Service get query 1768 * @client: SA client 1769 * @device: device to send query on 1770 * @port_num: port number to send query on 1771 * @rec: Service Record to send in query 1772 * @comp_mask: component mask to send in query 1773 * @timeout_ms: time to wait for response 1774 * @gfp_mask: GFP mask to use for internal allocations 1775 * @callback: function called when query completes, times out or is 1776 * canceled 1777 * @context: opaque user context passed to callback 1778 * @sa_query: query context, used to cancel query 1779 * 1780 * Send a Service Record Get query to the SA to look up a path. The 1781 * callback function will be called when the query completes (or 1782 * fails); status is 0 for a successful response, -EINTR if the query 1783 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error 1784 * occurred sending the query. The resp parameter of the callback is 1785 * only valid if status is 0. 1786 * 1787 * If the return value of ib_sa_service_rec_get() is negative, it is an 1788 * error code. Otherwise it is a query ID that can be used to cancel 1789 * the query. 1790 */ 1791 int ib_sa_service_rec_get(struct ib_sa_client *client, 1792 struct ib_device *device, u32 port_num, 1793 struct sa_service_rec *rec, 1794 ib_sa_comp_mask comp_mask, 1795 unsigned long timeout_ms, gfp_t gfp_mask, 1796 void (*callback)(int status, 1797 struct sa_service_rec *resp, 1798 unsigned int num_services, 1799 void *context), 1800 void *context, struct ib_sa_query **sa_query) 1801 { 1802 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client); 1803 struct ib_sa_service_query *query; 1804 struct ib_mad_agent *agent; 1805 struct ib_sa_port *port; 1806 struct ib_sa_mad *mad; 1807 int ret; 1808 1809 if (!sa_dev) 1810 return -ENODEV; 1811 1812 port = &sa_dev->port[port_num - sa_dev->start_port]; 1813 agent = port->agent; 1814 1815 query = kzalloc(sizeof(*query), gfp_mask); 1816 if (!query) 1817 return -ENOMEM; 1818 1819 query->sa_query.port = port; 1820 1821 ret = alloc_mad(&query->sa_query, gfp_mask); 1822 if (ret) 1823 goto err1; 1824 1825 ib_sa_client_get(client); 1826 query->sa_query.client = client; 1827 query->callback = callback; 1828 query->context = context; 1829 1830 mad = query->sa_query.mad_buf->mad; 1831 init_mad(&query->sa_query, agent); 1832 1833 query->sa_query.rmpp_callback = callback ? ib_sa_service_rec_callback : 1834 NULL; 1835 query->sa_query.release = ib_sa_service_rec_release; 1836 mad->mad_hdr.method = IB_MGMT_METHOD_GET_TABLE; 1837 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC); 1838 mad->sa_hdr.comp_mask = comp_mask; 1839 1840 ib_sa_pack_service(rec, mad->data); 1841 1842 *sa_query = &query->sa_query; 1843 query->sa_query.mad_buf->context[1] = rec; 1844 1845 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask); 1846 if (ret < 0) 1847 goto err2; 1848 1849 return ret; 1850 1851 err2: 1852 *sa_query = NULL; 1853 ib_sa_client_put(query->sa_query.client); 1854 free_mad(&query->sa_query); 1855 err1: 1856 kfree(query); 1857 return ret; 1858 } 1859 EXPORT_SYMBOL(ib_sa_service_rec_get); 1860 1861 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query, 1862 int status, struct ib_sa_mad *mad) 1863 { 1864 struct ib_sa_mcmember_query *query = 1865 container_of(sa_query, struct ib_sa_mcmember_query, sa_query); 1866 1867 if (mad) { 1868 struct ib_sa_mcmember_rec rec; 1869 1870 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table), 1871 mad->data, &rec); 1872 query->callback(status, &rec, query->context); 1873 } else 1874 query->callback(status, NULL, query->context); 1875 } 1876 1877 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query) 1878 { 1879 kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query)); 1880 } 1881 1882 int ib_sa_mcmember_rec_query(struct ib_sa_client *client, 1883 struct ib_device *device, u32 port_num, 1884 u8 method, 1885 struct ib_sa_mcmember_rec *rec, 1886 ib_sa_comp_mask comp_mask, 1887 unsigned long timeout_ms, gfp_t gfp_mask, 1888 void (*callback)(int status, 1889 struct ib_sa_mcmember_rec *resp, 1890 void *context), 1891 void *context, 1892 struct ib_sa_query **sa_query) 1893 { 1894 struct ib_sa_mcmember_query *query; 1895 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client); 1896 struct ib_sa_port *port; 1897 struct ib_mad_agent *agent; 1898 struct ib_sa_mad *mad; 1899 int ret; 1900 1901 if (!sa_dev) 1902 return -ENODEV; 1903 1904 port = &sa_dev->port[port_num - sa_dev->start_port]; 1905 agent = port->agent; 1906 1907 query = kzalloc(sizeof(*query), gfp_mask); 1908 if (!query) 1909 return -ENOMEM; 1910 1911 query->sa_query.port = port; 1912 ret = alloc_mad(&query->sa_query, gfp_mask); 1913 if (ret) 1914 goto err1; 1915 1916 ib_sa_client_get(client); 1917 query->sa_query.client = client; 1918 query->callback = callback; 1919 query->context = context; 1920 1921 mad = query->sa_query.mad_buf->mad; 1922 init_mad(&query->sa_query, agent); 1923 1924 query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL; 1925 query->sa_query.release = ib_sa_mcmember_rec_release; 1926 mad->mad_hdr.method = method; 1927 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC); 1928 mad->sa_hdr.comp_mask = comp_mask; 1929 1930 ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table), 1931 rec, mad->data); 1932 1933 *sa_query = &query->sa_query; 1934 1935 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask); 1936 if (ret < 0) 1937 goto err2; 1938 1939 return ret; 1940 1941 err2: 1942 *sa_query = NULL; 1943 ib_sa_client_put(query->sa_query.client); 1944 free_mad(&query->sa_query); 1945 1946 err1: 1947 kfree(query); 1948 return ret; 1949 } 1950 1951 /* Support GuidInfoRecord */ 1952 static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query, 1953 int status, struct ib_sa_mad *mad) 1954 { 1955 struct ib_sa_guidinfo_query *query = 1956 container_of(sa_query, struct ib_sa_guidinfo_query, sa_query); 1957 1958 if (mad) { 1959 struct ib_sa_guidinfo_rec rec; 1960 1961 ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), 1962 mad->data, &rec); 1963 query->callback(status, &rec, query->context); 1964 } else 1965 query->callback(status, NULL, query->context); 1966 } 1967 1968 static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query) 1969 { 1970 kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query)); 1971 } 1972 1973 int ib_sa_guid_info_rec_query(struct ib_sa_client *client, 1974 struct ib_device *device, u32 port_num, 1975 struct ib_sa_guidinfo_rec *rec, 1976 ib_sa_comp_mask comp_mask, u8 method, 1977 unsigned long timeout_ms, gfp_t gfp_mask, 1978 void (*callback)(int status, 1979 struct ib_sa_guidinfo_rec *resp, 1980 void *context), 1981 void *context, 1982 struct ib_sa_query **sa_query) 1983 { 1984 struct ib_sa_guidinfo_query *query; 1985 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client); 1986 struct ib_sa_port *port; 1987 struct ib_mad_agent *agent; 1988 struct ib_sa_mad *mad; 1989 int ret; 1990 1991 if (!sa_dev) 1992 return -ENODEV; 1993 1994 if (method != IB_MGMT_METHOD_GET && 1995 method != IB_MGMT_METHOD_SET && 1996 method != IB_SA_METHOD_DELETE) { 1997 return -EINVAL; 1998 } 1999 2000 port = &sa_dev->port[port_num - sa_dev->start_port]; 2001 agent = port->agent; 2002 2003 query = kzalloc(sizeof(*query), gfp_mask); 2004 if (!query) 2005 return -ENOMEM; 2006 2007 query->sa_query.port = port; 2008 ret = alloc_mad(&query->sa_query, gfp_mask); 2009 if (ret) 2010 goto err1; 2011 2012 ib_sa_client_get(client); 2013 query->sa_query.client = client; 2014 query->callback = callback; 2015 query->context = context; 2016 2017 mad = query->sa_query.mad_buf->mad; 2018 init_mad(&query->sa_query, agent); 2019 2020 query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL; 2021 query->sa_query.release = ib_sa_guidinfo_rec_release; 2022 2023 mad->mad_hdr.method = method; 2024 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC); 2025 mad->sa_hdr.comp_mask = comp_mask; 2026 2027 ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec, 2028 mad->data); 2029 2030 *sa_query = &query->sa_query; 2031 2032 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask); 2033 if (ret < 0) 2034 goto err2; 2035 2036 return ret; 2037 2038 err2: 2039 *sa_query = NULL; 2040 ib_sa_client_put(query->sa_query.client); 2041 free_mad(&query->sa_query); 2042 2043 err1: 2044 kfree(query); 2045 return ret; 2046 } 2047 EXPORT_SYMBOL(ib_sa_guid_info_rec_query); 2048 2049 struct ib_classport_info_context { 2050 struct completion done; 2051 struct ib_sa_query *sa_query; 2052 }; 2053 2054 static void ib_classportinfo_cb(void *context) 2055 { 2056 struct ib_classport_info_context *cb_ctx = context; 2057 2058 complete(&cb_ctx->done); 2059 } 2060 2061 static void ib_sa_classport_info_rec_callback(struct ib_sa_query *sa_query, 2062 int status, struct ib_sa_mad *mad) 2063 { 2064 unsigned long flags; 2065 struct ib_sa_classport_info_query *query = 2066 container_of(sa_query, struct ib_sa_classport_info_query, sa_query); 2067 struct ib_sa_classport_cache *info = &sa_query->port->classport_info; 2068 2069 if (mad) { 2070 if (sa_query->flags & IB_SA_QUERY_OPA) { 2071 struct opa_class_port_info rec; 2072 2073 ib_unpack(opa_classport_info_rec_table, 2074 ARRAY_SIZE(opa_classport_info_rec_table), 2075 mad->data, &rec); 2076 2077 spin_lock_irqsave(&sa_query->port->classport_lock, 2078 flags); 2079 if (!status && !info->valid) { 2080 memcpy(&info->data.opa, &rec, 2081 sizeof(info->data.opa)); 2082 2083 info->valid = true; 2084 info->data.type = RDMA_CLASS_PORT_INFO_OPA; 2085 } 2086 spin_unlock_irqrestore(&sa_query->port->classport_lock, 2087 flags); 2088 2089 } else { 2090 struct ib_class_port_info rec; 2091 2092 ib_unpack(ib_classport_info_rec_table, 2093 ARRAY_SIZE(ib_classport_info_rec_table), 2094 mad->data, &rec); 2095 2096 spin_lock_irqsave(&sa_query->port->classport_lock, 2097 flags); 2098 if (!status && !info->valid) { 2099 memcpy(&info->data.ib, &rec, 2100 sizeof(info->data.ib)); 2101 2102 info->valid = true; 2103 info->data.type = RDMA_CLASS_PORT_INFO_IB; 2104 } 2105 spin_unlock_irqrestore(&sa_query->port->classport_lock, 2106 flags); 2107 } 2108 } 2109 query->callback(query->context); 2110 } 2111 2112 static void ib_sa_classport_info_rec_release(struct ib_sa_query *sa_query) 2113 { 2114 kfree(container_of(sa_query, struct ib_sa_classport_info_query, 2115 sa_query)); 2116 } 2117 2118 static int ib_sa_classport_info_rec_query(struct ib_sa_port *port, 2119 unsigned long timeout_ms, 2120 void (*callback)(void *context), 2121 void *context, 2122 struct ib_sa_query **sa_query) 2123 { 2124 struct ib_mad_agent *agent; 2125 struct ib_sa_classport_info_query *query; 2126 struct ib_sa_mad *mad; 2127 gfp_t gfp_mask = GFP_KERNEL; 2128 int ret; 2129 2130 agent = port->agent; 2131 2132 query = kzalloc(sizeof(*query), gfp_mask); 2133 if (!query) 2134 return -ENOMEM; 2135 2136 query->sa_query.port = port; 2137 query->sa_query.flags |= rdma_cap_opa_ah(port->agent->device, 2138 port->port_num) ? 2139 IB_SA_QUERY_OPA : 0; 2140 ret = alloc_mad(&query->sa_query, gfp_mask); 2141 if (ret) 2142 goto err_free; 2143 2144 query->callback = callback; 2145 query->context = context; 2146 2147 mad = query->sa_query.mad_buf->mad; 2148 init_mad(&query->sa_query, agent); 2149 2150 query->sa_query.callback = ib_sa_classport_info_rec_callback; 2151 query->sa_query.release = ib_sa_classport_info_rec_release; 2152 mad->mad_hdr.method = IB_MGMT_METHOD_GET; 2153 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_CLASS_PORTINFO); 2154 mad->sa_hdr.comp_mask = 0; 2155 *sa_query = &query->sa_query; 2156 2157 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask); 2158 if (ret < 0) 2159 goto err_free_mad; 2160 2161 return ret; 2162 2163 err_free_mad: 2164 *sa_query = NULL; 2165 free_mad(&query->sa_query); 2166 2167 err_free: 2168 kfree(query); 2169 return ret; 2170 } 2171 2172 static void update_ib_cpi(struct work_struct *work) 2173 { 2174 struct ib_sa_port *port = 2175 container_of(work, struct ib_sa_port, ib_cpi_work.work); 2176 struct ib_classport_info_context *cb_context; 2177 unsigned long flags; 2178 int ret; 2179 2180 /* If the classport info is valid, nothing 2181 * to do here. 2182 */ 2183 spin_lock_irqsave(&port->classport_lock, flags); 2184 if (port->classport_info.valid) { 2185 spin_unlock_irqrestore(&port->classport_lock, flags); 2186 return; 2187 } 2188 spin_unlock_irqrestore(&port->classport_lock, flags); 2189 2190 cb_context = kmalloc(sizeof(*cb_context), GFP_KERNEL); 2191 if (!cb_context) 2192 goto err_nomem; 2193 2194 init_completion(&cb_context->done); 2195 2196 ret = ib_sa_classport_info_rec_query(port, 3000, 2197 ib_classportinfo_cb, cb_context, 2198 &cb_context->sa_query); 2199 if (ret < 0) 2200 goto free_cb_err; 2201 wait_for_completion(&cb_context->done); 2202 free_cb_err: 2203 kfree(cb_context); 2204 spin_lock_irqsave(&port->classport_lock, flags); 2205 2206 /* If the classport info is still not valid, the query should have 2207 * failed for some reason. Retry issuing the query 2208 */ 2209 if (!port->classport_info.valid) { 2210 port->classport_info.retry_cnt++; 2211 if (port->classport_info.retry_cnt <= 2212 IB_SA_CPI_MAX_RETRY_CNT) { 2213 unsigned long delay = 2214 msecs_to_jiffies(IB_SA_CPI_RETRY_WAIT); 2215 2216 queue_delayed_work(ib_wq, &port->ib_cpi_work, delay); 2217 } 2218 } 2219 spin_unlock_irqrestore(&port->classport_lock, flags); 2220 2221 err_nomem: 2222 return; 2223 } 2224 2225 static void send_handler(struct ib_mad_agent *agent, 2226 struct ib_mad_send_wc *mad_send_wc) 2227 { 2228 struct ib_sa_query *query = mad_send_wc->send_buf->context[0]; 2229 unsigned long flags; 2230 int status = 0; 2231 2232 if (query->callback || query->rmpp_callback) { 2233 switch (mad_send_wc->status) { 2234 case IB_WC_SUCCESS: 2235 /* No callback -- already got recv */ 2236 break; 2237 case IB_WC_RESP_TIMEOUT_ERR: 2238 status = -ETIMEDOUT; 2239 break; 2240 case IB_WC_WR_FLUSH_ERR: 2241 status = -EINTR; 2242 break; 2243 default: 2244 status = -EIO; 2245 break; 2246 } 2247 2248 if (status) 2249 query->callback ? query->callback(query, status, NULL) : 2250 query->rmpp_callback(query, status, NULL); 2251 } 2252 2253 xa_lock_irqsave(&queries, flags); 2254 __xa_erase(&queries, query->id); 2255 xa_unlock_irqrestore(&queries, flags); 2256 2257 free_mad(query); 2258 if (query->client) 2259 ib_sa_client_put(query->client); 2260 query->release(query); 2261 } 2262 2263 static void recv_handler(struct ib_mad_agent *mad_agent, 2264 struct ib_mad_send_buf *send_buf, 2265 struct ib_mad_recv_wc *mad_recv_wc) 2266 { 2267 struct ib_sa_query *query; 2268 struct ib_mad *mad; 2269 2270 2271 if (!send_buf) 2272 return; 2273 2274 query = send_buf->context[0]; 2275 mad = mad_recv_wc->recv_buf.mad; 2276 2277 if (query->rmpp_callback) { 2278 if (mad_recv_wc->wc->status == IB_WC_SUCCESS) 2279 query->rmpp_callback(query, mad->mad_hdr.status ? 2280 -EINVAL : 0, mad_recv_wc); 2281 else 2282 query->rmpp_callback(query, -EIO, NULL); 2283 } else if (query->callback) { 2284 if (mad_recv_wc->wc->status == IB_WC_SUCCESS) 2285 query->callback(query, mad->mad_hdr.status ? 2286 -EINVAL : 0, (struct ib_sa_mad *)mad); 2287 else 2288 query->callback(query, -EIO, NULL); 2289 } 2290 2291 ib_free_recv_mad(mad_recv_wc); 2292 } 2293 2294 static void update_sm_ah(struct work_struct *work) 2295 { 2296 struct ib_sa_port *port = 2297 container_of(work, struct ib_sa_port, update_task); 2298 struct ib_sa_sm_ah *new_ah; 2299 struct ib_port_attr port_attr; 2300 struct rdma_ah_attr ah_attr; 2301 bool grh_required; 2302 2303 if (ib_query_port(port->agent->device, port->port_num, &port_attr)) { 2304 pr_warn("Couldn't query port\n"); 2305 return; 2306 } 2307 2308 new_ah = kmalloc(sizeof(*new_ah), GFP_KERNEL); 2309 if (!new_ah) 2310 return; 2311 2312 kref_init(&new_ah->ref); 2313 new_ah->src_path_mask = (1 << port_attr.lmc) - 1; 2314 2315 new_ah->pkey_index = 0; 2316 if (ib_find_pkey(port->agent->device, port->port_num, 2317 IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index)) 2318 pr_err("Couldn't find index for default PKey\n"); 2319 2320 memset(&ah_attr, 0, sizeof(ah_attr)); 2321 ah_attr.type = rdma_ah_find_type(port->agent->device, 2322 port->port_num); 2323 rdma_ah_set_dlid(&ah_attr, port_attr.sm_lid); 2324 rdma_ah_set_sl(&ah_attr, port_attr.sm_sl); 2325 rdma_ah_set_port_num(&ah_attr, port->port_num); 2326 2327 grh_required = rdma_is_grh_required(port->agent->device, 2328 port->port_num); 2329 2330 /* 2331 * The OPA sm_lid of 0xFFFF needs special handling so that it can be 2332 * differentiated from a permissive LID of 0xFFFF. We set the 2333 * grh_required flag here so the SA can program the DGID in the 2334 * address handle appropriately 2335 */ 2336 if (ah_attr.type == RDMA_AH_ATTR_TYPE_OPA && 2337 (grh_required || 2338 port_attr.sm_lid == be16_to_cpu(IB_LID_PERMISSIVE))) 2339 rdma_ah_set_make_grd(&ah_attr, true); 2340 2341 if (ah_attr.type == RDMA_AH_ATTR_TYPE_IB && grh_required) { 2342 rdma_ah_set_ah_flags(&ah_attr, IB_AH_GRH); 2343 rdma_ah_set_subnet_prefix(&ah_attr, 2344 cpu_to_be64(port_attr.subnet_prefix)); 2345 rdma_ah_set_interface_id(&ah_attr, 2346 cpu_to_be64(IB_SA_WELL_KNOWN_GUID)); 2347 } 2348 2349 new_ah->ah = rdma_create_ah(port->agent->qp->pd, &ah_attr, 2350 RDMA_CREATE_AH_SLEEPABLE); 2351 if (IS_ERR(new_ah->ah)) { 2352 pr_warn("Couldn't create new SM AH\n"); 2353 kfree(new_ah); 2354 return; 2355 } 2356 2357 spin_lock_irq(&port->ah_lock); 2358 if (port->sm_ah) 2359 kref_put(&port->sm_ah->ref, free_sm_ah); 2360 port->sm_ah = new_ah; 2361 spin_unlock_irq(&port->ah_lock); 2362 } 2363 2364 static void ib_sa_event(struct ib_event_handler *handler, 2365 struct ib_event *event) 2366 { 2367 if (event->event == IB_EVENT_PORT_ERR || 2368 event->event == IB_EVENT_PORT_ACTIVE || 2369 event->event == IB_EVENT_LID_CHANGE || 2370 event->event == IB_EVENT_PKEY_CHANGE || 2371 event->event == IB_EVENT_SM_CHANGE || 2372 event->event == IB_EVENT_CLIENT_REREGISTER) { 2373 unsigned long flags; 2374 struct ib_sa_device *sa_dev = 2375 container_of(handler, typeof(*sa_dev), event_handler); 2376 u32 port_num = event->element.port_num - sa_dev->start_port; 2377 struct ib_sa_port *port = &sa_dev->port[port_num]; 2378 2379 if (!rdma_cap_ib_sa(handler->device, port->port_num)) 2380 return; 2381 2382 spin_lock_irqsave(&port->ah_lock, flags); 2383 if (port->sm_ah) 2384 kref_put(&port->sm_ah->ref, free_sm_ah); 2385 port->sm_ah = NULL; 2386 spin_unlock_irqrestore(&port->ah_lock, flags); 2387 2388 if (event->event == IB_EVENT_SM_CHANGE || 2389 event->event == IB_EVENT_CLIENT_REREGISTER || 2390 event->event == IB_EVENT_LID_CHANGE || 2391 event->event == IB_EVENT_PORT_ACTIVE) { 2392 unsigned long delay = 2393 msecs_to_jiffies(IB_SA_CPI_RETRY_WAIT); 2394 2395 spin_lock_irqsave(&port->classport_lock, flags); 2396 port->classport_info.valid = false; 2397 port->classport_info.retry_cnt = 0; 2398 spin_unlock_irqrestore(&port->classport_lock, flags); 2399 queue_delayed_work(ib_wq, 2400 &port->ib_cpi_work, delay); 2401 } 2402 queue_work(ib_wq, &sa_dev->port[port_num].update_task); 2403 } 2404 } 2405 2406 static int ib_sa_add_one(struct ib_device *device) 2407 { 2408 struct ib_sa_device *sa_dev; 2409 int s, e, i; 2410 int count = 0; 2411 int ret; 2412 2413 s = rdma_start_port(device); 2414 e = rdma_end_port(device); 2415 2416 sa_dev = kzalloc(struct_size(sa_dev, port, 2417 size_add(size_sub(e, s), 1)), 2418 GFP_KERNEL); 2419 if (!sa_dev) 2420 return -ENOMEM; 2421 2422 sa_dev->start_port = s; 2423 sa_dev->end_port = e; 2424 2425 for (i = 0; i <= e - s; ++i) { 2426 spin_lock_init(&sa_dev->port[i].ah_lock); 2427 if (!rdma_cap_ib_sa(device, i + 1)) 2428 continue; 2429 2430 sa_dev->port[i].sm_ah = NULL; 2431 sa_dev->port[i].port_num = i + s; 2432 2433 spin_lock_init(&sa_dev->port[i].classport_lock); 2434 sa_dev->port[i].classport_info.valid = false; 2435 2436 sa_dev->port[i].agent = 2437 ib_register_mad_agent(device, i + s, IB_QPT_GSI, 2438 NULL, IB_MGMT_RMPP_VERSION, 2439 send_handler, recv_handler, 2440 sa_dev, 0); 2441 if (IS_ERR(sa_dev->port[i].agent)) { 2442 ret = PTR_ERR(sa_dev->port[i].agent); 2443 goto err; 2444 } 2445 2446 INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah); 2447 INIT_DELAYED_WORK(&sa_dev->port[i].ib_cpi_work, 2448 update_ib_cpi); 2449 2450 count++; 2451 } 2452 2453 if (!count) { 2454 ret = -EOPNOTSUPP; 2455 goto free; 2456 } 2457 2458 ib_set_client_data(device, &sa_client, sa_dev); 2459 2460 /* 2461 * We register our event handler after everything is set up, 2462 * and then update our cached info after the event handler is 2463 * registered to avoid any problems if a port changes state 2464 * during our initialization. 2465 */ 2466 2467 INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event); 2468 ib_register_event_handler(&sa_dev->event_handler); 2469 2470 for (i = 0; i <= e - s; ++i) { 2471 if (rdma_cap_ib_sa(device, i + 1)) 2472 update_sm_ah(&sa_dev->port[i].update_task); 2473 } 2474 2475 return 0; 2476 2477 err: 2478 while (--i >= 0) { 2479 if (rdma_cap_ib_sa(device, i + 1)) 2480 ib_unregister_mad_agent(sa_dev->port[i].agent); 2481 } 2482 free: 2483 kfree(sa_dev); 2484 return ret; 2485 } 2486 2487 static void ib_sa_remove_one(struct ib_device *device, void *client_data) 2488 { 2489 struct ib_sa_device *sa_dev = client_data; 2490 int i; 2491 2492 ib_unregister_event_handler(&sa_dev->event_handler); 2493 flush_workqueue(ib_wq); 2494 2495 for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) { 2496 if (rdma_cap_ib_sa(device, i + 1)) { 2497 cancel_delayed_work_sync(&sa_dev->port[i].ib_cpi_work); 2498 ib_unregister_mad_agent(sa_dev->port[i].agent); 2499 if (sa_dev->port[i].sm_ah) 2500 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah); 2501 } 2502 2503 } 2504 2505 kfree(sa_dev); 2506 } 2507 2508 int ib_sa_init(void) 2509 { 2510 int ret; 2511 2512 get_random_bytes(&tid, sizeof tid); 2513 2514 atomic_set(&ib_nl_sa_request_seq, 0); 2515 2516 ret = ib_register_client(&sa_client); 2517 if (ret) { 2518 pr_err("Couldn't register ib_sa client\n"); 2519 goto err1; 2520 } 2521 2522 ret = mcast_init(); 2523 if (ret) { 2524 pr_err("Couldn't initialize multicast handling\n"); 2525 goto err2; 2526 } 2527 2528 ib_nl_wq = alloc_ordered_workqueue("ib_nl_sa_wq", WQ_MEM_RECLAIM); 2529 if (!ib_nl_wq) { 2530 ret = -ENOMEM; 2531 goto err3; 2532 } 2533 2534 INIT_DELAYED_WORK(&ib_nl_timed_work, ib_nl_request_timeout); 2535 2536 return 0; 2537 2538 err3: 2539 mcast_cleanup(); 2540 err2: 2541 ib_unregister_client(&sa_client); 2542 err1: 2543 return ret; 2544 } 2545 2546 void ib_sa_cleanup(void) 2547 { 2548 cancel_delayed_work(&ib_nl_timed_work); 2549 destroy_workqueue(ib_nl_wq); 2550 mcast_cleanup(); 2551 ib_unregister_client(&sa_client); 2552 WARN_ON(!xa_empty(&queries)); 2553 } 2554