ipath_qp.c
来自「linux 内核源代码」· C语言 代码 · 共 1,082 行 · 第 1/2 页
C
1,082 行
/* * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */#include <linux/err.h>#include <linux/vmalloc.h>#include "ipath_verbs.h"#include "ipath_kernel.h"#define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)#define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)#define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \ (off))#define find_next_offset(map, off) find_next_zero_bit((map)->page, \ BITS_PER_PAGE, off)/* * Convert the AETH credit code into the number of credits. */static u32 credit_table[31] = { 0, /* 0 */ 1, /* 1 */ 2, /* 2 */ 3, /* 3 */ 4, /* 4 */ 6, /* 5 */ 8, /* 6 */ 12, /* 7 */ 16, /* 8 */ 24, /* 9 */ 32, /* A */ 48, /* B */ 64, /* C */ 96, /* D */ 128, /* E */ 192, /* F */ 256, /* 10 */ 384, /* 11 */ 512, /* 12 */ 768, /* 13 */ 1024, /* 14 */ 1536, /* 15 */ 2048, /* 16 */ 3072, /* 17 */ 4096, /* 18 */ 6144, /* 19 */ 8192, /* 1A */ 12288, /* 1B */ 16384, /* 1C */ 24576, /* 1D */ 32768 /* 1E */};static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map){ unsigned long page = get_zeroed_page(GFP_KERNEL); unsigned long flags; /* * Free the page if someone raced with us installing it. */ spin_lock_irqsave(&qpt->lock, flags); if (map->page) free_page(page); else map->page = (void *)page; spin_unlock_irqrestore(&qpt->lock, flags);}static int alloc_qpn(struct ipath_qp_table *qpt, enum ib_qp_type type){ u32 i, offset, max_scan, qpn; struct qpn_map *map; u32 ret = -1; if (type == IB_QPT_SMI) ret = 0; else if (type == IB_QPT_GSI) ret = 1; if (ret != -1) { map = &qpt->map[0]; if (unlikely(!map->page)) { get_map_page(qpt, map); if (unlikely(!map->page)) { ret = -ENOMEM; goto bail; } } if (!test_and_set_bit(ret, map->page)) atomic_dec(&map->n_free); else ret = -EBUSY; goto bail; } qpn = qpt->last + 1; if (qpn >= QPN_MAX) qpn = 2; offset = qpn & BITS_PER_PAGE_MASK; map = &qpt->map[qpn / BITS_PER_PAGE]; max_scan = qpt->nmaps - !offset; for (i = 0;;) { if (unlikely(!map->page)) { get_map_page(qpt, map); if (unlikely(!map->page)) break; } if (likely(atomic_read(&map->n_free))) { do { if (!test_and_set_bit(offset, map->page)) { atomic_dec(&map->n_free); qpt->last = qpn; ret = qpn; goto bail; } offset = find_next_offset(map, offset); qpn = mk_qpn(qpt, map, offset); /* * This test differs from alloc_pidmap(). * If find_next_offset() does find a zero * bit, we don't need to check for QPN * wrapping around past our starting QPN. * We just need to be sure we don't loop * forever. */ } while (offset < BITS_PER_PAGE && qpn < QPN_MAX); } /* * In order to keep the number of pages allocated to a * minimum, we scan the all existing pages before increasing * the size of the bitmap table. */ if (++i > max_scan) { if (qpt->nmaps == QPNMAP_ENTRIES) break; map = &qpt->map[qpt->nmaps++]; offset = 0; } else if (map < &qpt->map[qpt->nmaps]) { ++map; offset = 0; } else { map = &qpt->map[0]; offset = 2; } qpn = mk_qpn(qpt, map, offset); } ret = -ENOMEM;bail: return ret;}static void free_qpn(struct ipath_qp_table *qpt, u32 qpn){ struct qpn_map *map; map = qpt->map + qpn / BITS_PER_PAGE; if (map->page) clear_bit(qpn & BITS_PER_PAGE_MASK, map->page); atomic_inc(&map->n_free);}/** * ipath_alloc_qpn - allocate a QP number * @qpt: the QP table * @qp: the QP * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special) * * Allocate the next available QPN and put the QP into the hash table. * The hash table holds a reference to the QP. */static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp, enum ib_qp_type type){ unsigned long flags; int ret; ret = alloc_qpn(qpt, type); if (ret < 0) goto bail; qp->ibqp.qp_num = ret; /* Add the QP to the hash table. */ spin_lock_irqsave(&qpt->lock, flags); ret %= qpt->max; qp->next = qpt->table[ret]; qpt->table[ret] = qp; atomic_inc(&qp->refcount); spin_unlock_irqrestore(&qpt->lock, flags); ret = 0;bail: return ret;}/** * ipath_free_qp - remove a QP from the QP table * @qpt: the QP table * @qp: the QP to remove * * Remove the QP from the table so it can't be found asynchronously by * the receive interrupt routine. */static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp){ struct ipath_qp *q, **qpp; unsigned long flags; int fnd = 0; spin_lock_irqsave(&qpt->lock, flags); /* Remove QP from the hash table. */ qpp = &qpt->table[qp->ibqp.qp_num % qpt->max]; for (; (q = *qpp) != NULL; qpp = &q->next) { if (q == qp) { *qpp = qp->next; qp->next = NULL; atomic_dec(&qp->refcount); fnd = 1; break; } } spin_unlock_irqrestore(&qpt->lock, flags); if (!fnd) return; free_qpn(qpt, qp->ibqp.qp_num); wait_event(qp->wait, !atomic_read(&qp->refcount));}/** * ipath_free_all_qps - remove all QPs from the table * @qpt: the QP table to empty */void ipath_free_all_qps(struct ipath_qp_table *qpt){ unsigned long flags; struct ipath_qp *qp, *nqp; u32 n; for (n = 0; n < qpt->max; n++) { spin_lock_irqsave(&qpt->lock, flags); qp = qpt->table[n]; qpt->table[n] = NULL; spin_unlock_irqrestore(&qpt->lock, flags); while (qp) { nqp = qp->next; free_qpn(qpt, qp->ibqp.qp_num); if (!atomic_dec_and_test(&qp->refcount) || !ipath_destroy_qp(&qp->ibqp)) ipath_dbg("QP memory leak!\n"); qp = nqp; } } for (n = 0; n < ARRAY_SIZE(qpt->map); n++) { if (qpt->map[n].page) free_page((unsigned long)qpt->map[n].page); }}/** * ipath_lookup_qpn - return the QP with the given QPN * @qpt: the QP table * @qpn: the QP number to look up * * The caller is responsible for decrementing the QP reference count * when done. */struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn){ unsigned long flags; struct ipath_qp *qp; spin_lock_irqsave(&qpt->lock, flags); for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) { if (qp->ibqp.qp_num == qpn) { atomic_inc(&qp->refcount); break; } } spin_unlock_irqrestore(&qpt->lock, flags); return qp;}/** * ipath_reset_qp - initialize the QP state to the reset state * @qp: the QP to reset */static void ipath_reset_qp(struct ipath_qp *qp){ qp->remote_qpn = 0; qp->qkey = 0; qp->qp_access_flags = 0; qp->s_busy = 0; qp->s_flags &= IPATH_S_SIGNAL_REQ_WR; qp->s_hdrwords = 0; qp->s_wqe = NULL; qp->s_psn = 0; qp->r_psn = 0; qp->r_msn = 0; if (qp->ibqp.qp_type == IB_QPT_RC) { qp->s_state = IB_OPCODE_RC_SEND_LAST; qp->r_state = IB_OPCODE_RC_SEND_LAST; } else { qp->s_state = IB_OPCODE_UC_SEND_LAST; qp->r_state = IB_OPCODE_UC_SEND_LAST; } qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; qp->r_nak_state = 0; qp->r_wrid_valid = 0; qp->s_rnr_timeout = 0; qp->s_head = 0; qp->s_tail = 0; qp->s_cur = 0; qp->s_last = 0; qp->s_ssn = 1; qp->s_lsn = 0; qp->s_wait_credit = 0; memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue)); qp->r_head_ack_queue = 0; qp->s_tail_ack_queue = 0; qp->s_num_rd_atomic = 0; if (qp->r_rq.wq) { qp->r_rq.wq->head = 0; qp->r_rq.wq->tail = 0; } qp->r_reuse_sge = 0;}/** * ipath_error_qp - put a QP into an error state * @qp: the QP to put into an error state * @err: the receive completion error to signal if a RWQE is active * * Flushes both send and receive work queues. * Returns true if last WQE event should be generated. * The QP s_lock should be held and interrupts disabled. */int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err){ struct ipath_ibdev *dev = to_idev(qp->ibqp.device); struct ib_wc wc; int ret = 0; ipath_dbg("QP%d/%d in error state\n", qp->ibqp.qp_num, qp->remote_qpn); spin_lock(&dev->pending_lock); /* XXX What if its already removed by the timeout code? */ if (!list_empty(&qp->timerwait)) list_del_init(&qp->timerwait); if (!list_empty(&qp->piowait)) list_del_init(&qp->piowait); spin_unlock(&dev->pending_lock); wc.vendor_err = 0; wc.byte_len = 0; wc.imm_data = 0; wc.qp = &qp->ibqp; wc.src_qp = 0; wc.wc_flags = 0; wc.pkey_index = 0; wc.slid = 0; wc.sl = 0; wc.dlid_path_bits = 0; wc.port_num = 0; if (qp->r_wrid_valid) { qp->r_wrid_valid = 0; wc.wr_id = qp->r_wr_id; wc.opcode = IB_WC_RECV; wc.status = err; ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1); } wc.status = IB_WC_WR_FLUSH_ERR; while (qp->s_last != qp->s_head) { struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last); wc.wr_id = wqe->wr.wr_id; wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode]; if (++qp->s_last >= qp->s_size) qp->s_last = 0; ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1); } qp->s_cur = qp->s_tail = qp->s_head; qp->s_hdrwords = 0; qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE; if (qp->r_rq.wq) { struct ipath_rwq *wq; u32 head; u32 tail; spin_lock(&qp->r_rq.lock); /* sanity check pointers before trusting them */ wq = qp->r_rq.wq; head = wq->head; if (head >= qp->r_rq.size) head = 0; tail = wq->tail; if (tail >= qp->r_rq.size) tail = 0; wc.opcode = IB_WC_RECV; while (tail != head) { wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id; if (++tail >= qp->r_rq.size) tail = 0; ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1); } wq->tail = tail; spin_unlock(&qp->r_rq.lock); } else if (qp->ibqp.event_handler) ret = 1; return ret;}/** * ipath_modify_qp - modify the attributes of a queue pair * @ibqp: the queue pair who's attributes we're modifying * @attr: the new attributes * @attr_mask: the mask of attributes to modify * @udata: user data for ipathverbs.so * * Returns 0 on success, otherwise returns an errno. */int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask, struct ib_udata *udata){ struct ipath_ibdev *dev = to_idev(ibqp->device); struct ipath_qp *qp = to_iqp(ibqp); enum ib_qp_state cur_state, new_state; unsigned long flags; int lastwqe = 0; int ret; spin_lock_irqsave(&qp->s_lock, flags); cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state; new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state; if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask)) goto inval; if (attr_mask & IB_QP_AV) { if (attr->ah_attr.dlid == 0 || attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE) goto inval; if ((attr->ah_attr.ah_flags & IB_AH_GRH) && (attr->ah_attr.grh.sgid_index > 1)) goto inval; } if (attr_mask & IB_QP_PKEY_INDEX) if (attr->pkey_index >= ipath_get_npkeys(dev->dd)) goto inval; if (attr_mask & IB_QP_MIN_RNR_TIMER) if (attr->min_rnr_timer > 31) goto inval; if (attr_mask & IB_QP_PORT) if (attr->port_num == 0 || attr->port_num > ibqp->device->phys_port_cnt) goto inval; /* * Note: the chips support a maximum MTU of 4096, but the driver * hasn't implemented this feature yet, so don't allow Path MTU * values greater than 2048. */ if (attr_mask & IB_QP_PATH_MTU) if (attr->path_mtu > IB_MTU_2048) goto inval; if (attr_mask & IB_QP_PATH_MIG_STATE) if (attr->path_mig_state != IB_MIG_MIGRATED && attr->path_mig_state != IB_MIG_REARM) goto inval; if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC) goto inval; switch (new_state) { case IB_QPS_RESET: ipath_reset_qp(qp); break; case IB_QPS_ERR: lastwqe = ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?