Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2015 Intel Corporation. |
| 3 | * |
| 4 | * This file is provided under a dual BSD/GPLv2 license. When using or |
| 5 | * redistributing this file, you may do so under either license. |
| 6 | * |
| 7 | * GPL LICENSE SUMMARY |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of version 2 of the GNU General Public License as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * BSD LICENSE |
| 19 | * |
| 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted provided that the following conditions |
| 22 | * are met: |
| 23 | * |
| 24 | * - Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * - Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in |
| 28 | * the documentation and/or other materials provided with the |
| 29 | * distribution. |
| 30 | * - Neither the name of Intel Corporation nor the names of its |
| 31 | * contributors may be used to endorse or promote products derived |
| 32 | * from this software without specific prior written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 35 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 36 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 37 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | * |
| 46 | */ |
| 47 | |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 48 | #include <linux/bitops.h> |
| 49 | #include <linux/lockdep.h> |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 50 | #include <linux/vmalloc.h> |
| 51 | #include <linux/slab.h> |
| 52 | #include <rdma/ib_verbs.h> |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 53 | #include "qp.h" |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 54 | #include "vt.h" |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 55 | |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 56 | static void get_map_page(struct rvt_qpn_table *qpt, struct rvt_qpn_map *map) |
| 57 | { |
| 58 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 59 | |
| 60 | /* |
| 61 | * Free the page if someone raced with us installing it. |
| 62 | */ |
| 63 | |
| 64 | spin_lock(&qpt->lock); |
| 65 | if (map->page) |
| 66 | free_page(page); |
| 67 | else |
| 68 | map->page = (void *)page; |
| 69 | spin_unlock(&qpt->lock); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * init_qpn_table - initialize the QP number table for a device |
| 74 | * @qpt: the QPN table |
| 75 | */ |
| 76 | static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt) |
| 77 | { |
| 78 | u32 offset, i; |
| 79 | struct rvt_qpn_map *map; |
| 80 | int ret = 0; |
| 81 | |
| 82 | if (!(rdi->dparms.qpn_res_end > rdi->dparms.qpn_res_start)) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | spin_lock_init(&qpt->lock); |
| 86 | |
| 87 | qpt->last = rdi->dparms.qpn_start; |
| 88 | qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift; |
| 89 | |
| 90 | /* |
| 91 | * Drivers may want some QPs beyond what we need for verbs let them use |
| 92 | * our qpn table. No need for two. Lets go ahead and mark the bitmaps |
| 93 | * for those. The reserved range must be *after* the range which verbs |
| 94 | * will pick from. |
| 95 | */ |
| 96 | |
| 97 | /* Figure out number of bit maps needed before reserved range */ |
| 98 | qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE; |
| 99 | |
| 100 | /* This should always be zero */ |
| 101 | offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK; |
| 102 | |
| 103 | /* Starting with the first reserved bit map */ |
| 104 | map = &qpt->map[qpt->nmaps]; |
| 105 | |
| 106 | rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n", |
| 107 | rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end); |
| 108 | for (i = rdi->dparms.qpn_res_start; i < rdi->dparms.qpn_res_end; i++) { |
| 109 | if (!map->page) { |
| 110 | get_map_page(qpt, map); |
| 111 | if (!map->page) { |
| 112 | ret = -ENOMEM; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | set_bit(offset, map->page); |
| 117 | offset++; |
| 118 | if (offset == RVT_BITS_PER_PAGE) { |
| 119 | /* next page */ |
| 120 | qpt->nmaps++; |
| 121 | map++; |
| 122 | offset = 0; |
| 123 | } |
| 124 | } |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * free_qpn_table - free the QP number table for a device |
| 130 | * @qpt: the QPN table |
| 131 | */ |
| 132 | static void free_qpn_table(struct rvt_qpn_table *qpt) |
| 133 | { |
| 134 | int i; |
| 135 | |
| 136 | for (i = 0; i < ARRAY_SIZE(qpt->map); i++) |
| 137 | free_page((unsigned long)qpt->map[i].page); |
| 138 | } |
| 139 | |
| 140 | int rvt_driver_qp_init(struct rvt_dev_info *rdi) |
| 141 | { |
| 142 | int i; |
| 143 | int ret = -ENOMEM; |
| 144 | |
| 145 | if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER) { |
| 146 | rvt_pr_info(rdi, "Driver is doing QP init.\n"); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | if (!rdi->dparms.qp_table_size) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | /* |
| 154 | * If driver is not doing any QP allocation then make sure it is |
| 155 | * providing the necessary QP functions. |
| 156 | */ |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 157 | if (!rdi->driver_f.free_all_qps || |
| 158 | !rdi->driver_f.qp_priv_alloc || |
| 159 | !rdi->driver_f.qp_priv_free || |
| 160 | !rdi->driver_f.notify_qp_reset) |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | |
| 163 | /* allocate parent object */ |
| 164 | rdi->qp_dev = kzalloc(sizeof(*rdi->qp_dev), GFP_KERNEL); |
| 165 | if (!rdi->qp_dev) |
| 166 | return -ENOMEM; |
| 167 | |
| 168 | /* allocate hash table */ |
| 169 | rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size; |
| 170 | rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size); |
| 171 | rdi->qp_dev->qp_table = |
| 172 | kmalloc(rdi->qp_dev->qp_table_size * |
| 173 | sizeof(*rdi->qp_dev->qp_table), |
| 174 | GFP_KERNEL); |
| 175 | if (!rdi->qp_dev->qp_table) |
| 176 | goto no_qp_table; |
| 177 | |
| 178 | for (i = 0; i < rdi->qp_dev->qp_table_size; i++) |
| 179 | RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL); |
| 180 | |
| 181 | spin_lock_init(&rdi->qp_dev->qpt_lock); |
| 182 | |
| 183 | /* initialize qpn map */ |
| 184 | if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table)) |
| 185 | goto fail_table; |
| 186 | |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 187 | spin_lock_init(&rdi->n_qps_lock); |
| 188 | |
| 189 | return 0; |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 190 | |
| 191 | fail_table: |
| 192 | kfree(rdi->qp_dev->qp_table); |
| 193 | free_qpn_table(&rdi->qp_dev->qpn_table); |
| 194 | |
| 195 | no_qp_table: |
| 196 | kfree(rdi->qp_dev); |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * free_all_qps - check for QPs still in use |
| 203 | * @qpt: the QP table to empty |
| 204 | * |
| 205 | * There should not be any QPs still in use. |
| 206 | * Free memory for table. |
| 207 | */ |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 208 | static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi) |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 209 | { |
| 210 | unsigned long flags; |
| 211 | struct rvt_qp *qp; |
| 212 | unsigned n, qp_inuse = 0; |
| 213 | spinlock_t *ql; /* work around too long line below */ |
| 214 | |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 215 | if (rdi->driver_f.free_all_qps) |
| 216 | qp_inuse = rdi->driver_f.free_all_qps(rdi); |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 217 | |
| 218 | if (!rdi->qp_dev) |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 219 | return qp_inuse; |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 220 | |
| 221 | ql = &rdi->qp_dev->qpt_lock; |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 222 | spin_lock_irqsave(ql, flags); |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 223 | for (n = 0; n < rdi->qp_dev->qp_table_size; n++) { |
| 224 | qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n], |
| 225 | lockdep_is_held(ql)); |
| 226 | RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL); |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 227 | |
| 228 | for (; qp; qp = rcu_dereference_protected(qp->next, |
| 229 | lockdep_is_held(ql))) |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 230 | qp_inuse++; |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 231 | } |
| 232 | spin_unlock_irqrestore(ql, flags); |
| 233 | synchronize_rcu(); |
| 234 | return qp_inuse; |
| 235 | } |
| 236 | |
| 237 | void rvt_qp_exit(struct rvt_dev_info *rdi) |
| 238 | { |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 239 | u32 qps_inuse = rvt_free_all_qps(rdi); |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 240 | |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 241 | if (qps_inuse) |
| 242 | rvt_pr_err(rdi, "QP memory leak! %u still in use\n", |
| 243 | qps_inuse); |
| 244 | if (!rdi->qp_dev) |
| 245 | return; |
| 246 | |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 247 | if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER) |
| 248 | return; /* driver did the qp init so nothing else to do */ |
| 249 | |
Dennis Dalessandro | 0acb0cc | 2016-01-06 10:04:46 -0800 | [diff] [blame] | 250 | kfree(rdi->qp_dev->qp_table); |
| 251 | free_qpn_table(&rdi->qp_dev->qpn_table); |
| 252 | kfree(rdi->qp_dev); |
| 253 | } |
| 254 | |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 255 | static inline unsigned mk_qpn(struct rvt_qpn_table *qpt, |
| 256 | struct rvt_qpn_map *map, unsigned off) |
| 257 | { |
| 258 | return (map - qpt->map) * RVT_BITS_PER_PAGE + off; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Allocate the next available QPN or |
| 263 | * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI. |
| 264 | */ |
| 265 | static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt, |
| 266 | enum ib_qp_type type, u8 port) |
| 267 | { |
| 268 | u32 i, offset, max_scan, qpn; |
| 269 | struct rvt_qpn_map *map; |
| 270 | u32 ret; |
| 271 | |
| 272 | if (rdi->driver_f.alloc_qpn) |
| 273 | return rdi->driver_f.alloc_qpn(rdi, qpt, type, port); |
| 274 | |
| 275 | if (type == IB_QPT_SMI || type == IB_QPT_GSI) { |
| 276 | unsigned n; |
| 277 | |
| 278 | ret = type == IB_QPT_GSI; |
| 279 | n = 1 << (ret + 2 * (port - 1)); |
| 280 | spin_lock(&qpt->lock); |
| 281 | if (qpt->flags & n) |
| 282 | ret = -EINVAL; |
| 283 | else |
| 284 | qpt->flags |= n; |
| 285 | spin_unlock(&qpt->lock); |
| 286 | goto bail; |
| 287 | } |
| 288 | |
| 289 | qpn = qpt->last + qpt->incr; |
| 290 | if (qpn >= RVT_QPN_MAX) |
| 291 | qpn = qpt->incr | ((qpt->last & 1) ^ 1); |
| 292 | /* offset carries bit 0 */ |
| 293 | offset = qpn & RVT_BITS_PER_PAGE_MASK; |
| 294 | map = &qpt->map[qpn / RVT_BITS_PER_PAGE]; |
| 295 | max_scan = qpt->nmaps - !offset; |
| 296 | for (i = 0;;) { |
| 297 | if (unlikely(!map->page)) { |
| 298 | get_map_page(qpt, map); |
| 299 | if (unlikely(!map->page)) |
| 300 | break; |
| 301 | } |
| 302 | do { |
| 303 | if (!test_and_set_bit(offset, map->page)) { |
| 304 | qpt->last = qpn; |
| 305 | ret = qpn; |
| 306 | goto bail; |
| 307 | } |
| 308 | offset += qpt->incr; |
| 309 | /* |
| 310 | * This qpn might be bogus if offset >= BITS_PER_PAGE. |
| 311 | * That is OK. It gets re-assigned below |
| 312 | */ |
| 313 | qpn = mk_qpn(qpt, map, offset); |
| 314 | } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX); |
| 315 | /* |
| 316 | * In order to keep the number of pages allocated to a |
| 317 | * minimum, we scan the all existing pages before increasing |
| 318 | * the size of the bitmap table. |
| 319 | */ |
| 320 | if (++i > max_scan) { |
| 321 | if (qpt->nmaps == RVT_QPNMAP_ENTRIES) |
| 322 | break; |
| 323 | map = &qpt->map[qpt->nmaps++]; |
| 324 | /* start at incr with current bit 0 */ |
| 325 | offset = qpt->incr | (offset & 1); |
| 326 | } else if (map < &qpt->map[qpt->nmaps]) { |
| 327 | ++map; |
| 328 | /* start at incr with current bit 0 */ |
| 329 | offset = qpt->incr | (offset & 1); |
| 330 | } else { |
| 331 | map = &qpt->map[0]; |
| 332 | /* wrap to first map page, invert bit 0 */ |
| 333 | offset = qpt->incr | ((offset & 1) ^ 1); |
| 334 | } |
| 335 | /* there can be no bits at shift and below */ |
| 336 | WARN_ON(offset & (rdi->dparms.qos_shift - 1)); |
| 337 | qpn = mk_qpn(qpt, map, offset); |
| 338 | } |
| 339 | |
| 340 | ret = -ENOMEM; |
| 341 | |
| 342 | bail: |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn) |
| 347 | { |
| 348 | struct rvt_qpn_map *map; |
| 349 | |
| 350 | map = qpt->map + qpn / RVT_BITS_PER_PAGE; |
| 351 | if (map->page) |
| 352 | clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * reset_qp - initialize the QP state to the reset state |
| 357 | * @qp: the QP to reset |
| 358 | * @type: the QP type |
| 359 | */ |
| 360 | static void reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp, |
| 361 | enum ib_qp_type type) |
| 362 | { |
| 363 | qp->remote_qpn = 0; |
| 364 | qp->qkey = 0; |
| 365 | qp->qp_access_flags = 0; |
| 366 | |
| 367 | /* |
| 368 | * Let driver do anything it needs to for a new/reset qp |
| 369 | */ |
| 370 | rdi->driver_f.notify_qp_reset(qp); |
| 371 | |
| 372 | qp->s_flags &= RVT_S_SIGNAL_REQ_WR; |
| 373 | qp->s_hdrwords = 0; |
| 374 | qp->s_wqe = NULL; |
| 375 | qp->s_draining = 0; |
| 376 | qp->s_next_psn = 0; |
| 377 | qp->s_last_psn = 0; |
| 378 | qp->s_sending_psn = 0; |
| 379 | qp->s_sending_hpsn = 0; |
| 380 | qp->s_psn = 0; |
| 381 | qp->r_psn = 0; |
| 382 | qp->r_msn = 0; |
| 383 | if (type == IB_QPT_RC) { |
| 384 | qp->s_state = IB_OPCODE_RC_SEND_LAST; |
| 385 | qp->r_state = IB_OPCODE_RC_SEND_LAST; |
| 386 | } else { |
| 387 | qp->s_state = IB_OPCODE_UC_SEND_LAST; |
| 388 | qp->r_state = IB_OPCODE_UC_SEND_LAST; |
| 389 | } |
| 390 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; |
| 391 | qp->r_nak_state = 0; |
| 392 | qp->r_aflags = 0; |
| 393 | qp->r_flags = 0; |
| 394 | qp->s_head = 0; |
| 395 | qp->s_tail = 0; |
| 396 | qp->s_cur = 0; |
| 397 | qp->s_acked = 0; |
| 398 | qp->s_last = 0; |
| 399 | qp->s_ssn = 1; |
| 400 | qp->s_lsn = 0; |
| 401 | qp->s_mig_state = IB_MIG_MIGRATED; |
| 402 | memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue)); |
| 403 | qp->r_head_ack_queue = 0; |
| 404 | qp->s_tail_ack_queue = 0; |
| 405 | qp->s_num_rd_atomic = 0; |
| 406 | if (qp->r_rq.wq) { |
| 407 | qp->r_rq.wq->head = 0; |
| 408 | qp->r_rq.wq->tail = 0; |
| 409 | } |
| 410 | qp->r_sge.num_sge = 0; |
| 411 | } |
| 412 | |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 413 | /** |
| 414 | * rvt_create_qp - create a queue pair for a device |
| 415 | * @ibpd: the protection domain who's device we create the queue pair for |
| 416 | * @init_attr: the attributes of the queue pair |
| 417 | * @udata: user data for libibverbs.so |
| 418 | * |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 419 | * Queue pair creation is mostly an rvt issue. However, drivers have their own |
| 420 | * unique idea of what queue pair numbers mean. For instance there is a reserved |
| 421 | * range for PSM. |
| 422 | * |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 423 | * Returns the queue pair on success, otherwise returns an errno. |
| 424 | * |
| 425 | * Called by the ib_create_qp() core verbs function. |
| 426 | */ |
| 427 | struct ib_qp *rvt_create_qp(struct ib_pd *ibpd, |
| 428 | struct ib_qp_init_attr *init_attr, |
| 429 | struct ib_udata *udata) |
| 430 | { |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 431 | struct rvt_qp *qp; |
| 432 | int err; |
| 433 | struct rvt_swqe *swq = NULL; |
| 434 | size_t sz; |
| 435 | size_t sg_list_sz; |
| 436 | struct ib_qp *ret = ERR_PTR(-ENOMEM); |
| 437 | struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device); |
| 438 | void *priv = NULL; |
| 439 | |
| 440 | if (!rdi) |
| 441 | return ERR_PTR(-EINVAL); |
| 442 | |
| 443 | if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge || |
| 444 | init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr || |
| 445 | init_attr->create_flags) |
| 446 | return ERR_PTR(-EINVAL); |
| 447 | |
| 448 | /* Check receive queue parameters if no SRQ is specified. */ |
| 449 | if (!init_attr->srq) { |
| 450 | if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge || |
| 451 | init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr) |
| 452 | return ERR_PTR(-EINVAL); |
| 453 | |
| 454 | if (init_attr->cap.max_send_sge + |
| 455 | init_attr->cap.max_send_wr + |
| 456 | init_attr->cap.max_recv_sge + |
| 457 | init_attr->cap.max_recv_wr == 0) |
| 458 | return ERR_PTR(-EINVAL); |
| 459 | } |
| 460 | |
| 461 | switch (init_attr->qp_type) { |
| 462 | case IB_QPT_SMI: |
| 463 | case IB_QPT_GSI: |
| 464 | if (init_attr->port_num == 0 || |
| 465 | init_attr->port_num > ibpd->device->phys_port_cnt) |
| 466 | return ERR_PTR(-EINVAL); |
| 467 | case IB_QPT_UC: |
| 468 | case IB_QPT_RC: |
| 469 | case IB_QPT_UD: |
| 470 | sz = sizeof(struct rvt_sge) * |
| 471 | init_attr->cap.max_send_sge + |
| 472 | sizeof(struct rvt_swqe); |
| 473 | swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz); |
| 474 | if (!swq) |
| 475 | return ERR_PTR(-ENOMEM); |
| 476 | |
| 477 | sz = sizeof(*qp); |
| 478 | sg_list_sz = 0; |
| 479 | if (init_attr->srq) { |
| 480 | struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq); |
| 481 | |
| 482 | if (srq->rq.max_sge > 1) |
| 483 | sg_list_sz = sizeof(*qp->r_sg_list) * |
| 484 | (srq->rq.max_sge - 1); |
| 485 | } else if (init_attr->cap.max_recv_sge > 1) |
| 486 | sg_list_sz = sizeof(*qp->r_sg_list) * |
| 487 | (init_attr->cap.max_recv_sge - 1); |
| 488 | qp = kzalloc(sz + sg_list_sz, GFP_KERNEL); |
| 489 | if (!qp) |
| 490 | goto bail_swq; |
| 491 | |
| 492 | RCU_INIT_POINTER(qp->next, NULL); |
| 493 | |
| 494 | /* |
| 495 | * Driver needs to set up it's private QP structure and do any |
| 496 | * initialization that is needed. |
| 497 | */ |
| 498 | priv = rdi->driver_f.qp_priv_alloc(rdi, qp); |
| 499 | if (!priv) |
| 500 | goto bail_qp; |
| 501 | qp->priv = priv; |
| 502 | qp->timeout_jiffies = |
| 503 | usecs_to_jiffies((4096UL * (1UL << qp->timeout)) / |
| 504 | 1000UL); |
| 505 | if (init_attr->srq) { |
| 506 | sz = 0; |
| 507 | } else { |
| 508 | qp->r_rq.size = init_attr->cap.max_recv_wr + 1; |
| 509 | qp->r_rq.max_sge = init_attr->cap.max_recv_sge; |
| 510 | sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) + |
| 511 | sizeof(struct rvt_rwqe); |
| 512 | qp->r_rq.wq = vmalloc_user(sizeof(struct rvt_rwq) + |
| 513 | qp->r_rq.size * sz); |
| 514 | if (!qp->r_rq.wq) |
| 515 | goto bail_driver_priv; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * ib_create_qp() will initialize qp->ibqp |
| 520 | * except for qp->ibqp.qp_num. |
| 521 | */ |
| 522 | spin_lock_init(&qp->r_lock); |
| 523 | spin_lock_init(&qp->s_lock); |
| 524 | spin_lock_init(&qp->r_rq.lock); |
| 525 | atomic_set(&qp->refcount, 0); |
| 526 | init_waitqueue_head(&qp->wait); |
| 527 | init_timer(&qp->s_timer); |
| 528 | qp->s_timer.data = (unsigned long)qp; |
| 529 | INIT_LIST_HEAD(&qp->rspwait); |
| 530 | qp->state = IB_QPS_RESET; |
| 531 | qp->s_wq = swq; |
| 532 | qp->s_size = init_attr->cap.max_send_wr + 1; |
| 533 | qp->s_max_sge = init_attr->cap.max_send_sge; |
| 534 | if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR) |
| 535 | qp->s_flags = RVT_S_SIGNAL_REQ_WR; |
| 536 | |
| 537 | err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table, |
| 538 | init_attr->qp_type, |
| 539 | init_attr->port_num); |
| 540 | if (err < 0) { |
| 541 | ret = ERR_PTR(err); |
| 542 | goto bail_rq_wq; |
| 543 | } |
| 544 | qp->ibqp.qp_num = err; |
| 545 | qp->port_num = init_attr->port_num; |
| 546 | reset_qp(rdi, qp, init_attr->qp_type); |
| 547 | break; |
| 548 | |
| 549 | default: |
| 550 | /* Don't support raw QPs */ |
| 551 | return ERR_PTR(-EINVAL); |
| 552 | } |
| 553 | |
| 554 | init_attr->cap.max_inline_data = 0; |
| 555 | |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 556 | /* |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 557 | * Return the address of the RWQ as the offset to mmap. |
| 558 | * See hfi1_mmap() for details. |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 559 | */ |
Dennis Dalessandro | 515667f | 2016-01-22 12:50:17 -0800 | [diff] [blame^] | 560 | if (udata && udata->outlen >= sizeof(__u64)) { |
| 561 | if (!qp->r_rq.wq) { |
| 562 | __u64 offset = 0; |
| 563 | |
| 564 | err = ib_copy_to_udata(udata, &offset, |
| 565 | sizeof(offset)); |
| 566 | if (err) { |
| 567 | ret = ERR_PTR(err); |
| 568 | goto bail_qpn; |
| 569 | } |
| 570 | } else { |
| 571 | u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz; |
| 572 | |
| 573 | qp->ip = rvt_create_mmap_info(rdi, s, |
| 574 | ibpd->uobject->context, |
| 575 | qp->r_rq.wq); |
| 576 | if (!qp->ip) { |
| 577 | ret = ERR_PTR(-ENOMEM); |
| 578 | goto bail_qpn; |
| 579 | } |
| 580 | |
| 581 | err = ib_copy_to_udata(udata, &qp->ip->offset, |
| 582 | sizeof(qp->ip->offset)); |
| 583 | if (err) { |
| 584 | ret = ERR_PTR(err); |
| 585 | goto bail_ip; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | spin_lock(&rdi->n_qps_lock); |
| 591 | if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) { |
| 592 | spin_unlock(&rdi->n_qps_lock); |
| 593 | ret = ERR_PTR(-ENOMEM); |
| 594 | goto bail_ip; |
| 595 | } |
| 596 | |
| 597 | rdi->n_qps_allocated++; |
| 598 | spin_unlock(&rdi->n_qps_lock); |
| 599 | |
| 600 | if (qp->ip) { |
| 601 | spin_lock_irq(&rdi->pending_lock); |
| 602 | list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps); |
| 603 | spin_unlock_irq(&rdi->pending_lock); |
| 604 | } |
| 605 | |
| 606 | ret = &qp->ibqp; |
| 607 | |
| 608 | /* |
| 609 | * We have our QP and its good, now keep track of what types of opcodes |
| 610 | * can be processed on this QP. We do this by keeping track of what the |
| 611 | * 3 high order bits of the opcode are. |
| 612 | */ |
| 613 | switch (init_attr->qp_type) { |
| 614 | case IB_QPT_SMI: |
| 615 | case IB_QPT_GSI: |
| 616 | case IB_QPT_UD: |
| 617 | qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK; |
| 618 | break; |
| 619 | case IB_QPT_RC: |
| 620 | qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK; |
| 621 | break; |
| 622 | case IB_QPT_UC: |
| 623 | qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK; |
| 624 | break; |
| 625 | default: |
| 626 | ret = ERR_PTR(-EINVAL); |
| 627 | goto bail_ip; |
| 628 | } |
| 629 | |
| 630 | return ret; |
| 631 | |
| 632 | bail_ip: |
| 633 | kref_put(&qp->ip->ref, rvt_release_mmap_info); |
| 634 | |
| 635 | bail_qpn: |
| 636 | free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num); |
| 637 | |
| 638 | bail_rq_wq: |
| 639 | vfree(qp->r_rq.wq); |
| 640 | |
| 641 | bail_driver_priv: |
| 642 | rdi->driver_f.qp_priv_free(rdi, qp); |
| 643 | |
| 644 | bail_qp: |
| 645 | kfree(qp); |
| 646 | |
| 647 | bail_swq: |
| 648 | vfree(swq); |
| 649 | |
| 650 | return ret; |
Dennis Dalessandro | b518d3e | 2016-01-06 09:56:15 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
| 654 | * qib_modify_qp - modify the attributes of a queue pair |
| 655 | * @ibqp: the queue pair who's attributes we're modifying |
| 656 | * @attr: the new attributes |
| 657 | * @attr_mask: the mask of attributes to modify |
| 658 | * @udata: user data for libibverbs.so |
| 659 | * |
| 660 | * Returns 0 on success, otherwise returns an errno. |
| 661 | */ |
| 662 | int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 663 | int attr_mask, struct ib_udata *udata) |
| 664 | { |
| 665 | /* |
| 666 | * VT-DRIVER-API: qp_mtu() |
| 667 | * OPA devices have a per VL MTU the driver has a mapping of IB SL to SC |
| 668 | * to VL and the mapping table of MTUs per VL. This is not something |
| 669 | * that IB has and should not live in the rvt. |
| 670 | */ |
| 671 | return -EOPNOTSUPP; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * rvt_destroy_qp - destroy a queue pair |
| 676 | * @ibqp: the queue pair to destroy |
| 677 | * |
| 678 | * Returns 0 on success. |
| 679 | * |
| 680 | * Note that this can be called while the QP is actively sending or |
| 681 | * receiving! |
| 682 | */ |
| 683 | int rvt_destroy_qp(struct ib_qp *ibqp) |
| 684 | { |
| 685 | /* |
| 686 | * VT-DRIVER-API: qp_flush() |
| 687 | * Driver provies a mechanism to flush and wait for that flush to |
| 688 | * finish. |
| 689 | */ |
| 690 | |
| 691 | return -EOPNOTSUPP; |
| 692 | } |
| 693 | |
| 694 | int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 695 | int attr_mask, struct ib_qp_init_attr *init_attr) |
| 696 | { |
| 697 | return -EOPNOTSUPP; |
| 698 | } |
Dennis Dalessandro | 8cf4020 | 2016-01-06 10:01:17 -0800 | [diff] [blame] | 699 | |
| 700 | /** |
| 701 | * rvt_post_receive - post a receive on a QP |
| 702 | * @ibqp: the QP to post the receive on |
| 703 | * @wr: the WR to post |
| 704 | * @bad_wr: the first bad WR is put here |
| 705 | * |
| 706 | * This may be called from interrupt context. |
| 707 | */ |
| 708 | int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr, |
| 709 | struct ib_recv_wr **bad_wr) |
| 710 | { |
| 711 | /* |
| 712 | * When a packet arrives the driver needs to call up to rvt to process |
| 713 | * the packet. The UD, RC, UC processing will be done in rvt, however |
| 714 | * the driver should be able to override this if it so choses. Perhaps a |
| 715 | * set of function pointers set up at registration time. |
| 716 | */ |
| 717 | |
| 718 | return -EOPNOTSUPP; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * rvt_post_send - post a send on a QP |
| 723 | * @ibqp: the QP to post the send on |
| 724 | * @wr: the list of work requests to post |
| 725 | * @bad_wr: the first bad WR is put here |
| 726 | * |
| 727 | * This may be called from interrupt context. |
| 728 | */ |
| 729 | int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr, |
| 730 | struct ib_send_wr **bad_wr) |
| 731 | { |
| 732 | /* |
| 733 | * VT-DRIVER-API: do_send() |
| 734 | * Driver needs to have a do_send() call which is a single entry point |
| 735 | * to take an already formed packet and throw it out on the wire. Once |
| 736 | * the packet is sent the driver needs to make an upcall to rvt so the |
| 737 | * completion queue can be notified and/or any other outstanding |
| 738 | * work/book keeping can be finished. |
| 739 | * |
| 740 | * Note that there should also be a way for rvt to protect itself |
| 741 | * against hangs in the driver layer. If a send doesn't actually |
| 742 | * complete in a timely manor rvt needs to return an error event. |
| 743 | */ |
| 744 | |
| 745 | return -EOPNOTSUPP; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * rvt_post_srq_receive - post a receive on a shared receive queue |
| 750 | * @ibsrq: the SRQ to post the receive on |
| 751 | * @wr: the list of work requests to post |
| 752 | * @bad_wr: A pointer to the first WR to cause a problem is put here |
| 753 | * |
| 754 | * This may be called from interrupt context. |
| 755 | */ |
| 756 | int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, |
| 757 | struct ib_recv_wr **bad_wr) |
| 758 | { |
| 759 | return -EOPNOTSUPP; |
| 760 | } |