Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 1 | /* |
Bryan O'Sullivan | 759d576 | 2006-07-01 04:35:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006 QLogic, Inc. 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> |
| 35 | #include <linux/vmalloc.h> |
| 36 | |
| 37 | #include "ipath_verbs.h" |
| 38 | #include "ips_common.h" |
| 39 | |
| 40 | #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE) |
| 41 | #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1) |
| 42 | #define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \ |
| 43 | (off)) |
| 44 | #define find_next_offset(map, off) find_next_zero_bit((map)->page, \ |
| 45 | BITS_PER_PAGE, off) |
| 46 | |
| 47 | #define TRANS_INVALID 0 |
| 48 | #define TRANS_ANY2RST 1 |
| 49 | #define TRANS_RST2INIT 2 |
| 50 | #define TRANS_INIT2INIT 3 |
| 51 | #define TRANS_INIT2RTR 4 |
| 52 | #define TRANS_RTR2RTS 5 |
| 53 | #define TRANS_RTS2RTS 6 |
| 54 | #define TRANS_SQERR2RTS 7 |
| 55 | #define TRANS_ANY2ERR 8 |
| 56 | #define TRANS_RTS2SQD 9 /* XXX Wait for expected ACKs & signal event */ |
| 57 | #define TRANS_SQD2SQD 10 /* error if not drained & parameter change */ |
| 58 | #define TRANS_SQD2RTS 11 /* error if not drained */ |
| 59 | |
| 60 | /* |
| 61 | * Convert the AETH credit code into the number of credits. |
| 62 | */ |
| 63 | static u32 credit_table[31] = { |
| 64 | 0, /* 0 */ |
| 65 | 1, /* 1 */ |
| 66 | 2, /* 2 */ |
| 67 | 3, /* 3 */ |
| 68 | 4, /* 4 */ |
| 69 | 6, /* 5 */ |
| 70 | 8, /* 6 */ |
| 71 | 12, /* 7 */ |
| 72 | 16, /* 8 */ |
| 73 | 24, /* 9 */ |
| 74 | 32, /* A */ |
| 75 | 48, /* B */ |
| 76 | 64, /* C */ |
| 77 | 96, /* D */ |
| 78 | 128, /* E */ |
| 79 | 192, /* F */ |
| 80 | 256, /* 10 */ |
| 81 | 384, /* 11 */ |
| 82 | 512, /* 12 */ |
| 83 | 768, /* 13 */ |
| 84 | 1024, /* 14 */ |
| 85 | 1536, /* 15 */ |
| 86 | 2048, /* 16 */ |
| 87 | 3072, /* 17 */ |
| 88 | 4096, /* 18 */ |
| 89 | 6144, /* 19 */ |
| 90 | 8192, /* 1A */ |
| 91 | 12288, /* 1B */ |
| 92 | 16384, /* 1C */ |
| 93 | 24576, /* 1D */ |
| 94 | 32768 /* 1E */ |
| 95 | }; |
| 96 | |
| 97 | static u32 alloc_qpn(struct ipath_qp_table *qpt) |
| 98 | { |
| 99 | u32 i, offset, max_scan, qpn; |
| 100 | struct qpn_map *map; |
| 101 | u32 ret; |
| 102 | |
| 103 | qpn = qpt->last + 1; |
| 104 | if (qpn >= QPN_MAX) |
| 105 | qpn = 2; |
| 106 | offset = qpn & BITS_PER_PAGE_MASK; |
| 107 | map = &qpt->map[qpn / BITS_PER_PAGE]; |
| 108 | max_scan = qpt->nmaps - !offset; |
| 109 | for (i = 0;;) { |
| 110 | if (unlikely(!map->page)) { |
| 111 | unsigned long page = get_zeroed_page(GFP_KERNEL); |
| 112 | unsigned long flags; |
| 113 | |
| 114 | /* |
| 115 | * Free the page if someone raced with us |
| 116 | * installing it: |
| 117 | */ |
| 118 | spin_lock_irqsave(&qpt->lock, flags); |
| 119 | if (map->page) |
| 120 | free_page(page); |
| 121 | else |
| 122 | map->page = (void *)page; |
| 123 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 124 | if (unlikely(!map->page)) |
| 125 | break; |
| 126 | } |
| 127 | if (likely(atomic_read(&map->n_free))) { |
| 128 | do { |
| 129 | if (!test_and_set_bit(offset, map->page)) { |
| 130 | atomic_dec(&map->n_free); |
| 131 | qpt->last = qpn; |
| 132 | ret = qpn; |
| 133 | goto bail; |
| 134 | } |
| 135 | offset = find_next_offset(map, offset); |
| 136 | qpn = mk_qpn(qpt, map, offset); |
| 137 | /* |
| 138 | * This test differs from alloc_pidmap(). |
| 139 | * If find_next_offset() does find a zero |
| 140 | * bit, we don't need to check for QPN |
| 141 | * wrapping around past our starting QPN. |
| 142 | * We just need to be sure we don't loop |
| 143 | * forever. |
| 144 | */ |
| 145 | } while (offset < BITS_PER_PAGE && qpn < QPN_MAX); |
| 146 | } |
| 147 | /* |
| 148 | * In order to keep the number of pages allocated to a |
| 149 | * minimum, we scan the all existing pages before increasing |
| 150 | * the size of the bitmap table. |
| 151 | */ |
| 152 | if (++i > max_scan) { |
| 153 | if (qpt->nmaps == QPNMAP_ENTRIES) |
| 154 | break; |
| 155 | map = &qpt->map[qpt->nmaps++]; |
| 156 | offset = 0; |
| 157 | } else if (map < &qpt->map[qpt->nmaps]) { |
| 158 | ++map; |
| 159 | offset = 0; |
| 160 | } else { |
| 161 | map = &qpt->map[0]; |
| 162 | offset = 2; |
| 163 | } |
| 164 | qpn = mk_qpn(qpt, map, offset); |
| 165 | } |
| 166 | |
| 167 | ret = 0; |
| 168 | |
| 169 | bail: |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static void free_qpn(struct ipath_qp_table *qpt, u32 qpn) |
| 174 | { |
| 175 | struct qpn_map *map; |
| 176 | |
| 177 | map = qpt->map + qpn / BITS_PER_PAGE; |
| 178 | if (map->page) |
| 179 | clear_bit(qpn & BITS_PER_PAGE_MASK, map->page); |
| 180 | atomic_inc(&map->n_free); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * ipath_alloc_qpn - allocate a QP number |
| 185 | * @qpt: the QP table |
| 186 | * @qp: the QP |
| 187 | * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special) |
| 188 | * |
| 189 | * Allocate the next available QPN and put the QP into the hash table. |
| 190 | * The hash table holds a reference to the QP. |
| 191 | */ |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 192 | static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp, |
| 193 | enum ib_qp_type type) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 194 | { |
| 195 | unsigned long flags; |
| 196 | u32 qpn; |
| 197 | int ret; |
| 198 | |
| 199 | if (type == IB_QPT_SMI) |
| 200 | qpn = 0; |
| 201 | else if (type == IB_QPT_GSI) |
| 202 | qpn = 1; |
| 203 | else { |
| 204 | /* Allocate the next available QPN */ |
| 205 | qpn = alloc_qpn(qpt); |
| 206 | if (qpn == 0) { |
| 207 | ret = -ENOMEM; |
| 208 | goto bail; |
| 209 | } |
| 210 | } |
| 211 | qp->ibqp.qp_num = qpn; |
| 212 | |
| 213 | /* Add the QP to the hash table. */ |
| 214 | spin_lock_irqsave(&qpt->lock, flags); |
| 215 | |
| 216 | qpn %= qpt->max; |
| 217 | qp->next = qpt->table[qpn]; |
| 218 | qpt->table[qpn] = qp; |
| 219 | atomic_inc(&qp->refcount); |
| 220 | |
| 221 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 222 | ret = 0; |
| 223 | |
| 224 | bail: |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * ipath_free_qp - remove a QP from the QP table |
| 230 | * @qpt: the QP table |
| 231 | * @qp: the QP to remove |
| 232 | * |
| 233 | * Remove the QP from the table so it can't be found asynchronously by |
| 234 | * the receive interrupt routine. |
| 235 | */ |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 236 | 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] | 237 | { |
| 238 | struct ipath_qp *q, **qpp; |
| 239 | unsigned long flags; |
| 240 | int fnd = 0; |
| 241 | |
| 242 | spin_lock_irqsave(&qpt->lock, flags); |
| 243 | |
| 244 | /* Remove QP from the hash table. */ |
| 245 | qpp = &qpt->table[qp->ibqp.qp_num % qpt->max]; |
| 246 | for (; (q = *qpp) != NULL; qpp = &q->next) { |
| 247 | if (q == qp) { |
| 248 | *qpp = qp->next; |
| 249 | qp->next = NULL; |
| 250 | atomic_dec(&qp->refcount); |
| 251 | fnd = 1; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 257 | |
| 258 | if (!fnd) |
| 259 | return; |
| 260 | |
| 261 | /* If QPN is not reserved, mark QPN free in the bitmap. */ |
| 262 | if (qp->ibqp.qp_num > 1) |
| 263 | free_qpn(qpt, qp->ibqp.qp_num); |
| 264 | |
| 265 | wait_event(qp->wait, !atomic_read(&qp->refcount)); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * ipath_free_all_qps - remove all QPs from the table |
| 270 | * @qpt: the QP table to empty |
| 271 | */ |
| 272 | void ipath_free_all_qps(struct ipath_qp_table *qpt) |
| 273 | { |
| 274 | unsigned long flags; |
| 275 | struct ipath_qp *qp, *nqp; |
| 276 | u32 n; |
| 277 | |
| 278 | for (n = 0; n < qpt->max; n++) { |
| 279 | spin_lock_irqsave(&qpt->lock, flags); |
| 280 | qp = qpt->table[n]; |
| 281 | qpt->table[n] = NULL; |
| 282 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 283 | |
| 284 | while (qp) { |
| 285 | nqp = qp->next; |
| 286 | if (qp->ibqp.qp_num > 1) |
| 287 | free_qpn(qpt, qp->ibqp.qp_num); |
| 288 | if (!atomic_dec_and_test(&qp->refcount) || |
| 289 | !ipath_destroy_qp(&qp->ibqp)) |
| 290 | _VERBS_INFO("QP memory leak!\n"); |
| 291 | qp = nqp; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | for (n = 0; n < ARRAY_SIZE(qpt->map); n++) { |
| 296 | if (qpt->map[n].page) |
| 297 | free_page((unsigned long)qpt->map[n].page); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * ipath_lookup_qpn - return the QP with the given QPN |
| 303 | * @qpt: the QP table |
| 304 | * @qpn: the QP number to look up |
| 305 | * |
| 306 | * The caller is responsible for decrementing the QP reference count |
| 307 | * when done. |
| 308 | */ |
| 309 | struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn) |
| 310 | { |
| 311 | unsigned long flags; |
| 312 | struct ipath_qp *qp; |
| 313 | |
| 314 | spin_lock_irqsave(&qpt->lock, flags); |
| 315 | |
| 316 | for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) { |
| 317 | if (qp->ibqp.qp_num == qpn) { |
| 318 | atomic_inc(&qp->refcount); |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | spin_unlock_irqrestore(&qpt->lock, flags); |
| 324 | return qp; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * ipath_reset_qp - initialize the QP state to the reset state |
| 329 | * @qp: the QP to reset |
| 330 | */ |
| 331 | static void ipath_reset_qp(struct ipath_qp *qp) |
| 332 | { |
| 333 | qp->remote_qpn = 0; |
| 334 | qp->qkey = 0; |
| 335 | qp->qp_access_flags = 0; |
| 336 | qp->s_hdrwords = 0; |
| 337 | qp->s_psn = 0; |
| 338 | qp->r_psn = 0; |
| 339 | atomic_set(&qp->msn, 0); |
| 340 | if (qp->ibqp.qp_type == IB_QPT_RC) { |
| 341 | qp->s_state = IB_OPCODE_RC_SEND_LAST; |
| 342 | qp->r_state = IB_OPCODE_RC_SEND_LAST; |
| 343 | } else { |
| 344 | qp->s_state = IB_OPCODE_UC_SEND_LAST; |
| 345 | qp->r_state = IB_OPCODE_UC_SEND_LAST; |
| 346 | } |
| 347 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; |
| 348 | qp->s_nak_state = 0; |
| 349 | qp->s_rnr_timeout = 0; |
| 350 | qp->s_head = 0; |
| 351 | qp->s_tail = 0; |
| 352 | qp->s_cur = 0; |
| 353 | qp->s_last = 0; |
| 354 | qp->s_ssn = 1; |
| 355 | qp->s_lsn = 0; |
| 356 | qp->r_rq.head = 0; |
| 357 | qp->r_rq.tail = 0; |
| 358 | qp->r_reuse_sge = 0; |
| 359 | } |
| 360 | |
| 361 | /** |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 362 | * ipath_error_qp - put a QP into an error state |
| 363 | * @qp: the QP to put into an error state |
| 364 | * |
| 365 | * Flushes both send and receive work queues. |
| 366 | * QP r_rq.lock and s_lock should be held. |
| 367 | */ |
| 368 | |
| 369 | static void ipath_error_qp(struct ipath_qp *qp) |
| 370 | { |
| 371 | struct ipath_ibdev *dev = to_idev(qp->ibqp.device); |
| 372 | struct ib_wc wc; |
| 373 | |
| 374 | _VERBS_INFO("QP%d/%d in error state\n", |
| 375 | qp->ibqp.qp_num, qp->remote_qpn); |
| 376 | |
| 377 | spin_lock(&dev->pending_lock); |
| 378 | /* XXX What if its already removed by the timeout code? */ |
Bryan O'Sullivan | 94b8d9f | 2006-05-23 11:32:32 -0700 | [diff] [blame] | 379 | if (!list_empty(&qp->timerwait)) |
| 380 | list_del_init(&qp->timerwait); |
| 381 | if (!list_empty(&qp->piowait)) |
| 382 | list_del_init(&qp->piowait); |
Roland Dreier | ac2ae4c | 2006-04-19 11:40:12 -0700 | [diff] [blame] | 383 | spin_unlock(&dev->pending_lock); |
| 384 | |
| 385 | wc.status = IB_WC_WR_FLUSH_ERR; |
| 386 | wc.vendor_err = 0; |
| 387 | wc.byte_len = 0; |
| 388 | wc.imm_data = 0; |
| 389 | wc.qp_num = qp->ibqp.qp_num; |
| 390 | wc.src_qp = 0; |
| 391 | wc.wc_flags = 0; |
| 392 | wc.pkey_index = 0; |
| 393 | wc.slid = 0; |
| 394 | wc.sl = 0; |
| 395 | wc.dlid_path_bits = 0; |
| 396 | wc.port_num = 0; |
| 397 | |
| 398 | while (qp->s_last != qp->s_head) { |
| 399 | struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last); |
| 400 | |
| 401 | wc.wr_id = wqe->wr.wr_id; |
| 402 | wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode]; |
| 403 | if (++qp->s_last >= qp->s_size) |
| 404 | qp->s_last = 0; |
| 405 | ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1); |
| 406 | } |
| 407 | qp->s_cur = qp->s_tail = qp->s_head; |
| 408 | qp->s_hdrwords = 0; |
| 409 | qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; |
| 410 | |
| 411 | wc.opcode = IB_WC_RECV; |
| 412 | while (qp->r_rq.tail != qp->r_rq.head) { |
| 413 | wc.wr_id = get_rwqe_ptr(&qp->r_rq, qp->r_rq.tail)->wr_id; |
| 414 | if (++qp->r_rq.tail >= qp->r_rq.size) |
| 415 | qp->r_rq.tail = 0; |
| 416 | ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 421 | * ipath_modify_qp - modify the attributes of a queue pair |
| 422 | * @ibqp: the queue pair who's attributes we're modifying |
| 423 | * @attr: the new attributes |
| 424 | * @attr_mask: the mask of attributes to modify |
| 425 | * |
| 426 | * Returns 0 on success, otherwise returns an errno. |
| 427 | */ |
| 428 | int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 429 | int attr_mask) |
| 430 | { |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 431 | struct ipath_ibdev *dev = to_idev(ibqp->device); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 432 | struct ipath_qp *qp = to_iqp(ibqp); |
| 433 | enum ib_qp_state cur_state, new_state; |
| 434 | unsigned long flags; |
| 435 | int ret; |
| 436 | |
| 437 | spin_lock_irqsave(&qp->r_rq.lock, flags); |
| 438 | spin_lock(&qp->s_lock); |
| 439 | |
| 440 | cur_state = attr_mask & IB_QP_CUR_STATE ? |
| 441 | attr->cur_qp_state : qp->state; |
| 442 | new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; |
| 443 | |
| 444 | if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, |
| 445 | attr_mask)) |
| 446 | goto inval; |
| 447 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 448 | if (attr_mask & IB_QP_AV) |
| 449 | if (attr->ah_attr.dlid == 0 || |
| 450 | attr->ah_attr.dlid >= IPS_MULTICAST_LID_BASE) |
| 451 | goto inval; |
| 452 | |
| 453 | if (attr_mask & IB_QP_PKEY_INDEX) |
| 454 | if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd)) |
| 455 | goto inval; |
| 456 | |
| 457 | if (attr_mask & IB_QP_MIN_RNR_TIMER) |
| 458 | if (attr->min_rnr_timer > 31) |
| 459 | goto inval; |
| 460 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 461 | switch (new_state) { |
| 462 | case IB_QPS_RESET: |
| 463 | ipath_reset_qp(qp); |
| 464 | break; |
| 465 | |
| 466 | case IB_QPS_ERR: |
| 467 | ipath_error_qp(qp); |
| 468 | break; |
| 469 | |
| 470 | default: |
| 471 | break; |
| 472 | |
| 473 | } |
| 474 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 475 | if (attr_mask & IB_QP_PKEY_INDEX) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 476 | qp->s_pkey_index = attr->pkey_index; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 477 | |
| 478 | if (attr_mask & IB_QP_DEST_QPN) |
| 479 | qp->remote_qpn = attr->dest_qp_num; |
| 480 | |
| 481 | if (attr_mask & IB_QP_SQ_PSN) { |
| 482 | qp->s_next_psn = attr->sq_psn; |
| 483 | qp->s_last_psn = qp->s_next_psn - 1; |
| 484 | } |
| 485 | |
| 486 | if (attr_mask & IB_QP_RQ_PSN) |
| 487 | qp->r_psn = attr->rq_psn; |
| 488 | |
| 489 | if (attr_mask & IB_QP_ACCESS_FLAGS) |
| 490 | qp->qp_access_flags = attr->qp_access_flags; |
| 491 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 492 | if (attr_mask & IB_QP_AV) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 493 | qp->remote_ah_attr = attr->ah_attr; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 494 | |
| 495 | if (attr_mask & IB_QP_PATH_MTU) |
| 496 | qp->path_mtu = attr->path_mtu; |
| 497 | |
| 498 | if (attr_mask & IB_QP_RETRY_CNT) |
| 499 | qp->s_retry = qp->s_retry_cnt = attr->retry_cnt; |
| 500 | |
| 501 | if (attr_mask & IB_QP_RNR_RETRY) { |
| 502 | qp->s_rnr_retry = attr->rnr_retry; |
| 503 | if (qp->s_rnr_retry > 7) |
| 504 | qp->s_rnr_retry = 7; |
| 505 | qp->s_rnr_retry_cnt = qp->s_rnr_retry; |
| 506 | } |
| 507 | |
Bryan O'Sullivan | b228b43 | 2006-05-23 11:32:30 -0700 | [diff] [blame] | 508 | if (attr_mask & IB_QP_MIN_RNR_TIMER) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 509 | qp->s_min_rnr_timer = attr->min_rnr_timer; |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 510 | |
| 511 | if (attr_mask & IB_QP_QKEY) |
| 512 | qp->qkey = attr->qkey; |
| 513 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 514 | qp->state = new_state; |
| 515 | spin_unlock(&qp->s_lock); |
| 516 | spin_unlock_irqrestore(&qp->r_rq.lock, flags); |
| 517 | |
| 518 | /* |
| 519 | * If QP1 changed to the RTS state, try to move to the link to INIT |
| 520 | * even if it was ACTIVE so the SM will reinitialize the SMA's |
| 521 | * state. |
| 522 | */ |
| 523 | if (qp->ibqp.qp_num == 1 && new_state == IB_QPS_RTS) { |
| 524 | struct ipath_ibdev *dev = to_idev(ibqp->device); |
| 525 | |
| 526 | ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKDOWN); |
| 527 | } |
| 528 | ret = 0; |
| 529 | goto bail; |
| 530 | |
| 531 | inval: |
| 532 | spin_unlock(&qp->s_lock); |
| 533 | spin_unlock_irqrestore(&qp->r_rq.lock, flags); |
| 534 | ret = -EINVAL; |
| 535 | |
| 536 | bail: |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 541 | int attr_mask, struct ib_qp_init_attr *init_attr) |
| 542 | { |
| 543 | struct ipath_qp *qp = to_iqp(ibqp); |
| 544 | |
| 545 | attr->qp_state = qp->state; |
| 546 | attr->cur_qp_state = attr->qp_state; |
| 547 | attr->path_mtu = qp->path_mtu; |
| 548 | attr->path_mig_state = 0; |
| 549 | attr->qkey = qp->qkey; |
| 550 | attr->rq_psn = qp->r_psn; |
| 551 | attr->sq_psn = qp->s_next_psn; |
| 552 | attr->dest_qp_num = qp->remote_qpn; |
| 553 | attr->qp_access_flags = qp->qp_access_flags; |
| 554 | attr->cap.max_send_wr = qp->s_size - 1; |
| 555 | attr->cap.max_recv_wr = qp->r_rq.size - 1; |
| 556 | attr->cap.max_send_sge = qp->s_max_sge; |
| 557 | attr->cap.max_recv_sge = qp->r_rq.max_sge; |
| 558 | attr->cap.max_inline_data = 0; |
| 559 | attr->ah_attr = qp->remote_ah_attr; |
| 560 | memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr)); |
| 561 | attr->pkey_index = qp->s_pkey_index; |
| 562 | attr->alt_pkey_index = 0; |
| 563 | attr->en_sqd_async_notify = 0; |
| 564 | attr->sq_draining = 0; |
| 565 | attr->max_rd_atomic = 1; |
| 566 | attr->max_dest_rd_atomic = 1; |
| 567 | attr->min_rnr_timer = qp->s_min_rnr_timer; |
| 568 | attr->port_num = 1; |
| 569 | attr->timeout = 0; |
| 570 | attr->retry_cnt = qp->s_retry_cnt; |
| 571 | attr->rnr_retry = qp->s_rnr_retry; |
| 572 | attr->alt_port_num = 0; |
| 573 | attr->alt_timeout = 0; |
| 574 | |
| 575 | init_attr->event_handler = qp->ibqp.event_handler; |
| 576 | init_attr->qp_context = qp->ibqp.qp_context; |
| 577 | init_attr->send_cq = qp->ibqp.send_cq; |
| 578 | init_attr->recv_cq = qp->ibqp.recv_cq; |
| 579 | init_attr->srq = qp->ibqp.srq; |
| 580 | init_attr->cap = attr->cap; |
| 581 | init_attr->sq_sig_type = |
| 582 | (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR)) |
| 583 | ? IB_SIGNAL_REQ_WR : 0; |
| 584 | init_attr->qp_type = qp->ibqp.qp_type; |
| 585 | init_attr->port_num = 1; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * ipath_compute_aeth - compute the AETH (syndrome + MSN) |
| 591 | * @qp: the queue pair to compute the AETH for |
| 592 | * |
| 593 | * Returns the AETH. |
| 594 | * |
| 595 | * The QP s_lock should be held. |
| 596 | */ |
| 597 | __be32 ipath_compute_aeth(struct ipath_qp *qp) |
| 598 | { |
| 599 | u32 aeth = atomic_read(&qp->msn) & IPS_MSN_MASK; |
| 600 | |
| 601 | if (qp->s_nak_state) { |
| 602 | aeth |= qp->s_nak_state << IPS_AETH_CREDIT_SHIFT; |
| 603 | } else if (qp->ibqp.srq) { |
| 604 | /* |
| 605 | * Shared receive queues don't generate credits. |
| 606 | * Set the credit field to the invalid value. |
| 607 | */ |
| 608 | aeth |= IPS_AETH_CREDIT_INVAL << IPS_AETH_CREDIT_SHIFT; |
| 609 | } else { |
| 610 | u32 min, max, x; |
| 611 | u32 credits; |
| 612 | |
| 613 | /* |
| 614 | * Compute the number of credits available (RWQEs). |
| 615 | * XXX Not holding the r_rq.lock here so there is a small |
| 616 | * chance that the pair of reads are not atomic. |
| 617 | */ |
| 618 | credits = qp->r_rq.head - qp->r_rq.tail; |
| 619 | if ((int)credits < 0) |
| 620 | credits += qp->r_rq.size; |
| 621 | /* |
| 622 | * Binary search the credit table to find the code to |
| 623 | * use. |
| 624 | */ |
| 625 | min = 0; |
| 626 | max = 31; |
| 627 | for (;;) { |
| 628 | x = (min + max) / 2; |
| 629 | if (credit_table[x] == credits) |
| 630 | break; |
| 631 | if (credit_table[x] > credits) |
| 632 | max = x; |
| 633 | else if (min == x) |
| 634 | break; |
| 635 | else |
| 636 | min = x; |
| 637 | } |
| 638 | aeth |= x << IPS_AETH_CREDIT_SHIFT; |
| 639 | } |
| 640 | return cpu_to_be32(aeth); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * ipath_create_qp - create a queue pair for a device |
| 645 | * @ibpd: the protection domain who's device we create the queue pair for |
| 646 | * @init_attr: the attributes of the queue pair |
| 647 | * @udata: unused by InfiniPath |
| 648 | * |
| 649 | * Returns the queue pair on success, otherwise returns an errno. |
| 650 | * |
| 651 | * Called by the ib_create_qp() core verbs function. |
| 652 | */ |
| 653 | struct ib_qp *ipath_create_qp(struct ib_pd *ibpd, |
| 654 | struct ib_qp_init_attr *init_attr, |
| 655 | struct ib_udata *udata) |
| 656 | { |
| 657 | struct ipath_qp *qp; |
| 658 | int err; |
| 659 | struct ipath_swqe *swq = NULL; |
| 660 | struct ipath_ibdev *dev; |
| 661 | size_t sz; |
| 662 | struct ib_qp *ret; |
| 663 | |
| 664 | if (init_attr->cap.max_send_sge > 255 || |
| 665 | init_attr->cap.max_recv_sge > 255) { |
| 666 | ret = ERR_PTR(-ENOMEM); |
| 667 | goto bail; |
| 668 | } |
| 669 | |
Bryan O'Sullivan | 4a45b7d | 2006-07-01 04:35:55 -0700 | [diff] [blame] | 670 | if (init_attr->cap.max_send_sge + |
| 671 | init_attr->cap.max_recv_sge + |
| 672 | init_attr->cap.max_send_wr + |
| 673 | init_attr->cap.max_recv_wr == 0) { |
| 674 | ret = ERR_PTR(-EINVAL); |
| 675 | goto bail; |
| 676 | } |
| 677 | |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 678 | switch (init_attr->qp_type) { |
| 679 | case IB_QPT_UC: |
| 680 | case IB_QPT_RC: |
| 681 | sz = sizeof(struct ipath_sge) * |
| 682 | init_attr->cap.max_send_sge + |
| 683 | sizeof(struct ipath_swqe); |
| 684 | swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz); |
| 685 | if (swq == NULL) { |
| 686 | ret = ERR_PTR(-ENOMEM); |
| 687 | goto bail; |
| 688 | } |
| 689 | /* FALLTHROUGH */ |
| 690 | case IB_QPT_UD: |
| 691 | case IB_QPT_SMI: |
| 692 | case IB_QPT_GSI: |
| 693 | qp = kmalloc(sizeof(*qp), GFP_KERNEL); |
| 694 | if (!qp) { |
Bryan O'Sullivan | 60460df | 2006-07-01 04:35:55 -0700 | [diff] [blame^] | 695 | vfree(swq); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 696 | ret = ERR_PTR(-ENOMEM); |
| 697 | goto bail; |
| 698 | } |
| 699 | qp->r_rq.size = init_attr->cap.max_recv_wr + 1; |
| 700 | sz = sizeof(struct ipath_sge) * |
| 701 | init_attr->cap.max_recv_sge + |
| 702 | sizeof(struct ipath_rwqe); |
| 703 | qp->r_rq.wq = vmalloc(qp->r_rq.size * sz); |
| 704 | if (!qp->r_rq.wq) { |
| 705 | kfree(qp); |
Bryan O'Sullivan | 60460df | 2006-07-01 04:35:55 -0700 | [diff] [blame^] | 706 | vfree(swq); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 707 | ret = ERR_PTR(-ENOMEM); |
| 708 | goto bail; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * ib_create_qp() will initialize qp->ibqp |
| 713 | * except for qp->ibqp.qp_num. |
| 714 | */ |
| 715 | spin_lock_init(&qp->s_lock); |
| 716 | spin_lock_init(&qp->r_rq.lock); |
| 717 | atomic_set(&qp->refcount, 0); |
| 718 | init_waitqueue_head(&qp->wait); |
Bryan O'Sullivan | ddd4bb2 | 2006-07-01 04:35:50 -0700 | [diff] [blame] | 719 | tasklet_init(&qp->s_task, ipath_do_ruc_send, |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 720 | (unsigned long)qp); |
Bryan O'Sullivan | 94b8d9f | 2006-05-23 11:32:32 -0700 | [diff] [blame] | 721 | INIT_LIST_HEAD(&qp->piowait); |
| 722 | INIT_LIST_HEAD(&qp->timerwait); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 723 | qp->state = IB_QPS_RESET; |
| 724 | qp->s_wq = swq; |
| 725 | qp->s_size = init_attr->cap.max_send_wr + 1; |
| 726 | qp->s_max_sge = init_attr->cap.max_send_sge; |
| 727 | qp->r_rq.max_sge = init_attr->cap.max_recv_sge; |
| 728 | qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ? |
| 729 | 1 << IPATH_S_SIGNAL_REQ_WR : 0; |
| 730 | dev = to_idev(ibpd->device); |
| 731 | err = ipath_alloc_qpn(&dev->qp_table, qp, |
| 732 | init_attr->qp_type); |
| 733 | if (err) { |
| 734 | vfree(swq); |
| 735 | vfree(qp->r_rq.wq); |
| 736 | kfree(qp); |
| 737 | ret = ERR_PTR(err); |
| 738 | goto bail; |
| 739 | } |
| 740 | ipath_reset_qp(qp); |
| 741 | |
| 742 | /* Tell the core driver that the kernel SMA is present. */ |
Bryan O'Sullivan | 09b74de | 2006-05-23 11:32:38 -0700 | [diff] [blame] | 743 | if (init_attr->qp_type == IB_QPT_SMI) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 744 | ipath_layer_set_verbs_flags(dev->dd, |
| 745 | IPATH_VERBS_KERNEL_SMA); |
| 746 | break; |
| 747 | |
| 748 | default: |
| 749 | /* Don't support raw QPs */ |
| 750 | ret = ERR_PTR(-ENOSYS); |
| 751 | goto bail; |
| 752 | } |
| 753 | |
| 754 | init_attr->cap.max_inline_data = 0; |
| 755 | |
| 756 | ret = &qp->ibqp; |
| 757 | |
| 758 | bail: |
| 759 | return ret; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * ipath_destroy_qp - destroy a queue pair |
| 764 | * @ibqp: the queue pair to destroy |
| 765 | * |
| 766 | * Returns 0 on success. |
| 767 | * |
| 768 | * Note that this can be called while the QP is actively sending or |
| 769 | * receiving! |
| 770 | */ |
| 771 | int ipath_destroy_qp(struct ib_qp *ibqp) |
| 772 | { |
| 773 | struct ipath_qp *qp = to_iqp(ibqp); |
| 774 | struct ipath_ibdev *dev = to_idev(ibqp->device); |
| 775 | unsigned long flags; |
| 776 | |
| 777 | /* Tell the core driver that the kernel SMA is gone. */ |
| 778 | if (qp->ibqp.qp_type == IB_QPT_SMI) |
| 779 | ipath_layer_set_verbs_flags(dev->dd, 0); |
| 780 | |
| 781 | spin_lock_irqsave(&qp->r_rq.lock, flags); |
| 782 | spin_lock(&qp->s_lock); |
| 783 | qp->state = IB_QPS_ERR; |
| 784 | spin_unlock(&qp->s_lock); |
| 785 | spin_unlock_irqrestore(&qp->r_rq.lock, flags); |
| 786 | |
| 787 | /* Stop the sending tasklet. */ |
| 788 | tasklet_kill(&qp->s_task); |
| 789 | |
| 790 | /* Make sure the QP isn't on the timeout list. */ |
| 791 | spin_lock_irqsave(&dev->pending_lock, flags); |
Bryan O'Sullivan | 94b8d9f | 2006-05-23 11:32:32 -0700 | [diff] [blame] | 792 | if (!list_empty(&qp->timerwait)) |
| 793 | list_del_init(&qp->timerwait); |
| 794 | if (!list_empty(&qp->piowait)) |
| 795 | list_del_init(&qp->piowait); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 796 | spin_unlock_irqrestore(&dev->pending_lock, flags); |
| 797 | |
| 798 | /* |
| 799 | * Make sure that the QP is not in the QPN table so receive |
| 800 | * interrupts will discard packets for this QP. XXX Also remove QP |
| 801 | * from multicast table. |
| 802 | */ |
| 803 | if (atomic_read(&qp->refcount) != 0) |
| 804 | ipath_free_qp(&dev->qp_table, qp); |
| 805 | |
| 806 | vfree(qp->s_wq); |
| 807 | vfree(qp->r_rq.wq); |
| 808 | kfree(qp); |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * ipath_init_qp_table - initialize the QP table for a device |
| 814 | * @idev: the device who's QP table we're initializing |
| 815 | * @size: the size of the QP table |
| 816 | * |
| 817 | * Returns 0 on success, otherwise returns an errno. |
| 818 | */ |
| 819 | int ipath_init_qp_table(struct ipath_ibdev *idev, int size) |
| 820 | { |
| 821 | int i; |
| 822 | int ret; |
| 823 | |
| 824 | idev->qp_table.last = 1; /* QPN 0 and 1 are special. */ |
| 825 | idev->qp_table.max = size; |
| 826 | idev->qp_table.nmaps = 1; |
| 827 | idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table), |
| 828 | GFP_KERNEL); |
| 829 | if (idev->qp_table.table == NULL) { |
| 830 | ret = -ENOMEM; |
| 831 | goto bail; |
| 832 | } |
| 833 | |
| 834 | for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) { |
| 835 | atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE); |
| 836 | idev->qp_table.map[i].page = NULL; |
| 837 | } |
| 838 | |
| 839 | ret = 0; |
| 840 | |
| 841 | bail: |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * ipath_sqerror_qp - put a QP's send queue into an error state |
| 847 | * @qp: QP who's send queue will be put into an error state |
| 848 | * @wc: the WC responsible for putting the QP in this state |
| 849 | * |
| 850 | * Flushes the send work queue. |
| 851 | * The QP s_lock should be held. |
| 852 | */ |
| 853 | |
| 854 | void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc) |
| 855 | { |
| 856 | struct ipath_ibdev *dev = to_idev(qp->ibqp.device); |
| 857 | struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last); |
| 858 | |
| 859 | _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n", |
| 860 | qp->ibqp.qp_num, qp->remote_qpn, wc->status); |
| 861 | |
| 862 | spin_lock(&dev->pending_lock); |
| 863 | /* XXX What if its already removed by the timeout code? */ |
Bryan O'Sullivan | 94b8d9f | 2006-05-23 11:32:32 -0700 | [diff] [blame] | 864 | if (!list_empty(&qp->timerwait)) |
| 865 | list_del_init(&qp->timerwait); |
| 866 | if (!list_empty(&qp->piowait)) |
| 867 | list_del_init(&qp->piowait); |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 868 | spin_unlock(&dev->pending_lock); |
| 869 | |
| 870 | ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1); |
| 871 | if (++qp->s_last >= qp->s_size) |
| 872 | qp->s_last = 0; |
| 873 | |
| 874 | wc->status = IB_WC_WR_FLUSH_ERR; |
| 875 | |
| 876 | while (qp->s_last != qp->s_head) { |
| 877 | wc->wr_id = wqe->wr.wr_id; |
| 878 | wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode]; |
| 879 | ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1); |
| 880 | if (++qp->s_last >= qp->s_size) |
| 881 | qp->s_last = 0; |
| 882 | wqe = get_swqe_ptr(qp, qp->s_last); |
| 883 | } |
| 884 | qp->s_cur = qp->s_tail = qp->s_head; |
| 885 | qp->state = IB_QPS_SQE; |
| 886 | } |
| 887 | |
| 888 | /** |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 889 | * ipath_get_credit - flush the send work queue of a QP |
| 890 | * @qp: the qp who's send work queue to flush |
| 891 | * @aeth: the Acknowledge Extended Transport Header |
| 892 | * |
| 893 | * The QP s_lock should be held. |
| 894 | */ |
| 895 | void ipath_get_credit(struct ipath_qp *qp, u32 aeth) |
| 896 | { |
| 897 | u32 credit = (aeth >> IPS_AETH_CREDIT_SHIFT) & IPS_AETH_CREDIT_MASK; |
| 898 | |
| 899 | /* |
| 900 | * If the credit is invalid, we can send |
| 901 | * as many packets as we like. Otherwise, we have to |
| 902 | * honor the credit field. |
| 903 | */ |
Bryan O'Sullivan | ddd4bb2 | 2006-07-01 04:35:50 -0700 | [diff] [blame] | 904 | if (credit == IPS_AETH_CREDIT_INVAL) |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 905 | qp->s_lsn = (u32) -1; |
Bryan O'Sullivan | ddd4bb2 | 2006-07-01 04:35:50 -0700 | [diff] [blame] | 906 | else if (qp->s_lsn != (u32) -1) { |
Bryan O'Sullivan | e28c00a | 2006-03-29 15:23:37 -0800 | [diff] [blame] | 907 | /* Compute new LSN (i.e., MSN + credit) */ |
| 908 | credit = (aeth + credit_table[credit]) & IPS_MSN_MASK; |
| 909 | if (ipath_cmp24(credit, qp->s_lsn) > 0) |
| 910 | qp->s_lsn = credit; |
| 911 | } |
| 912 | |
| 913 | /* Restart sending if it was blocked due to lack of credits. */ |
| 914 | if (qp->s_cur != qp->s_head && |
| 915 | (qp->s_lsn == (u32) -1 || |
| 916 | ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn, |
| 917 | qp->s_lsn + 1) <= 0)) |
| 918 | tasklet_hi_schedule(&qp->s_task); |
| 919 | } |