Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1 | /* |
Chien Tung | fa6c87d | 2009-12-09 15:21:56 -0800 | [diff] [blame] | 2 | * Copyright (c) 2006 - 2009 Intel Corporation. All rights reserved. |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/moduleparam.h> |
| 36 | #include <linux/random.h> |
| 37 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 39 | #include <asm/byteorder.h> |
| 40 | |
| 41 | #include <rdma/ib_verbs.h> |
| 42 | #include <rdma/iw_cm.h> |
| 43 | #include <rdma/ib_user_verbs.h> |
| 44 | |
| 45 | #include "nes.h" |
| 46 | |
| 47 | #include <rdma/ib_umem.h> |
| 48 | |
| 49 | atomic_t mod_qp_timouts; |
| 50 | atomic_t qps_created; |
| 51 | atomic_t sw_qps_destroyed; |
| 52 | |
Roland Dreier | 1a855fb | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 53 | static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * nes_alloc_mw |
| 57 | */ |
| 58 | static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 59 | struct nes_pd *nespd = to_nespd(ibpd); |
| 60 | struct nes_vnic *nesvnic = to_nesvnic(ibpd->device); |
| 61 | struct nes_device *nesdev = nesvnic->nesdev; |
| 62 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 63 | struct nes_cqp_request *cqp_request; |
| 64 | struct nes_mr *nesmr; |
| 65 | struct ib_mw *ibmw; |
| 66 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 67 | int ret; |
| 68 | u32 stag; |
| 69 | u32 stag_index = 0; |
| 70 | u32 next_stag_index = 0; |
| 71 | u32 driver_key = 0; |
| 72 | u8 stag_key = 0; |
| 73 | |
| 74 | get_random_bytes(&next_stag_index, sizeof(next_stag_index)); |
| 75 | stag_key = (u8)next_stag_index; |
| 76 | |
| 77 | driver_key = 0; |
| 78 | |
| 79 | next_stag_index >>= 8; |
| 80 | next_stag_index %= nesadapter->max_mr; |
| 81 | |
| 82 | ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, |
| 83 | nesadapter->max_mr, &stag_index, &next_stag_index); |
| 84 | if (ret) { |
| 85 | return ERR_PTR(ret); |
| 86 | } |
| 87 | |
| 88 | nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL); |
| 89 | if (!nesmr) { |
| 90 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 91 | return ERR_PTR(-ENOMEM); |
| 92 | } |
| 93 | |
| 94 | stag = stag_index << 8; |
| 95 | stag |= driver_key; |
| 96 | stag += (u32)stag_key; |
| 97 | |
| 98 | nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n", |
| 99 | stag, stag_index); |
| 100 | |
| 101 | /* Register the region with the adapter */ |
| 102 | cqp_request = nes_get_cqp_request(nesdev); |
| 103 | if (cqp_request == NULL) { |
| 104 | kfree(nesmr); |
| 105 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 106 | return ERR_PTR(-ENOMEM); |
| 107 | } |
| 108 | |
| 109 | cqp_request->waiting = 1; |
| 110 | cqp_wqe = &cqp_request->cqp_wqe; |
| 111 | |
| 112 | cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] = |
| 113 | cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ | |
| 114 | NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO | |
| 115 | NES_CQP_STAG_REM_ACC_EN); |
| 116 | |
| 117 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 118 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff)); |
| 119 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag); |
| 120 | |
| 121 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 122 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 123 | |
| 124 | /* Wait for CQP */ |
| 125 | ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0), |
| 126 | NES_EVENT_TIMEOUT); |
| 127 | nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u," |
| 128 | " CQP Major:Minor codes = 0x%04X:0x%04X.\n", |
| 129 | stag, ret, cqp_request->major_code, cqp_request->minor_code); |
| 130 | if ((!ret) || (cqp_request->major_code)) { |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 131 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 132 | kfree(nesmr); |
| 133 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 134 | if (!ret) { |
| 135 | return ERR_PTR(-ETIME); |
| 136 | } else { |
| 137 | return ERR_PTR(-ENOMEM); |
| 138 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 139 | } |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 140 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 141 | |
| 142 | nesmr->ibmw.rkey = stag; |
| 143 | nesmr->mode = IWNES_MEMREG_TYPE_MW; |
| 144 | ibmw = &nesmr->ibmw; |
| 145 | nesmr->pbl_4k = 0; |
| 146 | nesmr->pbls_used = 0; |
| 147 | |
| 148 | return ibmw; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * nes_dealloc_mw |
| 154 | */ |
| 155 | static int nes_dealloc_mw(struct ib_mw *ibmw) |
| 156 | { |
| 157 | struct nes_mr *nesmr = to_nesmw(ibmw); |
| 158 | struct nes_vnic *nesvnic = to_nesvnic(ibmw->device); |
| 159 | struct nes_device *nesdev = nesvnic->nesdev; |
| 160 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 161 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 162 | struct nes_cqp_request *cqp_request; |
| 163 | int err = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 164 | int ret; |
| 165 | |
| 166 | /* Deallocate the window with the adapter */ |
| 167 | cqp_request = nes_get_cqp_request(nesdev); |
| 168 | if (cqp_request == NULL) { |
| 169 | nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n"); |
| 170 | return -ENOMEM; |
| 171 | } |
| 172 | cqp_request->waiting = 1; |
| 173 | cqp_wqe = &cqp_request->cqp_wqe; |
| 174 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 175 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG); |
| 176 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey); |
| 177 | |
| 178 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 179 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 180 | |
| 181 | /* Wait for CQP */ |
| 182 | nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n", |
| 183 | ibmw->rkey); |
| 184 | ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done), |
| 185 | NES_EVENT_TIMEOUT); |
| 186 | nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u," |
| 187 | " CQP Major:Minor codes = 0x%04X:0x%04X.\n", |
| 188 | ret, cqp_request->major_code, cqp_request->minor_code); |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 189 | if (!ret) |
| 190 | err = -ETIME; |
| 191 | else if (cqp_request->major_code) |
| 192 | err = -EIO; |
| 193 | |
| 194 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 195 | |
| 196 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, |
| 197 | (ibmw->rkey & 0x0fffff00) >> 8); |
| 198 | kfree(nesmr); |
| 199 | |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * nes_bind_mw |
| 206 | */ |
| 207 | static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw, |
| 208 | struct ib_mw_bind *ibmw_bind) |
| 209 | { |
| 210 | u64 u64temp; |
| 211 | struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); |
| 212 | struct nes_device *nesdev = nesvnic->nesdev; |
| 213 | /* struct nes_mr *nesmr = to_nesmw(ibmw); */ |
| 214 | struct nes_qp *nesqp = to_nesqp(ibqp); |
| 215 | struct nes_hw_qp_wqe *wqe; |
| 216 | unsigned long flags = 0; |
| 217 | u32 head; |
| 218 | u32 wqe_misc = 0; |
| 219 | u32 qsize; |
| 220 | |
| 221 | if (nesqp->ibqp_state > IB_QPS_RTS) |
| 222 | return -EINVAL; |
| 223 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 224 | spin_lock_irqsave(&nesqp->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 225 | |
| 226 | head = nesqp->hwqp.sq_head; |
| 227 | qsize = nesqp->hwqp.sq_tail; |
| 228 | |
| 229 | /* Check for SQ overflow */ |
| 230 | if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) { |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 231 | spin_unlock_irqrestore(&nesqp->lock, flags); |
Or Gerlitz | 831d06c | 2010-02-04 11:25:07 +0000 | [diff] [blame] | 232 | return -ENOMEM; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | wqe = &nesqp->hwqp.sq_vbase[head]; |
| 236 | /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */ |
| 237 | nes_fill_init_qp_wqe(wqe, nesqp, head); |
| 238 | u64temp = ibmw_bind->wr_id; |
| 239 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp); |
| 240 | wqe_misc = NES_IWARP_SQ_OP_BIND; |
| 241 | |
| 242 | wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE; |
| 243 | |
| 244 | if (ibmw_bind->send_flags & IB_SEND_SIGNALED) |
| 245 | wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL; |
| 246 | |
| 247 | if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) { |
| 248 | wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE; |
| 249 | } |
| 250 | if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) { |
| 251 | wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ; |
| 252 | } |
| 253 | |
| 254 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc); |
| 255 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey); |
| 256 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey); |
| 257 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX, |
| 258 | ibmw_bind->length); |
| 259 | wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0; |
| 260 | u64temp = (u64)ibmw_bind->addr; |
| 261 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp); |
| 262 | |
| 263 | head++; |
| 264 | if (head >= qsize) |
| 265 | head = 0; |
| 266 | |
| 267 | nesqp->hwqp.sq_head = head; |
| 268 | barrier(); |
| 269 | |
| 270 | nes_write32(nesdev->regs+NES_WQE_ALLOC, |
| 271 | (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id); |
| 272 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 273 | spin_unlock_irqrestore(&nesqp->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 279 | /* |
| 280 | * nes_alloc_fast_mr |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 281 | */ |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 282 | static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd, |
| 283 | u32 stag, u32 page_count) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 284 | { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 285 | struct nes_hw_cqp_wqe *cqp_wqe; |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 286 | struct nes_cqp_request *cqp_request; |
| 287 | unsigned long flags; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 288 | int ret; |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 289 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 290 | u32 opcode = 0; |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 291 | u16 major_code; |
| 292 | u64 region_length = page_count * PAGE_SIZE; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 293 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 294 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 295 | cqp_request = nes_get_cqp_request(nesdev); |
| 296 | if (cqp_request == NULL) { |
| 297 | nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n"); |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 298 | return -ENOMEM; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 299 | } |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 300 | nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, " |
| 301 | "region_length = %llu\n", |
| 302 | page_count, region_length); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 303 | cqp_request->waiting = 1; |
| 304 | cqp_wqe = &cqp_request->cqp_wqe; |
| 305 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 306 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 307 | if (nesadapter->free_4kpbl > 0) { |
| 308 | nesadapter->free_4kpbl--; |
| 309 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 310 | } else { |
| 311 | /* No 4kpbl's available: */ |
| 312 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 313 | nes_debug(NES_DBG_MR, "Out of Pbls\n"); |
| 314 | nes_free_cqp_request(nesdev, cqp_request); |
| 315 | return -ENOMEM; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 318 | opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR | |
| 319 | NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO | |
| 320 | NES_CQP_STAG_REM_ACC_EN; |
| 321 | /* |
| 322 | * The current OFED API does not support the zero based TO option. |
| 323 | * If added then need to changed the NES_CQP_STAG_VA* option. Also, |
| 324 | * the API does not support that ability to have the MR set for local |
| 325 | * access only when created and not allow the SQ op to override. Given |
| 326 | * this the remote enable must be set here. |
| 327 | */ |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 328 | |
| 329 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 330 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode); |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 331 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 332 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 333 | cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] = |
| 334 | cpu_to_le32((u32)(region_length >> 8) & 0xff000000); |
| 335 | cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |= |
| 336 | cpu_to_le32(nespd->pd_id & 0x00007fff); |
| 337 | |
| 338 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag); |
| 339 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0); |
| 340 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0); |
| 341 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0); |
| 342 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8)); |
| 343 | cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE); |
| 344 | barrier(); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 345 | |
| 346 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 347 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 348 | |
| 349 | /* Wait for CQP */ |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 350 | ret = wait_event_timeout(cqp_request->waitq, |
| 351 | (0 != cqp_request->request_done), |
| 352 | NES_EVENT_TIMEOUT); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 353 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 354 | nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, " |
| 355 | "wait_event_timeout ret = %u, CQP Major:Minor codes = " |
| 356 | "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code, |
| 357 | cqp_request->minor_code); |
| 358 | major_code = cqp_request->major_code; |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 359 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 360 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 361 | if (!ret || major_code) { |
Chien Tung | 35c6d69 | 2008-11-02 21:37:35 -0800 | [diff] [blame] | 362 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 363 | nesadapter->free_4kpbl++; |
Chien Tung | 35c6d69 | 2008-11-02 21:37:35 -0800 | [diff] [blame] | 364 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 365 | } |
| 366 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 367 | if (!ret) |
| 368 | return -ETIME; |
| 369 | else if (major_code) |
| 370 | return -EIO; |
| 371 | return 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 374 | /* |
| 375 | * nes_alloc_fast_reg_mr |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 376 | */ |
Roland Dreier | 53978b4 | 2010-04-21 15:58:28 -0700 | [diff] [blame] | 377 | static struct ib_mr *nes_alloc_fast_reg_mr(struct ib_pd *ibpd, int max_page_list_len) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 378 | { |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 379 | struct nes_pd *nespd = to_nespd(ibpd); |
| 380 | struct nes_vnic *nesvnic = to_nesvnic(ibpd->device); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 381 | struct nes_device *nesdev = nesvnic->nesdev; |
Chien Tung | 35c6d69 | 2008-11-02 21:37:35 -0800 | [diff] [blame] | 382 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 383 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 384 | u32 next_stag_index; |
| 385 | u8 stag_key = 0; |
| 386 | u32 driver_key = 0; |
| 387 | int err = 0; |
| 388 | u32 stag_index = 0; |
| 389 | struct nes_mr *nesmr; |
| 390 | u32 stag; |
| 391 | int ret; |
| 392 | struct ib_mr *ibmr; |
| 393 | /* |
| 394 | * Note: Set to always use a fixed length single page entry PBL. This is to allow |
| 395 | * for the fast_reg_mr operation to always know the size of the PBL. |
| 396 | */ |
| 397 | if (max_page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) |
| 398 | return ERR_PTR(-E2BIG); |
| 399 | |
| 400 | get_random_bytes(&next_stag_index, sizeof(next_stag_index)); |
| 401 | stag_key = (u8)next_stag_index; |
| 402 | next_stag_index >>= 8; |
| 403 | next_stag_index %= nesadapter->max_mr; |
| 404 | |
| 405 | err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, |
| 406 | nesadapter->max_mr, &stag_index, |
| 407 | &next_stag_index); |
| 408 | if (err) |
| 409 | return ERR_PTR(err); |
| 410 | |
| 411 | nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL); |
| 412 | if (!nesmr) { |
| 413 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 414 | return ERR_PTR(-ENOMEM); |
| 415 | } |
| 416 | |
| 417 | stag = stag_index << 8; |
| 418 | stag |= driver_key; |
| 419 | stag += (u32)stag_key; |
| 420 | |
| 421 | nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n", |
| 422 | stag, stag_index); |
| 423 | |
| 424 | ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_page_list_len); |
| 425 | |
| 426 | if (ret == 0) { |
| 427 | nesmr->ibmr.rkey = stag; |
| 428 | nesmr->ibmr.lkey = stag; |
| 429 | nesmr->mode = IWNES_MEMREG_TYPE_FMEM; |
| 430 | ibmr = &nesmr->ibmr; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 431 | } else { |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 432 | kfree(nesmr); |
| 433 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 434 | ibmr = ERR_PTR(-ENOMEM); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 435 | } |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 436 | return ibmr; |
| 437 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 438 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 439 | /* |
| 440 | * nes_alloc_fast_reg_page_list |
| 441 | */ |
| 442 | static struct ib_fast_reg_page_list *nes_alloc_fast_reg_page_list( |
| 443 | struct ib_device *ibdev, |
| 444 | int page_list_len) |
| 445 | { |
| 446 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 447 | struct nes_device *nesdev = nesvnic->nesdev; |
| 448 | struct ib_fast_reg_page_list *pifrpl; |
| 449 | struct nes_ib_fast_reg_page_list *pnesfrpl; |
Don Wood | dae5d13 | 2009-03-06 15:12:09 -0800 | [diff] [blame] | 450 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 451 | if (page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) |
| 452 | return ERR_PTR(-E2BIG); |
| 453 | /* |
| 454 | * Allocate the ib_fast_reg_page_list structure, the |
| 455 | * nes_fast_bpl structure, and the PLB table. |
| 456 | */ |
| 457 | pnesfrpl = kmalloc(sizeof(struct nes_ib_fast_reg_page_list) + |
| 458 | page_list_len * sizeof(u64), GFP_KERNEL); |
| 459 | |
| 460 | if (!pnesfrpl) |
| 461 | return ERR_PTR(-ENOMEM); |
| 462 | |
| 463 | pifrpl = &pnesfrpl->ibfrpl; |
| 464 | pifrpl->page_list = &pnesfrpl->pbl; |
| 465 | pifrpl->max_page_list_len = page_list_len; |
| 466 | /* |
| 467 | * Allocate the WQE PBL |
| 468 | */ |
| 469 | pnesfrpl->nes_wqe_pbl.kva = pci_alloc_consistent(nesdev->pcidev, |
| 470 | page_list_len * sizeof(u64), |
| 471 | &pnesfrpl->nes_wqe_pbl.paddr); |
| 472 | |
| 473 | if (!pnesfrpl->nes_wqe_pbl.kva) { |
| 474 | kfree(pnesfrpl); |
| 475 | return ERR_PTR(-ENOMEM); |
Chien Tung | 35c6d69 | 2008-11-02 21:37:35 -0800 | [diff] [blame] | 476 | } |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 477 | nes_debug(NES_DBG_MR, "nes_alloc_fast_reg_pbl: nes_frpl = %p, " |
| 478 | "ibfrpl = %p, ibfrpl.page_list = %p, pbl.kva = %p, " |
Roland Dreier | 183ae74 | 2010-09-27 17:51:33 -0700 | [diff] [blame] | 479 | "pbl.paddr = %llx\n", pnesfrpl, &pnesfrpl->ibfrpl, |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 480 | pnesfrpl->ibfrpl.page_list, pnesfrpl->nes_wqe_pbl.kva, |
Roland Dreier | 183ae74 | 2010-09-27 17:51:33 -0700 | [diff] [blame] | 481 | (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr); |
Chien Tung | 35c6d69 | 2008-11-02 21:37:35 -0800 | [diff] [blame] | 482 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 483 | return pifrpl; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 486 | /* |
| 487 | * nes_free_fast_reg_page_list |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 488 | */ |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 489 | static void nes_free_fast_reg_page_list(struct ib_fast_reg_page_list *pifrpl) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 490 | { |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 491 | struct nes_vnic *nesvnic = to_nesvnic(pifrpl->device); |
| 492 | struct nes_device *nesdev = nesvnic->nesdev; |
| 493 | struct nes_ib_fast_reg_page_list *pnesfrpl; |
| 494 | |
| 495 | pnesfrpl = container_of(pifrpl, struct nes_ib_fast_reg_page_list, ibfrpl); |
| 496 | /* |
| 497 | * Free the WQE PBL. |
| 498 | */ |
| 499 | pci_free_consistent(nesdev->pcidev, |
| 500 | pifrpl->max_page_list_len * sizeof(u64), |
| 501 | pnesfrpl->nes_wqe_pbl.kva, |
| 502 | pnesfrpl->nes_wqe_pbl.paddr); |
| 503 | /* |
| 504 | * Free the PBL structure |
| 505 | */ |
| 506 | kfree(pnesfrpl); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 509 | /** |
| 510 | * nes_query_device |
| 511 | */ |
| 512 | static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props) |
| 513 | { |
| 514 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 515 | struct nes_device *nesdev = nesvnic->nesdev; |
| 516 | struct nes_ib_device *nesibdev = nesvnic->nesibdev; |
| 517 | |
| 518 | memset(props, 0, sizeof(*props)); |
| 519 | memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6); |
| 520 | |
Miroslaw Walukiewicz | ff0380c | 2010-07-15 14:53:29 +0000 | [diff] [blame] | 521 | props->fw_ver = nesdev->nesadapter->firmware_version; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 522 | props->device_cap_flags = nesdev->nesadapter->device_cap_flags; |
| 523 | props->vendor_id = nesdev->nesadapter->vendor_id; |
| 524 | props->vendor_part_id = nesdev->nesadapter->vendor_part_id; |
| 525 | props->hw_ver = nesdev->nesadapter->hw_rev; |
| 526 | props->max_mr_size = 0x80000000; |
| 527 | props->max_qp = nesibdev->max_qp; |
| 528 | props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2; |
| 529 | props->max_sge = nesdev->nesadapter->max_sge; |
| 530 | props->max_cq = nesibdev->max_cq; |
Chien Tung | 5924aea | 2009-12-09 15:21:56 -0800 | [diff] [blame] | 531 | props->max_cqe = nesdev->nesadapter->max_cqe; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 532 | props->max_mr = nesibdev->max_mr; |
| 533 | props->max_mw = nesibdev->max_mr; |
| 534 | props->max_pd = nesibdev->max_pd; |
| 535 | props->max_sge_rd = 1; |
| 536 | switch (nesdev->nesadapter->max_irrq_wr) { |
| 537 | case 0: |
Faisal Latif | 8ac7f6e | 2009-12-09 15:53:46 -0800 | [diff] [blame] | 538 | props->max_qp_rd_atom = 2; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 539 | break; |
| 540 | case 1: |
Faisal Latif | 8ac7f6e | 2009-12-09 15:53:46 -0800 | [diff] [blame] | 541 | props->max_qp_rd_atom = 8; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 542 | break; |
| 543 | case 2: |
Faisal Latif | 8ac7f6e | 2009-12-09 15:53:46 -0800 | [diff] [blame] | 544 | props->max_qp_rd_atom = 32; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 545 | break; |
| 546 | case 3: |
Faisal Latif | 8ac7f6e | 2009-12-09 15:53:46 -0800 | [diff] [blame] | 547 | props->max_qp_rd_atom = 64; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 548 | break; |
| 549 | default: |
| 550 | props->max_qp_rd_atom = 0; |
| 551 | } |
Faisal Latif | 66388d6 | 2009-06-22 22:52:30 -0700 | [diff] [blame] | 552 | props->max_qp_init_rd_atom = props->max_qp_rd_atom; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 553 | props->atomic_cap = IB_ATOMIC_NONE; |
| 554 | props->max_map_per_fmr = 1; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | /** |
| 561 | * nes_query_port |
| 562 | */ |
| 563 | static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props) |
| 564 | { |
Chien Tung | cd1d3f7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 565 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 566 | struct net_device *netdev = nesvnic->netdev; |
| 567 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 568 | memset(props, 0, sizeof(*props)); |
| 569 | |
Chien Tung | cd1d3f7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 570 | props->max_mtu = IB_MTU_4096; |
| 571 | |
| 572 | if (netdev->mtu >= 4096) |
| 573 | props->active_mtu = IB_MTU_4096; |
| 574 | else if (netdev->mtu >= 2048) |
| 575 | props->active_mtu = IB_MTU_2048; |
| 576 | else if (netdev->mtu >= 1024) |
| 577 | props->active_mtu = IB_MTU_1024; |
| 578 | else if (netdev->mtu >= 512) |
| 579 | props->active_mtu = IB_MTU_512; |
| 580 | else |
| 581 | props->active_mtu = IB_MTU_256; |
| 582 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 583 | props->lid = 1; |
| 584 | props->lmc = 0; |
| 585 | props->sm_lid = 0; |
| 586 | props->sm_sl = 0; |
Chien Tung | 2932772 | 2010-08-23 14:32:36 +0000 | [diff] [blame] | 587 | if (netif_queue_stopped(netdev)) |
| 588 | props->state = IB_PORT_DOWN; |
| 589 | else if (nesvnic->linkup) |
Chien Tung | cd1d3f7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 590 | props->state = IB_PORT_ACTIVE; |
| 591 | else |
| 592 | props->state = IB_PORT_DOWN; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 593 | props->phys_state = 0; |
| 594 | props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP | |
| 595 | IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP; |
| 596 | props->gid_tbl_len = 1; |
| 597 | props->pkey_tbl_len = 1; |
| 598 | props->qkey_viol_cntr = 0; |
| 599 | props->active_width = IB_WIDTH_4X; |
| 600 | props->active_speed = 1; |
| 601 | props->max_msg_sz = 0x80000000; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /** |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 608 | * nes_query_pkey |
| 609 | */ |
| 610 | static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey) |
| 611 | { |
| 612 | *pkey = 0; |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | /** |
| 618 | * nes_query_gid |
| 619 | */ |
| 620 | static int nes_query_gid(struct ib_device *ibdev, u8 port, |
| 621 | int index, union ib_gid *gid) |
| 622 | { |
| 623 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 624 | |
| 625 | memset(&(gid->raw[0]), 0, sizeof(gid->raw)); |
| 626 | memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6); |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | /** |
| 633 | * nes_alloc_ucontext - Allocate the user context data structure. This keeps track |
| 634 | * of all objects associated with a particular user-mode client. |
| 635 | */ |
| 636 | static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev, |
| 637 | struct ib_udata *udata) |
| 638 | { |
| 639 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 640 | struct nes_device *nesdev = nesvnic->nesdev; |
| 641 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 642 | struct nes_alloc_ucontext_req req; |
| 643 | struct nes_alloc_ucontext_resp uresp; |
| 644 | struct nes_ucontext *nes_ucontext; |
| 645 | struct nes_ib_device *nesibdev = nesvnic->nesibdev; |
| 646 | |
| 647 | |
| 648 | if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) { |
| 649 | printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n"); |
| 650 | return ERR_PTR(-EINVAL); |
| 651 | } |
| 652 | |
| 653 | if (req.userspace_ver != NES_ABI_USERSPACE_VER) { |
| 654 | printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n", |
| 655 | req.userspace_ver, NES_ABI_USERSPACE_VER); |
| 656 | return ERR_PTR(-EINVAL); |
| 657 | } |
| 658 | |
| 659 | |
| 660 | memset(&uresp, 0, sizeof uresp); |
| 661 | |
| 662 | uresp.max_qps = nesibdev->max_qp; |
| 663 | uresp.max_pds = nesibdev->max_pd; |
| 664 | uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2; |
| 665 | uresp.virtwq = nesadapter->virtwq; |
| 666 | uresp.kernel_ver = NES_ABI_KERNEL_VER; |
| 667 | |
| 668 | nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL); |
| 669 | if (!nes_ucontext) |
| 670 | return ERR_PTR(-ENOMEM); |
| 671 | |
| 672 | nes_ucontext->nesdev = nesdev; |
| 673 | nes_ucontext->mmap_wq_offset = uresp.max_pds; |
| 674 | nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset + |
| 675 | ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) / |
| 676 | PAGE_SIZE; |
| 677 | |
| 678 | |
| 679 | if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) { |
| 680 | kfree(nes_ucontext); |
| 681 | return ERR_PTR(-EFAULT); |
| 682 | } |
| 683 | |
| 684 | INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list); |
| 685 | INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list); |
| 686 | atomic_set(&nes_ucontext->usecnt, 1); |
| 687 | return &nes_ucontext->ibucontext; |
| 688 | } |
| 689 | |
| 690 | |
| 691 | /** |
| 692 | * nes_dealloc_ucontext |
| 693 | */ |
| 694 | static int nes_dealloc_ucontext(struct ib_ucontext *context) |
| 695 | { |
| 696 | /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */ |
| 697 | /* struct nes_device *nesdev = nesvnic->nesdev; */ |
| 698 | struct nes_ucontext *nes_ucontext = to_nesucontext(context); |
| 699 | |
| 700 | if (!atomic_dec_and_test(&nes_ucontext->usecnt)) |
| 701 | return 0; |
| 702 | kfree(nes_ucontext); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /** |
| 708 | * nes_mmap |
| 709 | */ |
| 710 | static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma) |
| 711 | { |
| 712 | unsigned long index; |
| 713 | struct nes_vnic *nesvnic = to_nesvnic(context->device); |
| 714 | struct nes_device *nesdev = nesvnic->nesdev; |
| 715 | /* struct nes_adapter *nesadapter = nesdev->nesadapter; */ |
| 716 | struct nes_ucontext *nes_ucontext; |
| 717 | struct nes_qp *nesqp; |
| 718 | |
| 719 | nes_ucontext = to_nesucontext(context); |
| 720 | |
| 721 | |
| 722 | if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) { |
| 723 | index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE; |
| 724 | index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) + |
| 725 | PAGE_SIZE-1) & (~(PAGE_SIZE-1)); |
| 726 | if (!test_bit(index, nes_ucontext->allocated_wqs)) { |
| 727 | nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index); |
| 728 | return -EFAULT; |
| 729 | } |
| 730 | nesqp = nes_ucontext->mmap_nesqp[index]; |
| 731 | if (nesqp == NULL) { |
| 732 | nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index); |
| 733 | return -EFAULT; |
| 734 | } |
| 735 | if (remap_pfn_range(vma, vma->vm_start, |
| 736 | virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT, |
| 737 | vma->vm_end - vma->vm_start, |
| 738 | vma->vm_page_prot)) { |
| 739 | nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n"); |
| 740 | return -EAGAIN; |
| 741 | } |
| 742 | vma->vm_private_data = nesqp; |
| 743 | return 0; |
| 744 | } else { |
| 745 | index = vma->vm_pgoff; |
| 746 | if (!test_bit(index, nes_ucontext->allocated_doorbells)) |
| 747 | return -EFAULT; |
| 748 | |
| 749 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 750 | if (io_remap_pfn_range(vma, vma->vm_start, |
| 751 | (nesdev->doorbell_start + |
| 752 | ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096)) |
| 753 | >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot)) |
| 754 | return -EAGAIN; |
| 755 | vma->vm_private_data = nes_ucontext; |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | return -ENOSYS; |
| 760 | } |
| 761 | |
| 762 | |
| 763 | /** |
| 764 | * nes_alloc_pd |
| 765 | */ |
| 766 | static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev, |
| 767 | struct ib_ucontext *context, struct ib_udata *udata) |
| 768 | { |
| 769 | struct nes_pd *nespd; |
| 770 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 771 | struct nes_device *nesdev = nesvnic->nesdev; |
| 772 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 773 | struct nes_ucontext *nesucontext; |
| 774 | struct nes_alloc_pd_resp uresp; |
| 775 | u32 pd_num = 0; |
| 776 | int err; |
| 777 | |
| 778 | nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n", |
| 779 | nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context, |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 780 | netdev_refcnt_read(nesvnic->netdev)); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 781 | |
| 782 | err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds, |
| 783 | nesadapter->max_pd, &pd_num, &nesadapter->next_pd); |
| 784 | if (err) { |
| 785 | return ERR_PTR(err); |
| 786 | } |
| 787 | |
| 788 | nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL); |
| 789 | if (!nespd) { |
| 790 | nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num); |
| 791 | return ERR_PTR(-ENOMEM); |
| 792 | } |
| 793 | |
| 794 | nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n", |
| 795 | nespd, nesvnic->nesibdev->ibdev.name); |
| 796 | |
| 797 | nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd; |
| 798 | |
| 799 | if (context) { |
| 800 | nesucontext = to_nesucontext(context); |
| 801 | nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells, |
| 802 | NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db); |
| 803 | nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n", |
| 804 | nespd->mmap_db_index, nespd->pd_id); |
Adrian Bunk | 65b07ec | 2008-02-21 08:01:18 -0600 | [diff] [blame] | 805 | if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 806 | nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n"); |
| 807 | nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num); |
| 808 | kfree(nespd); |
| 809 | return ERR_PTR(-ENOMEM); |
| 810 | } |
| 811 | |
| 812 | uresp.pd_id = nespd->pd_id; |
| 813 | uresp.mmap_db_index = nespd->mmap_db_index; |
| 814 | if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) { |
| 815 | nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num); |
| 816 | kfree(nespd); |
| 817 | return ERR_PTR(-EFAULT); |
| 818 | } |
| 819 | |
| 820 | set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells); |
| 821 | nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id; |
| 822 | nesucontext->first_free_db = nespd->mmap_db_index + 1; |
| 823 | } |
| 824 | |
| 825 | nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd); |
| 826 | return &nespd->ibpd; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | /** |
| 831 | * nes_dealloc_pd |
| 832 | */ |
| 833 | static int nes_dealloc_pd(struct ib_pd *ibpd) |
| 834 | { |
| 835 | struct nes_ucontext *nesucontext; |
| 836 | struct nes_pd *nespd = to_nespd(ibpd); |
| 837 | struct nes_vnic *nesvnic = to_nesvnic(ibpd->device); |
| 838 | struct nes_device *nesdev = nesvnic->nesdev; |
| 839 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 840 | |
| 841 | if ((ibpd->uobject) && (ibpd->uobject->context)) { |
| 842 | nesucontext = to_nesucontext(ibpd->uobject->context); |
| 843 | nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n", |
| 844 | nespd->mmap_db_index); |
| 845 | clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells); |
| 846 | nesucontext->mmap_db_index[nespd->mmap_db_index] = 0; |
| 847 | if (nesucontext->first_free_db > nespd->mmap_db_index) { |
| 848 | nesucontext->first_free_db = nespd->mmap_db_index; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n", |
| 853 | nespd->pd_id, nespd); |
| 854 | nes_free_resource(nesadapter, nesadapter->allocated_pds, |
| 855 | (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12)); |
| 856 | kfree(nespd); |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /** |
| 863 | * nes_create_ah |
| 864 | */ |
| 865 | static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr) |
| 866 | { |
| 867 | return ERR_PTR(-ENOSYS); |
| 868 | } |
| 869 | |
| 870 | |
| 871 | /** |
| 872 | * nes_destroy_ah |
| 873 | */ |
| 874 | static int nes_destroy_ah(struct ib_ah *ah) |
| 875 | { |
| 876 | return -ENOSYS; |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /** |
| 881 | * nes_get_encoded_size |
| 882 | */ |
| 883 | static inline u8 nes_get_encoded_size(int *size) |
| 884 | { |
| 885 | u8 encoded_size = 0; |
| 886 | if (*size <= 32) { |
| 887 | *size = 32; |
| 888 | encoded_size = 1; |
| 889 | } else if (*size <= 128) { |
| 890 | *size = 128; |
| 891 | encoded_size = 2; |
| 892 | } else if (*size <= 512) { |
| 893 | *size = 512; |
| 894 | encoded_size = 3; |
| 895 | } |
| 896 | return (encoded_size); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | |
| 901 | /** |
| 902 | * nes_setup_virt_qp |
| 903 | */ |
| 904 | static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl, |
| 905 | struct nes_vnic *nesvnic, int sq_size, int rq_size) |
| 906 | { |
| 907 | unsigned long flags; |
| 908 | void *mem; |
| 909 | __le64 *pbl = NULL; |
| 910 | __le64 *tpbl; |
| 911 | __le64 *pblbuffer; |
| 912 | struct nes_device *nesdev = nesvnic->nesdev; |
| 913 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 914 | u32 pbl_entries; |
| 915 | u8 rq_pbl_entries; |
| 916 | u8 sq_pbl_entries; |
| 917 | |
| 918 | pbl_entries = nespbl->pbl_size >> 3; |
Roland Dreier | 71e0957 | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 919 | nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n", |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 920 | nespbl->pbl_size, pbl_entries, |
| 921 | (void *)nespbl->pbl_vbase, |
Roland Dreier | 71e0957 | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 922 | (unsigned long) nespbl->pbl_pbase); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 923 | pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */ |
| 924 | /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */ |
| 925 | /* the first pbl to be fro the rq_vbase... */ |
| 926 | rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12; |
| 927 | sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12; |
| 928 | nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32); |
| 929 | if (!nespbl->page) { |
| 930 | nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n"); |
| 931 | kfree(nespbl); |
| 932 | return -ENOMEM; |
| 933 | } |
| 934 | |
| 935 | nesqp->hwqp.sq_vbase = kmap(nespbl->page); |
| 936 | nesqp->page = nespbl->page; |
| 937 | if (!nesqp->hwqp.sq_vbase) { |
| 938 | nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n"); |
| 939 | kfree(nespbl); |
| 940 | return -ENOMEM; |
| 941 | } |
| 942 | |
| 943 | /* Now to get to sq.. we need to calculate how many */ |
| 944 | /* PBL entries were used by the rq.. */ |
| 945 | pbl += sq_pbl_entries; |
| 946 | nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32); |
| 947 | /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */ |
| 948 | /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */ |
| 949 | |
Roland Dreier | 71e0957 | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 950 | nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n", |
| 951 | nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase, |
| 952 | nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 953 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 954 | if (!nesadapter->free_256pbl) { |
| 955 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase, |
| 956 | nespbl->pbl_pbase); |
| 957 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 958 | kunmap(nesqp->page); |
| 959 | kfree(nespbl); |
| 960 | return -ENOMEM; |
| 961 | } |
| 962 | nesadapter->free_256pbl--; |
| 963 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 964 | |
| 965 | nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase); |
| 966 | pblbuffer = nesqp->pbl_vbase; |
| 967 | if (!nesqp->pbl_vbase) { |
| 968 | /* memory allocated during nes_reg_user_mr() */ |
| 969 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase, |
| 970 | nespbl->pbl_pbase); |
| 971 | kfree(nespbl); |
| 972 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 973 | nesadapter->free_256pbl++; |
| 974 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 975 | kunmap(nesqp->page); |
| 976 | return -ENOMEM; |
| 977 | } |
| 978 | memset(nesqp->pbl_vbase, 0, 256); |
| 979 | /* fill in the page address in the pbl buffer.. */ |
| 980 | tpbl = pblbuffer + 16; |
| 981 | pbl = (__le64 *)nespbl->pbl_vbase; |
| 982 | while (sq_pbl_entries--) |
| 983 | *tpbl++ = *pbl++; |
| 984 | tpbl = pblbuffer; |
| 985 | while (rq_pbl_entries--) |
| 986 | *tpbl++ = *pbl++; |
| 987 | |
| 988 | /* done with memory allocated during nes_reg_user_mr() */ |
| 989 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase, |
| 990 | nespbl->pbl_pbase); |
| 991 | kfree(nespbl); |
| 992 | |
| 993 | nesqp->qp_mem_size = |
| 994 | max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */ |
| 995 | /* Round up to a multiple of a page */ |
| 996 | nesqp->qp_mem_size += PAGE_SIZE - 1; |
| 997 | nesqp->qp_mem_size &= ~(PAGE_SIZE - 1); |
| 998 | |
| 999 | mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size, |
| 1000 | &nesqp->hwqp.q2_pbase); |
| 1001 | |
| 1002 | if (!mem) { |
| 1003 | pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase); |
| 1004 | nesqp->pbl_vbase = NULL; |
| 1005 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 1006 | nesadapter->free_256pbl++; |
| 1007 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1008 | kunmap(nesqp->page); |
| 1009 | return -ENOMEM; |
| 1010 | } |
Faisal Latif | d2fa9b2 | 2009-12-09 15:54:28 -0800 | [diff] [blame] | 1011 | nesqp->sq_kmapped = 1; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1012 | nesqp->hwqp.q2_vbase = mem; |
| 1013 | mem += 256; |
| 1014 | memset(nesqp->hwqp.q2_vbase, 0, 256); |
| 1015 | nesqp->nesqp_context = mem; |
| 1016 | memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context)); |
| 1017 | nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256; |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /** |
| 1024 | * nes_setup_mmap_qp |
| 1025 | */ |
| 1026 | static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic, |
| 1027 | int sq_size, int rq_size) |
| 1028 | { |
| 1029 | void *mem; |
| 1030 | struct nes_device *nesdev = nesvnic->nesdev; |
| 1031 | |
| 1032 | nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) + |
| 1033 | (sizeof(struct nes_hw_qp_wqe) * rq_size) + |
| 1034 | max((u32)sizeof(struct nes_qp_context), ((u32)256)) + |
| 1035 | 256; /* this is Q2 */ |
| 1036 | /* Round up to a multiple of a page */ |
| 1037 | nesqp->qp_mem_size += PAGE_SIZE - 1; |
| 1038 | nesqp->qp_mem_size &= ~(PAGE_SIZE - 1); |
| 1039 | |
| 1040 | mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size, |
| 1041 | &nesqp->hwqp.sq_pbase); |
| 1042 | if (!mem) |
| 1043 | return -ENOMEM; |
| 1044 | nes_debug(NES_DBG_QP, "PCI consistent memory for " |
| 1045 | "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n", |
| 1046 | mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size); |
| 1047 | |
| 1048 | memset(mem, 0, nesqp->qp_mem_size); |
| 1049 | |
| 1050 | nesqp->hwqp.sq_vbase = mem; |
| 1051 | mem += sizeof(struct nes_hw_qp_wqe) * sq_size; |
| 1052 | |
| 1053 | nesqp->hwqp.rq_vbase = mem; |
| 1054 | nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase + |
| 1055 | sizeof(struct nes_hw_qp_wqe) * sq_size; |
| 1056 | mem += sizeof(struct nes_hw_qp_wqe) * rq_size; |
| 1057 | |
| 1058 | nesqp->hwqp.q2_vbase = mem; |
| 1059 | nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase + |
| 1060 | sizeof(struct nes_hw_qp_wqe) * rq_size; |
| 1061 | mem += 256; |
| 1062 | memset(nesqp->hwqp.q2_vbase, 0, 256); |
| 1063 | |
| 1064 | nesqp->nesqp_context = mem; |
| 1065 | nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256; |
| 1066 | memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context)); |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | /** |
| 1072 | * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory. |
| 1073 | */ |
| 1074 | static inline void nes_free_qp_mem(struct nes_device *nesdev, |
| 1075 | struct nes_qp *nesqp, int virt_wqs) |
| 1076 | { |
| 1077 | unsigned long flags; |
| 1078 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 1079 | if (!virt_wqs) { |
| 1080 | pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, |
| 1081 | nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase); |
| 1082 | }else { |
| 1083 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 1084 | nesadapter->free_256pbl++; |
| 1085 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1086 | pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase); |
| 1087 | pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase ); |
| 1088 | nesqp->pbl_vbase = NULL; |
Faisal Latif | d2fa9b2 | 2009-12-09 15:54:28 -0800 | [diff] [blame] | 1089 | if (nesqp->sq_kmapped) { |
| 1090 | nesqp->sq_kmapped = 0; |
| 1091 | kunmap(nesqp->page); |
| 1092 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | /** |
| 1098 | * nes_create_qp |
| 1099 | */ |
| 1100 | static struct ib_qp *nes_create_qp(struct ib_pd *ibpd, |
| 1101 | struct ib_qp_init_attr *init_attr, struct ib_udata *udata) |
| 1102 | { |
| 1103 | u64 u64temp= 0; |
| 1104 | u64 u64nesqp = 0; |
| 1105 | struct nes_pd *nespd = to_nespd(ibpd); |
| 1106 | struct nes_vnic *nesvnic = to_nesvnic(ibpd->device); |
| 1107 | struct nes_device *nesdev = nesvnic->nesdev; |
| 1108 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 1109 | struct nes_qp *nesqp; |
| 1110 | struct nes_cq *nescq; |
| 1111 | struct nes_ucontext *nes_ucontext; |
| 1112 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 1113 | struct nes_cqp_request *cqp_request; |
| 1114 | struct nes_create_qp_req req; |
| 1115 | struct nes_create_qp_resp uresp; |
| 1116 | struct nes_pbl *nespbl = NULL; |
| 1117 | u32 qp_num = 0; |
| 1118 | u32 opcode = 0; |
| 1119 | /* u32 counter = 0; */ |
| 1120 | void *mem; |
| 1121 | unsigned long flags; |
| 1122 | int ret; |
| 1123 | int err; |
| 1124 | int virt_wqs = 0; |
| 1125 | int sq_size; |
| 1126 | int rq_size; |
| 1127 | u8 sq_encoded_size; |
| 1128 | u8 rq_encoded_size; |
| 1129 | /* int counter; */ |
| 1130 | |
Eli Cohen | b846f25 | 2008-04-16 21:09:27 -0700 | [diff] [blame] | 1131 | if (init_attr->create_flags) |
| 1132 | return ERR_PTR(-EINVAL); |
| 1133 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1134 | atomic_inc(&qps_created); |
| 1135 | switch (init_attr->qp_type) { |
| 1136 | case IB_QPT_RC: |
| 1137 | if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) { |
| 1138 | init_attr->cap.max_inline_data = 0; |
| 1139 | } else { |
| 1140 | init_attr->cap.max_inline_data = 64; |
| 1141 | } |
| 1142 | sq_size = init_attr->cap.max_send_wr; |
| 1143 | rq_size = init_attr->cap.max_recv_wr; |
| 1144 | |
Glenn Streiff | 7495ab6 | 2008-04-29 13:46:54 -0700 | [diff] [blame] | 1145 | /* check if the encoded sizes are OK or not... */ |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1146 | sq_encoded_size = nes_get_encoded_size(&sq_size); |
| 1147 | rq_encoded_size = nes_get_encoded_size(&rq_size); |
| 1148 | |
| 1149 | if ((!sq_encoded_size) || (!rq_encoded_size)) { |
| 1150 | nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n", |
| 1151 | rq_size, sq_size); |
| 1152 | return ERR_PTR(-EINVAL); |
| 1153 | } |
| 1154 | |
| 1155 | init_attr->cap.max_send_wr = sq_size -2; |
| 1156 | init_attr->cap.max_recv_wr = rq_size -1; |
| 1157 | nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size); |
| 1158 | |
| 1159 | ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps, |
| 1160 | nesadapter->max_qp, &qp_num, &nesadapter->next_qp); |
| 1161 | if (ret) { |
| 1162 | return ERR_PTR(ret); |
| 1163 | } |
| 1164 | |
| 1165 | /* Need 512 (actually now 1024) byte alignment on this structure */ |
| 1166 | mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL); |
| 1167 | if (!mem) { |
| 1168 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1169 | nes_debug(NES_DBG_QP, "Unable to allocate QP\n"); |
| 1170 | return ERR_PTR(-ENOMEM); |
| 1171 | } |
| 1172 | u64nesqp = (unsigned long)mem; |
| 1173 | u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1; |
| 1174 | u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1; |
| 1175 | u64nesqp &= ~u64temp; |
| 1176 | nesqp = (struct nes_qp *)(unsigned long)u64nesqp; |
| 1177 | /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n", |
| 1178 | nesqp, mem, NES_SW_CONTEXT_ALIGN); */ |
| 1179 | nesqp->allocated_buffer = mem; |
| 1180 | |
| 1181 | if (udata) { |
| 1182 | if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) { |
| 1183 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1184 | kfree(nesqp->allocated_buffer); |
| 1185 | nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n"); |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | if (req.user_wqe_buffers) { |
| 1189 | virt_wqs = 1; |
| 1190 | } |
| 1191 | if ((ibpd->uobject) && (ibpd->uobject->context)) { |
| 1192 | nesqp->user_mode = 1; |
| 1193 | nes_ucontext = to_nesucontext(ibpd->uobject->context); |
| 1194 | if (virt_wqs) { |
| 1195 | err = 1; |
| 1196 | list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) { |
| 1197 | if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) { |
| 1198 | list_del(&nespbl->list); |
| 1199 | err = 0; |
| 1200 | nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n", |
| 1201 | nespbl, nespbl->user_base); |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
| 1205 | if (err) { |
| 1206 | nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n", |
| 1207 | (long long unsigned int)req.user_wqe_buffers); |
| 1208 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1209 | kfree(nesqp->allocated_buffer); |
Chien Tung | 9300c0c | 2008-02-21 07:51:17 -0600 | [diff] [blame] | 1210 | return ERR_PTR(-EFAULT); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | nes_ucontext = to_nesucontext(ibpd->uobject->context); |
| 1215 | nesqp->mmap_sq_db_index = |
| 1216 | find_next_zero_bit(nes_ucontext->allocated_wqs, |
| 1217 | NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq); |
| 1218 | /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n", |
| 1219 | nespd->mmap_db_index); */ |
Roland Dreier | 51af33e | 2008-02-18 10:33:59 -0800 | [diff] [blame] | 1220 | if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1221 | nes_debug(NES_DBG_QP, |
| 1222 | "db index > max user regions, failing create QP\n"); |
| 1223 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1224 | if (virt_wqs) { |
| 1225 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase, |
| 1226 | nespbl->pbl_pbase); |
| 1227 | kfree(nespbl); |
| 1228 | } |
| 1229 | kfree(nesqp->allocated_buffer); |
| 1230 | return ERR_PTR(-ENOMEM); |
| 1231 | } |
| 1232 | set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs); |
| 1233 | nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp; |
| 1234 | nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1; |
| 1235 | } else { |
| 1236 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1237 | kfree(nesqp->allocated_buffer); |
| 1238 | return ERR_PTR(-EFAULT); |
| 1239 | } |
| 1240 | } |
| 1241 | err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) : |
| 1242 | nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size); |
| 1243 | if (err) { |
| 1244 | nes_debug(NES_DBG_QP, |
| 1245 | "error geting qp mem code = %d\n", err); |
| 1246 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1247 | kfree(nesqp->allocated_buffer); |
| 1248 | return ERR_PTR(-ENOMEM); |
| 1249 | } |
| 1250 | |
| 1251 | nesqp->hwqp.sq_size = sq_size; |
| 1252 | nesqp->hwqp.sq_encoded_size = sq_encoded_size; |
| 1253 | nesqp->hwqp.sq_head = 1; |
| 1254 | nesqp->hwqp.rq_size = rq_size; |
| 1255 | nesqp->hwqp.rq_encoded_size = rq_encoded_size; |
| 1256 | /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n", |
| 1257 | (void *)nesqp->nesqp_context_pbase); |
| 1258 | */ |
| 1259 | nesqp->hwqp.qp_id = qp_num; |
| 1260 | nesqp->ibqp.qp_num = nesqp->hwqp.qp_id; |
| 1261 | nesqp->nespd = nespd; |
| 1262 | |
| 1263 | nescq = to_nescq(init_attr->send_cq); |
| 1264 | nesqp->nesscq = nescq; |
| 1265 | nescq = to_nescq(init_attr->recv_cq); |
| 1266 | nesqp->nesrcq = nescq; |
| 1267 | |
| 1268 | nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << |
| 1269 | NES_QPCONTEXT_MISC_PCI_FCN_SHIFT); |
| 1270 | nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size << |
| 1271 | NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT); |
| 1272 | nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size << |
| 1273 | NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT); |
Faisal Latif | c12e56e | 2009-03-12 14:34:59 -0700 | [diff] [blame] | 1274 | if (!udata) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1275 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN); |
| 1276 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN); |
Faisal Latif | c12e56e | 2009-03-12 14:34:59 -0700 | [diff] [blame] | 1277 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1278 | nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number + |
| 1279 | ((u32)nesqp->nesrcq->hw_cq.cq_number << 16)); |
| 1280 | u64temp = (u64)nesqp->hwqp.sq_pbase; |
| 1281 | nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp); |
| 1282 | nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32)); |
| 1283 | |
| 1284 | |
| 1285 | if (!virt_wqs) { |
| 1286 | u64temp = (u64)nesqp->hwqp.sq_pbase; |
| 1287 | nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp); |
| 1288 | nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32)); |
| 1289 | u64temp = (u64)nesqp->hwqp.rq_pbase; |
| 1290 | nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp); |
| 1291 | nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32)); |
| 1292 | } else { |
| 1293 | u64temp = (u64)nesqp->pbl_pbase; |
| 1294 | nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp); |
| 1295 | nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32)); |
| 1296 | } |
| 1297 | |
| 1298 | /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n", |
| 1299 | nesvnic->next_qp_nic_index, |
| 1300 | nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */ |
| 1301 | spin_lock_irqsave(&nesdev->cqp.lock, flags); |
| 1302 | nesqp->nesqp_context->misc2 |= cpu_to_le32( |
| 1303 | (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] << |
| 1304 | NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT); |
| 1305 | nesvnic->next_qp_nic_index++; |
| 1306 | if ((nesvnic->next_qp_nic_index > 3) || |
| 1307 | (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) { |
| 1308 | nesvnic->next_qp_nic_index = 0; |
| 1309 | } |
| 1310 | spin_unlock_irqrestore(&nesdev->cqp.lock, flags); |
| 1311 | |
| 1312 | nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16); |
| 1313 | u64temp = (u64)nesqp->hwqp.q2_pbase; |
| 1314 | nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp); |
| 1315 | nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32)); |
| 1316 | nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp))); |
| 1317 | nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp)))); |
| 1318 | nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM | |
Faisal Latif | 883c699 | 2010-03-02 17:22:51 -0600 | [diff] [blame] | 1319 | NES_QPCONTEXT_ORDIRD_AAH | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1320 | ((((u32)nesadapter->max_irrq_wr) << |
| 1321 | NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK)); |
| 1322 | if (disable_mpa_crc) { |
| 1323 | nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n"); |
| 1324 | nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | /* Create the QP */ |
| 1329 | cqp_request = nes_get_cqp_request(nesdev); |
| 1330 | if (cqp_request == NULL) { |
| 1331 | nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n"); |
| 1332 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1333 | nes_free_qp_mem(nesdev, nesqp,virt_wqs); |
| 1334 | kfree(nesqp->allocated_buffer); |
| 1335 | return ERR_PTR(-ENOMEM); |
| 1336 | } |
| 1337 | cqp_request->waiting = 1; |
| 1338 | cqp_wqe = &cqp_request->cqp_wqe; |
| 1339 | |
| 1340 | if (!virt_wqs) { |
| 1341 | opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | |
| 1342 | NES_CQP_QP_IWARP_STATE_IDLE; |
| 1343 | } else { |
| 1344 | opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS | |
| 1345 | NES_CQP_QP_IWARP_STATE_IDLE; |
| 1346 | } |
| 1347 | opcode |= NES_CQP_QP_CQS_VALID; |
| 1348 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 1349 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode); |
| 1350 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id); |
| 1351 | |
| 1352 | u64temp = (u64)nesqp->nesqp_context_pbase; |
| 1353 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp); |
| 1354 | |
| 1355 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1356 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1357 | |
| 1358 | /* Wait for CQP */ |
| 1359 | nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n", |
| 1360 | nesqp->hwqp.qp_id); |
| 1361 | ret = wait_event_timeout(cqp_request->waitq, |
| 1362 | (cqp_request->request_done != 0), NES_EVENT_TIMEOUT); |
| 1363 | nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u," |
| 1364 | " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u," |
| 1365 | " CQP Major:Minor codes = 0x%04X:0x%04X.\n", |
| 1366 | nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail, |
| 1367 | cqp_request->major_code, cqp_request->minor_code); |
| 1368 | if ((!ret) || (cqp_request->major_code)) { |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1369 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1370 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1371 | nes_free_qp_mem(nesdev, nesqp,virt_wqs); |
| 1372 | kfree(nesqp->allocated_buffer); |
| 1373 | if (!ret) { |
| 1374 | return ERR_PTR(-ETIME); |
| 1375 | } else { |
| 1376 | return ERR_PTR(-EIO); |
| 1377 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1380 | nes_put_cqp_request(nesdev, cqp_request); |
| 1381 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1382 | if (ibpd->uobject) { |
| 1383 | uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index; |
| 1384 | uresp.actual_sq_size = sq_size; |
| 1385 | uresp.actual_rq_size = rq_size; |
| 1386 | uresp.qp_id = nesqp->hwqp.qp_id; |
| 1387 | uresp.nes_drv_opt = nes_drv_opt; |
| 1388 | if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) { |
| 1389 | nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num); |
| 1390 | nes_free_qp_mem(nesdev, nesqp,virt_wqs); |
| 1391 | kfree(nesqp->allocated_buffer); |
| 1392 | return ERR_PTR(-EFAULT); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n", |
| 1397 | nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp)); |
| 1398 | spin_lock_init(&nesqp->lock); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1399 | nes_add_ref(&nesqp->ibqp); |
| 1400 | break; |
| 1401 | default: |
| 1402 | nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type); |
| 1403 | return ERR_PTR(-EINVAL); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
Chien Tung | d14152d | 2009-12-09 15:21:56 -0800 | [diff] [blame] | 1406 | nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR); |
| 1407 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1408 | /* update the QP table */ |
| 1409 | nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp; |
| 1410 | nes_debug(NES_DBG_QP, "netdev refcnt=%u\n", |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 1411 | netdev_refcnt_read(nesvnic->netdev)); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1412 | |
| 1413 | return &nesqp->ibqp; |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | /** |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 1418 | * nes_clean_cq |
| 1419 | */ |
| 1420 | static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq) |
| 1421 | { |
| 1422 | u32 cq_head; |
| 1423 | u32 lo; |
| 1424 | u32 hi; |
| 1425 | u64 u64temp; |
| 1426 | unsigned long flags = 0; |
| 1427 | |
| 1428 | spin_lock_irqsave(&nescq->lock, flags); |
| 1429 | |
| 1430 | cq_head = nescq->hw_cq.cq_head; |
| 1431 | while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) { |
| 1432 | rmb(); |
| 1433 | lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]); |
| 1434 | hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]); |
| 1435 | u64temp = (((u64)hi) << 32) | ((u64)lo); |
| 1436 | u64temp &= ~(NES_SW_CONTEXT_ALIGN-1); |
| 1437 | if (u64temp == (u64)(unsigned long)nesqp) { |
| 1438 | /* Zero the context value so cqe will be ignored */ |
| 1439 | nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0; |
| 1440 | nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0; |
| 1441 | } |
| 1442 | |
| 1443 | if (++cq_head >= nescq->hw_cq.cq_size) |
| 1444 | cq_head = 0; |
| 1445 | } |
| 1446 | |
| 1447 | spin_unlock_irqrestore(&nescq->lock, flags); |
| 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | /** |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1452 | * nes_destroy_qp |
| 1453 | */ |
| 1454 | static int nes_destroy_qp(struct ib_qp *ibqp) |
| 1455 | { |
| 1456 | struct nes_qp *nesqp = to_nesqp(ibqp); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1457 | struct nes_ucontext *nes_ucontext; |
| 1458 | struct ib_qp_attr attr; |
| 1459 | struct iw_cm_id *cm_id; |
| 1460 | struct iw_cm_event cm_event; |
Faisal Latif | 0f0bee8 | 2011-09-25 20:34:00 -0500 | [diff] [blame] | 1461 | int ret = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1462 | |
| 1463 | atomic_inc(&sw_qps_destroyed); |
| 1464 | nesqp->destroyed = 1; |
| 1465 | |
| 1466 | /* Blow away the connection if it exists. */ |
| 1467 | if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) { |
| 1468 | /* if (nesqp->ibqp_state == IB_QPS_RTS) { */ |
| 1469 | attr.qp_state = IB_QPS_ERR; |
| 1470 | nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL); |
| 1471 | } |
| 1472 | |
| 1473 | if (((nesqp->ibqp_state == IB_QPS_INIT) || |
| 1474 | (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) { |
| 1475 | cm_id = nesqp->cm_id; |
| 1476 | cm_event.event = IW_CM_EVENT_CONNECT_REPLY; |
Roland Dreier | d0c49bf | 2011-05-09 22:23:57 -0700 | [diff] [blame] | 1477 | cm_event.status = -ETIMEDOUT; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1478 | cm_event.local_addr = cm_id->local_addr; |
| 1479 | cm_event.remote_addr = cm_id->remote_addr; |
| 1480 | cm_event.private_data = NULL; |
| 1481 | cm_event.private_data_len = 0; |
| 1482 | |
| 1483 | nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for " |
| 1484 | "QP%u. cm_id = %p, refcount = %u. \n", |
| 1485 | nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount)); |
| 1486 | |
| 1487 | cm_id->rem_ref(cm_id); |
| 1488 | ret = cm_id->event_handler(cm_id, &cm_event); |
| 1489 | if (ret) |
| 1490 | nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret); |
| 1491 | } |
| 1492 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1493 | if (nesqp->user_mode) { |
| 1494 | if ((ibqp->uobject)&&(ibqp->uobject->context)) { |
| 1495 | nes_ucontext = to_nesucontext(ibqp->uobject->context); |
| 1496 | clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs); |
| 1497 | nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL; |
| 1498 | if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) { |
| 1499 | nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index; |
| 1500 | } |
| 1501 | } |
Faisal Latif | d2fa9b2 | 2009-12-09 15:54:28 -0800 | [diff] [blame] | 1502 | if (nesqp->pbl_pbase && nesqp->sq_kmapped) { |
| 1503 | nesqp->sq_kmapped = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1504 | kunmap(nesqp->page); |
Faisal Latif | d2fa9b2 | 2009-12-09 15:54:28 -0800 | [diff] [blame] | 1505 | } |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 1506 | } else { |
| 1507 | /* Clean any pending completions from the cq(s) */ |
| 1508 | if (nesqp->nesscq) |
| 1509 | nes_clean_cq(nesqp, nesqp->nesscq); |
| 1510 | |
| 1511 | if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq)) |
| 1512 | nes_clean_cq(nesqp, nesqp->nesrcq); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1513 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1514 | nes_rem_ref(&nesqp->ibqp); |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | /** |
| 1520 | * nes_create_cq |
| 1521 | */ |
| 1522 | static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries, |
| 1523 | int comp_vector, |
| 1524 | struct ib_ucontext *context, struct ib_udata *udata) |
| 1525 | { |
| 1526 | u64 u64temp; |
| 1527 | struct nes_vnic *nesvnic = to_nesvnic(ibdev); |
| 1528 | struct nes_device *nesdev = nesvnic->nesdev; |
| 1529 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 1530 | struct nes_cq *nescq; |
| 1531 | struct nes_ucontext *nes_ucontext = NULL; |
| 1532 | struct nes_cqp_request *cqp_request; |
| 1533 | void *mem = NULL; |
| 1534 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 1535 | struct nes_pbl *nespbl = NULL; |
| 1536 | struct nes_create_cq_req req; |
| 1537 | struct nes_create_cq_resp resp; |
| 1538 | u32 cq_num = 0; |
| 1539 | u32 opcode = 0; |
| 1540 | u32 pbl_entries = 1; |
| 1541 | int err; |
| 1542 | unsigned long flags; |
| 1543 | int ret; |
| 1544 | |
Chien Tung | 5924aea | 2009-12-09 15:21:56 -0800 | [diff] [blame] | 1545 | if (entries > nesadapter->max_cqe) |
| 1546 | return ERR_PTR(-EINVAL); |
| 1547 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1548 | err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs, |
| 1549 | nesadapter->max_cq, &cq_num, &nesadapter->next_cq); |
| 1550 | if (err) { |
| 1551 | return ERR_PTR(err); |
| 1552 | } |
| 1553 | |
| 1554 | nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL); |
| 1555 | if (!nescq) { |
| 1556 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1557 | nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n"); |
| 1558 | return ERR_PTR(-ENOMEM); |
| 1559 | } |
| 1560 | |
| 1561 | nescq->hw_cq.cq_size = max(entries + 1, 5); |
| 1562 | nescq->hw_cq.cq_number = cq_num; |
| 1563 | nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1; |
| 1564 | |
| 1565 | |
| 1566 | if (context) { |
| 1567 | nes_ucontext = to_nesucontext(context); |
| 1568 | if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) { |
| 1569 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1570 | kfree(nescq); |
| 1571 | return ERR_PTR(-EFAULT); |
| 1572 | } |
| 1573 | nesvnic->mcrq_ucontext = nes_ucontext; |
| 1574 | nes_ucontext->mcrqf = req.mcrqf; |
| 1575 | if (nes_ucontext->mcrqf) { |
| 1576 | if (nes_ucontext->mcrqf & 0x80000000) |
Vadim Makhervaks | 2e36954 | 2008-11-02 21:39:17 -0800 | [diff] [blame] | 1577 | nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1578 | else if (nes_ucontext->mcrqf & 0x40000000) |
| 1579 | nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff; |
| 1580 | else |
| 1581 | nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1; |
Miroslaw Walukiewicz | 5d1af5c | 2009-04-21 16:16:48 -0700 | [diff] [blame] | 1582 | nescq->mcrqf = nes_ucontext->mcrqf; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1583 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1584 | } |
| 1585 | nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n", |
| 1586 | (unsigned long)req.user_cq_buffer, entries); |
Chien Tung | 9300c0c | 2008-02-21 07:51:17 -0600 | [diff] [blame] | 1587 | err = 1; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1588 | list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) { |
| 1589 | if (nespbl->user_base == (unsigned long )req.user_cq_buffer) { |
| 1590 | list_del(&nespbl->list); |
| 1591 | err = 0; |
| 1592 | nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n", |
| 1593 | nespbl); |
| 1594 | break; |
| 1595 | } |
| 1596 | } |
| 1597 | if (err) { |
| 1598 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1599 | kfree(nescq); |
Chien Tung | 9300c0c | 2008-02-21 07:51:17 -0600 | [diff] [blame] | 1600 | return ERR_PTR(-EFAULT); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | pbl_entries = nespbl->pbl_size >> 3; |
| 1604 | nescq->cq_mem_size = 0; |
| 1605 | } else { |
| 1606 | nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe); |
| 1607 | nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n", |
| 1608 | entries, nescq->cq_mem_size, nescq->hw_cq.cq_number); |
| 1609 | |
| 1610 | /* allocate the physical buffer space */ |
| 1611 | mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size, |
| 1612 | &nescq->hw_cq.cq_pbase); |
| 1613 | if (!mem) { |
| 1614 | printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n"); |
| 1615 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1616 | kfree(nescq); |
| 1617 | return ERR_PTR(-ENOMEM); |
| 1618 | } |
| 1619 | |
| 1620 | memset(mem, 0, nescq->cq_mem_size); |
| 1621 | nescq->hw_cq.cq_vbase = mem; |
| 1622 | nescq->hw_cq.cq_head = 0; |
| 1623 | nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n", |
| 1624 | nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase, |
| 1625 | (u32)nescq->hw_cq.cq_pbase); |
| 1626 | } |
| 1627 | |
| 1628 | nescq->hw_cq.ce_handler = nes_iwarp_ce_handler; |
| 1629 | spin_lock_init(&nescq->lock); |
| 1630 | |
| 1631 | /* send CreateCQ request to CQP */ |
| 1632 | cqp_request = nes_get_cqp_request(nesdev); |
| 1633 | if (cqp_request == NULL) { |
| 1634 | nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n"); |
| 1635 | if (!context) |
| 1636 | pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem, |
| 1637 | nescq->hw_cq.cq_pbase); |
Miroslaw Walukiewicz | 5d1af5c | 2009-04-21 16:16:48 -0700 | [diff] [blame] | 1638 | else { |
| 1639 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, |
| 1640 | nespbl->pbl_vbase, nespbl->pbl_pbase); |
| 1641 | kfree(nespbl); |
| 1642 | } |
| 1643 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1644 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1645 | kfree(nescq); |
| 1646 | return ERR_PTR(-ENOMEM); |
| 1647 | } |
| 1648 | cqp_request->waiting = 1; |
| 1649 | cqp_wqe = &cqp_request->cqp_wqe; |
| 1650 | |
| 1651 | opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID | |
| 1652 | NES_CQP_CQ_CHK_OVERFLOW | |
| 1653 | NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16); |
| 1654 | |
| 1655 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 1656 | |
| 1657 | if (pbl_entries != 1) { |
| 1658 | if (pbl_entries > 32) { |
| 1659 | /* use 4k pbl */ |
| 1660 | nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries); |
| 1661 | if (nesadapter->free_4kpbl == 0) { |
Faisal Latif | c5d321e | 2008-11-21 20:50:38 -0600 | [diff] [blame] | 1662 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1663 | nes_free_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1664 | if (!context) |
| 1665 | pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem, |
| 1666 | nescq->hw_cq.cq_pbase); |
Miroslaw Walukiewicz | 5d1af5c | 2009-04-21 16:16:48 -0700 | [diff] [blame] | 1667 | else { |
| 1668 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, |
| 1669 | nespbl->pbl_vbase, nespbl->pbl_pbase); |
| 1670 | kfree(nespbl); |
| 1671 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1672 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1673 | kfree(nescq); |
| 1674 | return ERR_PTR(-ENOMEM); |
| 1675 | } else { |
| 1676 | opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK); |
| 1677 | nescq->virtual_cq = 2; |
| 1678 | nesadapter->free_4kpbl--; |
| 1679 | } |
| 1680 | } else { |
| 1681 | /* use 256 byte pbl */ |
| 1682 | nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries); |
| 1683 | if (nesadapter->free_256pbl == 0) { |
Faisal Latif | c5d321e | 2008-11-21 20:50:38 -0600 | [diff] [blame] | 1684 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1685 | nes_free_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1686 | if (!context) |
| 1687 | pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem, |
| 1688 | nescq->hw_cq.cq_pbase); |
Miroslaw Walukiewicz | 5d1af5c | 2009-04-21 16:16:48 -0700 | [diff] [blame] | 1689 | else { |
| 1690 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, |
| 1691 | nespbl->pbl_vbase, nespbl->pbl_pbase); |
| 1692 | kfree(nespbl); |
| 1693 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1694 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1695 | kfree(nescq); |
| 1696 | return ERR_PTR(-ENOMEM); |
| 1697 | } else { |
| 1698 | opcode |= NES_CQP_CQ_VIRT; |
| 1699 | nescq->virtual_cq = 1; |
| 1700 | nesadapter->free_256pbl--; |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1706 | |
| 1707 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 1708 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode); |
| 1709 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, |
| 1710 | (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16))); |
| 1711 | |
| 1712 | if (context) { |
| 1713 | if (pbl_entries != 1) |
| 1714 | u64temp = (u64)nespbl->pbl_pbase; |
| 1715 | else |
| 1716 | u64temp = le64_to_cpu(nespbl->pbl_vbase[0]); |
| 1717 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX, |
| 1718 | nes_ucontext->mmap_db_index[0]); |
| 1719 | } else { |
| 1720 | u64temp = (u64)nescq->hw_cq.cq_pbase; |
| 1721 | cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0; |
| 1722 | } |
| 1723 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp); |
| 1724 | cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0; |
| 1725 | u64temp = (u64)(unsigned long)&nescq->hw_cq; |
| 1726 | cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] = |
| 1727 | cpu_to_le32((u32)(u64temp >> 1)); |
| 1728 | cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = |
| 1729 | cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF); |
| 1730 | |
| 1731 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1732 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1733 | |
| 1734 | /* Wait for CQP */ |
| 1735 | nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n", |
| 1736 | nescq->hw_cq.cq_number); |
| 1737 | ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done), |
| 1738 | NES_EVENT_TIMEOUT * 2); |
| 1739 | nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n", |
| 1740 | nescq->hw_cq.cq_number, ret); |
| 1741 | if ((!ret) || (cqp_request->major_code)) { |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1742 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1743 | if (!context) |
| 1744 | pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem, |
| 1745 | nescq->hw_cq.cq_pbase); |
Miroslaw Walukiewicz | 5d1af5c | 2009-04-21 16:16:48 -0700 | [diff] [blame] | 1746 | else { |
| 1747 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, |
| 1748 | nespbl->pbl_vbase, nespbl->pbl_pbase); |
| 1749 | kfree(nespbl); |
| 1750 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1751 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1752 | kfree(nescq); |
| 1753 | return ERR_PTR(-EIO); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1754 | } |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1755 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1756 | |
| 1757 | if (context) { |
| 1758 | /* free the nespbl */ |
| 1759 | pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase, |
| 1760 | nespbl->pbl_pbase); |
| 1761 | kfree(nespbl); |
| 1762 | resp.cq_id = nescq->hw_cq.cq_number; |
| 1763 | resp.cq_size = nescq->hw_cq.cq_size; |
| 1764 | resp.mmap_db_index = 0; |
| 1765 | if (ib_copy_to_udata(udata, &resp, sizeof resp)) { |
| 1766 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num); |
| 1767 | kfree(nescq); |
| 1768 | return ERR_PTR(-EFAULT); |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | return &nescq->ibcq; |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | /** |
| 1777 | * nes_destroy_cq |
| 1778 | */ |
| 1779 | static int nes_destroy_cq(struct ib_cq *ib_cq) |
| 1780 | { |
| 1781 | struct nes_cq *nescq; |
| 1782 | struct nes_device *nesdev; |
| 1783 | struct nes_vnic *nesvnic; |
| 1784 | struct nes_adapter *nesadapter; |
| 1785 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 1786 | struct nes_cqp_request *cqp_request; |
| 1787 | unsigned long flags; |
| 1788 | u32 opcode = 0; |
| 1789 | int ret; |
| 1790 | |
| 1791 | if (ib_cq == NULL) |
| 1792 | return 0; |
| 1793 | |
| 1794 | nescq = to_nescq(ib_cq); |
| 1795 | nesvnic = to_nesvnic(ib_cq->device); |
| 1796 | nesdev = nesvnic->nesdev; |
| 1797 | nesadapter = nesdev->nesadapter; |
| 1798 | |
| 1799 | nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number); |
| 1800 | |
| 1801 | /* Send DestroyCQ request to CQP */ |
| 1802 | cqp_request = nes_get_cqp_request(nesdev); |
| 1803 | if (cqp_request == NULL) { |
| 1804 | nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n"); |
| 1805 | return -ENOMEM; |
| 1806 | } |
| 1807 | cqp_request->waiting = 1; |
| 1808 | cqp_wqe = &cqp_request->cqp_wqe; |
| 1809 | opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16); |
| 1810 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 1811 | if (nescq->virtual_cq == 1) { |
| 1812 | nesadapter->free_256pbl++; |
| 1813 | if (nesadapter->free_256pbl > nesadapter->max_256pbl) { |
| 1814 | printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n", |
Harvey Harrison | 3371836 | 2008-04-16 21:01:10 -0700 | [diff] [blame] | 1815 | __func__, nesadapter->free_256pbl, nesadapter->max_256pbl); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1816 | } |
| 1817 | } else if (nescq->virtual_cq == 2) { |
| 1818 | nesadapter->free_4kpbl++; |
| 1819 | if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) { |
| 1820 | printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n", |
Harvey Harrison | 3371836 | 2008-04-16 21:01:10 -0700 | [diff] [blame] | 1821 | __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1822 | } |
| 1823 | opcode |= NES_CQP_CQ_4KB_CHUNK; |
| 1824 | } |
| 1825 | |
| 1826 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1827 | |
| 1828 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 1829 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode); |
| 1830 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, |
| 1831 | (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16))); |
Miroslaw Walukiewicz | 5d1af5c | 2009-04-21 16:16:48 -0700 | [diff] [blame] | 1832 | if (!nescq->mcrqf) |
| 1833 | nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number); |
| 1834 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1835 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1836 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1837 | |
| 1838 | /* Wait for CQP */ |
| 1839 | nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n", |
| 1840 | nescq->hw_cq.cq_number); |
| 1841 | ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done), |
| 1842 | NES_EVENT_TIMEOUT); |
| 1843 | nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u," |
| 1844 | " CQP Major:Minor codes = 0x%04X:0x%04X.\n", |
| 1845 | nescq->hw_cq.cq_number, ret, cqp_request->major_code, |
| 1846 | cqp_request->minor_code); |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1847 | if (!ret) { |
| 1848 | nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n", |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1849 | nescq->hw_cq.cq_number); |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1850 | ret = -ETIME; |
| 1851 | } else if (cqp_request->major_code) { |
| 1852 | nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n", |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1853 | nescq->hw_cq.cq_number); |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1854 | ret = -EIO; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1855 | } else { |
| 1856 | ret = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1857 | } |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 1858 | nes_put_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1859 | |
| 1860 | if (nescq->cq_mem_size) |
| 1861 | pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, |
Roland Dreier | 31d1e34 | 2008-04-23 11:55:45 -0700 | [diff] [blame] | 1862 | nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1863 | kfree(nescq); |
| 1864 | |
| 1865 | return ret; |
| 1866 | } |
| 1867 | |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1868 | /** |
| 1869 | * root_256 |
| 1870 | */ |
| 1871 | static u32 root_256(struct nes_device *nesdev, |
| 1872 | struct nes_root_vpbl *root_vpbl, |
| 1873 | struct nes_root_vpbl *new_root, |
Matt Kraai | cc005fa | 2009-04-21 10:43:21 -0700 | [diff] [blame] | 1874 | u16 pbl_count_4k) |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1875 | { |
| 1876 | u64 leaf_pbl; |
| 1877 | int i, j, k; |
| 1878 | |
| 1879 | if (pbl_count_4k == 1) { |
| 1880 | new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, |
| 1881 | 512, &new_root->pbl_pbase); |
| 1882 | |
| 1883 | if (new_root->pbl_vbase == NULL) |
| 1884 | return 0; |
| 1885 | |
| 1886 | leaf_pbl = (u64)root_vpbl->pbl_pbase; |
| 1887 | for (i = 0; i < 16; i++) { |
| 1888 | new_root->pbl_vbase[i].pa_low = |
| 1889 | cpu_to_le32((u32)leaf_pbl); |
| 1890 | new_root->pbl_vbase[i].pa_high = |
| 1891 | cpu_to_le32((u32)((((u64)leaf_pbl) >> 32))); |
| 1892 | leaf_pbl += 256; |
| 1893 | } |
| 1894 | } else { |
| 1895 | for (i = 3; i >= 0; i--) { |
| 1896 | j = i * 16; |
| 1897 | root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i]; |
| 1898 | leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) + |
| 1899 | (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high)) |
| 1900 | << 32); |
| 1901 | for (k = 1; k < 16; k++) { |
| 1902 | leaf_pbl += 256; |
| 1903 | root_vpbl->pbl_vbase[j + k].pa_low = |
| 1904 | cpu_to_le32((u32)leaf_pbl); |
| 1905 | root_vpbl->pbl_vbase[j + k].pa_high = |
| 1906 | cpu_to_le32((u32)((((u64)leaf_pbl) >> 32))); |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | return 1; |
| 1912 | } |
| 1913 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1914 | |
| 1915 | /** |
| 1916 | * nes_reg_mr |
| 1917 | */ |
| 1918 | static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd, |
| 1919 | u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl, |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1920 | dma_addr_t single_buffer, u16 pbl_count_4k, |
| 1921 | u16 residual_page_count_4k, int acc, u64 *iova_start, |
| 1922 | u16 *actual_pbl_cnt, u8 *used_4k_pbls) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1923 | { |
| 1924 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 1925 | struct nes_cqp_request *cqp_request; |
| 1926 | unsigned long flags; |
| 1927 | int ret; |
| 1928 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1929 | uint pg_cnt = 0; |
Chien Tung | 1af9222 | 2009-04-20 14:50:36 -0700 | [diff] [blame] | 1930 | u16 pbl_count_256 = 0; |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1931 | u16 pbl_count = 0; |
| 1932 | u8 use_256_pbls = 0; |
| 1933 | u8 use_4k_pbls = 0; |
| 1934 | u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0; |
Or Gerlitz | 812d867 | 2010-07-20 12:00:20 +0000 | [diff] [blame] | 1935 | struct nes_root_vpbl new_root = { 0, NULL, NULL }; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1936 | u32 opcode = 0; |
| 1937 | u16 major_code; |
| 1938 | |
| 1939 | /* Register the region with the adapter */ |
| 1940 | cqp_request = nes_get_cqp_request(nesdev); |
| 1941 | if (cqp_request == NULL) { |
| 1942 | nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n"); |
| 1943 | return -ENOMEM; |
| 1944 | } |
| 1945 | cqp_request->waiting = 1; |
| 1946 | cqp_wqe = &cqp_request->cqp_wqe; |
| 1947 | |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1948 | if (pbl_count_4k) { |
| 1949 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 1950 | |
| 1951 | pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k; |
| 1952 | pbl_count_256 = (pg_cnt + 31) / 32; |
| 1953 | if (pg_cnt <= 32) { |
| 1954 | if (pbl_count_256 <= nesadapter->free_256pbl) |
| 1955 | use_256_pbls = 1; |
| 1956 | else if (pbl_count_4k <= nesadapter->free_4kpbl) |
| 1957 | use_4k_pbls = 1; |
| 1958 | } else if (pg_cnt <= 2048) { |
| 1959 | if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) && |
| 1960 | (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) { |
| 1961 | use_4k_pbls = 1; |
| 1962 | } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) { |
| 1963 | use_256_pbls = 1; |
| 1964 | use_two_level = 1; |
| 1965 | } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) { |
| 1966 | use_4k_pbls = 1; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1967 | } |
| 1968 | } else { |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1969 | if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl) |
| 1970 | use_4k_pbls = 1; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1971 | } |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1972 | |
| 1973 | if (use_256_pbls) { |
| 1974 | pbl_count = pbl_count_256; |
| 1975 | nesadapter->free_256pbl -= pbl_count + use_two_level; |
| 1976 | } else if (use_4k_pbls) { |
| 1977 | pbl_count = pbl_count_4k; |
| 1978 | nesadapter->free_4kpbl -= pbl_count + use_two_level; |
| 1979 | } else { |
| 1980 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 1981 | nes_debug(NES_DBG_MR, "Out of Pbls\n"); |
| 1982 | nes_free_cqp_request(nesdev, cqp_request); |
| 1983 | return -ENOMEM; |
| 1984 | } |
| 1985 | |
| 1986 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1989 | if (use_256_pbls && use_two_level) { |
Matt Kraai | cc005fa | 2009-04-21 10:43:21 -0700 | [diff] [blame] | 1990 | if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) { |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 1991 | if (new_root.pbl_pbase != 0) |
| 1992 | root_vpbl = &new_root; |
| 1993 | } else { |
| 1994 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 1995 | nesadapter->free_256pbl += pbl_count_256 + use_two_level; |
| 1996 | use_256_pbls = 0; |
| 1997 | |
| 1998 | if (pbl_count_4k == 1) |
| 1999 | use_two_level = 0; |
| 2000 | pbl_count = pbl_count_4k; |
| 2001 | |
| 2002 | if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) { |
| 2003 | nesadapter->free_4kpbl -= pbl_count + use_two_level; |
| 2004 | use_4k_pbls = 1; |
| 2005 | } |
| 2006 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 2007 | |
| 2008 | if (use_4k_pbls == 0) |
| 2009 | return -ENOMEM; |
| 2010 | } |
| 2011 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2012 | |
| 2013 | opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ | |
| 2014 | NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR; |
| 2015 | if (acc & IB_ACCESS_LOCAL_WRITE) |
| 2016 | opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE; |
| 2017 | if (acc & IB_ACCESS_REMOTE_WRITE) |
| 2018 | opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN; |
| 2019 | if (acc & IB_ACCESS_REMOTE_READ) |
| 2020 | opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN; |
| 2021 | if (acc & IB_ACCESS_MW_BIND) |
| 2022 | opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN; |
| 2023 | |
| 2024 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 2025 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode); |
| 2026 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start); |
| 2027 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length); |
| 2028 | |
| 2029 | cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] = |
| 2030 | cpu_to_le32((u32)(region_length >> 8) & 0xff000000); |
| 2031 | cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |= |
| 2032 | cpu_to_le32(nespd->pd_id & 0x00007fff); |
| 2033 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag); |
| 2034 | |
| 2035 | if (pbl_count == 0) { |
| 2036 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer); |
| 2037 | } else { |
| 2038 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase); |
| 2039 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count); |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2040 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8)); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2041 | |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2042 | if (use_4k_pbls) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2043 | cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE); |
| 2044 | } |
| 2045 | barrier(); |
| 2046 | |
| 2047 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 2048 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2049 | |
| 2050 | /* Wait for CQP */ |
| 2051 | ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done), |
| 2052 | NES_EVENT_TIMEOUT); |
| 2053 | nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u," |
| 2054 | " CQP Major:Minor codes = 0x%04X:0x%04X.\n", |
| 2055 | stag, ret, cqp_request->major_code, cqp_request->minor_code); |
| 2056 | major_code = cqp_request->major_code; |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 2057 | nes_put_cqp_request(nesdev, cqp_request); |
| 2058 | |
Don Wood | dae5d13 | 2009-03-06 15:12:09 -0800 | [diff] [blame] | 2059 | if ((!ret || major_code) && pbl_count != 0) { |
| 2060 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2061 | if (use_256_pbls) |
| 2062 | nesadapter->free_256pbl += pbl_count + use_two_level; |
| 2063 | else if (use_4k_pbls) |
| 2064 | nesadapter->free_4kpbl += pbl_count + use_two_level; |
Don Wood | dae5d13 | 2009-03-06 15:12:09 -0800 | [diff] [blame] | 2065 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 2066 | } |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2067 | if (new_root.pbl_pbase) |
| 2068 | pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase, |
| 2069 | new_root.pbl_pbase); |
| 2070 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2071 | if (!ret) |
| 2072 | return -ETIME; |
| 2073 | else if (major_code) |
| 2074 | return -EIO; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2075 | |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2076 | *actual_pbl_cnt = pbl_count + use_two_level; |
| 2077 | *used_4k_pbls = use_4k_pbls; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2078 | return 0; |
| 2079 | } |
| 2080 | |
| 2081 | |
| 2082 | /** |
| 2083 | * nes_reg_phys_mr |
| 2084 | */ |
| 2085 | static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, |
| 2086 | struct ib_phys_buf *buffer_list, int num_phys_buf, int acc, |
| 2087 | u64 * iova_start) |
| 2088 | { |
| 2089 | u64 region_length; |
| 2090 | struct nes_pd *nespd = to_nespd(ib_pd); |
| 2091 | struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device); |
| 2092 | struct nes_device *nesdev = nesvnic->nesdev; |
| 2093 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 2094 | struct nes_mr *nesmr; |
| 2095 | struct ib_mr *ibmr; |
| 2096 | struct nes_vpbl vpbl; |
| 2097 | struct nes_root_vpbl root_vpbl; |
| 2098 | u32 stag; |
| 2099 | u32 i; |
Don Wood | 3f32eb1 | 2009-04-20 14:53:00 -0700 | [diff] [blame] | 2100 | unsigned long mask; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2101 | u32 stag_index = 0; |
| 2102 | u32 next_stag_index = 0; |
| 2103 | u32 driver_key = 0; |
| 2104 | u32 root_pbl_index = 0; |
| 2105 | u32 cur_pbl_index = 0; |
Roland Dreier | 69d5102 | 2010-08-04 14:25:40 -0700 | [diff] [blame] | 2106 | int err = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2107 | int ret = 0; |
| 2108 | u16 pbl_count = 0; |
| 2109 | u8 single_page = 1; |
| 2110 | u8 stag_key = 0; |
| 2111 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2112 | region_length = 0; |
| 2113 | vpbl.pbl_vbase = NULL; |
| 2114 | root_vpbl.pbl_vbase = NULL; |
| 2115 | root_vpbl.pbl_pbase = 0; |
| 2116 | |
| 2117 | get_random_bytes(&next_stag_index, sizeof(next_stag_index)); |
| 2118 | stag_key = (u8)next_stag_index; |
| 2119 | |
| 2120 | driver_key = 0; |
| 2121 | |
| 2122 | next_stag_index >>= 8; |
| 2123 | next_stag_index %= nesadapter->max_mr; |
| 2124 | if (num_phys_buf > (1024*512)) { |
| 2125 | return ERR_PTR(-E2BIG); |
| 2126 | } |
| 2127 | |
Don Wood | 3f32eb1 | 2009-04-20 14:53:00 -0700 | [diff] [blame] | 2128 | if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK) |
| 2129 | return ERR_PTR(-EINVAL); |
| 2130 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2131 | err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr, |
| 2132 | &stag_index, &next_stag_index); |
| 2133 | if (err) { |
| 2134 | return ERR_PTR(err); |
| 2135 | } |
| 2136 | |
| 2137 | nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL); |
| 2138 | if (!nesmr) { |
| 2139 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2140 | return ERR_PTR(-ENOMEM); |
| 2141 | } |
| 2142 | |
| 2143 | for (i = 0; i < num_phys_buf; i++) { |
| 2144 | |
| 2145 | if ((i & 0x01FF) == 0) { |
| 2146 | if (root_pbl_index == 1) { |
| 2147 | /* Allocate the root PBL */ |
| 2148 | root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192, |
| 2149 | &root_vpbl.pbl_pbase); |
| 2150 | nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n", |
| 2151 | root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase); |
| 2152 | if (!root_vpbl.pbl_vbase) { |
| 2153 | pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, |
| 2154 | vpbl.pbl_pbase); |
| 2155 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2156 | kfree(nesmr); |
| 2157 | return ERR_PTR(-ENOMEM); |
| 2158 | } |
| 2159 | root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL); |
| 2160 | if (!root_vpbl.leaf_vpbl) { |
| 2161 | pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase, |
| 2162 | root_vpbl.pbl_pbase); |
| 2163 | pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, |
| 2164 | vpbl.pbl_pbase); |
| 2165 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2166 | kfree(nesmr); |
| 2167 | return ERR_PTR(-ENOMEM); |
| 2168 | } |
| 2169 | root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase); |
| 2170 | root_vpbl.pbl_vbase[0].pa_high = |
| 2171 | cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32))); |
| 2172 | root_vpbl.leaf_vpbl[0] = vpbl; |
| 2173 | } |
| 2174 | /* Allocate a 4K buffer for the PBL */ |
| 2175 | vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096, |
| 2176 | &vpbl.pbl_pbase); |
| 2177 | nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n", |
| 2178 | vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase); |
| 2179 | if (!vpbl.pbl_vbase) { |
| 2180 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2181 | ibmr = ERR_PTR(-ENOMEM); |
| 2182 | kfree(nesmr); |
| 2183 | goto reg_phys_err; |
| 2184 | } |
| 2185 | /* Fill in the root table */ |
| 2186 | if (1 <= root_pbl_index) { |
| 2187 | root_vpbl.pbl_vbase[root_pbl_index].pa_low = |
| 2188 | cpu_to_le32((u32)vpbl.pbl_pbase); |
| 2189 | root_vpbl.pbl_vbase[root_pbl_index].pa_high = |
| 2190 | cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32))); |
| 2191 | root_vpbl.leaf_vpbl[root_pbl_index] = vpbl; |
| 2192 | } |
| 2193 | root_pbl_index++; |
| 2194 | cur_pbl_index = 0; |
| 2195 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2196 | |
Don Wood | 3f32eb1 | 2009-04-20 14:53:00 -0700 | [diff] [blame] | 2197 | mask = !buffer_list[i].size; |
| 2198 | if (i != 0) |
| 2199 | mask |= buffer_list[i].addr; |
| 2200 | if (i != num_phys_buf - 1) |
| 2201 | mask |= buffer_list[i].addr + buffer_list[i].size; |
| 2202 | |
| 2203 | if (mask & ~PAGE_MASK) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2204 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
Don Wood | 3f32eb1 | 2009-04-20 14:53:00 -0700 | [diff] [blame] | 2205 | nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n"); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2206 | ibmr = ERR_PTR(-EINVAL); |
| 2207 | kfree(nesmr); |
| 2208 | goto reg_phys_err; |
| 2209 | } |
| 2210 | |
| 2211 | region_length += buffer_list[i].size; |
| 2212 | if ((i != 0) && (single_page)) { |
| 2213 | if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr) |
| 2214 | single_page = 0; |
| 2215 | } |
Don Wood | 3f32eb1 | 2009-04-20 14:53:00 -0700 | [diff] [blame] | 2216 | vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2217 | vpbl.pbl_vbase[cur_pbl_index++].pa_high = |
| 2218 | cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32))); |
| 2219 | } |
| 2220 | |
| 2221 | stag = stag_index << 8; |
| 2222 | stag |= driver_key; |
| 2223 | stag += (u32)stag_key; |
| 2224 | |
| 2225 | nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX," |
| 2226 | " length = 0x%016lX, index = 0x%08X\n", |
| 2227 | stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index); |
| 2228 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2229 | /* Make the leaf PBL the root if only one PBL */ |
| 2230 | if (root_pbl_index == 1) { |
| 2231 | root_vpbl.pbl_pbase = vpbl.pbl_pbase; |
| 2232 | } |
| 2233 | |
| 2234 | if (single_page) { |
| 2235 | pbl_count = 0; |
| 2236 | } else { |
| 2237 | pbl_count = root_pbl_index; |
| 2238 | } |
| 2239 | ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl, |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2240 | buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start, |
| 2241 | &nesmr->pbls_used, &nesmr->pbl_4k); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2242 | |
| 2243 | if (ret == 0) { |
| 2244 | nesmr->ibmr.rkey = stag; |
| 2245 | nesmr->ibmr.lkey = stag; |
| 2246 | nesmr->mode = IWNES_MEMREG_TYPE_MEM; |
| 2247 | ibmr = &nesmr->ibmr; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2248 | } else { |
| 2249 | kfree(nesmr); |
| 2250 | ibmr = ERR_PTR(-ENOMEM); |
| 2251 | } |
| 2252 | |
| 2253 | reg_phys_err: |
| 2254 | /* free the resources */ |
| 2255 | if (root_pbl_index == 1) { |
| 2256 | /* single PBL case */ |
| 2257 | pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase); |
| 2258 | } else { |
| 2259 | for (i=0; i<root_pbl_index; i++) { |
| 2260 | pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase, |
| 2261 | root_vpbl.leaf_vpbl[i].pbl_pbase); |
| 2262 | } |
| 2263 | kfree(root_vpbl.leaf_vpbl); |
| 2264 | pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase, |
| 2265 | root_vpbl.pbl_pbase); |
| 2266 | } |
| 2267 | |
| 2268 | return ibmr; |
| 2269 | } |
| 2270 | |
| 2271 | |
| 2272 | /** |
| 2273 | * nes_get_dma_mr |
| 2274 | */ |
| 2275 | static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc) |
| 2276 | { |
| 2277 | struct ib_phys_buf bl; |
| 2278 | u64 kva = 0; |
| 2279 | |
| 2280 | nes_debug(NES_DBG_MR, "\n"); |
| 2281 | |
| 2282 | bl.size = (u64)0xffffffffffULL; |
| 2283 | bl.addr = 0; |
| 2284 | return nes_reg_phys_mr(pd, &bl, 1, acc, &kva); |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | /** |
| 2289 | * nes_reg_user_mr |
| 2290 | */ |
| 2291 | static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length, |
| 2292 | u64 virt, int acc, struct ib_udata *udata) |
| 2293 | { |
| 2294 | u64 iova_start; |
| 2295 | __le64 *pbl; |
| 2296 | u64 region_length; |
| 2297 | dma_addr_t last_dma_addr = 0; |
| 2298 | dma_addr_t first_dma_addr = 0; |
| 2299 | struct nes_pd *nespd = to_nespd(pd); |
| 2300 | struct nes_vnic *nesvnic = to_nesvnic(pd->device); |
| 2301 | struct nes_device *nesdev = nesvnic->nesdev; |
| 2302 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 2303 | struct ib_mr *ibmr = ERR_PTR(-EINVAL); |
| 2304 | struct ib_umem_chunk *chunk; |
| 2305 | struct nes_ucontext *nes_ucontext; |
| 2306 | struct nes_pbl *nespbl; |
| 2307 | struct nes_mr *nesmr; |
| 2308 | struct ib_umem *region; |
| 2309 | struct nes_mem_reg_req req; |
| 2310 | struct nes_vpbl vpbl; |
| 2311 | struct nes_root_vpbl root_vpbl; |
| 2312 | int nmap_index, page_index; |
| 2313 | int page_count = 0; |
| 2314 | int err, pbl_depth = 0; |
| 2315 | int chunk_pages; |
| 2316 | int ret; |
| 2317 | u32 stag; |
| 2318 | u32 stag_index = 0; |
| 2319 | u32 next_stag_index; |
| 2320 | u32 driver_key; |
| 2321 | u32 root_pbl_index = 0; |
| 2322 | u32 cur_pbl_index = 0; |
| 2323 | u32 skip_pages; |
| 2324 | u16 pbl_count; |
| 2325 | u8 single_page = 1; |
| 2326 | u8 stag_key; |
| 2327 | |
Arthur Kepner | cb9fbc5 | 2008-04-29 01:00:34 -0700 | [diff] [blame] | 2328 | region = ib_umem_get(pd->uobject->context, start, length, acc, 0); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2329 | if (IS_ERR(region)) { |
| 2330 | return (struct ib_mr *)region; |
| 2331 | } |
| 2332 | |
| 2333 | nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u," |
| 2334 | " offset = %u, page size = %u.\n", |
| 2335 | (unsigned long int)start, (unsigned long int)virt, (u32)length, |
| 2336 | region->offset, region->page_size); |
| 2337 | |
| 2338 | skip_pages = ((u32)region->offset) >> 12; |
| 2339 | |
Julia Lawall | e2e435f | 2011-10-06 09:33:24 -0700 | [diff] [blame] | 2340 | if (ib_copy_from_udata(&req, udata, sizeof(req))) { |
| 2341 | ib_umem_release(region); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2342 | return ERR_PTR(-EFAULT); |
Julia Lawall | e2e435f | 2011-10-06 09:33:24 -0700 | [diff] [blame] | 2343 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2344 | nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type); |
| 2345 | |
| 2346 | switch (req.reg_type) { |
| 2347 | case IWNES_MEMREG_TYPE_MEM: |
| 2348 | pbl_depth = 0; |
| 2349 | region_length = 0; |
| 2350 | vpbl.pbl_vbase = NULL; |
| 2351 | root_vpbl.pbl_vbase = NULL; |
| 2352 | root_vpbl.pbl_pbase = 0; |
| 2353 | |
| 2354 | get_random_bytes(&next_stag_index, sizeof(next_stag_index)); |
| 2355 | stag_key = (u8)next_stag_index; |
| 2356 | |
| 2357 | driver_key = next_stag_index & 0x70000000; |
| 2358 | |
| 2359 | next_stag_index >>= 8; |
| 2360 | next_stag_index %= nesadapter->max_mr; |
| 2361 | |
| 2362 | err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, |
| 2363 | nesadapter->max_mr, &stag_index, &next_stag_index); |
| 2364 | if (err) { |
| 2365 | ib_umem_release(region); |
| 2366 | return ERR_PTR(err); |
| 2367 | } |
| 2368 | |
| 2369 | nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL); |
| 2370 | if (!nesmr) { |
| 2371 | ib_umem_release(region); |
| 2372 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2373 | return ERR_PTR(-ENOMEM); |
| 2374 | } |
| 2375 | nesmr->region = region; |
| 2376 | |
| 2377 | list_for_each_entry(chunk, ®ion->chunk_list, list) { |
| 2378 | nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n", |
| 2379 | chunk->nents, chunk->nmap); |
| 2380 | for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) { |
| 2381 | if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) { |
| 2382 | ib_umem_release(region); |
| 2383 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2384 | nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n", |
| 2385 | (unsigned int) sg_dma_address(&chunk->page_list[nmap_index])); |
| 2386 | ibmr = ERR_PTR(-EINVAL); |
| 2387 | kfree(nesmr); |
| 2388 | goto reg_user_mr_err; |
| 2389 | } |
| 2390 | |
| 2391 | if (!sg_dma_len(&chunk->page_list[nmap_index])) { |
| 2392 | ib_umem_release(region); |
| 2393 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, |
| 2394 | stag_index); |
| 2395 | nes_debug(NES_DBG_MR, "Invalid Buffer Size\n"); |
| 2396 | ibmr = ERR_PTR(-EINVAL); |
| 2397 | kfree(nesmr); |
| 2398 | goto reg_user_mr_err; |
| 2399 | } |
| 2400 | |
| 2401 | region_length += sg_dma_len(&chunk->page_list[nmap_index]); |
| 2402 | chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12; |
| 2403 | region_length -= skip_pages << 12; |
| 2404 | for (page_index=skip_pages; page_index < chunk_pages; page_index++) { |
| 2405 | skip_pages = 0; |
| 2406 | if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length) |
| 2407 | goto enough_pages; |
| 2408 | if ((page_count&0x01FF) == 0) { |
Roland Dreier | 24797a3 | 2008-06-10 12:29:49 -0700 | [diff] [blame] | 2409 | if (page_count >= 1024 * 512) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2410 | ib_umem_release(region); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2411 | nes_free_resource(nesadapter, |
| 2412 | nesadapter->allocated_mrs, stag_index); |
| 2413 | kfree(nesmr); |
| 2414 | ibmr = ERR_PTR(-E2BIG); |
| 2415 | goto reg_user_mr_err; |
| 2416 | } |
| 2417 | if (root_pbl_index == 1) { |
| 2418 | root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, |
| 2419 | 8192, &root_vpbl.pbl_pbase); |
| 2420 | nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n", |
| 2421 | root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase); |
| 2422 | if (!root_vpbl.pbl_vbase) { |
| 2423 | ib_umem_release(region); |
| 2424 | pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, |
| 2425 | vpbl.pbl_pbase); |
| 2426 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, |
| 2427 | stag_index); |
| 2428 | kfree(nesmr); |
| 2429 | ibmr = ERR_PTR(-ENOMEM); |
| 2430 | goto reg_user_mr_err; |
| 2431 | } |
| 2432 | root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, |
| 2433 | GFP_KERNEL); |
| 2434 | if (!root_vpbl.leaf_vpbl) { |
| 2435 | ib_umem_release(region); |
| 2436 | pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase, |
| 2437 | root_vpbl.pbl_pbase); |
| 2438 | pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, |
| 2439 | vpbl.pbl_pbase); |
| 2440 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, |
| 2441 | stag_index); |
| 2442 | kfree(nesmr); |
| 2443 | ibmr = ERR_PTR(-ENOMEM); |
| 2444 | goto reg_user_mr_err; |
| 2445 | } |
| 2446 | root_vpbl.pbl_vbase[0].pa_low = |
| 2447 | cpu_to_le32((u32)vpbl.pbl_pbase); |
| 2448 | root_vpbl.pbl_vbase[0].pa_high = |
| 2449 | cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32))); |
| 2450 | root_vpbl.leaf_vpbl[0] = vpbl; |
| 2451 | } |
| 2452 | vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096, |
| 2453 | &vpbl.pbl_pbase); |
| 2454 | nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n", |
| 2455 | vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase); |
| 2456 | if (!vpbl.pbl_vbase) { |
| 2457 | ib_umem_release(region); |
| 2458 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index); |
| 2459 | ibmr = ERR_PTR(-ENOMEM); |
| 2460 | kfree(nesmr); |
| 2461 | goto reg_user_mr_err; |
| 2462 | } |
| 2463 | if (1 <= root_pbl_index) { |
| 2464 | root_vpbl.pbl_vbase[root_pbl_index].pa_low = |
| 2465 | cpu_to_le32((u32)vpbl.pbl_pbase); |
| 2466 | root_vpbl.pbl_vbase[root_pbl_index].pa_high = |
| 2467 | cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32))); |
| 2468 | root_vpbl.leaf_vpbl[root_pbl_index] = vpbl; |
| 2469 | } |
| 2470 | root_pbl_index++; |
| 2471 | cur_pbl_index = 0; |
| 2472 | } |
| 2473 | if (single_page) { |
| 2474 | if (page_count != 0) { |
| 2475 | if ((last_dma_addr+4096) != |
| 2476 | (sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2477 | (page_index*4096))) |
| 2478 | single_page = 0; |
| 2479 | last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2480 | (page_index*4096); |
| 2481 | } else { |
| 2482 | first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2483 | (page_index*4096); |
| 2484 | last_dma_addr = first_dma_addr; |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | vpbl.pbl_vbase[cur_pbl_index].pa_low = |
| 2489 | cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2490 | (page_index*4096))); |
| 2491 | vpbl.pbl_vbase[cur_pbl_index].pa_high = |
| 2492 | cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2493 | (page_index*4096))) >> 32))); |
| 2494 | cur_pbl_index++; |
| 2495 | page_count++; |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | enough_pages: |
| 2500 | nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x," |
| 2501 | " stag_key=0x%08x\n", |
| 2502 | stag_index, driver_key, stag_key); |
| 2503 | stag = stag_index << 8; |
| 2504 | stag |= driver_key; |
| 2505 | stag += (u32)stag_key; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2506 | |
| 2507 | iova_start = virt; |
| 2508 | /* Make the leaf PBL the root if only one PBL */ |
| 2509 | if (root_pbl_index == 1) { |
| 2510 | root_vpbl.pbl_pbase = vpbl.pbl_pbase; |
| 2511 | } |
| 2512 | |
| 2513 | if (single_page) { |
| 2514 | pbl_count = 0; |
| 2515 | } else { |
| 2516 | pbl_count = root_pbl_index; |
| 2517 | first_dma_addr = 0; |
| 2518 | } |
| 2519 | nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X," |
| 2520 | " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n", |
| 2521 | stag, (unsigned int)iova_start, |
| 2522 | (unsigned int)region_length, stag_index, |
| 2523 | (unsigned long long)region->length, pbl_count); |
Don Wood | 0145f34 | 2009-03-06 15:15:00 -0800 | [diff] [blame] | 2524 | ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl, |
| 2525 | first_dma_addr, pbl_count, (u16)cur_pbl_index, acc, |
| 2526 | &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2527 | |
| 2528 | nes_debug(NES_DBG_MR, "ret=%d\n", ret); |
| 2529 | |
| 2530 | if (ret == 0) { |
| 2531 | nesmr->ibmr.rkey = stag; |
| 2532 | nesmr->ibmr.lkey = stag; |
| 2533 | nesmr->mode = IWNES_MEMREG_TYPE_MEM; |
| 2534 | ibmr = &nesmr->ibmr; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2535 | } else { |
| 2536 | ib_umem_release(region); |
| 2537 | kfree(nesmr); |
| 2538 | ibmr = ERR_PTR(-ENOMEM); |
| 2539 | } |
| 2540 | |
| 2541 | reg_user_mr_err: |
| 2542 | /* free the resources */ |
| 2543 | if (root_pbl_index == 1) { |
| 2544 | pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, |
| 2545 | vpbl.pbl_pbase); |
| 2546 | } else { |
| 2547 | for (page_index=0; page_index<root_pbl_index; page_index++) { |
| 2548 | pci_free_consistent(nesdev->pcidev, 4096, |
| 2549 | root_vpbl.leaf_vpbl[page_index].pbl_vbase, |
| 2550 | root_vpbl.leaf_vpbl[page_index].pbl_pbase); |
| 2551 | } |
| 2552 | kfree(root_vpbl.leaf_vpbl); |
| 2553 | pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase, |
| 2554 | root_vpbl.pbl_pbase); |
| 2555 | } |
| 2556 | |
| 2557 | nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr); |
| 2558 | |
| 2559 | return ibmr; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2560 | case IWNES_MEMREG_TYPE_QP: |
| 2561 | case IWNES_MEMREG_TYPE_CQ: |
| 2562 | nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL); |
| 2563 | if (!nespbl) { |
| 2564 | nes_debug(NES_DBG_MR, "Unable to allocate PBL\n"); |
| 2565 | ib_umem_release(region); |
| 2566 | return ERR_PTR(-ENOMEM); |
| 2567 | } |
| 2568 | nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL); |
| 2569 | if (!nesmr) { |
| 2570 | ib_umem_release(region); |
| 2571 | kfree(nespbl); |
| 2572 | nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n"); |
| 2573 | return ERR_PTR(-ENOMEM); |
| 2574 | } |
| 2575 | nesmr->region = region; |
| 2576 | nes_ucontext = to_nesucontext(pd->uobject->context); |
| 2577 | pbl_depth = region->length >> 12; |
| 2578 | pbl_depth += (region->length & (4096-1)) ? 1 : 0; |
| 2579 | nespbl->pbl_size = pbl_depth*sizeof(u64); |
| 2580 | if (req.reg_type == IWNES_MEMREG_TYPE_QP) { |
| 2581 | nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory"); |
| 2582 | } else { |
| 2583 | nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory"); |
| 2584 | } |
| 2585 | |
| 2586 | nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n", |
| 2587 | nespbl->pbl_size, pbl_depth); |
| 2588 | pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size, |
| 2589 | &nespbl->pbl_pbase); |
| 2590 | if (!pbl) { |
| 2591 | ib_umem_release(region); |
| 2592 | kfree(nesmr); |
| 2593 | kfree(nespbl); |
| 2594 | nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n"); |
| 2595 | return ERR_PTR(-ENOMEM); |
| 2596 | } |
| 2597 | |
| 2598 | nespbl->pbl_vbase = (u64 *)pbl; |
| 2599 | nespbl->user_base = start; |
Roland Dreier | 71e0957 | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 2600 | nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx," |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2601 | " pbl_vbase=%p user_base=0x%lx\n", |
Roland Dreier | 71e0957 | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 2602 | nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase, |
| 2603 | (void *) nespbl->pbl_vbase, nespbl->user_base); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2604 | |
| 2605 | list_for_each_entry(chunk, ®ion->chunk_list, list) { |
| 2606 | for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) { |
| 2607 | chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12; |
| 2608 | chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0; |
| 2609 | nespbl->page = sg_page(&chunk->page_list[0]); |
| 2610 | for (page_index=0; page_index<chunk_pages; page_index++) { |
| 2611 | ((__le32 *)pbl)[0] = cpu_to_le32((u32) |
| 2612 | (sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2613 | (page_index*4096))); |
| 2614 | ((__le32 *)pbl)[1] = cpu_to_le32(((u64) |
| 2615 | (sg_dma_address(&chunk->page_list[nmap_index])+ |
| 2616 | (page_index*4096)))>>32); |
| 2617 | nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl, |
| 2618 | (unsigned long long)*pbl, |
| 2619 | le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0])); |
| 2620 | pbl++; |
| 2621 | } |
| 2622 | } |
| 2623 | } |
| 2624 | if (req.reg_type == IWNES_MEMREG_TYPE_QP) { |
| 2625 | list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list); |
| 2626 | } else { |
| 2627 | list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list); |
| 2628 | } |
| 2629 | nesmr->ibmr.rkey = -1; |
| 2630 | nesmr->ibmr.lkey = -1; |
| 2631 | nesmr->mode = req.reg_type; |
| 2632 | return &nesmr->ibmr; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2633 | } |
| 2634 | |
Julia Lawall | e2e435f | 2011-10-06 09:33:24 -0700 | [diff] [blame] | 2635 | ib_umem_release(region); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2636 | return ERR_PTR(-ENOSYS); |
| 2637 | } |
| 2638 | |
| 2639 | |
| 2640 | /** |
| 2641 | * nes_dereg_mr |
| 2642 | */ |
| 2643 | static int nes_dereg_mr(struct ib_mr *ib_mr) |
| 2644 | { |
| 2645 | struct nes_mr *nesmr = to_nesmr(ib_mr); |
| 2646 | struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device); |
| 2647 | struct nes_device *nesdev = nesvnic->nesdev; |
| 2648 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 2649 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 2650 | struct nes_cqp_request *cqp_request; |
| 2651 | unsigned long flags; |
| 2652 | int ret; |
| 2653 | u16 major_code; |
| 2654 | u16 minor_code; |
| 2655 | |
| 2656 | if (nesmr->region) { |
| 2657 | ib_umem_release(nesmr->region); |
| 2658 | } |
| 2659 | if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) { |
| 2660 | kfree(nesmr); |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | /* Deallocate the region with the adapter */ |
| 2665 | |
| 2666 | cqp_request = nes_get_cqp_request(nesdev); |
| 2667 | if (cqp_request == NULL) { |
| 2668 | nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n"); |
| 2669 | return -ENOMEM; |
| 2670 | } |
| 2671 | cqp_request->waiting = 1; |
| 2672 | cqp_wqe = &cqp_request->cqp_wqe; |
| 2673 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2674 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 2675 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, |
| 2676 | NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO | |
| 2677 | NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR); |
| 2678 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey); |
| 2679 | |
| 2680 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 2681 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2682 | |
| 2683 | /* Wait for CQP */ |
| 2684 | nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey); |
| 2685 | ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0), |
| 2686 | NES_EVENT_TIMEOUT); |
| 2687 | nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u," |
| 2688 | " CQP Major:Minor codes = 0x%04X:0x%04X\n", |
| 2689 | ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code); |
| 2690 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2691 | major_code = cqp_request->major_code; |
| 2692 | minor_code = cqp_request->minor_code; |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 2693 | |
| 2694 | nes_put_cqp_request(nesdev, cqp_request); |
| 2695 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2696 | if (!ret) { |
| 2697 | nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag," |
| 2698 | " ib_mr=%p, rkey = 0x%08X\n", |
| 2699 | ib_mr, ib_mr->rkey); |
| 2700 | return -ETIME; |
| 2701 | } else if (major_code) { |
| 2702 | nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting" |
| 2703 | " to destroy STag, ib_mr=%p, rkey = 0x%08X\n", |
| 2704 | major_code, minor_code, ib_mr, ib_mr->rkey); |
| 2705 | return -EIO; |
Don Wood | dae5d13 | 2009-03-06 15:12:09 -0800 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | if (nesmr->pbls_used != 0) { |
| 2709 | spin_lock_irqsave(&nesadapter->pbl_lock, flags); |
| 2710 | if (nesmr->pbl_4k) { |
| 2711 | nesadapter->free_4kpbl += nesmr->pbls_used; |
| 2712 | if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) |
| 2713 | printk(KERN_ERR PFX "free 4KB PBLs(%u) has " |
| 2714 | "exceeded the max(%u)\n", |
| 2715 | nesadapter->free_4kpbl, |
| 2716 | nesadapter->max_4kpbl); |
| 2717 | } else { |
| 2718 | nesadapter->free_256pbl += nesmr->pbls_used; |
| 2719 | if (nesadapter->free_256pbl > nesadapter->max_256pbl) |
| 2720 | printk(KERN_ERR PFX "free 256B PBLs(%u) has " |
| 2721 | "exceeded the max(%u)\n", |
| 2722 | nesadapter->free_256pbl, |
| 2723 | nesadapter->max_256pbl); |
| 2724 | } |
| 2725 | spin_unlock_irqrestore(&nesadapter->pbl_lock, flags); |
| 2726 | } |
| 2727 | nes_free_resource(nesadapter, nesadapter->allocated_mrs, |
| 2728 | (ib_mr->rkey & 0x0fffff00) >> 8); |
| 2729 | |
| 2730 | kfree(nesmr); |
| 2731 | |
| 2732 | return 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | |
| 2736 | /** |
| 2737 | * show_rev |
| 2738 | */ |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2739 | static ssize_t show_rev(struct device *dev, struct device_attribute *attr, |
| 2740 | char *buf) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2741 | { |
| 2742 | struct nes_ib_device *nesibdev = |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2743 | container_of(dev, struct nes_ib_device, ibdev.dev); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2744 | struct nes_vnic *nesvnic = nesibdev->nesvnic; |
| 2745 | |
| 2746 | nes_debug(NES_DBG_INIT, "\n"); |
| 2747 | return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev); |
| 2748 | } |
| 2749 | |
| 2750 | |
| 2751 | /** |
| 2752 | * show_fw_ver |
| 2753 | */ |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2754 | static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr, |
| 2755 | char *buf) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2756 | { |
| 2757 | struct nes_ib_device *nesibdev = |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2758 | container_of(dev, struct nes_ib_device, ibdev.dev); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2759 | struct nes_vnic *nesvnic = nesibdev->nesvnic; |
| 2760 | |
| 2761 | nes_debug(NES_DBG_INIT, "\n"); |
Chien Tung | 0e4562d | 2009-04-27 13:33:48 -0700 | [diff] [blame] | 2762 | return sprintf(buf, "%u.%u\n", |
| 2763 | (nesvnic->nesdev->nesadapter->firmware_version >> 16), |
| 2764 | (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff)); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | |
| 2768 | /** |
| 2769 | * show_hca |
| 2770 | */ |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2771 | static ssize_t show_hca(struct device *dev, struct device_attribute *attr, |
| 2772 | char *buf) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2773 | { |
| 2774 | nes_debug(NES_DBG_INIT, "\n"); |
| 2775 | return sprintf(buf, "NES020\n"); |
| 2776 | } |
| 2777 | |
| 2778 | |
| 2779 | /** |
| 2780 | * show_board |
| 2781 | */ |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2782 | static ssize_t show_board(struct device *dev, struct device_attribute *attr, |
| 2783 | char *buf) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2784 | { |
| 2785 | nes_debug(NES_DBG_INIT, "\n"); |
| 2786 | return sprintf(buf, "%.*s\n", 32, "NES020 Board ID"); |
| 2787 | } |
| 2788 | |
| 2789 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2790 | static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); |
| 2791 | static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL); |
| 2792 | static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); |
| 2793 | static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2794 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 2795 | static struct device_attribute *nes_dev_attributes[] = { |
| 2796 | &dev_attr_hw_rev, |
| 2797 | &dev_attr_fw_ver, |
| 2798 | &dev_attr_hca_type, |
| 2799 | &dev_attr_board_id |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2800 | }; |
| 2801 | |
| 2802 | |
| 2803 | /** |
| 2804 | * nes_query_qp |
| 2805 | */ |
| 2806 | static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 2807 | int attr_mask, struct ib_qp_init_attr *init_attr) |
| 2808 | { |
| 2809 | struct nes_qp *nesqp = to_nesqp(ibqp); |
| 2810 | |
| 2811 | nes_debug(NES_DBG_QP, "\n"); |
| 2812 | |
| 2813 | attr->qp_access_flags = 0; |
| 2814 | attr->cap.max_send_wr = nesqp->hwqp.sq_size; |
| 2815 | attr->cap.max_recv_wr = nesqp->hwqp.rq_size; |
| 2816 | attr->cap.max_recv_sge = 1; |
Chien Tung | eadde3a | 2010-03-25 13:39:50 +0000 | [diff] [blame] | 2817 | if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) |
| 2818 | attr->cap.max_inline_data = 0; |
| 2819 | else |
| 2820 | attr->cap.max_inline_data = 64; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2821 | |
| 2822 | init_attr->event_handler = nesqp->ibqp.event_handler; |
| 2823 | init_attr->qp_context = nesqp->ibqp.qp_context; |
| 2824 | init_attr->send_cq = nesqp->ibqp.send_cq; |
| 2825 | init_attr->recv_cq = nesqp->ibqp.recv_cq; |
| 2826 | init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq; |
| 2827 | init_attr->cap = attr->cap; |
| 2828 | |
| 2829 | return 0; |
| 2830 | } |
| 2831 | |
| 2832 | |
| 2833 | /** |
| 2834 | * nes_hw_modify_qp |
| 2835 | */ |
| 2836 | int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp, |
Don Wood | 8b1c9dc | 2009-09-05 20:36:38 -0700 | [diff] [blame] | 2837 | u32 next_iwarp_state, u32 termlen, u32 wait_completion) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2838 | { |
| 2839 | struct nes_hw_cqp_wqe *cqp_wqe; |
| 2840 | /* struct iw_cm_id *cm_id = nesqp->cm_id; */ |
| 2841 | /* struct iw_cm_event cm_event; */ |
| 2842 | struct nes_cqp_request *cqp_request; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2843 | int ret; |
| 2844 | u16 major_code; |
| 2845 | |
| 2846 | nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n", |
| 2847 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount)); |
| 2848 | |
| 2849 | cqp_request = nes_get_cqp_request(nesdev); |
| 2850 | if (cqp_request == NULL) { |
| 2851 | nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n"); |
| 2852 | return -ENOMEM; |
| 2853 | } |
| 2854 | if (wait_completion) { |
| 2855 | cqp_request->waiting = 1; |
| 2856 | } else { |
| 2857 | cqp_request->waiting = 0; |
| 2858 | } |
| 2859 | cqp_wqe = &cqp_request->cqp_wqe; |
| 2860 | |
| 2861 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, |
| 2862 | NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state); |
| 2863 | nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n", |
| 2864 | next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX])); |
| 2865 | nes_fill_init_cqp_wqe(cqp_wqe, nesdev); |
| 2866 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id); |
| 2867 | set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase); |
| 2868 | |
Don Wood | 8b1c9dc | 2009-09-05 20:36:38 -0700 | [diff] [blame] | 2869 | /* If sending a terminate message, fill in the length (in words) */ |
| 2870 | if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) && |
| 2871 | !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) { |
| 2872 | termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT; |
| 2873 | set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen); |
| 2874 | } |
| 2875 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2876 | atomic_set(&cqp_request->refcount, 2); |
Roland Dreier | 8294f29 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 2877 | nes_post_cqp_request(nesdev, cqp_request); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2878 | |
| 2879 | /* Wait for CQP */ |
| 2880 | if (wait_completion) { |
| 2881 | /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n", |
| 2882 | nesqp->hwqp.qp_id); */ |
| 2883 | ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0), |
| 2884 | NES_EVENT_TIMEOUT); |
| 2885 | nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, " |
| 2886 | "CQP Major:Minor codes = 0x%04X:0x%04X.\n", |
| 2887 | nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code); |
| 2888 | major_code = cqp_request->major_code; |
| 2889 | if (major_code) { |
| 2890 | nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed" |
| 2891 | "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n", |
| 2892 | nesqp->hwqp.qp_id, cqp_request->major_code, |
| 2893 | cqp_request->minor_code, next_iwarp_state); |
| 2894 | } |
Roland Dreier | 1ff66e8 | 2008-07-14 23:48:49 -0700 | [diff] [blame] | 2895 | |
| 2896 | nes_put_cqp_request(nesdev, cqp_request); |
| 2897 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2898 | if (!ret) |
| 2899 | return -ETIME; |
| 2900 | else if (major_code) |
| 2901 | return -EIO; |
| 2902 | else |
| 2903 | return 0; |
| 2904 | } else { |
| 2905 | return 0; |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | |
| 2910 | /** |
| 2911 | * nes_modify_qp |
| 2912 | */ |
| 2913 | int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 2914 | int attr_mask, struct ib_udata *udata) |
| 2915 | { |
| 2916 | struct nes_qp *nesqp = to_nesqp(ibqp); |
| 2917 | struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); |
| 2918 | struct nes_device *nesdev = nesvnic->nesdev; |
| 2919 | /* u32 cqp_head; */ |
| 2920 | /* u32 counter; */ |
| 2921 | u32 next_iwarp_state = 0; |
| 2922 | int err; |
| 2923 | unsigned long qplockflags; |
| 2924 | int ret; |
| 2925 | u16 original_last_aeq; |
| 2926 | u8 issue_modify_qp = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2927 | u8 dont_wait = 0; |
| 2928 | |
| 2929 | nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u," |
| 2930 | " iwarp_state=0x%X, refcount=%d\n", |
| 2931 | nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state, |
| 2932 | nesqp->iwarp_state, atomic_read(&nesqp->refcount)); |
| 2933 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2934 | spin_lock_irqsave(&nesqp->lock, qplockflags); |
| 2935 | |
| 2936 | nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X," |
| 2937 | " QP Access Flags=0x%X, attr_mask = 0x%0x\n", |
| 2938 | nesqp->hwqp.qp_id, nesqp->hw_iwarp_state, |
| 2939 | nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask); |
| 2940 | |
| 2941 | if (attr_mask & IB_QP_STATE) { |
| 2942 | switch (attr->qp_state) { |
| 2943 | case IB_QPS_INIT: |
| 2944 | nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n", |
| 2945 | nesqp->hwqp.qp_id); |
| 2946 | if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) { |
| 2947 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2948 | return -EINVAL; |
| 2949 | } |
| 2950 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE; |
| 2951 | issue_modify_qp = 1; |
| 2952 | break; |
| 2953 | case IB_QPS_RTR: |
| 2954 | nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n", |
| 2955 | nesqp->hwqp.qp_id); |
| 2956 | if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) { |
| 2957 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2958 | return -EINVAL; |
| 2959 | } |
| 2960 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE; |
| 2961 | issue_modify_qp = 1; |
| 2962 | break; |
| 2963 | case IB_QPS_RTS: |
| 2964 | nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n", |
| 2965 | nesqp->hwqp.qp_id); |
| 2966 | if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) { |
| 2967 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2968 | return -EINVAL; |
| 2969 | } |
| 2970 | if (nesqp->cm_id == NULL) { |
| 2971 | nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n", |
| 2972 | nesqp->hwqp.qp_id ); |
| 2973 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2974 | return -EINVAL; |
| 2975 | } |
| 2976 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS; |
| 2977 | if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS) |
| 2978 | next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID | |
| 2979 | NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID; |
| 2980 | issue_modify_qp = 1; |
| 2981 | nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED; |
| 2982 | nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS; |
| 2983 | nesqp->hte_added = 1; |
| 2984 | break; |
| 2985 | case IB_QPS_SQD: |
| 2986 | issue_modify_qp = 1; |
| 2987 | nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n", |
| 2988 | nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail); |
| 2989 | if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) { |
| 2990 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2991 | return 0; |
| 2992 | } else { |
| 2993 | if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) { |
| 2994 | nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing" |
| 2995 | " ignored due to current iWARP state\n", |
| 2996 | nesqp->hwqp.qp_id); |
| 2997 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 2998 | return -EINVAL; |
| 2999 | } |
| 3000 | if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) { |
| 3001 | nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing" |
| 3002 | " already done based on hw state.\n", |
| 3003 | nesqp->hwqp.qp_id); |
| 3004 | issue_modify_qp = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3005 | } |
| 3006 | switch (nesqp->hw_iwarp_state) { |
| 3007 | case NES_AEQE_IWARP_STATE_CLOSING: |
| 3008 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING; |
| 3009 | case NES_AEQE_IWARP_STATE_TERMINATE: |
| 3010 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE; |
| 3011 | break; |
| 3012 | case NES_AEQE_IWARP_STATE_ERROR: |
| 3013 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR; |
| 3014 | break; |
| 3015 | default: |
| 3016 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3017 | nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING; |
| 3018 | break; |
| 3019 | } |
| 3020 | } |
| 3021 | break; |
| 3022 | case IB_QPS_SQE: |
| 3023 | nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n", |
| 3024 | nesqp->hwqp.qp_id); |
| 3025 | if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) { |
| 3026 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3027 | return -EINVAL; |
| 3028 | } |
| 3029 | /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */ |
| 3030 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE; |
| 3031 | nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE; |
| 3032 | issue_modify_qp = 1; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3033 | break; |
| 3034 | case IB_QPS_ERR: |
| 3035 | case IB_QPS_RESET: |
| 3036 | if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) { |
| 3037 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3038 | return -EINVAL; |
| 3039 | } |
| 3040 | nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n", |
| 3041 | nesqp->hwqp.qp_id); |
Don Wood | 8b1c9dc | 2009-09-05 20:36:38 -0700 | [diff] [blame] | 3042 | if (nesqp->term_flags) |
| 3043 | del_timer(&nesqp->terminate_timer); |
| 3044 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3045 | next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR; |
| 3046 | /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */ |
| 3047 | if (nesqp->hte_added) { |
| 3048 | nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n"); |
| 3049 | next_iwarp_state |= NES_CQP_QP_DEL_HTE; |
| 3050 | nesqp->hte_added = 0; |
| 3051 | } |
| 3052 | if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) && |
Faisal Latif | cd6860e | 2010-07-04 00:17:59 +0000 | [diff] [blame] | 3053 | (nesdev->iw_status) && |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3054 | (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) { |
| 3055 | next_iwarp_state |= NES_CQP_QP_RESET; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3056 | } else { |
| 3057 | nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n", |
| 3058 | nesqp->hwqp.qp_id, nesqp->hw_tcp_state); |
| 3059 | dont_wait = 1; |
| 3060 | } |
| 3061 | issue_modify_qp = 1; |
| 3062 | nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR; |
| 3063 | break; |
| 3064 | default: |
| 3065 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3066 | return -EINVAL; |
| 3067 | break; |
| 3068 | } |
| 3069 | |
| 3070 | nesqp->ibqp_state = attr->qp_state; |
| 3071 | if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == |
| 3072 | (u32)NES_CQP_QP_IWARP_STATE_RTS) && |
| 3073 | ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) > |
| 3074 | (u32)NES_CQP_QP_IWARP_STATE_RTS)) { |
| 3075 | nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK; |
| 3076 | nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n", |
| 3077 | nesqp->iwarp_state); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3078 | } else { |
| 3079 | nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK; |
| 3080 | nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n", |
| 3081 | nesqp->iwarp_state); |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | if (attr_mask & IB_QP_ACCESS_FLAGS) { |
| 3086 | if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) { |
| 3087 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN | |
| 3088 | NES_QPCONTEXT_MISC_RDMA_READ_EN); |
| 3089 | issue_modify_qp = 1; |
| 3090 | } |
| 3091 | if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) { |
| 3092 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN); |
| 3093 | issue_modify_qp = 1; |
| 3094 | } |
| 3095 | if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) { |
| 3096 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN); |
| 3097 | issue_modify_qp = 1; |
| 3098 | } |
| 3099 | if (attr->qp_access_flags & IB_ACCESS_MW_BIND) { |
| 3100 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN); |
| 3101 | issue_modify_qp = 1; |
| 3102 | } |
| 3103 | |
| 3104 | if (nesqp->user_mode) { |
| 3105 | nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN | |
| 3106 | NES_QPCONTEXT_MISC_RDMA_READ_EN); |
| 3107 | issue_modify_qp = 1; |
| 3108 | } |
| 3109 | } |
| 3110 | |
| 3111 | original_last_aeq = nesqp->last_aeq; |
| 3112 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
| 3113 | |
| 3114 | nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp); |
| 3115 | |
| 3116 | ret = 0; |
| 3117 | |
| 3118 | |
| 3119 | if (issue_modify_qp) { |
| 3120 | nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n"); |
Don Wood | 8b1c9dc | 2009-09-05 20:36:38 -0700 | [diff] [blame] | 3121 | ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3122 | if (ret) |
| 3123 | nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)" |
| 3124 | " failed for QP%u.\n", |
| 3125 | next_iwarp_state, nesqp->hwqp.qp_id); |
| 3126 | |
| 3127 | } |
| 3128 | |
| 3129 | if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) { |
| 3130 | nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d)," |
| 3131 | " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n", |
| 3132 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount), |
| 3133 | original_last_aeq, nesqp->last_aeq); |
| 3134 | if ((!ret) || |
| 3135 | ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) && |
| 3136 | (ret))) { |
| 3137 | if (dont_wait) { |
| 3138 | if (nesqp->cm_id && nesqp->hw_tcp_state != 0) { |
| 3139 | nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d)," |
| 3140 | " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n", |
| 3141 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount), |
| 3142 | original_last_aeq, nesqp->last_aeq); |
| 3143 | /* this one is for the cm_disconnect thread */ |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3144 | spin_lock_irqsave(&nesqp->lock, qplockflags); |
| 3145 | nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED; |
| 3146 | nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT; |
| 3147 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
| 3148 | nes_cm_disconn(nesqp); |
| 3149 | } else { |
| 3150 | nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n", |
| 3151 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount)); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3152 | } |
| 3153 | } else { |
| 3154 | spin_lock_irqsave(&nesqp->lock, qplockflags); |
| 3155 | if (nesqp->cm_id) { |
| 3156 | /* These two are for the timer thread */ |
| 3157 | if (atomic_inc_return(&nesqp->close_timer_started) == 1) { |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3158 | nesqp->cm_id->add_ref(nesqp->cm_id); |
| 3159 | nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d)," |
| 3160 | " need ae to finish up, original_last_aeq = 0x%04X." |
| 3161 | " last_aeq = 0x%04X, scheduling timer.\n", |
| 3162 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount), |
| 3163 | original_last_aeq, nesqp->last_aeq); |
| 3164 | schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0); |
| 3165 | } |
| 3166 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
| 3167 | } else { |
| 3168 | spin_unlock_irqrestore(&nesqp->lock, qplockflags); |
| 3169 | nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d)," |
| 3170 | " need ae to finish up, original_last_aeq = 0x%04X." |
| 3171 | " last_aeq = 0x%04X.\n", |
| 3172 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount), |
| 3173 | original_last_aeq, nesqp->last_aeq); |
| 3174 | } |
| 3175 | } |
| 3176 | } else { |
| 3177 | nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up," |
| 3178 | " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n", |
| 3179 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount), |
| 3180 | original_last_aeq, nesqp->last_aeq); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3181 | } |
| 3182 | } else { |
| 3183 | nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up," |
| 3184 | " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n", |
| 3185 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount), |
| 3186 | original_last_aeq, nesqp->last_aeq); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | err = 0; |
| 3190 | |
| 3191 | nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n", |
| 3192 | nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount)); |
| 3193 | |
| 3194 | return err; |
| 3195 | } |
| 3196 | |
| 3197 | |
| 3198 | /** |
| 3199 | * nes_muticast_attach |
| 3200 | */ |
| 3201 | static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) |
| 3202 | { |
| 3203 | nes_debug(NES_DBG_INIT, "\n"); |
| 3204 | return -ENOSYS; |
| 3205 | } |
| 3206 | |
| 3207 | |
| 3208 | /** |
| 3209 | * nes_multicast_detach |
| 3210 | */ |
| 3211 | static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) |
| 3212 | { |
| 3213 | nes_debug(NES_DBG_INIT, "\n"); |
| 3214 | return -ENOSYS; |
| 3215 | } |
| 3216 | |
| 3217 | |
| 3218 | /** |
| 3219 | * nes_process_mad |
| 3220 | */ |
| 3221 | static int nes_process_mad(struct ib_device *ibdev, int mad_flags, |
| 3222 | u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh, |
| 3223 | struct ib_mad *in_mad, struct ib_mad *out_mad) |
| 3224 | { |
| 3225 | nes_debug(NES_DBG_INIT, "\n"); |
| 3226 | return -ENOSYS; |
| 3227 | } |
| 3228 | |
| 3229 | static inline void |
| 3230 | fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey) |
| 3231 | { |
| 3232 | int sge_index; |
| 3233 | int total_payload_length = 0; |
| 3234 | for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) { |
| 3235 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4), |
| 3236 | ib_wr->sg_list[sge_index].addr); |
| 3237 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4), |
| 3238 | ib_wr->sg_list[sge_index].length); |
| 3239 | if (uselkey) |
| 3240 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), |
| 3241 | (ib_wr->sg_list[sge_index].lkey)); |
| 3242 | else |
| 3243 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0); |
| 3244 | |
| 3245 | total_payload_length += ib_wr->sg_list[sge_index].length; |
| 3246 | } |
| 3247 | nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n", |
| 3248 | total_payload_length); |
| 3249 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX, |
| 3250 | total_payload_length); |
| 3251 | } |
| 3252 | |
| 3253 | /** |
| 3254 | * nes_post_send |
| 3255 | */ |
| 3256 | static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr, |
| 3257 | struct ib_send_wr **bad_wr) |
| 3258 | { |
| 3259 | u64 u64temp; |
| 3260 | unsigned long flags = 0; |
| 3261 | struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); |
| 3262 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3263 | struct nes_qp *nesqp = to_nesqp(ibqp); |
| 3264 | struct nes_hw_qp_wqe *wqe; |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3265 | int err = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3266 | u32 qsize = nesqp->hwqp.sq_size; |
| 3267 | u32 head; |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3268 | u32 wqe_misc = 0; |
| 3269 | u32 wqe_count = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3270 | u32 counter; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3271 | |
Frank Zago | e5dec39 | 2009-12-09 13:51:36 -0800 | [diff] [blame] | 3272 | if (nesqp->ibqp_state > IB_QPS_RTS) { |
| 3273 | err = -EINVAL; |
| 3274 | goto out; |
| 3275 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3276 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 3277 | spin_lock_irqsave(&nesqp->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3278 | |
| 3279 | head = nesqp->hwqp.sq_head; |
| 3280 | |
| 3281 | while (ib_wr) { |
Don Wood | 8b1c9dc | 2009-09-05 20:36:38 -0700 | [diff] [blame] | 3282 | /* Check for QP error */ |
| 3283 | if (nesqp->term_flags) { |
| 3284 | err = -EINVAL; |
| 3285 | break; |
| 3286 | } |
| 3287 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3288 | /* Check for SQ overflow */ |
| 3289 | if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) { |
Or Gerlitz | 831d06c | 2010-02-04 11:25:07 +0000 | [diff] [blame] | 3290 | err = -ENOMEM; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3291 | break; |
| 3292 | } |
| 3293 | |
| 3294 | wqe = &nesqp->hwqp.sq_vbase[head]; |
| 3295 | /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n", |
| 3296 | nesqp->hwqp.qp_id, wqe, head); */ |
| 3297 | nes_fill_init_qp_wqe(wqe, nesqp, head); |
| 3298 | u64temp = (u64)(ib_wr->wr_id); |
| 3299 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, |
| 3300 | u64temp); |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3301 | switch (ib_wr->opcode) { |
| 3302 | case IB_WR_SEND: |
| 3303 | case IB_WR_SEND_WITH_INV: |
| 3304 | if (IB_WR_SEND == ib_wr->opcode) { |
| 3305 | if (ib_wr->send_flags & IB_SEND_SOLICITED) |
| 3306 | wqe_misc = NES_IWARP_SQ_OP_SENDSE; |
| 3307 | else |
| 3308 | wqe_misc = NES_IWARP_SQ_OP_SEND; |
| 3309 | } else { |
| 3310 | if (ib_wr->send_flags & IB_SEND_SOLICITED) |
| 3311 | wqe_misc = NES_IWARP_SQ_OP_SENDSEINV; |
| 3312 | else |
| 3313 | wqe_misc = NES_IWARP_SQ_OP_SENDINV; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3314 | |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3315 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX, |
| 3316 | ib_wr->ex.invalidate_rkey); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3317 | } |
| 3318 | |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3319 | if (ib_wr->num_sge > nesdev->nesadapter->max_sge) { |
| 3320 | err = -EINVAL; |
| 3321 | break; |
| 3322 | } |
| 3323 | |
| 3324 | if (ib_wr->send_flags & IB_SEND_FENCE) |
| 3325 | wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE; |
| 3326 | |
| 3327 | if ((ib_wr->send_flags & IB_SEND_INLINE) && |
| 3328 | ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) && |
| 3329 | (ib_wr->sg_list[0].length <= 64)) { |
| 3330 | memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX], |
| 3331 | (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length); |
| 3332 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX, |
| 3333 | ib_wr->sg_list[0].length); |
| 3334 | wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA; |
| 3335 | } else { |
| 3336 | fill_wqe_sg_send(wqe, ib_wr, 1); |
| 3337 | } |
| 3338 | |
| 3339 | break; |
| 3340 | case IB_WR_RDMA_WRITE: |
| 3341 | wqe_misc = NES_IWARP_SQ_OP_RDMAW; |
| 3342 | if (ib_wr->num_sge > nesdev->nesadapter->max_sge) { |
| 3343 | nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n", |
| 3344 | ib_wr->num_sge, nesdev->nesadapter->max_sge); |
| 3345 | err = -EINVAL; |
| 3346 | break; |
| 3347 | } |
| 3348 | |
| 3349 | if (ib_wr->send_flags & IB_SEND_FENCE) |
| 3350 | wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE; |
| 3351 | |
| 3352 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX, |
| 3353 | ib_wr->wr.rdma.rkey); |
| 3354 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX, |
| 3355 | ib_wr->wr.rdma.remote_addr); |
| 3356 | |
| 3357 | if ((ib_wr->send_flags & IB_SEND_INLINE) && |
| 3358 | ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) && |
| 3359 | (ib_wr->sg_list[0].length <= 64)) { |
| 3360 | memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX], |
| 3361 | (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length); |
| 3362 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX, |
| 3363 | ib_wr->sg_list[0].length); |
| 3364 | wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA; |
| 3365 | } else { |
| 3366 | fill_wqe_sg_send(wqe, ib_wr, 1); |
| 3367 | } |
| 3368 | |
| 3369 | wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = |
| 3370 | wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]; |
| 3371 | break; |
| 3372 | case IB_WR_RDMA_READ: |
| 3373 | case IB_WR_RDMA_READ_WITH_INV: |
| 3374 | /* iWARP only supports 1 sge for RDMA reads */ |
| 3375 | if (ib_wr->num_sge > 1) { |
| 3376 | nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n", |
| 3377 | ib_wr->num_sge); |
| 3378 | err = -EINVAL; |
| 3379 | break; |
| 3380 | } |
| 3381 | if (ib_wr->opcode == IB_WR_RDMA_READ) { |
| 3382 | wqe_misc = NES_IWARP_SQ_OP_RDMAR; |
| 3383 | } else { |
| 3384 | wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV; |
| 3385 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX, |
| 3386 | ib_wr->ex.invalidate_rkey); |
| 3387 | } |
| 3388 | |
| 3389 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX, |
| 3390 | ib_wr->wr.rdma.remote_addr); |
| 3391 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX, |
| 3392 | ib_wr->wr.rdma.rkey); |
| 3393 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX, |
| 3394 | ib_wr->sg_list->length); |
| 3395 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX, |
| 3396 | ib_wr->sg_list->addr); |
| 3397 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX, |
| 3398 | ib_wr->sg_list->lkey); |
| 3399 | break; |
| 3400 | case IB_WR_LOCAL_INV: |
| 3401 | wqe_misc = NES_IWARP_SQ_OP_LOCINV; |
| 3402 | set_wqe_32bit_value(wqe->wqe_words, |
| 3403 | NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX, |
| 3404 | ib_wr->ex.invalidate_rkey); |
| 3405 | break; |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 3406 | case IB_WR_FAST_REG_MR: |
| 3407 | { |
| 3408 | int i; |
| 3409 | int flags = ib_wr->wr.fast_reg.access_flags; |
| 3410 | struct nes_ib_fast_reg_page_list *pnesfrpl = |
| 3411 | container_of(ib_wr->wr.fast_reg.page_list, |
| 3412 | struct nes_ib_fast_reg_page_list, |
| 3413 | ibfrpl); |
| 3414 | u64 *src_page_list = pnesfrpl->ibfrpl.page_list; |
| 3415 | u64 *dst_page_list = pnesfrpl->nes_wqe_pbl.kva; |
| 3416 | |
| 3417 | if (ib_wr->wr.fast_reg.page_list_len > |
| 3418 | (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) { |
| 3419 | nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n"); |
| 3420 | err = -EINVAL; |
| 3421 | break; |
| 3422 | } |
| 3423 | wqe_misc = NES_IWARP_SQ_OP_FAST_REG; |
| 3424 | set_wqe_64bit_value(wqe->wqe_words, |
| 3425 | NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX, |
| 3426 | ib_wr->wr.fast_reg.iova_start); |
| 3427 | set_wqe_32bit_value(wqe->wqe_words, |
| 3428 | NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX, |
| 3429 | ib_wr->wr.fast_reg.length); |
| 3430 | set_wqe_32bit_value(wqe->wqe_words, |
| 3431 | NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX, |
| 3432 | ib_wr->wr.fast_reg.rkey); |
| 3433 | /* Set page size: */ |
| 3434 | if (ib_wr->wr.fast_reg.page_shift == 12) { |
| 3435 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K; |
| 3436 | } else if (ib_wr->wr.fast_reg.page_shift == 21) { |
| 3437 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M; |
| 3438 | } else { |
| 3439 | nes_debug(NES_DBG_IW_TX, "Invalid page shift," |
| 3440 | " ib_wr=%u, max=1\n", ib_wr->num_sge); |
| 3441 | err = -EINVAL; |
| 3442 | break; |
| 3443 | } |
| 3444 | /* Set access_flags */ |
| 3445 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ; |
| 3446 | if (flags & IB_ACCESS_LOCAL_WRITE) |
| 3447 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE; |
| 3448 | |
| 3449 | if (flags & IB_ACCESS_REMOTE_WRITE) |
| 3450 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE; |
| 3451 | |
| 3452 | if (flags & IB_ACCESS_REMOTE_READ) |
| 3453 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ; |
| 3454 | |
| 3455 | if (flags & IB_ACCESS_MW_BIND) |
| 3456 | wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND; |
| 3457 | |
| 3458 | /* Fill in PBL info: */ |
| 3459 | if (ib_wr->wr.fast_reg.page_list_len > |
| 3460 | pnesfrpl->ibfrpl.max_page_list_len) { |
| 3461 | nes_debug(NES_DBG_IW_TX, "Invalid page list length," |
| 3462 | " ib_wr=%p, value=%u, max=%u\n", |
| 3463 | ib_wr, ib_wr->wr.fast_reg.page_list_len, |
| 3464 | pnesfrpl->ibfrpl.max_page_list_len); |
| 3465 | err = -EINVAL; |
| 3466 | break; |
| 3467 | } |
| 3468 | |
| 3469 | set_wqe_64bit_value(wqe->wqe_words, |
| 3470 | NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX, |
| 3471 | pnesfrpl->nes_wqe_pbl.paddr); |
| 3472 | |
| 3473 | set_wqe_32bit_value(wqe->wqe_words, |
| 3474 | NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX, |
| 3475 | ib_wr->wr.fast_reg.page_list_len * 8); |
| 3476 | |
| 3477 | for (i = 0; i < ib_wr->wr.fast_reg.page_list_len; i++) |
| 3478 | dst_page_list[i] = cpu_to_le64(src_page_list[i]); |
| 3479 | |
Roland Dreier | 183ae74 | 2010-09-27 17:51:33 -0700 | [diff] [blame] | 3480 | nes_debug(NES_DBG_IW_TX, "SQ_FMR: iova_start: %llx, " |
| 3481 | "length: %d, rkey: %0x, pgl_paddr: %llx, " |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 3482 | "page_list_len: %u, wqe_misc: %x\n", |
Roland Dreier | 183ae74 | 2010-09-27 17:51:33 -0700 | [diff] [blame] | 3483 | (unsigned long long) ib_wr->wr.fast_reg.iova_start, |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 3484 | ib_wr->wr.fast_reg.length, |
| 3485 | ib_wr->wr.fast_reg.rkey, |
Roland Dreier | 183ae74 | 2010-09-27 17:51:33 -0700 | [diff] [blame] | 3486 | (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr, |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 3487 | ib_wr->wr.fast_reg.page_list_len, |
| 3488 | wqe_misc); |
| 3489 | break; |
| 3490 | } |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3491 | default: |
| 3492 | /* error */ |
| 3493 | err = -EINVAL; |
| 3494 | break; |
| 3495 | } |
| 3496 | |
Frank Zago | e5dec39 | 2009-12-09 13:51:36 -0800 | [diff] [blame] | 3497 | if (err) |
| 3498 | break; |
| 3499 | |
Chien Tung | d14152d | 2009-12-09 15:21:56 -0800 | [diff] [blame] | 3500 | if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3501 | wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL; |
Chien Tung | d14152d | 2009-12-09 15:21:56 -0800 | [diff] [blame] | 3502 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3503 | wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc); |
| 3504 | |
| 3505 | ib_wr = ib_wr->next; |
| 3506 | head++; |
| 3507 | wqe_count++; |
| 3508 | if (head >= qsize) |
| 3509 | head = 0; |
| 3510 | |
| 3511 | } |
| 3512 | |
| 3513 | nesqp->hwqp.sq_head = head; |
| 3514 | barrier(); |
| 3515 | while (wqe_count) { |
| 3516 | counter = min(wqe_count, ((u32)255)); |
| 3517 | wqe_count -= counter; |
| 3518 | nes_write32(nesdev->regs + NES_WQE_ALLOC, |
| 3519 | (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id); |
| 3520 | } |
| 3521 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 3522 | spin_unlock_irqrestore(&nesqp->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3523 | |
Frank Zago | e5dec39 | 2009-12-09 13:51:36 -0800 | [diff] [blame] | 3524 | out: |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3525 | if (err) |
| 3526 | *bad_wr = ib_wr; |
| 3527 | return err; |
| 3528 | } |
| 3529 | |
| 3530 | |
| 3531 | /** |
| 3532 | * nes_post_recv |
| 3533 | */ |
| 3534 | static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr, |
| 3535 | struct ib_recv_wr **bad_wr) |
| 3536 | { |
| 3537 | u64 u64temp; |
| 3538 | unsigned long flags = 0; |
| 3539 | struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); |
| 3540 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3541 | struct nes_qp *nesqp = to_nesqp(ibqp); |
| 3542 | struct nes_hw_qp_wqe *wqe; |
| 3543 | int err = 0; |
| 3544 | int sge_index; |
| 3545 | u32 qsize = nesqp->hwqp.rq_size; |
| 3546 | u32 head; |
| 3547 | u32 wqe_count = 0; |
| 3548 | u32 counter; |
| 3549 | u32 total_payload_length; |
| 3550 | |
Frank Zago | 4293fdc1 | 2009-12-09 13:51:36 -0800 | [diff] [blame] | 3551 | if (nesqp->ibqp_state > IB_QPS_RTS) { |
| 3552 | err = -EINVAL; |
| 3553 | goto out; |
| 3554 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3555 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 3556 | spin_lock_irqsave(&nesqp->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3557 | |
| 3558 | head = nesqp->hwqp.rq_head; |
| 3559 | |
| 3560 | while (ib_wr) { |
Don Wood | 8b1c9dc | 2009-09-05 20:36:38 -0700 | [diff] [blame] | 3561 | /* Check for QP error */ |
| 3562 | if (nesqp->term_flags) { |
| 3563 | err = -EINVAL; |
| 3564 | break; |
| 3565 | } |
| 3566 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3567 | if (ib_wr->num_sge > nesdev->nesadapter->max_sge) { |
| 3568 | err = -EINVAL; |
| 3569 | break; |
| 3570 | } |
| 3571 | /* Check for RQ overflow */ |
| 3572 | if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) { |
Or Gerlitz | 831d06c | 2010-02-04 11:25:07 +0000 | [diff] [blame] | 3573 | err = -ENOMEM; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3574 | break; |
| 3575 | } |
| 3576 | |
| 3577 | nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge); |
| 3578 | wqe = &nesqp->hwqp.rq_vbase[head]; |
| 3579 | |
| 3580 | /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n", |
| 3581 | nesqp->hwqp.qp_id, wqe, head); */ |
| 3582 | nes_fill_init_qp_wqe(wqe, nesqp, head); |
| 3583 | u64temp = (u64)(ib_wr->wr_id); |
| 3584 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, |
| 3585 | u64temp); |
| 3586 | total_payload_length = 0; |
| 3587 | for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) { |
| 3588 | set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4), |
| 3589 | ib_wr->sg_list[sge_index].addr); |
| 3590 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4), |
| 3591 | ib_wr->sg_list[sge_index].length); |
| 3592 | set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4), |
| 3593 | ib_wr->sg_list[sge_index].lkey); |
| 3594 | |
| 3595 | total_payload_length += ib_wr->sg_list[sge_index].length; |
| 3596 | } |
| 3597 | set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX, |
| 3598 | total_payload_length); |
| 3599 | |
| 3600 | ib_wr = ib_wr->next; |
| 3601 | head++; |
| 3602 | wqe_count++; |
| 3603 | if (head >= qsize) |
| 3604 | head = 0; |
| 3605 | } |
| 3606 | |
| 3607 | nesqp->hwqp.rq_head = head; |
| 3608 | barrier(); |
| 3609 | while (wqe_count) { |
| 3610 | counter = min(wqe_count, ((u32)255)); |
| 3611 | wqe_count -= counter; |
| 3612 | nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id); |
| 3613 | } |
| 3614 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 3615 | spin_unlock_irqrestore(&nesqp->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3616 | |
Frank Zago | 4293fdc1 | 2009-12-09 13:51:36 -0800 | [diff] [blame] | 3617 | out: |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3618 | if (err) |
| 3619 | *bad_wr = ib_wr; |
| 3620 | return err; |
| 3621 | } |
| 3622 | |
| 3623 | |
| 3624 | /** |
| 3625 | * nes_poll_cq |
| 3626 | */ |
| 3627 | static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry) |
| 3628 | { |
| 3629 | u64 u64temp; |
| 3630 | u64 wrid; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3631 | unsigned long flags = 0; |
| 3632 | struct nes_vnic *nesvnic = to_nesvnic(ibcq->device); |
| 3633 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3634 | struct nes_cq *nescq = to_nescq(ibcq); |
| 3635 | struct nes_qp *nesqp; |
| 3636 | struct nes_hw_cqe cqe; |
| 3637 | u32 head; |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3638 | u32 wq_tail = 0; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3639 | u32 cq_size; |
| 3640 | u32 cqe_count = 0; |
| 3641 | u32 wqe_index; |
| 3642 | u32 u32temp; |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3643 | u32 move_cq_head = 1; |
| 3644 | u32 err_code; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3645 | |
| 3646 | nes_debug(NES_DBG_CQ, "\n"); |
| 3647 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 3648 | spin_lock_irqsave(&nescq->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3649 | |
| 3650 | head = nescq->hw_cq.cq_head; |
| 3651 | cq_size = nescq->hw_cq.cq_size; |
| 3652 | |
| 3653 | while (cqe_count < num_entries) { |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3654 | if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) & |
| 3655 | NES_CQE_VALID) == 0) |
| 3656 | break; |
Roland Dreier | 31d1e34 | 2008-04-23 11:55:45 -0700 | [diff] [blame] | 3657 | |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3658 | /* |
| 3659 | * Make sure we read CQ entry contents *after* |
| 3660 | * we've checked the valid bit. |
| 3661 | */ |
| 3662 | rmb(); |
| 3663 | |
| 3664 | cqe = nescq->hw_cq.cq_vbase[head]; |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3665 | u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]); |
| 3666 | wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1); |
| 3667 | u32temp &= ~(NES_SW_CONTEXT_ALIGN-1); |
| 3668 | /* parse CQE, get completion context from WQE (either rq or sq) */ |
| 3669 | u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) | |
| 3670 | ((u64)u32temp); |
| 3671 | |
| 3672 | if (u64temp) { |
| 3673 | nesqp = (struct nes_qp *)(unsigned long)u64temp; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3674 | memset(entry, 0, sizeof *entry); |
| 3675 | if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) { |
| 3676 | entry->status = IB_WC_SUCCESS; |
| 3677 | } else { |
Don Wood | 320cdfd | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3678 | err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]); |
| 3679 | if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) { |
| 3680 | entry->status = err_code & 0x0000ffff; |
| 3681 | |
| 3682 | /* The rest of the cqe's will be marked as flushed */ |
| 3683 | nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] = |
| 3684 | cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) | |
| 3685 | NES_IWARP_CQE_MINOR_FLUSH); |
| 3686 | } else |
| 3687 | entry->status = IB_WC_WR_FLUSH_ERR; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | entry->qp = &nesqp->ibqp; |
| 3691 | entry->src_qp = nesqp->hwqp.qp_id; |
| 3692 | |
| 3693 | if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) { |
| 3694 | if (nesqp->skip_lsmm) { |
| 3695 | nesqp->skip_lsmm = 0; |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3696 | nesqp->hwqp.sq_tail++; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | /* Working on a SQ Completion*/ |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3700 | wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index]. |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3701 | wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) | |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3702 | ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index]. |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3703 | wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX]))); |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3704 | entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index]. |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3705 | wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]); |
| 3706 | |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3707 | switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index]. |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3708 | wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) { |
| 3709 | case NES_IWARP_SQ_OP_RDMAW: |
| 3710 | nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n"); |
| 3711 | entry->opcode = IB_WC_RDMA_WRITE; |
| 3712 | break; |
| 3713 | case NES_IWARP_SQ_OP_RDMAR: |
| 3714 | nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n"); |
| 3715 | entry->opcode = IB_WC_RDMA_READ; |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3716 | entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index]. |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3717 | wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]); |
| 3718 | break; |
| 3719 | case NES_IWARP_SQ_OP_SENDINV: |
| 3720 | case NES_IWARP_SQ_OP_SENDSEINV: |
| 3721 | case NES_IWARP_SQ_OP_SEND: |
| 3722 | case NES_IWARP_SQ_OP_SENDSE: |
| 3723 | nes_debug(NES_DBG_CQ, "Operation = Send.\n"); |
| 3724 | entry->opcode = IB_WC_SEND; |
| 3725 | break; |
Chien Tung | 649fe4a | 2009-12-09 13:51:37 -0800 | [diff] [blame] | 3726 | case NES_IWARP_SQ_OP_LOCINV: |
| 3727 | entry->opcode = IB_WR_LOCAL_INV; |
| 3728 | break; |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 3729 | case NES_IWARP_SQ_OP_FAST_REG: |
| 3730 | entry->opcode = IB_WC_FAST_REG_MR; |
| 3731 | break; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3732 | } |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3733 | |
| 3734 | nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1); |
| 3735 | if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) { |
| 3736 | move_cq_head = 0; |
| 3737 | wq_tail = nesqp->hwqp.sq_tail; |
| 3738 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3739 | } else { |
| 3740 | /* Working on a RQ Completion*/ |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3741 | entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]); |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3742 | wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) | |
| 3743 | ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3744 | entry->opcode = IB_WC_RECV; |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3745 | |
| 3746 | nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1); |
| 3747 | if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) { |
| 3748 | move_cq_head = 0; |
| 3749 | wq_tail = nesqp->hwqp.rq_tail; |
| 3750 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3751 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3752 | |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3753 | entry->wr_id = wrid; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3754 | entry++; |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3755 | cqe_count++; |
| 3756 | } |
| 3757 | |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3758 | if (move_cq_head) { |
| 3759 | nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0; |
| 3760 | if (++head >= cq_size) |
| 3761 | head = 0; |
| 3762 | nescq->polled_completions++; |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3763 | |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3764 | if ((nescq->polled_completions > (cq_size / 2)) || |
| 3765 | (nescq->polled_completions == 255)) { |
| 3766 | nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes" |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3767 | " are pending %u of %u.\n", |
| 3768 | nescq->hw_cq.cq_number, nescq->polled_completions, cq_size); |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3769 | nes_write32(nesdev->regs+NES_CQE_ALLOC, |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3770 | nescq->hw_cq.cq_number | (nescq->polled_completions << 16)); |
Don Wood | 6eed5e7 | 2009-09-05 20:36:39 -0700 | [diff] [blame] | 3771 | nescq->polled_completions = 0; |
| 3772 | } |
| 3773 | } else { |
| 3774 | /* Update the wqe index and set status to flush */ |
| 3775 | wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]); |
| 3776 | wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail; |
| 3777 | nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = |
| 3778 | cpu_to_le32(wqe_index); |
| 3779 | move_cq_head = 1; /* ready for next pass */ |
Don Wood | 5ee21fe | 2009-09-05 20:36:37 -0700 | [diff] [blame] | 3780 | } |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | if (nescq->polled_completions) { |
| 3784 | nes_write32(nesdev->regs+NES_CQE_ALLOC, |
| 3785 | nescq->hw_cq.cq_number | (nescq->polled_completions << 16)); |
| 3786 | nescq->polled_completions = 0; |
| 3787 | } |
| 3788 | |
| 3789 | nescq->hw_cq.cq_head = head; |
| 3790 | nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n", |
| 3791 | cqe_count, nescq->hw_cq.cq_number); |
| 3792 | |
Ilpo Järvinen | 5880ff1 | 2008-10-30 13:39:43 +0200 | [diff] [blame] | 3793 | spin_unlock_irqrestore(&nescq->lock, flags); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3794 | |
| 3795 | return cqe_count; |
| 3796 | } |
| 3797 | |
| 3798 | |
| 3799 | /** |
| 3800 | * nes_req_notify_cq |
| 3801 | */ |
| 3802 | static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags) |
| 3803 | { |
| 3804 | struct nes_vnic *nesvnic = to_nesvnic(ibcq->device); |
| 3805 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3806 | struct nes_cq *nescq = to_nescq(ibcq); |
| 3807 | u32 cq_arm; |
| 3808 | |
| 3809 | nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n", |
| 3810 | nescq->hw_cq.cq_number); |
| 3811 | |
| 3812 | cq_arm = nescq->hw_cq.cq_number; |
| 3813 | if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP) |
| 3814 | cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT; |
| 3815 | else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED) |
| 3816 | cq_arm |= NES_CQE_ALLOC_NOTIFY_SE; |
| 3817 | else |
| 3818 | return -EINVAL; |
| 3819 | |
| 3820 | nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm); |
| 3821 | nes_read32(nesdev->regs+NES_CQE_ALLOC); |
| 3822 | |
| 3823 | return 0; |
| 3824 | } |
| 3825 | |
| 3826 | |
| 3827 | /** |
| 3828 | * nes_init_ofa_device |
| 3829 | */ |
| 3830 | struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev) |
| 3831 | { |
| 3832 | struct nes_ib_device *nesibdev; |
| 3833 | struct nes_vnic *nesvnic = netdev_priv(netdev); |
| 3834 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3835 | |
| 3836 | nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device)); |
| 3837 | if (nesibdev == NULL) { |
| 3838 | return NULL; |
| 3839 | } |
| 3840 | strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX); |
| 3841 | nesibdev->ibdev.owner = THIS_MODULE; |
| 3842 | |
| 3843 | nesibdev->ibdev.node_type = RDMA_NODE_RNIC; |
| 3844 | memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid)); |
| 3845 | memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6); |
| 3846 | |
| 3847 | nesibdev->ibdev.uverbs_cmd_mask = |
| 3848 | (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | |
| 3849 | (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | |
| 3850 | (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | |
| 3851 | (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | |
| 3852 | (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | |
| 3853 | (1ull << IB_USER_VERBS_CMD_REG_MR) | |
| 3854 | (1ull << IB_USER_VERBS_CMD_DEREG_MR) | |
| 3855 | (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | |
| 3856 | (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | |
| 3857 | (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | |
| 3858 | (1ull << IB_USER_VERBS_CMD_CREATE_AH) | |
| 3859 | (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | |
| 3860 | (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) | |
| 3861 | (1ull << IB_USER_VERBS_CMD_CREATE_QP) | |
| 3862 | (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | |
| 3863 | (1ull << IB_USER_VERBS_CMD_POLL_CQ) | |
| 3864 | (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | |
| 3865 | (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | |
| 3866 | (1ull << IB_USER_VERBS_CMD_BIND_MW) | |
| 3867 | (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) | |
| 3868 | (1ull << IB_USER_VERBS_CMD_POST_RECV) | |
| 3869 | (1ull << IB_USER_VERBS_CMD_POST_SEND); |
| 3870 | |
| 3871 | nesibdev->ibdev.phys_port_cnt = 1; |
| 3872 | nesibdev->ibdev.num_comp_vectors = 1; |
| 3873 | nesibdev->ibdev.dma_device = &nesdev->pcidev->dev; |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 3874 | nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3875 | nesibdev->ibdev.query_device = nes_query_device; |
| 3876 | nesibdev->ibdev.query_port = nes_query_port; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3877 | nesibdev->ibdev.query_pkey = nes_query_pkey; |
| 3878 | nesibdev->ibdev.query_gid = nes_query_gid; |
| 3879 | nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext; |
| 3880 | nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext; |
| 3881 | nesibdev->ibdev.mmap = nes_mmap; |
| 3882 | nesibdev->ibdev.alloc_pd = nes_alloc_pd; |
| 3883 | nesibdev->ibdev.dealloc_pd = nes_dealloc_pd; |
| 3884 | nesibdev->ibdev.create_ah = nes_create_ah; |
| 3885 | nesibdev->ibdev.destroy_ah = nes_destroy_ah; |
| 3886 | nesibdev->ibdev.create_qp = nes_create_qp; |
| 3887 | nesibdev->ibdev.modify_qp = nes_modify_qp; |
| 3888 | nesibdev->ibdev.query_qp = nes_query_qp; |
| 3889 | nesibdev->ibdev.destroy_qp = nes_destroy_qp; |
| 3890 | nesibdev->ibdev.create_cq = nes_create_cq; |
| 3891 | nesibdev->ibdev.destroy_cq = nes_destroy_cq; |
| 3892 | nesibdev->ibdev.poll_cq = nes_poll_cq; |
| 3893 | nesibdev->ibdev.get_dma_mr = nes_get_dma_mr; |
| 3894 | nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr; |
| 3895 | nesibdev->ibdev.reg_user_mr = nes_reg_user_mr; |
| 3896 | nesibdev->ibdev.dereg_mr = nes_dereg_mr; |
| 3897 | nesibdev->ibdev.alloc_mw = nes_alloc_mw; |
| 3898 | nesibdev->ibdev.dealloc_mw = nes_dealloc_mw; |
| 3899 | nesibdev->ibdev.bind_mw = nes_bind_mw; |
| 3900 | |
Chien Tung | e293a26 | 2009-12-09 15:21:54 -0800 | [diff] [blame] | 3901 | nesibdev->ibdev.alloc_fast_reg_mr = nes_alloc_fast_reg_mr; |
| 3902 | nesibdev->ibdev.alloc_fast_reg_page_list = nes_alloc_fast_reg_page_list; |
| 3903 | nesibdev->ibdev.free_fast_reg_page_list = nes_free_fast_reg_page_list; |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3904 | |
| 3905 | nesibdev->ibdev.attach_mcast = nes_multicast_attach; |
| 3906 | nesibdev->ibdev.detach_mcast = nes_multicast_detach; |
| 3907 | nesibdev->ibdev.process_mad = nes_process_mad; |
| 3908 | |
| 3909 | nesibdev->ibdev.req_notify_cq = nes_req_notify_cq; |
| 3910 | nesibdev->ibdev.post_send = nes_post_send; |
| 3911 | nesibdev->ibdev.post_recv = nes_post_recv; |
| 3912 | |
| 3913 | nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL); |
| 3914 | if (nesibdev->ibdev.iwcm == NULL) { |
| 3915 | ib_dealloc_device(&nesibdev->ibdev); |
| 3916 | return NULL; |
| 3917 | } |
| 3918 | nesibdev->ibdev.iwcm->add_ref = nes_add_ref; |
| 3919 | nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref; |
| 3920 | nesibdev->ibdev.iwcm->get_qp = nes_get_qp; |
| 3921 | nesibdev->ibdev.iwcm->connect = nes_connect; |
| 3922 | nesibdev->ibdev.iwcm->accept = nes_accept; |
| 3923 | nesibdev->ibdev.iwcm->reject = nes_reject; |
| 3924 | nesibdev->ibdev.iwcm->create_listen = nes_create_listen; |
| 3925 | nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen; |
| 3926 | |
| 3927 | return nesibdev; |
| 3928 | } |
| 3929 | |
Maciej Sosnowski | ea62345 | 2010-11-24 17:29:38 +0000 | [diff] [blame] | 3930 | |
| 3931 | /** |
| 3932 | * nes_handle_delayed_event |
| 3933 | */ |
| 3934 | static void nes_handle_delayed_event(unsigned long data) |
| 3935 | { |
| 3936 | struct nes_vnic *nesvnic = (void *) data; |
| 3937 | |
| 3938 | if (nesvnic->delayed_event != nesvnic->last_dispatched_event) { |
| 3939 | struct ib_event event; |
| 3940 | |
| 3941 | event.device = &nesvnic->nesibdev->ibdev; |
| 3942 | if (!event.device) |
| 3943 | goto stop_timer; |
| 3944 | event.event = nesvnic->delayed_event; |
| 3945 | event.element.port_num = nesvnic->logical_port + 1; |
| 3946 | ib_dispatch_event(&event); |
| 3947 | } |
| 3948 | |
| 3949 | stop_timer: |
| 3950 | nesvnic->event_timer.function = NULL; |
| 3951 | } |
| 3952 | |
| 3953 | |
Faisal Latif | cd6860e | 2010-07-04 00:17:59 +0000 | [diff] [blame] | 3954 | void nes_port_ibevent(struct nes_vnic *nesvnic) |
| 3955 | { |
| 3956 | struct nes_ib_device *nesibdev = nesvnic->nesibdev; |
| 3957 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3958 | struct ib_event event; |
| 3959 | event.device = &nesibdev->ibdev; |
| 3960 | event.element.port_num = nesvnic->logical_port + 1; |
| 3961 | event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; |
Maciej Sosnowski | ea62345 | 2010-11-24 17:29:38 +0000 | [diff] [blame] | 3962 | |
| 3963 | if (!nesvnic->event_timer.function) { |
| 3964 | ib_dispatch_event(&event); |
| 3965 | nesvnic->last_dispatched_event = event.event; |
| 3966 | nesvnic->event_timer.function = nes_handle_delayed_event; |
| 3967 | nesvnic->event_timer.data = (unsigned long) nesvnic; |
| 3968 | nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY; |
| 3969 | add_timer(&nesvnic->event_timer); |
| 3970 | } else { |
| 3971 | mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY); |
| 3972 | } |
| 3973 | nesvnic->delayed_event = event.event; |
Faisal Latif | cd6860e | 2010-07-04 00:17:59 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 3976 | |
| 3977 | /** |
| 3978 | * nes_destroy_ofa_device |
| 3979 | */ |
| 3980 | void nes_destroy_ofa_device(struct nes_ib_device *nesibdev) |
| 3981 | { |
| 3982 | if (nesibdev == NULL) |
| 3983 | return; |
| 3984 | |
| 3985 | nes_unregister_ofa_device(nesibdev); |
| 3986 | |
| 3987 | kfree(nesibdev->ibdev.iwcm); |
| 3988 | ib_dealloc_device(&nesibdev->ibdev); |
| 3989 | } |
| 3990 | |
| 3991 | |
| 3992 | /** |
| 3993 | * nes_register_ofa_device |
| 3994 | */ |
| 3995 | int nes_register_ofa_device(struct nes_ib_device *nesibdev) |
| 3996 | { |
| 3997 | struct nes_vnic *nesvnic = nesibdev->nesvnic; |
| 3998 | struct nes_device *nesdev = nesvnic->nesdev; |
| 3999 | struct nes_adapter *nesadapter = nesdev->nesadapter; |
| 4000 | int i, ret; |
| 4001 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 4002 | ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 4003 | if (ret) { |
| 4004 | return ret; |
| 4005 | } |
| 4006 | |
| 4007 | /* Get the resources allocated to this device */ |
| 4008 | nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count; |
| 4009 | nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count; |
| 4010 | nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count; |
| 4011 | nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count; |
| 4012 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 4013 | for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) { |
| 4014 | ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 4015 | if (ret) { |
| 4016 | while (i > 0) { |
| 4017 | i--; |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 4018 | device_remove_file(&nesibdev->ibdev.dev, |
| 4019 | nes_dev_attributes[i]); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 4020 | } |
| 4021 | ib_unregister_device(&nesibdev->ibdev); |
| 4022 | return ret; |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | nesvnic->of_device_registered = 1; |
| 4027 | |
| 4028 | return 0; |
| 4029 | } |
| 4030 | |
| 4031 | |
| 4032 | /** |
| 4033 | * nes_unregister_ofa_device |
| 4034 | */ |
Roland Dreier | 1a855fb | 2008-04-16 21:01:09 -0700 | [diff] [blame] | 4035 | static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev) |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 4036 | { |
| 4037 | struct nes_vnic *nesvnic = nesibdev->nesvnic; |
| 4038 | int i; |
| 4039 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 4040 | for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) { |
| 4041 | device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]); |
Glenn Streiff | 3c2d774 | 2008-02-04 20:20:45 -0800 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | if (nesvnic->of_device_registered) { |
| 4045 | ib_unregister_device(&nesibdev->ibdev); |
| 4046 | } |
| 4047 | |
| 4048 | nesvnic->of_device_registered = 0; |
| 4049 | } |