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