Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1 | /* |
Ralph Campbell | e7eacd3 | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved. |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 3 | * Copyright (c) 2005, 2006 PathScale, Inc. 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/err.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame^] | 35 | #include <linux/sched.h> |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 36 | #include <linux/vmalloc.h> |
| 37 | |
| 38 | #include "ipath_verbs.h" |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 39 | #include "ipath_kernel.h" |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 40 | |
| 41 | #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE) |
| 42 | #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1) |
| 43 | #define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \ |
| 44 | (off)) |
| 45 | #define find_next_offset(map, off) find_next_zero_bit((map)->page, \ |
| 46 | BITS_PER_PAGE, off) |
| 47 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 48 | /* |
| 49 | * Convert the AETH credit code into the number of credits. |
| 50 | */ |
| 51 | static u32 credit_table[31] = { |
| 52 | 0, /* 0 */ |
| 53 | 1, /* 1 */ |
| 54 | 2, /* 2 */ |
| 55 | 3, /* 3 */ |
| 56 | 4, /* 4 */ |
| 57 | 6, /* 5 */ |
| 58 | 8, /* 6 */ |
| 59 | 12, /* 7 */ |
| 60 | 16, /* 8 */ |
| 61 | 24, /* 9 */ |
| 62 | 32, /* A */ |
| 63 | 48, /* B */ |
| 64 | 64, /* C */ |
| 65 | 96, /* D */ |
| 66 | 128, /* E */ |
| 67 | 192, /* F */ |
| 68 | 256, /* 10 */ |
| 69 | 384, /* 11 */ |
| 70 | 512, /* 12 */ |
| 71 | 768, /* 13 */ |
| 72 | 1024, /* 14 */ |
| 73 | 1536, /* 15 */ |
| 74 | 2048, /* 16 */ |
| 75 | 3072, /* 17 */ |
| 76 | 4096, /* 18 */ |
| 77 | 6144, /* 19 */ |
| 78 | 8192, /* 1A */ |
| 79 | 12288, /* 1B */ |
| 80 | 16384, /* 1C */ |
| 81 | 24576, /* 1D */ |
| 82 | 32768 /* 1E */ |
| 83 | }; |
| 84 | |
Bryan O'Sullivan | 662af58 | 2007-03-15 14:45:12 -0700 | [diff] [blame] | 85 | |
| 86 | static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map) |
| 87 | { |
| 88 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 89 | unsigned long flags; |
| 90 | |
| 91 | /* |
| 92 | * Free the page if someone raced with us installing it. |
| 93 | */ |
| 94 | |
| 95 | spin_lock_irqsave(&qpt->lock, flags); |
| 96 | if (map->page) |
| 97 | free_page(page); |
| 98 | else |
| 99 | map->page = (void *)page; |
| 100 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static int alloc_qpn(struct ipath_qp_table *qpt, enum ib_qp_type type) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 105 | { |
| 106 | u32 i, offset, max_scan, qpn; |
| 107 | struct qpn_map *map; |
Bryan O'Sullivan | 662af58 | 2007-03-15 14:45:12 -0700 | [diff] [blame] | 108 | u32 ret = -1; |
| 109 | |
| 110 | if (type == IB_QPT_SMI) |
| 111 | ret = 0; |
| 112 | else if (type == IB_QPT_GSI) |
| 113 | ret = 1; |
| 114 | |
| 115 | if (ret != -1) { |
| 116 | map = &qpt->map[0]; |
| 117 | if (unlikely(!map->page)) { |
| 118 | get_map_page(qpt, map); |
| 119 | if (unlikely(!map->page)) { |
| 120 | ret = -ENOMEM; |
| 121 | goto bail; |
| 122 | } |
| 123 | } |
| 124 | if (!test_and_set_bit(ret, map->page)) |
| 125 | atomic_dec(&map->n_free); |
| 126 | else |
| 127 | ret = -EBUSY; |
| 128 | goto bail; |
| 129 | } |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 130 | |
| 131 | qpn = qpt->last + 1; |
| 132 | if (qpn >= QPN_MAX) |
| 133 | qpn = 2; |
| 134 | offset = qpn & BITS_PER_PAGE_MASK; |
| 135 | map = &qpt->map[qpn / BITS_PER_PAGE]; |
| 136 | max_scan = qpt->nmaps - !offset; |
| 137 | for (i = 0;;) { |
| 138 | if (unlikely(!map->page)) { |
Bryan O'Sullivan | 662af58 | 2007-03-15 14:45:12 -0700 | [diff] [blame] | 139 | get_map_page(qpt, map); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 140 | if (unlikely(!map->page)) |
| 141 | break; |
| 142 | } |
| 143 | if (likely(atomic_read(&map->n_free))) { |
| 144 | do { |
| 145 | if (!test_and_set_bit(offset, map->page)) { |
| 146 | atomic_dec(&map->n_free); |
| 147 | qpt->last = qpn; |
| 148 | ret = qpn; |
| 149 | goto bail; |
| 150 | } |
| 151 | offset = find_next_offset(map, offset); |
| 152 | qpn = mk_qpn(qpt, map, offset); |
| 153 | /* |
| 154 | * This test differs from alloc_pidmap(). |
| 155 | * If find_next_offset() does find a zero |
| 156 | * bit, we don't need to check for QPN |
| 157 | * wrapping around past our starting QPN. |
| 158 | * We just need to be sure we don't loop |
| 159 | * forever. |
| 160 | */ |
| 161 | } while (offset < BITS_PER_PAGE && qpn < QPN_MAX); |
| 162 | } |
| 163 | /* |
| 164 | * In order to keep the number of pages allocated to a |
| 165 | * minimum, we scan the all existing pages before increasing |
| 166 | * the size of the bitmap table. |
| 167 | */ |
| 168 | if (++i > max_scan) { |
| 169 | if (qpt->nmaps == QPNMAP_ENTRIES) |
| 170 | break; |
| 171 | map = &qpt->map[qpt->nmaps++]; |
| 172 | offset = 0; |
| 173 | } else if (map < &qpt->map[qpt->nmaps]) { |
| 174 | ++map; |
| 175 | offset = 0; |
| 176 | } else { |
| 177 | map = &qpt->map[0]; |
| 178 | offset = 2; |
| 179 | } |
| 180 | qpn = mk_qpn(qpt, map, offset); |
| 181 | } |
| 182 | |
Bryan O'Sullivan | 662af58 | 2007-03-15 14:45:12 -0700 | [diff] [blame] | 183 | ret = -ENOMEM; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 184 | |
| 185 | bail: |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static void free_qpn(struct ipath_qp_table *qpt, u32 qpn) |
| 190 | { |
| 191 | struct qpn_map *map; |
| 192 | |
| 193 | map = qpt->map + qpn / BITS_PER_PAGE; |
| 194 | if (map->page) |
| 195 | clear_bit(qpn & BITS_PER_PAGE_MASK, map->page); |
| 196 | atomic_inc(&map->n_free); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * ipath_alloc_qpn - allocate a QP number |
| 201 | * @qpt: the QP table |
| 202 | * @qp: the QP |
| 203 | * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special) |
| 204 | * |
| 205 | * Allocate the next available QPN and put the QP into the hash table. |
| 206 | * The hash table holds a reference to the QP. |
| 207 | */ |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 208 | static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp, |
| 209 | enum ib_qp_type type) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 210 | { |
| 211 | unsigned long flags; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 212 | int ret; |
| 213 | |
Bryan O'Sullivan | 662af58 | 2007-03-15 14:45:12 -0700 | [diff] [blame] | 214 | ret = alloc_qpn(qpt, type); |
| 215 | if (ret < 0) |
| 216 | goto bail; |
| 217 | qp->ibqp.qp_num = ret; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 218 | |
| 219 | /* Add the QP to the hash table. */ |
| 220 | spin_lock_irqsave(&qpt->lock, flags); |
| 221 | |
Bryan O'Sullivan | 662af58 | 2007-03-15 14:45:12 -0700 | [diff] [blame] | 222 | ret %= qpt->max; |
| 223 | qp->next = qpt->table[ret]; |
| 224 | qpt->table[ret] = qp; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 225 | atomic_inc(&qp->refcount); |
| 226 | |
| 227 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 228 | ret = 0; |
| 229 | |
| 230 | bail: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * ipath_free_qp - remove a QP from the QP table |
| 236 | * @qpt: the QP table |
| 237 | * @qp: the QP to remove |
| 238 | * |
| 239 | * Remove the QP from the table so it can't be found asynchronously by |
| 240 | * the receive interrupt routine. |
| 241 | */ |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 242 | static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 243 | { |
| 244 | struct ipath_qp *q, **qpp; |
| 245 | unsigned long flags; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 246 | |
| 247 | spin_lock_irqsave(&qpt->lock, flags); |
| 248 | |
| 249 | /* Remove QP from the hash table. */ |
| 250 | qpp = &qpt->table[qp->ibqp.qp_num % qpt->max]; |
| 251 | for (; (q = *qpp) != NULL; qpp = &q->next) { |
| 252 | if (q == qp) { |
| 253 | *qpp = qp->next; |
| 254 | qp->next = NULL; |
| 255 | atomic_dec(&qp->refcount); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | spin_unlock_irqrestore(&qpt->lock, flags); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 264 | * ipath_free_all_qps - check for QPs still in use |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 265 | * @qpt: the QP table to empty |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 266 | * |
| 267 | * There should not be any QPs still in use. |
| 268 | * Free memory for table. |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 269 | */ |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 270 | unsigned ipath_free_all_qps(struct ipath_qp_table *qpt) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 271 | { |
| 272 | unsigned long flags; |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 273 | struct ipath_qp *qp; |
| 274 | u32 n, qp_inuse = 0; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 275 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 276 | spin_lock_irqsave(&qpt->lock, flags); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 277 | for (n = 0; n < qpt->max; n++) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 278 | qp = qpt->table[n]; |
| 279 | qpt->table[n] = NULL; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 280 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 281 | for (; qp; qp = qp->next) |
| 282 | qp_inuse++; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 283 | } |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 284 | spin_unlock_irqrestore(&qpt->lock, flags); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 285 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 286 | for (n = 0; n < ARRAY_SIZE(qpt->map); n++) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 287 | if (qpt->map[n].page) |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 288 | free_page((unsigned long) qpt->map[n].page); |
| 289 | return qp_inuse; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * ipath_lookup_qpn - return the QP with the given QPN |
| 294 | * @qpt: the QP table |
| 295 | * @qpn: the QP number to look up |
| 296 | * |
| 297 | * The caller is responsible for decrementing the QP reference count |
| 298 | * when done. |
| 299 | */ |
| 300 | struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn) |
| 301 | { |
| 302 | unsigned long flags; |
| 303 | struct ipath_qp *qp; |
| 304 | |
| 305 | spin_lock_irqsave(&qpt->lock, flags); |
| 306 | |
| 307 | for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) { |
| 308 | if (qp->ibqp.qp_num == qpn) { |
| 309 | atomic_inc(&qp->refcount); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 315 | return qp; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * ipath_reset_qp - initialize the QP state to the reset state |
| 320 | * @qp: the QP to reset |
Patrick Marchand Latifi | 4cd5060 | 2008-01-18 20:10:48 -0800 | [diff] [blame] | 321 | * @type: the QP type |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 322 | */ |
Patrick Marchand Latifi | 4cd5060 | 2008-01-18 20:10:48 -0800 | [diff] [blame] | 323 | static void ipath_reset_qp(struct ipath_qp *qp, enum ib_qp_type type) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 324 | { |
| 325 | qp->remote_qpn = 0; |
| 326 | qp->qkey = 0; |
| 327 | qp->qp_access_flags = 0; |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 328 | atomic_set(&qp->s_dma_busy, 0); |
Robert Walsh | 991bda2 | 2007-06-04 09:55:48 -0700 | [diff] [blame] | 329 | qp->s_flags &= IPATH_S_SIGNAL_REQ_WR; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 330 | qp->s_hdrwords = 0; |
Ralph Campbell | 4ee9718 | 2007-07-25 11:08:28 -0700 | [diff] [blame] | 331 | qp->s_wqe = NULL; |
Dave Olson | 124b4dc | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 332 | qp->s_pkt_delay = 0; |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 333 | qp->s_draining = 0; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 334 | qp->s_psn = 0; |
| 335 | qp->r_psn = 0; |
Bryan O'Sullivan | 12eef41 | 2006-07-01 04:36:10 -0700 | [diff] [blame] | 336 | qp->r_msn = 0; |
Patrick Marchand Latifi | 4cd5060 | 2008-01-18 20:10:48 -0800 | [diff] [blame] | 337 | if (type == IB_QPT_RC) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 338 | qp->s_state = IB_OPCODE_RC_SEND_LAST; |
| 339 | qp->r_state = IB_OPCODE_RC_SEND_LAST; |
| 340 | } else { |
| 341 | qp->s_state = IB_OPCODE_UC_SEND_LAST; |
| 342 | qp->r_state = IB_OPCODE_UC_SEND_LAST; |
| 343 | } |
| 344 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; |
Bryan O'Sullivan | 12eef41 | 2006-07-01 04:36:10 -0700 | [diff] [blame] | 345 | qp->r_nak_state = 0; |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 346 | qp->r_aflags = 0; |
| 347 | qp->r_flags = 0; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 348 | qp->s_rnr_timeout = 0; |
| 349 | qp->s_head = 0; |
| 350 | qp->s_tail = 0; |
| 351 | qp->s_cur = 0; |
| 352 | qp->s_last = 0; |
| 353 | qp->s_ssn = 1; |
| 354 | qp->s_lsn = 0; |
Ralph Campbell | 3859e39 | 2007-03-15 14:44:51 -0700 | [diff] [blame] | 355 | memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue)); |
| 356 | qp->r_head_ack_queue = 0; |
| 357 | qp->s_tail_ack_queue = 0; |
| 358 | qp->s_num_rd_atomic = 0; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 359 | if (qp->r_rq.wq) { |
| 360 | qp->r_rq.wq->head = 0; |
| 361 | qp->r_rq.wq->tail = 0; |
| 362 | } |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
Ralph Campbell | 53dc1ca | 2008-05-13 11:40:25 -0700 | [diff] [blame] | 366 | * ipath_error_qp - put a QP into the error state |
| 367 | * @qp: the QP to put into the error state |
Bryan O'Sullivan | 8d0208c | 2006-09-28 09:00:14 -0700 | [diff] [blame] | 368 | * @err: the receive completion error to signal if a RWQE is active |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 369 | * |
| 370 | * Flushes both send and receive work queues. |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 371 | * Returns true if last WQE event should be generated. |
Ralph Campbell | 0434d27 | 2007-03-15 14:44:53 -0700 | [diff] [blame] | 372 | * The QP s_lock should be held and interrupts disabled. |
Ralph Campbell | 53dc1ca | 2008-05-13 11:40:25 -0700 | [diff] [blame] | 373 | * If we are already in error state, just return. |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 374 | */ |
| 375 | |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 376 | int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err) |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 377 | { |
| 378 | struct ipath_ibdev *dev = to_idev(qp->ibqp.device); |
| 379 | struct ib_wc wc; |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 380 | int ret = 0; |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 381 | |
Ralph Campbell | 53dc1ca | 2008-05-13 11:40:25 -0700 | [diff] [blame] | 382 | if (qp->state == IB_QPS_ERR) |
| 383 | goto bail; |
| 384 | |
| 385 | qp->state = IB_QPS_ERR; |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 386 | |
| 387 | spin_lock(&dev->pending_lock); |
Bryan O'Sullivan | 94b8d9f | 2006-05-23 11:32:32 -0700 | [diff] [blame] | 388 | if (!list_empty(&qp->timerwait)) |
| 389 | list_del_init(&qp->timerwait); |
| 390 | if (!list_empty(&qp->piowait)) |
| 391 | list_del_init(&qp->piowait); |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 392 | spin_unlock(&dev->pending_lock); |
| 393 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 394 | /* Schedule the sending tasklet to drain the send work queue. */ |
| 395 | if (qp->s_last != qp->s_head) |
| 396 | ipath_schedule_send(qp); |
| 397 | |
| 398 | memset(&wc, 0, sizeof(wc)); |
Michael S. Tsirkin | 062dbb6 | 2006-12-31 21:09:42 +0200 | [diff] [blame] | 399 | wc.qp = &qp->ibqp; |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 400 | wc.opcode = IB_WC_RECV; |
| 401 | |
| 402 | if (test_and_clear_bit(IPATH_R_WRID_VALID, &qp->r_aflags)) { |
Ralph Campbell | 0434d27 | 2007-03-15 14:44:53 -0700 | [diff] [blame] | 403 | wc.wr_id = qp->r_wr_id; |
Bryan O'Sullivan | 8d0208c | 2006-09-28 09:00:14 -0700 | [diff] [blame] | 404 | wc.status = err; |
Patrick Marchand Latifi | 2a049e5 | 2008-01-31 00:24:37 -0800 | [diff] [blame] | 405 | ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1); |
Bryan O'Sullivan | 8d0208c | 2006-09-28 09:00:14 -0700 | [diff] [blame] | 406 | } |
| 407 | wc.status = IB_WC_WR_FLUSH_ERR; |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 408 | |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 409 | if (qp->r_rq.wq) { |
| 410 | struct ipath_rwq *wq; |
| 411 | u32 head; |
| 412 | u32 tail; |
| 413 | |
| 414 | spin_lock(&qp->r_rq.lock); |
| 415 | |
| 416 | /* sanity check pointers before trusting them */ |
| 417 | wq = qp->r_rq.wq; |
| 418 | head = wq->head; |
| 419 | if (head >= qp->r_rq.size) |
| 420 | head = 0; |
| 421 | tail = wq->tail; |
| 422 | if (tail >= qp->r_rq.size) |
| 423 | tail = 0; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 424 | while (tail != head) { |
| 425 | wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id; |
| 426 | if (++tail >= qp->r_rq.size) |
| 427 | tail = 0; |
| 428 | ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1); |
| 429 | } |
| 430 | wq->tail = tail; |
| 431 | |
| 432 | spin_unlock(&qp->r_rq.lock); |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 433 | } else if (qp->ibqp.event_handler) |
| 434 | ret = 1; |
| 435 | |
Ralph Campbell | 53dc1ca | 2008-05-13 11:40:25 -0700 | [diff] [blame] | 436 | bail: |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 437 | return ret; |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 441 | * ipath_modify_qp - modify the attributes of a queue pair |
| 442 | * @ibqp: the queue pair who's attributes we're modifying |
| 443 | * @attr: the new attributes |
| 444 | * @attr_mask: the mask of attributes to modify |
Ralph Campbell | 9bc57e2 | 2006-08-11 14:58:09 -0700 | [diff] [blame] | 445 | * @udata: user data for ipathverbs.so |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 446 | * |
| 447 | * Returns 0 on success, otherwise returns an errno. |
| 448 | */ |
| 449 | int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
Ralph Campbell | 9bc57e2 | 2006-08-11 14:58:09 -0700 | [diff] [blame] | 450 | int attr_mask, struct ib_udata *udata) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 451 | { |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 452 | struct ipath_ibdev *dev = to_idev(ibqp->device); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 453 | struct ipath_qp *qp = to_iqp(ibqp); |
| 454 | enum ib_qp_state cur_state, new_state; |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 455 | int lastwqe = 0; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 456 | int ret; |
| 457 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 458 | spin_lock_irq(&qp->s_lock); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 459 | |
| 460 | cur_state = attr_mask & IB_QP_CUR_STATE ? |
| 461 | attr->cur_qp_state : qp->state; |
| 462 | new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; |
| 463 | |
| 464 | if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, |
| 465 | attr_mask)) |
| 466 | goto inval; |
| 467 | |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 468 | if (attr_mask & IB_QP_AV) { |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 469 | if (attr->ah_attr.dlid == 0 || |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 470 | attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE) |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 471 | goto inval; |
| 472 | |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 473 | if ((attr->ah_attr.ah_flags & IB_AH_GRH) && |
| 474 | (attr->ah_attr.grh.sgid_index > 1)) |
| 475 | goto inval; |
| 476 | } |
| 477 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 478 | if (attr_mask & IB_QP_PKEY_INDEX) |
Bryan O'Sullivan | 34b2aaf | 2006-08-25 11:24:32 -0700 | [diff] [blame] | 479 | if (attr->pkey_index >= ipath_get_npkeys(dev->dd)) |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 480 | goto inval; |
| 481 | |
| 482 | if (attr_mask & IB_QP_MIN_RNR_TIMER) |
| 483 | if (attr->min_rnr_timer > 31) |
| 484 | goto inval; |
| 485 | |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 486 | if (attr_mask & IB_QP_PORT) |
| 487 | if (attr->port_num == 0 || |
| 488 | attr->port_num > ibqp->device->phys_port_cnt) |
| 489 | goto inval; |
| 490 | |
Robert Walsh | e7340f0 | 2007-06-18 14:24:35 -0700 | [diff] [blame] | 491 | /* |
Dave Olson | 826d801 | 2008-04-16 21:01:12 -0700 | [diff] [blame] | 492 | * don't allow invalid Path MTU values or greater than 2048 |
| 493 | * unless we are configured for a 4KB MTU |
Robert Walsh | e7340f0 | 2007-06-18 14:24:35 -0700 | [diff] [blame] | 494 | */ |
Dave Olson | 826d801 | 2008-04-16 21:01:12 -0700 | [diff] [blame] | 495 | if ((attr_mask & IB_QP_PATH_MTU) && |
| 496 | (ib_mtu_enum_to_int(attr->path_mtu) == -1 || |
| 497 | (attr->path_mtu > IB_MTU_2048 && !ipath_mtu4096))) |
| 498 | goto inval; |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 499 | |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 500 | if (attr_mask & IB_QP_PATH_MIG_STATE) |
Bryan O'Sullivan | ca4ce38 | 2006-08-25 11:24:42 -0700 | [diff] [blame] | 501 | if (attr->path_mig_state != IB_MIG_MIGRATED && |
| 502 | attr->path_mig_state != IB_MIG_REARM) |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 503 | goto inval; |
| 504 | |
Ralph Campbell | 3859e39 | 2007-03-15 14:44:51 -0700 | [diff] [blame] | 505 | if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) |
| 506 | if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC) |
| 507 | goto inval; |
| 508 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 509 | switch (new_state) { |
| 510 | case IB_QPS_RESET: |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 511 | if (qp->state != IB_QPS_RESET) { |
| 512 | qp->state = IB_QPS_RESET; |
| 513 | spin_lock(&dev->pending_lock); |
| 514 | if (!list_empty(&qp->timerwait)) |
| 515 | list_del_init(&qp->timerwait); |
| 516 | if (!list_empty(&qp->piowait)) |
| 517 | list_del_init(&qp->piowait); |
| 518 | spin_unlock(&dev->pending_lock); |
| 519 | qp->s_flags &= ~IPATH_S_ANY_WAIT; |
| 520 | spin_unlock_irq(&qp->s_lock); |
| 521 | /* Stop the sending tasklet */ |
| 522 | tasklet_kill(&qp->s_task); |
| 523 | wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy)); |
| 524 | spin_lock_irq(&qp->s_lock); |
| 525 | } |
Patrick Marchand Latifi | 4cd5060 | 2008-01-18 20:10:48 -0800 | [diff] [blame] | 526 | ipath_reset_qp(qp, ibqp->qp_type); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 527 | break; |
| 528 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 529 | case IB_QPS_SQD: |
| 530 | qp->s_draining = qp->s_last != qp->s_cur; |
| 531 | qp->state = new_state; |
| 532 | break; |
| 533 | |
| 534 | case IB_QPS_SQE: |
| 535 | if (qp->ibqp.qp_type == IB_QPT_RC) |
| 536 | goto inval; |
| 537 | qp->state = new_state; |
| 538 | break; |
| 539 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 540 | case IB_QPS_ERR: |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 541 | lastwqe = ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 542 | break; |
| 543 | |
| 544 | default: |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 545 | qp->state = new_state; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 546 | break; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 549 | if (attr_mask & IB_QP_PKEY_INDEX) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 550 | qp->s_pkey_index = attr->pkey_index; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 551 | |
| 552 | if (attr_mask & IB_QP_DEST_QPN) |
| 553 | qp->remote_qpn = attr->dest_qp_num; |
| 554 | |
| 555 | if (attr_mask & IB_QP_SQ_PSN) { |
Bryan O'Sullivan | 6022943 | 2006-09-28 08:59:57 -0700 | [diff] [blame] | 556 | qp->s_psn = qp->s_next_psn = attr->sq_psn; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 557 | qp->s_last_psn = qp->s_next_psn - 1; |
| 558 | } |
| 559 | |
| 560 | if (attr_mask & IB_QP_RQ_PSN) |
| 561 | qp->r_psn = attr->rq_psn; |
| 562 | |
| 563 | if (attr_mask & IB_QP_ACCESS_FLAGS) |
| 564 | qp->qp_access_flags = attr->qp_access_flags; |
| 565 | |
Dave Olson | 124b4dc | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 566 | if (attr_mask & IB_QP_AV) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 567 | qp->remote_ah_attr = attr->ah_attr; |
Dave Olson | 124b4dc | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 568 | qp->s_dmult = ipath_ib_rate_to_mult(attr->ah_attr.static_rate); |
| 569 | } |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 570 | |
| 571 | if (attr_mask & IB_QP_PATH_MTU) |
| 572 | qp->path_mtu = attr->path_mtu; |
| 573 | |
| 574 | if (attr_mask & IB_QP_RETRY_CNT) |
| 575 | qp->s_retry = qp->s_retry_cnt = attr->retry_cnt; |
| 576 | |
| 577 | if (attr_mask & IB_QP_RNR_RETRY) { |
| 578 | qp->s_rnr_retry = attr->rnr_retry; |
| 579 | if (qp->s_rnr_retry > 7) |
| 580 | qp->s_rnr_retry = 7; |
| 581 | qp->s_rnr_retry_cnt = qp->s_rnr_retry; |
| 582 | } |
| 583 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 584 | if (attr_mask & IB_QP_MIN_RNR_TIMER) |
Bryan O'Sullivan | 12eef41 | 2006-07-01 04:36:10 -0700 | [diff] [blame] | 585 | qp->r_min_rnr_timer = attr->min_rnr_timer; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 586 | |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 587 | if (attr_mask & IB_QP_TIMEOUT) |
| 588 | qp->timeout = attr->timeout; |
| 589 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 590 | if (attr_mask & IB_QP_QKEY) |
| 591 | qp->qkey = attr->qkey; |
| 592 | |
Ralph Campbell | 3859e39 | 2007-03-15 14:44:51 -0700 | [diff] [blame] | 593 | if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) |
| 594 | qp->r_max_rd_atomic = attr->max_dest_rd_atomic; |
| 595 | |
| 596 | if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) |
| 597 | qp->s_max_rd_atomic = attr->max_rd_atomic; |
| 598 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 599 | spin_unlock_irq(&qp->s_lock); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 600 | |
Ralph Campbell | d42b01b | 2007-08-25 16:45:03 -0700 | [diff] [blame] | 601 | if (lastwqe) { |
| 602 | struct ib_event ev; |
| 603 | |
| 604 | ev.device = qp->ibqp.device; |
| 605 | ev.element.qp = &qp->ibqp; |
| 606 | ev.event = IB_EVENT_QP_LAST_WQE_REACHED; |
| 607 | qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); |
| 608 | } |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 609 | ret = 0; |
| 610 | goto bail; |
| 611 | |
| 612 | inval: |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 613 | spin_unlock_irq(&qp->s_lock); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 614 | ret = -EINVAL; |
| 615 | |
| 616 | bail: |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 621 | int attr_mask, struct ib_qp_init_attr *init_attr) |
| 622 | { |
| 623 | struct ipath_qp *qp = to_iqp(ibqp); |
| 624 | |
| 625 | attr->qp_state = qp->state; |
| 626 | attr->cur_qp_state = attr->qp_state; |
| 627 | attr->path_mtu = qp->path_mtu; |
| 628 | attr->path_mig_state = 0; |
| 629 | attr->qkey = qp->qkey; |
| 630 | attr->rq_psn = qp->r_psn; |
| 631 | attr->sq_psn = qp->s_next_psn; |
| 632 | attr->dest_qp_num = qp->remote_qpn; |
| 633 | attr->qp_access_flags = qp->qp_access_flags; |
| 634 | attr->cap.max_send_wr = qp->s_size - 1; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 635 | attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 636 | attr->cap.max_send_sge = qp->s_max_sge; |
| 637 | attr->cap.max_recv_sge = qp->r_rq.max_sge; |
| 638 | attr->cap.max_inline_data = 0; |
| 639 | attr->ah_attr = qp->remote_ah_attr; |
| 640 | memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr)); |
| 641 | attr->pkey_index = qp->s_pkey_index; |
| 642 | attr->alt_pkey_index = 0; |
| 643 | attr->en_sqd_async_notify = 0; |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 644 | attr->sq_draining = qp->s_draining; |
Ralph Campbell | 3859e39 | 2007-03-15 14:44:51 -0700 | [diff] [blame] | 645 | attr->max_rd_atomic = qp->s_max_rd_atomic; |
| 646 | attr->max_dest_rd_atomic = qp->r_max_rd_atomic; |
Bryan O'Sullivan | 12eef41 | 2006-07-01 04:36:10 -0700 | [diff] [blame] | 647 | attr->min_rnr_timer = qp->r_min_rnr_timer; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 648 | attr->port_num = 1; |
Bryan O'Sullivan | fc8cf8c | 2006-08-25 11:24:41 -0700 | [diff] [blame] | 649 | attr->timeout = qp->timeout; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 650 | attr->retry_cnt = qp->s_retry_cnt; |
Patrick Marchand Latifi | 87d5aed | 2008-01-07 23:43:04 -0800 | [diff] [blame] | 651 | attr->rnr_retry = qp->s_rnr_retry_cnt; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 652 | attr->alt_port_num = 0; |
| 653 | attr->alt_timeout = 0; |
| 654 | |
| 655 | init_attr->event_handler = qp->ibqp.event_handler; |
| 656 | init_attr->qp_context = qp->ibqp.qp_context; |
| 657 | init_attr->send_cq = qp->ibqp.send_cq; |
| 658 | init_attr->recv_cq = qp->ibqp.recv_cq; |
| 659 | init_attr->srq = qp->ibqp.srq; |
| 660 | init_attr->cap = attr->cap; |
Ralph Campbell | 3859e39 | 2007-03-15 14:44:51 -0700 | [diff] [blame] | 661 | if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR) |
Bryan O'Sullivan | a78aa6f | 2006-08-25 11:24:44 -0700 | [diff] [blame] | 662 | init_attr->sq_sig_type = IB_SIGNAL_REQ_WR; |
| 663 | else |
| 664 | init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 665 | init_attr->qp_type = qp->ibqp.qp_type; |
| 666 | init_attr->port_num = 1; |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * ipath_compute_aeth - compute the AETH (syndrome + MSN) |
| 672 | * @qp: the queue pair to compute the AETH for |
| 673 | * |
| 674 | * Returns the AETH. |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 675 | */ |
| 676 | __be32 ipath_compute_aeth(struct ipath_qp *qp) |
| 677 | { |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 678 | u32 aeth = qp->r_msn & IPATH_MSN_MASK; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 679 | |
Bryan O'Sullivan | 12eef41 | 2006-07-01 04:36:10 -0700 | [diff] [blame] | 680 | if (qp->ibqp.srq) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 681 | /* |
| 682 | * Shared receive queues don't generate credits. |
| 683 | * Set the credit field to the invalid value. |
| 684 | */ |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 685 | aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 686 | } else { |
| 687 | u32 min, max, x; |
| 688 | u32 credits; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 689 | struct ipath_rwq *wq = qp->r_rq.wq; |
| 690 | u32 head; |
| 691 | u32 tail; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 692 | |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 693 | /* sanity check pointers before trusting them */ |
| 694 | head = wq->head; |
| 695 | if (head >= qp->r_rq.size) |
| 696 | head = 0; |
| 697 | tail = wq->tail; |
| 698 | if (tail >= qp->r_rq.size) |
| 699 | tail = 0; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 700 | /* |
| 701 | * Compute the number of credits available (RWQEs). |
| 702 | * XXX Not holding the r_rq.lock here so there is a small |
| 703 | * chance that the pair of reads are not atomic. |
| 704 | */ |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 705 | credits = head - tail; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 706 | if ((int)credits < 0) |
| 707 | credits += qp->r_rq.size; |
| 708 | /* |
| 709 | * Binary search the credit table to find the code to |
| 710 | * use. |
| 711 | */ |
| 712 | min = 0; |
| 713 | max = 31; |
| 714 | for (;;) { |
| 715 | x = (min + max) / 2; |
| 716 | if (credit_table[x] == credits) |
| 717 | break; |
| 718 | if (credit_table[x] > credits) |
| 719 | max = x; |
| 720 | else if (min == x) |
| 721 | break; |
| 722 | else |
| 723 | min = x; |
| 724 | } |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 725 | aeth |= x << IPATH_AETH_CREDIT_SHIFT; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 726 | } |
| 727 | return cpu_to_be32(aeth); |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * ipath_create_qp - create a queue pair for a device |
| 732 | * @ibpd: the protection domain who's device we create the queue pair for |
| 733 | * @init_attr: the attributes of the queue pair |
| 734 | * @udata: unused by InfiniPath |
| 735 | * |
| 736 | * Returns the queue pair on success, otherwise returns an errno. |
| 737 | * |
| 738 | * Called by the ib_create_qp() core verbs function. |
| 739 | */ |
| 740 | struct ib_qp *ipath_create_qp(struct ib_pd *ibpd, |
| 741 | struct ib_qp_init_attr *init_attr, |
| 742 | struct ib_udata *udata) |
| 743 | { |
| 744 | struct ipath_qp *qp; |
| 745 | int err; |
| 746 | struct ipath_swqe *swq = NULL; |
| 747 | struct ipath_ibdev *dev; |
| 748 | size_t sz; |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 749 | size_t sg_list_sz; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 750 | struct ib_qp *ret; |
| 751 | |
Eli Cohen | b846f25 | 2008-04-16 21:09:27 -0700 | [diff] [blame] | 752 | if (init_attr->create_flags) { |
| 753 | ret = ERR_PTR(-EINVAL); |
| 754 | goto bail; |
| 755 | } |
| 756 | |
Bryan O'Sullivan | fe62546 | 2006-07-01 04:35:58 -0700 | [diff] [blame] | 757 | if (init_attr->cap.max_send_sge > ib_ipath_max_sges || |
Ralph Campbell | 10a8c3c | 2008-04-16 21:09:25 -0700 | [diff] [blame] | 758 | init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs) { |
| 759 | ret = ERR_PTR(-EINVAL); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 760 | goto bail; |
| 761 | } |
| 762 | |
Ralph Campbell | 10a8c3c | 2008-04-16 21:09:25 -0700 | [diff] [blame] | 763 | /* Check receive queue parameters if no SRQ is specified. */ |
| 764 | if (!init_attr->srq) { |
| 765 | if (init_attr->cap.max_recv_sge > ib_ipath_max_sges || |
| 766 | init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) { |
| 767 | ret = ERR_PTR(-EINVAL); |
| 768 | goto bail; |
| 769 | } |
| 770 | if (init_attr->cap.max_send_sge + |
| 771 | init_attr->cap.max_send_wr + |
| 772 | init_attr->cap.max_recv_sge + |
| 773 | init_attr->cap.max_recv_wr == 0) { |
| 774 | ret = ERR_PTR(-EINVAL); |
| 775 | goto bail; |
| 776 | } |
Bryan O'Sullivan | 4a45b7d | 2006-07-01 04:35:55 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 779 | switch (init_attr->qp_type) { |
| 780 | case IB_QPT_UC: |
| 781 | case IB_QPT_RC: |
Ralph Campbell | 4ee9718 | 2007-07-25 11:08:28 -0700 | [diff] [blame] | 782 | case IB_QPT_UD: |
| 783 | case IB_QPT_SMI: |
| 784 | case IB_QPT_GSI: |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 785 | sz = sizeof(struct ipath_sge) * |
| 786 | init_attr->cap.max_send_sge + |
| 787 | sizeof(struct ipath_swqe); |
| 788 | swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz); |
| 789 | if (swq == NULL) { |
| 790 | ret = ERR_PTR(-ENOMEM); |
| 791 | goto bail; |
| 792 | } |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 793 | sz = sizeof(*qp); |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 794 | sg_list_sz = 0; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 795 | if (init_attr->srq) { |
| 796 | struct ipath_srq *srq = to_isrq(init_attr->srq); |
| 797 | |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 798 | if (srq->rq.max_sge > 1) |
| 799 | sg_list_sz = sizeof(*qp->r_sg_list) * |
| 800 | (srq->rq.max_sge - 1); |
| 801 | } else if (init_attr->cap.max_recv_sge > 1) |
| 802 | sg_list_sz = sizeof(*qp->r_sg_list) * |
| 803 | (init_attr->cap.max_recv_sge - 1); |
| 804 | qp = kmalloc(sz + sg_list_sz, GFP_KERNEL); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 805 | if (!qp) { |
| 806 | ret = ERR_PTR(-ENOMEM); |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 807 | goto bail_swq; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 808 | } |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 809 | if (sg_list_sz && (init_attr->qp_type == IB_QPT_UD || |
| 810 | init_attr->qp_type == IB_QPT_SMI || |
| 811 | init_attr->qp_type == IB_QPT_GSI)) { |
| 812 | qp->r_ud_sg_list = kmalloc(sg_list_sz, GFP_KERNEL); |
| 813 | if (!qp->r_ud_sg_list) { |
| 814 | ret = ERR_PTR(-ENOMEM); |
| 815 | goto bail_qp; |
| 816 | } |
| 817 | } else |
| 818 | qp->r_ud_sg_list = NULL; |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 819 | if (init_attr->srq) { |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 820 | sz = 0; |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 821 | qp->r_rq.size = 0; |
| 822 | qp->r_rq.max_sge = 0; |
| 823 | qp->r_rq.wq = NULL; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 824 | init_attr->cap.max_recv_wr = 0; |
| 825 | init_attr->cap.max_recv_sge = 0; |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 826 | } else { |
| 827 | qp->r_rq.size = init_attr->cap.max_recv_wr + 1; |
| 828 | qp->r_rq.max_sge = init_attr->cap.max_recv_sge; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 829 | sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) + |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 830 | sizeof(struct ipath_rwqe); |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 831 | qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + |
| 832 | qp->r_rq.size * sz); |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 833 | if (!qp->r_rq.wq) { |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 834 | ret = ERR_PTR(-ENOMEM); |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 835 | goto bail_sg_list; |
Bryan O'Sullivan | 357b552 | 2006-07-01 04:36:16 -0700 | [diff] [blame] | 836 | } |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* |
| 840 | * ib_create_qp() will initialize qp->ibqp |
| 841 | * except for qp->ibqp.qp_num. |
| 842 | */ |
| 843 | spin_lock_init(&qp->s_lock); |
| 844 | spin_lock_init(&qp->r_rq.lock); |
| 845 | atomic_set(&qp->refcount, 0); |
| 846 | init_waitqueue_head(&qp->wait); |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 847 | init_waitqueue_head(&qp->wait_dma); |
Ralph Campbell | 4ee9718 | 2007-07-25 11:08:28 -0700 | [diff] [blame] | 848 | tasklet_init(&qp->s_task, ipath_do_send, (unsigned long)qp); |
Bryan O'Sullivan | 94b8d9f | 2006-05-23 11:32:32 -0700 | [diff] [blame] | 849 | INIT_LIST_HEAD(&qp->piowait); |
| 850 | INIT_LIST_HEAD(&qp->timerwait); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 851 | qp->state = IB_QPS_RESET; |
| 852 | qp->s_wq = swq; |
| 853 | qp->s_size = init_attr->cap.max_send_wr + 1; |
| 854 | qp->s_max_sge = init_attr->cap.max_send_sge; |
Bryan O'Sullivan | a78aa6f | 2006-08-25 11:24:44 -0700 | [diff] [blame] | 855 | if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR) |
Ralph Campbell | 3859e39 | 2007-03-15 14:44:51 -0700 | [diff] [blame] | 856 | qp->s_flags = IPATH_S_SIGNAL_REQ_WR; |
Bryan O'Sullivan | a78aa6f | 2006-08-25 11:24:44 -0700 | [diff] [blame] | 857 | else |
| 858 | qp->s_flags = 0; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 859 | dev = to_idev(ibpd->device); |
| 860 | err = ipath_alloc_qpn(&dev->qp_table, qp, |
| 861 | init_attr->qp_type); |
| 862 | if (err) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 863 | ret = ERR_PTR(err); |
Ralph Campbell | 8a278e6 | 2007-11-14 12:09:05 -0800 | [diff] [blame] | 864 | vfree(qp->r_rq.wq); |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 865 | goto bail_sg_list; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 866 | } |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 867 | qp->ip = NULL; |
Dave Olson | 124b4dc | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 868 | qp->s_tx = NULL; |
Patrick Marchand Latifi | 4cd5060 | 2008-01-18 20:10:48 -0800 | [diff] [blame] | 869 | ipath_reset_qp(qp, init_attr->qp_type); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 870 | break; |
| 871 | |
| 872 | default: |
| 873 | /* Don't support raw QPs */ |
| 874 | ret = ERR_PTR(-ENOSYS); |
| 875 | goto bail; |
| 876 | } |
| 877 | |
| 878 | init_attr->cap.max_inline_data = 0; |
| 879 | |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 880 | /* |
| 881 | * Return the address of the RWQ as the offset to mmap. |
| 882 | * See ipath_mmap() for details. |
| 883 | */ |
| 884 | if (udata && udata->outlen >= sizeof(__u64)) { |
Robert Walsh | 6b66b2d | 2007-04-27 21:07:23 -0700 | [diff] [blame] | 885 | if (!qp->r_rq.wq) { |
| 886 | __u64 offset = 0; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 887 | |
Robert Walsh | 6b66b2d | 2007-04-27 21:07:23 -0700 | [diff] [blame] | 888 | err = ib_copy_to_udata(udata, &offset, |
| 889 | sizeof(offset)); |
| 890 | if (err) { |
| 891 | ret = ERR_PTR(err); |
Ralph Campbell | 8a278e6 | 2007-11-14 12:09:05 -0800 | [diff] [blame] | 892 | goto bail_ip; |
Robert Walsh | 6b66b2d | 2007-04-27 21:07:23 -0700 | [diff] [blame] | 893 | } |
| 894 | } else { |
| 895 | u32 s = sizeof(struct ipath_rwq) + |
| 896 | qp->r_rq.size * sz; |
| 897 | |
| 898 | qp->ip = |
| 899 | ipath_create_mmap_info(dev, s, |
| 900 | ibpd->uobject->context, |
| 901 | qp->r_rq.wq); |
| 902 | if (!qp->ip) { |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 903 | ret = ERR_PTR(-ENOMEM); |
Ralph Campbell | 8a278e6 | 2007-11-14 12:09:05 -0800 | [diff] [blame] | 904 | goto bail_ip; |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 905 | } |
Robert Walsh | 6b66b2d | 2007-04-27 21:07:23 -0700 | [diff] [blame] | 906 | |
| 907 | err = ib_copy_to_udata(udata, &(qp->ip->offset), |
| 908 | sizeof(qp->ip->offset)); |
| 909 | if (err) { |
| 910 | ret = ERR_PTR(err); |
| 911 | goto bail_ip; |
| 912 | } |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Bryan O'Sullivan | 0b81e4f | 2006-08-25 11:24:43 -0700 | [diff] [blame] | 916 | spin_lock(&dev->n_qps_lock); |
| 917 | if (dev->n_qps_allocated == ib_ipath_max_qps) { |
| 918 | spin_unlock(&dev->n_qps_lock); |
| 919 | ret = ERR_PTR(-ENOMEM); |
| 920 | goto bail_ip; |
| 921 | } |
| 922 | |
| 923 | dev->n_qps_allocated++; |
| 924 | spin_unlock(&dev->n_qps_lock); |
| 925 | |
Robert Walsh | 6b66b2d | 2007-04-27 21:07:23 -0700 | [diff] [blame] | 926 | if (qp->ip) { |
| 927 | spin_lock_irq(&dev->pending_lock); |
| 928 | list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps); |
| 929 | spin_unlock_irq(&dev->pending_lock); |
| 930 | } |
| 931 | |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 932 | ret = &qp->ibqp; |
| 933 | goto bail; |
| 934 | |
Bryan O'Sullivan | 0b81e4f | 2006-08-25 11:24:43 -0700 | [diff] [blame] | 935 | bail_ip: |
Ralph Campbell | 8a278e6 | 2007-11-14 12:09:05 -0800 | [diff] [blame] | 936 | if (qp->ip) |
| 937 | kref_put(&qp->ip->ref, ipath_release_mmap_info); |
| 938 | else |
| 939 | vfree(qp->r_rq.wq); |
| 940 | ipath_free_qp(&dev->qp_table, qp); |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 941 | free_qpn(&dev->qp_table, qp->ibqp.qp_num); |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 942 | bail_sg_list: |
| 943 | kfree(qp->r_ud_sg_list); |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 944 | bail_qp: |
| 945 | kfree(qp); |
| 946 | bail_swq: |
| 947 | vfree(swq); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 948 | bail: |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * ipath_destroy_qp - destroy a queue pair |
| 954 | * @ibqp: the queue pair to destroy |
| 955 | * |
| 956 | * Returns 0 on success. |
| 957 | * |
| 958 | * Note that this can be called while the QP is actively sending or |
| 959 | * receiving! |
| 960 | */ |
| 961 | int ipath_destroy_qp(struct ib_qp *ibqp) |
| 962 | { |
| 963 | struct ipath_qp *qp = to_iqp(ibqp); |
| 964 | struct ipath_ibdev *dev = to_idev(ibqp->device); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 965 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 966 | /* Make sure HW and driver activity is stopped. */ |
| 967 | spin_lock_irq(&qp->s_lock); |
| 968 | if (qp->state != IB_QPS_RESET) { |
| 969 | qp->state = IB_QPS_RESET; |
| 970 | spin_lock(&dev->pending_lock); |
| 971 | if (!list_empty(&qp->timerwait)) |
| 972 | list_del_init(&qp->timerwait); |
| 973 | if (!list_empty(&qp->piowait)) |
| 974 | list_del_init(&qp->piowait); |
| 975 | spin_unlock(&dev->pending_lock); |
| 976 | qp->s_flags &= ~IPATH_S_ANY_WAIT; |
| 977 | spin_unlock_irq(&qp->s_lock); |
| 978 | /* Stop the sending tasklet */ |
| 979 | tasklet_kill(&qp->s_task); |
| 980 | wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy)); |
| 981 | } else |
| 982 | spin_unlock_irq(&qp->s_lock); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 983 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 984 | ipath_free_qp(&dev->qp_table, qp); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 985 | |
Dave Olson | 124b4dc | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 986 | if (qp->s_tx) { |
| 987 | atomic_dec(&qp->refcount); |
| 988 | if (qp->s_tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEBUF) |
| 989 | kfree(qp->s_tx->txreq.map_addr); |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 990 | spin_lock_irq(&dev->pending_lock); |
| 991 | list_add(&qp->s_tx->txreq.list, &dev->txreq_free); |
| 992 | spin_unlock_irq(&dev->pending_lock); |
| 993 | qp->s_tx = NULL; |
Dave Olson | 124b4dc | 2008-04-16 21:09:32 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 996 | wait_event(qp->wait, !atomic_read(&qp->refcount)); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 997 | |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 998 | /* all user's cleaned up, mark it available */ |
| 999 | free_qpn(&dev->qp_table, qp->ibqp.qp_num); |
| 1000 | spin_lock(&dev->n_qps_lock); |
| 1001 | dev->n_qps_allocated--; |
| 1002 | spin_unlock(&dev->n_qps_lock); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1003 | |
Ralph Campbell | 373d991 | 2006-09-22 15:22:26 -0700 | [diff] [blame] | 1004 | if (qp->ip) |
| 1005 | kref_put(&qp->ip->ref, ipath_release_mmap_info); |
| 1006 | else |
| 1007 | vfree(qp->r_rq.wq); |
Ralph Campbell | 7c37d74 | 2008-12-01 20:59:08 -0800 | [diff] [blame] | 1008 | kfree(qp->r_ud_sg_list); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1009 | vfree(qp->s_wq); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1010 | kfree(qp); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * ipath_init_qp_table - initialize the QP table for a device |
| 1016 | * @idev: the device who's QP table we're initializing |
| 1017 | * @size: the size of the QP table |
| 1018 | * |
| 1019 | * Returns 0 on success, otherwise returns an errno. |
| 1020 | */ |
| 1021 | int ipath_init_qp_table(struct ipath_ibdev *idev, int size) |
| 1022 | { |
| 1023 | int i; |
| 1024 | int ret; |
| 1025 | |
| 1026 | idev->qp_table.last = 1; /* QPN 0 and 1 are special. */ |
| 1027 | idev->qp_table.max = size; |
| 1028 | idev->qp_table.nmaps = 1; |
| 1029 | idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table), |
| 1030 | GFP_KERNEL); |
| 1031 | if (idev->qp_table.table == NULL) { |
| 1032 | ret = -ENOMEM; |
| 1033 | goto bail; |
| 1034 | } |
| 1035 | |
| 1036 | for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) { |
| 1037 | atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE); |
| 1038 | idev->qp_table.map[i].page = NULL; |
| 1039 | } |
| 1040 | |
| 1041 | ret = 0; |
| 1042 | |
| 1043 | bail: |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1048 | * ipath_get_credit - flush the send work queue of a QP |
| 1049 | * @qp: the qp who's send work queue to flush |
| 1050 | * @aeth: the Acknowledge Extended Transport Header |
| 1051 | * |
| 1052 | * The QP s_lock should be held. |
| 1053 | */ |
| 1054 | void ipath_get_credit(struct ipath_qp *qp, u32 aeth) |
| 1055 | { |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 1056 | u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1057 | |
| 1058 | /* |
| 1059 | * If the credit is invalid, we can send |
| 1060 | * as many packets as we like. Otherwise, we have to |
| 1061 | * honor the credit field. |
| 1062 | */ |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 1063 | if (credit == IPATH_AETH_CREDIT_INVAL) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1064 | qp->s_lsn = (u32) -1; |
Bryan O'Sullivan | ddd4bb2 | 2006-07-01 04:35:50 -0700 | [diff] [blame] | 1065 | else if (qp->s_lsn != (u32) -1) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1066 | /* Compute new LSN (i.e., MSN + credit) */ |
Bryan O'Sullivan | 27b678d | 2006-07-01 04:36:17 -0700 | [diff] [blame] | 1067 | credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1068 | if (ipath_cmp24(credit, qp->s_lsn) > 0) |
| 1069 | qp->s_lsn = credit; |
| 1070 | } |
| 1071 | |
| 1072 | /* Restart sending if it was blocked due to lack of credits. */ |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 1073 | if ((qp->s_flags & IPATH_S_WAIT_SSN_CREDIT) && |
| 1074 | qp->s_cur != qp->s_head && |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1075 | (qp->s_lsn == (u32) -1 || |
| 1076 | ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn, |
| 1077 | qp->s_lsn + 1) <= 0)) |
Ralph Campbell | e509be8 | 2008-05-13 11:41:29 -0700 | [diff] [blame] | 1078 | ipath_schedule_send(qp); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1079 | } |