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