1 /* 2 * Copyright (c) 2006, 2019 Oracle and/or its affiliates. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 */ 33 #include <linux/kernel.h> 34 #include <linux/in.h> 35 #include <linux/slab.h> 36 #include <linux/vmalloc.h> 37 #include <linux/ratelimit.h> 38 #include <net/addrconf.h> 39 #include <rdma/ib_cm.h> 40 41 #include "rds_single_path.h" 42 #include "rds.h" 43 #include "ib.h" 44 #include "ib_mr.h" 45 46 /* 47 * Set the selected protocol version 48 */ 49 static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version) 50 { 51 conn->c_version = version; 52 } 53 54 /* 55 * Set up flow control 56 */ 57 static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits) 58 { 59 struct rds_ib_connection *ic = conn->c_transport_data; 60 61 if (rds_ib_sysctl_flow_control && credits != 0) { 62 /* We're doing flow control */ 63 ic->i_flowctl = 1; 64 rds_ib_send_add_credits(conn, credits); 65 } else { 66 ic->i_flowctl = 0; 67 } 68 } 69 70 /* 71 * Tune RNR behavior. Without flow control, we use a rather 72 * low timeout, but not the absolute minimum - this should 73 * be tunable. 74 * 75 * We already set the RNR retry count to 7 (which is the 76 * smallest infinite number :-) above. 77 * If flow control is off, we want to change this back to 0 78 * so that we learn quickly when our credit accounting is 79 * buggy. 80 * 81 * Caller passes in a qp_attr pointer - don't waste stack spacv 82 * by allocation this twice. 83 */ 84 static void 85 rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr) 86 { 87 int ret; 88 89 attr->min_rnr_timer = IB_RNR_TIMER_000_32; 90 ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER); 91 if (ret) 92 printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret); 93 } 94 95 /* 96 * Connection established. 97 * We get here for both outgoing and incoming connection. 98 */ 99 void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event) 100 { 101 struct rds_ib_connection *ic = conn->c_transport_data; 102 const union rds_ib_conn_priv *dp = NULL; 103 struct ib_qp_attr qp_attr; 104 __be64 ack_seq = 0; 105 __be32 credit = 0; 106 u8 major = 0; 107 u8 minor = 0; 108 int err; 109 110 dp = event->param.conn.private_data; 111 if (conn->c_isv6) { 112 if (event->param.conn.private_data_len >= 113 sizeof(struct rds6_ib_connect_private)) { 114 major = dp->ricp_v6.dp_protocol_major; 115 minor = dp->ricp_v6.dp_protocol_minor; 116 credit = dp->ricp_v6.dp_credit; 117 /* dp structure start is not guaranteed to be 8 bytes 118 * aligned. Since dp_ack_seq is 64-bit extended load 119 * operations can be used so go through get_unaligned 120 * to avoid unaligned errors. 121 */ 122 ack_seq = get_unaligned(&dp->ricp_v6.dp_ack_seq); 123 } 124 } else if (event->param.conn.private_data_len >= 125 sizeof(struct rds_ib_connect_private)) { 126 major = dp->ricp_v4.dp_protocol_major; 127 minor = dp->ricp_v4.dp_protocol_minor; 128 credit = dp->ricp_v4.dp_credit; 129 ack_seq = get_unaligned(&dp->ricp_v4.dp_ack_seq); 130 } 131 132 /* make sure it isn't empty data */ 133 if (major) { 134 rds_ib_set_protocol(conn, RDS_PROTOCOL(major, minor)); 135 rds_ib_set_flow_control(conn, be32_to_cpu(credit)); 136 } 137 138 if (conn->c_version < RDS_PROTOCOL_VERSION) { 139 if (conn->c_version != RDS_PROTOCOL_COMPAT_VERSION) { 140 pr_notice("RDS/IB: Connection <%pI6c,%pI6c> version %u.%u no longer supported\n", 141 &conn->c_laddr, &conn->c_faddr, 142 RDS_PROTOCOL_MAJOR(conn->c_version), 143 RDS_PROTOCOL_MINOR(conn->c_version)); 144 rds_conn_destroy(conn); 145 return; 146 } 147 } 148 149 pr_notice("RDS/IB: %s conn connected <%pI6c,%pI6c,%d> version %u.%u%s\n", 150 ic->i_active_side ? "Active" : "Passive", 151 &conn->c_laddr, &conn->c_faddr, conn->c_tos, 152 RDS_PROTOCOL_MAJOR(conn->c_version), 153 RDS_PROTOCOL_MINOR(conn->c_version), 154 ic->i_flowctl ? ", flow control" : ""); 155 156 /* receive sl from the peer */ 157 ic->i_sl = ic->i_cm_id->route.path_rec->sl; 158 159 atomic_set(&ic->i_cq_quiesce, 0); 160 161 /* Init rings and fill recv. this needs to wait until protocol 162 * negotiation is complete, since ring layout is different 163 * from 3.1 to 4.1. 164 */ 165 rds_ib_send_init_ring(ic); 166 rds_ib_recv_init_ring(ic); 167 /* Post receive buffers - as a side effect, this will update 168 * the posted credit count. */ 169 rds_ib_recv_refill(conn, 1, GFP_KERNEL); 170 171 /* Tune RNR behavior */ 172 rds_ib_tune_rnr(ic, &qp_attr); 173 174 qp_attr.qp_state = IB_QPS_RTS; 175 err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE); 176 if (err) 177 printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err); 178 179 /* update ib_device with this local ipaddr */ 180 err = rds_ib_update_ipaddr(ic->rds_ibdev, &conn->c_laddr); 181 if (err) 182 printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n", 183 err); 184 185 /* If the peer gave us the last packet it saw, process this as if 186 * we had received a regular ACK. */ 187 if (dp) { 188 if (ack_seq) 189 rds_send_drop_acked(conn, be64_to_cpu(ack_seq), 190 NULL); 191 } 192 193 conn->c_proposed_version = conn->c_version; 194 rds_connect_complete(conn); 195 } 196 197 static void rds_ib_cm_fill_conn_param(struct rds_connection *conn, 198 struct rdma_conn_param *conn_param, 199 union rds_ib_conn_priv *dp, 200 u32 protocol_version, 201 u32 max_responder_resources, 202 u32 max_initiator_depth, 203 bool isv6) 204 { 205 struct rds_ib_connection *ic = conn->c_transport_data; 206 struct rds_ib_device *rds_ibdev = ic->rds_ibdev; 207 208 memset(conn_param, 0, sizeof(struct rdma_conn_param)); 209 210 conn_param->responder_resources = 211 min_t(u32, rds_ibdev->max_responder_resources, max_responder_resources); 212 conn_param->initiator_depth = 213 min_t(u32, rds_ibdev->max_initiator_depth, max_initiator_depth); 214 conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7); 215 conn_param->rnr_retry_count = 7; 216 217 if (dp) { 218 memset(dp, 0, sizeof(*dp)); 219 if (isv6) { 220 dp->ricp_v6.dp_saddr = conn->c_laddr; 221 dp->ricp_v6.dp_daddr = conn->c_faddr; 222 dp->ricp_v6.dp_protocol_major = 223 RDS_PROTOCOL_MAJOR(protocol_version); 224 dp->ricp_v6.dp_protocol_minor = 225 RDS_PROTOCOL_MINOR(protocol_version); 226 dp->ricp_v6.dp_protocol_minor_mask = 227 cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS); 228 dp->ricp_v6.dp_ack_seq = 229 cpu_to_be64(rds_ib_piggyb_ack(ic)); 230 dp->ricp_v6.dp_cmn.ricpc_dp_toss = conn->c_tos; 231 232 conn_param->private_data = &dp->ricp_v6; 233 conn_param->private_data_len = sizeof(dp->ricp_v6); 234 } else { 235 dp->ricp_v4.dp_saddr = conn->c_laddr.s6_addr32[3]; 236 dp->ricp_v4.dp_daddr = conn->c_faddr.s6_addr32[3]; 237 dp->ricp_v4.dp_protocol_major = 238 RDS_PROTOCOL_MAJOR(protocol_version); 239 dp->ricp_v4.dp_protocol_minor = 240 RDS_PROTOCOL_MINOR(protocol_version); 241 dp->ricp_v4.dp_protocol_minor_mask = 242 cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS); 243 dp->ricp_v4.dp_ack_seq = 244 cpu_to_be64(rds_ib_piggyb_ack(ic)); 245 dp->ricp_v4.dp_cmn.ricpc_dp_toss = conn->c_tos; 246 247 conn_param->private_data = &dp->ricp_v4; 248 conn_param->private_data_len = sizeof(dp->ricp_v4); 249 } 250 251 /* Advertise flow control */ 252 if (ic->i_flowctl) { 253 unsigned int credits; 254 255 credits = IB_GET_POST_CREDITS 256 (atomic_read(&ic->i_credits)); 257 if (isv6) 258 dp->ricp_v6.dp_credit = cpu_to_be32(credits); 259 else 260 dp->ricp_v4.dp_credit = cpu_to_be32(credits); 261 atomic_sub(IB_SET_POST_CREDITS(credits), 262 &ic->i_credits); 263 } 264 } 265 } 266 267 static void rds_ib_cq_event_handler(struct ib_event *event, void *data) 268 { 269 rdsdebug("event %u (%s) data %p\n", 270 event->event, ib_event_msg(event->event), data); 271 } 272 273 /* Plucking the oldest entry from the ring can be done concurrently with 274 * the thread refilling the ring. Each ring operation is protected by 275 * spinlocks and the transient state of refilling doesn't change the 276 * recording of which entry is oldest. 277 * 278 * This relies on IB only calling one cq comp_handler for each cq so that 279 * there will only be one caller of rds_recv_incoming() per RDS connection. 280 */ 281 static void rds_ib_cq_comp_handler_recv(struct ib_cq *cq, void *context) 282 { 283 struct rds_connection *conn = context; 284 struct rds_ib_connection *ic = conn->c_transport_data; 285 286 rdsdebug("conn %p cq %p\n", conn, cq); 287 288 rds_ib_stats_inc(s_ib_evt_handler_call); 289 290 tasklet_schedule(&ic->i_recv_tasklet); 291 } 292 293 static void poll_scq(struct rds_ib_connection *ic, struct ib_cq *cq, 294 struct ib_wc *wcs) 295 { 296 int nr, i; 297 struct ib_wc *wc; 298 299 while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) { 300 for (i = 0; i < nr; i++) { 301 wc = wcs + i; 302 rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n", 303 (unsigned long long)wc->wr_id, wc->status, 304 wc->byte_len, be32_to_cpu(wc->ex.imm_data)); 305 306 if (wc->wr_id <= ic->i_send_ring.w_nr || 307 wc->wr_id == RDS_IB_ACK_WR_ID) 308 rds_ib_send_cqe_handler(ic, wc); 309 else 310 rds_ib_mr_cqe_handler(ic, wc); 311 312 } 313 } 314 } 315 316 static void rds_ib_tasklet_fn_send(unsigned long data) 317 { 318 struct rds_ib_connection *ic = (struct rds_ib_connection *)data; 319 struct rds_connection *conn = ic->conn; 320 321 rds_ib_stats_inc(s_ib_tasklet_call); 322 323 /* if cq has been already reaped, ignore incoming cq event */ 324 if (atomic_read(&ic->i_cq_quiesce)) 325 return; 326 327 poll_scq(ic, ic->i_send_cq, ic->i_send_wc); 328 ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP); 329 poll_scq(ic, ic->i_send_cq, ic->i_send_wc); 330 331 if (rds_conn_up(conn) && 332 (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags) || 333 test_bit(0, &conn->c_map_queued))) 334 rds_send_xmit(&ic->conn->c_path[0]); 335 } 336 337 static void poll_rcq(struct rds_ib_connection *ic, struct ib_cq *cq, 338 struct ib_wc *wcs, 339 struct rds_ib_ack_state *ack_state) 340 { 341 int nr, i; 342 struct ib_wc *wc; 343 344 while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) { 345 for (i = 0; i < nr; i++) { 346 wc = wcs + i; 347 rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n", 348 (unsigned long long)wc->wr_id, wc->status, 349 wc->byte_len, be32_to_cpu(wc->ex.imm_data)); 350 351 rds_ib_recv_cqe_handler(ic, wc, ack_state); 352 } 353 } 354 } 355 356 static void rds_ib_tasklet_fn_recv(unsigned long data) 357 { 358 struct rds_ib_connection *ic = (struct rds_ib_connection *)data; 359 struct rds_connection *conn = ic->conn; 360 struct rds_ib_device *rds_ibdev = ic->rds_ibdev; 361 struct rds_ib_ack_state state; 362 363 if (!rds_ibdev) 364 rds_conn_drop(conn); 365 366 rds_ib_stats_inc(s_ib_tasklet_call); 367 368 /* if cq has been already reaped, ignore incoming cq event */ 369 if (atomic_read(&ic->i_cq_quiesce)) 370 return; 371 372 memset(&state, 0, sizeof(state)); 373 poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state); 374 ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED); 375 poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state); 376 377 if (state.ack_next_valid) 378 rds_ib_set_ack(ic, state.ack_next, state.ack_required); 379 if (state.ack_recv_valid && state.ack_recv > ic->i_ack_recv) { 380 rds_send_drop_acked(conn, state.ack_recv, NULL); 381 ic->i_ack_recv = state.ack_recv; 382 } 383 384 if (rds_conn_up(conn)) 385 rds_ib_attempt_ack(ic); 386 } 387 388 static void rds_ib_qp_event_handler(struct ib_event *event, void *data) 389 { 390 struct rds_connection *conn = data; 391 struct rds_ib_connection *ic = conn->c_transport_data; 392 393 rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event, 394 ib_event_msg(event->event)); 395 396 switch (event->event) { 397 case IB_EVENT_COMM_EST: 398 rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST); 399 break; 400 default: 401 rdsdebug("Fatal QP Event %u (%s) - connection %pI6c->%pI6c, reconnecting\n", 402 event->event, ib_event_msg(event->event), 403 &conn->c_laddr, &conn->c_faddr); 404 rds_conn_drop(conn); 405 break; 406 } 407 } 408 409 static void rds_ib_cq_comp_handler_send(struct ib_cq *cq, void *context) 410 { 411 struct rds_connection *conn = context; 412 struct rds_ib_connection *ic = conn->c_transport_data; 413 414 rdsdebug("conn %p cq %p\n", conn, cq); 415 416 rds_ib_stats_inc(s_ib_evt_handler_call); 417 418 tasklet_schedule(&ic->i_send_tasklet); 419 } 420 421 static inline int ibdev_get_unused_vector(struct rds_ib_device *rds_ibdev) 422 { 423 int min = rds_ibdev->vector_load[rds_ibdev->dev->num_comp_vectors - 1]; 424 int index = rds_ibdev->dev->num_comp_vectors - 1; 425 int i; 426 427 for (i = rds_ibdev->dev->num_comp_vectors - 1; i >= 0; i--) { 428 if (rds_ibdev->vector_load[i] < min) { 429 index = i; 430 min = rds_ibdev->vector_load[i]; 431 } 432 } 433 434 rds_ibdev->vector_load[index]++; 435 return index; 436 } 437 438 static inline void ibdev_put_vector(struct rds_ib_device *rds_ibdev, int index) 439 { 440 rds_ibdev->vector_load[index]--; 441 } 442 443 static void rds_dma_hdr_free(struct ib_device *dev, struct rds_header *hdr, 444 dma_addr_t dma_addr, enum dma_data_direction dir) 445 { 446 ib_dma_unmap_single(dev, dma_addr, sizeof(*hdr), dir); 447 kfree(hdr); 448 } 449 450 static struct rds_header *rds_dma_hdr_alloc(struct ib_device *dev, 451 dma_addr_t *dma_addr, enum dma_data_direction dir) 452 { 453 struct rds_header *hdr; 454 455 hdr = kzalloc_node(sizeof(*hdr), GFP_KERNEL, ibdev_to_node(dev)); 456 if (!hdr) 457 return NULL; 458 459 *dma_addr = ib_dma_map_single(dev, hdr, sizeof(*hdr), 460 DMA_BIDIRECTIONAL); 461 if (ib_dma_mapping_error(dev, *dma_addr)) { 462 kfree(hdr); 463 return NULL; 464 } 465 466 return hdr; 467 } 468 469 /* Free the DMA memory used to store struct rds_header. 470 * 471 * @dev: the RDS IB device 472 * @hdrs: pointer to the array storing DMA memory pointers 473 * @dma_addrs: pointer to the array storing DMA addresses 474 * @num_hdars: number of headers to free. 475 */ 476 static void rds_dma_hdrs_free(struct rds_ib_device *dev, 477 struct rds_header **hdrs, dma_addr_t *dma_addrs, u32 num_hdrs, 478 enum dma_data_direction dir) 479 { 480 u32 i; 481 482 for (i = 0; i < num_hdrs; i++) 483 rds_dma_hdr_free(dev->dev, hdrs[i], dma_addrs[i], dir); 484 kvfree(hdrs); 485 kvfree(dma_addrs); 486 } 487 488 489 /* Allocate DMA coherent memory to be used to store struct rds_header for 490 * sending/receiving packets. The pointers to the DMA memory and the 491 * associated DMA addresses are stored in two arrays. 492 * 493 * @dev: the RDS IB device 494 * @dma_addrs: pointer to the array for storing DMA addresses 495 * @num_hdrs: number of headers to allocate 496 * 497 * It returns the pointer to the array storing the DMA memory pointers. On 498 * error, NULL pointer is returned. 499 */ 500 static struct rds_header **rds_dma_hdrs_alloc(struct rds_ib_device *dev, 501 dma_addr_t **dma_addrs, u32 num_hdrs, 502 enum dma_data_direction dir) 503 { 504 struct rds_header **hdrs; 505 dma_addr_t *hdr_daddrs; 506 u32 i; 507 508 hdrs = kvmalloc_node(sizeof(*hdrs) * num_hdrs, GFP_KERNEL, 509 ibdev_to_node(dev->dev)); 510 if (!hdrs) 511 return NULL; 512 513 hdr_daddrs = kvmalloc_node(sizeof(*hdr_daddrs) * num_hdrs, GFP_KERNEL, 514 ibdev_to_node(dev->dev)); 515 if (!hdr_daddrs) { 516 kvfree(hdrs); 517 return NULL; 518 } 519 520 for (i = 0; i < num_hdrs; i++) { 521 hdrs[i] = rds_dma_hdr_alloc(dev->dev, &hdr_daddrs[i], dir); 522 if (!hdrs[i]) { 523 rds_dma_hdrs_free(dev, hdrs, hdr_daddrs, i, dir); 524 return NULL; 525 } 526 } 527 528 *dma_addrs = hdr_daddrs; 529 return hdrs; 530 } 531 532 /* 533 * This needs to be very careful to not leave IS_ERR pointers around for 534 * cleanup to trip over. 535 */ 536 static int rds_ib_setup_qp(struct rds_connection *conn) 537 { 538 struct rds_ib_connection *ic = conn->c_transport_data; 539 struct ib_device *dev = ic->i_cm_id->device; 540 struct ib_qp_init_attr attr; 541 struct ib_cq_init_attr cq_attr = {}; 542 struct rds_ib_device *rds_ibdev; 543 unsigned long max_wrs; 544 int ret, fr_queue_space; 545 546 /* 547 * It's normal to see a null device if an incoming connection races 548 * with device removal, so we don't print a warning. 549 */ 550 rds_ibdev = rds_ib_get_client_data(dev); 551 if (!rds_ibdev) 552 return -EOPNOTSUPP; 553 554 /* The fr_queue_space is currently set to 512, to add extra space on 555 * completion queue and send queue. This extra space is used for FRWR 556 * registration and invalidation work requests 557 */ 558 fr_queue_space = RDS_IB_DEFAULT_FR_WR; 559 560 /* add the conn now so that connection establishment has the dev */ 561 rds_ib_add_conn(rds_ibdev, conn); 562 563 max_wrs = rds_ibdev->max_wrs < rds_ib_sysctl_max_send_wr + 1 ? 564 rds_ibdev->max_wrs - 1 : rds_ib_sysctl_max_send_wr; 565 if (ic->i_send_ring.w_nr != max_wrs) 566 rds_ib_ring_resize(&ic->i_send_ring, max_wrs); 567 568 max_wrs = rds_ibdev->max_wrs < rds_ib_sysctl_max_recv_wr + 1 ? 569 rds_ibdev->max_wrs - 1 : rds_ib_sysctl_max_recv_wr; 570 if (ic->i_recv_ring.w_nr != max_wrs) 571 rds_ib_ring_resize(&ic->i_recv_ring, max_wrs); 572 573 /* Protection domain and memory range */ 574 ic->i_pd = rds_ibdev->pd; 575 576 ic->i_scq_vector = ibdev_get_unused_vector(rds_ibdev); 577 cq_attr.cqe = ic->i_send_ring.w_nr + fr_queue_space + 1; 578 cq_attr.comp_vector = ic->i_scq_vector; 579 ic->i_send_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_send, 580 rds_ib_cq_event_handler, conn, 581 &cq_attr); 582 if (IS_ERR(ic->i_send_cq)) { 583 ret = PTR_ERR(ic->i_send_cq); 584 ic->i_send_cq = NULL; 585 ibdev_put_vector(rds_ibdev, ic->i_scq_vector); 586 rdsdebug("ib_create_cq send failed: %d\n", ret); 587 goto rds_ibdev_out; 588 } 589 590 ic->i_rcq_vector = ibdev_get_unused_vector(rds_ibdev); 591 cq_attr.cqe = ic->i_recv_ring.w_nr; 592 cq_attr.comp_vector = ic->i_rcq_vector; 593 ic->i_recv_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_recv, 594 rds_ib_cq_event_handler, conn, 595 &cq_attr); 596 if (IS_ERR(ic->i_recv_cq)) { 597 ret = PTR_ERR(ic->i_recv_cq); 598 ic->i_recv_cq = NULL; 599 ibdev_put_vector(rds_ibdev, ic->i_rcq_vector); 600 rdsdebug("ib_create_cq recv failed: %d\n", ret); 601 goto send_cq_out; 602 } 603 604 ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP); 605 if (ret) { 606 rdsdebug("ib_req_notify_cq send failed: %d\n", ret); 607 goto recv_cq_out; 608 } 609 610 ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED); 611 if (ret) { 612 rdsdebug("ib_req_notify_cq recv failed: %d\n", ret); 613 goto recv_cq_out; 614 } 615 616 /* XXX negotiate max send/recv with remote? */ 617 memset(&attr, 0, sizeof(attr)); 618 attr.event_handler = rds_ib_qp_event_handler; 619 attr.qp_context = conn; 620 /* + 1 to allow for the single ack message */ 621 attr.cap.max_send_wr = ic->i_send_ring.w_nr + fr_queue_space + 1; 622 attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1; 623 attr.cap.max_send_sge = rds_ibdev->max_sge; 624 attr.cap.max_recv_sge = RDS_IB_RECV_SGE; 625 attr.sq_sig_type = IB_SIGNAL_REQ_WR; 626 attr.qp_type = IB_QPT_RC; 627 attr.send_cq = ic->i_send_cq; 628 attr.recv_cq = ic->i_recv_cq; 629 630 /* 631 * XXX this can fail if max_*_wr is too large? Are we supposed 632 * to back off until we get a value that the hardware can support? 633 */ 634 ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr); 635 if (ret) { 636 rdsdebug("rdma_create_qp failed: %d\n", ret); 637 goto recv_cq_out; 638 } 639 640 ic->i_send_hdrs = rds_dma_hdrs_alloc(rds_ibdev, &ic->i_send_hdrs_dma, 641 ic->i_send_ring.w_nr, 642 DMA_TO_DEVICE); 643 if (!ic->i_send_hdrs) { 644 ret = -ENOMEM; 645 rdsdebug("DMA send hdrs alloc failed\n"); 646 goto qp_out; 647 } 648 649 ic->i_recv_hdrs = rds_dma_hdrs_alloc(rds_ibdev, &ic->i_recv_hdrs_dma, 650 ic->i_recv_ring.w_nr, 651 DMA_FROM_DEVICE); 652 if (!ic->i_recv_hdrs) { 653 ret = -ENOMEM; 654 rdsdebug("DMA recv hdrs alloc failed\n"); 655 goto send_hdrs_dma_out; 656 } 657 658 ic->i_ack = rds_dma_hdr_alloc(rds_ibdev->dev, &ic->i_ack_dma, 659 DMA_TO_DEVICE); 660 if (!ic->i_ack) { 661 ret = -ENOMEM; 662 rdsdebug("DMA ack header alloc failed\n"); 663 goto recv_hdrs_dma_out; 664 } 665 666 ic->i_sends = vzalloc_node(array_size(sizeof(struct rds_ib_send_work), 667 ic->i_send_ring.w_nr), 668 ibdev_to_node(dev)); 669 if (!ic->i_sends) { 670 ret = -ENOMEM; 671 rdsdebug("send allocation failed\n"); 672 goto ack_dma_out; 673 } 674 675 ic->i_recvs = vzalloc_node(array_size(sizeof(struct rds_ib_recv_work), 676 ic->i_recv_ring.w_nr), 677 ibdev_to_node(dev)); 678 if (!ic->i_recvs) { 679 ret = -ENOMEM; 680 rdsdebug("recv allocation failed\n"); 681 goto sends_out; 682 } 683 684 rds_ib_recv_init_ack(ic); 685 686 rdsdebug("conn %p pd %p cq %p %p\n", conn, ic->i_pd, 687 ic->i_send_cq, ic->i_recv_cq); 688 689 goto out; 690 691 sends_out: 692 vfree(ic->i_sends); 693 694 ack_dma_out: 695 rds_dma_hdr_free(rds_ibdev->dev, ic->i_ack, ic->i_ack_dma, 696 DMA_TO_DEVICE); 697 ic->i_ack = NULL; 698 699 recv_hdrs_dma_out: 700 rds_dma_hdrs_free(rds_ibdev, ic->i_recv_hdrs, ic->i_recv_hdrs_dma, 701 ic->i_recv_ring.w_nr, DMA_FROM_DEVICE); 702 ic->i_recv_hdrs = NULL; 703 ic->i_recv_hdrs_dma = NULL; 704 705 send_hdrs_dma_out: 706 rds_dma_hdrs_free(rds_ibdev, ic->i_send_hdrs, ic->i_send_hdrs_dma, 707 ic->i_send_ring.w_nr, DMA_TO_DEVICE); 708 ic->i_send_hdrs = NULL; 709 ic->i_send_hdrs_dma = NULL; 710 711 qp_out: 712 rdma_destroy_qp(ic->i_cm_id); 713 recv_cq_out: 714 ib_destroy_cq(ic->i_recv_cq); 715 ic->i_recv_cq = NULL; 716 send_cq_out: 717 ib_destroy_cq(ic->i_send_cq); 718 ic->i_send_cq = NULL; 719 rds_ibdev_out: 720 rds_ib_remove_conn(rds_ibdev, conn); 721 out: 722 rds_ib_dev_put(rds_ibdev); 723 724 return ret; 725 } 726 727 static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event, bool isv6) 728 { 729 const union rds_ib_conn_priv *dp = event->param.conn.private_data; 730 u8 data_len, major, minor; 731 u32 version = 0; 732 __be16 mask; 733 u16 common; 734 735 /* 736 * rdma_cm private data is odd - when there is any private data in the 737 * request, we will be given a pretty large buffer without telling us the 738 * original size. The only way to tell the difference is by looking at 739 * the contents, which are initialized to zero. 740 * If the protocol version fields aren't set, this is a connection attempt 741 * from an older version. This could be 3.0 or 2.0 - we can't tell. 742 * We really should have changed this for OFED 1.3 :-( 743 */ 744 745 /* Be paranoid. RDS always has privdata */ 746 if (!event->param.conn.private_data_len) { 747 printk(KERN_NOTICE "RDS incoming connection has no private data, " 748 "rejecting\n"); 749 return 0; 750 } 751 752 if (isv6) { 753 data_len = sizeof(struct rds6_ib_connect_private); 754 major = dp->ricp_v6.dp_protocol_major; 755 minor = dp->ricp_v6.dp_protocol_minor; 756 mask = dp->ricp_v6.dp_protocol_minor_mask; 757 } else { 758 data_len = sizeof(struct rds_ib_connect_private); 759 major = dp->ricp_v4.dp_protocol_major; 760 minor = dp->ricp_v4.dp_protocol_minor; 761 mask = dp->ricp_v4.dp_protocol_minor_mask; 762 } 763 764 /* Even if len is crap *now* I still want to check it. -ASG */ 765 if (event->param.conn.private_data_len < data_len || major == 0) 766 return RDS_PROTOCOL_4_0; 767 768 common = be16_to_cpu(mask) & RDS_IB_SUPPORTED_PROTOCOLS; 769 if (major == 4 && common) { 770 version = RDS_PROTOCOL_4_0; 771 while ((common >>= 1) != 0) 772 version++; 773 } else if (RDS_PROTOCOL_COMPAT_VERSION == 774 RDS_PROTOCOL(major, minor)) { 775 version = RDS_PROTOCOL_COMPAT_VERSION; 776 } else { 777 if (isv6) 778 printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI6c using incompatible protocol version %u.%u\n", 779 &dp->ricp_v6.dp_saddr, major, minor); 780 else 781 printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using incompatible protocol version %u.%u\n", 782 &dp->ricp_v4.dp_saddr, major, minor); 783 } 784 return version; 785 } 786 787 #if IS_ENABLED(CONFIG_IPV6) 788 /* Given an IPv6 address, find the net_device which hosts that address and 789 * return its index. This is used by the rds_ib_cm_handle_connect() code to 790 * find the interface index of where an incoming request comes from when 791 * the request is using a link local address. 792 * 793 * Note one problem in this search. It is possible that two interfaces have 794 * the same link local address. Unfortunately, this cannot be solved unless 795 * the underlying layer gives us the interface which an incoming RDMA connect 796 * request comes from. 797 */ 798 static u32 __rds_find_ifindex(struct net *net, const struct in6_addr *addr) 799 { 800 struct net_device *dev; 801 int idx = 0; 802 803 rcu_read_lock(); 804 for_each_netdev_rcu(net, dev) { 805 if (ipv6_chk_addr(net, addr, dev, 1)) { 806 idx = dev->ifindex; 807 break; 808 } 809 } 810 rcu_read_unlock(); 811 812 return idx; 813 } 814 #endif 815 816 int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id, 817 struct rdma_cm_event *event, bool isv6) 818 { 819 __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id; 820 __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id; 821 const struct rds_ib_conn_priv_cmn *dp_cmn; 822 struct rds_connection *conn = NULL; 823 struct rds_ib_connection *ic = NULL; 824 struct rdma_conn_param conn_param; 825 const union rds_ib_conn_priv *dp; 826 union rds_ib_conn_priv dp_rep; 827 struct in6_addr s_mapped_addr; 828 struct in6_addr d_mapped_addr; 829 const struct in6_addr *saddr6; 830 const struct in6_addr *daddr6; 831 int destroy = 1; 832 u32 ifindex = 0; 833 u32 version; 834 int err = 1; 835 836 /* Check whether the remote protocol version matches ours. */ 837 version = rds_ib_protocol_compatible(event, isv6); 838 if (!version) { 839 err = RDS_RDMA_REJ_INCOMPAT; 840 goto out; 841 } 842 843 dp = event->param.conn.private_data; 844 if (isv6) { 845 #if IS_ENABLED(CONFIG_IPV6) 846 dp_cmn = &dp->ricp_v6.dp_cmn; 847 saddr6 = &dp->ricp_v6.dp_saddr; 848 daddr6 = &dp->ricp_v6.dp_daddr; 849 /* If either address is link local, need to find the 850 * interface index in order to create a proper RDS 851 * connection. 852 */ 853 if (ipv6_addr_type(daddr6) & IPV6_ADDR_LINKLOCAL) { 854 /* Using init_net for now .. */ 855 ifindex = __rds_find_ifindex(&init_net, daddr6); 856 /* No index found... Need to bail out. */ 857 if (ifindex == 0) { 858 err = -EOPNOTSUPP; 859 goto out; 860 } 861 } else if (ipv6_addr_type(saddr6) & IPV6_ADDR_LINKLOCAL) { 862 /* Use our address to find the correct index. */ 863 ifindex = __rds_find_ifindex(&init_net, daddr6); 864 /* No index found... Need to bail out. */ 865 if (ifindex == 0) { 866 err = -EOPNOTSUPP; 867 goto out; 868 } 869 } 870 #else 871 err = -EOPNOTSUPP; 872 goto out; 873 #endif 874 } else { 875 dp_cmn = &dp->ricp_v4.dp_cmn; 876 ipv6_addr_set_v4mapped(dp->ricp_v4.dp_saddr, &s_mapped_addr); 877 ipv6_addr_set_v4mapped(dp->ricp_v4.dp_daddr, &d_mapped_addr); 878 saddr6 = &s_mapped_addr; 879 daddr6 = &d_mapped_addr; 880 } 881 882 rdsdebug("saddr %pI6c daddr %pI6c RDSv%u.%u lguid 0x%llx fguid 0x%llx, tos:%d\n", 883 saddr6, daddr6, RDS_PROTOCOL_MAJOR(version), 884 RDS_PROTOCOL_MINOR(version), 885 (unsigned long long)be64_to_cpu(lguid), 886 (unsigned long long)be64_to_cpu(fguid), dp_cmn->ricpc_dp_toss); 887 888 /* RDS/IB is not currently netns aware, thus init_net */ 889 conn = rds_conn_create(&init_net, daddr6, saddr6, 890 &rds_ib_transport, dp_cmn->ricpc_dp_toss, 891 GFP_KERNEL, ifindex); 892 if (IS_ERR(conn)) { 893 rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn)); 894 conn = NULL; 895 goto out; 896 } 897 898 /* 899 * The connection request may occur while the 900 * previous connection exist, e.g. in case of failover. 901 * But as connections may be initiated simultaneously 902 * by both hosts, we have a random backoff mechanism - 903 * see the comment above rds_queue_reconnect() 904 */ 905 mutex_lock(&conn->c_cm_lock); 906 if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) { 907 if (rds_conn_state(conn) == RDS_CONN_UP) { 908 rdsdebug("incoming connect while connecting\n"); 909 rds_conn_drop(conn); 910 rds_ib_stats_inc(s_ib_listen_closed_stale); 911 } else 912 if (rds_conn_state(conn) == RDS_CONN_CONNECTING) { 913 /* Wait and see - our connect may still be succeeding */ 914 rds_ib_stats_inc(s_ib_connect_raced); 915 } 916 goto out; 917 } 918 919 ic = conn->c_transport_data; 920 921 rds_ib_set_protocol(conn, version); 922 rds_ib_set_flow_control(conn, be32_to_cpu(dp_cmn->ricpc_credit)); 923 924 /* If the peer gave us the last packet it saw, process this as if 925 * we had received a regular ACK. */ 926 if (dp_cmn->ricpc_ack_seq) 927 rds_send_drop_acked(conn, be64_to_cpu(dp_cmn->ricpc_ack_seq), 928 NULL); 929 930 BUG_ON(cm_id->context); 931 BUG_ON(ic->i_cm_id); 932 933 ic->i_cm_id = cm_id; 934 cm_id->context = conn; 935 936 /* We got halfway through setting up the ib_connection, if we 937 * fail now, we have to take the long route out of this mess. */ 938 destroy = 0; 939 940 err = rds_ib_setup_qp(conn); 941 if (err) { 942 rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err); 943 goto out; 944 } 945 946 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version, 947 event->param.conn.responder_resources, 948 event->param.conn.initiator_depth, isv6); 949 950 /* rdma_accept() calls rdma_reject() internally if it fails */ 951 if (rdma_accept(cm_id, &conn_param)) 952 rds_ib_conn_error(conn, "rdma_accept failed\n"); 953 954 out: 955 if (conn) 956 mutex_unlock(&conn->c_cm_lock); 957 if (err) 958 rdma_reject(cm_id, &err, sizeof(int), 959 IB_CM_REJ_CONSUMER_DEFINED); 960 return destroy; 961 } 962 963 964 int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6) 965 { 966 struct rds_connection *conn = cm_id->context; 967 struct rds_ib_connection *ic = conn->c_transport_data; 968 struct rdma_conn_param conn_param; 969 union rds_ib_conn_priv dp; 970 int ret; 971 972 /* If the peer doesn't do protocol negotiation, we must 973 * default to RDSv3.0 */ 974 rds_ib_set_protocol(conn, RDS_PROTOCOL_4_1); 975 ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */ 976 977 ret = rds_ib_setup_qp(conn); 978 if (ret) { 979 rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret); 980 goto out; 981 } 982 983 rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, 984 conn->c_proposed_version, 985 UINT_MAX, UINT_MAX, isv6); 986 ret = rdma_connect_locked(cm_id, &conn_param); 987 if (ret) 988 rds_ib_conn_error(conn, "rdma_connect_locked failed (%d)\n", 989 ret); 990 991 out: 992 /* Beware - returning non-zero tells the rdma_cm to destroy 993 * the cm_id. We should certainly not do it as long as we still 994 * "own" the cm_id. */ 995 if (ret) { 996 if (ic->i_cm_id == cm_id) 997 ret = 0; 998 } 999 ic->i_active_side = true; 1000 return ret; 1001 } 1002 1003 int rds_ib_conn_path_connect(struct rds_conn_path *cp) 1004 { 1005 struct rds_connection *conn = cp->cp_conn; 1006 struct sockaddr_storage src, dest; 1007 rdma_cm_event_handler handler; 1008 struct rds_ib_connection *ic; 1009 int ret; 1010 1011 ic = conn->c_transport_data; 1012 1013 /* XXX I wonder what affect the port space has */ 1014 /* delegate cm event handler to rdma_transport */ 1015 #if IS_ENABLED(CONFIG_IPV6) 1016 if (conn->c_isv6) 1017 handler = rds6_rdma_cm_event_handler; 1018 else 1019 #endif 1020 handler = rds_rdma_cm_event_handler; 1021 ic->i_cm_id = rdma_create_id(&init_net, handler, conn, 1022 RDMA_PS_TCP, IB_QPT_RC); 1023 if (IS_ERR(ic->i_cm_id)) { 1024 ret = PTR_ERR(ic->i_cm_id); 1025 ic->i_cm_id = NULL; 1026 rdsdebug("rdma_create_id() failed: %d\n", ret); 1027 goto out; 1028 } 1029 1030 rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn); 1031 1032 if (ipv6_addr_v4mapped(&conn->c_faddr)) { 1033 struct sockaddr_in *sin; 1034 1035 sin = (struct sockaddr_in *)&src; 1036 sin->sin_family = AF_INET; 1037 sin->sin_addr.s_addr = conn->c_laddr.s6_addr32[3]; 1038 sin->sin_port = 0; 1039 1040 sin = (struct sockaddr_in *)&dest; 1041 sin->sin_family = AF_INET; 1042 sin->sin_addr.s_addr = conn->c_faddr.s6_addr32[3]; 1043 sin->sin_port = htons(RDS_PORT); 1044 } else { 1045 struct sockaddr_in6 *sin6; 1046 1047 sin6 = (struct sockaddr_in6 *)&src; 1048 sin6->sin6_family = AF_INET6; 1049 sin6->sin6_addr = conn->c_laddr; 1050 sin6->sin6_port = 0; 1051 sin6->sin6_scope_id = conn->c_dev_if; 1052 1053 sin6 = (struct sockaddr_in6 *)&dest; 1054 sin6->sin6_family = AF_INET6; 1055 sin6->sin6_addr = conn->c_faddr; 1056 sin6->sin6_port = htons(RDS_CM_PORT); 1057 sin6->sin6_scope_id = conn->c_dev_if; 1058 } 1059 1060 ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src, 1061 (struct sockaddr *)&dest, 1062 RDS_RDMA_RESOLVE_TIMEOUT_MS); 1063 if (ret) { 1064 rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id, 1065 ret); 1066 rdma_destroy_id(ic->i_cm_id); 1067 ic->i_cm_id = NULL; 1068 } 1069 1070 out: 1071 return ret; 1072 } 1073 1074 /* 1075 * This is so careful about only cleaning up resources that were built up 1076 * so that it can be called at any point during startup. In fact it 1077 * can be called multiple times for a given connection. 1078 */ 1079 void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) 1080 { 1081 struct rds_connection *conn = cp->cp_conn; 1082 struct rds_ib_connection *ic = conn->c_transport_data; 1083 int err = 0; 1084 1085 rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id, 1086 ic->i_pd, ic->i_send_cq, ic->i_recv_cq, 1087 ic->i_cm_id ? ic->i_cm_id->qp : NULL); 1088 1089 if (ic->i_cm_id) { 1090 rdsdebug("disconnecting cm %p\n", ic->i_cm_id); 1091 err = rdma_disconnect(ic->i_cm_id); 1092 if (err) { 1093 /* Actually this may happen quite frequently, when 1094 * an outgoing connect raced with an incoming connect. 1095 */ 1096 rdsdebug("failed to disconnect, cm: %p err %d\n", 1097 ic->i_cm_id, err); 1098 } 1099 1100 /* kick off "flush_worker" for all pools in order to reap 1101 * all FRMR registrations that are still marked "FRMR_IS_INUSE" 1102 */ 1103 rds_ib_flush_mrs(); 1104 1105 /* 1106 * We want to wait for tx and rx completion to finish 1107 * before we tear down the connection, but we have to be 1108 * careful not to get stuck waiting on a send ring that 1109 * only has unsignaled sends in it. We've shutdown new 1110 * sends before getting here so by waiting for signaled 1111 * sends to complete we're ensured that there will be no 1112 * more tx processing. 1113 */ 1114 wait_event(rds_ib_ring_empty_wait, 1115 rds_ib_ring_empty(&ic->i_recv_ring) && 1116 (atomic_read(&ic->i_signaled_sends) == 0) && 1117 (atomic_read(&ic->i_fastreg_inuse_count) == 0) && 1118 (atomic_read(&ic->i_fastreg_wrs) == RDS_IB_DEFAULT_FR_WR)); 1119 tasklet_kill(&ic->i_send_tasklet); 1120 tasklet_kill(&ic->i_recv_tasklet); 1121 1122 atomic_set(&ic->i_cq_quiesce, 1); 1123 1124 /* first destroy the ib state that generates callbacks */ 1125 if (ic->i_cm_id->qp) 1126 rdma_destroy_qp(ic->i_cm_id); 1127 if (ic->i_send_cq) { 1128 if (ic->rds_ibdev) 1129 ibdev_put_vector(ic->rds_ibdev, ic->i_scq_vector); 1130 ib_destroy_cq(ic->i_send_cq); 1131 } 1132 1133 if (ic->i_recv_cq) { 1134 if (ic->rds_ibdev) 1135 ibdev_put_vector(ic->rds_ibdev, ic->i_rcq_vector); 1136 ib_destroy_cq(ic->i_recv_cq); 1137 } 1138 1139 if (ic->rds_ibdev) { 1140 /* then free the resources that ib callbacks use */ 1141 if (ic->i_send_hdrs) { 1142 rds_dma_hdrs_free(ic->rds_ibdev, 1143 ic->i_send_hdrs, 1144 ic->i_send_hdrs_dma, 1145 ic->i_send_ring.w_nr, 1146 DMA_TO_DEVICE); 1147 ic->i_send_hdrs = NULL; 1148 ic->i_send_hdrs_dma = NULL; 1149 } 1150 1151 if (ic->i_recv_hdrs) { 1152 rds_dma_hdrs_free(ic->rds_ibdev, 1153 ic->i_recv_hdrs, 1154 ic->i_recv_hdrs_dma, 1155 ic->i_recv_ring.w_nr, 1156 DMA_FROM_DEVICE); 1157 ic->i_recv_hdrs = NULL; 1158 ic->i_recv_hdrs_dma = NULL; 1159 } 1160 1161 if (ic->i_ack) { 1162 rds_dma_hdr_free(ic->rds_ibdev->dev, ic->i_ack, 1163 ic->i_ack_dma, DMA_TO_DEVICE); 1164 ic->i_ack = NULL; 1165 } 1166 } else { 1167 WARN_ON(ic->i_send_hdrs); 1168 WARN_ON(ic->i_send_hdrs_dma); 1169 WARN_ON(ic->i_recv_hdrs); 1170 WARN_ON(ic->i_recv_hdrs_dma); 1171 WARN_ON(ic->i_ack); 1172 } 1173 1174 if (ic->i_sends) 1175 rds_ib_send_clear_ring(ic); 1176 if (ic->i_recvs) 1177 rds_ib_recv_clear_ring(ic); 1178 1179 rdma_destroy_id(ic->i_cm_id); 1180 1181 /* 1182 * Move connection back to the nodev list. 1183 */ 1184 if (ic->rds_ibdev) 1185 rds_ib_remove_conn(ic->rds_ibdev, conn); 1186 1187 ic->i_cm_id = NULL; 1188 ic->i_pd = NULL; 1189 ic->i_send_cq = NULL; 1190 ic->i_recv_cq = NULL; 1191 } 1192 BUG_ON(ic->rds_ibdev); 1193 1194 /* Clear pending transmit */ 1195 if (ic->i_data_op) { 1196 struct rds_message *rm; 1197 1198 rm = container_of(ic->i_data_op, struct rds_message, data); 1199 rds_message_put(rm); 1200 ic->i_data_op = NULL; 1201 } 1202 1203 /* Clear the ACK state */ 1204 clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags); 1205 #ifdef KERNEL_HAS_ATOMIC64 1206 atomic64_set(&ic->i_ack_next, 0); 1207 #else 1208 ic->i_ack_next = 0; 1209 #endif 1210 ic->i_ack_recv = 0; 1211 1212 /* Clear flow control state */ 1213 ic->i_flowctl = 0; 1214 atomic_set(&ic->i_credits, 0); 1215 1216 /* Re-init rings, but retain sizes. */ 1217 rds_ib_ring_init(&ic->i_send_ring, ic->i_send_ring.w_nr); 1218 rds_ib_ring_init(&ic->i_recv_ring, ic->i_recv_ring.w_nr); 1219 1220 if (ic->i_ibinc) { 1221 rds_inc_put(&ic->i_ibinc->ii_inc); 1222 ic->i_ibinc = NULL; 1223 } 1224 1225 vfree(ic->i_sends); 1226 ic->i_sends = NULL; 1227 vfree(ic->i_recvs); 1228 ic->i_recvs = NULL; 1229 ic->i_active_side = false; 1230 } 1231 1232 int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp) 1233 { 1234 struct rds_ib_connection *ic; 1235 unsigned long flags; 1236 int ret; 1237 1238 /* XXX too lazy? */ 1239 ic = kzalloc(sizeof(struct rds_ib_connection), gfp); 1240 if (!ic) 1241 return -ENOMEM; 1242 1243 ret = rds_ib_recv_alloc_caches(ic, gfp); 1244 if (ret) { 1245 kfree(ic); 1246 return ret; 1247 } 1248 1249 INIT_LIST_HEAD(&ic->ib_node); 1250 tasklet_init(&ic->i_send_tasklet, rds_ib_tasklet_fn_send, 1251 (unsigned long)ic); 1252 tasklet_init(&ic->i_recv_tasklet, rds_ib_tasklet_fn_recv, 1253 (unsigned long)ic); 1254 mutex_init(&ic->i_recv_mutex); 1255 #ifndef KERNEL_HAS_ATOMIC64 1256 spin_lock_init(&ic->i_ack_lock); 1257 #endif 1258 atomic_set(&ic->i_signaled_sends, 0); 1259 atomic_set(&ic->i_fastreg_wrs, RDS_IB_DEFAULT_FR_WR); 1260 1261 /* 1262 * rds_ib_conn_shutdown() waits for these to be emptied so they 1263 * must be initialized before it can be called. 1264 */ 1265 rds_ib_ring_init(&ic->i_send_ring, 0); 1266 rds_ib_ring_init(&ic->i_recv_ring, 0); 1267 1268 ic->conn = conn; 1269 conn->c_transport_data = ic; 1270 1271 spin_lock_irqsave(&ib_nodev_conns_lock, flags); 1272 list_add_tail(&ic->ib_node, &ib_nodev_conns); 1273 spin_unlock_irqrestore(&ib_nodev_conns_lock, flags); 1274 1275 1276 rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data); 1277 return 0; 1278 } 1279 1280 /* 1281 * Free a connection. Connection must be shut down and not set for reconnect. 1282 */ 1283 void rds_ib_conn_free(void *arg) 1284 { 1285 struct rds_ib_connection *ic = arg; 1286 spinlock_t *lock_ptr; 1287 1288 rdsdebug("ic %p\n", ic); 1289 1290 /* 1291 * Conn is either on a dev's list or on the nodev list. 1292 * A race with shutdown() or connect() would cause problems 1293 * (since rds_ibdev would change) but that should never happen. 1294 */ 1295 lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock; 1296 1297 spin_lock_irq(lock_ptr); 1298 list_del(&ic->ib_node); 1299 spin_unlock_irq(lock_ptr); 1300 1301 rds_ib_recv_free_caches(ic); 1302 1303 kfree(ic); 1304 } 1305 1306 1307 /* 1308 * An error occurred on the connection 1309 */ 1310 void 1311 __rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...) 1312 { 1313 va_list ap; 1314 1315 rds_conn_drop(conn); 1316 1317 va_start(ap, fmt); 1318 vprintk(fmt, ap); 1319 va_end(ap); 1320 } 1321