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