1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright (c) 2004-2007 Intel Corporation. All rights reserved. 4 * Copyright (c) 2004 Topspin Corporation. All rights reserved. 5 * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved. 6 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 7 * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved. 8 */ 9 10 #include <linux/completion.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/device.h> 13 #include <linux/module.h> 14 #include <linux/err.h> 15 #include <linux/idr.h> 16 #include <linux/interrupt.h> 17 #include <linux/random.h> 18 #include <linux/rbtree.h> 19 #include <linux/spinlock.h> 20 #include <linux/slab.h> 21 #include <linux/sysfs.h> 22 #include <linux/workqueue.h> 23 #include <linux/kdev_t.h> 24 #include <linux/etherdevice.h> 25 26 #include <rdma/ib_cache.h> 27 #include <rdma/ib_cm.h> 28 #include "cm_msgs.h" 29 #include "core_priv.h" 30 #include "cm_trace.h" 31 32 MODULE_AUTHOR("Sean Hefty"); 33 MODULE_DESCRIPTION("InfiniBand CM"); 34 MODULE_LICENSE("Dual BSD/GPL"); 35 36 static const char * const ibcm_rej_reason_strs[] = { 37 [IB_CM_REJ_NO_QP] = "no QP", 38 [IB_CM_REJ_NO_EEC] = "no EEC", 39 [IB_CM_REJ_NO_RESOURCES] = "no resources", 40 [IB_CM_REJ_TIMEOUT] = "timeout", 41 [IB_CM_REJ_UNSUPPORTED] = "unsupported", 42 [IB_CM_REJ_INVALID_COMM_ID] = "invalid comm ID", 43 [IB_CM_REJ_INVALID_COMM_INSTANCE] = "invalid comm instance", 44 [IB_CM_REJ_INVALID_SERVICE_ID] = "invalid service ID", 45 [IB_CM_REJ_INVALID_TRANSPORT_TYPE] = "invalid transport type", 46 [IB_CM_REJ_STALE_CONN] = "stale conn", 47 [IB_CM_REJ_RDC_NOT_EXIST] = "RDC not exist", 48 [IB_CM_REJ_INVALID_GID] = "invalid GID", 49 [IB_CM_REJ_INVALID_LID] = "invalid LID", 50 [IB_CM_REJ_INVALID_SL] = "invalid SL", 51 [IB_CM_REJ_INVALID_TRAFFIC_CLASS] = "invalid traffic class", 52 [IB_CM_REJ_INVALID_HOP_LIMIT] = "invalid hop limit", 53 [IB_CM_REJ_INVALID_PACKET_RATE] = "invalid packet rate", 54 [IB_CM_REJ_INVALID_ALT_GID] = "invalid alt GID", 55 [IB_CM_REJ_INVALID_ALT_LID] = "invalid alt LID", 56 [IB_CM_REJ_INVALID_ALT_SL] = "invalid alt SL", 57 [IB_CM_REJ_INVALID_ALT_TRAFFIC_CLASS] = "invalid alt traffic class", 58 [IB_CM_REJ_INVALID_ALT_HOP_LIMIT] = "invalid alt hop limit", 59 [IB_CM_REJ_INVALID_ALT_PACKET_RATE] = "invalid alt packet rate", 60 [IB_CM_REJ_PORT_CM_REDIRECT] = "port CM redirect", 61 [IB_CM_REJ_PORT_REDIRECT] = "port redirect", 62 [IB_CM_REJ_INVALID_MTU] = "invalid MTU", 63 [IB_CM_REJ_INSUFFICIENT_RESP_RESOURCES] = "insufficient resp resources", 64 [IB_CM_REJ_CONSUMER_DEFINED] = "consumer defined", 65 [IB_CM_REJ_INVALID_RNR_RETRY] = "invalid RNR retry", 66 [IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID] = "duplicate local comm ID", 67 [IB_CM_REJ_INVALID_CLASS_VERSION] = "invalid class version", 68 [IB_CM_REJ_INVALID_FLOW_LABEL] = "invalid flow label", 69 [IB_CM_REJ_INVALID_ALT_FLOW_LABEL] = "invalid alt flow label", 70 [IB_CM_REJ_VENDOR_OPTION_NOT_SUPPORTED] = 71 "vendor option is not supported", 72 }; 73 74 const char *__attribute_const__ ibcm_reject_msg(int reason) 75 { 76 size_t index = reason; 77 78 if (index < ARRAY_SIZE(ibcm_rej_reason_strs) && 79 ibcm_rej_reason_strs[index]) 80 return ibcm_rej_reason_strs[index]; 81 else 82 return "unrecognized reason"; 83 } 84 EXPORT_SYMBOL(ibcm_reject_msg); 85 86 struct cm_id_private; 87 struct cm_work; 88 static int cm_add_one(struct ib_device *device); 89 static void cm_remove_one(struct ib_device *device, void *client_data); 90 static void cm_process_work(struct cm_id_private *cm_id_priv, 91 struct cm_work *work); 92 static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv, 93 struct ib_cm_sidr_rep_param *param); 94 static int cm_send_dreq_locked(struct cm_id_private *cm_id_priv, 95 const void *private_data, u8 private_data_len); 96 static int cm_send_drep_locked(struct cm_id_private *cm_id_priv, 97 void *private_data, u8 private_data_len); 98 static int cm_send_rej_locked(struct cm_id_private *cm_id_priv, 99 enum ib_cm_rej_reason reason, void *ari, 100 u8 ari_length, const void *private_data, 101 u8 private_data_len); 102 103 static struct ib_client cm_client = { 104 .name = "cm", 105 .add = cm_add_one, 106 .remove = cm_remove_one 107 }; 108 109 static struct ib_cm { 110 spinlock_t lock; 111 struct list_head device_list; 112 rwlock_t device_lock; 113 struct rb_root listen_service_table; 114 u64 listen_service_id; 115 /* struct rb_root peer_service_table; todo: fix peer to peer */ 116 struct rb_root remote_qp_table; 117 struct rb_root remote_id_table; 118 struct rb_root remote_sidr_table; 119 struct xarray local_id_table; 120 u32 local_id_next; 121 __be32 random_id_operand; 122 struct list_head timewait_list; 123 struct workqueue_struct *wq; 124 /* Sync on cm change port state */ 125 spinlock_t state_lock; 126 } cm; 127 128 /* Counter indexes ordered by attribute ID */ 129 enum { 130 CM_REQ_COUNTER, 131 CM_MRA_COUNTER, 132 CM_REJ_COUNTER, 133 CM_REP_COUNTER, 134 CM_RTU_COUNTER, 135 CM_DREQ_COUNTER, 136 CM_DREP_COUNTER, 137 CM_SIDR_REQ_COUNTER, 138 CM_SIDR_REP_COUNTER, 139 CM_LAP_COUNTER, 140 CM_APR_COUNTER, 141 CM_ATTR_COUNT, 142 CM_ATTR_ID_OFFSET = 0x0010, 143 }; 144 145 enum { 146 CM_XMIT, 147 CM_XMIT_RETRIES, 148 CM_RECV, 149 CM_RECV_DUPLICATES, 150 CM_COUNTER_GROUPS 151 }; 152 153 static char const counter_group_names[CM_COUNTER_GROUPS] 154 [sizeof("cm_rx_duplicates")] = { 155 "cm_tx_msgs", "cm_tx_retries", 156 "cm_rx_msgs", "cm_rx_duplicates" 157 }; 158 159 struct cm_counter_group { 160 struct kobject obj; 161 atomic_long_t counter[CM_ATTR_COUNT]; 162 }; 163 164 struct cm_counter_attribute { 165 struct attribute attr; 166 int index; 167 }; 168 169 #define CM_COUNTER_ATTR(_name, _index) \ 170 struct cm_counter_attribute cm_##_name##_counter_attr = { \ 171 .attr = { .name = __stringify(_name), .mode = 0444 }, \ 172 .index = _index \ 173 } 174 175 static CM_COUNTER_ATTR(req, CM_REQ_COUNTER); 176 static CM_COUNTER_ATTR(mra, CM_MRA_COUNTER); 177 static CM_COUNTER_ATTR(rej, CM_REJ_COUNTER); 178 static CM_COUNTER_ATTR(rep, CM_REP_COUNTER); 179 static CM_COUNTER_ATTR(rtu, CM_RTU_COUNTER); 180 static CM_COUNTER_ATTR(dreq, CM_DREQ_COUNTER); 181 static CM_COUNTER_ATTR(drep, CM_DREP_COUNTER); 182 static CM_COUNTER_ATTR(sidr_req, CM_SIDR_REQ_COUNTER); 183 static CM_COUNTER_ATTR(sidr_rep, CM_SIDR_REP_COUNTER); 184 static CM_COUNTER_ATTR(lap, CM_LAP_COUNTER); 185 static CM_COUNTER_ATTR(apr, CM_APR_COUNTER); 186 187 static struct attribute *cm_counter_default_attrs[] = { 188 &cm_req_counter_attr.attr, 189 &cm_mra_counter_attr.attr, 190 &cm_rej_counter_attr.attr, 191 &cm_rep_counter_attr.attr, 192 &cm_rtu_counter_attr.attr, 193 &cm_dreq_counter_attr.attr, 194 &cm_drep_counter_attr.attr, 195 &cm_sidr_req_counter_attr.attr, 196 &cm_sidr_rep_counter_attr.attr, 197 &cm_lap_counter_attr.attr, 198 &cm_apr_counter_attr.attr, 199 NULL 200 }; 201 202 struct cm_port { 203 struct cm_device *cm_dev; 204 struct ib_mad_agent *mad_agent; 205 u32 port_num; 206 struct list_head cm_priv_prim_list; 207 struct list_head cm_priv_altr_list; 208 struct cm_counter_group counter_group[CM_COUNTER_GROUPS]; 209 }; 210 211 struct cm_device { 212 struct list_head list; 213 struct ib_device *ib_device; 214 u8 ack_delay; 215 int going_down; 216 struct cm_port *port[]; 217 }; 218 219 struct cm_av { 220 struct cm_port *port; 221 union ib_gid dgid; 222 struct rdma_ah_attr ah_attr; 223 u16 pkey_index; 224 u8 timeout; 225 }; 226 227 struct cm_work { 228 struct delayed_work work; 229 struct list_head list; 230 struct cm_port *port; 231 struct ib_mad_recv_wc *mad_recv_wc; /* Received MADs */ 232 __be32 local_id; /* Established / timewait */ 233 __be32 remote_id; 234 struct ib_cm_event cm_event; 235 struct sa_path_rec path[]; 236 }; 237 238 struct cm_timewait_info { 239 struct cm_work work; 240 struct list_head list; 241 struct rb_node remote_qp_node; 242 struct rb_node remote_id_node; 243 __be64 remote_ca_guid; 244 __be32 remote_qpn; 245 u8 inserted_remote_qp; 246 u8 inserted_remote_id; 247 }; 248 249 struct cm_id_private { 250 struct ib_cm_id id; 251 252 struct rb_node service_node; 253 struct rb_node sidr_id_node; 254 spinlock_t lock; /* Do not acquire inside cm.lock */ 255 struct completion comp; 256 refcount_t refcount; 257 /* Number of clients sharing this ib_cm_id. Only valid for listeners. 258 * Protected by the cm.lock spinlock. 259 */ 260 int listen_sharecount; 261 struct rcu_head rcu; 262 263 struct ib_mad_send_buf *msg; 264 struct cm_timewait_info *timewait_info; 265 /* todo: use alternate port on send failure */ 266 struct cm_av av; 267 struct cm_av alt_av; 268 269 void *private_data; 270 __be64 tid; 271 __be32 local_qpn; 272 __be32 remote_qpn; 273 enum ib_qp_type qp_type; 274 __be32 sq_psn; 275 __be32 rq_psn; 276 int timeout_ms; 277 enum ib_mtu path_mtu; 278 __be16 pkey; 279 u8 private_data_len; 280 u8 max_cm_retries; 281 u8 responder_resources; 282 u8 initiator_depth; 283 u8 retry_count; 284 u8 rnr_retry_count; 285 u8 service_timeout; 286 u8 target_ack_delay; 287 288 struct list_head prim_list; 289 struct list_head altr_list; 290 /* Indicates that the send port mad is registered and av is set */ 291 int prim_send_port_not_ready; 292 int altr_send_port_not_ready; 293 294 struct list_head work_list; 295 atomic_t work_count; 296 297 struct rdma_ucm_ece ece; 298 }; 299 300 static void cm_work_handler(struct work_struct *work); 301 302 static inline void cm_deref_id(struct cm_id_private *cm_id_priv) 303 { 304 if (refcount_dec_and_test(&cm_id_priv->refcount)) 305 complete(&cm_id_priv->comp); 306 } 307 308 static int cm_alloc_msg(struct cm_id_private *cm_id_priv, 309 struct ib_mad_send_buf **msg) 310 { 311 struct ib_mad_agent *mad_agent; 312 struct ib_mad_send_buf *m; 313 struct ib_ah *ah; 314 struct cm_av *av; 315 unsigned long flags, flags2; 316 int ret = 0; 317 318 /* don't let the port to be released till the agent is down */ 319 spin_lock_irqsave(&cm.state_lock, flags2); 320 spin_lock_irqsave(&cm.lock, flags); 321 if (!cm_id_priv->prim_send_port_not_ready) 322 av = &cm_id_priv->av; 323 else if (!cm_id_priv->altr_send_port_not_ready && 324 (cm_id_priv->alt_av.port)) 325 av = &cm_id_priv->alt_av; 326 else { 327 pr_info("%s: not valid CM id\n", __func__); 328 ret = -ENODEV; 329 spin_unlock_irqrestore(&cm.lock, flags); 330 goto out; 331 } 332 spin_unlock_irqrestore(&cm.lock, flags); 333 /* Make sure the port haven't released the mad yet */ 334 mad_agent = cm_id_priv->av.port->mad_agent; 335 if (!mad_agent) { 336 pr_info("%s: not a valid MAD agent\n", __func__); 337 ret = -ENODEV; 338 goto out; 339 } 340 ah = rdma_create_ah(mad_agent->qp->pd, &av->ah_attr, 0); 341 if (IS_ERR(ah)) { 342 ret = PTR_ERR(ah); 343 goto out; 344 } 345 346 m = ib_create_send_mad(mad_agent, cm_id_priv->id.remote_cm_qpn, 347 av->pkey_index, 348 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, 349 GFP_ATOMIC, 350 IB_MGMT_BASE_VERSION); 351 if (IS_ERR(m)) { 352 rdma_destroy_ah(ah, 0); 353 ret = PTR_ERR(m); 354 goto out; 355 } 356 357 /* Timeout set by caller if response is expected. */ 358 m->ah = ah; 359 m->retries = cm_id_priv->max_cm_retries; 360 361 refcount_inc(&cm_id_priv->refcount); 362 m->context[0] = cm_id_priv; 363 *msg = m; 364 365 out: 366 spin_unlock_irqrestore(&cm.state_lock, flags2); 367 return ret; 368 } 369 370 static struct ib_mad_send_buf *cm_alloc_response_msg_no_ah(struct cm_port *port, 371 struct ib_mad_recv_wc *mad_recv_wc) 372 { 373 return ib_create_send_mad(port->mad_agent, 1, mad_recv_wc->wc->pkey_index, 374 0, IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA, 375 GFP_ATOMIC, 376 IB_MGMT_BASE_VERSION); 377 } 378 379 static int cm_create_response_msg_ah(struct cm_port *port, 380 struct ib_mad_recv_wc *mad_recv_wc, 381 struct ib_mad_send_buf *msg) 382 { 383 struct ib_ah *ah; 384 385 ah = ib_create_ah_from_wc(port->mad_agent->qp->pd, mad_recv_wc->wc, 386 mad_recv_wc->recv_buf.grh, port->port_num); 387 if (IS_ERR(ah)) 388 return PTR_ERR(ah); 389 390 msg->ah = ah; 391 return 0; 392 } 393 394 static void cm_free_msg(struct ib_mad_send_buf *msg) 395 { 396 if (msg->ah) 397 rdma_destroy_ah(msg->ah, 0); 398 if (msg->context[0]) 399 cm_deref_id(msg->context[0]); 400 ib_free_send_mad(msg); 401 } 402 403 static int cm_alloc_response_msg(struct cm_port *port, 404 struct ib_mad_recv_wc *mad_recv_wc, 405 struct ib_mad_send_buf **msg) 406 { 407 struct ib_mad_send_buf *m; 408 int ret; 409 410 m = cm_alloc_response_msg_no_ah(port, mad_recv_wc); 411 if (IS_ERR(m)) 412 return PTR_ERR(m); 413 414 ret = cm_create_response_msg_ah(port, mad_recv_wc, m); 415 if (ret) { 416 cm_free_msg(m); 417 return ret; 418 } 419 420 *msg = m; 421 return 0; 422 } 423 424 static void *cm_copy_private_data(const void *private_data, u8 private_data_len) 425 { 426 void *data; 427 428 if (!private_data || !private_data_len) 429 return NULL; 430 431 data = kmemdup(private_data, private_data_len, GFP_KERNEL); 432 if (!data) 433 return ERR_PTR(-ENOMEM); 434 435 return data; 436 } 437 438 static void cm_set_private_data(struct cm_id_private *cm_id_priv, 439 void *private_data, u8 private_data_len) 440 { 441 if (cm_id_priv->private_data && cm_id_priv->private_data_len) 442 kfree(cm_id_priv->private_data); 443 444 cm_id_priv->private_data = private_data; 445 cm_id_priv->private_data_len = private_data_len; 446 } 447 448 static int cm_init_av_for_lap(struct cm_port *port, struct ib_wc *wc, 449 struct ib_grh *grh, struct cm_av *av) 450 { 451 struct rdma_ah_attr new_ah_attr; 452 int ret; 453 454 av->port = port; 455 av->pkey_index = wc->pkey_index; 456 457 /* 458 * av->ah_attr might be initialized based on past wc during incoming 459 * connect request or while sending out connect request. So initialize 460 * a new ah_attr on stack. If initialization fails, old ah_attr is 461 * used for sending any responses. If initialization is successful, 462 * than new ah_attr is used by overwriting old one. 463 */ 464 ret = ib_init_ah_attr_from_wc(port->cm_dev->ib_device, 465 port->port_num, wc, 466 grh, &new_ah_attr); 467 if (ret) 468 return ret; 469 470 rdma_move_ah_attr(&av->ah_attr, &new_ah_attr); 471 return 0; 472 } 473 474 static int cm_init_av_for_response(struct cm_port *port, struct ib_wc *wc, 475 struct ib_grh *grh, struct cm_av *av) 476 { 477 av->port = port; 478 av->pkey_index = wc->pkey_index; 479 return ib_init_ah_attr_from_wc(port->cm_dev->ib_device, 480 port->port_num, wc, 481 grh, &av->ah_attr); 482 } 483 484 static void add_cm_id_to_port_list(struct cm_id_private *cm_id_priv, 485 struct cm_av *av, struct cm_port *port) 486 { 487 unsigned long flags; 488 489 spin_lock_irqsave(&cm.lock, flags); 490 if (&cm_id_priv->av == av) 491 list_add_tail(&cm_id_priv->prim_list, &port->cm_priv_prim_list); 492 else if (&cm_id_priv->alt_av == av) 493 list_add_tail(&cm_id_priv->altr_list, &port->cm_priv_altr_list); 494 else 495 WARN_ON(true); 496 spin_unlock_irqrestore(&cm.lock, flags); 497 } 498 499 static struct cm_port * 500 get_cm_port_from_path(struct sa_path_rec *path, const struct ib_gid_attr *attr) 501 { 502 struct cm_device *cm_dev; 503 struct cm_port *port = NULL; 504 unsigned long flags; 505 506 if (attr) { 507 read_lock_irqsave(&cm.device_lock, flags); 508 list_for_each_entry(cm_dev, &cm.device_list, list) { 509 if (cm_dev->ib_device == attr->device) { 510 port = cm_dev->port[attr->port_num - 1]; 511 break; 512 } 513 } 514 read_unlock_irqrestore(&cm.device_lock, flags); 515 } else { 516 /* SGID attribute can be NULL in following 517 * conditions. 518 * (a) Alternative path 519 * (b) IB link layer without GRH 520 * (c) LAP send messages 521 */ 522 read_lock_irqsave(&cm.device_lock, flags); 523 list_for_each_entry(cm_dev, &cm.device_list, list) { 524 attr = rdma_find_gid(cm_dev->ib_device, 525 &path->sgid, 526 sa_conv_pathrec_to_gid_type(path), 527 NULL); 528 if (!IS_ERR(attr)) { 529 port = cm_dev->port[attr->port_num - 1]; 530 break; 531 } 532 } 533 read_unlock_irqrestore(&cm.device_lock, flags); 534 if (port) 535 rdma_put_gid_attr(attr); 536 } 537 return port; 538 } 539 540 static int cm_init_av_by_path(struct sa_path_rec *path, 541 const struct ib_gid_attr *sgid_attr, 542 struct cm_av *av, 543 struct cm_id_private *cm_id_priv) 544 { 545 struct rdma_ah_attr new_ah_attr; 546 struct cm_device *cm_dev; 547 struct cm_port *port; 548 int ret; 549 550 port = get_cm_port_from_path(path, sgid_attr); 551 if (!port) 552 return -EINVAL; 553 cm_dev = port->cm_dev; 554 555 ret = ib_find_cached_pkey(cm_dev->ib_device, port->port_num, 556 be16_to_cpu(path->pkey), &av->pkey_index); 557 if (ret) 558 return ret; 559 560 av->port = port; 561 562 /* 563 * av->ah_attr might be initialized based on wc or during 564 * request processing time which might have reference to sgid_attr. 565 * So initialize a new ah_attr on stack. 566 * If initialization fails, old ah_attr is used for sending any 567 * responses. If initialization is successful, than new ah_attr 568 * is used by overwriting the old one. So that right ah_attr 569 * can be used to return an error response. 570 */ 571 ret = ib_init_ah_attr_from_path(cm_dev->ib_device, port->port_num, path, 572 &new_ah_attr, sgid_attr); 573 if (ret) 574 return ret; 575 576 av->timeout = path->packet_life_time + 1; 577 add_cm_id_to_port_list(cm_id_priv, av, port); 578 rdma_move_ah_attr(&av->ah_attr, &new_ah_attr); 579 return 0; 580 } 581 582 static u32 cm_local_id(__be32 local_id) 583 { 584 return (__force u32) (local_id ^ cm.random_id_operand); 585 } 586 587 static struct cm_id_private *cm_acquire_id(__be32 local_id, __be32 remote_id) 588 { 589 struct cm_id_private *cm_id_priv; 590 591 rcu_read_lock(); 592 cm_id_priv = xa_load(&cm.local_id_table, cm_local_id(local_id)); 593 if (!cm_id_priv || cm_id_priv->id.remote_id != remote_id || 594 !refcount_inc_not_zero(&cm_id_priv->refcount)) 595 cm_id_priv = NULL; 596 rcu_read_unlock(); 597 598 return cm_id_priv; 599 } 600 601 /* 602 * Trivial helpers to strip endian annotation and compare; the 603 * endianness doesn't actually matter since we just need a stable 604 * order for the RB tree. 605 */ 606 static int be32_lt(__be32 a, __be32 b) 607 { 608 return (__force u32) a < (__force u32) b; 609 } 610 611 static int be32_gt(__be32 a, __be32 b) 612 { 613 return (__force u32) a > (__force u32) b; 614 } 615 616 static int be64_lt(__be64 a, __be64 b) 617 { 618 return (__force u64) a < (__force u64) b; 619 } 620 621 static int be64_gt(__be64 a, __be64 b) 622 { 623 return (__force u64) a > (__force u64) b; 624 } 625 626 /* 627 * Inserts a new cm_id_priv into the listen_service_table. Returns cm_id_priv 628 * if the new ID was inserted, NULL if it could not be inserted due to a 629 * collision, or the existing cm_id_priv ready for shared usage. 630 */ 631 static struct cm_id_private *cm_insert_listen(struct cm_id_private *cm_id_priv, 632 ib_cm_handler shared_handler) 633 { 634 struct rb_node **link = &cm.listen_service_table.rb_node; 635 struct rb_node *parent = NULL; 636 struct cm_id_private *cur_cm_id_priv; 637 __be64 service_id = cm_id_priv->id.service_id; 638 __be64 service_mask = cm_id_priv->id.service_mask; 639 unsigned long flags; 640 641 spin_lock_irqsave(&cm.lock, flags); 642 while (*link) { 643 parent = *link; 644 cur_cm_id_priv = rb_entry(parent, struct cm_id_private, 645 service_node); 646 if ((cur_cm_id_priv->id.service_mask & service_id) == 647 (service_mask & cur_cm_id_priv->id.service_id) && 648 (cm_id_priv->id.device == cur_cm_id_priv->id.device)) { 649 /* 650 * Sharing an ib_cm_id with different handlers is not 651 * supported 652 */ 653 if (cur_cm_id_priv->id.cm_handler != shared_handler || 654 cur_cm_id_priv->id.context || 655 WARN_ON(!cur_cm_id_priv->id.cm_handler)) { 656 spin_unlock_irqrestore(&cm.lock, flags); 657 return NULL; 658 } 659 refcount_inc(&cur_cm_id_priv->refcount); 660 cur_cm_id_priv->listen_sharecount++; 661 spin_unlock_irqrestore(&cm.lock, flags); 662 return cur_cm_id_priv; 663 } 664 665 if (cm_id_priv->id.device < cur_cm_id_priv->id.device) 666 link = &(*link)->rb_left; 667 else if (cm_id_priv->id.device > cur_cm_id_priv->id.device) 668 link = &(*link)->rb_right; 669 else if (be64_lt(service_id, cur_cm_id_priv->id.service_id)) 670 link = &(*link)->rb_left; 671 else if (be64_gt(service_id, cur_cm_id_priv->id.service_id)) 672 link = &(*link)->rb_right; 673 else 674 link = &(*link)->rb_right; 675 } 676 cm_id_priv->listen_sharecount++; 677 rb_link_node(&cm_id_priv->service_node, parent, link); 678 rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table); 679 spin_unlock_irqrestore(&cm.lock, flags); 680 return cm_id_priv; 681 } 682 683 static struct cm_id_private *cm_find_listen(struct ib_device *device, 684 __be64 service_id) 685 { 686 struct rb_node *node = cm.listen_service_table.rb_node; 687 struct cm_id_private *cm_id_priv; 688 689 while (node) { 690 cm_id_priv = rb_entry(node, struct cm_id_private, service_node); 691 if ((cm_id_priv->id.service_mask & service_id) == 692 cm_id_priv->id.service_id && 693 (cm_id_priv->id.device == device)) { 694 refcount_inc(&cm_id_priv->refcount); 695 return cm_id_priv; 696 } 697 if (device < cm_id_priv->id.device) 698 node = node->rb_left; 699 else if (device > cm_id_priv->id.device) 700 node = node->rb_right; 701 else if (be64_lt(service_id, cm_id_priv->id.service_id)) 702 node = node->rb_left; 703 else if (be64_gt(service_id, cm_id_priv->id.service_id)) 704 node = node->rb_right; 705 else 706 node = node->rb_right; 707 } 708 return NULL; 709 } 710 711 static struct cm_timewait_info * 712 cm_insert_remote_id(struct cm_timewait_info *timewait_info) 713 { 714 struct rb_node **link = &cm.remote_id_table.rb_node; 715 struct rb_node *parent = NULL; 716 struct cm_timewait_info *cur_timewait_info; 717 __be64 remote_ca_guid = timewait_info->remote_ca_guid; 718 __be32 remote_id = timewait_info->work.remote_id; 719 720 while (*link) { 721 parent = *link; 722 cur_timewait_info = rb_entry(parent, struct cm_timewait_info, 723 remote_id_node); 724 if (be32_lt(remote_id, cur_timewait_info->work.remote_id)) 725 link = &(*link)->rb_left; 726 else if (be32_gt(remote_id, cur_timewait_info->work.remote_id)) 727 link = &(*link)->rb_right; 728 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid)) 729 link = &(*link)->rb_left; 730 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid)) 731 link = &(*link)->rb_right; 732 else 733 return cur_timewait_info; 734 } 735 timewait_info->inserted_remote_id = 1; 736 rb_link_node(&timewait_info->remote_id_node, parent, link); 737 rb_insert_color(&timewait_info->remote_id_node, &cm.remote_id_table); 738 return NULL; 739 } 740 741 static struct cm_id_private *cm_find_remote_id(__be64 remote_ca_guid, 742 __be32 remote_id) 743 { 744 struct rb_node *node = cm.remote_id_table.rb_node; 745 struct cm_timewait_info *timewait_info; 746 struct cm_id_private *res = NULL; 747 748 spin_lock_irq(&cm.lock); 749 while (node) { 750 timewait_info = rb_entry(node, struct cm_timewait_info, 751 remote_id_node); 752 if (be32_lt(remote_id, timewait_info->work.remote_id)) 753 node = node->rb_left; 754 else if (be32_gt(remote_id, timewait_info->work.remote_id)) 755 node = node->rb_right; 756 else if (be64_lt(remote_ca_guid, timewait_info->remote_ca_guid)) 757 node = node->rb_left; 758 else if (be64_gt(remote_ca_guid, timewait_info->remote_ca_guid)) 759 node = node->rb_right; 760 else { 761 res = cm_acquire_id(timewait_info->work.local_id, 762 timewait_info->work.remote_id); 763 break; 764 } 765 } 766 spin_unlock_irq(&cm.lock); 767 return res; 768 } 769 770 static struct cm_timewait_info * 771 cm_insert_remote_qpn(struct cm_timewait_info *timewait_info) 772 { 773 struct rb_node **link = &cm.remote_qp_table.rb_node; 774 struct rb_node *parent = NULL; 775 struct cm_timewait_info *cur_timewait_info; 776 __be64 remote_ca_guid = timewait_info->remote_ca_guid; 777 __be32 remote_qpn = timewait_info->remote_qpn; 778 779 while (*link) { 780 parent = *link; 781 cur_timewait_info = rb_entry(parent, struct cm_timewait_info, 782 remote_qp_node); 783 if (be32_lt(remote_qpn, cur_timewait_info->remote_qpn)) 784 link = &(*link)->rb_left; 785 else if (be32_gt(remote_qpn, cur_timewait_info->remote_qpn)) 786 link = &(*link)->rb_right; 787 else if (be64_lt(remote_ca_guid, cur_timewait_info->remote_ca_guid)) 788 link = &(*link)->rb_left; 789 else if (be64_gt(remote_ca_guid, cur_timewait_info->remote_ca_guid)) 790 link = &(*link)->rb_right; 791 else 792 return cur_timewait_info; 793 } 794 timewait_info->inserted_remote_qp = 1; 795 rb_link_node(&timewait_info->remote_qp_node, parent, link); 796 rb_insert_color(&timewait_info->remote_qp_node, &cm.remote_qp_table); 797 return NULL; 798 } 799 800 static struct cm_id_private * 801 cm_insert_remote_sidr(struct cm_id_private *cm_id_priv) 802 { 803 struct rb_node **link = &cm.remote_sidr_table.rb_node; 804 struct rb_node *parent = NULL; 805 struct cm_id_private *cur_cm_id_priv; 806 union ib_gid *port_gid = &cm_id_priv->av.dgid; 807 __be32 remote_id = cm_id_priv->id.remote_id; 808 809 while (*link) { 810 parent = *link; 811 cur_cm_id_priv = rb_entry(parent, struct cm_id_private, 812 sidr_id_node); 813 if (be32_lt(remote_id, cur_cm_id_priv->id.remote_id)) 814 link = &(*link)->rb_left; 815 else if (be32_gt(remote_id, cur_cm_id_priv->id.remote_id)) 816 link = &(*link)->rb_right; 817 else { 818 int cmp; 819 cmp = memcmp(port_gid, &cur_cm_id_priv->av.dgid, 820 sizeof *port_gid); 821 if (cmp < 0) 822 link = &(*link)->rb_left; 823 else if (cmp > 0) 824 link = &(*link)->rb_right; 825 else 826 return cur_cm_id_priv; 827 } 828 } 829 rb_link_node(&cm_id_priv->sidr_id_node, parent, link); 830 rb_insert_color(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); 831 return NULL; 832 } 833 834 static struct cm_id_private *cm_alloc_id_priv(struct ib_device *device, 835 ib_cm_handler cm_handler, 836 void *context) 837 { 838 struct cm_id_private *cm_id_priv; 839 u32 id; 840 int ret; 841 842 cm_id_priv = kzalloc(sizeof *cm_id_priv, GFP_KERNEL); 843 if (!cm_id_priv) 844 return ERR_PTR(-ENOMEM); 845 846 cm_id_priv->id.state = IB_CM_IDLE; 847 cm_id_priv->id.device = device; 848 cm_id_priv->id.cm_handler = cm_handler; 849 cm_id_priv->id.context = context; 850 cm_id_priv->id.remote_cm_qpn = 1; 851 852 RB_CLEAR_NODE(&cm_id_priv->service_node); 853 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node); 854 spin_lock_init(&cm_id_priv->lock); 855 init_completion(&cm_id_priv->comp); 856 INIT_LIST_HEAD(&cm_id_priv->work_list); 857 INIT_LIST_HEAD(&cm_id_priv->prim_list); 858 INIT_LIST_HEAD(&cm_id_priv->altr_list); 859 atomic_set(&cm_id_priv->work_count, -1); 860 refcount_set(&cm_id_priv->refcount, 1); 861 862 ret = xa_alloc_cyclic(&cm.local_id_table, &id, NULL, xa_limit_32b, 863 &cm.local_id_next, GFP_KERNEL); 864 if (ret < 0) 865 goto error; 866 cm_id_priv->id.local_id = (__force __be32)id ^ cm.random_id_operand; 867 868 return cm_id_priv; 869 870 error: 871 kfree(cm_id_priv); 872 return ERR_PTR(ret); 873 } 874 875 /* 876 * Make the ID visible to the MAD handlers and other threads that use the 877 * xarray. 878 */ 879 static void cm_finalize_id(struct cm_id_private *cm_id_priv) 880 { 881 xa_store(&cm.local_id_table, cm_local_id(cm_id_priv->id.local_id), 882 cm_id_priv, GFP_ATOMIC); 883 } 884 885 struct ib_cm_id *ib_create_cm_id(struct ib_device *device, 886 ib_cm_handler cm_handler, 887 void *context) 888 { 889 struct cm_id_private *cm_id_priv; 890 891 cm_id_priv = cm_alloc_id_priv(device, cm_handler, context); 892 if (IS_ERR(cm_id_priv)) 893 return ERR_CAST(cm_id_priv); 894 895 cm_finalize_id(cm_id_priv); 896 return &cm_id_priv->id; 897 } 898 EXPORT_SYMBOL(ib_create_cm_id); 899 900 static struct cm_work *cm_dequeue_work(struct cm_id_private *cm_id_priv) 901 { 902 struct cm_work *work; 903 904 if (list_empty(&cm_id_priv->work_list)) 905 return NULL; 906 907 work = list_entry(cm_id_priv->work_list.next, struct cm_work, list); 908 list_del(&work->list); 909 return work; 910 } 911 912 static void cm_free_work(struct cm_work *work) 913 { 914 if (work->mad_recv_wc) 915 ib_free_recv_mad(work->mad_recv_wc); 916 kfree(work); 917 } 918 919 static void cm_queue_work_unlock(struct cm_id_private *cm_id_priv, 920 struct cm_work *work) 921 __releases(&cm_id_priv->lock) 922 { 923 bool immediate; 924 925 /* 926 * To deliver the event to the user callback we have the drop the 927 * spinlock, however, we need to ensure that the user callback is single 928 * threaded and receives events in the temporal order. If there are 929 * already events being processed then thread new events onto a list, 930 * the thread currently processing will pick them up. 931 */ 932 immediate = atomic_inc_and_test(&cm_id_priv->work_count); 933 if (!immediate) { 934 list_add_tail(&work->list, &cm_id_priv->work_list); 935 /* 936 * This routine always consumes incoming reference. Once queued 937 * to the work_list then a reference is held by the thread 938 * currently running cm_process_work() and this reference is not 939 * needed. 940 */ 941 cm_deref_id(cm_id_priv); 942 } 943 spin_unlock_irq(&cm_id_priv->lock); 944 945 if (immediate) 946 cm_process_work(cm_id_priv, work); 947 } 948 949 static inline int cm_convert_to_ms(int iba_time) 950 { 951 /* approximate conversion to ms from 4.096us x 2^iba_time */ 952 return 1 << max(iba_time - 8, 0); 953 } 954 955 /* 956 * calculate: 4.096x2^ack_timeout = 4.096x2^ack_delay + 2x4.096x2^life_time 957 * Because of how ack_timeout is stored, adding one doubles the timeout. 958 * To avoid large timeouts, select the max(ack_delay, life_time + 1), and 959 * increment it (round up) only if the other is within 50%. 960 */ 961 static u8 cm_ack_timeout(u8 ca_ack_delay, u8 packet_life_time) 962 { 963 int ack_timeout = packet_life_time + 1; 964 965 if (ack_timeout >= ca_ack_delay) 966 ack_timeout += (ca_ack_delay >= (ack_timeout - 1)); 967 else 968 ack_timeout = ca_ack_delay + 969 (ack_timeout >= (ca_ack_delay - 1)); 970 971 return min(31, ack_timeout); 972 } 973 974 static void cm_remove_remote(struct cm_id_private *cm_id_priv) 975 { 976 struct cm_timewait_info *timewait_info = cm_id_priv->timewait_info; 977 978 if (timewait_info->inserted_remote_id) { 979 rb_erase(&timewait_info->remote_id_node, &cm.remote_id_table); 980 timewait_info->inserted_remote_id = 0; 981 } 982 983 if (timewait_info->inserted_remote_qp) { 984 rb_erase(&timewait_info->remote_qp_node, &cm.remote_qp_table); 985 timewait_info->inserted_remote_qp = 0; 986 } 987 } 988 989 static struct cm_timewait_info *cm_create_timewait_info(__be32 local_id) 990 { 991 struct cm_timewait_info *timewait_info; 992 993 timewait_info = kzalloc(sizeof *timewait_info, GFP_KERNEL); 994 if (!timewait_info) 995 return ERR_PTR(-ENOMEM); 996 997 timewait_info->work.local_id = local_id; 998 INIT_DELAYED_WORK(&timewait_info->work.work, cm_work_handler); 999 timewait_info->work.cm_event.event = IB_CM_TIMEWAIT_EXIT; 1000 return timewait_info; 1001 } 1002 1003 static void cm_enter_timewait(struct cm_id_private *cm_id_priv) 1004 { 1005 int wait_time; 1006 unsigned long flags; 1007 struct cm_device *cm_dev; 1008 1009 lockdep_assert_held(&cm_id_priv->lock); 1010 1011 cm_dev = ib_get_client_data(cm_id_priv->id.device, &cm_client); 1012 if (!cm_dev) 1013 return; 1014 1015 spin_lock_irqsave(&cm.lock, flags); 1016 cm_remove_remote(cm_id_priv); 1017 list_add_tail(&cm_id_priv->timewait_info->list, &cm.timewait_list); 1018 spin_unlock_irqrestore(&cm.lock, flags); 1019 1020 /* 1021 * The cm_id could be destroyed by the user before we exit timewait. 1022 * To protect against this, we search for the cm_id after exiting 1023 * timewait before notifying the user that we've exited timewait. 1024 */ 1025 cm_id_priv->id.state = IB_CM_TIMEWAIT; 1026 wait_time = cm_convert_to_ms(cm_id_priv->av.timeout); 1027 1028 /* Check if the device started its remove_one */ 1029 spin_lock_irqsave(&cm.lock, flags); 1030 if (!cm_dev->going_down) 1031 queue_delayed_work(cm.wq, &cm_id_priv->timewait_info->work.work, 1032 msecs_to_jiffies(wait_time)); 1033 spin_unlock_irqrestore(&cm.lock, flags); 1034 1035 /* 1036 * The timewait_info is converted into a work and gets freed during 1037 * cm_free_work() in cm_timewait_handler(). 1038 */ 1039 BUILD_BUG_ON(offsetof(struct cm_timewait_info, work) != 0); 1040 cm_id_priv->timewait_info = NULL; 1041 } 1042 1043 static void cm_reset_to_idle(struct cm_id_private *cm_id_priv) 1044 { 1045 unsigned long flags; 1046 1047 lockdep_assert_held(&cm_id_priv->lock); 1048 1049 cm_id_priv->id.state = IB_CM_IDLE; 1050 if (cm_id_priv->timewait_info) { 1051 spin_lock_irqsave(&cm.lock, flags); 1052 cm_remove_remote(cm_id_priv); 1053 spin_unlock_irqrestore(&cm.lock, flags); 1054 kfree(cm_id_priv->timewait_info); 1055 cm_id_priv->timewait_info = NULL; 1056 } 1057 } 1058 1059 static void cm_destroy_id(struct ib_cm_id *cm_id, int err) 1060 { 1061 struct cm_id_private *cm_id_priv; 1062 struct cm_work *work; 1063 1064 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 1065 spin_lock_irq(&cm_id_priv->lock); 1066 retest: 1067 switch (cm_id->state) { 1068 case IB_CM_LISTEN: 1069 spin_lock(&cm.lock); 1070 if (--cm_id_priv->listen_sharecount > 0) { 1071 /* The id is still shared. */ 1072 WARN_ON(refcount_read(&cm_id_priv->refcount) == 1); 1073 spin_unlock(&cm.lock); 1074 spin_unlock_irq(&cm_id_priv->lock); 1075 cm_deref_id(cm_id_priv); 1076 return; 1077 } 1078 cm_id->state = IB_CM_IDLE; 1079 rb_erase(&cm_id_priv->service_node, &cm.listen_service_table); 1080 RB_CLEAR_NODE(&cm_id_priv->service_node); 1081 spin_unlock(&cm.lock); 1082 break; 1083 case IB_CM_SIDR_REQ_SENT: 1084 cm_id->state = IB_CM_IDLE; 1085 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 1086 break; 1087 case IB_CM_SIDR_REQ_RCVD: 1088 cm_send_sidr_rep_locked(cm_id_priv, 1089 &(struct ib_cm_sidr_rep_param){ 1090 .status = IB_SIDR_REJECT }); 1091 /* cm_send_sidr_rep_locked will not move to IDLE if it fails */ 1092 cm_id->state = IB_CM_IDLE; 1093 break; 1094 case IB_CM_REQ_SENT: 1095 case IB_CM_MRA_REQ_RCVD: 1096 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 1097 cm_send_rej_locked(cm_id_priv, IB_CM_REJ_TIMEOUT, 1098 &cm_id_priv->id.device->node_guid, 1099 sizeof(cm_id_priv->id.device->node_guid), 1100 NULL, 0); 1101 break; 1102 case IB_CM_REQ_RCVD: 1103 if (err == -ENOMEM) { 1104 /* Do not reject to allow future retries. */ 1105 cm_reset_to_idle(cm_id_priv); 1106 } else { 1107 cm_send_rej_locked(cm_id_priv, 1108 IB_CM_REJ_CONSUMER_DEFINED, NULL, 0, 1109 NULL, 0); 1110 } 1111 break; 1112 case IB_CM_REP_SENT: 1113 case IB_CM_MRA_REP_RCVD: 1114 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 1115 cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL, 1116 0, NULL, 0); 1117 goto retest; 1118 case IB_CM_MRA_REQ_SENT: 1119 case IB_CM_REP_RCVD: 1120 case IB_CM_MRA_REP_SENT: 1121 cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, NULL, 1122 0, NULL, 0); 1123 break; 1124 case IB_CM_ESTABLISHED: 1125 if (cm_id_priv->qp_type == IB_QPT_XRC_TGT) { 1126 cm_id->state = IB_CM_IDLE; 1127 break; 1128 } 1129 cm_send_dreq_locked(cm_id_priv, NULL, 0); 1130 goto retest; 1131 case IB_CM_DREQ_SENT: 1132 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 1133 cm_enter_timewait(cm_id_priv); 1134 goto retest; 1135 case IB_CM_DREQ_RCVD: 1136 cm_send_drep_locked(cm_id_priv, NULL, 0); 1137 WARN_ON(cm_id->state != IB_CM_TIMEWAIT); 1138 goto retest; 1139 case IB_CM_TIMEWAIT: 1140 /* 1141 * The cm_acquire_id in cm_timewait_handler will stop working 1142 * once we do xa_erase below, so just move to idle here for 1143 * consistency. 1144 */ 1145 cm_id->state = IB_CM_IDLE; 1146 break; 1147 case IB_CM_IDLE: 1148 break; 1149 } 1150 WARN_ON(cm_id->state != IB_CM_IDLE); 1151 1152 spin_lock(&cm.lock); 1153 /* Required for cleanup paths related cm_req_handler() */ 1154 if (cm_id_priv->timewait_info) { 1155 cm_remove_remote(cm_id_priv); 1156 kfree(cm_id_priv->timewait_info); 1157 cm_id_priv->timewait_info = NULL; 1158 } 1159 if (!list_empty(&cm_id_priv->altr_list) && 1160 (!cm_id_priv->altr_send_port_not_ready)) 1161 list_del(&cm_id_priv->altr_list); 1162 if (!list_empty(&cm_id_priv->prim_list) && 1163 (!cm_id_priv->prim_send_port_not_ready)) 1164 list_del(&cm_id_priv->prim_list); 1165 WARN_ON(cm_id_priv->listen_sharecount); 1166 WARN_ON(!RB_EMPTY_NODE(&cm_id_priv->service_node)); 1167 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) 1168 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); 1169 spin_unlock(&cm.lock); 1170 spin_unlock_irq(&cm_id_priv->lock); 1171 1172 xa_erase(&cm.local_id_table, cm_local_id(cm_id->local_id)); 1173 cm_deref_id(cm_id_priv); 1174 wait_for_completion(&cm_id_priv->comp); 1175 while ((work = cm_dequeue_work(cm_id_priv)) != NULL) 1176 cm_free_work(work); 1177 1178 rdma_destroy_ah_attr(&cm_id_priv->av.ah_attr); 1179 rdma_destroy_ah_attr(&cm_id_priv->alt_av.ah_attr); 1180 kfree(cm_id_priv->private_data); 1181 kfree_rcu(cm_id_priv, rcu); 1182 } 1183 1184 void ib_destroy_cm_id(struct ib_cm_id *cm_id) 1185 { 1186 cm_destroy_id(cm_id, 0); 1187 } 1188 EXPORT_SYMBOL(ib_destroy_cm_id); 1189 1190 static int cm_init_listen(struct cm_id_private *cm_id_priv, __be64 service_id, 1191 __be64 service_mask) 1192 { 1193 service_mask = service_mask ? service_mask : ~cpu_to_be64(0); 1194 service_id &= service_mask; 1195 if ((service_id & IB_SERVICE_ID_AGN_MASK) == IB_CM_ASSIGN_SERVICE_ID && 1196 (service_id != IB_CM_ASSIGN_SERVICE_ID)) 1197 return -EINVAL; 1198 1199 if (service_id == IB_CM_ASSIGN_SERVICE_ID) { 1200 cm_id_priv->id.service_id = cpu_to_be64(cm.listen_service_id++); 1201 cm_id_priv->id.service_mask = ~cpu_to_be64(0); 1202 } else { 1203 cm_id_priv->id.service_id = service_id; 1204 cm_id_priv->id.service_mask = service_mask; 1205 } 1206 return 0; 1207 } 1208 1209 /** 1210 * ib_cm_listen - Initiates listening on the specified service ID for 1211 * connection and service ID resolution requests. 1212 * @cm_id: Connection identifier associated with the listen request. 1213 * @service_id: Service identifier matched against incoming connection 1214 * and service ID resolution requests. The service ID should be specified 1215 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will 1216 * assign a service ID to the caller. 1217 * @service_mask: Mask applied to service ID used to listen across a 1218 * range of service IDs. If set to 0, the service ID is matched 1219 * exactly. This parameter is ignored if %service_id is set to 1220 * IB_CM_ASSIGN_SERVICE_ID. 1221 */ 1222 int ib_cm_listen(struct ib_cm_id *cm_id, __be64 service_id, __be64 service_mask) 1223 { 1224 struct cm_id_private *cm_id_priv = 1225 container_of(cm_id, struct cm_id_private, id); 1226 unsigned long flags; 1227 int ret; 1228 1229 spin_lock_irqsave(&cm_id_priv->lock, flags); 1230 if (cm_id_priv->id.state != IB_CM_IDLE) { 1231 ret = -EINVAL; 1232 goto out; 1233 } 1234 1235 ret = cm_init_listen(cm_id_priv, service_id, service_mask); 1236 if (ret) 1237 goto out; 1238 1239 if (!cm_insert_listen(cm_id_priv, NULL)) { 1240 ret = -EBUSY; 1241 goto out; 1242 } 1243 1244 cm_id_priv->id.state = IB_CM_LISTEN; 1245 ret = 0; 1246 1247 out: 1248 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 1249 return ret; 1250 } 1251 EXPORT_SYMBOL(ib_cm_listen); 1252 1253 /** 1254 * ib_cm_insert_listen - Create a new listening ib_cm_id and listen on 1255 * the given service ID. 1256 * 1257 * If there's an existing ID listening on that same device and service ID, 1258 * return it. 1259 * 1260 * @device: Device associated with the cm_id. All related communication will 1261 * be associated with the specified device. 1262 * @cm_handler: Callback invoked to notify the user of CM events. 1263 * @service_id: Service identifier matched against incoming connection 1264 * and service ID resolution requests. The service ID should be specified 1265 * network-byte order. If set to IB_CM_ASSIGN_SERVICE_ID, the CM will 1266 * assign a service ID to the caller. 1267 * 1268 * Callers should call ib_destroy_cm_id when done with the listener ID. 1269 */ 1270 struct ib_cm_id *ib_cm_insert_listen(struct ib_device *device, 1271 ib_cm_handler cm_handler, 1272 __be64 service_id) 1273 { 1274 struct cm_id_private *listen_id_priv; 1275 struct cm_id_private *cm_id_priv; 1276 int err = 0; 1277 1278 /* Create an ID in advance, since the creation may sleep */ 1279 cm_id_priv = cm_alloc_id_priv(device, cm_handler, NULL); 1280 if (IS_ERR(cm_id_priv)) 1281 return ERR_CAST(cm_id_priv); 1282 1283 err = cm_init_listen(cm_id_priv, service_id, 0); 1284 if (err) 1285 return ERR_PTR(err); 1286 1287 spin_lock_irq(&cm_id_priv->lock); 1288 listen_id_priv = cm_insert_listen(cm_id_priv, cm_handler); 1289 if (listen_id_priv != cm_id_priv) { 1290 spin_unlock_irq(&cm_id_priv->lock); 1291 ib_destroy_cm_id(&cm_id_priv->id); 1292 if (!listen_id_priv) 1293 return ERR_PTR(-EINVAL); 1294 return &listen_id_priv->id; 1295 } 1296 cm_id_priv->id.state = IB_CM_LISTEN; 1297 spin_unlock_irq(&cm_id_priv->lock); 1298 1299 /* 1300 * A listen ID does not need to be in the xarray since it does not 1301 * receive mads, is not placed in the remote_id or remote_qpn rbtree, 1302 * and does not enter timewait. 1303 */ 1304 1305 return &cm_id_priv->id; 1306 } 1307 EXPORT_SYMBOL(ib_cm_insert_listen); 1308 1309 static __be64 cm_form_tid(struct cm_id_private *cm_id_priv) 1310 { 1311 u64 hi_tid, low_tid; 1312 1313 hi_tid = ((u64) cm_id_priv->av.port->mad_agent->hi_tid) << 32; 1314 low_tid = (u64)cm_id_priv->id.local_id; 1315 return cpu_to_be64(hi_tid | low_tid); 1316 } 1317 1318 static void cm_format_mad_hdr(struct ib_mad_hdr *hdr, 1319 __be16 attr_id, __be64 tid) 1320 { 1321 hdr->base_version = IB_MGMT_BASE_VERSION; 1322 hdr->mgmt_class = IB_MGMT_CLASS_CM; 1323 hdr->class_version = IB_CM_CLASS_VERSION; 1324 hdr->method = IB_MGMT_METHOD_SEND; 1325 hdr->attr_id = attr_id; 1326 hdr->tid = tid; 1327 } 1328 1329 static void cm_format_mad_ece_hdr(struct ib_mad_hdr *hdr, __be16 attr_id, 1330 __be64 tid, u32 attr_mod) 1331 { 1332 cm_format_mad_hdr(hdr, attr_id, tid); 1333 hdr->attr_mod = cpu_to_be32(attr_mod); 1334 } 1335 1336 static void cm_format_req(struct cm_req_msg *req_msg, 1337 struct cm_id_private *cm_id_priv, 1338 struct ib_cm_req_param *param) 1339 { 1340 struct sa_path_rec *pri_path = param->primary_path; 1341 struct sa_path_rec *alt_path = param->alternate_path; 1342 bool pri_ext = false; 1343 1344 if (pri_path->rec_type == SA_PATH_REC_TYPE_OPA) 1345 pri_ext = opa_is_extended_lid(pri_path->opa.dlid, 1346 pri_path->opa.slid); 1347 1348 cm_format_mad_ece_hdr(&req_msg->hdr, CM_REQ_ATTR_ID, 1349 cm_form_tid(cm_id_priv), param->ece.attr_mod); 1350 1351 IBA_SET(CM_REQ_LOCAL_COMM_ID, req_msg, 1352 be32_to_cpu(cm_id_priv->id.local_id)); 1353 IBA_SET(CM_REQ_SERVICE_ID, req_msg, be64_to_cpu(param->service_id)); 1354 IBA_SET(CM_REQ_LOCAL_CA_GUID, req_msg, 1355 be64_to_cpu(cm_id_priv->id.device->node_guid)); 1356 IBA_SET(CM_REQ_LOCAL_QPN, req_msg, param->qp_num); 1357 IBA_SET(CM_REQ_INITIATOR_DEPTH, req_msg, param->initiator_depth); 1358 IBA_SET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg, 1359 param->remote_cm_response_timeout); 1360 cm_req_set_qp_type(req_msg, param->qp_type); 1361 IBA_SET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg, param->flow_control); 1362 IBA_SET(CM_REQ_STARTING_PSN, req_msg, param->starting_psn); 1363 IBA_SET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg, 1364 param->local_cm_response_timeout); 1365 IBA_SET(CM_REQ_PARTITION_KEY, req_msg, 1366 be16_to_cpu(param->primary_path->pkey)); 1367 IBA_SET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg, 1368 param->primary_path->mtu); 1369 IBA_SET(CM_REQ_MAX_CM_RETRIES, req_msg, param->max_cm_retries); 1370 1371 if (param->qp_type != IB_QPT_XRC_INI) { 1372 IBA_SET(CM_REQ_RESPONDER_RESOURCES, req_msg, 1373 param->responder_resources); 1374 IBA_SET(CM_REQ_RETRY_COUNT, req_msg, param->retry_count); 1375 IBA_SET(CM_REQ_RNR_RETRY_COUNT, req_msg, 1376 param->rnr_retry_count); 1377 IBA_SET(CM_REQ_SRQ, req_msg, param->srq); 1378 } 1379 1380 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg) = 1381 pri_path->sgid; 1382 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg) = 1383 pri_path->dgid; 1384 if (pri_ext) { 1385 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg) 1386 ->global.interface_id = 1387 OPA_MAKE_ID(be32_to_cpu(pri_path->opa.slid)); 1388 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg) 1389 ->global.interface_id = 1390 OPA_MAKE_ID(be32_to_cpu(pri_path->opa.dlid)); 1391 } 1392 if (pri_path->hop_limit <= 1) { 1393 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg, 1394 be16_to_cpu(pri_ext ? 0 : 1395 htons(ntohl(sa_path_get_slid( 1396 pri_path))))); 1397 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg, 1398 be16_to_cpu(pri_ext ? 0 : 1399 htons(ntohl(sa_path_get_dlid( 1400 pri_path))))); 1401 } else { 1402 /* Work-around until there's a way to obtain remote LID info */ 1403 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg, 1404 be16_to_cpu(IB_LID_PERMISSIVE)); 1405 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg, 1406 be16_to_cpu(IB_LID_PERMISSIVE)); 1407 } 1408 IBA_SET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg, 1409 be32_to_cpu(pri_path->flow_label)); 1410 IBA_SET(CM_REQ_PRIMARY_PACKET_RATE, req_msg, pri_path->rate); 1411 IBA_SET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg, pri_path->traffic_class); 1412 IBA_SET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg, pri_path->hop_limit); 1413 IBA_SET(CM_REQ_PRIMARY_SL, req_msg, pri_path->sl); 1414 IBA_SET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg, 1415 (pri_path->hop_limit <= 1)); 1416 IBA_SET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg, 1417 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay, 1418 pri_path->packet_life_time)); 1419 1420 if (alt_path) { 1421 bool alt_ext = false; 1422 1423 if (alt_path->rec_type == SA_PATH_REC_TYPE_OPA) 1424 alt_ext = opa_is_extended_lid(alt_path->opa.dlid, 1425 alt_path->opa.slid); 1426 1427 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg) = 1428 alt_path->sgid; 1429 *IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg) = 1430 alt_path->dgid; 1431 if (alt_ext) { 1432 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, 1433 req_msg) 1434 ->global.interface_id = 1435 OPA_MAKE_ID(be32_to_cpu(alt_path->opa.slid)); 1436 IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_REMOTE_PORT_GID, 1437 req_msg) 1438 ->global.interface_id = 1439 OPA_MAKE_ID(be32_to_cpu(alt_path->opa.dlid)); 1440 } 1441 if (alt_path->hop_limit <= 1) { 1442 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg, 1443 be16_to_cpu( 1444 alt_ext ? 0 : 1445 htons(ntohl(sa_path_get_slid( 1446 alt_path))))); 1447 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg, 1448 be16_to_cpu( 1449 alt_ext ? 0 : 1450 htons(ntohl(sa_path_get_dlid( 1451 alt_path))))); 1452 } else { 1453 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg, 1454 be16_to_cpu(IB_LID_PERMISSIVE)); 1455 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg, 1456 be16_to_cpu(IB_LID_PERMISSIVE)); 1457 } 1458 IBA_SET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg, 1459 be32_to_cpu(alt_path->flow_label)); 1460 IBA_SET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg, alt_path->rate); 1461 IBA_SET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg, 1462 alt_path->traffic_class); 1463 IBA_SET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg, 1464 alt_path->hop_limit); 1465 IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, alt_path->sl); 1466 IBA_SET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg, 1467 (alt_path->hop_limit <= 1)); 1468 IBA_SET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg, 1469 cm_ack_timeout(cm_id_priv->av.port->cm_dev->ack_delay, 1470 alt_path->packet_life_time)); 1471 } 1472 IBA_SET(CM_REQ_VENDOR_ID, req_msg, param->ece.vendor_id); 1473 1474 if (param->private_data && param->private_data_len) 1475 IBA_SET_MEM(CM_REQ_PRIVATE_DATA, req_msg, param->private_data, 1476 param->private_data_len); 1477 } 1478 1479 static int cm_validate_req_param(struct ib_cm_req_param *param) 1480 { 1481 if (!param->primary_path) 1482 return -EINVAL; 1483 1484 if (param->qp_type != IB_QPT_RC && param->qp_type != IB_QPT_UC && 1485 param->qp_type != IB_QPT_XRC_INI) 1486 return -EINVAL; 1487 1488 if (param->private_data && 1489 param->private_data_len > IB_CM_REQ_PRIVATE_DATA_SIZE) 1490 return -EINVAL; 1491 1492 if (param->alternate_path && 1493 (param->alternate_path->pkey != param->primary_path->pkey || 1494 param->alternate_path->mtu != param->primary_path->mtu)) 1495 return -EINVAL; 1496 1497 return 0; 1498 } 1499 1500 int ib_send_cm_req(struct ib_cm_id *cm_id, 1501 struct ib_cm_req_param *param) 1502 { 1503 struct cm_id_private *cm_id_priv; 1504 struct cm_req_msg *req_msg; 1505 unsigned long flags; 1506 int ret; 1507 1508 ret = cm_validate_req_param(param); 1509 if (ret) 1510 return ret; 1511 1512 /* Verify that we're not in timewait. */ 1513 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 1514 spin_lock_irqsave(&cm_id_priv->lock, flags); 1515 if (cm_id->state != IB_CM_IDLE || WARN_ON(cm_id_priv->timewait_info)) { 1516 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 1517 ret = -EINVAL; 1518 goto out; 1519 } 1520 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 1521 1522 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv-> 1523 id.local_id); 1524 if (IS_ERR(cm_id_priv->timewait_info)) { 1525 ret = PTR_ERR(cm_id_priv->timewait_info); 1526 cm_id_priv->timewait_info = NULL; 1527 goto out; 1528 } 1529 1530 ret = cm_init_av_by_path(param->primary_path, 1531 param->ppath_sgid_attr, &cm_id_priv->av, 1532 cm_id_priv); 1533 if (ret) 1534 goto out; 1535 if (param->alternate_path) { 1536 ret = cm_init_av_by_path(param->alternate_path, NULL, 1537 &cm_id_priv->alt_av, cm_id_priv); 1538 if (ret) 1539 goto out; 1540 } 1541 cm_id->service_id = param->service_id; 1542 cm_id->service_mask = ~cpu_to_be64(0); 1543 cm_id_priv->timeout_ms = cm_convert_to_ms( 1544 param->primary_path->packet_life_time) * 2 + 1545 cm_convert_to_ms( 1546 param->remote_cm_response_timeout); 1547 cm_id_priv->max_cm_retries = param->max_cm_retries; 1548 cm_id_priv->initiator_depth = param->initiator_depth; 1549 cm_id_priv->responder_resources = param->responder_resources; 1550 cm_id_priv->retry_count = param->retry_count; 1551 cm_id_priv->path_mtu = param->primary_path->mtu; 1552 cm_id_priv->pkey = param->primary_path->pkey; 1553 cm_id_priv->qp_type = param->qp_type; 1554 1555 ret = cm_alloc_msg(cm_id_priv, &cm_id_priv->msg); 1556 if (ret) 1557 goto out; 1558 1559 req_msg = (struct cm_req_msg *) cm_id_priv->msg->mad; 1560 cm_format_req(req_msg, cm_id_priv, param); 1561 cm_id_priv->tid = req_msg->hdr.tid; 1562 cm_id_priv->msg->timeout_ms = cm_id_priv->timeout_ms; 1563 cm_id_priv->msg->context[1] = (void *) (unsigned long) IB_CM_REQ_SENT; 1564 1565 cm_id_priv->local_qpn = cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg)); 1566 cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg)); 1567 1568 trace_icm_send_req(&cm_id_priv->id); 1569 spin_lock_irqsave(&cm_id_priv->lock, flags); 1570 ret = ib_post_send_mad(cm_id_priv->msg, NULL); 1571 if (ret) { 1572 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 1573 goto error2; 1574 } 1575 BUG_ON(cm_id->state != IB_CM_IDLE); 1576 cm_id->state = IB_CM_REQ_SENT; 1577 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 1578 return 0; 1579 1580 error2: cm_free_msg(cm_id_priv->msg); 1581 out: return ret; 1582 } 1583 EXPORT_SYMBOL(ib_send_cm_req); 1584 1585 static int cm_issue_rej(struct cm_port *port, 1586 struct ib_mad_recv_wc *mad_recv_wc, 1587 enum ib_cm_rej_reason reason, 1588 enum cm_msg_response msg_rejected, 1589 void *ari, u8 ari_length) 1590 { 1591 struct ib_mad_send_buf *msg = NULL; 1592 struct cm_rej_msg *rej_msg, *rcv_msg; 1593 int ret; 1594 1595 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg); 1596 if (ret) 1597 return ret; 1598 1599 /* We just need common CM header information. Cast to any message. */ 1600 rcv_msg = (struct cm_rej_msg *) mad_recv_wc->recv_buf.mad; 1601 rej_msg = (struct cm_rej_msg *) msg->mad; 1602 1603 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, rcv_msg->hdr.tid); 1604 IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg, 1605 IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg)); 1606 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, 1607 IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg)); 1608 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, msg_rejected); 1609 IBA_SET(CM_REJ_REASON, rej_msg, reason); 1610 1611 if (ari && ari_length) { 1612 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length); 1613 IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length); 1614 } 1615 1616 trace_icm_issue_rej( 1617 IBA_GET(CM_REJ_LOCAL_COMM_ID, rcv_msg), 1618 IBA_GET(CM_REJ_REMOTE_COMM_ID, rcv_msg)); 1619 ret = ib_post_send_mad(msg, NULL); 1620 if (ret) 1621 cm_free_msg(msg); 1622 1623 return ret; 1624 } 1625 1626 static bool cm_req_has_alt_path(struct cm_req_msg *req_msg) 1627 { 1628 return ((cpu_to_be16( 1629 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg))) || 1630 (ib_is_opa_gid(IBA_GET_MEM_PTR(CM_REQ_ALTERNATE_LOCAL_PORT_GID, 1631 req_msg)))); 1632 } 1633 1634 static void cm_path_set_rec_type(struct ib_device *ib_device, u32 port_num, 1635 struct sa_path_rec *path, union ib_gid *gid) 1636 { 1637 if (ib_is_opa_gid(gid) && rdma_cap_opa_ah(ib_device, port_num)) 1638 path->rec_type = SA_PATH_REC_TYPE_OPA; 1639 else 1640 path->rec_type = SA_PATH_REC_TYPE_IB; 1641 } 1642 1643 static void cm_format_path_lid_from_req(struct cm_req_msg *req_msg, 1644 struct sa_path_rec *primary_path, 1645 struct sa_path_rec *alt_path) 1646 { 1647 u32 lid; 1648 1649 if (primary_path->rec_type != SA_PATH_REC_TYPE_OPA) { 1650 sa_path_set_dlid(primary_path, 1651 IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID, 1652 req_msg)); 1653 sa_path_set_slid(primary_path, 1654 IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID, 1655 req_msg)); 1656 } else { 1657 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR( 1658 CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg)); 1659 sa_path_set_dlid(primary_path, lid); 1660 1661 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR( 1662 CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg)); 1663 sa_path_set_slid(primary_path, lid); 1664 } 1665 1666 if (!cm_req_has_alt_path(req_msg)) 1667 return; 1668 1669 if (alt_path->rec_type != SA_PATH_REC_TYPE_OPA) { 1670 sa_path_set_dlid(alt_path, 1671 IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, 1672 req_msg)); 1673 sa_path_set_slid(alt_path, 1674 IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, 1675 req_msg)); 1676 } else { 1677 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR( 1678 CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg)); 1679 sa_path_set_dlid(alt_path, lid); 1680 1681 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR( 1682 CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg)); 1683 sa_path_set_slid(alt_path, lid); 1684 } 1685 } 1686 1687 static void cm_format_paths_from_req(struct cm_req_msg *req_msg, 1688 struct sa_path_rec *primary_path, 1689 struct sa_path_rec *alt_path) 1690 { 1691 primary_path->dgid = 1692 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, req_msg); 1693 primary_path->sgid = 1694 *IBA_GET_MEM_PTR(CM_REQ_PRIMARY_REMOTE_PORT_GID, req_msg); 1695 primary_path->flow_label = 1696 cpu_to_be32(IBA_GET(CM_REQ_PRIMARY_FLOW_LABEL, req_msg)); 1697 primary_path->hop_limit = IBA_GET(CM_REQ_PRIMARY_HOP_LIMIT, req_msg); 1698 primary_path->traffic_class = 1699 IBA_GET(CM_REQ_PRIMARY_TRAFFIC_CLASS, req_msg); 1700 primary_path->reversible = 1; 1701 primary_path->pkey = 1702 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg)); 1703 primary_path->sl = IBA_GET(CM_REQ_PRIMARY_SL, req_msg); 1704 primary_path->mtu_selector = IB_SA_EQ; 1705 primary_path->mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg); 1706 primary_path->rate_selector = IB_SA_EQ; 1707 primary_path->rate = IBA_GET(CM_REQ_PRIMARY_PACKET_RATE, req_msg); 1708 primary_path->packet_life_time_selector = IB_SA_EQ; 1709 primary_path->packet_life_time = 1710 IBA_GET(CM_REQ_PRIMARY_LOCAL_ACK_TIMEOUT, req_msg); 1711 primary_path->packet_life_time -= (primary_path->packet_life_time > 0); 1712 primary_path->service_id = 1713 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)); 1714 if (sa_path_is_roce(primary_path)) 1715 primary_path->roce.route_resolved = false; 1716 1717 if (cm_req_has_alt_path(req_msg)) { 1718 alt_path->dgid = *IBA_GET_MEM_PTR( 1719 CM_REQ_ALTERNATE_LOCAL_PORT_GID, req_msg); 1720 alt_path->sgid = *IBA_GET_MEM_PTR( 1721 CM_REQ_ALTERNATE_REMOTE_PORT_GID, req_msg); 1722 alt_path->flow_label = cpu_to_be32( 1723 IBA_GET(CM_REQ_ALTERNATE_FLOW_LABEL, req_msg)); 1724 alt_path->hop_limit = 1725 IBA_GET(CM_REQ_ALTERNATE_HOP_LIMIT, req_msg); 1726 alt_path->traffic_class = 1727 IBA_GET(CM_REQ_ALTERNATE_TRAFFIC_CLASS, req_msg); 1728 alt_path->reversible = 1; 1729 alt_path->pkey = 1730 cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg)); 1731 alt_path->sl = IBA_GET(CM_REQ_ALTERNATE_SL, req_msg); 1732 alt_path->mtu_selector = IB_SA_EQ; 1733 alt_path->mtu = 1734 IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg); 1735 alt_path->rate_selector = IB_SA_EQ; 1736 alt_path->rate = IBA_GET(CM_REQ_ALTERNATE_PACKET_RATE, req_msg); 1737 alt_path->packet_life_time_selector = IB_SA_EQ; 1738 alt_path->packet_life_time = 1739 IBA_GET(CM_REQ_ALTERNATE_LOCAL_ACK_TIMEOUT, req_msg); 1740 alt_path->packet_life_time -= (alt_path->packet_life_time > 0); 1741 alt_path->service_id = 1742 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)); 1743 1744 if (sa_path_is_roce(alt_path)) 1745 alt_path->roce.route_resolved = false; 1746 } 1747 cm_format_path_lid_from_req(req_msg, primary_path, alt_path); 1748 } 1749 1750 static u16 cm_get_bth_pkey(struct cm_work *work) 1751 { 1752 struct ib_device *ib_dev = work->port->cm_dev->ib_device; 1753 u32 port_num = work->port->port_num; 1754 u16 pkey_index = work->mad_recv_wc->wc->pkey_index; 1755 u16 pkey; 1756 int ret; 1757 1758 ret = ib_get_cached_pkey(ib_dev, port_num, pkey_index, &pkey); 1759 if (ret) { 1760 dev_warn_ratelimited(&ib_dev->dev, "ib_cm: Couldn't retrieve pkey for incoming request (port %d, pkey index %d). %d\n", 1761 port_num, pkey_index, ret); 1762 return 0; 1763 } 1764 1765 return pkey; 1766 } 1767 1768 /** 1769 * cm_opa_to_ib_sgid - Convert OPA SGID to IB SGID 1770 * ULPs (such as IPoIB) do not understand OPA GIDs and will 1771 * reject them as the local_gid will not match the sgid. Therefore, 1772 * change the pathrec's SGID to an IB SGID. 1773 * 1774 * @work: Work completion 1775 * @path: Path record 1776 */ 1777 static void cm_opa_to_ib_sgid(struct cm_work *work, 1778 struct sa_path_rec *path) 1779 { 1780 struct ib_device *dev = work->port->cm_dev->ib_device; 1781 u32 port_num = work->port->port_num; 1782 1783 if (rdma_cap_opa_ah(dev, port_num) && 1784 (ib_is_opa_gid(&path->sgid))) { 1785 union ib_gid sgid; 1786 1787 if (rdma_query_gid(dev, port_num, 0, &sgid)) { 1788 dev_warn(&dev->dev, 1789 "Error updating sgid in CM request\n"); 1790 return; 1791 } 1792 1793 path->sgid = sgid; 1794 } 1795 } 1796 1797 static void cm_format_req_event(struct cm_work *work, 1798 struct cm_id_private *cm_id_priv, 1799 struct ib_cm_id *listen_id) 1800 { 1801 struct cm_req_msg *req_msg; 1802 struct ib_cm_req_event_param *param; 1803 1804 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; 1805 param = &work->cm_event.param.req_rcvd; 1806 param->listen_id = listen_id; 1807 param->bth_pkey = cm_get_bth_pkey(work); 1808 param->port = cm_id_priv->av.port->port_num; 1809 param->primary_path = &work->path[0]; 1810 cm_opa_to_ib_sgid(work, param->primary_path); 1811 if (cm_req_has_alt_path(req_msg)) { 1812 param->alternate_path = &work->path[1]; 1813 cm_opa_to_ib_sgid(work, param->alternate_path); 1814 } else { 1815 param->alternate_path = NULL; 1816 } 1817 param->remote_ca_guid = 1818 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg)); 1819 param->remote_qkey = IBA_GET(CM_REQ_LOCAL_Q_KEY, req_msg); 1820 param->remote_qpn = IBA_GET(CM_REQ_LOCAL_QPN, req_msg); 1821 param->qp_type = cm_req_get_qp_type(req_msg); 1822 param->starting_psn = IBA_GET(CM_REQ_STARTING_PSN, req_msg); 1823 param->responder_resources = IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg); 1824 param->initiator_depth = IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg); 1825 param->local_cm_response_timeout = 1826 IBA_GET(CM_REQ_REMOTE_CM_RESPONSE_TIMEOUT, req_msg); 1827 param->flow_control = IBA_GET(CM_REQ_END_TO_END_FLOW_CONTROL, req_msg); 1828 param->remote_cm_response_timeout = 1829 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg); 1830 param->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg); 1831 param->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg); 1832 param->srq = IBA_GET(CM_REQ_SRQ, req_msg); 1833 param->ppath_sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr; 1834 param->ece.vendor_id = IBA_GET(CM_REQ_VENDOR_ID, req_msg); 1835 param->ece.attr_mod = be32_to_cpu(req_msg->hdr.attr_mod); 1836 1837 work->cm_event.private_data = 1838 IBA_GET_MEM_PTR(CM_REQ_PRIVATE_DATA, req_msg); 1839 } 1840 1841 static void cm_process_work(struct cm_id_private *cm_id_priv, 1842 struct cm_work *work) 1843 { 1844 int ret; 1845 1846 /* We will typically only have the current event to report. */ 1847 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event); 1848 cm_free_work(work); 1849 1850 while (!ret && !atomic_add_negative(-1, &cm_id_priv->work_count)) { 1851 spin_lock_irq(&cm_id_priv->lock); 1852 work = cm_dequeue_work(cm_id_priv); 1853 spin_unlock_irq(&cm_id_priv->lock); 1854 if (!work) 1855 return; 1856 1857 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, 1858 &work->cm_event); 1859 cm_free_work(work); 1860 } 1861 cm_deref_id(cm_id_priv); 1862 if (ret) 1863 cm_destroy_id(&cm_id_priv->id, ret); 1864 } 1865 1866 static void cm_format_mra(struct cm_mra_msg *mra_msg, 1867 struct cm_id_private *cm_id_priv, 1868 enum cm_msg_response msg_mraed, u8 service_timeout, 1869 const void *private_data, u8 private_data_len) 1870 { 1871 cm_format_mad_hdr(&mra_msg->hdr, CM_MRA_ATTR_ID, cm_id_priv->tid); 1872 IBA_SET(CM_MRA_MESSAGE_MRAED, mra_msg, msg_mraed); 1873 IBA_SET(CM_MRA_LOCAL_COMM_ID, mra_msg, 1874 be32_to_cpu(cm_id_priv->id.local_id)); 1875 IBA_SET(CM_MRA_REMOTE_COMM_ID, mra_msg, 1876 be32_to_cpu(cm_id_priv->id.remote_id)); 1877 IBA_SET(CM_MRA_SERVICE_TIMEOUT, mra_msg, service_timeout); 1878 1879 if (private_data && private_data_len) 1880 IBA_SET_MEM(CM_MRA_PRIVATE_DATA, mra_msg, private_data, 1881 private_data_len); 1882 } 1883 1884 static void cm_format_rej(struct cm_rej_msg *rej_msg, 1885 struct cm_id_private *cm_id_priv, 1886 enum ib_cm_rej_reason reason, void *ari, 1887 u8 ari_length, const void *private_data, 1888 u8 private_data_len, enum ib_cm_state state) 1889 { 1890 lockdep_assert_held(&cm_id_priv->lock); 1891 1892 cm_format_mad_hdr(&rej_msg->hdr, CM_REJ_ATTR_ID, cm_id_priv->tid); 1893 IBA_SET(CM_REJ_REMOTE_COMM_ID, rej_msg, 1894 be32_to_cpu(cm_id_priv->id.remote_id)); 1895 1896 switch (state) { 1897 case IB_CM_REQ_RCVD: 1898 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, be32_to_cpu(0)); 1899 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ); 1900 break; 1901 case IB_CM_MRA_REQ_SENT: 1902 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, 1903 be32_to_cpu(cm_id_priv->id.local_id)); 1904 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REQ); 1905 break; 1906 case IB_CM_REP_RCVD: 1907 case IB_CM_MRA_REP_SENT: 1908 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, 1909 be32_to_cpu(cm_id_priv->id.local_id)); 1910 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, CM_MSG_RESPONSE_REP); 1911 break; 1912 default: 1913 IBA_SET(CM_REJ_LOCAL_COMM_ID, rej_msg, 1914 be32_to_cpu(cm_id_priv->id.local_id)); 1915 IBA_SET(CM_REJ_MESSAGE_REJECTED, rej_msg, 1916 CM_MSG_RESPONSE_OTHER); 1917 break; 1918 } 1919 1920 IBA_SET(CM_REJ_REASON, rej_msg, reason); 1921 if (ari && ari_length) { 1922 IBA_SET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg, ari_length); 1923 IBA_SET_MEM(CM_REJ_ARI, rej_msg, ari, ari_length); 1924 } 1925 1926 if (private_data && private_data_len) 1927 IBA_SET_MEM(CM_REJ_PRIVATE_DATA, rej_msg, private_data, 1928 private_data_len); 1929 } 1930 1931 static void cm_dup_req_handler(struct cm_work *work, 1932 struct cm_id_private *cm_id_priv) 1933 { 1934 struct ib_mad_send_buf *msg = NULL; 1935 int ret; 1936 1937 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 1938 counter[CM_REQ_COUNTER]); 1939 1940 /* Quick state check to discard duplicate REQs. */ 1941 spin_lock_irq(&cm_id_priv->lock); 1942 if (cm_id_priv->id.state == IB_CM_REQ_RCVD) { 1943 spin_unlock_irq(&cm_id_priv->lock); 1944 return; 1945 } 1946 spin_unlock_irq(&cm_id_priv->lock); 1947 1948 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); 1949 if (ret) 1950 return; 1951 1952 spin_lock_irq(&cm_id_priv->lock); 1953 switch (cm_id_priv->id.state) { 1954 case IB_CM_MRA_REQ_SENT: 1955 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, 1956 CM_MSG_RESPONSE_REQ, cm_id_priv->service_timeout, 1957 cm_id_priv->private_data, 1958 cm_id_priv->private_data_len); 1959 break; 1960 case IB_CM_TIMEWAIT: 1961 cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, 1962 IB_CM_REJ_STALE_CONN, NULL, 0, NULL, 0, 1963 IB_CM_TIMEWAIT); 1964 break; 1965 default: 1966 goto unlock; 1967 } 1968 spin_unlock_irq(&cm_id_priv->lock); 1969 1970 trace_icm_send_dup_req(&cm_id_priv->id); 1971 ret = ib_post_send_mad(msg, NULL); 1972 if (ret) 1973 goto free; 1974 return; 1975 1976 unlock: spin_unlock_irq(&cm_id_priv->lock); 1977 free: cm_free_msg(msg); 1978 } 1979 1980 static struct cm_id_private *cm_match_req(struct cm_work *work, 1981 struct cm_id_private *cm_id_priv) 1982 { 1983 struct cm_id_private *listen_cm_id_priv, *cur_cm_id_priv; 1984 struct cm_timewait_info *timewait_info; 1985 struct cm_req_msg *req_msg; 1986 1987 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; 1988 1989 /* Check for possible duplicate REQ. */ 1990 spin_lock_irq(&cm.lock); 1991 timewait_info = cm_insert_remote_id(cm_id_priv->timewait_info); 1992 if (timewait_info) { 1993 cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id, 1994 timewait_info->work.remote_id); 1995 spin_unlock_irq(&cm.lock); 1996 if (cur_cm_id_priv) { 1997 cm_dup_req_handler(work, cur_cm_id_priv); 1998 cm_deref_id(cur_cm_id_priv); 1999 } 2000 return NULL; 2001 } 2002 2003 /* Check for stale connections. */ 2004 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info); 2005 if (timewait_info) { 2006 cm_remove_remote(cm_id_priv); 2007 cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id, 2008 timewait_info->work.remote_id); 2009 2010 spin_unlock_irq(&cm.lock); 2011 cm_issue_rej(work->port, work->mad_recv_wc, 2012 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REQ, 2013 NULL, 0); 2014 if (cur_cm_id_priv) { 2015 ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0); 2016 cm_deref_id(cur_cm_id_priv); 2017 } 2018 return NULL; 2019 } 2020 2021 /* Find matching listen request. */ 2022 listen_cm_id_priv = cm_find_listen( 2023 cm_id_priv->id.device, 2024 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg))); 2025 if (!listen_cm_id_priv) { 2026 cm_remove_remote(cm_id_priv); 2027 spin_unlock_irq(&cm.lock); 2028 cm_issue_rej(work->port, work->mad_recv_wc, 2029 IB_CM_REJ_INVALID_SERVICE_ID, CM_MSG_RESPONSE_REQ, 2030 NULL, 0); 2031 return NULL; 2032 } 2033 spin_unlock_irq(&cm.lock); 2034 return listen_cm_id_priv; 2035 } 2036 2037 /* 2038 * Work-around for inter-subnet connections. If the LIDs are permissive, 2039 * we need to override the LID/SL data in the REQ with the LID information 2040 * in the work completion. 2041 */ 2042 static void cm_process_routed_req(struct cm_req_msg *req_msg, struct ib_wc *wc) 2043 { 2044 if (!IBA_GET(CM_REQ_PRIMARY_SUBNET_LOCAL, req_msg)) { 2045 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_LOCAL_PORT_LID, 2046 req_msg)) == IB_LID_PERMISSIVE) { 2047 IBA_SET(CM_REQ_PRIMARY_LOCAL_PORT_LID, req_msg, 2048 be16_to_cpu(ib_lid_be16(wc->slid))); 2049 IBA_SET(CM_REQ_PRIMARY_SL, req_msg, wc->sl); 2050 } 2051 2052 if (cpu_to_be16(IBA_GET(CM_REQ_PRIMARY_REMOTE_PORT_LID, 2053 req_msg)) == IB_LID_PERMISSIVE) 2054 IBA_SET(CM_REQ_PRIMARY_REMOTE_PORT_LID, req_msg, 2055 wc->dlid_path_bits); 2056 } 2057 2058 if (!IBA_GET(CM_REQ_ALTERNATE_SUBNET_LOCAL, req_msg)) { 2059 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, 2060 req_msg)) == IB_LID_PERMISSIVE) { 2061 IBA_SET(CM_REQ_ALTERNATE_LOCAL_PORT_LID, req_msg, 2062 be16_to_cpu(ib_lid_be16(wc->slid))); 2063 IBA_SET(CM_REQ_ALTERNATE_SL, req_msg, wc->sl); 2064 } 2065 2066 if (cpu_to_be16(IBA_GET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, 2067 req_msg)) == IB_LID_PERMISSIVE) 2068 IBA_SET(CM_REQ_ALTERNATE_REMOTE_PORT_LID, req_msg, 2069 wc->dlid_path_bits); 2070 } 2071 } 2072 2073 static int cm_req_handler(struct cm_work *work) 2074 { 2075 struct cm_id_private *cm_id_priv, *listen_cm_id_priv; 2076 struct cm_req_msg *req_msg; 2077 const struct ib_global_route *grh; 2078 const struct ib_gid_attr *gid_attr; 2079 int ret; 2080 2081 req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad; 2082 2083 cm_id_priv = 2084 cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL); 2085 if (IS_ERR(cm_id_priv)) 2086 return PTR_ERR(cm_id_priv); 2087 2088 cm_id_priv->id.remote_id = 2089 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_COMM_ID, req_msg)); 2090 cm_id_priv->id.service_id = 2091 cpu_to_be64(IBA_GET(CM_REQ_SERVICE_ID, req_msg)); 2092 cm_id_priv->id.service_mask = ~cpu_to_be64(0); 2093 cm_id_priv->tid = req_msg->hdr.tid; 2094 cm_id_priv->timeout_ms = cm_convert_to_ms( 2095 IBA_GET(CM_REQ_LOCAL_CM_RESPONSE_TIMEOUT, req_msg)); 2096 cm_id_priv->max_cm_retries = IBA_GET(CM_REQ_MAX_CM_RETRIES, req_msg); 2097 cm_id_priv->remote_qpn = 2098 cpu_to_be32(IBA_GET(CM_REQ_LOCAL_QPN, req_msg)); 2099 cm_id_priv->initiator_depth = 2100 IBA_GET(CM_REQ_RESPONDER_RESOURCES, req_msg); 2101 cm_id_priv->responder_resources = 2102 IBA_GET(CM_REQ_INITIATOR_DEPTH, req_msg); 2103 cm_id_priv->path_mtu = IBA_GET(CM_REQ_PATH_PACKET_PAYLOAD_MTU, req_msg); 2104 cm_id_priv->pkey = cpu_to_be16(IBA_GET(CM_REQ_PARTITION_KEY, req_msg)); 2105 cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REQ_STARTING_PSN, req_msg)); 2106 cm_id_priv->retry_count = IBA_GET(CM_REQ_RETRY_COUNT, req_msg); 2107 cm_id_priv->rnr_retry_count = IBA_GET(CM_REQ_RNR_RETRY_COUNT, req_msg); 2108 cm_id_priv->qp_type = cm_req_get_qp_type(req_msg); 2109 2110 ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc, 2111 work->mad_recv_wc->recv_buf.grh, 2112 &cm_id_priv->av); 2113 if (ret) 2114 goto destroy; 2115 cm_id_priv->timewait_info = cm_create_timewait_info(cm_id_priv-> 2116 id.local_id); 2117 if (IS_ERR(cm_id_priv->timewait_info)) { 2118 ret = PTR_ERR(cm_id_priv->timewait_info); 2119 cm_id_priv->timewait_info = NULL; 2120 goto destroy; 2121 } 2122 cm_id_priv->timewait_info->work.remote_id = cm_id_priv->id.remote_id; 2123 cm_id_priv->timewait_info->remote_ca_guid = 2124 cpu_to_be64(IBA_GET(CM_REQ_LOCAL_CA_GUID, req_msg)); 2125 cm_id_priv->timewait_info->remote_qpn = cm_id_priv->remote_qpn; 2126 2127 /* 2128 * Note that the ID pointer is not in the xarray at this point, 2129 * so this set is only visible to the local thread. 2130 */ 2131 cm_id_priv->id.state = IB_CM_REQ_RCVD; 2132 2133 listen_cm_id_priv = cm_match_req(work, cm_id_priv); 2134 if (!listen_cm_id_priv) { 2135 trace_icm_no_listener_err(&cm_id_priv->id); 2136 cm_id_priv->id.state = IB_CM_IDLE; 2137 ret = -EINVAL; 2138 goto destroy; 2139 } 2140 2141 memset(&work->path[0], 0, sizeof(work->path[0])); 2142 if (cm_req_has_alt_path(req_msg)) 2143 memset(&work->path[1], 0, sizeof(work->path[1])); 2144 grh = rdma_ah_read_grh(&cm_id_priv->av.ah_attr); 2145 gid_attr = grh->sgid_attr; 2146 2147 if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE) { 2148 work->path[0].rec_type = 2149 sa_conv_gid_to_pathrec_type(gid_attr->gid_type); 2150 } else { 2151 cm_process_routed_req(req_msg, work->mad_recv_wc->wc); 2152 cm_path_set_rec_type( 2153 work->port->cm_dev->ib_device, work->port->port_num, 2154 &work->path[0], 2155 IBA_GET_MEM_PTR(CM_REQ_PRIMARY_LOCAL_PORT_GID, 2156 req_msg)); 2157 } 2158 if (cm_req_has_alt_path(req_msg)) 2159 work->path[1].rec_type = work->path[0].rec_type; 2160 cm_format_paths_from_req(req_msg, &work->path[0], 2161 &work->path[1]); 2162 if (cm_id_priv->av.ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE) 2163 sa_path_set_dmac(&work->path[0], 2164 cm_id_priv->av.ah_attr.roce.dmac); 2165 work->path[0].hop_limit = grh->hop_limit; 2166 ret = cm_init_av_by_path(&work->path[0], gid_attr, &cm_id_priv->av, 2167 cm_id_priv); 2168 if (ret) { 2169 int err; 2170 2171 err = rdma_query_gid(work->port->cm_dev->ib_device, 2172 work->port->port_num, 0, 2173 &work->path[0].sgid); 2174 if (err) 2175 ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID, 2176 NULL, 0, NULL, 0); 2177 else 2178 ib_send_cm_rej(&cm_id_priv->id, IB_CM_REJ_INVALID_GID, 2179 &work->path[0].sgid, 2180 sizeof(work->path[0].sgid), 2181 NULL, 0); 2182 goto rejected; 2183 } 2184 if (cm_req_has_alt_path(req_msg)) { 2185 ret = cm_init_av_by_path(&work->path[1], NULL, 2186 &cm_id_priv->alt_av, cm_id_priv); 2187 if (ret) { 2188 ib_send_cm_rej(&cm_id_priv->id, 2189 IB_CM_REJ_INVALID_ALT_GID, 2190 &work->path[0].sgid, 2191 sizeof(work->path[0].sgid), NULL, 0); 2192 goto rejected; 2193 } 2194 } 2195 2196 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler; 2197 cm_id_priv->id.context = listen_cm_id_priv->id.context; 2198 cm_format_req_event(work, cm_id_priv, &listen_cm_id_priv->id); 2199 2200 /* Now MAD handlers can see the new ID */ 2201 spin_lock_irq(&cm_id_priv->lock); 2202 cm_finalize_id(cm_id_priv); 2203 2204 /* Refcount belongs to the event, pairs with cm_process_work() */ 2205 refcount_inc(&cm_id_priv->refcount); 2206 cm_queue_work_unlock(cm_id_priv, work); 2207 /* 2208 * Since this ID was just created and was not made visible to other MAD 2209 * handlers until the cm_finalize_id() above we know that the 2210 * cm_process_work() will deliver the event and the listen_cm_id 2211 * embedded in the event can be derefed here. 2212 */ 2213 cm_deref_id(listen_cm_id_priv); 2214 return 0; 2215 2216 rejected: 2217 cm_deref_id(listen_cm_id_priv); 2218 destroy: 2219 ib_destroy_cm_id(&cm_id_priv->id); 2220 return ret; 2221 } 2222 2223 static void cm_format_rep(struct cm_rep_msg *rep_msg, 2224 struct cm_id_private *cm_id_priv, 2225 struct ib_cm_rep_param *param) 2226 { 2227 cm_format_mad_ece_hdr(&rep_msg->hdr, CM_REP_ATTR_ID, cm_id_priv->tid, 2228 param->ece.attr_mod); 2229 IBA_SET(CM_REP_LOCAL_COMM_ID, rep_msg, 2230 be32_to_cpu(cm_id_priv->id.local_id)); 2231 IBA_SET(CM_REP_REMOTE_COMM_ID, rep_msg, 2232 be32_to_cpu(cm_id_priv->id.remote_id)); 2233 IBA_SET(CM_REP_STARTING_PSN, rep_msg, param->starting_psn); 2234 IBA_SET(CM_REP_RESPONDER_RESOURCES, rep_msg, 2235 param->responder_resources); 2236 IBA_SET(CM_REP_TARGET_ACK_DELAY, rep_msg, 2237 cm_id_priv->av.port->cm_dev->ack_delay); 2238 IBA_SET(CM_REP_FAILOVER_ACCEPTED, rep_msg, param->failover_accepted); 2239 IBA_SET(CM_REP_RNR_RETRY_COUNT, rep_msg, param->rnr_retry_count); 2240 IBA_SET(CM_REP_LOCAL_CA_GUID, rep_msg, 2241 be64_to_cpu(cm_id_priv->id.device->node_guid)); 2242 2243 if (cm_id_priv->qp_type != IB_QPT_XRC_TGT) { 2244 IBA_SET(CM_REP_INITIATOR_DEPTH, rep_msg, 2245 param->initiator_depth); 2246 IBA_SET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg, 2247 param->flow_control); 2248 IBA_SET(CM_REP_SRQ, rep_msg, param->srq); 2249 IBA_SET(CM_REP_LOCAL_QPN, rep_msg, param->qp_num); 2250 } else { 2251 IBA_SET(CM_REP_SRQ, rep_msg, 1); 2252 IBA_SET(CM_REP_LOCAL_EE_CONTEXT_NUMBER, rep_msg, param->qp_num); 2253 } 2254 2255 IBA_SET(CM_REP_VENDOR_ID_L, rep_msg, param->ece.vendor_id); 2256 IBA_SET(CM_REP_VENDOR_ID_M, rep_msg, param->ece.vendor_id >> 8); 2257 IBA_SET(CM_REP_VENDOR_ID_H, rep_msg, param->ece.vendor_id >> 16); 2258 2259 if (param->private_data && param->private_data_len) 2260 IBA_SET_MEM(CM_REP_PRIVATE_DATA, rep_msg, param->private_data, 2261 param->private_data_len); 2262 } 2263 2264 int ib_send_cm_rep(struct ib_cm_id *cm_id, 2265 struct ib_cm_rep_param *param) 2266 { 2267 struct cm_id_private *cm_id_priv; 2268 struct ib_mad_send_buf *msg; 2269 struct cm_rep_msg *rep_msg; 2270 unsigned long flags; 2271 int ret; 2272 2273 if (param->private_data && 2274 param->private_data_len > IB_CM_REP_PRIVATE_DATA_SIZE) 2275 return -EINVAL; 2276 2277 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 2278 spin_lock_irqsave(&cm_id_priv->lock, flags); 2279 if (cm_id->state != IB_CM_REQ_RCVD && 2280 cm_id->state != IB_CM_MRA_REQ_SENT) { 2281 trace_icm_send_rep_err(cm_id_priv->id.local_id, cm_id->state); 2282 ret = -EINVAL; 2283 goto out; 2284 } 2285 2286 ret = cm_alloc_msg(cm_id_priv, &msg); 2287 if (ret) 2288 goto out; 2289 2290 rep_msg = (struct cm_rep_msg *) msg->mad; 2291 cm_format_rep(rep_msg, cm_id_priv, param); 2292 msg->timeout_ms = cm_id_priv->timeout_ms; 2293 msg->context[1] = (void *) (unsigned long) IB_CM_REP_SENT; 2294 2295 trace_icm_send_rep(cm_id); 2296 ret = ib_post_send_mad(msg, NULL); 2297 if (ret) { 2298 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2299 cm_free_msg(msg); 2300 return ret; 2301 } 2302 2303 cm_id->state = IB_CM_REP_SENT; 2304 cm_id_priv->msg = msg; 2305 cm_id_priv->initiator_depth = param->initiator_depth; 2306 cm_id_priv->responder_resources = param->responder_resources; 2307 cm_id_priv->rq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg)); 2308 WARN_ONCE(param->qp_num & 0xFF000000, 2309 "IBTA declares QPN to be 24 bits, but it is 0x%X\n", 2310 param->qp_num); 2311 cm_id_priv->local_qpn = cpu_to_be32(param->qp_num & 0xFFFFFF); 2312 2313 out: spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2314 return ret; 2315 } 2316 EXPORT_SYMBOL(ib_send_cm_rep); 2317 2318 static void cm_format_rtu(struct cm_rtu_msg *rtu_msg, 2319 struct cm_id_private *cm_id_priv, 2320 const void *private_data, 2321 u8 private_data_len) 2322 { 2323 cm_format_mad_hdr(&rtu_msg->hdr, CM_RTU_ATTR_ID, cm_id_priv->tid); 2324 IBA_SET(CM_RTU_LOCAL_COMM_ID, rtu_msg, 2325 be32_to_cpu(cm_id_priv->id.local_id)); 2326 IBA_SET(CM_RTU_REMOTE_COMM_ID, rtu_msg, 2327 be32_to_cpu(cm_id_priv->id.remote_id)); 2328 2329 if (private_data && private_data_len) 2330 IBA_SET_MEM(CM_RTU_PRIVATE_DATA, rtu_msg, private_data, 2331 private_data_len); 2332 } 2333 2334 int ib_send_cm_rtu(struct ib_cm_id *cm_id, 2335 const void *private_data, 2336 u8 private_data_len) 2337 { 2338 struct cm_id_private *cm_id_priv; 2339 struct ib_mad_send_buf *msg; 2340 unsigned long flags; 2341 void *data; 2342 int ret; 2343 2344 if (private_data && private_data_len > IB_CM_RTU_PRIVATE_DATA_SIZE) 2345 return -EINVAL; 2346 2347 data = cm_copy_private_data(private_data, private_data_len); 2348 if (IS_ERR(data)) 2349 return PTR_ERR(data); 2350 2351 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 2352 spin_lock_irqsave(&cm_id_priv->lock, flags); 2353 if (cm_id->state != IB_CM_REP_RCVD && 2354 cm_id->state != IB_CM_MRA_REP_SENT) { 2355 trace_icm_send_cm_rtu_err(cm_id); 2356 ret = -EINVAL; 2357 goto error; 2358 } 2359 2360 ret = cm_alloc_msg(cm_id_priv, &msg); 2361 if (ret) 2362 goto error; 2363 2364 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv, 2365 private_data, private_data_len); 2366 2367 trace_icm_send_rtu(cm_id); 2368 ret = ib_post_send_mad(msg, NULL); 2369 if (ret) { 2370 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2371 cm_free_msg(msg); 2372 kfree(data); 2373 return ret; 2374 } 2375 2376 cm_id->state = IB_CM_ESTABLISHED; 2377 cm_set_private_data(cm_id_priv, data, private_data_len); 2378 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2379 return 0; 2380 2381 error: spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2382 kfree(data); 2383 return ret; 2384 } 2385 EXPORT_SYMBOL(ib_send_cm_rtu); 2386 2387 static void cm_format_rep_event(struct cm_work *work, enum ib_qp_type qp_type) 2388 { 2389 struct cm_rep_msg *rep_msg; 2390 struct ib_cm_rep_event_param *param; 2391 2392 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad; 2393 param = &work->cm_event.param.rep_rcvd; 2394 param->remote_ca_guid = 2395 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg)); 2396 param->remote_qkey = IBA_GET(CM_REP_LOCAL_Q_KEY, rep_msg); 2397 param->remote_qpn = be32_to_cpu(cm_rep_get_qpn(rep_msg, qp_type)); 2398 param->starting_psn = IBA_GET(CM_REP_STARTING_PSN, rep_msg); 2399 param->responder_resources = IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg); 2400 param->initiator_depth = IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg); 2401 param->target_ack_delay = IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg); 2402 param->failover_accepted = IBA_GET(CM_REP_FAILOVER_ACCEPTED, rep_msg); 2403 param->flow_control = IBA_GET(CM_REP_END_TO_END_FLOW_CONTROL, rep_msg); 2404 param->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg); 2405 param->srq = IBA_GET(CM_REP_SRQ, rep_msg); 2406 param->ece.vendor_id = IBA_GET(CM_REP_VENDOR_ID_H, rep_msg) << 16; 2407 param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_M, rep_msg) << 8; 2408 param->ece.vendor_id |= IBA_GET(CM_REP_VENDOR_ID_L, rep_msg); 2409 param->ece.attr_mod = be32_to_cpu(rep_msg->hdr.attr_mod); 2410 2411 work->cm_event.private_data = 2412 IBA_GET_MEM_PTR(CM_REP_PRIVATE_DATA, rep_msg); 2413 } 2414 2415 static void cm_dup_rep_handler(struct cm_work *work) 2416 { 2417 struct cm_id_private *cm_id_priv; 2418 struct cm_rep_msg *rep_msg; 2419 struct ib_mad_send_buf *msg = NULL; 2420 int ret; 2421 2422 rep_msg = (struct cm_rep_msg *) work->mad_recv_wc->recv_buf.mad; 2423 cm_id_priv = cm_acquire_id( 2424 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)), 2425 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg))); 2426 if (!cm_id_priv) 2427 return; 2428 2429 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 2430 counter[CM_REP_COUNTER]); 2431 ret = cm_alloc_response_msg(work->port, work->mad_recv_wc, &msg); 2432 if (ret) 2433 goto deref; 2434 2435 spin_lock_irq(&cm_id_priv->lock); 2436 if (cm_id_priv->id.state == IB_CM_ESTABLISHED) 2437 cm_format_rtu((struct cm_rtu_msg *) msg->mad, cm_id_priv, 2438 cm_id_priv->private_data, 2439 cm_id_priv->private_data_len); 2440 else if (cm_id_priv->id.state == IB_CM_MRA_REP_SENT) 2441 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, 2442 CM_MSG_RESPONSE_REP, cm_id_priv->service_timeout, 2443 cm_id_priv->private_data, 2444 cm_id_priv->private_data_len); 2445 else 2446 goto unlock; 2447 spin_unlock_irq(&cm_id_priv->lock); 2448 2449 trace_icm_send_dup_rep(&cm_id_priv->id); 2450 ret = ib_post_send_mad(msg, NULL); 2451 if (ret) 2452 goto free; 2453 goto deref; 2454 2455 unlock: spin_unlock_irq(&cm_id_priv->lock); 2456 free: cm_free_msg(msg); 2457 deref: cm_deref_id(cm_id_priv); 2458 } 2459 2460 static int cm_rep_handler(struct cm_work *work) 2461 { 2462 struct cm_id_private *cm_id_priv; 2463 struct cm_rep_msg *rep_msg; 2464 int ret; 2465 struct cm_id_private *cur_cm_id_priv; 2466 struct cm_timewait_info *timewait_info; 2467 2468 rep_msg = (struct cm_rep_msg *)work->mad_recv_wc->recv_buf.mad; 2469 cm_id_priv = cm_acquire_id( 2470 cpu_to_be32(IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)), 0); 2471 if (!cm_id_priv) { 2472 cm_dup_rep_handler(work); 2473 trace_icm_remote_no_priv_err( 2474 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)); 2475 return -EINVAL; 2476 } 2477 2478 cm_format_rep_event(work, cm_id_priv->qp_type); 2479 2480 spin_lock_irq(&cm_id_priv->lock); 2481 switch (cm_id_priv->id.state) { 2482 case IB_CM_REQ_SENT: 2483 case IB_CM_MRA_REQ_RCVD: 2484 break; 2485 default: 2486 ret = -EINVAL; 2487 trace_icm_rep_unknown_err( 2488 IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg), 2489 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg), 2490 cm_id_priv->id.state); 2491 spin_unlock_irq(&cm_id_priv->lock); 2492 goto error; 2493 } 2494 2495 cm_id_priv->timewait_info->work.remote_id = 2496 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg)); 2497 cm_id_priv->timewait_info->remote_ca_guid = 2498 cpu_to_be64(IBA_GET(CM_REP_LOCAL_CA_GUID, rep_msg)); 2499 cm_id_priv->timewait_info->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type); 2500 2501 spin_lock(&cm.lock); 2502 /* Check for duplicate REP. */ 2503 if (cm_insert_remote_id(cm_id_priv->timewait_info)) { 2504 spin_unlock(&cm.lock); 2505 spin_unlock_irq(&cm_id_priv->lock); 2506 ret = -EINVAL; 2507 trace_icm_insert_failed_err( 2508 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)); 2509 goto error; 2510 } 2511 /* Check for a stale connection. */ 2512 timewait_info = cm_insert_remote_qpn(cm_id_priv->timewait_info); 2513 if (timewait_info) { 2514 cm_remove_remote(cm_id_priv); 2515 cur_cm_id_priv = cm_acquire_id(timewait_info->work.local_id, 2516 timewait_info->work.remote_id); 2517 2518 spin_unlock(&cm.lock); 2519 spin_unlock_irq(&cm_id_priv->lock); 2520 cm_issue_rej(work->port, work->mad_recv_wc, 2521 IB_CM_REJ_STALE_CONN, CM_MSG_RESPONSE_REP, 2522 NULL, 0); 2523 ret = -EINVAL; 2524 trace_icm_staleconn_err( 2525 IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg), 2526 IBA_GET(CM_REP_REMOTE_COMM_ID, rep_msg)); 2527 2528 if (cur_cm_id_priv) { 2529 ib_send_cm_dreq(&cur_cm_id_priv->id, NULL, 0); 2530 cm_deref_id(cur_cm_id_priv); 2531 } 2532 2533 goto error; 2534 } 2535 spin_unlock(&cm.lock); 2536 2537 cm_id_priv->id.state = IB_CM_REP_RCVD; 2538 cm_id_priv->id.remote_id = 2539 cpu_to_be32(IBA_GET(CM_REP_LOCAL_COMM_ID, rep_msg)); 2540 cm_id_priv->remote_qpn = cm_rep_get_qpn(rep_msg, cm_id_priv->qp_type); 2541 cm_id_priv->initiator_depth = 2542 IBA_GET(CM_REP_RESPONDER_RESOURCES, rep_msg); 2543 cm_id_priv->responder_resources = 2544 IBA_GET(CM_REP_INITIATOR_DEPTH, rep_msg); 2545 cm_id_priv->sq_psn = cpu_to_be32(IBA_GET(CM_REP_STARTING_PSN, rep_msg)); 2546 cm_id_priv->rnr_retry_count = IBA_GET(CM_REP_RNR_RETRY_COUNT, rep_msg); 2547 cm_id_priv->target_ack_delay = 2548 IBA_GET(CM_REP_TARGET_ACK_DELAY, rep_msg); 2549 cm_id_priv->av.timeout = 2550 cm_ack_timeout(cm_id_priv->target_ack_delay, 2551 cm_id_priv->av.timeout - 1); 2552 cm_id_priv->alt_av.timeout = 2553 cm_ack_timeout(cm_id_priv->target_ack_delay, 2554 cm_id_priv->alt_av.timeout - 1); 2555 2556 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2557 cm_queue_work_unlock(cm_id_priv, work); 2558 return 0; 2559 2560 error: 2561 cm_deref_id(cm_id_priv); 2562 return ret; 2563 } 2564 2565 static int cm_establish_handler(struct cm_work *work) 2566 { 2567 struct cm_id_private *cm_id_priv; 2568 2569 /* See comment in cm_establish about lookup. */ 2570 cm_id_priv = cm_acquire_id(work->local_id, work->remote_id); 2571 if (!cm_id_priv) 2572 return -EINVAL; 2573 2574 spin_lock_irq(&cm_id_priv->lock); 2575 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) { 2576 spin_unlock_irq(&cm_id_priv->lock); 2577 goto out; 2578 } 2579 2580 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2581 cm_queue_work_unlock(cm_id_priv, work); 2582 return 0; 2583 out: 2584 cm_deref_id(cm_id_priv); 2585 return -EINVAL; 2586 } 2587 2588 static int cm_rtu_handler(struct cm_work *work) 2589 { 2590 struct cm_id_private *cm_id_priv; 2591 struct cm_rtu_msg *rtu_msg; 2592 2593 rtu_msg = (struct cm_rtu_msg *)work->mad_recv_wc->recv_buf.mad; 2594 cm_id_priv = cm_acquire_id( 2595 cpu_to_be32(IBA_GET(CM_RTU_REMOTE_COMM_ID, rtu_msg)), 2596 cpu_to_be32(IBA_GET(CM_RTU_LOCAL_COMM_ID, rtu_msg))); 2597 if (!cm_id_priv) 2598 return -EINVAL; 2599 2600 work->cm_event.private_data = 2601 IBA_GET_MEM_PTR(CM_RTU_PRIVATE_DATA, rtu_msg); 2602 2603 spin_lock_irq(&cm_id_priv->lock); 2604 if (cm_id_priv->id.state != IB_CM_REP_SENT && 2605 cm_id_priv->id.state != IB_CM_MRA_REP_RCVD) { 2606 spin_unlock_irq(&cm_id_priv->lock); 2607 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 2608 counter[CM_RTU_COUNTER]); 2609 goto out; 2610 } 2611 cm_id_priv->id.state = IB_CM_ESTABLISHED; 2612 2613 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2614 cm_queue_work_unlock(cm_id_priv, work); 2615 return 0; 2616 out: 2617 cm_deref_id(cm_id_priv); 2618 return -EINVAL; 2619 } 2620 2621 static void cm_format_dreq(struct cm_dreq_msg *dreq_msg, 2622 struct cm_id_private *cm_id_priv, 2623 const void *private_data, 2624 u8 private_data_len) 2625 { 2626 cm_format_mad_hdr(&dreq_msg->hdr, CM_DREQ_ATTR_ID, 2627 cm_form_tid(cm_id_priv)); 2628 IBA_SET(CM_DREQ_LOCAL_COMM_ID, dreq_msg, 2629 be32_to_cpu(cm_id_priv->id.local_id)); 2630 IBA_SET(CM_DREQ_REMOTE_COMM_ID, dreq_msg, 2631 be32_to_cpu(cm_id_priv->id.remote_id)); 2632 IBA_SET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg, 2633 be32_to_cpu(cm_id_priv->remote_qpn)); 2634 2635 if (private_data && private_data_len) 2636 IBA_SET_MEM(CM_DREQ_PRIVATE_DATA, dreq_msg, private_data, 2637 private_data_len); 2638 } 2639 2640 static int cm_send_dreq_locked(struct cm_id_private *cm_id_priv, 2641 const void *private_data, u8 private_data_len) 2642 { 2643 struct ib_mad_send_buf *msg; 2644 int ret; 2645 2646 lockdep_assert_held(&cm_id_priv->lock); 2647 2648 if (private_data && private_data_len > IB_CM_DREQ_PRIVATE_DATA_SIZE) 2649 return -EINVAL; 2650 2651 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) { 2652 trace_icm_dreq_skipped(&cm_id_priv->id); 2653 return -EINVAL; 2654 } 2655 2656 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT || 2657 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD) 2658 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2659 2660 ret = cm_alloc_msg(cm_id_priv, &msg); 2661 if (ret) { 2662 cm_enter_timewait(cm_id_priv); 2663 return ret; 2664 } 2665 2666 cm_format_dreq((struct cm_dreq_msg *) msg->mad, cm_id_priv, 2667 private_data, private_data_len); 2668 msg->timeout_ms = cm_id_priv->timeout_ms; 2669 msg->context[1] = (void *) (unsigned long) IB_CM_DREQ_SENT; 2670 2671 trace_icm_send_dreq(&cm_id_priv->id); 2672 ret = ib_post_send_mad(msg, NULL); 2673 if (ret) { 2674 cm_enter_timewait(cm_id_priv); 2675 cm_free_msg(msg); 2676 return ret; 2677 } 2678 2679 cm_id_priv->id.state = IB_CM_DREQ_SENT; 2680 cm_id_priv->msg = msg; 2681 return 0; 2682 } 2683 2684 int ib_send_cm_dreq(struct ib_cm_id *cm_id, const void *private_data, 2685 u8 private_data_len) 2686 { 2687 struct cm_id_private *cm_id_priv = 2688 container_of(cm_id, struct cm_id_private, id); 2689 unsigned long flags; 2690 int ret; 2691 2692 spin_lock_irqsave(&cm_id_priv->lock, flags); 2693 ret = cm_send_dreq_locked(cm_id_priv, private_data, private_data_len); 2694 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2695 return ret; 2696 } 2697 EXPORT_SYMBOL(ib_send_cm_dreq); 2698 2699 static void cm_format_drep(struct cm_drep_msg *drep_msg, 2700 struct cm_id_private *cm_id_priv, 2701 const void *private_data, 2702 u8 private_data_len) 2703 { 2704 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, cm_id_priv->tid); 2705 IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg, 2706 be32_to_cpu(cm_id_priv->id.local_id)); 2707 IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg, 2708 be32_to_cpu(cm_id_priv->id.remote_id)); 2709 2710 if (private_data && private_data_len) 2711 IBA_SET_MEM(CM_DREP_PRIVATE_DATA, drep_msg, private_data, 2712 private_data_len); 2713 } 2714 2715 static int cm_send_drep_locked(struct cm_id_private *cm_id_priv, 2716 void *private_data, u8 private_data_len) 2717 { 2718 struct ib_mad_send_buf *msg; 2719 int ret; 2720 2721 lockdep_assert_held(&cm_id_priv->lock); 2722 2723 if (private_data && private_data_len > IB_CM_DREP_PRIVATE_DATA_SIZE) 2724 return -EINVAL; 2725 2726 if (cm_id_priv->id.state != IB_CM_DREQ_RCVD) { 2727 trace_icm_send_drep_err(&cm_id_priv->id); 2728 kfree(private_data); 2729 return -EINVAL; 2730 } 2731 2732 cm_set_private_data(cm_id_priv, private_data, private_data_len); 2733 cm_enter_timewait(cm_id_priv); 2734 2735 ret = cm_alloc_msg(cm_id_priv, &msg); 2736 if (ret) 2737 return ret; 2738 2739 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv, 2740 private_data, private_data_len); 2741 2742 trace_icm_send_drep(&cm_id_priv->id); 2743 ret = ib_post_send_mad(msg, NULL); 2744 if (ret) { 2745 cm_free_msg(msg); 2746 return ret; 2747 } 2748 return 0; 2749 } 2750 2751 int ib_send_cm_drep(struct ib_cm_id *cm_id, const void *private_data, 2752 u8 private_data_len) 2753 { 2754 struct cm_id_private *cm_id_priv = 2755 container_of(cm_id, struct cm_id_private, id); 2756 unsigned long flags; 2757 void *data; 2758 int ret; 2759 2760 data = cm_copy_private_data(private_data, private_data_len); 2761 if (IS_ERR(data)) 2762 return PTR_ERR(data); 2763 2764 spin_lock_irqsave(&cm_id_priv->lock, flags); 2765 ret = cm_send_drep_locked(cm_id_priv, data, private_data_len); 2766 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2767 return ret; 2768 } 2769 EXPORT_SYMBOL(ib_send_cm_drep); 2770 2771 static int cm_issue_drep(struct cm_port *port, 2772 struct ib_mad_recv_wc *mad_recv_wc) 2773 { 2774 struct ib_mad_send_buf *msg = NULL; 2775 struct cm_dreq_msg *dreq_msg; 2776 struct cm_drep_msg *drep_msg; 2777 int ret; 2778 2779 ret = cm_alloc_response_msg(port, mad_recv_wc, &msg); 2780 if (ret) 2781 return ret; 2782 2783 dreq_msg = (struct cm_dreq_msg *) mad_recv_wc->recv_buf.mad; 2784 drep_msg = (struct cm_drep_msg *) msg->mad; 2785 2786 cm_format_mad_hdr(&drep_msg->hdr, CM_DREP_ATTR_ID, dreq_msg->hdr.tid); 2787 IBA_SET(CM_DREP_REMOTE_COMM_ID, drep_msg, 2788 IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg)); 2789 IBA_SET(CM_DREP_LOCAL_COMM_ID, drep_msg, 2790 IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)); 2791 2792 trace_icm_issue_drep( 2793 IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg), 2794 IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)); 2795 ret = ib_post_send_mad(msg, NULL); 2796 if (ret) 2797 cm_free_msg(msg); 2798 2799 return ret; 2800 } 2801 2802 static int cm_dreq_handler(struct cm_work *work) 2803 { 2804 struct cm_id_private *cm_id_priv; 2805 struct cm_dreq_msg *dreq_msg; 2806 struct ib_mad_send_buf *msg = NULL; 2807 2808 dreq_msg = (struct cm_dreq_msg *)work->mad_recv_wc->recv_buf.mad; 2809 cm_id_priv = cm_acquire_id( 2810 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)), 2811 cpu_to_be32(IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg))); 2812 if (!cm_id_priv) { 2813 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 2814 counter[CM_DREQ_COUNTER]); 2815 cm_issue_drep(work->port, work->mad_recv_wc); 2816 trace_icm_no_priv_err( 2817 IBA_GET(CM_DREQ_LOCAL_COMM_ID, dreq_msg), 2818 IBA_GET(CM_DREQ_REMOTE_COMM_ID, dreq_msg)); 2819 return -EINVAL; 2820 } 2821 2822 work->cm_event.private_data = 2823 IBA_GET_MEM_PTR(CM_DREQ_PRIVATE_DATA, dreq_msg); 2824 2825 spin_lock_irq(&cm_id_priv->lock); 2826 if (cm_id_priv->local_qpn != 2827 cpu_to_be32(IBA_GET(CM_DREQ_REMOTE_QPN_EECN, dreq_msg))) 2828 goto unlock; 2829 2830 switch (cm_id_priv->id.state) { 2831 case IB_CM_REP_SENT: 2832 case IB_CM_DREQ_SENT: 2833 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2834 break; 2835 case IB_CM_ESTABLISHED: 2836 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT || 2837 cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD) 2838 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2839 break; 2840 case IB_CM_MRA_REP_RCVD: 2841 break; 2842 case IB_CM_TIMEWAIT: 2843 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 2844 counter[CM_DREQ_COUNTER]); 2845 msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc); 2846 if (IS_ERR(msg)) 2847 goto unlock; 2848 2849 cm_format_drep((struct cm_drep_msg *) msg->mad, cm_id_priv, 2850 cm_id_priv->private_data, 2851 cm_id_priv->private_data_len); 2852 spin_unlock_irq(&cm_id_priv->lock); 2853 2854 if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) || 2855 ib_post_send_mad(msg, NULL)) 2856 cm_free_msg(msg); 2857 goto deref; 2858 case IB_CM_DREQ_RCVD: 2859 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 2860 counter[CM_DREQ_COUNTER]); 2861 goto unlock; 2862 default: 2863 trace_icm_dreq_unknown_err(&cm_id_priv->id); 2864 goto unlock; 2865 } 2866 cm_id_priv->id.state = IB_CM_DREQ_RCVD; 2867 cm_id_priv->tid = dreq_msg->hdr.tid; 2868 cm_queue_work_unlock(cm_id_priv, work); 2869 return 0; 2870 2871 unlock: spin_unlock_irq(&cm_id_priv->lock); 2872 deref: cm_deref_id(cm_id_priv); 2873 return -EINVAL; 2874 } 2875 2876 static int cm_drep_handler(struct cm_work *work) 2877 { 2878 struct cm_id_private *cm_id_priv; 2879 struct cm_drep_msg *drep_msg; 2880 2881 drep_msg = (struct cm_drep_msg *)work->mad_recv_wc->recv_buf.mad; 2882 cm_id_priv = cm_acquire_id( 2883 cpu_to_be32(IBA_GET(CM_DREP_REMOTE_COMM_ID, drep_msg)), 2884 cpu_to_be32(IBA_GET(CM_DREP_LOCAL_COMM_ID, drep_msg))); 2885 if (!cm_id_priv) 2886 return -EINVAL; 2887 2888 work->cm_event.private_data = 2889 IBA_GET_MEM_PTR(CM_DREP_PRIVATE_DATA, drep_msg); 2890 2891 spin_lock_irq(&cm_id_priv->lock); 2892 if (cm_id_priv->id.state != IB_CM_DREQ_SENT && 2893 cm_id_priv->id.state != IB_CM_DREQ_RCVD) { 2894 spin_unlock_irq(&cm_id_priv->lock); 2895 goto out; 2896 } 2897 cm_enter_timewait(cm_id_priv); 2898 2899 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 2900 cm_queue_work_unlock(cm_id_priv, work); 2901 return 0; 2902 out: 2903 cm_deref_id(cm_id_priv); 2904 return -EINVAL; 2905 } 2906 2907 static int cm_send_rej_locked(struct cm_id_private *cm_id_priv, 2908 enum ib_cm_rej_reason reason, void *ari, 2909 u8 ari_length, const void *private_data, 2910 u8 private_data_len) 2911 { 2912 enum ib_cm_state state = cm_id_priv->id.state; 2913 struct ib_mad_send_buf *msg; 2914 int ret; 2915 2916 lockdep_assert_held(&cm_id_priv->lock); 2917 2918 if ((private_data && private_data_len > IB_CM_REJ_PRIVATE_DATA_SIZE) || 2919 (ari && ari_length > IB_CM_REJ_ARI_LENGTH)) 2920 return -EINVAL; 2921 2922 switch (state) { 2923 case IB_CM_REQ_SENT: 2924 case IB_CM_MRA_REQ_RCVD: 2925 case IB_CM_REQ_RCVD: 2926 case IB_CM_MRA_REQ_SENT: 2927 case IB_CM_REP_RCVD: 2928 case IB_CM_MRA_REP_SENT: 2929 cm_reset_to_idle(cm_id_priv); 2930 ret = cm_alloc_msg(cm_id_priv, &msg); 2931 if (ret) 2932 return ret; 2933 cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason, 2934 ari, ari_length, private_data, private_data_len, 2935 state); 2936 break; 2937 case IB_CM_REP_SENT: 2938 case IB_CM_MRA_REP_RCVD: 2939 cm_enter_timewait(cm_id_priv); 2940 ret = cm_alloc_msg(cm_id_priv, &msg); 2941 if (ret) 2942 return ret; 2943 cm_format_rej((struct cm_rej_msg *)msg->mad, cm_id_priv, reason, 2944 ari, ari_length, private_data, private_data_len, 2945 state); 2946 break; 2947 default: 2948 trace_icm_send_unknown_rej_err(&cm_id_priv->id); 2949 return -EINVAL; 2950 } 2951 2952 trace_icm_send_rej(&cm_id_priv->id, reason); 2953 ret = ib_post_send_mad(msg, NULL); 2954 if (ret) { 2955 cm_free_msg(msg); 2956 return ret; 2957 } 2958 2959 return 0; 2960 } 2961 2962 int ib_send_cm_rej(struct ib_cm_id *cm_id, enum ib_cm_rej_reason reason, 2963 void *ari, u8 ari_length, const void *private_data, 2964 u8 private_data_len) 2965 { 2966 struct cm_id_private *cm_id_priv = 2967 container_of(cm_id, struct cm_id_private, id); 2968 unsigned long flags; 2969 int ret; 2970 2971 spin_lock_irqsave(&cm_id_priv->lock, flags); 2972 ret = cm_send_rej_locked(cm_id_priv, reason, ari, ari_length, 2973 private_data, private_data_len); 2974 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 2975 return ret; 2976 } 2977 EXPORT_SYMBOL(ib_send_cm_rej); 2978 2979 static void cm_format_rej_event(struct cm_work *work) 2980 { 2981 struct cm_rej_msg *rej_msg; 2982 struct ib_cm_rej_event_param *param; 2983 2984 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad; 2985 param = &work->cm_event.param.rej_rcvd; 2986 param->ari = IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg); 2987 param->ari_length = IBA_GET(CM_REJ_REJECTED_INFO_LENGTH, rej_msg); 2988 param->reason = IBA_GET(CM_REJ_REASON, rej_msg); 2989 work->cm_event.private_data = 2990 IBA_GET_MEM_PTR(CM_REJ_PRIVATE_DATA, rej_msg); 2991 } 2992 2993 static struct cm_id_private *cm_acquire_rejected_id(struct cm_rej_msg *rej_msg) 2994 { 2995 struct cm_id_private *cm_id_priv; 2996 __be32 remote_id; 2997 2998 remote_id = cpu_to_be32(IBA_GET(CM_REJ_LOCAL_COMM_ID, rej_msg)); 2999 3000 if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_TIMEOUT) { 3001 cm_id_priv = cm_find_remote_id( 3002 *((__be64 *)IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg)), 3003 remote_id); 3004 } else if (IBA_GET(CM_REJ_MESSAGE_REJECTED, rej_msg) == 3005 CM_MSG_RESPONSE_REQ) 3006 cm_id_priv = cm_acquire_id( 3007 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)), 3008 0); 3009 else 3010 cm_id_priv = cm_acquire_id( 3011 cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)), 3012 remote_id); 3013 3014 return cm_id_priv; 3015 } 3016 3017 static int cm_rej_handler(struct cm_work *work) 3018 { 3019 struct cm_id_private *cm_id_priv; 3020 struct cm_rej_msg *rej_msg; 3021 3022 rej_msg = (struct cm_rej_msg *)work->mad_recv_wc->recv_buf.mad; 3023 cm_id_priv = cm_acquire_rejected_id(rej_msg); 3024 if (!cm_id_priv) 3025 return -EINVAL; 3026 3027 cm_format_rej_event(work); 3028 3029 spin_lock_irq(&cm_id_priv->lock); 3030 switch (cm_id_priv->id.state) { 3031 case IB_CM_REQ_SENT: 3032 case IB_CM_MRA_REQ_RCVD: 3033 case IB_CM_REP_SENT: 3034 case IB_CM_MRA_REP_RCVD: 3035 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 3036 fallthrough; 3037 case IB_CM_REQ_RCVD: 3038 case IB_CM_MRA_REQ_SENT: 3039 if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_STALE_CONN) 3040 cm_enter_timewait(cm_id_priv); 3041 else 3042 cm_reset_to_idle(cm_id_priv); 3043 break; 3044 case IB_CM_DREQ_SENT: 3045 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 3046 fallthrough; 3047 case IB_CM_REP_RCVD: 3048 case IB_CM_MRA_REP_SENT: 3049 cm_enter_timewait(cm_id_priv); 3050 break; 3051 case IB_CM_ESTABLISHED: 3052 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT || 3053 cm_id_priv->id.lap_state == IB_CM_LAP_SENT) { 3054 if (cm_id_priv->id.lap_state == IB_CM_LAP_SENT) 3055 ib_cancel_mad(cm_id_priv->av.port->mad_agent, 3056 cm_id_priv->msg); 3057 cm_enter_timewait(cm_id_priv); 3058 break; 3059 } 3060 fallthrough; 3061 default: 3062 trace_icm_rej_unknown_err(&cm_id_priv->id); 3063 spin_unlock_irq(&cm_id_priv->lock); 3064 goto out; 3065 } 3066 3067 cm_queue_work_unlock(cm_id_priv, work); 3068 return 0; 3069 out: 3070 cm_deref_id(cm_id_priv); 3071 return -EINVAL; 3072 } 3073 3074 int ib_send_cm_mra(struct ib_cm_id *cm_id, 3075 u8 service_timeout, 3076 const void *private_data, 3077 u8 private_data_len) 3078 { 3079 struct cm_id_private *cm_id_priv; 3080 struct ib_mad_send_buf *msg; 3081 enum ib_cm_state cm_state; 3082 enum ib_cm_lap_state lap_state; 3083 enum cm_msg_response msg_response; 3084 void *data; 3085 unsigned long flags; 3086 int ret; 3087 3088 if (private_data && private_data_len > IB_CM_MRA_PRIVATE_DATA_SIZE) 3089 return -EINVAL; 3090 3091 data = cm_copy_private_data(private_data, private_data_len); 3092 if (IS_ERR(data)) 3093 return PTR_ERR(data); 3094 3095 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 3096 3097 spin_lock_irqsave(&cm_id_priv->lock, flags); 3098 switch (cm_id_priv->id.state) { 3099 case IB_CM_REQ_RCVD: 3100 cm_state = IB_CM_MRA_REQ_SENT; 3101 lap_state = cm_id->lap_state; 3102 msg_response = CM_MSG_RESPONSE_REQ; 3103 break; 3104 case IB_CM_REP_RCVD: 3105 cm_state = IB_CM_MRA_REP_SENT; 3106 lap_state = cm_id->lap_state; 3107 msg_response = CM_MSG_RESPONSE_REP; 3108 break; 3109 case IB_CM_ESTABLISHED: 3110 if (cm_id->lap_state == IB_CM_LAP_RCVD) { 3111 cm_state = cm_id->state; 3112 lap_state = IB_CM_MRA_LAP_SENT; 3113 msg_response = CM_MSG_RESPONSE_OTHER; 3114 break; 3115 } 3116 fallthrough; 3117 default: 3118 trace_icm_send_mra_unknown_err(&cm_id_priv->id); 3119 ret = -EINVAL; 3120 goto error1; 3121 } 3122 3123 if (!(service_timeout & IB_CM_MRA_FLAG_DELAY)) { 3124 ret = cm_alloc_msg(cm_id_priv, &msg); 3125 if (ret) 3126 goto error1; 3127 3128 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, 3129 msg_response, service_timeout, 3130 private_data, private_data_len); 3131 trace_icm_send_mra(cm_id); 3132 ret = ib_post_send_mad(msg, NULL); 3133 if (ret) 3134 goto error2; 3135 } 3136 3137 cm_id->state = cm_state; 3138 cm_id->lap_state = lap_state; 3139 cm_id_priv->service_timeout = service_timeout; 3140 cm_set_private_data(cm_id_priv, data, private_data_len); 3141 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3142 return 0; 3143 3144 error1: spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3145 kfree(data); 3146 return ret; 3147 3148 error2: spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3149 kfree(data); 3150 cm_free_msg(msg); 3151 return ret; 3152 } 3153 EXPORT_SYMBOL(ib_send_cm_mra); 3154 3155 static struct cm_id_private *cm_acquire_mraed_id(struct cm_mra_msg *mra_msg) 3156 { 3157 switch (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg)) { 3158 case CM_MSG_RESPONSE_REQ: 3159 return cm_acquire_id( 3160 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)), 3161 0); 3162 case CM_MSG_RESPONSE_REP: 3163 case CM_MSG_RESPONSE_OTHER: 3164 return cm_acquire_id( 3165 cpu_to_be32(IBA_GET(CM_MRA_REMOTE_COMM_ID, mra_msg)), 3166 cpu_to_be32(IBA_GET(CM_MRA_LOCAL_COMM_ID, mra_msg))); 3167 default: 3168 return NULL; 3169 } 3170 } 3171 3172 static int cm_mra_handler(struct cm_work *work) 3173 { 3174 struct cm_id_private *cm_id_priv; 3175 struct cm_mra_msg *mra_msg; 3176 int timeout; 3177 3178 mra_msg = (struct cm_mra_msg *)work->mad_recv_wc->recv_buf.mad; 3179 cm_id_priv = cm_acquire_mraed_id(mra_msg); 3180 if (!cm_id_priv) 3181 return -EINVAL; 3182 3183 work->cm_event.private_data = 3184 IBA_GET_MEM_PTR(CM_MRA_PRIVATE_DATA, mra_msg); 3185 work->cm_event.param.mra_rcvd.service_timeout = 3186 IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg); 3187 timeout = cm_convert_to_ms(IBA_GET(CM_MRA_SERVICE_TIMEOUT, mra_msg)) + 3188 cm_convert_to_ms(cm_id_priv->av.timeout); 3189 3190 spin_lock_irq(&cm_id_priv->lock); 3191 switch (cm_id_priv->id.state) { 3192 case IB_CM_REQ_SENT: 3193 if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) != 3194 CM_MSG_RESPONSE_REQ || 3195 ib_modify_mad(cm_id_priv->av.port->mad_agent, 3196 cm_id_priv->msg, timeout)) 3197 goto out; 3198 cm_id_priv->id.state = IB_CM_MRA_REQ_RCVD; 3199 break; 3200 case IB_CM_REP_SENT: 3201 if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) != 3202 CM_MSG_RESPONSE_REP || 3203 ib_modify_mad(cm_id_priv->av.port->mad_agent, 3204 cm_id_priv->msg, timeout)) 3205 goto out; 3206 cm_id_priv->id.state = IB_CM_MRA_REP_RCVD; 3207 break; 3208 case IB_CM_ESTABLISHED: 3209 if (IBA_GET(CM_MRA_MESSAGE_MRAED, mra_msg) != 3210 CM_MSG_RESPONSE_OTHER || 3211 cm_id_priv->id.lap_state != IB_CM_LAP_SENT || 3212 ib_modify_mad(cm_id_priv->av.port->mad_agent, 3213 cm_id_priv->msg, timeout)) { 3214 if (cm_id_priv->id.lap_state == IB_CM_MRA_LAP_RCVD) 3215 atomic_long_inc(&work->port-> 3216 counter_group[CM_RECV_DUPLICATES]. 3217 counter[CM_MRA_COUNTER]); 3218 goto out; 3219 } 3220 cm_id_priv->id.lap_state = IB_CM_MRA_LAP_RCVD; 3221 break; 3222 case IB_CM_MRA_REQ_RCVD: 3223 case IB_CM_MRA_REP_RCVD: 3224 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 3225 counter[CM_MRA_COUNTER]); 3226 fallthrough; 3227 default: 3228 trace_icm_mra_unknown_err(&cm_id_priv->id); 3229 goto out; 3230 } 3231 3232 cm_id_priv->msg->context[1] = (void *) (unsigned long) 3233 cm_id_priv->id.state; 3234 cm_queue_work_unlock(cm_id_priv, work); 3235 return 0; 3236 out: 3237 spin_unlock_irq(&cm_id_priv->lock); 3238 cm_deref_id(cm_id_priv); 3239 return -EINVAL; 3240 } 3241 3242 static void cm_format_path_lid_from_lap(struct cm_lap_msg *lap_msg, 3243 struct sa_path_rec *path) 3244 { 3245 u32 lid; 3246 3247 if (path->rec_type != SA_PATH_REC_TYPE_OPA) { 3248 sa_path_set_dlid(path, IBA_GET(CM_LAP_ALTERNATE_LOCAL_PORT_LID, 3249 lap_msg)); 3250 sa_path_set_slid(path, IBA_GET(CM_LAP_ALTERNATE_REMOTE_PORT_LID, 3251 lap_msg)); 3252 } else { 3253 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR( 3254 CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg)); 3255 sa_path_set_dlid(path, lid); 3256 3257 lid = opa_get_lid_from_gid(IBA_GET_MEM_PTR( 3258 CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg)); 3259 sa_path_set_slid(path, lid); 3260 } 3261 } 3262 3263 static void cm_format_path_from_lap(struct cm_id_private *cm_id_priv, 3264 struct sa_path_rec *path, 3265 struct cm_lap_msg *lap_msg) 3266 { 3267 path->dgid = *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID, lap_msg); 3268 path->sgid = 3269 *IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_REMOTE_PORT_GID, lap_msg); 3270 path->flow_label = 3271 cpu_to_be32(IBA_GET(CM_LAP_ALTERNATE_FLOW_LABEL, lap_msg)); 3272 path->hop_limit = IBA_GET(CM_LAP_ALTERNATE_HOP_LIMIT, lap_msg); 3273 path->traffic_class = IBA_GET(CM_LAP_ALTERNATE_TRAFFIC_CLASS, lap_msg); 3274 path->reversible = 1; 3275 path->pkey = cm_id_priv->pkey; 3276 path->sl = IBA_GET(CM_LAP_ALTERNATE_SL, lap_msg); 3277 path->mtu_selector = IB_SA_EQ; 3278 path->mtu = cm_id_priv->path_mtu; 3279 path->rate_selector = IB_SA_EQ; 3280 path->rate = IBA_GET(CM_LAP_ALTERNATE_PACKET_RATE, lap_msg); 3281 path->packet_life_time_selector = IB_SA_EQ; 3282 path->packet_life_time = 3283 IBA_GET(CM_LAP_ALTERNATE_LOCAL_ACK_TIMEOUT, lap_msg); 3284 path->packet_life_time -= (path->packet_life_time > 0); 3285 cm_format_path_lid_from_lap(lap_msg, path); 3286 } 3287 3288 static int cm_lap_handler(struct cm_work *work) 3289 { 3290 struct cm_id_private *cm_id_priv; 3291 struct cm_lap_msg *lap_msg; 3292 struct ib_cm_lap_event_param *param; 3293 struct ib_mad_send_buf *msg = NULL; 3294 int ret; 3295 3296 /* Currently Alternate path messages are not supported for 3297 * RoCE link layer. 3298 */ 3299 if (rdma_protocol_roce(work->port->cm_dev->ib_device, 3300 work->port->port_num)) 3301 return -EINVAL; 3302 3303 /* todo: verify LAP request and send reject APR if invalid. */ 3304 lap_msg = (struct cm_lap_msg *)work->mad_recv_wc->recv_buf.mad; 3305 cm_id_priv = cm_acquire_id( 3306 cpu_to_be32(IBA_GET(CM_LAP_REMOTE_COMM_ID, lap_msg)), 3307 cpu_to_be32(IBA_GET(CM_LAP_LOCAL_COMM_ID, lap_msg))); 3308 if (!cm_id_priv) 3309 return -EINVAL; 3310 3311 param = &work->cm_event.param.lap_rcvd; 3312 memset(&work->path[0], 0, sizeof(work->path[1])); 3313 cm_path_set_rec_type(work->port->cm_dev->ib_device, 3314 work->port->port_num, &work->path[0], 3315 IBA_GET_MEM_PTR(CM_LAP_ALTERNATE_LOCAL_PORT_GID, 3316 lap_msg)); 3317 param->alternate_path = &work->path[0]; 3318 cm_format_path_from_lap(cm_id_priv, param->alternate_path, lap_msg); 3319 work->cm_event.private_data = 3320 IBA_GET_MEM_PTR(CM_LAP_PRIVATE_DATA, lap_msg); 3321 3322 spin_lock_irq(&cm_id_priv->lock); 3323 if (cm_id_priv->id.state != IB_CM_ESTABLISHED) 3324 goto unlock; 3325 3326 switch (cm_id_priv->id.lap_state) { 3327 case IB_CM_LAP_UNINIT: 3328 case IB_CM_LAP_IDLE: 3329 break; 3330 case IB_CM_MRA_LAP_SENT: 3331 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 3332 counter[CM_LAP_COUNTER]); 3333 msg = cm_alloc_response_msg_no_ah(work->port, work->mad_recv_wc); 3334 if (IS_ERR(msg)) 3335 goto unlock; 3336 3337 cm_format_mra((struct cm_mra_msg *) msg->mad, cm_id_priv, 3338 CM_MSG_RESPONSE_OTHER, 3339 cm_id_priv->service_timeout, 3340 cm_id_priv->private_data, 3341 cm_id_priv->private_data_len); 3342 spin_unlock_irq(&cm_id_priv->lock); 3343 3344 if (cm_create_response_msg_ah(work->port, work->mad_recv_wc, msg) || 3345 ib_post_send_mad(msg, NULL)) 3346 cm_free_msg(msg); 3347 goto deref; 3348 case IB_CM_LAP_RCVD: 3349 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 3350 counter[CM_LAP_COUNTER]); 3351 goto unlock; 3352 default: 3353 goto unlock; 3354 } 3355 3356 ret = cm_init_av_for_lap(work->port, work->mad_recv_wc->wc, 3357 work->mad_recv_wc->recv_buf.grh, 3358 &cm_id_priv->av); 3359 if (ret) 3360 goto unlock; 3361 3362 ret = cm_init_av_by_path(param->alternate_path, NULL, 3363 &cm_id_priv->alt_av, cm_id_priv); 3364 if (ret) 3365 goto unlock; 3366 3367 cm_id_priv->id.lap_state = IB_CM_LAP_RCVD; 3368 cm_id_priv->tid = lap_msg->hdr.tid; 3369 cm_queue_work_unlock(cm_id_priv, work); 3370 return 0; 3371 3372 unlock: spin_unlock_irq(&cm_id_priv->lock); 3373 deref: cm_deref_id(cm_id_priv); 3374 return -EINVAL; 3375 } 3376 3377 static int cm_apr_handler(struct cm_work *work) 3378 { 3379 struct cm_id_private *cm_id_priv; 3380 struct cm_apr_msg *apr_msg; 3381 3382 /* Currently Alternate path messages are not supported for 3383 * RoCE link layer. 3384 */ 3385 if (rdma_protocol_roce(work->port->cm_dev->ib_device, 3386 work->port->port_num)) 3387 return -EINVAL; 3388 3389 apr_msg = (struct cm_apr_msg *)work->mad_recv_wc->recv_buf.mad; 3390 cm_id_priv = cm_acquire_id( 3391 cpu_to_be32(IBA_GET(CM_APR_REMOTE_COMM_ID, apr_msg)), 3392 cpu_to_be32(IBA_GET(CM_APR_LOCAL_COMM_ID, apr_msg))); 3393 if (!cm_id_priv) 3394 return -EINVAL; /* Unmatched reply. */ 3395 3396 work->cm_event.param.apr_rcvd.ap_status = 3397 IBA_GET(CM_APR_AR_STATUS, apr_msg); 3398 work->cm_event.param.apr_rcvd.apr_info = 3399 IBA_GET_MEM_PTR(CM_APR_ADDITIONAL_INFORMATION, apr_msg); 3400 work->cm_event.param.apr_rcvd.info_len = 3401 IBA_GET(CM_APR_ADDITIONAL_INFORMATION_LENGTH, apr_msg); 3402 work->cm_event.private_data = 3403 IBA_GET_MEM_PTR(CM_APR_PRIVATE_DATA, apr_msg); 3404 3405 spin_lock_irq(&cm_id_priv->lock); 3406 if (cm_id_priv->id.state != IB_CM_ESTABLISHED || 3407 (cm_id_priv->id.lap_state != IB_CM_LAP_SENT && 3408 cm_id_priv->id.lap_state != IB_CM_MRA_LAP_RCVD)) { 3409 spin_unlock_irq(&cm_id_priv->lock); 3410 goto out; 3411 } 3412 cm_id_priv->id.lap_state = IB_CM_LAP_IDLE; 3413 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 3414 cm_id_priv->msg = NULL; 3415 cm_queue_work_unlock(cm_id_priv, work); 3416 return 0; 3417 out: 3418 cm_deref_id(cm_id_priv); 3419 return -EINVAL; 3420 } 3421 3422 static int cm_timewait_handler(struct cm_work *work) 3423 { 3424 struct cm_timewait_info *timewait_info; 3425 struct cm_id_private *cm_id_priv; 3426 3427 timewait_info = container_of(work, struct cm_timewait_info, work); 3428 spin_lock_irq(&cm.lock); 3429 list_del(&timewait_info->list); 3430 spin_unlock_irq(&cm.lock); 3431 3432 cm_id_priv = cm_acquire_id(timewait_info->work.local_id, 3433 timewait_info->work.remote_id); 3434 if (!cm_id_priv) 3435 return -EINVAL; 3436 3437 spin_lock_irq(&cm_id_priv->lock); 3438 if (cm_id_priv->id.state != IB_CM_TIMEWAIT || 3439 cm_id_priv->remote_qpn != timewait_info->remote_qpn) { 3440 spin_unlock_irq(&cm_id_priv->lock); 3441 goto out; 3442 } 3443 cm_id_priv->id.state = IB_CM_IDLE; 3444 cm_queue_work_unlock(cm_id_priv, work); 3445 return 0; 3446 out: 3447 cm_deref_id(cm_id_priv); 3448 return -EINVAL; 3449 } 3450 3451 static void cm_format_sidr_req(struct cm_sidr_req_msg *sidr_req_msg, 3452 struct cm_id_private *cm_id_priv, 3453 struct ib_cm_sidr_req_param *param) 3454 { 3455 cm_format_mad_hdr(&sidr_req_msg->hdr, CM_SIDR_REQ_ATTR_ID, 3456 cm_form_tid(cm_id_priv)); 3457 IBA_SET(CM_SIDR_REQ_REQUESTID, sidr_req_msg, 3458 be32_to_cpu(cm_id_priv->id.local_id)); 3459 IBA_SET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg, 3460 be16_to_cpu(param->path->pkey)); 3461 IBA_SET(CM_SIDR_REQ_SERVICEID, sidr_req_msg, 3462 be64_to_cpu(param->service_id)); 3463 3464 if (param->private_data && param->private_data_len) 3465 IBA_SET_MEM(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg, 3466 param->private_data, param->private_data_len); 3467 } 3468 3469 int ib_send_cm_sidr_req(struct ib_cm_id *cm_id, 3470 struct ib_cm_sidr_req_param *param) 3471 { 3472 struct cm_id_private *cm_id_priv; 3473 struct ib_mad_send_buf *msg; 3474 unsigned long flags; 3475 int ret; 3476 3477 if (!param->path || (param->private_data && 3478 param->private_data_len > IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE)) 3479 return -EINVAL; 3480 3481 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 3482 ret = cm_init_av_by_path(param->path, param->sgid_attr, 3483 &cm_id_priv->av, 3484 cm_id_priv); 3485 if (ret) 3486 goto out; 3487 3488 cm_id->service_id = param->service_id; 3489 cm_id->service_mask = ~cpu_to_be64(0); 3490 cm_id_priv->timeout_ms = param->timeout_ms; 3491 cm_id_priv->max_cm_retries = param->max_cm_retries; 3492 ret = cm_alloc_msg(cm_id_priv, &msg); 3493 if (ret) 3494 goto out; 3495 3496 cm_format_sidr_req((struct cm_sidr_req_msg *) msg->mad, cm_id_priv, 3497 param); 3498 msg->timeout_ms = cm_id_priv->timeout_ms; 3499 msg->context[1] = (void *) (unsigned long) IB_CM_SIDR_REQ_SENT; 3500 3501 spin_lock_irqsave(&cm_id_priv->lock, flags); 3502 if (cm_id->state == IB_CM_IDLE) { 3503 trace_icm_send_sidr_req(&cm_id_priv->id); 3504 ret = ib_post_send_mad(msg, NULL); 3505 } else { 3506 ret = -EINVAL; 3507 } 3508 3509 if (ret) { 3510 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3511 cm_free_msg(msg); 3512 goto out; 3513 } 3514 cm_id->state = IB_CM_SIDR_REQ_SENT; 3515 cm_id_priv->msg = msg; 3516 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3517 out: 3518 return ret; 3519 } 3520 EXPORT_SYMBOL(ib_send_cm_sidr_req); 3521 3522 static void cm_format_sidr_req_event(struct cm_work *work, 3523 const struct cm_id_private *rx_cm_id, 3524 struct ib_cm_id *listen_id) 3525 { 3526 struct cm_sidr_req_msg *sidr_req_msg; 3527 struct ib_cm_sidr_req_event_param *param; 3528 3529 sidr_req_msg = (struct cm_sidr_req_msg *) 3530 work->mad_recv_wc->recv_buf.mad; 3531 param = &work->cm_event.param.sidr_req_rcvd; 3532 param->pkey = IBA_GET(CM_SIDR_REQ_PARTITION_KEY, sidr_req_msg); 3533 param->listen_id = listen_id; 3534 param->service_id = 3535 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg)); 3536 param->bth_pkey = cm_get_bth_pkey(work); 3537 param->port = work->port->port_num; 3538 param->sgid_attr = rx_cm_id->av.ah_attr.grh.sgid_attr; 3539 work->cm_event.private_data = 3540 IBA_GET_MEM_PTR(CM_SIDR_REQ_PRIVATE_DATA, sidr_req_msg); 3541 } 3542 3543 static int cm_sidr_req_handler(struct cm_work *work) 3544 { 3545 struct cm_id_private *cm_id_priv, *listen_cm_id_priv; 3546 struct cm_sidr_req_msg *sidr_req_msg; 3547 struct ib_wc *wc; 3548 int ret; 3549 3550 cm_id_priv = 3551 cm_alloc_id_priv(work->port->cm_dev->ib_device, NULL, NULL); 3552 if (IS_ERR(cm_id_priv)) 3553 return PTR_ERR(cm_id_priv); 3554 3555 /* Record SGID/SLID and request ID for lookup. */ 3556 sidr_req_msg = (struct cm_sidr_req_msg *) 3557 work->mad_recv_wc->recv_buf.mad; 3558 3559 cm_id_priv->id.remote_id = 3560 cpu_to_be32(IBA_GET(CM_SIDR_REQ_REQUESTID, sidr_req_msg)); 3561 cm_id_priv->id.service_id = 3562 cpu_to_be64(IBA_GET(CM_SIDR_REQ_SERVICEID, sidr_req_msg)); 3563 cm_id_priv->id.service_mask = ~cpu_to_be64(0); 3564 cm_id_priv->tid = sidr_req_msg->hdr.tid; 3565 3566 wc = work->mad_recv_wc->wc; 3567 cm_id_priv->av.dgid.global.subnet_prefix = cpu_to_be64(wc->slid); 3568 cm_id_priv->av.dgid.global.interface_id = 0; 3569 ret = cm_init_av_for_response(work->port, work->mad_recv_wc->wc, 3570 work->mad_recv_wc->recv_buf.grh, 3571 &cm_id_priv->av); 3572 if (ret) 3573 goto out; 3574 3575 spin_lock_irq(&cm.lock); 3576 listen_cm_id_priv = cm_insert_remote_sidr(cm_id_priv); 3577 if (listen_cm_id_priv) { 3578 spin_unlock_irq(&cm.lock); 3579 atomic_long_inc(&work->port->counter_group[CM_RECV_DUPLICATES]. 3580 counter[CM_SIDR_REQ_COUNTER]); 3581 goto out; /* Duplicate message. */ 3582 } 3583 cm_id_priv->id.state = IB_CM_SIDR_REQ_RCVD; 3584 listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device, 3585 cm_id_priv->id.service_id); 3586 if (!listen_cm_id_priv) { 3587 spin_unlock_irq(&cm.lock); 3588 ib_send_cm_sidr_rep(&cm_id_priv->id, 3589 &(struct ib_cm_sidr_rep_param){ 3590 .status = IB_SIDR_UNSUPPORTED }); 3591 goto out; /* No match. */ 3592 } 3593 spin_unlock_irq(&cm.lock); 3594 3595 cm_id_priv->id.cm_handler = listen_cm_id_priv->id.cm_handler; 3596 cm_id_priv->id.context = listen_cm_id_priv->id.context; 3597 3598 /* 3599 * A SIDR ID does not need to be in the xarray since it does not receive 3600 * mads, is not placed in the remote_id or remote_qpn rbtree, and does 3601 * not enter timewait. 3602 */ 3603 3604 cm_format_sidr_req_event(work, cm_id_priv, &listen_cm_id_priv->id); 3605 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &work->cm_event); 3606 cm_free_work(work); 3607 /* 3608 * A pointer to the listen_cm_id is held in the event, so this deref 3609 * must be after the event is delivered above. 3610 */ 3611 cm_deref_id(listen_cm_id_priv); 3612 if (ret) 3613 cm_destroy_id(&cm_id_priv->id, ret); 3614 return 0; 3615 out: 3616 ib_destroy_cm_id(&cm_id_priv->id); 3617 return -EINVAL; 3618 } 3619 3620 static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg, 3621 struct cm_id_private *cm_id_priv, 3622 struct ib_cm_sidr_rep_param *param) 3623 { 3624 cm_format_mad_ece_hdr(&sidr_rep_msg->hdr, CM_SIDR_REP_ATTR_ID, 3625 cm_id_priv->tid, param->ece.attr_mod); 3626 IBA_SET(CM_SIDR_REP_REQUESTID, sidr_rep_msg, 3627 be32_to_cpu(cm_id_priv->id.remote_id)); 3628 IBA_SET(CM_SIDR_REP_STATUS, sidr_rep_msg, param->status); 3629 IBA_SET(CM_SIDR_REP_QPN, sidr_rep_msg, param->qp_num); 3630 IBA_SET(CM_SIDR_REP_SERVICEID, sidr_rep_msg, 3631 be64_to_cpu(cm_id_priv->id.service_id)); 3632 IBA_SET(CM_SIDR_REP_Q_KEY, sidr_rep_msg, param->qkey); 3633 IBA_SET(CM_SIDR_REP_VENDOR_ID_L, sidr_rep_msg, 3634 param->ece.vendor_id & 0xFF); 3635 IBA_SET(CM_SIDR_REP_VENDOR_ID_H, sidr_rep_msg, 3636 (param->ece.vendor_id >> 8) & 0xFF); 3637 3638 if (param->info && param->info_length) 3639 IBA_SET_MEM(CM_SIDR_REP_ADDITIONAL_INFORMATION, sidr_rep_msg, 3640 param->info, param->info_length); 3641 3642 if (param->private_data && param->private_data_len) 3643 IBA_SET_MEM(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg, 3644 param->private_data, param->private_data_len); 3645 } 3646 3647 static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv, 3648 struct ib_cm_sidr_rep_param *param) 3649 { 3650 struct ib_mad_send_buf *msg; 3651 unsigned long flags; 3652 int ret; 3653 3654 lockdep_assert_held(&cm_id_priv->lock); 3655 3656 if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) || 3657 (param->private_data && 3658 param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE)) 3659 return -EINVAL; 3660 3661 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_RCVD) 3662 return -EINVAL; 3663 3664 ret = cm_alloc_msg(cm_id_priv, &msg); 3665 if (ret) 3666 return ret; 3667 3668 cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv, 3669 param); 3670 trace_icm_send_sidr_rep(&cm_id_priv->id); 3671 ret = ib_post_send_mad(msg, NULL); 3672 if (ret) { 3673 cm_free_msg(msg); 3674 return ret; 3675 } 3676 cm_id_priv->id.state = IB_CM_IDLE; 3677 spin_lock_irqsave(&cm.lock, flags); 3678 if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) { 3679 rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table); 3680 RB_CLEAR_NODE(&cm_id_priv->sidr_id_node); 3681 } 3682 spin_unlock_irqrestore(&cm.lock, flags); 3683 return 0; 3684 } 3685 3686 int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id, 3687 struct ib_cm_sidr_rep_param *param) 3688 { 3689 struct cm_id_private *cm_id_priv = 3690 container_of(cm_id, struct cm_id_private, id); 3691 unsigned long flags; 3692 int ret; 3693 3694 spin_lock_irqsave(&cm_id_priv->lock, flags); 3695 ret = cm_send_sidr_rep_locked(cm_id_priv, param); 3696 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3697 return ret; 3698 } 3699 EXPORT_SYMBOL(ib_send_cm_sidr_rep); 3700 3701 static void cm_format_sidr_rep_event(struct cm_work *work, 3702 const struct cm_id_private *cm_id_priv) 3703 { 3704 struct cm_sidr_rep_msg *sidr_rep_msg; 3705 struct ib_cm_sidr_rep_event_param *param; 3706 3707 sidr_rep_msg = (struct cm_sidr_rep_msg *) 3708 work->mad_recv_wc->recv_buf.mad; 3709 param = &work->cm_event.param.sidr_rep_rcvd; 3710 param->status = IBA_GET(CM_SIDR_REP_STATUS, sidr_rep_msg); 3711 param->qkey = IBA_GET(CM_SIDR_REP_Q_KEY, sidr_rep_msg); 3712 param->qpn = IBA_GET(CM_SIDR_REP_QPN, sidr_rep_msg); 3713 param->info = IBA_GET_MEM_PTR(CM_SIDR_REP_ADDITIONAL_INFORMATION, 3714 sidr_rep_msg); 3715 param->info_len = IBA_GET(CM_SIDR_REP_ADDITIONAL_INFORMATION_LENGTH, 3716 sidr_rep_msg); 3717 param->sgid_attr = cm_id_priv->av.ah_attr.grh.sgid_attr; 3718 work->cm_event.private_data = 3719 IBA_GET_MEM_PTR(CM_SIDR_REP_PRIVATE_DATA, sidr_rep_msg); 3720 } 3721 3722 static int cm_sidr_rep_handler(struct cm_work *work) 3723 { 3724 struct cm_sidr_rep_msg *sidr_rep_msg; 3725 struct cm_id_private *cm_id_priv; 3726 3727 sidr_rep_msg = (struct cm_sidr_rep_msg *) 3728 work->mad_recv_wc->recv_buf.mad; 3729 cm_id_priv = cm_acquire_id( 3730 cpu_to_be32(IBA_GET(CM_SIDR_REP_REQUESTID, sidr_rep_msg)), 0); 3731 if (!cm_id_priv) 3732 return -EINVAL; /* Unmatched reply. */ 3733 3734 spin_lock_irq(&cm_id_priv->lock); 3735 if (cm_id_priv->id.state != IB_CM_SIDR_REQ_SENT) { 3736 spin_unlock_irq(&cm_id_priv->lock); 3737 goto out; 3738 } 3739 cm_id_priv->id.state = IB_CM_IDLE; 3740 ib_cancel_mad(cm_id_priv->av.port->mad_agent, cm_id_priv->msg); 3741 spin_unlock_irq(&cm_id_priv->lock); 3742 3743 cm_format_sidr_rep_event(work, cm_id_priv); 3744 cm_process_work(cm_id_priv, work); 3745 return 0; 3746 out: 3747 cm_deref_id(cm_id_priv); 3748 return -EINVAL; 3749 } 3750 3751 static void cm_process_send_error(struct ib_mad_send_buf *msg, 3752 enum ib_wc_status wc_status) 3753 { 3754 struct cm_id_private *cm_id_priv; 3755 struct ib_cm_event cm_event; 3756 enum ib_cm_state state; 3757 int ret; 3758 3759 memset(&cm_event, 0, sizeof cm_event); 3760 cm_id_priv = msg->context[0]; 3761 3762 /* Discard old sends or ones without a response. */ 3763 spin_lock_irq(&cm_id_priv->lock); 3764 state = (enum ib_cm_state) (unsigned long) msg->context[1]; 3765 if (msg != cm_id_priv->msg || state != cm_id_priv->id.state) 3766 goto discard; 3767 3768 trace_icm_mad_send_err(state, wc_status); 3769 switch (state) { 3770 case IB_CM_REQ_SENT: 3771 case IB_CM_MRA_REQ_RCVD: 3772 cm_reset_to_idle(cm_id_priv); 3773 cm_event.event = IB_CM_REQ_ERROR; 3774 break; 3775 case IB_CM_REP_SENT: 3776 case IB_CM_MRA_REP_RCVD: 3777 cm_reset_to_idle(cm_id_priv); 3778 cm_event.event = IB_CM_REP_ERROR; 3779 break; 3780 case IB_CM_DREQ_SENT: 3781 cm_enter_timewait(cm_id_priv); 3782 cm_event.event = IB_CM_DREQ_ERROR; 3783 break; 3784 case IB_CM_SIDR_REQ_SENT: 3785 cm_id_priv->id.state = IB_CM_IDLE; 3786 cm_event.event = IB_CM_SIDR_REQ_ERROR; 3787 break; 3788 default: 3789 goto discard; 3790 } 3791 spin_unlock_irq(&cm_id_priv->lock); 3792 cm_event.param.send_status = wc_status; 3793 3794 /* No other events can occur on the cm_id at this point. */ 3795 ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, &cm_event); 3796 cm_free_msg(msg); 3797 if (ret) 3798 ib_destroy_cm_id(&cm_id_priv->id); 3799 return; 3800 discard: 3801 spin_unlock_irq(&cm_id_priv->lock); 3802 cm_free_msg(msg); 3803 } 3804 3805 static void cm_send_handler(struct ib_mad_agent *mad_agent, 3806 struct ib_mad_send_wc *mad_send_wc) 3807 { 3808 struct ib_mad_send_buf *msg = mad_send_wc->send_buf; 3809 struct cm_port *port; 3810 u16 attr_index; 3811 3812 port = mad_agent->context; 3813 attr_index = be16_to_cpu(((struct ib_mad_hdr *) 3814 msg->mad)->attr_id) - CM_ATTR_ID_OFFSET; 3815 3816 /* 3817 * If the send was in response to a received message (context[0] is not 3818 * set to a cm_id), and is not a REJ, then it is a send that was 3819 * manually retried. 3820 */ 3821 if (!msg->context[0] && (attr_index != CM_REJ_COUNTER)) 3822 msg->retries = 1; 3823 3824 atomic_long_add(1 + msg->retries, 3825 &port->counter_group[CM_XMIT].counter[attr_index]); 3826 if (msg->retries) 3827 atomic_long_add(msg->retries, 3828 &port->counter_group[CM_XMIT_RETRIES]. 3829 counter[attr_index]); 3830 3831 switch (mad_send_wc->status) { 3832 case IB_WC_SUCCESS: 3833 case IB_WC_WR_FLUSH_ERR: 3834 cm_free_msg(msg); 3835 break; 3836 default: 3837 if (msg->context[0] && msg->context[1]) 3838 cm_process_send_error(msg, mad_send_wc->status); 3839 else 3840 cm_free_msg(msg); 3841 break; 3842 } 3843 } 3844 3845 static void cm_work_handler(struct work_struct *_work) 3846 { 3847 struct cm_work *work = container_of(_work, struct cm_work, work.work); 3848 int ret; 3849 3850 switch (work->cm_event.event) { 3851 case IB_CM_REQ_RECEIVED: 3852 ret = cm_req_handler(work); 3853 break; 3854 case IB_CM_MRA_RECEIVED: 3855 ret = cm_mra_handler(work); 3856 break; 3857 case IB_CM_REJ_RECEIVED: 3858 ret = cm_rej_handler(work); 3859 break; 3860 case IB_CM_REP_RECEIVED: 3861 ret = cm_rep_handler(work); 3862 break; 3863 case IB_CM_RTU_RECEIVED: 3864 ret = cm_rtu_handler(work); 3865 break; 3866 case IB_CM_USER_ESTABLISHED: 3867 ret = cm_establish_handler(work); 3868 break; 3869 case IB_CM_DREQ_RECEIVED: 3870 ret = cm_dreq_handler(work); 3871 break; 3872 case IB_CM_DREP_RECEIVED: 3873 ret = cm_drep_handler(work); 3874 break; 3875 case IB_CM_SIDR_REQ_RECEIVED: 3876 ret = cm_sidr_req_handler(work); 3877 break; 3878 case IB_CM_SIDR_REP_RECEIVED: 3879 ret = cm_sidr_rep_handler(work); 3880 break; 3881 case IB_CM_LAP_RECEIVED: 3882 ret = cm_lap_handler(work); 3883 break; 3884 case IB_CM_APR_RECEIVED: 3885 ret = cm_apr_handler(work); 3886 break; 3887 case IB_CM_TIMEWAIT_EXIT: 3888 ret = cm_timewait_handler(work); 3889 break; 3890 default: 3891 trace_icm_handler_err(work->cm_event.event); 3892 ret = -EINVAL; 3893 break; 3894 } 3895 if (ret) 3896 cm_free_work(work); 3897 } 3898 3899 static int cm_establish(struct ib_cm_id *cm_id) 3900 { 3901 struct cm_id_private *cm_id_priv; 3902 struct cm_work *work; 3903 unsigned long flags; 3904 int ret = 0; 3905 struct cm_device *cm_dev; 3906 3907 cm_dev = ib_get_client_data(cm_id->device, &cm_client); 3908 if (!cm_dev) 3909 return -ENODEV; 3910 3911 work = kmalloc(sizeof *work, GFP_ATOMIC); 3912 if (!work) 3913 return -ENOMEM; 3914 3915 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 3916 spin_lock_irqsave(&cm_id_priv->lock, flags); 3917 switch (cm_id->state) { 3918 case IB_CM_REP_SENT: 3919 case IB_CM_MRA_REP_RCVD: 3920 cm_id->state = IB_CM_ESTABLISHED; 3921 break; 3922 case IB_CM_ESTABLISHED: 3923 ret = -EISCONN; 3924 break; 3925 default: 3926 trace_icm_establish_err(cm_id); 3927 ret = -EINVAL; 3928 break; 3929 } 3930 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3931 3932 if (ret) { 3933 kfree(work); 3934 goto out; 3935 } 3936 3937 /* 3938 * The CM worker thread may try to destroy the cm_id before it 3939 * can execute this work item. To prevent potential deadlock, 3940 * we need to find the cm_id once we're in the context of the 3941 * worker thread, rather than holding a reference on it. 3942 */ 3943 INIT_DELAYED_WORK(&work->work, cm_work_handler); 3944 work->local_id = cm_id->local_id; 3945 work->remote_id = cm_id->remote_id; 3946 work->mad_recv_wc = NULL; 3947 work->cm_event.event = IB_CM_USER_ESTABLISHED; 3948 3949 /* Check if the device started its remove_one */ 3950 spin_lock_irqsave(&cm.lock, flags); 3951 if (!cm_dev->going_down) { 3952 queue_delayed_work(cm.wq, &work->work, 0); 3953 } else { 3954 kfree(work); 3955 ret = -ENODEV; 3956 } 3957 spin_unlock_irqrestore(&cm.lock, flags); 3958 3959 out: 3960 return ret; 3961 } 3962 3963 static int cm_migrate(struct ib_cm_id *cm_id) 3964 { 3965 struct cm_id_private *cm_id_priv; 3966 struct cm_av tmp_av; 3967 unsigned long flags; 3968 int tmp_send_port_not_ready; 3969 int ret = 0; 3970 3971 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 3972 spin_lock_irqsave(&cm_id_priv->lock, flags); 3973 if (cm_id->state == IB_CM_ESTABLISHED && 3974 (cm_id->lap_state == IB_CM_LAP_UNINIT || 3975 cm_id->lap_state == IB_CM_LAP_IDLE)) { 3976 cm_id->lap_state = IB_CM_LAP_IDLE; 3977 /* Swap address vector */ 3978 tmp_av = cm_id_priv->av; 3979 cm_id_priv->av = cm_id_priv->alt_av; 3980 cm_id_priv->alt_av = tmp_av; 3981 /* Swap port send ready state */ 3982 tmp_send_port_not_ready = cm_id_priv->prim_send_port_not_ready; 3983 cm_id_priv->prim_send_port_not_ready = cm_id_priv->altr_send_port_not_ready; 3984 cm_id_priv->altr_send_port_not_ready = tmp_send_port_not_ready; 3985 } else 3986 ret = -EINVAL; 3987 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 3988 3989 return ret; 3990 } 3991 3992 int ib_cm_notify(struct ib_cm_id *cm_id, enum ib_event_type event) 3993 { 3994 int ret; 3995 3996 switch (event) { 3997 case IB_EVENT_COMM_EST: 3998 ret = cm_establish(cm_id); 3999 break; 4000 case IB_EVENT_PATH_MIG: 4001 ret = cm_migrate(cm_id); 4002 break; 4003 default: 4004 ret = -EINVAL; 4005 } 4006 return ret; 4007 } 4008 EXPORT_SYMBOL(ib_cm_notify); 4009 4010 static void cm_recv_handler(struct ib_mad_agent *mad_agent, 4011 struct ib_mad_send_buf *send_buf, 4012 struct ib_mad_recv_wc *mad_recv_wc) 4013 { 4014 struct cm_port *port = mad_agent->context; 4015 struct cm_work *work; 4016 enum ib_cm_event_type event; 4017 bool alt_path = false; 4018 u16 attr_id; 4019 int paths = 0; 4020 int going_down = 0; 4021 4022 switch (mad_recv_wc->recv_buf.mad->mad_hdr.attr_id) { 4023 case CM_REQ_ATTR_ID: 4024 alt_path = cm_req_has_alt_path((struct cm_req_msg *) 4025 mad_recv_wc->recv_buf.mad); 4026 paths = 1 + (alt_path != 0); 4027 event = IB_CM_REQ_RECEIVED; 4028 break; 4029 case CM_MRA_ATTR_ID: 4030 event = IB_CM_MRA_RECEIVED; 4031 break; 4032 case CM_REJ_ATTR_ID: 4033 event = IB_CM_REJ_RECEIVED; 4034 break; 4035 case CM_REP_ATTR_ID: 4036 event = IB_CM_REP_RECEIVED; 4037 break; 4038 case CM_RTU_ATTR_ID: 4039 event = IB_CM_RTU_RECEIVED; 4040 break; 4041 case CM_DREQ_ATTR_ID: 4042 event = IB_CM_DREQ_RECEIVED; 4043 break; 4044 case CM_DREP_ATTR_ID: 4045 event = IB_CM_DREP_RECEIVED; 4046 break; 4047 case CM_SIDR_REQ_ATTR_ID: 4048 event = IB_CM_SIDR_REQ_RECEIVED; 4049 break; 4050 case CM_SIDR_REP_ATTR_ID: 4051 event = IB_CM_SIDR_REP_RECEIVED; 4052 break; 4053 case CM_LAP_ATTR_ID: 4054 paths = 1; 4055 event = IB_CM_LAP_RECEIVED; 4056 break; 4057 case CM_APR_ATTR_ID: 4058 event = IB_CM_APR_RECEIVED; 4059 break; 4060 default: 4061 ib_free_recv_mad(mad_recv_wc); 4062 return; 4063 } 4064 4065 attr_id = be16_to_cpu(mad_recv_wc->recv_buf.mad->mad_hdr.attr_id); 4066 atomic_long_inc(&port->counter_group[CM_RECV]. 4067 counter[attr_id - CM_ATTR_ID_OFFSET]); 4068 4069 work = kmalloc(struct_size(work, path, paths), GFP_KERNEL); 4070 if (!work) { 4071 ib_free_recv_mad(mad_recv_wc); 4072 return; 4073 } 4074 4075 INIT_DELAYED_WORK(&work->work, cm_work_handler); 4076 work->cm_event.event = event; 4077 work->mad_recv_wc = mad_recv_wc; 4078 work->port = port; 4079 4080 /* Check if the device started its remove_one */ 4081 spin_lock_irq(&cm.lock); 4082 if (!port->cm_dev->going_down) 4083 queue_delayed_work(cm.wq, &work->work, 0); 4084 else 4085 going_down = 1; 4086 spin_unlock_irq(&cm.lock); 4087 4088 if (going_down) { 4089 kfree(work); 4090 ib_free_recv_mad(mad_recv_wc); 4091 } 4092 } 4093 4094 static int cm_init_qp_init_attr(struct cm_id_private *cm_id_priv, 4095 struct ib_qp_attr *qp_attr, 4096 int *qp_attr_mask) 4097 { 4098 unsigned long flags; 4099 int ret; 4100 4101 spin_lock_irqsave(&cm_id_priv->lock, flags); 4102 switch (cm_id_priv->id.state) { 4103 case IB_CM_REQ_SENT: 4104 case IB_CM_MRA_REQ_RCVD: 4105 case IB_CM_REQ_RCVD: 4106 case IB_CM_MRA_REQ_SENT: 4107 case IB_CM_REP_RCVD: 4108 case IB_CM_MRA_REP_SENT: 4109 case IB_CM_REP_SENT: 4110 case IB_CM_MRA_REP_RCVD: 4111 case IB_CM_ESTABLISHED: 4112 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | 4113 IB_QP_PKEY_INDEX | IB_QP_PORT; 4114 qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE; 4115 if (cm_id_priv->responder_resources) 4116 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ | 4117 IB_ACCESS_REMOTE_ATOMIC; 4118 qp_attr->pkey_index = cm_id_priv->av.pkey_index; 4119 qp_attr->port_num = cm_id_priv->av.port->port_num; 4120 ret = 0; 4121 break; 4122 default: 4123 trace_icm_qp_init_err(&cm_id_priv->id); 4124 ret = -EINVAL; 4125 break; 4126 } 4127 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 4128 return ret; 4129 } 4130 4131 static int cm_init_qp_rtr_attr(struct cm_id_private *cm_id_priv, 4132 struct ib_qp_attr *qp_attr, 4133 int *qp_attr_mask) 4134 { 4135 unsigned long flags; 4136 int ret; 4137 4138 spin_lock_irqsave(&cm_id_priv->lock, flags); 4139 switch (cm_id_priv->id.state) { 4140 case IB_CM_REQ_RCVD: 4141 case IB_CM_MRA_REQ_SENT: 4142 case IB_CM_REP_RCVD: 4143 case IB_CM_MRA_REP_SENT: 4144 case IB_CM_REP_SENT: 4145 case IB_CM_MRA_REP_RCVD: 4146 case IB_CM_ESTABLISHED: 4147 *qp_attr_mask = IB_QP_STATE | IB_QP_AV | IB_QP_PATH_MTU | 4148 IB_QP_DEST_QPN | IB_QP_RQ_PSN; 4149 qp_attr->ah_attr = cm_id_priv->av.ah_attr; 4150 qp_attr->path_mtu = cm_id_priv->path_mtu; 4151 qp_attr->dest_qp_num = be32_to_cpu(cm_id_priv->remote_qpn); 4152 qp_attr->rq_psn = be32_to_cpu(cm_id_priv->rq_psn); 4153 if (cm_id_priv->qp_type == IB_QPT_RC || 4154 cm_id_priv->qp_type == IB_QPT_XRC_TGT) { 4155 *qp_attr_mask |= IB_QP_MAX_DEST_RD_ATOMIC | 4156 IB_QP_MIN_RNR_TIMER; 4157 qp_attr->max_dest_rd_atomic = 4158 cm_id_priv->responder_resources; 4159 qp_attr->min_rnr_timer = 0; 4160 } 4161 if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) { 4162 *qp_attr_mask |= IB_QP_ALT_PATH; 4163 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num; 4164 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index; 4165 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout; 4166 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr; 4167 } 4168 ret = 0; 4169 break; 4170 default: 4171 trace_icm_qp_rtr_err(&cm_id_priv->id); 4172 ret = -EINVAL; 4173 break; 4174 } 4175 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 4176 return ret; 4177 } 4178 4179 static int cm_init_qp_rts_attr(struct cm_id_private *cm_id_priv, 4180 struct ib_qp_attr *qp_attr, 4181 int *qp_attr_mask) 4182 { 4183 unsigned long flags; 4184 int ret; 4185 4186 spin_lock_irqsave(&cm_id_priv->lock, flags); 4187 switch (cm_id_priv->id.state) { 4188 /* Allow transition to RTS before sending REP */ 4189 case IB_CM_REQ_RCVD: 4190 case IB_CM_MRA_REQ_SENT: 4191 4192 case IB_CM_REP_RCVD: 4193 case IB_CM_MRA_REP_SENT: 4194 case IB_CM_REP_SENT: 4195 case IB_CM_MRA_REP_RCVD: 4196 case IB_CM_ESTABLISHED: 4197 if (cm_id_priv->id.lap_state == IB_CM_LAP_UNINIT) { 4198 *qp_attr_mask = IB_QP_STATE | IB_QP_SQ_PSN; 4199 qp_attr->sq_psn = be32_to_cpu(cm_id_priv->sq_psn); 4200 switch (cm_id_priv->qp_type) { 4201 case IB_QPT_RC: 4202 case IB_QPT_XRC_INI: 4203 *qp_attr_mask |= IB_QP_RETRY_CNT | IB_QP_RNR_RETRY | 4204 IB_QP_MAX_QP_RD_ATOMIC; 4205 qp_attr->retry_cnt = cm_id_priv->retry_count; 4206 qp_attr->rnr_retry = cm_id_priv->rnr_retry_count; 4207 qp_attr->max_rd_atomic = cm_id_priv->initiator_depth; 4208 fallthrough; 4209 case IB_QPT_XRC_TGT: 4210 *qp_attr_mask |= IB_QP_TIMEOUT; 4211 qp_attr->timeout = cm_id_priv->av.timeout; 4212 break; 4213 default: 4214 break; 4215 } 4216 if (rdma_ah_get_dlid(&cm_id_priv->alt_av.ah_attr)) { 4217 *qp_attr_mask |= IB_QP_PATH_MIG_STATE; 4218 qp_attr->path_mig_state = IB_MIG_REARM; 4219 } 4220 } else { 4221 *qp_attr_mask = IB_QP_ALT_PATH | IB_QP_PATH_MIG_STATE; 4222 qp_attr->alt_port_num = cm_id_priv->alt_av.port->port_num; 4223 qp_attr->alt_pkey_index = cm_id_priv->alt_av.pkey_index; 4224 qp_attr->alt_timeout = cm_id_priv->alt_av.timeout; 4225 qp_attr->alt_ah_attr = cm_id_priv->alt_av.ah_attr; 4226 qp_attr->path_mig_state = IB_MIG_REARM; 4227 } 4228 ret = 0; 4229 break; 4230 default: 4231 trace_icm_qp_rts_err(&cm_id_priv->id); 4232 ret = -EINVAL; 4233 break; 4234 } 4235 spin_unlock_irqrestore(&cm_id_priv->lock, flags); 4236 return ret; 4237 } 4238 4239 int ib_cm_init_qp_attr(struct ib_cm_id *cm_id, 4240 struct ib_qp_attr *qp_attr, 4241 int *qp_attr_mask) 4242 { 4243 struct cm_id_private *cm_id_priv; 4244 int ret; 4245 4246 cm_id_priv = container_of(cm_id, struct cm_id_private, id); 4247 switch (qp_attr->qp_state) { 4248 case IB_QPS_INIT: 4249 ret = cm_init_qp_init_attr(cm_id_priv, qp_attr, qp_attr_mask); 4250 break; 4251 case IB_QPS_RTR: 4252 ret = cm_init_qp_rtr_attr(cm_id_priv, qp_attr, qp_attr_mask); 4253 break; 4254 case IB_QPS_RTS: 4255 ret = cm_init_qp_rts_attr(cm_id_priv, qp_attr, qp_attr_mask); 4256 break; 4257 default: 4258 ret = -EINVAL; 4259 break; 4260 } 4261 return ret; 4262 } 4263 EXPORT_SYMBOL(ib_cm_init_qp_attr); 4264 4265 static ssize_t cm_show_counter(struct kobject *obj, struct attribute *attr, 4266 char *buf) 4267 { 4268 struct cm_counter_group *group; 4269 struct cm_counter_attribute *cm_attr; 4270 4271 group = container_of(obj, struct cm_counter_group, obj); 4272 cm_attr = container_of(attr, struct cm_counter_attribute, attr); 4273 4274 return sysfs_emit(buf, "%ld\n", 4275 atomic_long_read(&group->counter[cm_attr->index])); 4276 } 4277 4278 static const struct sysfs_ops cm_counter_ops = { 4279 .show = cm_show_counter 4280 }; 4281 4282 static struct kobj_type cm_counter_obj_type = { 4283 .sysfs_ops = &cm_counter_ops, 4284 .default_attrs = cm_counter_default_attrs 4285 }; 4286 4287 static int cm_create_port_fs(struct cm_port *port) 4288 { 4289 int i, ret; 4290 4291 for (i = 0; i < CM_COUNTER_GROUPS; i++) { 4292 ret = ib_port_register_module_stat(port->cm_dev->ib_device, 4293 port->port_num, 4294 &port->counter_group[i].obj, 4295 &cm_counter_obj_type, 4296 counter_group_names[i]); 4297 if (ret) 4298 goto error; 4299 } 4300 4301 return 0; 4302 4303 error: 4304 while (i--) 4305 ib_port_unregister_module_stat(&port->counter_group[i].obj); 4306 return ret; 4307 4308 } 4309 4310 static void cm_remove_port_fs(struct cm_port *port) 4311 { 4312 int i; 4313 4314 for (i = 0; i < CM_COUNTER_GROUPS; i++) 4315 ib_port_unregister_module_stat(&port->counter_group[i].obj); 4316 4317 } 4318 4319 static int cm_add_one(struct ib_device *ib_device) 4320 { 4321 struct cm_device *cm_dev; 4322 struct cm_port *port; 4323 struct ib_mad_reg_req reg_req = { 4324 .mgmt_class = IB_MGMT_CLASS_CM, 4325 .mgmt_class_version = IB_CM_CLASS_VERSION, 4326 }; 4327 struct ib_port_modify port_modify = { 4328 .set_port_cap_mask = IB_PORT_CM_SUP 4329 }; 4330 unsigned long flags; 4331 int ret; 4332 int count = 0; 4333 u32 i; 4334 4335 cm_dev = kzalloc(struct_size(cm_dev, port, ib_device->phys_port_cnt), 4336 GFP_KERNEL); 4337 if (!cm_dev) 4338 return -ENOMEM; 4339 4340 cm_dev->ib_device = ib_device; 4341 cm_dev->ack_delay = ib_device->attrs.local_ca_ack_delay; 4342 cm_dev->going_down = 0; 4343 4344 set_bit(IB_MGMT_METHOD_SEND, reg_req.method_mask); 4345 rdma_for_each_port (ib_device, i) { 4346 if (!rdma_cap_ib_cm(ib_device, i)) 4347 continue; 4348 4349 port = kzalloc(sizeof *port, GFP_KERNEL); 4350 if (!port) { 4351 ret = -ENOMEM; 4352 goto error1; 4353 } 4354 4355 cm_dev->port[i-1] = port; 4356 port->cm_dev = cm_dev; 4357 port->port_num = i; 4358 4359 INIT_LIST_HEAD(&port->cm_priv_prim_list); 4360 INIT_LIST_HEAD(&port->cm_priv_altr_list); 4361 4362 ret = cm_create_port_fs(port); 4363 if (ret) 4364 goto error1; 4365 4366 port->mad_agent = ib_register_mad_agent(ib_device, i, 4367 IB_QPT_GSI, 4368 ®_req, 4369 0, 4370 cm_send_handler, 4371 cm_recv_handler, 4372 port, 4373 0); 4374 if (IS_ERR(port->mad_agent)) { 4375 ret = PTR_ERR(port->mad_agent); 4376 goto error2; 4377 } 4378 4379 ret = ib_modify_port(ib_device, i, 0, &port_modify); 4380 if (ret) 4381 goto error3; 4382 4383 count++; 4384 } 4385 4386 if (!count) { 4387 ret = -EOPNOTSUPP; 4388 goto free; 4389 } 4390 4391 ib_set_client_data(ib_device, &cm_client, cm_dev); 4392 4393 write_lock_irqsave(&cm.device_lock, flags); 4394 list_add_tail(&cm_dev->list, &cm.device_list); 4395 write_unlock_irqrestore(&cm.device_lock, flags); 4396 return 0; 4397 4398 error3: 4399 ib_unregister_mad_agent(port->mad_agent); 4400 error2: 4401 cm_remove_port_fs(port); 4402 error1: 4403 port_modify.set_port_cap_mask = 0; 4404 port_modify.clr_port_cap_mask = IB_PORT_CM_SUP; 4405 kfree(port); 4406 while (--i) { 4407 if (!rdma_cap_ib_cm(ib_device, i)) 4408 continue; 4409 4410 port = cm_dev->port[i-1]; 4411 ib_modify_port(ib_device, port->port_num, 0, &port_modify); 4412 ib_unregister_mad_agent(port->mad_agent); 4413 cm_remove_port_fs(port); 4414 kfree(port); 4415 } 4416 free: 4417 kfree(cm_dev); 4418 return ret; 4419 } 4420 4421 static void cm_remove_one(struct ib_device *ib_device, void *client_data) 4422 { 4423 struct cm_device *cm_dev = client_data; 4424 struct cm_port *port; 4425 struct cm_id_private *cm_id_priv; 4426 struct ib_mad_agent *cur_mad_agent; 4427 struct ib_port_modify port_modify = { 4428 .clr_port_cap_mask = IB_PORT_CM_SUP 4429 }; 4430 unsigned long flags; 4431 u32 i; 4432 4433 write_lock_irqsave(&cm.device_lock, flags); 4434 list_del(&cm_dev->list); 4435 write_unlock_irqrestore(&cm.device_lock, flags); 4436 4437 spin_lock_irq(&cm.lock); 4438 cm_dev->going_down = 1; 4439 spin_unlock_irq(&cm.lock); 4440 4441 rdma_for_each_port (ib_device, i) { 4442 if (!rdma_cap_ib_cm(ib_device, i)) 4443 continue; 4444 4445 port = cm_dev->port[i-1]; 4446 ib_modify_port(ib_device, port->port_num, 0, &port_modify); 4447 /* Mark all the cm_id's as not valid */ 4448 spin_lock_irq(&cm.lock); 4449 list_for_each_entry(cm_id_priv, &port->cm_priv_altr_list, altr_list) 4450 cm_id_priv->altr_send_port_not_ready = 1; 4451 list_for_each_entry(cm_id_priv, &port->cm_priv_prim_list, prim_list) 4452 cm_id_priv->prim_send_port_not_ready = 1; 4453 spin_unlock_irq(&cm.lock); 4454 /* 4455 * We flush the queue here after the going_down set, this 4456 * verify that no new works will be queued in the recv handler, 4457 * after that we can call the unregister_mad_agent 4458 */ 4459 flush_workqueue(cm.wq); 4460 spin_lock_irq(&cm.state_lock); 4461 cur_mad_agent = port->mad_agent; 4462 port->mad_agent = NULL; 4463 spin_unlock_irq(&cm.state_lock); 4464 ib_unregister_mad_agent(cur_mad_agent); 4465 cm_remove_port_fs(port); 4466 kfree(port); 4467 } 4468 4469 kfree(cm_dev); 4470 } 4471 4472 static int __init ib_cm_init(void) 4473 { 4474 int ret; 4475 4476 INIT_LIST_HEAD(&cm.device_list); 4477 rwlock_init(&cm.device_lock); 4478 spin_lock_init(&cm.lock); 4479 spin_lock_init(&cm.state_lock); 4480 cm.listen_service_table = RB_ROOT; 4481 cm.listen_service_id = be64_to_cpu(IB_CM_ASSIGN_SERVICE_ID); 4482 cm.remote_id_table = RB_ROOT; 4483 cm.remote_qp_table = RB_ROOT; 4484 cm.remote_sidr_table = RB_ROOT; 4485 xa_init_flags(&cm.local_id_table, XA_FLAGS_ALLOC); 4486 get_random_bytes(&cm.random_id_operand, sizeof cm.random_id_operand); 4487 INIT_LIST_HEAD(&cm.timewait_list); 4488 4489 cm.wq = alloc_workqueue("ib_cm", 0, 1); 4490 if (!cm.wq) { 4491 ret = -ENOMEM; 4492 goto error2; 4493 } 4494 4495 ret = ib_register_client(&cm_client); 4496 if (ret) 4497 goto error3; 4498 4499 return 0; 4500 error3: 4501 destroy_workqueue(cm.wq); 4502 error2: 4503 return ret; 4504 } 4505 4506 static void __exit ib_cm_cleanup(void) 4507 { 4508 struct cm_timewait_info *timewait_info, *tmp; 4509 4510 spin_lock_irq(&cm.lock); 4511 list_for_each_entry(timewait_info, &cm.timewait_list, list) 4512 cancel_delayed_work(&timewait_info->work.work); 4513 spin_unlock_irq(&cm.lock); 4514 4515 ib_unregister_client(&cm_client); 4516 destroy_workqueue(cm.wq); 4517 4518 list_for_each_entry_safe(timewait_info, tmp, &cm.timewait_list, list) { 4519 list_del(&timewait_info->list); 4520 kfree(timewait_info); 4521 } 4522 4523 WARN_ON(!xa_empty(&cm.local_id_table)); 4524 } 4525 4526 module_init(ib_cm_init); 4527 module_exit(ib_cm_cleanup); 4528