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