1 /* 2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <linux/log2.h> 35 #include <linux/etherdevice.h> 36 #include <net/ip.h> 37 #include <linux/slab.h> 38 #include <linux/netdevice.h> 39 40 #include <rdma/ib_cache.h> 41 #include <rdma/ib_pack.h> 42 #include <rdma/ib_addr.h> 43 #include <rdma/ib_mad.h> 44 #include <rdma/uverbs_ioctl.h> 45 46 #include <linux/mlx4/driver.h> 47 #include <linux/mlx4/qp.h> 48 49 #include "mlx4_ib.h" 50 #include <rdma/mlx4-abi.h> 51 52 static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, 53 struct mlx4_ib_cq *recv_cq); 54 static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, 55 struct mlx4_ib_cq *recv_cq); 56 static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state, 57 struct ib_udata *udata); 58 59 enum { 60 MLX4_IB_ACK_REQ_FREQ = 8, 61 }; 62 63 enum { 64 MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83, 65 MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f, 66 MLX4_IB_LINK_TYPE_IB = 0, 67 MLX4_IB_LINK_TYPE_ETH = 1 68 }; 69 70 enum { 71 MLX4_IB_MIN_SQ_STRIDE = 6, 72 MLX4_IB_CACHE_LINE_SIZE = 64, 73 }; 74 75 enum { 76 MLX4_RAW_QP_MTU = 7, 77 MLX4_RAW_QP_MSGMAX = 31, 78 }; 79 80 #ifndef ETH_ALEN 81 #define ETH_ALEN 6 82 #endif 83 84 static const __be32 mlx4_ib_opcode[] = { 85 [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND), 86 [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO), 87 [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM), 88 [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE), 89 [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM), 90 [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ), 91 [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS), 92 [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA), 93 [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL), 94 [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL), 95 [IB_WR_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR), 96 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS), 97 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA), 98 }; 99 100 enum mlx4_ib_source_type { 101 MLX4_IB_QP_SRC = 0, 102 MLX4_IB_RWQ_SRC = 1, 103 }; 104 105 struct mlx4_ib_qp_event_work { 106 struct work_struct work; 107 struct mlx4_qp *qp; 108 enum mlx4_event type; 109 }; 110 111 static struct workqueue_struct *mlx4_ib_qp_event_wq; 112 113 static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp) 114 { 115 if (!mlx4_is_master(dev->dev)) 116 return 0; 117 118 return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn && 119 qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn + 120 8 * MLX4_MFUNC_MAX; 121 } 122 123 static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp) 124 { 125 int proxy_sqp = 0; 126 int real_sqp = 0; 127 int i; 128 /* PPF or Native -- real SQP */ 129 real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) && 130 qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn && 131 qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3); 132 if (real_sqp) 133 return 1; 134 /* VF or PF -- proxy SQP */ 135 if (mlx4_is_mfunc(dev->dev)) { 136 for (i = 0; i < dev->dev->caps.num_ports; i++) { 137 if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy || 138 qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp1_proxy) { 139 proxy_sqp = 1; 140 break; 141 } 142 } 143 } 144 if (proxy_sqp) 145 return 1; 146 147 return !!(qp->flags & MLX4_IB_ROCE_V2_GSI_QP); 148 } 149 150 /* used for INIT/CLOSE port logic */ 151 static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp) 152 { 153 int proxy_qp0 = 0; 154 int real_qp0 = 0; 155 int i; 156 /* PPF or Native -- real QP0 */ 157 real_qp0 = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) && 158 qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn && 159 qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 1); 160 if (real_qp0) 161 return 1; 162 /* VF or PF -- proxy QP0 */ 163 if (mlx4_is_mfunc(dev->dev)) { 164 for (i = 0; i < dev->dev->caps.num_ports; i++) { 165 if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy) { 166 proxy_qp0 = 1; 167 break; 168 } 169 } 170 } 171 return proxy_qp0; 172 } 173 174 static void *get_wqe(struct mlx4_ib_qp *qp, int offset) 175 { 176 return mlx4_buf_offset(&qp->buf, offset); 177 } 178 179 static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n) 180 { 181 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift)); 182 } 183 184 static void *get_send_wqe(struct mlx4_ib_qp *qp, int n) 185 { 186 return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift)); 187 } 188 189 /* 190 * Stamp a SQ WQE so that it is invalid if prefetched by marking the 191 * first four bytes of every 64 byte chunk with 0xffffffff, except for 192 * the very first chunk of the WQE. 193 */ 194 static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n) 195 { 196 __be32 *wqe; 197 int i; 198 int s; 199 void *buf; 200 struct mlx4_wqe_ctrl_seg *ctrl; 201 202 buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1)); 203 ctrl = (struct mlx4_wqe_ctrl_seg *)buf; 204 s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4; 205 for (i = 64; i < s; i += 64) { 206 wqe = buf + i; 207 *wqe = cpu_to_be32(0xffffffff); 208 } 209 } 210 211 static void mlx4_ib_handle_qp_event(struct work_struct *_work) 212 { 213 struct mlx4_ib_qp_event_work *qpe_work = 214 container_of(_work, struct mlx4_ib_qp_event_work, work); 215 struct ib_qp *ibqp = &to_mibqp(qpe_work->qp)->ibqp; 216 struct ib_event event = {}; 217 218 event.device = ibqp->device; 219 event.element.qp = ibqp; 220 221 switch (qpe_work->type) { 222 case MLX4_EVENT_TYPE_PATH_MIG: 223 event.event = IB_EVENT_PATH_MIG; 224 break; 225 case MLX4_EVENT_TYPE_COMM_EST: 226 event.event = IB_EVENT_COMM_EST; 227 break; 228 case MLX4_EVENT_TYPE_SQ_DRAINED: 229 event.event = IB_EVENT_SQ_DRAINED; 230 break; 231 case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE: 232 event.event = IB_EVENT_QP_LAST_WQE_REACHED; 233 break; 234 case MLX4_EVENT_TYPE_WQ_CATAS_ERROR: 235 event.event = IB_EVENT_QP_FATAL; 236 break; 237 case MLX4_EVENT_TYPE_PATH_MIG_FAILED: 238 event.event = IB_EVENT_PATH_MIG_ERR; 239 break; 240 case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR: 241 event.event = IB_EVENT_QP_REQ_ERR; 242 break; 243 case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR: 244 event.event = IB_EVENT_QP_ACCESS_ERR; 245 break; 246 default: 247 pr_warn("Unexpected event type %d on QP %06x\n", 248 qpe_work->type, qpe_work->qp->qpn); 249 goto out; 250 } 251 252 ibqp->event_handler(&event, ibqp->qp_context); 253 254 out: 255 mlx4_put_qp(qpe_work->qp); 256 kfree(qpe_work); 257 } 258 259 static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type) 260 { 261 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp; 262 struct mlx4_ib_qp_event_work *qpe_work; 263 264 if (type == MLX4_EVENT_TYPE_PATH_MIG) 265 to_mibqp(qp)->port = to_mibqp(qp)->alt_port; 266 267 if (!ibqp->event_handler) 268 goto out_no_handler; 269 270 qpe_work = kzalloc(sizeof(*qpe_work), GFP_ATOMIC); 271 if (!qpe_work) 272 goto out_no_handler; 273 274 qpe_work->qp = qp; 275 qpe_work->type = type; 276 INIT_WORK(&qpe_work->work, mlx4_ib_handle_qp_event); 277 queue_work(mlx4_ib_qp_event_wq, &qpe_work->work); 278 return; 279 280 out_no_handler: 281 mlx4_put_qp(qp); 282 } 283 284 static void mlx4_ib_wq_event(struct mlx4_qp *qp, enum mlx4_event type) 285 { 286 pr_warn_ratelimited("Unexpected event type %d on WQ 0x%06x. Events are not supported for WQs\n", 287 type, qp->qpn); 288 } 289 290 static int send_wqe_overhead(enum mlx4_ib_qp_type type, u32 flags) 291 { 292 /* 293 * UD WQEs must have a datagram segment. 294 * RC and UC WQEs might have a remote address segment. 295 * MLX WQEs need two extra inline data segments (for the UD 296 * header and space for the ICRC). 297 */ 298 switch (type) { 299 case MLX4_IB_QPT_UD: 300 return sizeof (struct mlx4_wqe_ctrl_seg) + 301 sizeof (struct mlx4_wqe_datagram_seg) + 302 ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0); 303 case MLX4_IB_QPT_PROXY_SMI_OWNER: 304 case MLX4_IB_QPT_PROXY_SMI: 305 case MLX4_IB_QPT_PROXY_GSI: 306 return sizeof (struct mlx4_wqe_ctrl_seg) + 307 sizeof (struct mlx4_wqe_datagram_seg) + 64; 308 case MLX4_IB_QPT_TUN_SMI_OWNER: 309 case MLX4_IB_QPT_TUN_GSI: 310 return sizeof (struct mlx4_wqe_ctrl_seg) + 311 sizeof (struct mlx4_wqe_datagram_seg); 312 313 case MLX4_IB_QPT_UC: 314 return sizeof (struct mlx4_wqe_ctrl_seg) + 315 sizeof (struct mlx4_wqe_raddr_seg); 316 case MLX4_IB_QPT_RC: 317 return sizeof (struct mlx4_wqe_ctrl_seg) + 318 sizeof (struct mlx4_wqe_masked_atomic_seg) + 319 sizeof (struct mlx4_wqe_raddr_seg); 320 case MLX4_IB_QPT_SMI: 321 case MLX4_IB_QPT_GSI: 322 return sizeof (struct mlx4_wqe_ctrl_seg) + 323 ALIGN(MLX4_IB_UD_HEADER_SIZE + 324 DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE, 325 MLX4_INLINE_ALIGN) * 326 sizeof (struct mlx4_wqe_inline_seg), 327 sizeof (struct mlx4_wqe_data_seg)) + 328 ALIGN(4 + 329 sizeof (struct mlx4_wqe_inline_seg), 330 sizeof (struct mlx4_wqe_data_seg)); 331 default: 332 return sizeof (struct mlx4_wqe_ctrl_seg); 333 } 334 } 335 336 static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap, 337 bool is_user, bool has_rq, struct mlx4_ib_qp *qp, 338 u32 inl_recv_sz) 339 { 340 /* Sanity check RQ size before proceeding */ 341 if (cap->max_recv_wr > dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE || 342 cap->max_recv_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg)) 343 return -EINVAL; 344 345 if (!has_rq) { 346 if (cap->max_recv_wr || inl_recv_sz) 347 return -EINVAL; 348 349 qp->rq.wqe_cnt = qp->rq.max_gs = 0; 350 } else { 351 u32 max_inl_recv_sz = dev->dev->caps.max_rq_sg * 352 sizeof(struct mlx4_wqe_data_seg); 353 u32 wqe_size; 354 355 /* HW requires >= 1 RQ entry with >= 1 gather entry */ 356 if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge || 357 inl_recv_sz > max_inl_recv_sz)) 358 return -EINVAL; 359 360 qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr)); 361 qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge)); 362 wqe_size = qp->rq.max_gs * sizeof(struct mlx4_wqe_data_seg); 363 qp->rq.wqe_shift = ilog2(max_t(u32, wqe_size, inl_recv_sz)); 364 } 365 366 /* leave userspace return values as they were, so as not to break ABI */ 367 if (is_user) { 368 cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt; 369 cap->max_recv_sge = qp->rq.max_gs; 370 } else { 371 cap->max_recv_wr = qp->rq.max_post = 372 min(dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE, qp->rq.wqe_cnt); 373 cap->max_recv_sge = min(qp->rq.max_gs, 374 min(dev->dev->caps.max_sq_sg, 375 dev->dev->caps.max_rq_sg)); 376 } 377 378 return 0; 379 } 380 381 static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap, 382 enum mlx4_ib_qp_type type, struct mlx4_ib_qp *qp) 383 { 384 int s; 385 386 /* Sanity check SQ size before proceeding */ 387 if (cap->max_send_wr > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) || 388 cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) || 389 cap->max_inline_data + send_wqe_overhead(type, qp->flags) + 390 sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz) 391 return -EINVAL; 392 393 /* 394 * For MLX transport we need 2 extra S/G entries: 395 * one for the header and one for the checksum at the end 396 */ 397 if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI || 398 type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) && 399 cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg) 400 return -EINVAL; 401 402 s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg), 403 cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) + 404 send_wqe_overhead(type, qp->flags); 405 406 if (s > dev->dev->caps.max_sq_desc_sz) 407 return -EINVAL; 408 409 qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s)); 410 411 /* 412 * We need to leave 2 KB + 1 WR of headroom in the SQ to 413 * allow HW to prefetch. 414 */ 415 qp->sq_spare_wqes = MLX4_IB_SQ_HEADROOM(qp->sq.wqe_shift); 416 qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr + 417 qp->sq_spare_wqes); 418 419 qp->sq.max_gs = 420 (min(dev->dev->caps.max_sq_desc_sz, 421 (1 << qp->sq.wqe_shift)) - 422 send_wqe_overhead(type, qp->flags)) / 423 sizeof (struct mlx4_wqe_data_seg); 424 425 qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) + 426 (qp->sq.wqe_cnt << qp->sq.wqe_shift); 427 if (qp->rq.wqe_shift > qp->sq.wqe_shift) { 428 qp->rq.offset = 0; 429 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift; 430 } else { 431 qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift; 432 qp->sq.offset = 0; 433 } 434 435 cap->max_send_wr = qp->sq.max_post = 436 qp->sq.wqe_cnt - qp->sq_spare_wqes; 437 cap->max_send_sge = min(qp->sq.max_gs, 438 min(dev->dev->caps.max_sq_sg, 439 dev->dev->caps.max_rq_sg)); 440 /* We don't support inline sends for kernel QPs (yet) */ 441 cap->max_inline_data = 0; 442 443 return 0; 444 } 445 446 static int set_user_sq_size(struct mlx4_ib_dev *dev, 447 struct mlx4_ib_qp *qp, 448 struct mlx4_ib_create_qp *ucmd) 449 { 450 u32 cnt; 451 452 /* Sanity check SQ size before proceeding */ 453 if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) || 454 cnt > dev->dev->caps.max_wqes) 455 return -EINVAL; 456 if (ucmd->log_sq_stride > 457 ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) || 458 ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE) 459 return -EINVAL; 460 461 qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count; 462 qp->sq.wqe_shift = ucmd->log_sq_stride; 463 464 qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) + 465 (qp->sq.wqe_cnt << qp->sq.wqe_shift); 466 467 return 0; 468 } 469 470 static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp) 471 { 472 int i; 473 474 qp->sqp_proxy_rcv = 475 kmalloc_array(qp->rq.wqe_cnt, sizeof(struct mlx4_ib_buf), 476 GFP_KERNEL); 477 if (!qp->sqp_proxy_rcv) 478 return -ENOMEM; 479 for (i = 0; i < qp->rq.wqe_cnt; i++) { 480 qp->sqp_proxy_rcv[i].addr = 481 kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr), 482 GFP_KERNEL); 483 if (!qp->sqp_proxy_rcv[i].addr) 484 goto err; 485 qp->sqp_proxy_rcv[i].map = 486 ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr, 487 sizeof (struct mlx4_ib_proxy_sqp_hdr), 488 DMA_FROM_DEVICE); 489 if (ib_dma_mapping_error(dev, qp->sqp_proxy_rcv[i].map)) { 490 kfree(qp->sqp_proxy_rcv[i].addr); 491 goto err; 492 } 493 } 494 return 0; 495 496 err: 497 while (i > 0) { 498 --i; 499 ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map, 500 sizeof (struct mlx4_ib_proxy_sqp_hdr), 501 DMA_FROM_DEVICE); 502 kfree(qp->sqp_proxy_rcv[i].addr); 503 } 504 kfree(qp->sqp_proxy_rcv); 505 qp->sqp_proxy_rcv = NULL; 506 return -ENOMEM; 507 } 508 509 static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp) 510 { 511 int i; 512 513 for (i = 0; i < qp->rq.wqe_cnt; i++) { 514 ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map, 515 sizeof (struct mlx4_ib_proxy_sqp_hdr), 516 DMA_FROM_DEVICE); 517 kfree(qp->sqp_proxy_rcv[i].addr); 518 } 519 kfree(qp->sqp_proxy_rcv); 520 } 521 522 static bool qp_has_rq(struct ib_qp_init_attr *attr) 523 { 524 if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT) 525 return false; 526 527 return !attr->srq; 528 } 529 530 static int qp0_enabled_vf(struct mlx4_dev *dev, int qpn) 531 { 532 int i; 533 for (i = 0; i < dev->caps.num_ports; i++) { 534 if (qpn == dev->caps.spec_qps[i].qp0_proxy) 535 return !!dev->caps.spec_qps[i].qp0_qkey; 536 } 537 return 0; 538 } 539 540 static void mlx4_ib_free_qp_counter(struct mlx4_ib_dev *dev, 541 struct mlx4_ib_qp *qp) 542 { 543 mutex_lock(&dev->counters_table[qp->port - 1].mutex); 544 mlx4_counter_free(dev->dev, qp->counter_index->index); 545 list_del(&qp->counter_index->list); 546 mutex_unlock(&dev->counters_table[qp->port - 1].mutex); 547 548 kfree(qp->counter_index); 549 qp->counter_index = NULL; 550 } 551 552 static int set_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_rss *rss_ctx, 553 struct ib_qp_init_attr *init_attr, 554 struct mlx4_ib_create_qp_rss *ucmd) 555 { 556 rss_ctx->base_qpn_tbl_sz = init_attr->rwq_ind_tbl->ind_tbl[0]->wq_num | 557 (init_attr->rwq_ind_tbl->log_ind_tbl_size << 24); 558 559 if ((ucmd->rx_hash_function == MLX4_IB_RX_HASH_FUNC_TOEPLITZ) && 560 (dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP)) { 561 memcpy(rss_ctx->rss_key, ucmd->rx_hash_key, 562 MLX4_EN_RSS_KEY_SIZE); 563 } else { 564 pr_debug("RX Hash function is not supported\n"); 565 return (-EOPNOTSUPP); 566 } 567 568 if (ucmd->rx_hash_fields_mask & ~(u64)(MLX4_IB_RX_HASH_SRC_IPV4 | 569 MLX4_IB_RX_HASH_DST_IPV4 | 570 MLX4_IB_RX_HASH_SRC_IPV6 | 571 MLX4_IB_RX_HASH_DST_IPV6 | 572 MLX4_IB_RX_HASH_SRC_PORT_TCP | 573 MLX4_IB_RX_HASH_DST_PORT_TCP | 574 MLX4_IB_RX_HASH_SRC_PORT_UDP | 575 MLX4_IB_RX_HASH_DST_PORT_UDP | 576 MLX4_IB_RX_HASH_INNER)) { 577 pr_debug("RX Hash fields_mask has unsupported mask (0x%llx)\n", 578 ucmd->rx_hash_fields_mask); 579 return (-EOPNOTSUPP); 580 } 581 582 if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) && 583 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) { 584 rss_ctx->flags = MLX4_RSS_IPV4; 585 } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) || 586 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) { 587 pr_debug("RX Hash fields_mask is not supported - both IPv4 SRC and DST must be set\n"); 588 return (-EOPNOTSUPP); 589 } 590 591 if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) && 592 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) { 593 rss_ctx->flags |= MLX4_RSS_IPV6; 594 } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) || 595 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) { 596 pr_debug("RX Hash fields_mask is not supported - both IPv6 SRC and DST must be set\n"); 597 return (-EOPNOTSUPP); 598 } 599 600 if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) && 601 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) { 602 if (!(dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UDP_RSS)) { 603 pr_debug("RX Hash fields_mask for UDP is not supported\n"); 604 return (-EOPNOTSUPP); 605 } 606 607 if (rss_ctx->flags & MLX4_RSS_IPV4) 608 rss_ctx->flags |= MLX4_RSS_UDP_IPV4; 609 if (rss_ctx->flags & MLX4_RSS_IPV6) 610 rss_ctx->flags |= MLX4_RSS_UDP_IPV6; 611 if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) { 612 pr_debug("RX Hash fields_mask is not supported - UDP must be set with IPv4 or IPv6\n"); 613 return (-EOPNOTSUPP); 614 } 615 } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) || 616 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) { 617 pr_debug("RX Hash fields_mask is not supported - both UDP SRC and DST must be set\n"); 618 return (-EOPNOTSUPP); 619 } 620 621 if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) && 622 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) { 623 if (rss_ctx->flags & MLX4_RSS_IPV4) 624 rss_ctx->flags |= MLX4_RSS_TCP_IPV4; 625 if (rss_ctx->flags & MLX4_RSS_IPV6) 626 rss_ctx->flags |= MLX4_RSS_TCP_IPV6; 627 if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) { 628 pr_debug("RX Hash fields_mask is not supported - TCP must be set with IPv4 or IPv6\n"); 629 return (-EOPNOTSUPP); 630 } 631 } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) || 632 (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) { 633 pr_debug("RX Hash fields_mask is not supported - both TCP SRC and DST must be set\n"); 634 return (-EOPNOTSUPP); 635 } 636 637 if (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_INNER) { 638 if (dev->dev->caps.tunnel_offload_mode == 639 MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) { 640 /* 641 * Hash according to inner headers if exist, otherwise 642 * according to outer headers. 643 */ 644 rss_ctx->flags |= MLX4_RSS_BY_INNER_HEADERS_IPONLY; 645 } else { 646 pr_debug("RSS Hash for inner headers isn't supported\n"); 647 return (-EOPNOTSUPP); 648 } 649 } 650 651 return 0; 652 } 653 654 static int create_qp_rss(struct mlx4_ib_dev *dev, 655 struct ib_qp_init_attr *init_attr, 656 struct mlx4_ib_create_qp_rss *ucmd, 657 struct mlx4_ib_qp *qp) 658 { 659 int qpn; 660 int err; 661 662 qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS; 663 664 err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn, 0, qp->mqp.usage); 665 if (err) 666 return err; 667 668 err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp); 669 if (err) 670 goto err_qpn; 671 672 INIT_LIST_HEAD(&qp->gid_list); 673 INIT_LIST_HEAD(&qp->steering_rules); 674 675 qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET; 676 qp->state = IB_QPS_RESET; 677 678 /* Set dummy send resources to be compatible with HV and PRM */ 679 qp->sq_no_prefetch = 1; 680 qp->sq.wqe_cnt = 1; 681 qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE; 682 qp->buf_size = qp->sq.wqe_cnt << MLX4_IB_MIN_SQ_STRIDE; 683 qp->mtt = (to_mqp( 684 (struct ib_qp *)init_attr->rwq_ind_tbl->ind_tbl[0]))->mtt; 685 686 qp->rss_ctx = kzalloc(sizeof(*qp->rss_ctx), GFP_KERNEL); 687 if (!qp->rss_ctx) { 688 err = -ENOMEM; 689 goto err_qp_alloc; 690 } 691 692 err = set_qp_rss(dev, qp->rss_ctx, init_attr, ucmd); 693 if (err) 694 goto err; 695 696 return 0; 697 698 err: 699 kfree(qp->rss_ctx); 700 701 err_qp_alloc: 702 mlx4_qp_remove(dev->dev, &qp->mqp); 703 mlx4_qp_free(dev->dev, &qp->mqp); 704 705 err_qpn: 706 mlx4_qp_release_range(dev->dev, qpn, 1); 707 return err; 708 } 709 710 static int _mlx4_ib_create_qp_rss(struct ib_pd *pd, struct mlx4_ib_qp *qp, 711 struct ib_qp_init_attr *init_attr, 712 struct ib_udata *udata) 713 { 714 struct mlx4_ib_create_qp_rss ucmd = {}; 715 size_t required_cmd_sz; 716 int err; 717 718 if (!udata) { 719 pr_debug("RSS QP with NULL udata\n"); 720 return -EINVAL; 721 } 722 723 if (udata->outlen) 724 return -EOPNOTSUPP; 725 726 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + 727 sizeof(ucmd.reserved1); 728 if (udata->inlen < required_cmd_sz) { 729 pr_debug("invalid inlen\n"); 730 return -EINVAL; 731 } 732 733 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) { 734 pr_debug("copy failed\n"); 735 return -EFAULT; 736 } 737 738 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved))) 739 return -EOPNOTSUPP; 740 741 if (ucmd.comp_mask || ucmd.reserved1) 742 return -EOPNOTSUPP; 743 744 if (udata->inlen > sizeof(ucmd) && 745 !ib_is_udata_cleared(udata, sizeof(ucmd), 746 udata->inlen - sizeof(ucmd))) { 747 pr_debug("inlen is not supported\n"); 748 return -EOPNOTSUPP; 749 } 750 751 if (init_attr->qp_type != IB_QPT_RAW_PACKET) { 752 pr_debug("RSS QP with unsupported QP type %d\n", 753 init_attr->qp_type); 754 return -EOPNOTSUPP; 755 } 756 757 if (init_attr->create_flags) { 758 pr_debug("RSS QP doesn't support create flags\n"); 759 return -EOPNOTSUPP; 760 } 761 762 if (init_attr->send_cq || init_attr->cap.max_send_wr) { 763 pr_debug("RSS QP with unsupported send attributes\n"); 764 return -EOPNOTSUPP; 765 } 766 767 qp->pri.vid = 0xFFFF; 768 qp->alt.vid = 0xFFFF; 769 770 err = create_qp_rss(to_mdev(pd->device), init_attr, &ucmd, qp); 771 if (err) 772 return err; 773 774 qp->ibqp.qp_num = qp->mqp.qpn; 775 return 0; 776 } 777 778 /* 779 * This function allocates a WQN from a range which is consecutive and aligned 780 * to its size. In case the range is full, then it creates a new range and 781 * allocates WQN from it. The new range will be used for following allocations. 782 */ 783 static int mlx4_ib_alloc_wqn(struct mlx4_ib_ucontext *context, 784 struct mlx4_ib_qp *qp, int range_size, int *wqn) 785 { 786 struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device); 787 struct mlx4_wqn_range *range; 788 int err = 0; 789 790 mutex_lock(&context->wqn_ranges_mutex); 791 792 range = list_first_entry_or_null(&context->wqn_ranges_list, 793 struct mlx4_wqn_range, list); 794 795 if (!range || (range->refcount == range->size) || range->dirty) { 796 range = kzalloc(sizeof(*range), GFP_KERNEL); 797 if (!range) { 798 err = -ENOMEM; 799 goto out; 800 } 801 802 err = mlx4_qp_reserve_range(dev->dev, range_size, 803 range_size, &range->base_wqn, 0, 804 qp->mqp.usage); 805 if (err) { 806 kfree(range); 807 goto out; 808 } 809 810 range->size = range_size; 811 list_add(&range->list, &context->wqn_ranges_list); 812 } else if (range_size != 1) { 813 /* 814 * Requesting a new range (>1) when last range is still open, is 815 * not valid. 816 */ 817 err = -EINVAL; 818 goto out; 819 } 820 821 qp->wqn_range = range; 822 823 *wqn = range->base_wqn + range->refcount; 824 825 range->refcount++; 826 827 out: 828 mutex_unlock(&context->wqn_ranges_mutex); 829 830 return err; 831 } 832 833 static void mlx4_ib_release_wqn(struct mlx4_ib_ucontext *context, 834 struct mlx4_ib_qp *qp, bool dirty_release) 835 { 836 struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device); 837 struct mlx4_wqn_range *range; 838 839 mutex_lock(&context->wqn_ranges_mutex); 840 841 range = qp->wqn_range; 842 843 range->refcount--; 844 if (!range->refcount) { 845 mlx4_qp_release_range(dev->dev, range->base_wqn, 846 range->size); 847 list_del(&range->list); 848 kfree(range); 849 } else if (dirty_release) { 850 /* 851 * A range which one of its WQNs is destroyed, won't be able to be 852 * reused for further WQN allocations. 853 * The next created WQ will allocate a new range. 854 */ 855 range->dirty = true; 856 } 857 858 mutex_unlock(&context->wqn_ranges_mutex); 859 } 860 861 static int create_rq(struct ib_pd *pd, struct ib_qp_init_attr *init_attr, 862 struct ib_udata *udata, struct mlx4_ib_qp *qp) 863 { 864 struct mlx4_ib_dev *dev = to_mdev(pd->device); 865 int qpn; 866 int err; 867 struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context( 868 udata, struct mlx4_ib_ucontext, ibucontext); 869 struct mlx4_ib_cq *mcq; 870 unsigned long flags; 871 int range_size; 872 struct mlx4_ib_create_wq wq; 873 size_t copy_len; 874 int shift; 875 int n; 876 877 qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET; 878 879 spin_lock_init(&qp->sq.lock); 880 spin_lock_init(&qp->rq.lock); 881 INIT_LIST_HEAD(&qp->gid_list); 882 INIT_LIST_HEAD(&qp->steering_rules); 883 884 qp->state = IB_QPS_RESET; 885 886 copy_len = min(sizeof(struct mlx4_ib_create_wq), udata->inlen); 887 888 if (ib_copy_from_udata(&wq, udata, copy_len)) { 889 err = -EFAULT; 890 goto err; 891 } 892 893 if (wq.comp_mask || wq.reserved[0] || wq.reserved[1] || 894 wq.reserved[2]) { 895 pr_debug("user command isn't supported\n"); 896 err = -EOPNOTSUPP; 897 goto err; 898 } 899 900 if (wq.log_range_size > ilog2(dev->dev->caps.max_rss_tbl_sz)) { 901 pr_debug("WQN range size must be equal or smaller than %d\n", 902 dev->dev->caps.max_rss_tbl_sz); 903 err = -EOPNOTSUPP; 904 goto err; 905 } 906 range_size = 1 << wq.log_range_size; 907 908 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) 909 qp->flags |= MLX4_IB_QP_SCATTER_FCS; 910 911 err = set_rq_size(dev, &init_attr->cap, true, true, qp, qp->inl_recv_sz); 912 if (err) 913 goto err; 914 915 qp->sq_no_prefetch = 1; 916 qp->sq.wqe_cnt = 1; 917 qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE; 918 qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) + 919 (qp->sq.wqe_cnt << qp->sq.wqe_shift); 920 921 qp->umem = ib_umem_get(pd->device, wq.buf_addr, qp->buf_size, 0); 922 if (IS_ERR(qp->umem)) { 923 err = PTR_ERR(qp->umem); 924 goto err; 925 } 926 927 shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n); 928 if (shift < 0) { 929 err = shift; 930 goto err_buf; 931 } 932 933 err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt); 934 if (err) 935 goto err_buf; 936 937 err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem); 938 if (err) 939 goto err_mtt; 940 941 err = mlx4_ib_db_map_user(udata, wq.db_addr, &qp->db); 942 if (err) 943 goto err_mtt; 944 qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS; 945 946 err = mlx4_ib_alloc_wqn(context, qp, range_size, &qpn); 947 if (err) 948 goto err_wrid; 949 950 err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp); 951 if (err) 952 goto err_qpn; 953 954 /* 955 * Hardware wants QPN written in big-endian order (after 956 * shifting) for send doorbell. Precompute this value to save 957 * a little bit when posting sends. 958 */ 959 qp->doorbell_qpn = swab32(qp->mqp.qpn << 8); 960 961 qp->mqp.event = mlx4_ib_wq_event; 962 963 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags); 964 mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq), 965 to_mcq(init_attr->recv_cq)); 966 /* Maintain device to QPs access, needed for further handling 967 * via reset flow 968 */ 969 list_add_tail(&qp->qps_list, &dev->qp_list); 970 /* Maintain CQ to QPs access, needed for further handling 971 * via reset flow 972 */ 973 mcq = to_mcq(init_attr->send_cq); 974 list_add_tail(&qp->cq_send_list, &mcq->send_qp_list); 975 mcq = to_mcq(init_attr->recv_cq); 976 list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list); 977 mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq), 978 to_mcq(init_attr->recv_cq)); 979 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags); 980 return 0; 981 982 err_qpn: 983 mlx4_ib_release_wqn(context, qp, 0); 984 err_wrid: 985 mlx4_ib_db_unmap_user(context, &qp->db); 986 987 err_mtt: 988 mlx4_mtt_cleanup(dev->dev, &qp->mtt); 989 err_buf: 990 ib_umem_release(qp->umem); 991 err: 992 return err; 993 } 994 995 static int create_qp_common(struct ib_pd *pd, struct ib_qp_init_attr *init_attr, 996 struct ib_udata *udata, int sqpn, 997 struct mlx4_ib_qp *qp) 998 { 999 struct mlx4_ib_dev *dev = to_mdev(pd->device); 1000 int qpn; 1001 int err; 1002 struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context( 1003 udata, struct mlx4_ib_ucontext, ibucontext); 1004 enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type; 1005 struct mlx4_ib_cq *mcq; 1006 unsigned long flags; 1007 1008 /* When tunneling special qps, we use a plain UD qp */ 1009 if (sqpn) { 1010 if (mlx4_is_mfunc(dev->dev) && 1011 (!mlx4_is_master(dev->dev) || 1012 !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) { 1013 if (init_attr->qp_type == IB_QPT_GSI) 1014 qp_type = MLX4_IB_QPT_PROXY_GSI; 1015 else { 1016 if (mlx4_is_master(dev->dev) || 1017 qp0_enabled_vf(dev->dev, sqpn)) 1018 qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER; 1019 else 1020 qp_type = MLX4_IB_QPT_PROXY_SMI; 1021 } 1022 } 1023 qpn = sqpn; 1024 /* add extra sg entry for tunneling */ 1025 init_attr->cap.max_recv_sge++; 1026 } else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) { 1027 struct mlx4_ib_qp_tunnel_init_attr *tnl_init = 1028 container_of(init_attr, 1029 struct mlx4_ib_qp_tunnel_init_attr, init_attr); 1030 if ((tnl_init->proxy_qp_type != IB_QPT_SMI && 1031 tnl_init->proxy_qp_type != IB_QPT_GSI) || 1032 !mlx4_is_master(dev->dev)) 1033 return -EINVAL; 1034 if (tnl_init->proxy_qp_type == IB_QPT_GSI) 1035 qp_type = MLX4_IB_QPT_TUN_GSI; 1036 else if (tnl_init->slave == mlx4_master_func_num(dev->dev) || 1037 mlx4_vf_smi_enabled(dev->dev, tnl_init->slave, 1038 tnl_init->port)) 1039 qp_type = MLX4_IB_QPT_TUN_SMI_OWNER; 1040 else 1041 qp_type = MLX4_IB_QPT_TUN_SMI; 1042 /* we are definitely in the PPF here, since we are creating 1043 * tunnel QPs. base_tunnel_sqpn is therefore valid. */ 1044 qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave 1045 + tnl_init->proxy_qp_type * 2 + tnl_init->port - 1; 1046 sqpn = qpn; 1047 } 1048 1049 if (init_attr->qp_type == IB_QPT_SMI || 1050 init_attr->qp_type == IB_QPT_GSI || qp_type == MLX4_IB_QPT_SMI || 1051 qp_type == MLX4_IB_QPT_GSI || 1052 (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER | 1053 MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) { 1054 qp->sqp = kzalloc(sizeof(struct mlx4_ib_sqp), GFP_KERNEL); 1055 if (!qp->sqp) 1056 return -ENOMEM; 1057 } 1058 1059 qp->mlx4_ib_qp_type = qp_type; 1060 1061 spin_lock_init(&qp->sq.lock); 1062 spin_lock_init(&qp->rq.lock); 1063 INIT_LIST_HEAD(&qp->gid_list); 1064 INIT_LIST_HEAD(&qp->steering_rules); 1065 1066 qp->state = IB_QPS_RESET; 1067 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) 1068 qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE); 1069 1070 if (udata) { 1071 struct mlx4_ib_create_qp ucmd; 1072 size_t copy_len; 1073 int shift; 1074 int n; 1075 1076 copy_len = sizeof(struct mlx4_ib_create_qp); 1077 1078 if (ib_copy_from_udata(&ucmd, udata, copy_len)) { 1079 err = -EFAULT; 1080 goto err; 1081 } 1082 1083 qp->inl_recv_sz = ucmd.inl_recv_sz; 1084 1085 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) { 1086 if (!(dev->dev->caps.flags & 1087 MLX4_DEV_CAP_FLAG_FCS_KEEP)) { 1088 pr_debug("scatter FCS is unsupported\n"); 1089 err = -EOPNOTSUPP; 1090 goto err; 1091 } 1092 1093 qp->flags |= MLX4_IB_QP_SCATTER_FCS; 1094 } 1095 1096 err = set_rq_size(dev, &init_attr->cap, udata, 1097 qp_has_rq(init_attr), qp, qp->inl_recv_sz); 1098 if (err) 1099 goto err; 1100 1101 qp->sq_no_prefetch = ucmd.sq_no_prefetch; 1102 1103 err = set_user_sq_size(dev, qp, &ucmd); 1104 if (err) 1105 goto err; 1106 1107 qp->umem = 1108 ib_umem_get(pd->device, ucmd.buf_addr, qp->buf_size, 0); 1109 if (IS_ERR(qp->umem)) { 1110 err = PTR_ERR(qp->umem); 1111 goto err; 1112 } 1113 1114 shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n); 1115 if (shift < 0) { 1116 err = shift; 1117 goto err_buf; 1118 } 1119 1120 err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt); 1121 if (err) 1122 goto err_buf; 1123 1124 err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem); 1125 if (err) 1126 goto err_mtt; 1127 1128 if (qp_has_rq(init_attr)) { 1129 err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &qp->db); 1130 if (err) 1131 goto err_mtt; 1132 } 1133 qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS; 1134 } else { 1135 err = set_rq_size(dev, &init_attr->cap, udata, 1136 qp_has_rq(init_attr), qp, 0); 1137 if (err) 1138 goto err; 1139 1140 qp->sq_no_prefetch = 0; 1141 1142 if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO) 1143 qp->flags |= MLX4_IB_QP_LSO; 1144 1145 if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) { 1146 if (dev->steering_support == 1147 MLX4_STEERING_MODE_DEVICE_MANAGED) 1148 qp->flags |= MLX4_IB_QP_NETIF; 1149 else { 1150 err = -EINVAL; 1151 goto err; 1152 } 1153 } 1154 1155 err = set_kernel_sq_size(dev, &init_attr->cap, qp_type, qp); 1156 if (err) 1157 goto err; 1158 1159 if (qp_has_rq(init_attr)) { 1160 err = mlx4_db_alloc(dev->dev, &qp->db, 0); 1161 if (err) 1162 goto err; 1163 1164 *qp->db.db = 0; 1165 } 1166 1167 if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, 1168 &qp->buf)) { 1169 err = -ENOMEM; 1170 goto err_db; 1171 } 1172 1173 err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift, 1174 &qp->mtt); 1175 if (err) 1176 goto err_buf; 1177 1178 err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf); 1179 if (err) 1180 goto err_mtt; 1181 1182 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt, 1183 sizeof(u64), GFP_KERNEL); 1184 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt, 1185 sizeof(u64), GFP_KERNEL); 1186 if (!qp->sq.wrid || !qp->rq.wrid) { 1187 err = -ENOMEM; 1188 goto err_wrid; 1189 } 1190 qp->mqp.usage = MLX4_RES_USAGE_DRIVER; 1191 } 1192 1193 if (sqpn) { 1194 if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER | 1195 MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) { 1196 if (alloc_proxy_bufs(pd->device, qp)) { 1197 err = -ENOMEM; 1198 goto err_wrid; 1199 } 1200 } 1201 } else { 1202 /* Raw packet QPNs may not have bits 6,7 set in their qp_num; 1203 * otherwise, the WQE BlueFlame setup flow wrongly causes 1204 * VLAN insertion. */ 1205 if (init_attr->qp_type == IB_QPT_RAW_PACKET) 1206 err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn, 1207 (init_attr->cap.max_send_wr ? 1208 MLX4_RESERVE_ETH_BF_QP : 0) | 1209 (init_attr->cap.max_recv_wr ? 1210 MLX4_RESERVE_A0_QP : 0), 1211 qp->mqp.usage); 1212 else 1213 if (qp->flags & MLX4_IB_QP_NETIF) 1214 err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn); 1215 else 1216 err = mlx4_qp_reserve_range(dev->dev, 1, 1, 1217 &qpn, 0, qp->mqp.usage); 1218 if (err) 1219 goto err_proxy; 1220 } 1221 1222 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) 1223 qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK; 1224 1225 err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp); 1226 if (err) 1227 goto err_qpn; 1228 1229 if (init_attr->qp_type == IB_QPT_XRC_TGT) 1230 qp->mqp.qpn |= (1 << 23); 1231 1232 /* 1233 * Hardware wants QPN written in big-endian order (after 1234 * shifting) for send doorbell. Precompute this value to save 1235 * a little bit when posting sends. 1236 */ 1237 qp->doorbell_qpn = swab32(qp->mqp.qpn << 8); 1238 1239 qp->mqp.event = mlx4_ib_qp_event; 1240 1241 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags); 1242 mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq), 1243 to_mcq(init_attr->recv_cq)); 1244 /* Maintain device to QPs access, needed for further handling 1245 * via reset flow 1246 */ 1247 list_add_tail(&qp->qps_list, &dev->qp_list); 1248 /* Maintain CQ to QPs access, needed for further handling 1249 * via reset flow 1250 */ 1251 mcq = to_mcq(init_attr->send_cq); 1252 list_add_tail(&qp->cq_send_list, &mcq->send_qp_list); 1253 mcq = to_mcq(init_attr->recv_cq); 1254 list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list); 1255 mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq), 1256 to_mcq(init_attr->recv_cq)); 1257 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags); 1258 return 0; 1259 1260 err_qpn: 1261 if (!sqpn) { 1262 if (qp->flags & MLX4_IB_QP_NETIF) 1263 mlx4_ib_steer_qp_free(dev, qpn, 1); 1264 else 1265 mlx4_qp_release_range(dev->dev, qpn, 1); 1266 } 1267 err_proxy: 1268 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI) 1269 free_proxy_bufs(pd->device, qp); 1270 err_wrid: 1271 if (udata) { 1272 if (qp_has_rq(init_attr)) 1273 mlx4_ib_db_unmap_user(context, &qp->db); 1274 } else { 1275 kvfree(qp->sq.wrid); 1276 kvfree(qp->rq.wrid); 1277 } 1278 1279 err_mtt: 1280 mlx4_mtt_cleanup(dev->dev, &qp->mtt); 1281 1282 err_buf: 1283 if (!qp->umem) 1284 mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf); 1285 ib_umem_release(qp->umem); 1286 1287 err_db: 1288 if (!udata && qp_has_rq(init_attr)) 1289 mlx4_db_free(dev->dev, &qp->db); 1290 1291 err: 1292 kfree(qp->sqp); 1293 return err; 1294 } 1295 1296 static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state) 1297 { 1298 switch (state) { 1299 case IB_QPS_RESET: return MLX4_QP_STATE_RST; 1300 case IB_QPS_INIT: return MLX4_QP_STATE_INIT; 1301 case IB_QPS_RTR: return MLX4_QP_STATE_RTR; 1302 case IB_QPS_RTS: return MLX4_QP_STATE_RTS; 1303 case IB_QPS_SQD: return MLX4_QP_STATE_SQD; 1304 case IB_QPS_SQE: return MLX4_QP_STATE_SQER; 1305 case IB_QPS_ERR: return MLX4_QP_STATE_ERR; 1306 default: return -1; 1307 } 1308 } 1309 1310 static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq) 1311 __acquires(&send_cq->lock) __acquires(&recv_cq->lock) 1312 { 1313 if (send_cq == recv_cq) { 1314 spin_lock(&send_cq->lock); 1315 __acquire(&recv_cq->lock); 1316 } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) { 1317 spin_lock(&send_cq->lock); 1318 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING); 1319 } else { 1320 spin_lock(&recv_cq->lock); 1321 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING); 1322 } 1323 } 1324 1325 static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq) 1326 __releases(&send_cq->lock) __releases(&recv_cq->lock) 1327 { 1328 if (send_cq == recv_cq) { 1329 __release(&recv_cq->lock); 1330 spin_unlock(&send_cq->lock); 1331 } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) { 1332 spin_unlock(&recv_cq->lock); 1333 spin_unlock(&send_cq->lock); 1334 } else { 1335 spin_unlock(&send_cq->lock); 1336 spin_unlock(&recv_cq->lock); 1337 } 1338 } 1339 1340 static void del_gid_entries(struct mlx4_ib_qp *qp) 1341 { 1342 struct mlx4_ib_gid_entry *ge, *tmp; 1343 1344 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) { 1345 list_del(&ge->list); 1346 kfree(ge); 1347 } 1348 } 1349 1350 static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp) 1351 { 1352 if (qp->ibqp.qp_type == IB_QPT_XRC_TGT) 1353 return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd); 1354 else 1355 return to_mpd(qp->ibqp.pd); 1356 } 1357 1358 static void get_cqs(struct mlx4_ib_qp *qp, enum mlx4_ib_source_type src, 1359 struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq) 1360 { 1361 switch (qp->ibqp.qp_type) { 1362 case IB_QPT_XRC_TGT: 1363 *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq); 1364 *recv_cq = *send_cq; 1365 break; 1366 case IB_QPT_XRC_INI: 1367 *send_cq = to_mcq(qp->ibqp.send_cq); 1368 *recv_cq = *send_cq; 1369 break; 1370 default: 1371 *recv_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.recv_cq) : 1372 to_mcq(qp->ibwq.cq); 1373 *send_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.send_cq) : 1374 *recv_cq; 1375 break; 1376 } 1377 } 1378 1379 static void destroy_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp) 1380 { 1381 if (qp->state != IB_QPS_RESET) { 1382 int i; 1383 1384 for (i = 0; i < (1 << qp->ibqp.rwq_ind_tbl->log_ind_tbl_size); 1385 i++) { 1386 struct ib_wq *ibwq = qp->ibqp.rwq_ind_tbl->ind_tbl[i]; 1387 struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq); 1388 1389 mutex_lock(&wq->mutex); 1390 1391 wq->rss_usecnt--; 1392 1393 mutex_unlock(&wq->mutex); 1394 } 1395 1396 if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state), 1397 MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp)) 1398 pr_warn("modify QP %06x to RESET failed.\n", 1399 qp->mqp.qpn); 1400 } 1401 1402 mlx4_qp_remove(dev->dev, &qp->mqp); 1403 mlx4_qp_free(dev->dev, &qp->mqp); 1404 mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1); 1405 del_gid_entries(qp); 1406 } 1407 1408 static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp, 1409 enum mlx4_ib_source_type src, 1410 struct ib_udata *udata) 1411 { 1412 struct mlx4_ib_cq *send_cq, *recv_cq; 1413 unsigned long flags; 1414 1415 if (qp->state != IB_QPS_RESET) { 1416 if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state), 1417 MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp)) 1418 pr_warn("modify QP %06x to RESET failed.\n", 1419 qp->mqp.qpn); 1420 if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) { 1421 mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac); 1422 qp->pri.smac = 0; 1423 qp->pri.smac_port = 0; 1424 } 1425 if (qp->alt.smac) { 1426 mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac); 1427 qp->alt.smac = 0; 1428 } 1429 if (qp->pri.vid < 0x1000) { 1430 mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid); 1431 qp->pri.vid = 0xFFFF; 1432 qp->pri.candidate_vid = 0xFFFF; 1433 qp->pri.update_vid = 0; 1434 } 1435 if (qp->alt.vid < 0x1000) { 1436 mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid); 1437 qp->alt.vid = 0xFFFF; 1438 qp->alt.candidate_vid = 0xFFFF; 1439 qp->alt.update_vid = 0; 1440 } 1441 } 1442 1443 get_cqs(qp, src, &send_cq, &recv_cq); 1444 1445 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags); 1446 mlx4_ib_lock_cqs(send_cq, recv_cq); 1447 1448 /* del from lists under both locks above to protect reset flow paths */ 1449 list_del(&qp->qps_list); 1450 list_del(&qp->cq_send_list); 1451 list_del(&qp->cq_recv_list); 1452 if (!udata) { 1453 __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn, 1454 qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL); 1455 if (send_cq != recv_cq) 1456 __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL); 1457 } 1458 1459 mlx4_qp_remove(dev->dev, &qp->mqp); 1460 1461 mlx4_ib_unlock_cqs(send_cq, recv_cq); 1462 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags); 1463 1464 mlx4_qp_free(dev->dev, &qp->mqp); 1465 1466 if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) { 1467 if (qp->flags & MLX4_IB_QP_NETIF) 1468 mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1); 1469 else if (src == MLX4_IB_RWQ_SRC) 1470 mlx4_ib_release_wqn( 1471 rdma_udata_to_drv_context( 1472 udata, 1473 struct mlx4_ib_ucontext, 1474 ibucontext), 1475 qp, 1); 1476 else 1477 mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1); 1478 } 1479 1480 mlx4_mtt_cleanup(dev->dev, &qp->mtt); 1481 1482 if (udata) { 1483 if (qp->rq.wqe_cnt) { 1484 struct mlx4_ib_ucontext *mcontext = 1485 rdma_udata_to_drv_context( 1486 udata, 1487 struct mlx4_ib_ucontext, 1488 ibucontext); 1489 1490 mlx4_ib_db_unmap_user(mcontext, &qp->db); 1491 } 1492 } else { 1493 kvfree(qp->sq.wrid); 1494 kvfree(qp->rq.wrid); 1495 if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER | 1496 MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) 1497 free_proxy_bufs(&dev->ib_dev, qp); 1498 mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf); 1499 if (qp->rq.wqe_cnt) 1500 mlx4_db_free(dev->dev, &qp->db); 1501 } 1502 ib_umem_release(qp->umem); 1503 1504 del_gid_entries(qp); 1505 } 1506 1507 static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr) 1508 { 1509 /* Native or PPF */ 1510 if (!mlx4_is_mfunc(dev->dev) || 1511 (mlx4_is_master(dev->dev) && 1512 attr->create_flags & MLX4_IB_SRIOV_SQP)) { 1513 return dev->dev->phys_caps.base_sqpn + 1514 (attr->qp_type == IB_QPT_SMI ? 0 : 2) + 1515 attr->port_num - 1; 1516 } 1517 /* PF or VF -- creating proxies */ 1518 if (attr->qp_type == IB_QPT_SMI) 1519 return dev->dev->caps.spec_qps[attr->port_num - 1].qp0_proxy; 1520 else 1521 return dev->dev->caps.spec_qps[attr->port_num - 1].qp1_proxy; 1522 } 1523 1524 static int _mlx4_ib_create_qp(struct ib_pd *pd, struct mlx4_ib_qp *qp, 1525 struct ib_qp_init_attr *init_attr, 1526 struct ib_udata *udata) 1527 { 1528 int err; 1529 int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK; 1530 u16 xrcdn = 0; 1531 1532 if (init_attr->rwq_ind_tbl) 1533 return _mlx4_ib_create_qp_rss(pd, qp, init_attr, udata); 1534 1535 /* 1536 * We only support LSO, vendor flag1, and multicast loopback blocking, 1537 * and only for kernel UD QPs. 1538 */ 1539 if (init_attr->create_flags & ~(MLX4_IB_QP_LSO | 1540 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK | 1541 MLX4_IB_SRIOV_TUNNEL_QP | 1542 MLX4_IB_SRIOV_SQP | 1543 MLX4_IB_QP_NETIF | 1544 MLX4_IB_QP_CREATE_ROCE_V2_GSI)) 1545 return -EOPNOTSUPP; 1546 1547 if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) { 1548 if (init_attr->qp_type != IB_QPT_UD) 1549 return -EINVAL; 1550 } 1551 1552 if (init_attr->create_flags) { 1553 if (udata && init_attr->create_flags & ~(sup_u_create_flags)) 1554 return -EINVAL; 1555 1556 if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP | 1557 MLX4_IB_QP_CREATE_ROCE_V2_GSI | 1558 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) && 1559 init_attr->qp_type != IB_QPT_UD) || 1560 (init_attr->create_flags & MLX4_IB_SRIOV_SQP && 1561 init_attr->qp_type > IB_QPT_GSI) || 1562 (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI && 1563 init_attr->qp_type != IB_QPT_GSI)) 1564 return -EINVAL; 1565 } 1566 1567 switch (init_attr->qp_type) { 1568 case IB_QPT_XRC_TGT: 1569 pd = to_mxrcd(init_attr->xrcd)->pd; 1570 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn; 1571 init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq; 1572 fallthrough; 1573 case IB_QPT_XRC_INI: 1574 if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)) 1575 return -ENOSYS; 1576 init_attr->recv_cq = init_attr->send_cq; 1577 fallthrough; 1578 case IB_QPT_RC: 1579 case IB_QPT_UC: 1580 case IB_QPT_RAW_PACKET: 1581 case IB_QPT_UD: 1582 qp->pri.vid = 0xFFFF; 1583 qp->alt.vid = 0xFFFF; 1584 err = create_qp_common(pd, init_attr, udata, 0, qp); 1585 if (err) 1586 return err; 1587 1588 qp->ibqp.qp_num = qp->mqp.qpn; 1589 qp->xrcdn = xrcdn; 1590 break; 1591 case IB_QPT_SMI: 1592 case IB_QPT_GSI: 1593 { 1594 int sqpn; 1595 1596 if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) { 1597 int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev, 1598 1, 1, &sqpn, 0, 1599 MLX4_RES_USAGE_DRIVER); 1600 1601 if (res) 1602 return res; 1603 } else { 1604 sqpn = get_sqp_num(to_mdev(pd->device), init_attr); 1605 } 1606 1607 qp->pri.vid = 0xFFFF; 1608 qp->alt.vid = 0xFFFF; 1609 err = create_qp_common(pd, init_attr, udata, sqpn, qp); 1610 if (err) 1611 return err; 1612 1613 if (init_attr->create_flags & 1614 (MLX4_IB_SRIOV_SQP | MLX4_IB_SRIOV_TUNNEL_QP)) 1615 /* Internal QP created with ib_create_qp */ 1616 rdma_restrack_no_track(&qp->ibqp.res); 1617 1618 qp->port = init_attr->port_num; 1619 qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1620 init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1; 1621 break; 1622 } 1623 default: 1624 /* Don't support raw QPs */ 1625 return -EOPNOTSUPP; 1626 } 1627 return 0; 1628 } 1629 1630 int mlx4_ib_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr, 1631 struct ib_udata *udata) 1632 { 1633 struct ib_device *device = ibqp->device; 1634 struct mlx4_ib_dev *dev = to_mdev(device); 1635 struct mlx4_ib_qp *qp = to_mqp(ibqp); 1636 struct ib_pd *pd = ibqp->pd; 1637 int ret; 1638 1639 mutex_init(&qp->mutex); 1640 ret = _mlx4_ib_create_qp(pd, qp, init_attr, udata); 1641 if (ret) 1642 return ret; 1643 1644 if (init_attr->qp_type == IB_QPT_GSI && 1645 !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) { 1646 struct mlx4_ib_sqp *sqp = qp->sqp; 1647 int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num); 1648 1649 if (is_eth && 1650 dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) { 1651 init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI; 1652 sqp->roce_v2_gsi = ib_create_qp(pd, init_attr); 1653 1654 if (IS_ERR(sqp->roce_v2_gsi)) { 1655 pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi)); 1656 sqp->roce_v2_gsi = NULL; 1657 } else { 1658 to_mqp(sqp->roce_v2_gsi)->flags |= 1659 MLX4_IB_ROCE_V2_GSI_QP; 1660 } 1661 1662 init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI; 1663 } 1664 } 1665 return 0; 1666 } 1667 1668 static int _mlx4_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata) 1669 { 1670 struct mlx4_ib_dev *dev = to_mdev(qp->device); 1671 struct mlx4_ib_qp *mqp = to_mqp(qp); 1672 1673 if (is_qp0(dev, mqp)) 1674 mlx4_CLOSE_PORT(dev->dev, mqp->port); 1675 1676 if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI && 1677 dev->qp1_proxy[mqp->port - 1] == mqp) { 1678 mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]); 1679 dev->qp1_proxy[mqp->port - 1] = NULL; 1680 mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]); 1681 } 1682 1683 if (mqp->counter_index) 1684 mlx4_ib_free_qp_counter(dev, mqp); 1685 1686 if (qp->rwq_ind_tbl) { 1687 destroy_qp_rss(dev, mqp); 1688 } else { 1689 destroy_qp_common(dev, mqp, MLX4_IB_QP_SRC, udata); 1690 } 1691 1692 kfree(mqp->sqp); 1693 return 0; 1694 } 1695 1696 int mlx4_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata) 1697 { 1698 struct mlx4_ib_qp *mqp = to_mqp(qp); 1699 1700 if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) { 1701 struct mlx4_ib_sqp *sqp = mqp->sqp; 1702 1703 if (sqp->roce_v2_gsi) 1704 ib_destroy_qp(sqp->roce_v2_gsi); 1705 } 1706 1707 return _mlx4_ib_destroy_qp(qp, udata); 1708 } 1709 1710 static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type) 1711 { 1712 switch (type) { 1713 case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC; 1714 case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC; 1715 case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD; 1716 case MLX4_IB_QPT_XRC_INI: 1717 case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC; 1718 case MLX4_IB_QPT_SMI: 1719 case MLX4_IB_QPT_GSI: 1720 case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX; 1721 1722 case MLX4_IB_QPT_PROXY_SMI_OWNER: 1723 case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ? 1724 MLX4_QP_ST_MLX : -1); 1725 case MLX4_IB_QPT_PROXY_SMI: 1726 case MLX4_IB_QPT_TUN_SMI: 1727 case MLX4_IB_QPT_PROXY_GSI: 1728 case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ? 1729 MLX4_QP_ST_UD : -1); 1730 default: return -1; 1731 } 1732 } 1733 1734 static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr, 1735 int attr_mask) 1736 { 1737 u8 dest_rd_atomic; 1738 u32 access_flags; 1739 u32 hw_access_flags = 0; 1740 1741 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) 1742 dest_rd_atomic = attr->max_dest_rd_atomic; 1743 else 1744 dest_rd_atomic = qp->resp_depth; 1745 1746 if (attr_mask & IB_QP_ACCESS_FLAGS) 1747 access_flags = attr->qp_access_flags; 1748 else 1749 access_flags = qp->atomic_rd_en; 1750 1751 if (!dest_rd_atomic) 1752 access_flags &= IB_ACCESS_REMOTE_WRITE; 1753 1754 if (access_flags & IB_ACCESS_REMOTE_READ) 1755 hw_access_flags |= MLX4_QP_BIT_RRE; 1756 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) 1757 hw_access_flags |= MLX4_QP_BIT_RAE; 1758 if (access_flags & IB_ACCESS_REMOTE_WRITE) 1759 hw_access_flags |= MLX4_QP_BIT_RWE; 1760 1761 return cpu_to_be32(hw_access_flags); 1762 } 1763 1764 static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr, 1765 int attr_mask) 1766 { 1767 if (attr_mask & IB_QP_PKEY_INDEX) 1768 sqp->pkey_index = attr->pkey_index; 1769 if (attr_mask & IB_QP_QKEY) 1770 sqp->qkey = attr->qkey; 1771 if (attr_mask & IB_QP_SQ_PSN) 1772 sqp->send_psn = attr->sq_psn; 1773 } 1774 1775 static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port) 1776 { 1777 path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6); 1778 } 1779 1780 static int _mlx4_set_path(struct mlx4_ib_dev *dev, 1781 const struct rdma_ah_attr *ah, 1782 u64 smac, u16 vlan_tag, struct mlx4_qp_path *path, 1783 struct mlx4_roce_smac_vlan_info *smac_info, u8 port) 1784 { 1785 int vidx; 1786 int smac_index; 1787 int err; 1788 1789 path->grh_mylmc = rdma_ah_get_path_bits(ah) & 0x7f; 1790 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah)); 1791 if (rdma_ah_get_static_rate(ah)) { 1792 path->static_rate = rdma_ah_get_static_rate(ah) + 1793 MLX4_STAT_RATE_OFFSET; 1794 while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET && 1795 !(1 << path->static_rate & dev->dev->caps.stat_rate_support)) 1796 --path->static_rate; 1797 } else 1798 path->static_rate = 0; 1799 1800 if (rdma_ah_get_ah_flags(ah) & IB_AH_GRH) { 1801 const struct ib_global_route *grh = rdma_ah_read_grh(ah); 1802 int real_sgid_index = 1803 mlx4_ib_gid_index_to_real_index(dev, grh->sgid_attr); 1804 1805 if (real_sgid_index < 0) 1806 return real_sgid_index; 1807 if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) { 1808 pr_err("sgid_index (%u) too large. max is %d\n", 1809 real_sgid_index, dev->dev->caps.gid_table_len[port] - 1); 1810 return -1; 1811 } 1812 1813 path->grh_mylmc |= 1 << 7; 1814 path->mgid_index = real_sgid_index; 1815 path->hop_limit = grh->hop_limit; 1816 path->tclass_flowlabel = 1817 cpu_to_be32((grh->traffic_class << 20) | 1818 (grh->flow_label)); 1819 memcpy(path->rgid, grh->dgid.raw, 16); 1820 } 1821 1822 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) { 1823 if (!(rdma_ah_get_ah_flags(ah) & IB_AH_GRH)) 1824 return -1; 1825 1826 path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | 1827 ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 7) << 3); 1828 1829 path->feup |= MLX4_FEUP_FORCE_ETH_UP; 1830 if (vlan_tag < 0x1000) { 1831 if (smac_info->vid < 0x1000) { 1832 /* both valid vlan ids */ 1833 if (smac_info->vid != vlan_tag) { 1834 /* different VIDs. unreg old and reg new */ 1835 err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx); 1836 if (err) 1837 return err; 1838 smac_info->candidate_vid = vlan_tag; 1839 smac_info->candidate_vlan_index = vidx; 1840 smac_info->candidate_vlan_port = port; 1841 smac_info->update_vid = 1; 1842 path->vlan_index = vidx; 1843 } else { 1844 path->vlan_index = smac_info->vlan_index; 1845 } 1846 } else { 1847 /* no current vlan tag in qp */ 1848 err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx); 1849 if (err) 1850 return err; 1851 smac_info->candidate_vid = vlan_tag; 1852 smac_info->candidate_vlan_index = vidx; 1853 smac_info->candidate_vlan_port = port; 1854 smac_info->update_vid = 1; 1855 path->vlan_index = vidx; 1856 } 1857 path->feup |= MLX4_FVL_FORCE_ETH_VLAN; 1858 path->fl = 1 << 6; 1859 } else { 1860 /* have current vlan tag. unregister it at modify-qp success */ 1861 if (smac_info->vid < 0x1000) { 1862 smac_info->candidate_vid = 0xFFFF; 1863 smac_info->update_vid = 1; 1864 } 1865 } 1866 1867 /* get smac_index for RoCE use. 1868 * If no smac was yet assigned, register one. 1869 * If one was already assigned, but the new mac differs, 1870 * unregister the old one and register the new one. 1871 */ 1872 if ((!smac_info->smac && !smac_info->smac_port) || 1873 smac_info->smac != smac) { 1874 /* register candidate now, unreg if needed, after success */ 1875 smac_index = mlx4_register_mac(dev->dev, port, smac); 1876 if (smac_index >= 0) { 1877 smac_info->candidate_smac_index = smac_index; 1878 smac_info->candidate_smac = smac; 1879 smac_info->candidate_smac_port = port; 1880 } else { 1881 return -EINVAL; 1882 } 1883 } else { 1884 smac_index = smac_info->smac_index; 1885 } 1886 memcpy(path->dmac, ah->roce.dmac, 6); 1887 path->ackto = MLX4_IB_LINK_TYPE_ETH; 1888 /* put MAC table smac index for IBoE */ 1889 path->grh_mylmc = (u8) (smac_index) | 0x80; 1890 } else { 1891 path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | 1892 ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 0xf) << 2); 1893 } 1894 1895 return 0; 1896 } 1897 1898 static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp, 1899 enum ib_qp_attr_mask qp_attr_mask, 1900 struct mlx4_ib_qp *mqp, 1901 struct mlx4_qp_path *path, u8 port, 1902 u16 vlan_id, u8 *smac) 1903 { 1904 return _mlx4_set_path(dev, &qp->ah_attr, 1905 ether_addr_to_u64(smac), 1906 vlan_id, 1907 path, &mqp->pri, port); 1908 } 1909 1910 static int mlx4_set_alt_path(struct mlx4_ib_dev *dev, 1911 const struct ib_qp_attr *qp, 1912 enum ib_qp_attr_mask qp_attr_mask, 1913 struct mlx4_ib_qp *mqp, 1914 struct mlx4_qp_path *path, u8 port) 1915 { 1916 return _mlx4_set_path(dev, &qp->alt_ah_attr, 1917 0, 1918 0xffff, 1919 path, &mqp->alt, port); 1920 } 1921 1922 static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp) 1923 { 1924 struct mlx4_ib_gid_entry *ge, *tmp; 1925 1926 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) { 1927 if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) { 1928 ge->added = 1; 1929 ge->port = qp->port; 1930 } 1931 } 1932 } 1933 1934 static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev, 1935 struct mlx4_ib_qp *qp, 1936 struct mlx4_qp_context *context) 1937 { 1938 u64 u64_mac; 1939 int smac_index; 1940 1941 u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]); 1942 1943 context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6); 1944 if (!qp->pri.smac && !qp->pri.smac_port) { 1945 smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac); 1946 if (smac_index >= 0) { 1947 qp->pri.candidate_smac_index = smac_index; 1948 qp->pri.candidate_smac = u64_mac; 1949 qp->pri.candidate_smac_port = qp->port; 1950 context->pri_path.grh_mylmc = 0x80 | (u8) smac_index; 1951 } else { 1952 return -ENOENT; 1953 } 1954 } 1955 return 0; 1956 } 1957 1958 static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp) 1959 { 1960 struct counter_index *new_counter_index; 1961 int err; 1962 u32 tmp_idx; 1963 1964 if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) != 1965 IB_LINK_LAYER_ETHERNET || 1966 !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) || 1967 !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK)) 1968 return 0; 1969 1970 err = mlx4_counter_alloc(dev->dev, &tmp_idx, MLX4_RES_USAGE_DRIVER); 1971 if (err) 1972 return err; 1973 1974 new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL); 1975 if (!new_counter_index) { 1976 mlx4_counter_free(dev->dev, tmp_idx); 1977 return -ENOMEM; 1978 } 1979 1980 new_counter_index->index = tmp_idx; 1981 new_counter_index->allocated = 1; 1982 qp->counter_index = new_counter_index; 1983 1984 mutex_lock(&dev->counters_table[qp->port - 1].mutex); 1985 list_add_tail(&new_counter_index->list, 1986 &dev->counters_table[qp->port - 1].counters_list); 1987 mutex_unlock(&dev->counters_table[qp->port - 1].mutex); 1988 1989 return 0; 1990 } 1991 1992 enum { 1993 MLX4_QPC_ROCE_MODE_1 = 0, 1994 MLX4_QPC_ROCE_MODE_2 = 2, 1995 MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff 1996 }; 1997 1998 static u8 gid_type_to_qpc(enum ib_gid_type gid_type) 1999 { 2000 switch (gid_type) { 2001 case IB_GID_TYPE_ROCE: 2002 return MLX4_QPC_ROCE_MODE_1; 2003 case IB_GID_TYPE_ROCE_UDP_ENCAP: 2004 return MLX4_QPC_ROCE_MODE_2; 2005 default: 2006 return MLX4_QPC_ROCE_MODE_UNDEFINED; 2007 } 2008 } 2009 2010 /* 2011 * Go over all RSS QP's childes (WQs) and apply their HW state according to 2012 * their logic state if the RSS QP is the first RSS QP associated for the WQ. 2013 */ 2014 static int bringup_rss_rwqs(struct ib_rwq_ind_table *ind_tbl, u8 port_num, 2015 struct ib_udata *udata) 2016 { 2017 int err = 0; 2018 int i; 2019 2020 for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) { 2021 struct ib_wq *ibwq = ind_tbl->ind_tbl[i]; 2022 struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq); 2023 2024 mutex_lock(&wq->mutex); 2025 2026 /* Mlx4_ib restrictions: 2027 * WQ's is associated to a port according to the RSS QP it is 2028 * associates to. 2029 * In case the WQ is associated to a different port by another 2030 * RSS QP, return a failure. 2031 */ 2032 if ((wq->rss_usecnt > 0) && (wq->port != port_num)) { 2033 err = -EINVAL; 2034 mutex_unlock(&wq->mutex); 2035 break; 2036 } 2037 wq->port = port_num; 2038 if ((wq->rss_usecnt == 0) && (ibwq->state == IB_WQS_RDY)) { 2039 err = _mlx4_ib_modify_wq(ibwq, IB_WQS_RDY, udata); 2040 if (err) { 2041 mutex_unlock(&wq->mutex); 2042 break; 2043 } 2044 } 2045 wq->rss_usecnt++; 2046 2047 mutex_unlock(&wq->mutex); 2048 } 2049 2050 if (i && err) { 2051 int j; 2052 2053 for (j = (i - 1); j >= 0; j--) { 2054 struct ib_wq *ibwq = ind_tbl->ind_tbl[j]; 2055 struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq); 2056 2057 mutex_lock(&wq->mutex); 2058 2059 if ((wq->rss_usecnt == 1) && 2060 (ibwq->state == IB_WQS_RDY)) 2061 if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET, 2062 udata)) 2063 pr_warn("failed to reverse WQN=0x%06x\n", 2064 ibwq->wq_num); 2065 wq->rss_usecnt--; 2066 2067 mutex_unlock(&wq->mutex); 2068 } 2069 } 2070 2071 return err; 2072 } 2073 2074 static void bring_down_rss_rwqs(struct ib_rwq_ind_table *ind_tbl, 2075 struct ib_udata *udata) 2076 { 2077 int i; 2078 2079 for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) { 2080 struct ib_wq *ibwq = ind_tbl->ind_tbl[i]; 2081 struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq); 2082 2083 mutex_lock(&wq->mutex); 2084 2085 if ((wq->rss_usecnt == 1) && (ibwq->state == IB_WQS_RDY)) 2086 if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET, udata)) 2087 pr_warn("failed to reverse WQN=%x\n", 2088 ibwq->wq_num); 2089 wq->rss_usecnt--; 2090 2091 mutex_unlock(&wq->mutex); 2092 } 2093 } 2094 2095 static void fill_qp_rss_context(struct mlx4_qp_context *context, 2096 struct mlx4_ib_qp *qp) 2097 { 2098 struct mlx4_rss_context *rss_context; 2099 2100 rss_context = (void *)context + offsetof(struct mlx4_qp_context, 2101 pri_path) + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH; 2102 2103 rss_context->base_qpn = cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz); 2104 rss_context->default_qpn = 2105 cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz & 0xffffff); 2106 if (qp->rss_ctx->flags & (MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6)) 2107 rss_context->base_qpn_udp = rss_context->default_qpn; 2108 rss_context->flags = qp->rss_ctx->flags; 2109 /* Currently support just toeplitz */ 2110 rss_context->hash_fn = MLX4_RSS_HASH_TOP; 2111 2112 memcpy(rss_context->rss_key, qp->rss_ctx->rss_key, 2113 MLX4_EN_RSS_KEY_SIZE); 2114 } 2115 2116 static int __mlx4_ib_modify_qp(void *src, enum mlx4_ib_source_type src_type, 2117 const struct ib_qp_attr *attr, int attr_mask, 2118 enum ib_qp_state cur_state, 2119 enum ib_qp_state new_state, 2120 struct ib_udata *udata) 2121 { 2122 struct ib_srq *ibsrq; 2123 const struct ib_gid_attr *gid_attr = NULL; 2124 struct ib_rwq_ind_table *rwq_ind_tbl; 2125 enum ib_qp_type qp_type; 2126 struct mlx4_ib_dev *dev; 2127 struct mlx4_ib_qp *qp; 2128 struct mlx4_ib_pd *pd; 2129 struct mlx4_ib_cq *send_cq, *recv_cq; 2130 struct mlx4_ib_ucontext *ucontext = rdma_udata_to_drv_context( 2131 udata, struct mlx4_ib_ucontext, ibucontext); 2132 struct mlx4_qp_context *context; 2133 enum mlx4_qp_optpar optpar = 0; 2134 int sqd_event; 2135 int steer_qp = 0; 2136 int err = -EINVAL; 2137 int counter_index; 2138 2139 if (src_type == MLX4_IB_RWQ_SRC) { 2140 struct ib_wq *ibwq; 2141 2142 ibwq = (struct ib_wq *)src; 2143 ibsrq = NULL; 2144 rwq_ind_tbl = NULL; 2145 qp_type = IB_QPT_RAW_PACKET; 2146 qp = to_mqp((struct ib_qp *)ibwq); 2147 dev = to_mdev(ibwq->device); 2148 pd = to_mpd(ibwq->pd); 2149 } else { 2150 struct ib_qp *ibqp; 2151 2152 ibqp = (struct ib_qp *)src; 2153 ibsrq = ibqp->srq; 2154 rwq_ind_tbl = ibqp->rwq_ind_tbl; 2155 qp_type = ibqp->qp_type; 2156 qp = to_mqp(ibqp); 2157 dev = to_mdev(ibqp->device); 2158 pd = get_pd(qp); 2159 } 2160 2161 /* APM is not supported under RoCE */ 2162 if (attr_mask & IB_QP_ALT_PATH && 2163 rdma_port_get_link_layer(&dev->ib_dev, qp->port) == 2164 IB_LINK_LAYER_ETHERNET) 2165 return -ENOTSUPP; 2166 2167 context = kzalloc(sizeof *context, GFP_KERNEL); 2168 if (!context) 2169 return -ENOMEM; 2170 2171 context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) | 2172 (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16)); 2173 2174 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) 2175 context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11); 2176 else { 2177 optpar |= MLX4_QP_OPTPAR_PM_STATE; 2178 switch (attr->path_mig_state) { 2179 case IB_MIG_MIGRATED: 2180 context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11); 2181 break; 2182 case IB_MIG_REARM: 2183 context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11); 2184 break; 2185 case IB_MIG_ARMED: 2186 context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11); 2187 break; 2188 } 2189 } 2190 2191 if (qp->inl_recv_sz) 2192 context->param3 |= cpu_to_be32(1 << 25); 2193 2194 if (qp->flags & MLX4_IB_QP_SCATTER_FCS) 2195 context->param3 |= cpu_to_be32(1 << 29); 2196 2197 if (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI) 2198 context->mtu_msgmax = (IB_MTU_4096 << 5) | 11; 2199 else if (qp_type == IB_QPT_RAW_PACKET) 2200 context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX; 2201 else if (qp_type == IB_QPT_UD) { 2202 if (qp->flags & MLX4_IB_QP_LSO) 2203 context->mtu_msgmax = (IB_MTU_4096 << 5) | 2204 ilog2(dev->dev->caps.max_gso_sz); 2205 else 2206 context->mtu_msgmax = (IB_MTU_4096 << 5) | 13; 2207 } else if (attr_mask & IB_QP_PATH_MTU) { 2208 if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) { 2209 pr_err("path MTU (%u) is invalid\n", 2210 attr->path_mtu); 2211 goto out; 2212 } 2213 context->mtu_msgmax = (attr->path_mtu << 5) | 2214 ilog2(dev->dev->caps.max_msg_sz); 2215 } 2216 2217 if (!rwq_ind_tbl) { /* PRM RSS receive side should be left zeros */ 2218 if (qp->rq.wqe_cnt) 2219 context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3; 2220 context->rq_size_stride |= qp->rq.wqe_shift - 4; 2221 } 2222 2223 if (qp->sq.wqe_cnt) 2224 context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3; 2225 context->sq_size_stride |= qp->sq.wqe_shift - 4; 2226 2227 if (new_state == IB_QPS_RESET && qp->counter_index) 2228 mlx4_ib_free_qp_counter(dev, qp); 2229 2230 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) { 2231 context->sq_size_stride |= !!qp->sq_no_prefetch << 7; 2232 context->xrcd = cpu_to_be32((u32) qp->xrcdn); 2233 if (qp_type == IB_QPT_RAW_PACKET) 2234 context->param3 |= cpu_to_be32(1 << 30); 2235 } 2236 2237 if (ucontext) 2238 context->usr_page = cpu_to_be32( 2239 mlx4_to_hw_uar_index(dev->dev, ucontext->uar.index)); 2240 else 2241 context->usr_page = cpu_to_be32( 2242 mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index)); 2243 2244 if (attr_mask & IB_QP_DEST_QPN) 2245 context->remote_qpn = cpu_to_be32(attr->dest_qp_num); 2246 2247 if (attr_mask & IB_QP_PORT) { 2248 if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD && 2249 !(attr_mask & IB_QP_AV)) { 2250 mlx4_set_sched(&context->pri_path, attr->port_num); 2251 optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE; 2252 } 2253 } 2254 2255 if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) { 2256 err = create_qp_lb_counter(dev, qp); 2257 if (err) 2258 goto out; 2259 2260 counter_index = 2261 dev->counters_table[qp->port - 1].default_counter; 2262 if (qp->counter_index) 2263 counter_index = qp->counter_index->index; 2264 2265 if (counter_index != -1) { 2266 context->pri_path.counter_index = counter_index; 2267 optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX; 2268 if (qp->counter_index) { 2269 context->pri_path.fl |= 2270 MLX4_FL_ETH_SRC_CHECK_MC_LB; 2271 context->pri_path.vlan_control |= 2272 MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER; 2273 } 2274 } else 2275 context->pri_path.counter_index = 2276 MLX4_SINK_COUNTER_INDEX(dev->dev); 2277 2278 if (qp->flags & MLX4_IB_QP_NETIF) { 2279 mlx4_ib_steer_qp_reg(dev, qp, 1); 2280 steer_qp = 1; 2281 } 2282 2283 if (qp_type == IB_QPT_GSI) { 2284 enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ? 2285 IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE; 2286 u8 qpc_roce_mode = gid_type_to_qpc(gid_type); 2287 2288 context->rlkey_roce_mode |= (qpc_roce_mode << 6); 2289 } 2290 } 2291 2292 if (attr_mask & IB_QP_PKEY_INDEX) { 2293 if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) 2294 context->pri_path.disable_pkey_check = 0x40; 2295 context->pri_path.pkey_index = attr->pkey_index; 2296 optpar |= MLX4_QP_OPTPAR_PKEY_INDEX; 2297 } 2298 2299 if (attr_mask & IB_QP_AV) { 2300 u8 port_num = mlx4_is_bonded(dev->dev) ? 1 : 2301 attr_mask & IB_QP_PORT ? attr->port_num : qp->port; 2302 u16 vlan = 0xffff; 2303 u8 smac[ETH_ALEN]; 2304 int is_eth = 2305 rdma_cap_eth_ah(&dev->ib_dev, port_num) && 2306 rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH; 2307 2308 if (is_eth) { 2309 gid_attr = attr->ah_attr.grh.sgid_attr; 2310 err = rdma_read_gid_l2_fields(gid_attr, &vlan, 2311 &smac[0]); 2312 if (err) 2313 goto out; 2314 } 2315 2316 if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path, 2317 port_num, vlan, smac)) 2318 goto out; 2319 2320 optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH | 2321 MLX4_QP_OPTPAR_SCHED_QUEUE); 2322 2323 if (is_eth && 2324 (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) { 2325 u8 qpc_roce_mode = gid_type_to_qpc(gid_attr->gid_type); 2326 2327 if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) { 2328 err = -EINVAL; 2329 goto out; 2330 } 2331 context->rlkey_roce_mode |= (qpc_roce_mode << 6); 2332 } 2333 2334 } 2335 2336 if (attr_mask & IB_QP_TIMEOUT) { 2337 context->pri_path.ackto |= attr->timeout << 3; 2338 optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT; 2339 } 2340 2341 if (attr_mask & IB_QP_ALT_PATH) { 2342 if (attr->alt_port_num == 0 || 2343 attr->alt_port_num > dev->dev->caps.num_ports) 2344 goto out; 2345 2346 if (attr->alt_pkey_index >= 2347 dev->dev->caps.pkey_table_len[attr->alt_port_num]) 2348 goto out; 2349 2350 if (mlx4_set_alt_path(dev, attr, attr_mask, qp, 2351 &context->alt_path, 2352 attr->alt_port_num)) 2353 goto out; 2354 2355 context->alt_path.pkey_index = attr->alt_pkey_index; 2356 context->alt_path.ackto = attr->alt_timeout << 3; 2357 optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH; 2358 } 2359 2360 context->pd = cpu_to_be32(pd->pdn); 2361 2362 if (!rwq_ind_tbl) { 2363 context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28); 2364 get_cqs(qp, src_type, &send_cq, &recv_cq); 2365 } else { /* Set dummy CQs to be compatible with HV and PRM */ 2366 send_cq = to_mcq(rwq_ind_tbl->ind_tbl[0]->cq); 2367 recv_cq = send_cq; 2368 } 2369 context->cqn_send = cpu_to_be32(send_cq->mcq.cqn); 2370 context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn); 2371 2372 /* Set "fast registration enabled" for all kernel QPs */ 2373 if (!ucontext) 2374 context->params1 |= cpu_to_be32(1 << 11); 2375 2376 if (attr_mask & IB_QP_RNR_RETRY) { 2377 context->params1 |= cpu_to_be32(attr->rnr_retry << 13); 2378 optpar |= MLX4_QP_OPTPAR_RNR_RETRY; 2379 } 2380 2381 if (attr_mask & IB_QP_RETRY_CNT) { 2382 context->params1 |= cpu_to_be32(attr->retry_cnt << 16); 2383 optpar |= MLX4_QP_OPTPAR_RETRY_COUNT; 2384 } 2385 2386 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) { 2387 if (attr->max_rd_atomic) 2388 context->params1 |= 2389 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21); 2390 optpar |= MLX4_QP_OPTPAR_SRA_MAX; 2391 } 2392 2393 if (attr_mask & IB_QP_SQ_PSN) 2394 context->next_send_psn = cpu_to_be32(attr->sq_psn); 2395 2396 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) { 2397 if (attr->max_dest_rd_atomic) 2398 context->params2 |= 2399 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21); 2400 optpar |= MLX4_QP_OPTPAR_RRA_MAX; 2401 } 2402 2403 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) { 2404 context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask); 2405 optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE; 2406 } 2407 2408 if (ibsrq) 2409 context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC); 2410 2411 if (attr_mask & IB_QP_MIN_RNR_TIMER) { 2412 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24); 2413 optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT; 2414 } 2415 if (attr_mask & IB_QP_RQ_PSN) 2416 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn); 2417 2418 /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */ 2419 if (attr_mask & IB_QP_QKEY) { 2420 if (qp->mlx4_ib_qp_type & 2421 (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) 2422 context->qkey = cpu_to_be32(IB_QP_SET_QKEY); 2423 else { 2424 if (mlx4_is_mfunc(dev->dev) && 2425 !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) && 2426 (attr->qkey & MLX4_RESERVED_QKEY_MASK) == 2427 MLX4_RESERVED_QKEY_BASE) { 2428 pr_err("Cannot use reserved QKEY" 2429 " 0x%x (range 0xffff0000..0xffffffff" 2430 " is reserved)\n", attr->qkey); 2431 err = -EINVAL; 2432 goto out; 2433 } 2434 context->qkey = cpu_to_be32(attr->qkey); 2435 } 2436 optpar |= MLX4_QP_OPTPAR_Q_KEY; 2437 } 2438 2439 if (ibsrq) 2440 context->srqn = cpu_to_be32(1 << 24 | 2441 to_msrq(ibsrq)->msrq.srqn); 2442 2443 if (qp->rq.wqe_cnt && 2444 cur_state == IB_QPS_RESET && 2445 new_state == IB_QPS_INIT) 2446 context->db_rec_addr = cpu_to_be64(qp->db.dma); 2447 2448 if (cur_state == IB_QPS_INIT && 2449 new_state == IB_QPS_RTR && 2450 (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI || 2451 qp_type == IB_QPT_UD || qp_type == IB_QPT_RAW_PACKET)) { 2452 context->pri_path.sched_queue = (qp->port - 1) << 6; 2453 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI || 2454 qp->mlx4_ib_qp_type & 2455 (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) { 2456 context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE; 2457 if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI) 2458 context->pri_path.fl = 0x80; 2459 } else { 2460 if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) 2461 context->pri_path.fl = 0x80; 2462 context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE; 2463 } 2464 if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) == 2465 IB_LINK_LAYER_ETHERNET) { 2466 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI || 2467 qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) 2468 context->pri_path.feup = 1 << 7; /* don't fsm */ 2469 /* handle smac_index */ 2470 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD || 2471 qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI || 2472 qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) { 2473 err = handle_eth_ud_smac_index(dev, qp, context); 2474 if (err) { 2475 err = -EINVAL; 2476 goto out; 2477 } 2478 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI) 2479 dev->qp1_proxy[qp->port - 1] = qp; 2480 } 2481 } 2482 } 2483 2484 if (qp_type == IB_QPT_RAW_PACKET) { 2485 context->pri_path.ackto = (context->pri_path.ackto & 0xf8) | 2486 MLX4_IB_LINK_TYPE_ETH; 2487 if (dev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) { 2488 /* set QP to receive both tunneled & non-tunneled packets */ 2489 if (!rwq_ind_tbl) 2490 context->srqn = cpu_to_be32(7 << 28); 2491 } 2492 } 2493 2494 if (qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) { 2495 int is_eth = rdma_port_get_link_layer( 2496 &dev->ib_dev, qp->port) == 2497 IB_LINK_LAYER_ETHERNET; 2498 if (is_eth) { 2499 context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH; 2500 optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH; 2501 } 2502 } 2503 2504 if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD && 2505 attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify) 2506 sqd_event = 1; 2507 else 2508 sqd_event = 0; 2509 2510 if (!ucontext && 2511 cur_state == IB_QPS_RESET && 2512 new_state == IB_QPS_INIT) 2513 context->rlkey_roce_mode |= (1 << 4); 2514 2515 /* 2516 * Before passing a kernel QP to the HW, make sure that the 2517 * ownership bits of the send queue are set and the SQ 2518 * headroom is stamped so that the hardware doesn't start 2519 * processing stale work requests. 2520 */ 2521 if (!ucontext && 2522 cur_state == IB_QPS_RESET && 2523 new_state == IB_QPS_INIT) { 2524 struct mlx4_wqe_ctrl_seg *ctrl; 2525 int i; 2526 2527 for (i = 0; i < qp->sq.wqe_cnt; ++i) { 2528 ctrl = get_send_wqe(qp, i); 2529 ctrl->owner_opcode = cpu_to_be32(1 << 31); 2530 ctrl->qpn_vlan.fence_size = 2531 1 << (qp->sq.wqe_shift - 4); 2532 stamp_send_wqe(qp, i); 2533 } 2534 } 2535 2536 if (rwq_ind_tbl && 2537 cur_state == IB_QPS_RESET && 2538 new_state == IB_QPS_INIT) { 2539 fill_qp_rss_context(context, qp); 2540 context->flags |= cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET); 2541 } 2542 2543 err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state), 2544 to_mlx4_state(new_state), context, optpar, 2545 sqd_event, &qp->mqp); 2546 if (err) 2547 goto out; 2548 2549 qp->state = new_state; 2550 2551 if (attr_mask & IB_QP_ACCESS_FLAGS) 2552 qp->atomic_rd_en = attr->qp_access_flags; 2553 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) 2554 qp->resp_depth = attr->max_dest_rd_atomic; 2555 if (attr_mask & IB_QP_PORT) { 2556 qp->port = attr->port_num; 2557 update_mcg_macs(dev, qp); 2558 } 2559 if (attr_mask & IB_QP_ALT_PATH) 2560 qp->alt_port = attr->alt_port_num; 2561 2562 if (is_sqp(dev, qp)) 2563 store_sqp_attrs(qp->sqp, attr, attr_mask); 2564 2565 /* 2566 * If we moved QP0 to RTR, bring the IB link up; if we moved 2567 * QP0 to RESET or ERROR, bring the link back down. 2568 */ 2569 if (is_qp0(dev, qp)) { 2570 if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR) 2571 if (mlx4_INIT_PORT(dev->dev, qp->port)) 2572 pr_warn("INIT_PORT failed for port %d\n", 2573 qp->port); 2574 2575 if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR && 2576 (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR)) 2577 mlx4_CLOSE_PORT(dev->dev, qp->port); 2578 } 2579 2580 /* 2581 * If we moved a kernel QP to RESET, clean up all old CQ 2582 * entries and reinitialize the QP. 2583 */ 2584 if (new_state == IB_QPS_RESET) { 2585 if (!ucontext) { 2586 mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn, 2587 ibsrq ? to_msrq(ibsrq) : NULL); 2588 if (send_cq != recv_cq) 2589 mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL); 2590 2591 qp->rq.head = 0; 2592 qp->rq.tail = 0; 2593 qp->sq.head = 0; 2594 qp->sq.tail = 0; 2595 qp->sq_next_wqe = 0; 2596 if (qp->rq.wqe_cnt) 2597 *qp->db.db = 0; 2598 2599 if (qp->flags & MLX4_IB_QP_NETIF) 2600 mlx4_ib_steer_qp_reg(dev, qp, 0); 2601 } 2602 if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) { 2603 mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac); 2604 qp->pri.smac = 0; 2605 qp->pri.smac_port = 0; 2606 } 2607 if (qp->alt.smac) { 2608 mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac); 2609 qp->alt.smac = 0; 2610 } 2611 if (qp->pri.vid < 0x1000) { 2612 mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid); 2613 qp->pri.vid = 0xFFFF; 2614 qp->pri.candidate_vid = 0xFFFF; 2615 qp->pri.update_vid = 0; 2616 } 2617 2618 if (qp->alt.vid < 0x1000) { 2619 mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid); 2620 qp->alt.vid = 0xFFFF; 2621 qp->alt.candidate_vid = 0xFFFF; 2622 qp->alt.update_vid = 0; 2623 } 2624 } 2625 out: 2626 if (err && qp->counter_index) 2627 mlx4_ib_free_qp_counter(dev, qp); 2628 if (err && steer_qp) 2629 mlx4_ib_steer_qp_reg(dev, qp, 0); 2630 kfree(context); 2631 if (qp->pri.candidate_smac || 2632 (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) { 2633 if (err) { 2634 mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac); 2635 } else { 2636 if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) 2637 mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac); 2638 qp->pri.smac = qp->pri.candidate_smac; 2639 qp->pri.smac_index = qp->pri.candidate_smac_index; 2640 qp->pri.smac_port = qp->pri.candidate_smac_port; 2641 } 2642 qp->pri.candidate_smac = 0; 2643 qp->pri.candidate_smac_index = 0; 2644 qp->pri.candidate_smac_port = 0; 2645 } 2646 if (qp->alt.candidate_smac) { 2647 if (err) { 2648 mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac); 2649 } else { 2650 if (qp->alt.smac) 2651 mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac); 2652 qp->alt.smac = qp->alt.candidate_smac; 2653 qp->alt.smac_index = qp->alt.candidate_smac_index; 2654 qp->alt.smac_port = qp->alt.candidate_smac_port; 2655 } 2656 qp->alt.candidate_smac = 0; 2657 qp->alt.candidate_smac_index = 0; 2658 qp->alt.candidate_smac_port = 0; 2659 } 2660 2661 if (qp->pri.update_vid) { 2662 if (err) { 2663 if (qp->pri.candidate_vid < 0x1000) 2664 mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port, 2665 qp->pri.candidate_vid); 2666 } else { 2667 if (qp->pri.vid < 0x1000) 2668 mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, 2669 qp->pri.vid); 2670 qp->pri.vid = qp->pri.candidate_vid; 2671 qp->pri.vlan_port = qp->pri.candidate_vlan_port; 2672 qp->pri.vlan_index = qp->pri.candidate_vlan_index; 2673 } 2674 qp->pri.candidate_vid = 0xFFFF; 2675 qp->pri.update_vid = 0; 2676 } 2677 2678 if (qp->alt.update_vid) { 2679 if (err) { 2680 if (qp->alt.candidate_vid < 0x1000) 2681 mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port, 2682 qp->alt.candidate_vid); 2683 } else { 2684 if (qp->alt.vid < 0x1000) 2685 mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, 2686 qp->alt.vid); 2687 qp->alt.vid = qp->alt.candidate_vid; 2688 qp->alt.vlan_port = qp->alt.candidate_vlan_port; 2689 qp->alt.vlan_index = qp->alt.candidate_vlan_index; 2690 } 2691 qp->alt.candidate_vid = 0xFFFF; 2692 qp->alt.update_vid = 0; 2693 } 2694 2695 return err; 2696 } 2697 2698 enum { 2699 MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK = (IB_QP_STATE | 2700 IB_QP_PORT), 2701 }; 2702 2703 static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 2704 int attr_mask, struct ib_udata *udata) 2705 { 2706 struct mlx4_ib_dev *dev = to_mdev(ibqp->device); 2707 struct mlx4_ib_qp *qp = to_mqp(ibqp); 2708 enum ib_qp_state cur_state, new_state; 2709 int err = -EINVAL; 2710 mutex_lock(&qp->mutex); 2711 2712 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state; 2713 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; 2714 2715 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, 2716 attr_mask)) { 2717 pr_debug("qpn 0x%x: invalid attribute mask specified " 2718 "for transition %d to %d. qp_type %d," 2719 " attr_mask 0x%x\n", 2720 ibqp->qp_num, cur_state, new_state, 2721 ibqp->qp_type, attr_mask); 2722 goto out; 2723 } 2724 2725 if (ibqp->rwq_ind_tbl) { 2726 if (!(((cur_state == IB_QPS_RESET) && 2727 (new_state == IB_QPS_INIT)) || 2728 ((cur_state == IB_QPS_INIT) && 2729 (new_state == IB_QPS_RTR)))) { 2730 pr_debug("qpn 0x%x: RSS QP unsupported transition %d to %d\n", 2731 ibqp->qp_num, cur_state, new_state); 2732 2733 err = -EOPNOTSUPP; 2734 goto out; 2735 } 2736 2737 if (attr_mask & ~MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK) { 2738 pr_debug("qpn 0x%x: RSS QP unsupported attribute mask 0x%x for transition %d to %d\n", 2739 ibqp->qp_num, attr_mask, cur_state, new_state); 2740 2741 err = -EOPNOTSUPP; 2742 goto out; 2743 } 2744 } 2745 2746 if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) { 2747 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) { 2748 if ((ibqp->qp_type == IB_QPT_RC) || 2749 (ibqp->qp_type == IB_QPT_UD) || 2750 (ibqp->qp_type == IB_QPT_UC) || 2751 (ibqp->qp_type == IB_QPT_RAW_PACKET) || 2752 (ibqp->qp_type == IB_QPT_XRC_INI)) { 2753 attr->port_num = mlx4_ib_bond_next_port(dev); 2754 } 2755 } else { 2756 /* no sense in changing port_num 2757 * when ports are bonded */ 2758 attr_mask &= ~IB_QP_PORT; 2759 } 2760 } 2761 2762 if ((attr_mask & IB_QP_PORT) && 2763 (attr->port_num == 0 || attr->port_num > dev->num_ports)) { 2764 pr_debug("qpn 0x%x: invalid port number (%d) specified " 2765 "for transition %d to %d. qp_type %d\n", 2766 ibqp->qp_num, attr->port_num, cur_state, 2767 new_state, ibqp->qp_type); 2768 goto out; 2769 } 2770 2771 if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) && 2772 (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) != 2773 IB_LINK_LAYER_ETHERNET)) 2774 goto out; 2775 2776 if (attr_mask & IB_QP_PKEY_INDEX) { 2777 int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port; 2778 if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) { 2779 pr_debug("qpn 0x%x: invalid pkey index (%d) specified " 2780 "for transition %d to %d. qp_type %d\n", 2781 ibqp->qp_num, attr->pkey_index, cur_state, 2782 new_state, ibqp->qp_type); 2783 goto out; 2784 } 2785 } 2786 2787 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC && 2788 attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) { 2789 pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. " 2790 "Transition %d to %d. qp_type %d\n", 2791 ibqp->qp_num, attr->max_rd_atomic, cur_state, 2792 new_state, ibqp->qp_type); 2793 goto out; 2794 } 2795 2796 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC && 2797 attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) { 2798 pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. " 2799 "Transition %d to %d. qp_type %d\n", 2800 ibqp->qp_num, attr->max_dest_rd_atomic, cur_state, 2801 new_state, ibqp->qp_type); 2802 goto out; 2803 } 2804 2805 if (cur_state == new_state && cur_state == IB_QPS_RESET) { 2806 err = 0; 2807 goto out; 2808 } 2809 2810 if (ibqp->rwq_ind_tbl && (new_state == IB_QPS_INIT)) { 2811 err = bringup_rss_rwqs(ibqp->rwq_ind_tbl, attr->port_num, 2812 udata); 2813 if (err) 2814 goto out; 2815 } 2816 2817 err = __mlx4_ib_modify_qp(ibqp, MLX4_IB_QP_SRC, attr, attr_mask, 2818 cur_state, new_state, udata); 2819 2820 if (ibqp->rwq_ind_tbl && err) 2821 bring_down_rss_rwqs(ibqp->rwq_ind_tbl, udata); 2822 2823 if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) 2824 attr->port_num = 1; 2825 2826 out: 2827 mutex_unlock(&qp->mutex); 2828 return err; 2829 } 2830 2831 int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 2832 int attr_mask, struct ib_udata *udata) 2833 { 2834 struct mlx4_ib_qp *mqp = to_mqp(ibqp); 2835 int ret; 2836 2837 if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS) 2838 return -EOPNOTSUPP; 2839 2840 ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata); 2841 2842 if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) { 2843 struct mlx4_ib_sqp *sqp = mqp->sqp; 2844 int err = 0; 2845 2846 if (sqp->roce_v2_gsi) 2847 err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask); 2848 if (err) 2849 pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n", 2850 err); 2851 } 2852 return ret; 2853 } 2854 2855 static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey) 2856 { 2857 int i; 2858 for (i = 0; i < dev->caps.num_ports; i++) { 2859 if (qpn == dev->caps.spec_qps[i].qp0_proxy || 2860 qpn == dev->caps.spec_qps[i].qp0_tunnel) { 2861 *qkey = dev->caps.spec_qps[i].qp0_qkey; 2862 return 0; 2863 } 2864 } 2865 return -EINVAL; 2866 } 2867 2868 static int build_sriov_qp0_header(struct mlx4_ib_qp *qp, 2869 const struct ib_ud_wr *wr, 2870 void *wqe, unsigned *mlx_seg_len) 2871 { 2872 struct mlx4_ib_dev *mdev = to_mdev(qp->ibqp.device); 2873 struct mlx4_ib_sqp *sqp = qp->sqp; 2874 struct ib_device *ib_dev = qp->ibqp.device; 2875 struct mlx4_wqe_mlx_seg *mlx = wqe; 2876 struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx; 2877 struct mlx4_ib_ah *ah = to_mah(wr->ah); 2878 u16 pkey; 2879 u32 qkey; 2880 int send_size; 2881 int header_size; 2882 int spc; 2883 int err; 2884 int i; 2885 2886 if (wr->wr.opcode != IB_WR_SEND) 2887 return -EINVAL; 2888 2889 send_size = 0; 2890 2891 for (i = 0; i < wr->wr.num_sge; ++i) 2892 send_size += wr->wr.sg_list[i].length; 2893 2894 /* for proxy-qp0 sends, need to add in size of tunnel header */ 2895 /* for tunnel-qp0 sends, tunnel header is already in s/g list */ 2896 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) 2897 send_size += sizeof (struct mlx4_ib_tunnel_header); 2898 2899 ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header); 2900 2901 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) { 2902 sqp->ud_header.lrh.service_level = 2903 be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28; 2904 sqp->ud_header.lrh.destination_lid = 2905 cpu_to_be16(ah->av.ib.g_slid & 0x7f); 2906 sqp->ud_header.lrh.source_lid = 2907 cpu_to_be16(ah->av.ib.g_slid & 0x7f); 2908 } 2909 2910 mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE); 2911 2912 /* force loopback */ 2913 mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR); 2914 mlx->rlid = sqp->ud_header.lrh.destination_lid; 2915 2916 sqp->ud_header.lrh.virtual_lane = 0; 2917 sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED); 2918 err = ib_get_cached_pkey(ib_dev, qp->port, 0, &pkey); 2919 if (err) 2920 return err; 2921 sqp->ud_header.bth.pkey = cpu_to_be16(pkey); 2922 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER) 2923 sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn); 2924 else 2925 sqp->ud_header.bth.destination_qpn = 2926 cpu_to_be32(mdev->dev->caps.spec_qps[qp->port - 1].qp0_tunnel); 2927 2928 sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1)); 2929 if (mlx4_is_master(mdev->dev)) { 2930 if (mlx4_get_parav_qkey(mdev->dev, qp->mqp.qpn, &qkey)) 2931 return -EINVAL; 2932 } else { 2933 if (vf_get_qp0_qkey(mdev->dev, qp->mqp.qpn, &qkey)) 2934 return -EINVAL; 2935 } 2936 sqp->ud_header.deth.qkey = cpu_to_be32(qkey); 2937 sqp->ud_header.deth.source_qpn = cpu_to_be32(qp->mqp.qpn); 2938 2939 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY; 2940 sqp->ud_header.immediate_present = 0; 2941 2942 header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf); 2943 2944 /* 2945 * Inline data segments may not cross a 64 byte boundary. If 2946 * our UD header is bigger than the space available up to the 2947 * next 64 byte boundary in the WQE, use two inline data 2948 * segments to hold the UD header. 2949 */ 2950 spc = MLX4_INLINE_ALIGN - 2951 ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1)); 2952 if (header_size <= spc) { 2953 inl->byte_count = cpu_to_be32(1 << 31 | header_size); 2954 memcpy(inl + 1, sqp->header_buf, header_size); 2955 i = 1; 2956 } else { 2957 inl->byte_count = cpu_to_be32(1 << 31 | spc); 2958 memcpy(inl + 1, sqp->header_buf, spc); 2959 2960 inl = (void *) (inl + 1) + spc; 2961 memcpy(inl + 1, sqp->header_buf + spc, header_size - spc); 2962 /* 2963 * Need a barrier here to make sure all the data is 2964 * visible before the byte_count field is set. 2965 * Otherwise the HCA prefetcher could grab the 64-byte 2966 * chunk with this inline segment and get a valid (!= 2967 * 0xffffffff) byte count but stale data, and end up 2968 * generating a packet with bad headers. 2969 * 2970 * The first inline segment's byte_count field doesn't 2971 * need a barrier, because it comes after a 2972 * control/MLX segment and therefore is at an offset 2973 * of 16 mod 64. 2974 */ 2975 wmb(); 2976 inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc)); 2977 i = 2; 2978 } 2979 2980 *mlx_seg_len = 2981 ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16); 2982 return 0; 2983 } 2984 2985 static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num) 2986 { 2987 union sl2vl_tbl_to_u64 tmp_vltab; 2988 u8 vl; 2989 2990 if (sl > 15) 2991 return 0xf; 2992 tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]); 2993 vl = tmp_vltab.sl8[sl >> 1]; 2994 if (sl & 1) 2995 vl &= 0x0f; 2996 else 2997 vl >>= 4; 2998 return vl; 2999 } 3000 3001 static int fill_gid_by_hw_index(struct mlx4_ib_dev *ibdev, u8 port_num, 3002 int index, union ib_gid *gid, 3003 enum ib_gid_type *gid_type) 3004 { 3005 struct mlx4_ib_iboe *iboe = &ibdev->iboe; 3006 struct mlx4_port_gid_table *port_gid_table; 3007 unsigned long flags; 3008 3009 port_gid_table = &iboe->gids[port_num - 1]; 3010 spin_lock_irqsave(&iboe->lock, flags); 3011 memcpy(gid, &port_gid_table->gids[index].gid, sizeof(*gid)); 3012 *gid_type = port_gid_table->gids[index].gid_type; 3013 spin_unlock_irqrestore(&iboe->lock, flags); 3014 if (rdma_is_zero_gid(gid)) 3015 return -ENOENT; 3016 3017 return 0; 3018 } 3019 3020 #define MLX4_ROCEV2_QP1_SPORT 0xC000 3021 static int build_mlx_header(struct mlx4_ib_qp *qp, const struct ib_ud_wr *wr, 3022 void *wqe, unsigned *mlx_seg_len) 3023 { 3024 struct mlx4_ib_sqp *sqp = qp->sqp; 3025 struct ib_device *ib_dev = qp->ibqp.device; 3026 struct mlx4_ib_dev *ibdev = to_mdev(ib_dev); 3027 struct mlx4_wqe_mlx_seg *mlx = wqe; 3028 struct mlx4_wqe_ctrl_seg *ctrl = wqe; 3029 struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx; 3030 struct mlx4_ib_ah *ah = to_mah(wr->ah); 3031 union ib_gid sgid; 3032 u16 pkey; 3033 int send_size; 3034 int header_size; 3035 int spc; 3036 int i; 3037 int err = 0; 3038 u16 vlan = 0xffff; 3039 bool is_eth; 3040 bool is_vlan = false; 3041 bool is_grh; 3042 bool is_udp = false; 3043 int ip_version = 0; 3044 3045 send_size = 0; 3046 for (i = 0; i < wr->wr.num_sge; ++i) 3047 send_size += wr->wr.sg_list[i].length; 3048 3049 is_eth = rdma_port_get_link_layer(qp->ibqp.device, qp->port) == IB_LINK_LAYER_ETHERNET; 3050 is_grh = mlx4_ib_ah_grh_present(ah); 3051 if (is_eth) { 3052 enum ib_gid_type gid_type; 3053 if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) { 3054 /* When multi-function is enabled, the ib_core gid 3055 * indexes don't necessarily match the hw ones, so 3056 * we must use our own cache */ 3057 err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev, 3058 be32_to_cpu(ah->av.ib.port_pd) >> 24, 3059 ah->av.ib.gid_index, &sgid.raw[0]); 3060 if (err) 3061 return err; 3062 } else { 3063 err = fill_gid_by_hw_index(ibdev, qp->port, 3064 ah->av.ib.gid_index, &sgid, 3065 &gid_type); 3066 if (!err) { 3067 is_udp = gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP; 3068 if (is_udp) { 3069 if (ipv6_addr_v4mapped((struct in6_addr *)&sgid)) 3070 ip_version = 4; 3071 else 3072 ip_version = 6; 3073 is_grh = false; 3074 } 3075 } else { 3076 return err; 3077 } 3078 } 3079 if (ah->av.eth.vlan != cpu_to_be16(0xffff)) { 3080 vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff; 3081 is_vlan = true; 3082 } 3083 } 3084 err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh, 3085 ip_version, is_udp, 0, &sqp->ud_header); 3086 if (err) 3087 return err; 3088 3089 if (!is_eth) { 3090 sqp->ud_header.lrh.service_level = 3091 be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28; 3092 sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid; 3093 sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f); 3094 } 3095 3096 if (is_grh || (ip_version == 6)) { 3097 sqp->ud_header.grh.traffic_class = 3098 (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff; 3099 sqp->ud_header.grh.flow_label = 3100 ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff); 3101 sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit; 3102 if (is_eth) { 3103 memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16); 3104 } else { 3105 if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) { 3106 /* When multi-function is enabled, the ib_core gid 3107 * indexes don't necessarily match the hw ones, so 3108 * we must use our own cache 3109 */ 3110 sqp->ud_header.grh.source_gid.global 3111 .subnet_prefix = 3112 cpu_to_be64(atomic64_read( 3113 &(to_mdev(ib_dev) 3114 ->sriov 3115 .demux[qp->port - 1] 3116 .subnet_prefix))); 3117 sqp->ud_header.grh.source_gid.global 3118 .interface_id = 3119 to_mdev(ib_dev) 3120 ->sriov.demux[qp->port - 1] 3121 .guid_cache[ah->av.ib.gid_index]; 3122 } else { 3123 sqp->ud_header.grh.source_gid = 3124 ah->ibah.sgid_attr->gid; 3125 } 3126 } 3127 memcpy(sqp->ud_header.grh.destination_gid.raw, 3128 ah->av.ib.dgid, 16); 3129 } 3130 3131 if (ip_version == 4) { 3132 sqp->ud_header.ip4.tos = 3133 (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff; 3134 sqp->ud_header.ip4.id = 0; 3135 sqp->ud_header.ip4.frag_off = htons(IP_DF); 3136 sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit; 3137 3138 memcpy(&sqp->ud_header.ip4.saddr, 3139 sgid.raw + 12, 4); 3140 memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4); 3141 sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header); 3142 } 3143 3144 if (is_udp) { 3145 sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT); 3146 sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT); 3147 sqp->ud_header.udp.csum = 0; 3148 } 3149 3150 mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE); 3151 3152 if (!is_eth) { 3153 mlx->flags |= 3154 cpu_to_be32((!qp->ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) | 3155 (sqp->ud_header.lrh.destination_lid == 3156 IB_LID_PERMISSIVE ? 3157 MLX4_WQE_MLX_SLR : 3158 0) | 3159 (sqp->ud_header.lrh.service_level << 8)); 3160 if (ah->av.ib.port_pd & cpu_to_be32(0x80000000)) 3161 mlx->flags |= cpu_to_be32(0x1); /* force loopback */ 3162 mlx->rlid = sqp->ud_header.lrh.destination_lid; 3163 } 3164 3165 switch (wr->wr.opcode) { 3166 case IB_WR_SEND: 3167 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY; 3168 sqp->ud_header.immediate_present = 0; 3169 break; 3170 case IB_WR_SEND_WITH_IMM: 3171 sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE; 3172 sqp->ud_header.immediate_present = 1; 3173 sqp->ud_header.immediate_data = wr->wr.ex.imm_data; 3174 break; 3175 default: 3176 return -EINVAL; 3177 } 3178 3179 if (is_eth) { 3180 u16 ether_type; 3181 u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13; 3182 3183 ether_type = (!is_udp) ? ETH_P_IBOE: 3184 (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6); 3185 3186 mlx->sched_prio = cpu_to_be16(pcp); 3187 3188 ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac); 3189 ether_addr_copy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac); 3190 memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2); 3191 memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4); 3192 3193 if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6)) 3194 mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK); 3195 if (!is_vlan) { 3196 sqp->ud_header.eth.type = cpu_to_be16(ether_type); 3197 } else { 3198 sqp->ud_header.vlan.type = cpu_to_be16(ether_type); 3199 sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp); 3200 } 3201 } else { 3202 sqp->ud_header.lrh.virtual_lane = 3203 !qp->ibqp.qp_num ? 3204 15 : 3205 sl_to_vl(to_mdev(ib_dev), 3206 sqp->ud_header.lrh.service_level, 3207 qp->port); 3208 if (qp->ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15) 3209 return -EINVAL; 3210 if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE) 3211 sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE; 3212 } 3213 sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED); 3214 if (!qp->ibqp.qp_num) 3215 err = ib_get_cached_pkey(ib_dev, qp->port, sqp->pkey_index, 3216 &pkey); 3217 else 3218 err = ib_get_cached_pkey(ib_dev, qp->port, wr->pkey_index, 3219 &pkey); 3220 if (err) 3221 return err; 3222 3223 sqp->ud_header.bth.pkey = cpu_to_be16(pkey); 3224 sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn); 3225 sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1)); 3226 sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ? 3227 sqp->qkey : wr->remote_qkey); 3228 sqp->ud_header.deth.source_qpn = cpu_to_be32(qp->ibqp.qp_num); 3229 3230 header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf); 3231 3232 if (0) { 3233 pr_err("built UD header of size %d:\n", header_size); 3234 for (i = 0; i < header_size / 4; ++i) { 3235 if (i % 8 == 0) 3236 pr_err(" [%02x] ", i * 4); 3237 pr_cont(" %08x", 3238 be32_to_cpu(((__be32 *) sqp->header_buf)[i])); 3239 if ((i + 1) % 8 == 0) 3240 pr_cont("\n"); 3241 } 3242 pr_err("\n"); 3243 } 3244 3245 /* 3246 * Inline data segments may not cross a 64 byte boundary. If 3247 * our UD header is bigger than the space available up to the 3248 * next 64 byte boundary in the WQE, use two inline data 3249 * segments to hold the UD header. 3250 */ 3251 spc = MLX4_INLINE_ALIGN - 3252 ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1)); 3253 if (header_size <= spc) { 3254 inl->byte_count = cpu_to_be32(1 << 31 | header_size); 3255 memcpy(inl + 1, sqp->header_buf, header_size); 3256 i = 1; 3257 } else { 3258 inl->byte_count = cpu_to_be32(1 << 31 | spc); 3259 memcpy(inl + 1, sqp->header_buf, spc); 3260 3261 inl = (void *) (inl + 1) + spc; 3262 memcpy(inl + 1, sqp->header_buf + spc, header_size - spc); 3263 /* 3264 * Need a barrier here to make sure all the data is 3265 * visible before the byte_count field is set. 3266 * Otherwise the HCA prefetcher could grab the 64-byte 3267 * chunk with this inline segment and get a valid (!= 3268 * 0xffffffff) byte count but stale data, and end up 3269 * generating a packet with bad headers. 3270 * 3271 * The first inline segment's byte_count field doesn't 3272 * need a barrier, because it comes after a 3273 * control/MLX segment and therefore is at an offset 3274 * of 16 mod 64. 3275 */ 3276 wmb(); 3277 inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc)); 3278 i = 2; 3279 } 3280 3281 *mlx_seg_len = 3282 ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16); 3283 return 0; 3284 } 3285 3286 static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq) 3287 { 3288 unsigned cur; 3289 struct mlx4_ib_cq *cq; 3290 3291 cur = wq->head - wq->tail; 3292 if (likely(cur + nreq < wq->max_post)) 3293 return 0; 3294 3295 cq = to_mcq(ib_cq); 3296 spin_lock(&cq->lock); 3297 cur = wq->head - wq->tail; 3298 spin_unlock(&cq->lock); 3299 3300 return cur + nreq >= wq->max_post; 3301 } 3302 3303 static __be32 convert_access(int acc) 3304 { 3305 return (acc & IB_ACCESS_REMOTE_ATOMIC ? 3306 cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) | 3307 (acc & IB_ACCESS_REMOTE_WRITE ? 3308 cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) | 3309 (acc & IB_ACCESS_REMOTE_READ ? 3310 cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) | 3311 (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) | 3312 cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ); 3313 } 3314 3315 static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg, 3316 const struct ib_reg_wr *wr) 3317 { 3318 struct mlx4_ib_mr *mr = to_mmr(wr->mr); 3319 3320 fseg->flags = convert_access(wr->access); 3321 fseg->mem_key = cpu_to_be32(wr->key); 3322 fseg->buf_list = cpu_to_be64(mr->page_map); 3323 fseg->start_addr = cpu_to_be64(mr->ibmr.iova); 3324 fseg->reg_len = cpu_to_be64(mr->ibmr.length); 3325 fseg->offset = 0; /* XXX -- is this just for ZBVA? */ 3326 fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size)); 3327 fseg->reserved[0] = 0; 3328 fseg->reserved[1] = 0; 3329 } 3330 3331 static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey) 3332 { 3333 memset(iseg, 0, sizeof(*iseg)); 3334 iseg->mem_key = cpu_to_be32(rkey); 3335 } 3336 3337 static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg, 3338 u64 remote_addr, u32 rkey) 3339 { 3340 rseg->raddr = cpu_to_be64(remote_addr); 3341 rseg->rkey = cpu_to_be32(rkey); 3342 rseg->reserved = 0; 3343 } 3344 3345 static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, 3346 const struct ib_atomic_wr *wr) 3347 { 3348 if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) { 3349 aseg->swap_add = cpu_to_be64(wr->swap); 3350 aseg->compare = cpu_to_be64(wr->compare_add); 3351 } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) { 3352 aseg->swap_add = cpu_to_be64(wr->compare_add); 3353 aseg->compare = cpu_to_be64(wr->compare_add_mask); 3354 } else { 3355 aseg->swap_add = cpu_to_be64(wr->compare_add); 3356 aseg->compare = 0; 3357 } 3358 3359 } 3360 3361 static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg, 3362 const struct ib_atomic_wr *wr) 3363 { 3364 aseg->swap_add = cpu_to_be64(wr->swap); 3365 aseg->swap_add_mask = cpu_to_be64(wr->swap_mask); 3366 aseg->compare = cpu_to_be64(wr->compare_add); 3367 aseg->compare_mask = cpu_to_be64(wr->compare_add_mask); 3368 } 3369 3370 static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg, 3371 const struct ib_ud_wr *wr) 3372 { 3373 memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av)); 3374 dseg->dqpn = cpu_to_be32(wr->remote_qpn); 3375 dseg->qkey = cpu_to_be32(wr->remote_qkey); 3376 dseg->vlan = to_mah(wr->ah)->av.eth.vlan; 3377 memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6); 3378 } 3379 3380 static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev, 3381 struct mlx4_wqe_datagram_seg *dseg, 3382 const struct ib_ud_wr *wr, 3383 enum mlx4_ib_qp_type qpt) 3384 { 3385 union mlx4_ext_av *av = &to_mah(wr->ah)->av; 3386 struct mlx4_av sqp_av = {0}; 3387 int port = *((u8 *) &av->ib.port_pd) & 0x3; 3388 3389 /* force loopback */ 3390 sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000); 3391 sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */ 3392 sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel & 3393 cpu_to_be32(0xf0000000); 3394 3395 memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av)); 3396 if (qpt == MLX4_IB_QPT_PROXY_GSI) 3397 dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp1_tunnel); 3398 else 3399 dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp0_tunnel); 3400 /* Use QKEY from the QP context, which is set by master */ 3401 dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY); 3402 } 3403 3404 static void build_tunnel_header(const struct ib_ud_wr *wr, void *wqe, 3405 unsigned *mlx_seg_len) 3406 { 3407 struct mlx4_wqe_inline_seg *inl = wqe; 3408 struct mlx4_ib_tunnel_header hdr; 3409 struct mlx4_ib_ah *ah = to_mah(wr->ah); 3410 int spc; 3411 int i; 3412 3413 memcpy(&hdr.av, &ah->av, sizeof hdr.av); 3414 hdr.remote_qpn = cpu_to_be32(wr->remote_qpn); 3415 hdr.pkey_index = cpu_to_be16(wr->pkey_index); 3416 hdr.qkey = cpu_to_be32(wr->remote_qkey); 3417 memcpy(hdr.mac, ah->av.eth.mac, 6); 3418 hdr.vlan = ah->av.eth.vlan; 3419 3420 spc = MLX4_INLINE_ALIGN - 3421 ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1)); 3422 if (sizeof (hdr) <= spc) { 3423 memcpy(inl + 1, &hdr, sizeof (hdr)); 3424 wmb(); 3425 inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr)); 3426 i = 1; 3427 } else { 3428 memcpy(inl + 1, &hdr, spc); 3429 wmb(); 3430 inl->byte_count = cpu_to_be32(1 << 31 | spc); 3431 3432 inl = (void *) (inl + 1) + spc; 3433 memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc); 3434 wmb(); 3435 inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc)); 3436 i = 2; 3437 } 3438 3439 *mlx_seg_len = 3440 ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16); 3441 } 3442 3443 static void set_mlx_icrc_seg(void *dseg) 3444 { 3445 u32 *t = dseg; 3446 struct mlx4_wqe_inline_seg *iseg = dseg; 3447 3448 t[1] = 0; 3449 3450 /* 3451 * Need a barrier here before writing the byte_count field to 3452 * make sure that all the data is visible before the 3453 * byte_count field is set. Otherwise, if the segment begins 3454 * a new cacheline, the HCA prefetcher could grab the 64-byte 3455 * chunk and get a valid (!= * 0xffffffff) byte count but 3456 * stale data, and end up sending the wrong data. 3457 */ 3458 wmb(); 3459 3460 iseg->byte_count = cpu_to_be32((1 << 31) | 4); 3461 } 3462 3463 static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg) 3464 { 3465 dseg->lkey = cpu_to_be32(sg->lkey); 3466 dseg->addr = cpu_to_be64(sg->addr); 3467 3468 /* 3469 * Need a barrier here before writing the byte_count field to 3470 * make sure that all the data is visible before the 3471 * byte_count field is set. Otherwise, if the segment begins 3472 * a new cacheline, the HCA prefetcher could grab the 64-byte 3473 * chunk and get a valid (!= * 0xffffffff) byte count but 3474 * stale data, and end up sending the wrong data. 3475 */ 3476 wmb(); 3477 3478 dseg->byte_count = cpu_to_be32(sg->length); 3479 } 3480 3481 static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg) 3482 { 3483 dseg->byte_count = cpu_to_be32(sg->length); 3484 dseg->lkey = cpu_to_be32(sg->lkey); 3485 dseg->addr = cpu_to_be64(sg->addr); 3486 } 3487 3488 static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, 3489 const struct ib_ud_wr *wr, struct mlx4_ib_qp *qp, 3490 unsigned *lso_seg_len, __be32 *lso_hdr_sz, __be32 *blh) 3491 { 3492 unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16); 3493 3494 if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE)) 3495 *blh = cpu_to_be32(1 << 6); 3496 3497 if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) && 3498 wr->wr.num_sge > qp->sq.max_gs - (halign >> 4))) 3499 return -EINVAL; 3500 3501 memcpy(wqe->header, wr->header, wr->hlen); 3502 3503 *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen); 3504 *lso_seg_len = halign; 3505 return 0; 3506 } 3507 3508 static __be32 send_ieth(const struct ib_send_wr *wr) 3509 { 3510 switch (wr->opcode) { 3511 case IB_WR_SEND_WITH_IMM: 3512 case IB_WR_RDMA_WRITE_WITH_IMM: 3513 return wr->ex.imm_data; 3514 3515 case IB_WR_SEND_WITH_INV: 3516 return cpu_to_be32(wr->ex.invalidate_rkey); 3517 3518 default: 3519 return 0; 3520 } 3521 } 3522 3523 static void add_zero_len_inline(void *wqe) 3524 { 3525 struct mlx4_wqe_inline_seg *inl = wqe; 3526 memset(wqe, 0, 16); 3527 inl->byte_count = cpu_to_be32(1 << 31); 3528 } 3529 3530 static int _mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 3531 const struct ib_send_wr **bad_wr, bool drain) 3532 { 3533 struct mlx4_ib_qp *qp = to_mqp(ibqp); 3534 void *wqe; 3535 struct mlx4_wqe_ctrl_seg *ctrl; 3536 struct mlx4_wqe_data_seg *dseg; 3537 unsigned long flags; 3538 int nreq; 3539 int err = 0; 3540 unsigned ind; 3541 int size; 3542 unsigned seglen; 3543 __be32 dummy; 3544 __be32 *lso_wqe; 3545 __be32 lso_hdr_sz; 3546 __be32 blh; 3547 int i; 3548 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device); 3549 3550 if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) { 3551 struct mlx4_ib_sqp *sqp = qp->sqp; 3552 3553 if (sqp->roce_v2_gsi) { 3554 struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah); 3555 enum ib_gid_type gid_type; 3556 union ib_gid gid; 3557 3558 if (!fill_gid_by_hw_index(mdev, qp->port, 3559 ah->av.ib.gid_index, 3560 &gid, &gid_type)) 3561 qp = (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 3562 to_mqp(sqp->roce_v2_gsi) : qp; 3563 else 3564 pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n", 3565 ah->av.ib.gid_index); 3566 } 3567 } 3568 3569 spin_lock_irqsave(&qp->sq.lock, flags); 3570 if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR && 3571 !drain) { 3572 err = -EIO; 3573 *bad_wr = wr; 3574 nreq = 0; 3575 goto out; 3576 } 3577 3578 ind = qp->sq_next_wqe; 3579 3580 for (nreq = 0; wr; ++nreq, wr = wr->next) { 3581 lso_wqe = &dummy; 3582 blh = 0; 3583 3584 if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) { 3585 err = -ENOMEM; 3586 *bad_wr = wr; 3587 goto out; 3588 } 3589 3590 if (unlikely(wr->num_sge > qp->sq.max_gs)) { 3591 err = -EINVAL; 3592 *bad_wr = wr; 3593 goto out; 3594 } 3595 3596 ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1)); 3597 qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id; 3598 3599 ctrl->srcrb_flags = 3600 (wr->send_flags & IB_SEND_SIGNALED ? 3601 cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) | 3602 (wr->send_flags & IB_SEND_SOLICITED ? 3603 cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) | 3604 ((wr->send_flags & IB_SEND_IP_CSUM) ? 3605 cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM | 3606 MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) | 3607 qp->sq_signal_bits; 3608 3609 ctrl->imm = send_ieth(wr); 3610 3611 wqe += sizeof *ctrl; 3612 size = sizeof *ctrl / 16; 3613 3614 switch (qp->mlx4_ib_qp_type) { 3615 case MLX4_IB_QPT_RC: 3616 case MLX4_IB_QPT_UC: 3617 switch (wr->opcode) { 3618 case IB_WR_ATOMIC_CMP_AND_SWP: 3619 case IB_WR_ATOMIC_FETCH_AND_ADD: 3620 case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD: 3621 set_raddr_seg(wqe, atomic_wr(wr)->remote_addr, 3622 atomic_wr(wr)->rkey); 3623 wqe += sizeof (struct mlx4_wqe_raddr_seg); 3624 3625 set_atomic_seg(wqe, atomic_wr(wr)); 3626 wqe += sizeof (struct mlx4_wqe_atomic_seg); 3627 3628 size += (sizeof (struct mlx4_wqe_raddr_seg) + 3629 sizeof (struct mlx4_wqe_atomic_seg)) / 16; 3630 3631 break; 3632 3633 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP: 3634 set_raddr_seg(wqe, atomic_wr(wr)->remote_addr, 3635 atomic_wr(wr)->rkey); 3636 wqe += sizeof (struct mlx4_wqe_raddr_seg); 3637 3638 set_masked_atomic_seg(wqe, atomic_wr(wr)); 3639 wqe += sizeof (struct mlx4_wqe_masked_atomic_seg); 3640 3641 size += (sizeof (struct mlx4_wqe_raddr_seg) + 3642 sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16; 3643 3644 break; 3645 3646 case IB_WR_RDMA_READ: 3647 case IB_WR_RDMA_WRITE: 3648 case IB_WR_RDMA_WRITE_WITH_IMM: 3649 set_raddr_seg(wqe, rdma_wr(wr)->remote_addr, 3650 rdma_wr(wr)->rkey); 3651 wqe += sizeof (struct mlx4_wqe_raddr_seg); 3652 size += sizeof (struct mlx4_wqe_raddr_seg) / 16; 3653 break; 3654 3655 case IB_WR_LOCAL_INV: 3656 ctrl->srcrb_flags |= 3657 cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER); 3658 set_local_inv_seg(wqe, wr->ex.invalidate_rkey); 3659 wqe += sizeof (struct mlx4_wqe_local_inval_seg); 3660 size += sizeof (struct mlx4_wqe_local_inval_seg) / 16; 3661 break; 3662 3663 case IB_WR_REG_MR: 3664 ctrl->srcrb_flags |= 3665 cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER); 3666 set_reg_seg(wqe, reg_wr(wr)); 3667 wqe += sizeof(struct mlx4_wqe_fmr_seg); 3668 size += sizeof(struct mlx4_wqe_fmr_seg) / 16; 3669 break; 3670 3671 default: 3672 /* No extra segments required for sends */ 3673 break; 3674 } 3675 break; 3676 3677 case MLX4_IB_QPT_TUN_SMI_OWNER: 3678 err = build_sriov_qp0_header(qp, ud_wr(wr), ctrl, 3679 &seglen); 3680 if (unlikely(err)) { 3681 *bad_wr = wr; 3682 goto out; 3683 } 3684 wqe += seglen; 3685 size += seglen / 16; 3686 break; 3687 case MLX4_IB_QPT_TUN_SMI: 3688 case MLX4_IB_QPT_TUN_GSI: 3689 /* this is a UD qp used in MAD responses to slaves. */ 3690 set_datagram_seg(wqe, ud_wr(wr)); 3691 /* set the forced-loopback bit in the data seg av */ 3692 *(__be32 *) wqe |= cpu_to_be32(0x80000000); 3693 wqe += sizeof (struct mlx4_wqe_datagram_seg); 3694 size += sizeof (struct mlx4_wqe_datagram_seg) / 16; 3695 break; 3696 case MLX4_IB_QPT_UD: 3697 set_datagram_seg(wqe, ud_wr(wr)); 3698 wqe += sizeof (struct mlx4_wqe_datagram_seg); 3699 size += sizeof (struct mlx4_wqe_datagram_seg) / 16; 3700 3701 if (wr->opcode == IB_WR_LSO) { 3702 err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen, 3703 &lso_hdr_sz, &blh); 3704 if (unlikely(err)) { 3705 *bad_wr = wr; 3706 goto out; 3707 } 3708 lso_wqe = (__be32 *) wqe; 3709 wqe += seglen; 3710 size += seglen / 16; 3711 } 3712 break; 3713 3714 case MLX4_IB_QPT_PROXY_SMI_OWNER: 3715 err = build_sriov_qp0_header(qp, ud_wr(wr), ctrl, 3716 &seglen); 3717 if (unlikely(err)) { 3718 *bad_wr = wr; 3719 goto out; 3720 } 3721 wqe += seglen; 3722 size += seglen / 16; 3723 /* to start tunnel header on a cache-line boundary */ 3724 add_zero_len_inline(wqe); 3725 wqe += 16; 3726 size++; 3727 build_tunnel_header(ud_wr(wr), wqe, &seglen); 3728 wqe += seglen; 3729 size += seglen / 16; 3730 break; 3731 case MLX4_IB_QPT_PROXY_SMI: 3732 case MLX4_IB_QPT_PROXY_GSI: 3733 /* If we are tunneling special qps, this is a UD qp. 3734 * In this case we first add a UD segment targeting 3735 * the tunnel qp, and then add a header with address 3736 * information */ 3737 set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe, 3738 ud_wr(wr), 3739 qp->mlx4_ib_qp_type); 3740 wqe += sizeof (struct mlx4_wqe_datagram_seg); 3741 size += sizeof (struct mlx4_wqe_datagram_seg) / 16; 3742 build_tunnel_header(ud_wr(wr), wqe, &seglen); 3743 wqe += seglen; 3744 size += seglen / 16; 3745 break; 3746 3747 case MLX4_IB_QPT_SMI: 3748 case MLX4_IB_QPT_GSI: 3749 err = build_mlx_header(qp, ud_wr(wr), ctrl, &seglen); 3750 if (unlikely(err)) { 3751 *bad_wr = wr; 3752 goto out; 3753 } 3754 wqe += seglen; 3755 size += seglen / 16; 3756 break; 3757 3758 default: 3759 break; 3760 } 3761 3762 /* 3763 * Write data segments in reverse order, so as to 3764 * overwrite cacheline stamp last within each 3765 * cacheline. This avoids issues with WQE 3766 * prefetching. 3767 */ 3768 3769 dseg = wqe; 3770 dseg += wr->num_sge - 1; 3771 size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16); 3772 3773 /* Add one more inline data segment for ICRC for MLX sends */ 3774 if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI || 3775 qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI || 3776 qp->mlx4_ib_qp_type & 3777 (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) { 3778 set_mlx_icrc_seg(dseg + 1); 3779 size += sizeof (struct mlx4_wqe_data_seg) / 16; 3780 } 3781 3782 for (i = wr->num_sge - 1; i >= 0; --i, --dseg) 3783 set_data_seg(dseg, wr->sg_list + i); 3784 3785 /* 3786 * Possibly overwrite stamping in cacheline with LSO 3787 * segment only after making sure all data segments 3788 * are written. 3789 */ 3790 wmb(); 3791 *lso_wqe = lso_hdr_sz; 3792 3793 ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ? 3794 MLX4_WQE_CTRL_FENCE : 0) | size; 3795 3796 /* 3797 * Make sure descriptor is fully written before 3798 * setting ownership bit (because HW can start 3799 * executing as soon as we do). 3800 */ 3801 wmb(); 3802 3803 if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) { 3804 *bad_wr = wr; 3805 err = -EINVAL; 3806 goto out; 3807 } 3808 3809 ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] | 3810 (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh; 3811 3812 /* 3813 * We can improve latency by not stamping the last 3814 * send queue WQE until after ringing the doorbell, so 3815 * only stamp here if there are still more WQEs to post. 3816 */ 3817 if (wr->next) 3818 stamp_send_wqe(qp, ind + qp->sq_spare_wqes); 3819 ind++; 3820 } 3821 3822 out: 3823 if (likely(nreq)) { 3824 qp->sq.head += nreq; 3825 3826 /* 3827 * Make sure that descriptors are written before 3828 * doorbell record. 3829 */ 3830 wmb(); 3831 3832 writel_relaxed(qp->doorbell_qpn, 3833 to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL); 3834 3835 stamp_send_wqe(qp, ind + qp->sq_spare_wqes - 1); 3836 3837 qp->sq_next_wqe = ind; 3838 } 3839 3840 spin_unlock_irqrestore(&qp->sq.lock, flags); 3841 3842 return err; 3843 } 3844 3845 int mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 3846 const struct ib_send_wr **bad_wr) 3847 { 3848 return _mlx4_ib_post_send(ibqp, wr, bad_wr, false); 3849 } 3850 3851 static int _mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 3852 const struct ib_recv_wr **bad_wr, bool drain) 3853 { 3854 struct mlx4_ib_qp *qp = to_mqp(ibqp); 3855 struct mlx4_wqe_data_seg *scat; 3856 unsigned long flags; 3857 int err = 0; 3858 int nreq; 3859 int ind; 3860 int max_gs; 3861 int i; 3862 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device); 3863 3864 max_gs = qp->rq.max_gs; 3865 spin_lock_irqsave(&qp->rq.lock, flags); 3866 3867 if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR && 3868 !drain) { 3869 err = -EIO; 3870 *bad_wr = wr; 3871 nreq = 0; 3872 goto out; 3873 } 3874 3875 ind = qp->rq.head & (qp->rq.wqe_cnt - 1); 3876 3877 for (nreq = 0; wr; ++nreq, wr = wr->next) { 3878 if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) { 3879 err = -ENOMEM; 3880 *bad_wr = wr; 3881 goto out; 3882 } 3883 3884 if (unlikely(wr->num_sge > qp->rq.max_gs)) { 3885 err = -EINVAL; 3886 *bad_wr = wr; 3887 goto out; 3888 } 3889 3890 scat = get_recv_wqe(qp, ind); 3891 3892 if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER | 3893 MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) { 3894 ib_dma_sync_single_for_device(ibqp->device, 3895 qp->sqp_proxy_rcv[ind].map, 3896 sizeof (struct mlx4_ib_proxy_sqp_hdr), 3897 DMA_FROM_DEVICE); 3898 scat->byte_count = 3899 cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr)); 3900 /* use dma lkey from upper layer entry */ 3901 scat->lkey = cpu_to_be32(wr->sg_list->lkey); 3902 scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map); 3903 scat++; 3904 max_gs--; 3905 } 3906 3907 for (i = 0; i < wr->num_sge; ++i) 3908 __set_data_seg(scat + i, wr->sg_list + i); 3909 3910 if (i < max_gs) { 3911 scat[i].byte_count = 0; 3912 scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY); 3913 scat[i].addr = 0; 3914 } 3915 3916 qp->rq.wrid[ind] = wr->wr_id; 3917 3918 ind = (ind + 1) & (qp->rq.wqe_cnt - 1); 3919 } 3920 3921 out: 3922 if (likely(nreq)) { 3923 qp->rq.head += nreq; 3924 3925 /* 3926 * Make sure that descriptors are written before 3927 * doorbell record. 3928 */ 3929 wmb(); 3930 3931 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff); 3932 } 3933 3934 spin_unlock_irqrestore(&qp->rq.lock, flags); 3935 3936 return err; 3937 } 3938 3939 int mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 3940 const struct ib_recv_wr **bad_wr) 3941 { 3942 return _mlx4_ib_post_recv(ibqp, wr, bad_wr, false); 3943 } 3944 3945 static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state) 3946 { 3947 switch (mlx4_state) { 3948 case MLX4_QP_STATE_RST: return IB_QPS_RESET; 3949 case MLX4_QP_STATE_INIT: return IB_QPS_INIT; 3950 case MLX4_QP_STATE_RTR: return IB_QPS_RTR; 3951 case MLX4_QP_STATE_RTS: return IB_QPS_RTS; 3952 case MLX4_QP_STATE_SQ_DRAINING: 3953 case MLX4_QP_STATE_SQD: return IB_QPS_SQD; 3954 case MLX4_QP_STATE_SQER: return IB_QPS_SQE; 3955 case MLX4_QP_STATE_ERR: return IB_QPS_ERR; 3956 default: return -1; 3957 } 3958 } 3959 3960 static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state) 3961 { 3962 switch (mlx4_mig_state) { 3963 case MLX4_QP_PM_ARMED: return IB_MIG_ARMED; 3964 case MLX4_QP_PM_REARM: return IB_MIG_REARM; 3965 case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED; 3966 default: return -1; 3967 } 3968 } 3969 3970 static int to_ib_qp_access_flags(int mlx4_flags) 3971 { 3972 int ib_flags = 0; 3973 3974 if (mlx4_flags & MLX4_QP_BIT_RRE) 3975 ib_flags |= IB_ACCESS_REMOTE_READ; 3976 if (mlx4_flags & MLX4_QP_BIT_RWE) 3977 ib_flags |= IB_ACCESS_REMOTE_WRITE; 3978 if (mlx4_flags & MLX4_QP_BIT_RAE) 3979 ib_flags |= IB_ACCESS_REMOTE_ATOMIC; 3980 3981 return ib_flags; 3982 } 3983 3984 static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev, 3985 struct rdma_ah_attr *ah_attr, 3986 struct mlx4_qp_path *path) 3987 { 3988 struct mlx4_dev *dev = ibdev->dev; 3989 u8 port_num = path->sched_queue & 0x40 ? 2 : 1; 3990 3991 memset(ah_attr, 0, sizeof(*ah_attr)); 3992 if (port_num == 0 || port_num > dev->caps.num_ports) 3993 return; 3994 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num); 3995 3996 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) 3997 rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) | 3998 ((path->sched_queue & 4) << 1)); 3999 else 4000 rdma_ah_set_sl(ah_attr, (path->sched_queue >> 2) & 0xf); 4001 rdma_ah_set_port_num(ah_attr, port_num); 4002 4003 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid)); 4004 rdma_ah_set_path_bits(ah_attr, path->grh_mylmc & 0x7f); 4005 rdma_ah_set_static_rate(ah_attr, 4006 path->static_rate ? path->static_rate - 5 : 0); 4007 if (path->grh_mylmc & (1 << 7)) { 4008 rdma_ah_set_grh(ah_attr, NULL, 4009 be32_to_cpu(path->tclass_flowlabel) & 0xfffff, 4010 path->mgid_index, 4011 path->hop_limit, 4012 (be32_to_cpu(path->tclass_flowlabel) 4013 >> 20) & 0xff); 4014 rdma_ah_set_dgid_raw(ah_attr, path->rgid); 4015 } 4016 } 4017 4018 int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask, 4019 struct ib_qp_init_attr *qp_init_attr) 4020 { 4021 struct mlx4_ib_dev *dev = to_mdev(ibqp->device); 4022 struct mlx4_ib_qp *qp = to_mqp(ibqp); 4023 struct mlx4_qp_context context; 4024 int mlx4_state; 4025 int err = 0; 4026 4027 if (ibqp->rwq_ind_tbl) 4028 return -EOPNOTSUPP; 4029 4030 mutex_lock(&qp->mutex); 4031 4032 if (qp->state == IB_QPS_RESET) { 4033 qp_attr->qp_state = IB_QPS_RESET; 4034 goto done; 4035 } 4036 4037 err = mlx4_qp_query(dev->dev, &qp->mqp, &context); 4038 if (err) { 4039 err = -EINVAL; 4040 goto out; 4041 } 4042 4043 mlx4_state = be32_to_cpu(context.flags) >> 28; 4044 4045 qp->state = to_ib_qp_state(mlx4_state); 4046 qp_attr->qp_state = qp->state; 4047 qp_attr->path_mtu = context.mtu_msgmax >> 5; 4048 qp_attr->path_mig_state = 4049 to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3); 4050 qp_attr->qkey = be32_to_cpu(context.qkey); 4051 qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff; 4052 qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff; 4053 qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff; 4054 qp_attr->qp_access_flags = 4055 to_ib_qp_access_flags(be32_to_cpu(context.params2)); 4056 4057 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC || 4058 qp->ibqp.qp_type == IB_QPT_XRC_INI || 4059 qp->ibqp.qp_type == IB_QPT_XRC_TGT) { 4060 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path); 4061 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path); 4062 qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f; 4063 qp_attr->alt_port_num = 4064 rdma_ah_get_port_num(&qp_attr->alt_ah_attr); 4065 } 4066 4067 qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f; 4068 if (qp_attr->qp_state == IB_QPS_INIT) 4069 qp_attr->port_num = qp->port; 4070 else 4071 qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1; 4072 4073 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */ 4074 qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING; 4075 4076 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7); 4077 4078 qp_attr->max_dest_rd_atomic = 4079 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7); 4080 qp_attr->min_rnr_timer = 4081 (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f; 4082 qp_attr->timeout = context.pri_path.ackto >> 3; 4083 qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7; 4084 qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7; 4085 qp_attr->alt_timeout = context.alt_path.ackto >> 3; 4086 4087 done: 4088 qp_attr->cur_qp_state = qp_attr->qp_state; 4089 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt; 4090 qp_attr->cap.max_recv_sge = qp->rq.max_gs; 4091 4092 if (!ibqp->uobject) { 4093 qp_attr->cap.max_send_wr = qp->sq.wqe_cnt; 4094 qp_attr->cap.max_send_sge = qp->sq.max_gs; 4095 } else { 4096 qp_attr->cap.max_send_wr = 0; 4097 qp_attr->cap.max_send_sge = 0; 4098 } 4099 4100 /* 4101 * We don't support inline sends for kernel QPs (yet), and we 4102 * don't know what userspace's value should be. 4103 */ 4104 qp_attr->cap.max_inline_data = 0; 4105 4106 qp_init_attr->cap = qp_attr->cap; 4107 4108 qp_init_attr->create_flags = 0; 4109 if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) 4110 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK; 4111 4112 if (qp->flags & MLX4_IB_QP_LSO) 4113 qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO; 4114 4115 if (qp->flags & MLX4_IB_QP_NETIF) 4116 qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP; 4117 4118 qp_init_attr->sq_sig_type = 4119 qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ? 4120 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR; 4121 4122 out: 4123 mutex_unlock(&qp->mutex); 4124 return err; 4125 } 4126 4127 struct ib_wq *mlx4_ib_create_wq(struct ib_pd *pd, 4128 struct ib_wq_init_attr *init_attr, 4129 struct ib_udata *udata) 4130 { 4131 struct mlx4_dev *dev = to_mdev(pd->device)->dev; 4132 struct ib_qp_init_attr ib_qp_init_attr = {}; 4133 struct mlx4_ib_qp *qp; 4134 struct mlx4_ib_create_wq ucmd; 4135 int err, required_cmd_sz; 4136 4137 if (!udata) 4138 return ERR_PTR(-EINVAL); 4139 4140 required_cmd_sz = offsetof(typeof(ucmd), comp_mask) + 4141 sizeof(ucmd.comp_mask); 4142 if (udata->inlen < required_cmd_sz) { 4143 pr_debug("invalid inlen\n"); 4144 return ERR_PTR(-EINVAL); 4145 } 4146 4147 if (udata->inlen > sizeof(ucmd) && 4148 !ib_is_udata_cleared(udata, sizeof(ucmd), 4149 udata->inlen - sizeof(ucmd))) { 4150 pr_debug("inlen is not supported\n"); 4151 return ERR_PTR(-EOPNOTSUPP); 4152 } 4153 4154 if (udata->outlen) 4155 return ERR_PTR(-EOPNOTSUPP); 4156 4157 if (init_attr->wq_type != IB_WQT_RQ) { 4158 pr_debug("unsupported wq type %d\n", init_attr->wq_type); 4159 return ERR_PTR(-EOPNOTSUPP); 4160 } 4161 4162 if (init_attr->create_flags & ~IB_WQ_FLAGS_SCATTER_FCS || 4163 !(dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)) { 4164 pr_debug("unsupported create_flags %u\n", 4165 init_attr->create_flags); 4166 return ERR_PTR(-EOPNOTSUPP); 4167 } 4168 4169 qp = kzalloc(sizeof(*qp), GFP_KERNEL); 4170 if (!qp) 4171 return ERR_PTR(-ENOMEM); 4172 4173 mutex_init(&qp->mutex); 4174 qp->pri.vid = 0xFFFF; 4175 qp->alt.vid = 0xFFFF; 4176 4177 ib_qp_init_attr.qp_context = init_attr->wq_context; 4178 ib_qp_init_attr.qp_type = IB_QPT_RAW_PACKET; 4179 ib_qp_init_attr.cap.max_recv_wr = init_attr->max_wr; 4180 ib_qp_init_attr.cap.max_recv_sge = init_attr->max_sge; 4181 ib_qp_init_attr.recv_cq = init_attr->cq; 4182 ib_qp_init_attr.send_cq = ib_qp_init_attr.recv_cq; /* Dummy CQ */ 4183 4184 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) 4185 ib_qp_init_attr.create_flags |= IB_QP_CREATE_SCATTER_FCS; 4186 4187 err = create_rq(pd, &ib_qp_init_attr, udata, qp); 4188 if (err) { 4189 kfree(qp); 4190 return ERR_PTR(err); 4191 } 4192 4193 qp->ibwq.event_handler = init_attr->event_handler; 4194 qp->ibwq.wq_num = qp->mqp.qpn; 4195 qp->ibwq.state = IB_WQS_RESET; 4196 4197 return &qp->ibwq; 4198 } 4199 4200 static int ib_wq2qp_state(enum ib_wq_state state) 4201 { 4202 switch (state) { 4203 case IB_WQS_RESET: 4204 return IB_QPS_RESET; 4205 case IB_WQS_RDY: 4206 return IB_QPS_RTR; 4207 default: 4208 return IB_QPS_ERR; 4209 } 4210 } 4211 4212 static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state, 4213 struct ib_udata *udata) 4214 { 4215 struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq); 4216 enum ib_qp_state qp_cur_state; 4217 enum ib_qp_state qp_new_state; 4218 int attr_mask; 4219 int err; 4220 4221 /* ib_qp.state represents the WQ HW state while ib_wq.state represents 4222 * the WQ logic state. 4223 */ 4224 qp_cur_state = qp->state; 4225 qp_new_state = ib_wq2qp_state(new_state); 4226 4227 if (ib_wq2qp_state(new_state) == qp_cur_state) 4228 return 0; 4229 4230 if (new_state == IB_WQS_RDY) { 4231 struct ib_qp_attr attr = {}; 4232 4233 attr.port_num = qp->port; 4234 attr_mask = IB_QP_PORT; 4235 4236 err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, &attr, 4237 attr_mask, IB_QPS_RESET, IB_QPS_INIT, 4238 udata); 4239 if (err) { 4240 pr_debug("WQN=0x%06x failed to apply RST->INIT on the HW QP\n", 4241 ibwq->wq_num); 4242 return err; 4243 } 4244 4245 qp_cur_state = IB_QPS_INIT; 4246 } 4247 4248 attr_mask = 0; 4249 err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL, attr_mask, 4250 qp_cur_state, qp_new_state, udata); 4251 4252 if (err && (qp_cur_state == IB_QPS_INIT)) { 4253 qp_new_state = IB_QPS_RESET; 4254 if (__mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL, 4255 attr_mask, IB_QPS_INIT, IB_QPS_RESET, 4256 udata)) { 4257 pr_warn("WQN=0x%06x failed with reverting HW's resources failure\n", 4258 ibwq->wq_num); 4259 qp_new_state = IB_QPS_INIT; 4260 } 4261 } 4262 4263 qp->state = qp_new_state; 4264 4265 return err; 4266 } 4267 4268 int mlx4_ib_modify_wq(struct ib_wq *ibwq, struct ib_wq_attr *wq_attr, 4269 u32 wq_attr_mask, struct ib_udata *udata) 4270 { 4271 struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq); 4272 struct mlx4_ib_modify_wq ucmd = {}; 4273 size_t required_cmd_sz; 4274 enum ib_wq_state cur_state, new_state; 4275 int err = 0; 4276 4277 required_cmd_sz = offsetof(typeof(ucmd), reserved) + 4278 sizeof(ucmd.reserved); 4279 if (udata->inlen < required_cmd_sz) 4280 return -EINVAL; 4281 4282 if (udata->inlen > sizeof(ucmd) && 4283 !ib_is_udata_cleared(udata, sizeof(ucmd), 4284 udata->inlen - sizeof(ucmd))) 4285 return -EOPNOTSUPP; 4286 4287 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) 4288 return -EFAULT; 4289 4290 if (ucmd.comp_mask || ucmd.reserved) 4291 return -EOPNOTSUPP; 4292 4293 if (wq_attr_mask & IB_WQ_FLAGS) 4294 return -EOPNOTSUPP; 4295 4296 cur_state = wq_attr->curr_wq_state; 4297 new_state = wq_attr->wq_state; 4298 4299 if ((new_state == IB_WQS_RDY) && (cur_state == IB_WQS_ERR)) 4300 return -EINVAL; 4301 4302 if ((new_state == IB_WQS_ERR) && (cur_state == IB_WQS_RESET)) 4303 return -EINVAL; 4304 4305 /* Need to protect against the parent RSS which also may modify WQ 4306 * state. 4307 */ 4308 mutex_lock(&qp->mutex); 4309 4310 /* Can update HW state only if a RSS QP has already associated to this 4311 * WQ, so we can apply its port on the WQ. 4312 */ 4313 if (qp->rss_usecnt) 4314 err = _mlx4_ib_modify_wq(ibwq, new_state, udata); 4315 4316 if (!err) 4317 ibwq->state = new_state; 4318 4319 mutex_unlock(&qp->mutex); 4320 4321 return err; 4322 } 4323 4324 int mlx4_ib_destroy_wq(struct ib_wq *ibwq, struct ib_udata *udata) 4325 { 4326 struct mlx4_ib_dev *dev = to_mdev(ibwq->device); 4327 struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq); 4328 4329 if (qp->counter_index) 4330 mlx4_ib_free_qp_counter(dev, qp); 4331 4332 destroy_qp_common(dev, qp, MLX4_IB_RWQ_SRC, udata); 4333 4334 kfree(qp); 4335 return 0; 4336 } 4337 4338 int mlx4_ib_create_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table, 4339 struct ib_rwq_ind_table_init_attr *init_attr, 4340 struct ib_udata *udata) 4341 { 4342 struct mlx4_ib_create_rwq_ind_tbl_resp resp = {}; 4343 unsigned int ind_tbl_size = 1 << init_attr->log_ind_tbl_size; 4344 struct ib_device *device = rwq_ind_table->device; 4345 unsigned int base_wqn; 4346 size_t min_resp_len; 4347 int i, err = 0; 4348 4349 if (udata->inlen > 0 && 4350 !ib_is_udata_cleared(udata, 0, 4351 udata->inlen)) 4352 return -EOPNOTSUPP; 4353 4354 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved); 4355 if (udata->outlen && udata->outlen < min_resp_len) 4356 return -EINVAL; 4357 4358 if (ind_tbl_size > 4359 device->attrs.rss_caps.max_rwq_indirection_table_size) { 4360 pr_debug("log_ind_tbl_size = %d is bigger than supported = %d\n", 4361 ind_tbl_size, 4362 device->attrs.rss_caps.max_rwq_indirection_table_size); 4363 return -EINVAL; 4364 } 4365 4366 base_wqn = init_attr->ind_tbl[0]->wq_num; 4367 4368 if (base_wqn % ind_tbl_size) { 4369 pr_debug("WQN=0x%x isn't aligned with indirection table size\n", 4370 base_wqn); 4371 return -EINVAL; 4372 } 4373 4374 for (i = 1; i < ind_tbl_size; i++) { 4375 if (++base_wqn != init_attr->ind_tbl[i]->wq_num) { 4376 pr_debug("indirection table's WQNs aren't consecutive\n"); 4377 return -EINVAL; 4378 } 4379 } 4380 4381 if (udata->outlen) { 4382 resp.response_length = offsetof(typeof(resp), response_length) + 4383 sizeof(resp.response_length); 4384 err = ib_copy_to_udata(udata, &resp, resp.response_length); 4385 } 4386 4387 return err; 4388 } 4389 4390 struct mlx4_ib_drain_cqe { 4391 struct ib_cqe cqe; 4392 struct completion done; 4393 }; 4394 4395 static void mlx4_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc) 4396 { 4397 struct mlx4_ib_drain_cqe *cqe = container_of(wc->wr_cqe, 4398 struct mlx4_ib_drain_cqe, 4399 cqe); 4400 4401 complete(&cqe->done); 4402 } 4403 4404 /* This function returns only once the drained WR was completed */ 4405 static void handle_drain_completion(struct ib_cq *cq, 4406 struct mlx4_ib_drain_cqe *sdrain, 4407 struct mlx4_ib_dev *dev) 4408 { 4409 struct mlx4_dev *mdev = dev->dev; 4410 4411 if (cq->poll_ctx == IB_POLL_DIRECT) { 4412 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0) 4413 ib_process_cq_direct(cq, -1); 4414 return; 4415 } 4416 4417 if (mdev->persist->state == MLX4_DEVICE_STATE_INTERNAL_ERROR) { 4418 struct mlx4_ib_cq *mcq = to_mcq(cq); 4419 bool triggered = false; 4420 unsigned long flags; 4421 4422 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags); 4423 /* Make sure that the CQ handler won't run if wasn't run yet */ 4424 if (!mcq->mcq.reset_notify_added) 4425 mcq->mcq.reset_notify_added = 1; 4426 else 4427 triggered = true; 4428 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags); 4429 4430 if (triggered) { 4431 /* Wait for any scheduled/running task to be ended */ 4432 switch (cq->poll_ctx) { 4433 case IB_POLL_SOFTIRQ: 4434 irq_poll_disable(&cq->iop); 4435 irq_poll_enable(&cq->iop); 4436 break; 4437 case IB_POLL_WORKQUEUE: 4438 cancel_work_sync(&cq->work); 4439 break; 4440 default: 4441 WARN_ON_ONCE(1); 4442 } 4443 } 4444 4445 /* Run the CQ handler - this makes sure that the drain WR will 4446 * be processed if wasn't processed yet. 4447 */ 4448 mcq->mcq.comp(&mcq->mcq); 4449 } 4450 4451 wait_for_completion(&sdrain->done); 4452 } 4453 4454 void mlx4_ib_drain_sq(struct ib_qp *qp) 4455 { 4456 struct ib_cq *cq = qp->send_cq; 4457 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR }; 4458 struct mlx4_ib_drain_cqe sdrain; 4459 const struct ib_send_wr *bad_swr; 4460 struct ib_rdma_wr swr = { 4461 .wr = { 4462 .next = NULL, 4463 { .wr_cqe = &sdrain.cqe, }, 4464 .opcode = IB_WR_RDMA_WRITE, 4465 }, 4466 }; 4467 int ret; 4468 struct mlx4_ib_dev *dev = to_mdev(qp->device); 4469 struct mlx4_dev *mdev = dev->dev; 4470 4471 ret = ib_modify_qp(qp, &attr, IB_QP_STATE); 4472 if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) { 4473 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret); 4474 return; 4475 } 4476 4477 sdrain.cqe.done = mlx4_ib_drain_qp_done; 4478 init_completion(&sdrain.done); 4479 4480 ret = _mlx4_ib_post_send(qp, &swr.wr, &bad_swr, true); 4481 if (ret) { 4482 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret); 4483 return; 4484 } 4485 4486 handle_drain_completion(cq, &sdrain, dev); 4487 } 4488 4489 void mlx4_ib_drain_rq(struct ib_qp *qp) 4490 { 4491 struct ib_cq *cq = qp->recv_cq; 4492 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR }; 4493 struct mlx4_ib_drain_cqe rdrain; 4494 struct ib_recv_wr rwr = {}; 4495 const struct ib_recv_wr *bad_rwr; 4496 int ret; 4497 struct mlx4_ib_dev *dev = to_mdev(qp->device); 4498 struct mlx4_dev *mdev = dev->dev; 4499 4500 ret = ib_modify_qp(qp, &attr, IB_QP_STATE); 4501 if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) { 4502 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret); 4503 return; 4504 } 4505 4506 rwr.wr_cqe = &rdrain.cqe; 4507 rdrain.cqe.done = mlx4_ib_drain_qp_done; 4508 init_completion(&rdrain.done); 4509 4510 ret = _mlx4_ib_post_recv(qp, &rwr, &bad_rwr, true); 4511 if (ret) { 4512 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret); 4513 return; 4514 } 4515 4516 handle_drain_completion(cq, &rdrain, dev); 4517 } 4518 4519 int mlx4_ib_qp_event_init(void) 4520 { 4521 mlx4_ib_qp_event_wq = alloc_ordered_workqueue("mlx4_ib_qp_event_wq", 0); 4522 if (!mlx4_ib_qp_event_wq) 4523 return -ENOMEM; 4524 4525 return 0; 4526 } 4527 4528 void mlx4_ib_qp_event_cleanup(void) 4529 { 4530 destroy_workqueue(mlx4_ib_qp_event_wq); 4531 } 4532