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