blob: a3d3d8172f9aef4ab286a2db0f7c8a1cf9e8f46c [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Tatyana Nikolovac5488c52011-11-28 14:22:29 -06002 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080039#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
49atomic_t mod_qp_timouts;
50atomic_t qps_created;
51atomic_t sw_qps_destroyed;
52
Roland Dreier1a855fbf2008-04-16 21:01:09 -070053static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080054
55/**
56 * nes_alloc_mw
57 */
Shani Michaeli7083e422013-02-06 16:19:12 +000058static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type)
59{
Glenn Streiff3c2d7742008-02-04 20:20:45 -080060 struct nes_pd *nespd = to_nespd(ibpd);
61 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
62 struct nes_device *nesdev = nesvnic->nesdev;
63 struct nes_adapter *nesadapter = nesdev->nesadapter;
64 struct nes_cqp_request *cqp_request;
65 struct nes_mr *nesmr;
66 struct ib_mw *ibmw;
67 struct nes_hw_cqp_wqe *cqp_wqe;
68 int ret;
69 u32 stag;
70 u32 stag_index = 0;
71 u32 next_stag_index = 0;
72 u32 driver_key = 0;
73 u8 stag_key = 0;
74
Shani Michaeli7083e422013-02-06 16:19:12 +000075 if (type != IB_MW_TYPE_1)
76 return ERR_PTR(-EINVAL);
77
Glenn Streiff3c2d7742008-02-04 20:20:45 -080078 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
79 stag_key = (u8)next_stag_index;
80
81 driver_key = 0;
82
83 next_stag_index >>= 8;
84 next_stag_index %= nesadapter->max_mr;
85
86 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +000087 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080088 if (ret) {
89 return ERR_PTR(ret);
90 }
91
92 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
93 if (!nesmr) {
94 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
95 return ERR_PTR(-ENOMEM);
96 }
97
98 stag = stag_index << 8;
99 stag |= driver_key;
100 stag += (u32)stag_key;
101
102 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
103 stag, stag_index);
104
105 /* Register the region with the adapter */
106 cqp_request = nes_get_cqp_request(nesdev);
107 if (cqp_request == NULL) {
108 kfree(nesmr);
109 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
110 return ERR_PTR(-ENOMEM);
111 }
112
113 cqp_request->waiting = 1;
114 cqp_wqe = &cqp_request->cqp_wqe;
115
116 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
117 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
118 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
119 NES_CQP_STAG_REM_ACC_EN);
120
121 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
122 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
123 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
124
125 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700126 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800127
128 /* Wait for CQP */
129 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
130 NES_EVENT_TIMEOUT);
131 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
132 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
133 stag, ret, cqp_request->major_code, cqp_request->minor_code);
134 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -0700135 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800136 kfree(nesmr);
137 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
138 if (!ret) {
139 return ERR_PTR(-ETIME);
140 } else {
141 return ERR_PTR(-ENOMEM);
142 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800143 }
Roland Dreier1ff66e82008-07-14 23:48:49 -0700144 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800145
146 nesmr->ibmw.rkey = stag;
147 nesmr->mode = IWNES_MEMREG_TYPE_MW;
148 ibmw = &nesmr->ibmw;
149 nesmr->pbl_4k = 0;
150 nesmr->pbls_used = 0;
151
152 return ibmw;
153}
154
155
156/**
157 * nes_dealloc_mw
158 */
159static int nes_dealloc_mw(struct ib_mw *ibmw)
160{
161 struct nes_mr *nesmr = to_nesmw(ibmw);
162 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
163 struct nes_device *nesdev = nesvnic->nesdev;
164 struct nes_adapter *nesadapter = nesdev->nesadapter;
165 struct nes_hw_cqp_wqe *cqp_wqe;
166 struct nes_cqp_request *cqp_request;
167 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800168 int ret;
169
170 /* Deallocate the window with the adapter */
171 cqp_request = nes_get_cqp_request(nesdev);
172 if (cqp_request == NULL) {
173 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
174 return -ENOMEM;
175 }
176 cqp_request->waiting = 1;
177 cqp_wqe = &cqp_request->cqp_wqe;
178 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
179 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
180 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
181
182 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700183 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800184
185 /* Wait for CQP */
186 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
187 ibmw->rkey);
188 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
189 NES_EVENT_TIMEOUT);
190 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
191 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
192 ret, cqp_request->major_code, cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -0700193 if (!ret)
194 err = -ETIME;
195 else if (cqp_request->major_code)
196 err = -EIO;
197
198 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800199
200 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
201 (ibmw->rkey & 0x0fffff00) >> 8);
202 kfree(nesmr);
203
204 return err;
205}
206
207
208/**
209 * nes_bind_mw
210 */
211static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
212 struct ib_mw_bind *ibmw_bind)
213{
214 u64 u64temp;
215 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
216 struct nes_device *nesdev = nesvnic->nesdev;
217 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
218 struct nes_qp *nesqp = to_nesqp(ibqp);
219 struct nes_hw_qp_wqe *wqe;
220 unsigned long flags = 0;
221 u32 head;
222 u32 wqe_misc = 0;
223 u32 qsize;
224
225 if (nesqp->ibqp_state > IB_QPS_RTS)
226 return -EINVAL;
227
Ilpo Järvinen5880ff12008-10-30 13:39:43 +0200228 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800229
230 head = nesqp->hwqp.sq_head;
231 qsize = nesqp->hwqp.sq_tail;
232
233 /* Check for SQ overflow */
234 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Ilpo Järvinen5880ff12008-10-30 13:39:43 +0200235 spin_unlock_irqrestore(&nesqp->lock, flags);
Or Gerlitz831d06c2010-02-04 11:25:07 +0000236 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800237 }
238
239 wqe = &nesqp->hwqp.sq_vbase[head];
240 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
241 nes_fill_init_qp_wqe(wqe, nesqp, head);
242 u64temp = ibmw_bind->wr_id;
243 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
244 wqe_misc = NES_IWARP_SQ_OP_BIND;
245
246 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
247
248 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
249 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
250
Shani Michaeli7083e422013-02-06 16:19:12 +0000251 if (ibmw_bind->bind_info.mw_access_flags & IB_ACCESS_REMOTE_WRITE)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
Shani Michaeli7083e422013-02-06 16:19:12 +0000253 if (ibmw_bind->bind_info.mw_access_flags & IB_ACCESS_REMOTE_READ)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800254 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800255
256 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
Shani Michaeli7083e422013-02-06 16:19:12 +0000257 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX,
258 ibmw_bind->bind_info.mr->lkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800259 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
260 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
Shani Michaeli7083e422013-02-06 16:19:12 +0000261 ibmw_bind->bind_info.length);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800262 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
Shani Michaeli7083e422013-02-06 16:19:12 +0000263 u64temp = (u64)ibmw_bind->bind_info.addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800264 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
265
266 head++;
267 if (head >= qsize)
268 head = 0;
269
270 nesqp->hwqp.sq_head = head;
271 barrier();
272
273 nes_write32(nesdev->regs+NES_WQE_ALLOC,
274 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
275
Ilpo Järvinen5880ff12008-10-30 13:39:43 +0200276 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800277
278 return 0;
279}
280
281
Chien Tunge293a262009-12-09 15:21:54 -0800282/*
283 * nes_alloc_fast_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800284 */
Chien Tunge293a262009-12-09 15:21:54 -0800285static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
286 u32 stag, u32 page_count)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800287{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800288 struct nes_hw_cqp_wqe *cqp_wqe;
Chien Tunge293a262009-12-09 15:21:54 -0800289 struct nes_cqp_request *cqp_request;
290 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800291 int ret;
Chien Tunge293a262009-12-09 15:21:54 -0800292 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800293 u32 opcode = 0;
Chien Tunge293a262009-12-09 15:21:54 -0800294 u16 major_code;
295 u64 region_length = page_count * PAGE_SIZE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800296
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800297
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800298 cqp_request = nes_get_cqp_request(nesdev);
299 if (cqp_request == NULL) {
300 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
Chien Tunge293a262009-12-09 15:21:54 -0800301 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800302 }
Chien Tunge293a262009-12-09 15:21:54 -0800303 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
304 "region_length = %llu\n",
305 page_count, region_length);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800306 cqp_request->waiting = 1;
307 cqp_wqe = &cqp_request->cqp_wqe;
308
Chien Tunge293a262009-12-09 15:21:54 -0800309 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
310 if (nesadapter->free_4kpbl > 0) {
311 nesadapter->free_4kpbl--;
312 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
313 } else {
314 /* No 4kpbl's available: */
315 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
316 nes_debug(NES_DBG_MR, "Out of Pbls\n");
317 nes_free_cqp_request(nesdev, cqp_request);
318 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800319 }
320
Chien Tunge293a262009-12-09 15:21:54 -0800321 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
322 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
323 NES_CQP_STAG_REM_ACC_EN;
324 /*
325 * The current OFED API does not support the zero based TO option.
326 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
327 * the API does not support that ability to have the MR set for local
328 * access only when created and not allow the SQ op to override. Given
329 * this the remote enable must be set here.
330 */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800331
332 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
333 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
Chien Tunge293a262009-12-09 15:21:54 -0800334 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800335
Chien Tunge293a262009-12-09 15:21:54 -0800336 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
337 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
338 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
339 cpu_to_le32(nespd->pd_id & 0x00007fff);
340
341 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
342 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
343 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
344 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
345 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
346 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
347 barrier();
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800348
349 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700350 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800351
352 /* Wait for CQP */
Chien Tunge293a262009-12-09 15:21:54 -0800353 ret = wait_event_timeout(cqp_request->waitq,
354 (0 != cqp_request->request_done),
355 NES_EVENT_TIMEOUT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800356
Chien Tunge293a262009-12-09 15:21:54 -0800357 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
358 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
359 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
360 cqp_request->minor_code);
361 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -0700362 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800363
Chien Tunge293a262009-12-09 15:21:54 -0800364 if (!ret || major_code) {
Chien Tung35c6d692008-11-02 21:37:35 -0800365 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Chien Tunge293a262009-12-09 15:21:54 -0800366 nesadapter->free_4kpbl++;
Chien Tung35c6d692008-11-02 21:37:35 -0800367 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
368 }
369
Chien Tunge293a262009-12-09 15:21:54 -0800370 if (!ret)
371 return -ETIME;
372 else if (major_code)
373 return -EIO;
374 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800375}
376
Chien Tunge293a262009-12-09 15:21:54 -0800377/*
378 * nes_alloc_fast_reg_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800379 */
Roland Dreier53978b42010-04-21 15:58:28 -0700380static struct ib_mr *nes_alloc_fast_reg_mr(struct ib_pd *ibpd, int max_page_list_len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800381{
Chien Tunge293a262009-12-09 15:21:54 -0800382 struct nes_pd *nespd = to_nespd(ibpd);
383 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800384 struct nes_device *nesdev = nesvnic->nesdev;
Chien Tung35c6d692008-11-02 21:37:35 -0800385 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800386
Chien Tunge293a262009-12-09 15:21:54 -0800387 u32 next_stag_index;
388 u8 stag_key = 0;
389 u32 driver_key = 0;
390 int err = 0;
391 u32 stag_index = 0;
392 struct nes_mr *nesmr;
393 u32 stag;
394 int ret;
395 struct ib_mr *ibmr;
396/*
397 * Note: Set to always use a fixed length single page entry PBL. This is to allow
398 * for the fast_reg_mr operation to always know the size of the PBL.
399 */
400 if (max_page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
401 return ERR_PTR(-E2BIG);
402
403 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
404 stag_key = (u8)next_stag_index;
405 next_stag_index >>= 8;
406 next_stag_index %= nesadapter->max_mr;
407
408 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
409 nesadapter->max_mr, &stag_index,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000410 &next_stag_index, NES_RESOURCE_FAST_MR);
Chien Tunge293a262009-12-09 15:21:54 -0800411 if (err)
412 return ERR_PTR(err);
413
414 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
415 if (!nesmr) {
416 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
417 return ERR_PTR(-ENOMEM);
418 }
419
420 stag = stag_index << 8;
421 stag |= driver_key;
422 stag += (u32)stag_key;
423
424 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
425 stag, stag_index);
426
427 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_page_list_len);
428
429 if (ret == 0) {
430 nesmr->ibmr.rkey = stag;
431 nesmr->ibmr.lkey = stag;
432 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
433 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800434 } else {
Chien Tunge293a262009-12-09 15:21:54 -0800435 kfree(nesmr);
436 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
437 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800438 }
Chien Tunge293a262009-12-09 15:21:54 -0800439 return ibmr;
440}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800441
Chien Tunge293a262009-12-09 15:21:54 -0800442/*
443 * nes_alloc_fast_reg_page_list
444 */
445static struct ib_fast_reg_page_list *nes_alloc_fast_reg_page_list(
446 struct ib_device *ibdev,
447 int page_list_len)
448{
449 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
450 struct nes_device *nesdev = nesvnic->nesdev;
451 struct ib_fast_reg_page_list *pifrpl;
452 struct nes_ib_fast_reg_page_list *pnesfrpl;
Don Wooddae5d132009-03-06 15:12:09 -0800453
Chien Tunge293a262009-12-09 15:21:54 -0800454 if (page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
455 return ERR_PTR(-E2BIG);
456 /*
457 * Allocate the ib_fast_reg_page_list structure, the
458 * nes_fast_bpl structure, and the PLB table.
459 */
460 pnesfrpl = kmalloc(sizeof(struct nes_ib_fast_reg_page_list) +
461 page_list_len * sizeof(u64), GFP_KERNEL);
462
463 if (!pnesfrpl)
464 return ERR_PTR(-ENOMEM);
465
466 pifrpl = &pnesfrpl->ibfrpl;
467 pifrpl->page_list = &pnesfrpl->pbl;
468 pifrpl->max_page_list_len = page_list_len;
469 /*
470 * Allocate the WQE PBL
471 */
472 pnesfrpl->nes_wqe_pbl.kva = pci_alloc_consistent(nesdev->pcidev,
473 page_list_len * sizeof(u64),
474 &pnesfrpl->nes_wqe_pbl.paddr);
475
476 if (!pnesfrpl->nes_wqe_pbl.kva) {
477 kfree(pnesfrpl);
478 return ERR_PTR(-ENOMEM);
Chien Tung35c6d692008-11-02 21:37:35 -0800479 }
Chien Tunge293a262009-12-09 15:21:54 -0800480 nes_debug(NES_DBG_MR, "nes_alloc_fast_reg_pbl: nes_frpl = %p, "
481 "ibfrpl = %p, ibfrpl.page_list = %p, pbl.kva = %p, "
Roland Dreier183ae742010-09-27 17:51:33 -0700482 "pbl.paddr = %llx\n", pnesfrpl, &pnesfrpl->ibfrpl,
Chien Tunge293a262009-12-09 15:21:54 -0800483 pnesfrpl->ibfrpl.page_list, pnesfrpl->nes_wqe_pbl.kva,
Roland Dreier183ae742010-09-27 17:51:33 -0700484 (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr);
Chien Tung35c6d692008-11-02 21:37:35 -0800485
Chien Tunge293a262009-12-09 15:21:54 -0800486 return pifrpl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487}
488
Chien Tunge293a262009-12-09 15:21:54 -0800489/*
490 * nes_free_fast_reg_page_list
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800491 */
Chien Tunge293a262009-12-09 15:21:54 -0800492static void nes_free_fast_reg_page_list(struct ib_fast_reg_page_list *pifrpl)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800493{
Chien Tunge293a262009-12-09 15:21:54 -0800494 struct nes_vnic *nesvnic = to_nesvnic(pifrpl->device);
495 struct nes_device *nesdev = nesvnic->nesdev;
496 struct nes_ib_fast_reg_page_list *pnesfrpl;
497
498 pnesfrpl = container_of(pifrpl, struct nes_ib_fast_reg_page_list, ibfrpl);
499 /*
500 * Free the WQE PBL.
501 */
502 pci_free_consistent(nesdev->pcidev,
503 pifrpl->max_page_list_len * sizeof(u64),
504 pnesfrpl->nes_wqe_pbl.kva,
505 pnesfrpl->nes_wqe_pbl.paddr);
506 /*
507 * Free the PBL structure
508 */
509 kfree(pnesfrpl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800510}
511
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800512/**
513 * nes_query_device
514 */
515static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
516{
517 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
518 struct nes_device *nesdev = nesvnic->nesdev;
519 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
520
521 memset(props, 0, sizeof(*props));
522 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
523
Miroslaw Walukiewiczff0380c2010-07-15 14:53:29 +0000524 props->fw_ver = nesdev->nesadapter->firmware_version;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800525 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
526 props->vendor_id = nesdev->nesadapter->vendor_id;
527 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
528 props->hw_ver = nesdev->nesadapter->hw_rev;
529 props->max_mr_size = 0x80000000;
530 props->max_qp = nesibdev->max_qp;
531 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
532 props->max_sge = nesdev->nesadapter->max_sge;
533 props->max_cq = nesibdev->max_cq;
Chien Tung5924aea2009-12-09 15:21:56 -0800534 props->max_cqe = nesdev->nesadapter->max_cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800535 props->max_mr = nesibdev->max_mr;
536 props->max_mw = nesibdev->max_mr;
537 props->max_pd = nesibdev->max_pd;
538 props->max_sge_rd = 1;
539 switch (nesdev->nesadapter->max_irrq_wr) {
540 case 0:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800541 props->max_qp_rd_atom = 2;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 break;
543 case 1:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800544 props->max_qp_rd_atom = 8;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800545 break;
546 case 2:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800547 props->max_qp_rd_atom = 32;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800548 break;
549 case 3:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800550 props->max_qp_rd_atom = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800551 break;
552 default:
553 props->max_qp_rd_atom = 0;
554 }
Faisal Latif66388d62009-06-22 22:52:30 -0700555 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800556 props->atomic_cap = IB_ATOMIC_NONE;
557 props->max_map_per_fmr = 1;
558
559 return 0;
560}
561
562
563/**
564 * nes_query_port
565 */
566static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
567{
Chien Tungcd1d3f72009-09-05 20:36:39 -0700568 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
569 struct net_device *netdev = nesvnic->netdev;
570
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800571 memset(props, 0, sizeof(*props));
572
Chien Tungcd1d3f72009-09-05 20:36:39 -0700573 props->max_mtu = IB_MTU_4096;
574
575 if (netdev->mtu >= 4096)
576 props->active_mtu = IB_MTU_4096;
577 else if (netdev->mtu >= 2048)
578 props->active_mtu = IB_MTU_2048;
579 else if (netdev->mtu >= 1024)
580 props->active_mtu = IB_MTU_1024;
581 else if (netdev->mtu >= 512)
582 props->active_mtu = IB_MTU_512;
583 else
584 props->active_mtu = IB_MTU_256;
585
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800586 props->lid = 1;
587 props->lmc = 0;
588 props->sm_lid = 0;
589 props->sm_sl = 0;
Chien Tung29327722010-08-23 14:32:36 +0000590 if (netif_queue_stopped(netdev))
591 props->state = IB_PORT_DOWN;
592 else if (nesvnic->linkup)
Chien Tungcd1d3f72009-09-05 20:36:39 -0700593 props->state = IB_PORT_ACTIVE;
594 else
595 props->state = IB_PORT_DOWN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800596 props->phys_state = 0;
597 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
598 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
599 props->gid_tbl_len = 1;
600 props->pkey_tbl_len = 1;
601 props->qkey_viol_cntr = 0;
602 props->active_width = IB_WIDTH_4X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200603 props->active_speed = IB_SPEED_SDR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800604 props->max_msg_sz = 0x80000000;
605
606 return 0;
607}
608
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800609/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800610 * nes_query_pkey
611 */
612static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
613{
614 *pkey = 0;
615 return 0;
616}
617
618
619/**
620 * nes_query_gid
621 */
622static int nes_query_gid(struct ib_device *ibdev, u8 port,
623 int index, union ib_gid *gid)
624{
625 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
626
627 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
628 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
629
630 return 0;
631}
632
633
634/**
635 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
636 * of all objects associated with a particular user-mode client.
637 */
638static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
639 struct ib_udata *udata)
640{
641 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
642 struct nes_device *nesdev = nesvnic->nesdev;
643 struct nes_adapter *nesadapter = nesdev->nesadapter;
644 struct nes_alloc_ucontext_req req;
645 struct nes_alloc_ucontext_resp uresp;
646 struct nes_ucontext *nes_ucontext;
647 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
648
649
650 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
651 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
652 return ERR_PTR(-EINVAL);
653 }
654
655 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
656 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
657 req.userspace_ver, NES_ABI_USERSPACE_VER);
658 return ERR_PTR(-EINVAL);
659 }
660
661
662 memset(&uresp, 0, sizeof uresp);
663
664 uresp.max_qps = nesibdev->max_qp;
665 uresp.max_pds = nesibdev->max_pd;
666 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
667 uresp.virtwq = nesadapter->virtwq;
668 uresp.kernel_ver = NES_ABI_KERNEL_VER;
669
670 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
671 if (!nes_ucontext)
672 return ERR_PTR(-ENOMEM);
673
674 nes_ucontext->nesdev = nesdev;
675 nes_ucontext->mmap_wq_offset = uresp.max_pds;
676 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
677 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
678 PAGE_SIZE;
679
680
681 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
682 kfree(nes_ucontext);
683 return ERR_PTR(-EFAULT);
684 }
685
686 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
687 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
688 atomic_set(&nes_ucontext->usecnt, 1);
689 return &nes_ucontext->ibucontext;
690}
691
692
693/**
694 * nes_dealloc_ucontext
695 */
696static int nes_dealloc_ucontext(struct ib_ucontext *context)
697{
698 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
699 /* struct nes_device *nesdev = nesvnic->nesdev; */
700 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
701
702 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
703 return 0;
704 kfree(nes_ucontext);
705 return 0;
706}
707
708
709/**
710 * nes_mmap
711 */
712static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
713{
714 unsigned long index;
715 struct nes_vnic *nesvnic = to_nesvnic(context->device);
716 struct nes_device *nesdev = nesvnic->nesdev;
717 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
718 struct nes_ucontext *nes_ucontext;
719 struct nes_qp *nesqp;
720
721 nes_ucontext = to_nesucontext(context);
722
723
724 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
725 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
726 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
727 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
728 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
729 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
730 return -EFAULT;
731 }
732 nesqp = nes_ucontext->mmap_nesqp[index];
733 if (nesqp == NULL) {
734 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
735 return -EFAULT;
736 }
737 if (remap_pfn_range(vma, vma->vm_start,
738 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
739 vma->vm_end - vma->vm_start,
740 vma->vm_page_prot)) {
741 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
742 return -EAGAIN;
743 }
744 vma->vm_private_data = nesqp;
745 return 0;
746 } else {
747 index = vma->vm_pgoff;
748 if (!test_bit(index, nes_ucontext->allocated_doorbells))
749 return -EFAULT;
750
751 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
752 if (io_remap_pfn_range(vma, vma->vm_start,
753 (nesdev->doorbell_start +
754 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
755 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
756 return -EAGAIN;
757 vma->vm_private_data = nes_ucontext;
758 return 0;
759 }
760
761 return -ENOSYS;
762}
763
764
765/**
766 * nes_alloc_pd
767 */
768static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
769 struct ib_ucontext *context, struct ib_udata *udata)
770{
771 struct nes_pd *nespd;
772 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
773 struct nes_device *nesdev = nesvnic->nesdev;
774 struct nes_adapter *nesadapter = nesdev->nesadapter;
775 struct nes_ucontext *nesucontext;
776 struct nes_alloc_pd_resp uresp;
777 u32 pd_num = 0;
778 int err;
779
780 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
781 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
Eric Dumazet29b44332010-10-11 10:22:12 +0000782 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800783
784 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000785 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800786 if (err) {
787 return ERR_PTR(err);
788 }
789
790 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
791 if (!nespd) {
792 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
793 return ERR_PTR(-ENOMEM);
794 }
795
796 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
797 nespd, nesvnic->nesibdev->ibdev.name);
798
799 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
800
801 if (context) {
802 nesucontext = to_nesucontext(context);
803 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
804 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
805 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
806 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600807 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800808 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
809 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
810 kfree(nespd);
811 return ERR_PTR(-ENOMEM);
812 }
813
814 uresp.pd_id = nespd->pd_id;
815 uresp.mmap_db_index = nespd->mmap_db_index;
816 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
817 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
818 kfree(nespd);
819 return ERR_PTR(-EFAULT);
820 }
821
822 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
823 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
824 nesucontext->first_free_db = nespd->mmap_db_index + 1;
825 }
826
827 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
828 return &nespd->ibpd;
829}
830
831
832/**
833 * nes_dealloc_pd
834 */
835static int nes_dealloc_pd(struct ib_pd *ibpd)
836{
837 struct nes_ucontext *nesucontext;
838 struct nes_pd *nespd = to_nespd(ibpd);
839 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
840 struct nes_device *nesdev = nesvnic->nesdev;
841 struct nes_adapter *nesadapter = nesdev->nesadapter;
842
843 if ((ibpd->uobject) && (ibpd->uobject->context)) {
844 nesucontext = to_nesucontext(ibpd->uobject->context);
845 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
846 nespd->mmap_db_index);
847 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
848 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
849 if (nesucontext->first_free_db > nespd->mmap_db_index) {
850 nesucontext->first_free_db = nespd->mmap_db_index;
851 }
852 }
853
854 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
855 nespd->pd_id, nespd);
856 nes_free_resource(nesadapter, nesadapter->allocated_pds,
857 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
858 kfree(nespd);
859
860 return 0;
861}
862
863
864/**
865 * nes_create_ah
866 */
867static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
868{
869 return ERR_PTR(-ENOSYS);
870}
871
872
873/**
874 * nes_destroy_ah
875 */
876static int nes_destroy_ah(struct ib_ah *ah)
877{
878 return -ENOSYS;
879}
880
881
882/**
883 * nes_get_encoded_size
884 */
885static inline u8 nes_get_encoded_size(int *size)
886{
887 u8 encoded_size = 0;
888 if (*size <= 32) {
889 *size = 32;
890 encoded_size = 1;
891 } else if (*size <= 128) {
892 *size = 128;
893 encoded_size = 2;
894 } else if (*size <= 512) {
895 *size = 512;
896 encoded_size = 3;
897 }
898 return (encoded_size);
899}
900
901
902
903/**
904 * nes_setup_virt_qp
905 */
906static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
907 struct nes_vnic *nesvnic, int sq_size, int rq_size)
908{
909 unsigned long flags;
910 void *mem;
911 __le64 *pbl = NULL;
912 __le64 *tpbl;
913 __le64 *pblbuffer;
914 struct nes_device *nesdev = nesvnic->nesdev;
915 struct nes_adapter *nesadapter = nesdev->nesadapter;
916 u32 pbl_entries;
917 u8 rq_pbl_entries;
918 u8 sq_pbl_entries;
919
920 pbl_entries = nespbl->pbl_size >> 3;
Roland Dreier71e09572008-04-16 21:01:09 -0700921 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800922 nespbl->pbl_size, pbl_entries,
923 (void *)nespbl->pbl_vbase,
Roland Dreier71e09572008-04-16 21:01:09 -0700924 (unsigned long) nespbl->pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800925 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
926 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
927 /* the first pbl to be fro the rq_vbase... */
928 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
929 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
930 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
931 if (!nespbl->page) {
932 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
933 kfree(nespbl);
934 return -ENOMEM;
935 }
936
937 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
938 nesqp->page = nespbl->page;
939 if (!nesqp->hwqp.sq_vbase) {
940 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
941 kfree(nespbl);
942 return -ENOMEM;
943 }
944
945 /* Now to get to sq.. we need to calculate how many */
946 /* PBL entries were used by the rq.. */
947 pbl += sq_pbl_entries;
948 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
949 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
950 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
951
Roland Dreier71e09572008-04-16 21:01:09 -0700952 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
953 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
954 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800955 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
956 if (!nesadapter->free_256pbl) {
957 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
958 nespbl->pbl_pbase);
959 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
960 kunmap(nesqp->page);
961 kfree(nespbl);
962 return -ENOMEM;
963 }
964 nesadapter->free_256pbl--;
965 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
966
967 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
968 pblbuffer = nesqp->pbl_vbase;
969 if (!nesqp->pbl_vbase) {
970 /* memory allocated during nes_reg_user_mr() */
971 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
972 nespbl->pbl_pbase);
973 kfree(nespbl);
974 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
975 nesadapter->free_256pbl++;
976 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
977 kunmap(nesqp->page);
978 return -ENOMEM;
979 }
980 memset(nesqp->pbl_vbase, 0, 256);
981 /* fill in the page address in the pbl buffer.. */
982 tpbl = pblbuffer + 16;
983 pbl = (__le64 *)nespbl->pbl_vbase;
984 while (sq_pbl_entries--)
985 *tpbl++ = *pbl++;
986 tpbl = pblbuffer;
987 while (rq_pbl_entries--)
988 *tpbl++ = *pbl++;
989
990 /* done with memory allocated during nes_reg_user_mr() */
991 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
992 nespbl->pbl_pbase);
993 kfree(nespbl);
994
995 nesqp->qp_mem_size =
996 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
997 /* Round up to a multiple of a page */
998 nesqp->qp_mem_size += PAGE_SIZE - 1;
999 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1000
1001 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1002 &nesqp->hwqp.q2_pbase);
1003
1004 if (!mem) {
1005 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1006 nesqp->pbl_vbase = NULL;
1007 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1008 nesadapter->free_256pbl++;
1009 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1010 kunmap(nesqp->page);
1011 return -ENOMEM;
1012 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001013 nesqp->sq_kmapped = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001014 nesqp->hwqp.q2_vbase = mem;
1015 mem += 256;
1016 memset(nesqp->hwqp.q2_vbase, 0, 256);
1017 nesqp->nesqp_context = mem;
1018 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1019 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1020
1021 return 0;
1022}
1023
1024
1025/**
1026 * nes_setup_mmap_qp
1027 */
1028static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1029 int sq_size, int rq_size)
1030{
1031 void *mem;
1032 struct nes_device *nesdev = nesvnic->nesdev;
1033
1034 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1035 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1036 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1037 256; /* this is Q2 */
1038 /* Round up to a multiple of a page */
1039 nesqp->qp_mem_size += PAGE_SIZE - 1;
1040 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1041
1042 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1043 &nesqp->hwqp.sq_pbase);
1044 if (!mem)
1045 return -ENOMEM;
1046 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1047 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1048 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1049
1050 memset(mem, 0, nesqp->qp_mem_size);
1051
1052 nesqp->hwqp.sq_vbase = mem;
1053 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1054
1055 nesqp->hwqp.rq_vbase = mem;
1056 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1057 sizeof(struct nes_hw_qp_wqe) * sq_size;
1058 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1059
1060 nesqp->hwqp.q2_vbase = mem;
1061 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1062 sizeof(struct nes_hw_qp_wqe) * rq_size;
1063 mem += 256;
1064 memset(nesqp->hwqp.q2_vbase, 0, 256);
1065
1066 nesqp->nesqp_context = mem;
1067 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1068 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1069 return 0;
1070}
1071
1072
1073/**
1074 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1075 */
1076static inline void nes_free_qp_mem(struct nes_device *nesdev,
1077 struct nes_qp *nesqp, int virt_wqs)
1078{
1079 unsigned long flags;
1080 struct nes_adapter *nesadapter = nesdev->nesadapter;
1081 if (!virt_wqs) {
1082 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1083 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1084 }else {
1085 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1086 nesadapter->free_256pbl++;
1087 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1088 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1089 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1090 nesqp->pbl_vbase = NULL;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001091 if (nesqp->sq_kmapped) {
1092 nesqp->sq_kmapped = 0;
1093 kunmap(nesqp->page);
1094 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095 }
1096}
1097
1098
1099/**
1100 * nes_create_qp
1101 */
1102static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1103 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1104{
1105 u64 u64temp= 0;
1106 u64 u64nesqp = 0;
1107 struct nes_pd *nespd = to_nespd(ibpd);
1108 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1109 struct nes_device *nesdev = nesvnic->nesdev;
1110 struct nes_adapter *nesadapter = nesdev->nesadapter;
1111 struct nes_qp *nesqp;
1112 struct nes_cq *nescq;
1113 struct nes_ucontext *nes_ucontext;
1114 struct nes_hw_cqp_wqe *cqp_wqe;
1115 struct nes_cqp_request *cqp_request;
1116 struct nes_create_qp_req req;
1117 struct nes_create_qp_resp uresp;
1118 struct nes_pbl *nespbl = NULL;
1119 u32 qp_num = 0;
1120 u32 opcode = 0;
1121 /* u32 counter = 0; */
1122 void *mem;
1123 unsigned long flags;
1124 int ret;
1125 int err;
1126 int virt_wqs = 0;
1127 int sq_size;
1128 int rq_size;
1129 u8 sq_encoded_size;
1130 u8 rq_encoded_size;
1131 /* int counter; */
1132
Eli Cohenb846f252008-04-16 21:09:27 -07001133 if (init_attr->create_flags)
1134 return ERR_PTR(-EINVAL);
1135
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001136 atomic_inc(&qps_created);
1137 switch (init_attr->qp_type) {
1138 case IB_QPT_RC:
1139 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1140 init_attr->cap.max_inline_data = 0;
1141 } else {
1142 init_attr->cap.max_inline_data = 64;
1143 }
1144 sq_size = init_attr->cap.max_send_wr;
1145 rq_size = init_attr->cap.max_recv_wr;
1146
Glenn Streiff7495ab62008-04-29 13:46:54 -07001147 /* check if the encoded sizes are OK or not... */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001148 sq_encoded_size = nes_get_encoded_size(&sq_size);
1149 rq_encoded_size = nes_get_encoded_size(&rq_size);
1150
1151 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1152 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1153 rq_size, sq_size);
1154 return ERR_PTR(-EINVAL);
1155 }
1156
1157 init_attr->cap.max_send_wr = sq_size -2;
1158 init_attr->cap.max_recv_wr = rq_size -1;
1159 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1160
1161 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001162 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001163 if (ret) {
1164 return ERR_PTR(ret);
1165 }
1166
1167 /* Need 512 (actually now 1024) byte alignment on this structure */
1168 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1169 if (!mem) {
1170 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1171 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1172 return ERR_PTR(-ENOMEM);
1173 }
1174 u64nesqp = (unsigned long)mem;
1175 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1176 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1177 u64nesqp &= ~u64temp;
1178 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1179 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1180 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1181 nesqp->allocated_buffer = mem;
1182
1183 if (udata) {
1184 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1185 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1186 kfree(nesqp->allocated_buffer);
1187 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
Yann Droneaud9d194d12014-03-10 23:06:27 +01001188 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001189 }
1190 if (req.user_wqe_buffers) {
1191 virt_wqs = 1;
1192 }
Tatyana Nikolova43adff32014-03-11 14:20:17 -05001193 if (req.user_qp_buffer)
1194 nesqp->nesuqp_addr = req.user_qp_buffer;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001195 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1196 nesqp->user_mode = 1;
1197 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1198 if (virt_wqs) {
1199 err = 1;
1200 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1201 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1202 list_del(&nespbl->list);
1203 err = 0;
1204 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1205 nespbl, nespbl->user_base);
1206 break;
1207 }
1208 }
1209 if (err) {
1210 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1211 (long long unsigned int)req.user_wqe_buffers);
1212 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1213 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001214 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001215 }
1216 }
1217
1218 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1219 nesqp->mmap_sq_db_index =
1220 find_next_zero_bit(nes_ucontext->allocated_wqs,
1221 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1222 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1223 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001224 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001225 nes_debug(NES_DBG_QP,
1226 "db index > max user regions, failing create QP\n");
1227 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1228 if (virt_wqs) {
1229 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1230 nespbl->pbl_pbase);
1231 kfree(nespbl);
1232 }
1233 kfree(nesqp->allocated_buffer);
1234 return ERR_PTR(-ENOMEM);
1235 }
1236 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1237 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1238 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1239 } else {
1240 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1241 kfree(nesqp->allocated_buffer);
1242 return ERR_PTR(-EFAULT);
1243 }
1244 }
1245 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1246 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1247 if (err) {
1248 nes_debug(NES_DBG_QP,
1249 "error geting qp mem code = %d\n", err);
1250 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1251 kfree(nesqp->allocated_buffer);
1252 return ERR_PTR(-ENOMEM);
1253 }
1254
1255 nesqp->hwqp.sq_size = sq_size;
1256 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1257 nesqp->hwqp.sq_head = 1;
1258 nesqp->hwqp.rq_size = rq_size;
1259 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1260 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1261 (void *)nesqp->nesqp_context_pbase);
1262 */
1263 nesqp->hwqp.qp_id = qp_num;
1264 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1265 nesqp->nespd = nespd;
1266
1267 nescq = to_nescq(init_attr->send_cq);
1268 nesqp->nesscq = nescq;
1269 nescq = to_nescq(init_attr->recv_cq);
1270 nesqp->nesrcq = nescq;
1271
1272 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1273 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1274 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1275 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1276 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1277 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001278 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001279 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1280 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001281 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001282 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1283 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1284 u64temp = (u64)nesqp->hwqp.sq_pbase;
1285 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1286 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1287
1288
1289 if (!virt_wqs) {
1290 u64temp = (u64)nesqp->hwqp.sq_pbase;
1291 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1292 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1293 u64temp = (u64)nesqp->hwqp.rq_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 } else {
1297 u64temp = (u64)nesqp->pbl_pbase;
1298 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1299 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1300 }
1301
1302 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1303 nesvnic->next_qp_nic_index,
1304 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1305 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1306 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1307 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1308 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1309 nesvnic->next_qp_nic_index++;
1310 if ((nesvnic->next_qp_nic_index > 3) ||
1311 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1312 nesvnic->next_qp_nic_index = 0;
1313 }
1314 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1315
1316 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1317 u64temp = (u64)nesqp->hwqp.q2_pbase;
1318 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1319 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1320 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1321 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1322 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
Faisal Latif883c6992010-03-02 17:22:51 -06001323 NES_QPCONTEXT_ORDIRD_AAH |
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001324 ((((u32)nesadapter->max_irrq_wr) <<
1325 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1326 if (disable_mpa_crc) {
1327 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1328 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1329 }
1330
1331
1332 /* Create the QP */
1333 cqp_request = nes_get_cqp_request(nesdev);
1334 if (cqp_request == NULL) {
1335 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1336 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1337 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1338 kfree(nesqp->allocated_buffer);
1339 return ERR_PTR(-ENOMEM);
1340 }
1341 cqp_request->waiting = 1;
1342 cqp_wqe = &cqp_request->cqp_wqe;
1343
1344 if (!virt_wqs) {
1345 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1346 NES_CQP_QP_IWARP_STATE_IDLE;
1347 } else {
1348 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1349 NES_CQP_QP_IWARP_STATE_IDLE;
1350 }
1351 opcode |= NES_CQP_QP_CQS_VALID;
1352 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1353 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1354 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1355
1356 u64temp = (u64)nesqp->nesqp_context_pbase;
1357 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1358
1359 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001360 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001361
1362 /* Wait for CQP */
1363 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1364 nesqp->hwqp.qp_id);
1365 ret = wait_event_timeout(cqp_request->waitq,
1366 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1367 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1368 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1369 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1370 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1371 cqp_request->major_code, cqp_request->minor_code);
1372 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001373 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001374 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1375 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1376 kfree(nesqp->allocated_buffer);
1377 if (!ret) {
1378 return ERR_PTR(-ETIME);
1379 } else {
1380 return ERR_PTR(-EIO);
1381 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001382 }
1383
Roland Dreier1ff66e82008-07-14 23:48:49 -07001384 nes_put_cqp_request(nesdev, cqp_request);
1385
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001386 if (ibpd->uobject) {
1387 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
Dan Carpenter60429632013-07-25 20:04:59 +03001388 uresp.mmap_rq_db_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001389 uresp.actual_sq_size = sq_size;
1390 uresp.actual_rq_size = rq_size;
1391 uresp.qp_id = nesqp->hwqp.qp_id;
1392 uresp.nes_drv_opt = nes_drv_opt;
1393 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1394 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1395 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1396 kfree(nesqp->allocated_buffer);
1397 return ERR_PTR(-EFAULT);
1398 }
1399 }
1400
1401 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1402 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1403 spin_lock_init(&nesqp->lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001404 nes_add_ref(&nesqp->ibqp);
1405 break;
1406 default:
1407 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1408 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001409 }
1410
Chien Tungd14152d2009-12-09 15:21:56 -08001411 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
Tatyana Nikolova7bfcfa52012-12-06 19:58:27 +00001412 init_timer(&nesqp->terminate_timer);
1413 nesqp->terminate_timer.function = nes_terminate_timeout;
1414 nesqp->terminate_timer.data = (unsigned long)nesqp;
Chien Tungd14152d2009-12-09 15:21:56 -08001415
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001416 /* update the QP table */
1417 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1418 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00001419 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001420
1421 return &nesqp->ibqp;
1422}
1423
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001424/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001425 * nes_clean_cq
1426 */
1427static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1428{
1429 u32 cq_head;
1430 u32 lo;
1431 u32 hi;
1432 u64 u64temp;
1433 unsigned long flags = 0;
1434
1435 spin_lock_irqsave(&nescq->lock, flags);
1436
1437 cq_head = nescq->hw_cq.cq_head;
1438 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1439 rmb();
1440 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1441 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1442 u64temp = (((u64)hi) << 32) | ((u64)lo);
1443 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1444 if (u64temp == (u64)(unsigned long)nesqp) {
1445 /* Zero the context value so cqe will be ignored */
1446 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1447 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1448 }
1449
1450 if (++cq_head >= nescq->hw_cq.cq_size)
1451 cq_head = 0;
1452 }
1453
1454 spin_unlock_irqrestore(&nescq->lock, flags);
1455}
1456
1457
1458/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001459 * nes_destroy_qp
1460 */
1461static int nes_destroy_qp(struct ib_qp *ibqp)
1462{
1463 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001464 struct nes_ucontext *nes_ucontext;
1465 struct ib_qp_attr attr;
1466 struct iw_cm_id *cm_id;
1467 struct iw_cm_event cm_event;
Faisal Latif0f0bee82011-09-25 20:34:00 -05001468 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469
1470 atomic_inc(&sw_qps_destroyed);
1471 nesqp->destroyed = 1;
1472
1473 /* Blow away the connection if it exists. */
1474 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1475 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1476 attr.qp_state = IB_QPS_ERR;
1477 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1478 }
1479
1480 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1481 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1482 cm_id = nesqp->cm_id;
1483 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07001484 cm_event.status = -ETIMEDOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001485 cm_event.local_addr = cm_id->local_addr;
1486 cm_event.remote_addr = cm_id->remote_addr;
1487 cm_event.private_data = NULL;
1488 cm_event.private_data_len = 0;
1489
1490 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1491 "QP%u. cm_id = %p, refcount = %u. \n",
1492 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1493
1494 cm_id->rem_ref(cm_id);
1495 ret = cm_id->event_handler(cm_id, &cm_event);
1496 if (ret)
1497 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1498 }
1499
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001500 if (nesqp->user_mode) {
1501 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1502 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1503 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1504 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1505 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1506 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1507 }
1508 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001509 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1510 nesqp->sq_kmapped = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001511 kunmap(nesqp->page);
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001512 }
Don Wood5ee21fe2009-09-05 20:36:37 -07001513 } else {
1514 /* Clean any pending completions from the cq(s) */
1515 if (nesqp->nesscq)
1516 nes_clean_cq(nesqp, nesqp->nesscq);
1517
1518 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1519 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001520 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001521 nes_rem_ref(&nesqp->ibqp);
1522 return 0;
1523}
1524
1525
1526/**
1527 * nes_create_cq
1528 */
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001529static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1530 const struct ib_cq_init_attr *attr,
1531 struct ib_ucontext *context,
1532 struct ib_udata *udata)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001533{
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001534 int entries = attr->cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001535 u64 u64temp;
1536 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1537 struct nes_device *nesdev = nesvnic->nesdev;
1538 struct nes_adapter *nesadapter = nesdev->nesadapter;
1539 struct nes_cq *nescq;
1540 struct nes_ucontext *nes_ucontext = NULL;
1541 struct nes_cqp_request *cqp_request;
1542 void *mem = NULL;
1543 struct nes_hw_cqp_wqe *cqp_wqe;
1544 struct nes_pbl *nespbl = NULL;
1545 struct nes_create_cq_req req;
1546 struct nes_create_cq_resp resp;
1547 u32 cq_num = 0;
1548 u32 opcode = 0;
1549 u32 pbl_entries = 1;
1550 int err;
1551 unsigned long flags;
1552 int ret;
1553
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001554 if (attr->flags)
1555 return ERR_PTR(-EINVAL);
1556
Chien Tung5924aea2009-12-09 15:21:56 -08001557 if (entries > nesadapter->max_cqe)
1558 return ERR_PTR(-EINVAL);
1559
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001560 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001561 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001562 if (err) {
1563 return ERR_PTR(err);
1564 }
1565
1566 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1567 if (!nescq) {
1568 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1569 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1570 return ERR_PTR(-ENOMEM);
1571 }
1572
1573 nescq->hw_cq.cq_size = max(entries + 1, 5);
1574 nescq->hw_cq.cq_number = cq_num;
1575 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1576
1577
1578 if (context) {
1579 nes_ucontext = to_nesucontext(context);
1580 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1581 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1582 kfree(nescq);
1583 return ERR_PTR(-EFAULT);
1584 }
1585 nesvnic->mcrq_ucontext = nes_ucontext;
1586 nes_ucontext->mcrqf = req.mcrqf;
1587 if (nes_ucontext->mcrqf) {
1588 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001589 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001590 else if (nes_ucontext->mcrqf & 0x40000000)
1591 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1592 else
1593 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001594 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001595 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1596 }
1597 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1598 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001599 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001600 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1601 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1602 list_del(&nespbl->list);
1603 err = 0;
1604 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1605 nespbl);
1606 break;
1607 }
1608 }
1609 if (err) {
1610 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1611 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001612 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001613 }
1614
1615 pbl_entries = nespbl->pbl_size >> 3;
1616 nescq->cq_mem_size = 0;
1617 } else {
1618 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1619 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1620 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1621
1622 /* allocate the physical buffer space */
Joe Perches9011a672014-08-08 14:24:16 -07001623 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1624 &nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001625 if (!mem) {
1626 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1627 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1628 kfree(nescq);
1629 return ERR_PTR(-ENOMEM);
1630 }
1631
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001632 nescq->hw_cq.cq_vbase = mem;
1633 nescq->hw_cq.cq_head = 0;
1634 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1635 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1636 (u32)nescq->hw_cq.cq_pbase);
1637 }
1638
1639 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1640 spin_lock_init(&nescq->lock);
1641
1642 /* send CreateCQ request to CQP */
1643 cqp_request = nes_get_cqp_request(nesdev);
1644 if (cqp_request == NULL) {
1645 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1646 if (!context)
1647 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1648 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001649 else {
1650 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1651 nespbl->pbl_vbase, nespbl->pbl_pbase);
1652 kfree(nespbl);
1653 }
1654
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001655 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1656 kfree(nescq);
1657 return ERR_PTR(-ENOMEM);
1658 }
1659 cqp_request->waiting = 1;
1660 cqp_wqe = &cqp_request->cqp_wqe;
1661
1662 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1663 NES_CQP_CQ_CHK_OVERFLOW |
1664 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1665
1666 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1667
1668 if (pbl_entries != 1) {
1669 if (pbl_entries > 32) {
1670 /* use 4k pbl */
1671 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1672 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001673 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1674 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001675 if (!context)
1676 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1677 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001678 else {
1679 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1680 nespbl->pbl_vbase, nespbl->pbl_pbase);
1681 kfree(nespbl);
1682 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001683 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1684 kfree(nescq);
1685 return ERR_PTR(-ENOMEM);
1686 } else {
1687 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1688 nescq->virtual_cq = 2;
1689 nesadapter->free_4kpbl--;
1690 }
1691 } else {
1692 /* use 256 byte pbl */
1693 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1694 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001695 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1696 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001697 if (!context)
1698 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1699 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001700 else {
1701 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1702 nespbl->pbl_vbase, nespbl->pbl_pbase);
1703 kfree(nespbl);
1704 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001705 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1706 kfree(nescq);
1707 return ERR_PTR(-ENOMEM);
1708 } else {
1709 opcode |= NES_CQP_CQ_VIRT;
1710 nescq->virtual_cq = 1;
1711 nesadapter->free_256pbl--;
1712 }
1713 }
1714 }
1715
1716 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1717
1718 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1719 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1720 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1721 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1722
1723 if (context) {
1724 if (pbl_entries != 1)
1725 u64temp = (u64)nespbl->pbl_pbase;
1726 else
1727 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1728 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1729 nes_ucontext->mmap_db_index[0]);
1730 } else {
1731 u64temp = (u64)nescq->hw_cq.cq_pbase;
1732 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1733 }
1734 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1735 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1736 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1737 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1738 cpu_to_le32((u32)(u64temp >> 1));
1739 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1740 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1741
1742 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001743 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001744
1745 /* Wait for CQP */
1746 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1747 nescq->hw_cq.cq_number);
1748 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1749 NES_EVENT_TIMEOUT * 2);
1750 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1751 nescq->hw_cq.cq_number, ret);
1752 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001753 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001754 if (!context)
1755 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1756 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001757 else {
1758 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1759 nespbl->pbl_vbase, nespbl->pbl_pbase);
1760 kfree(nespbl);
1761 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001762 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1763 kfree(nescq);
1764 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001765 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001766 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001767
1768 if (context) {
1769 /* free the nespbl */
1770 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1771 nespbl->pbl_pbase);
1772 kfree(nespbl);
1773 resp.cq_id = nescq->hw_cq.cq_number;
1774 resp.cq_size = nescq->hw_cq.cq_size;
1775 resp.mmap_db_index = 0;
Dan Carpenter60429632013-07-25 20:04:59 +03001776 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001777 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1778 kfree(nescq);
1779 return ERR_PTR(-EFAULT);
1780 }
1781 }
1782
1783 return &nescq->ibcq;
1784}
1785
1786
1787/**
1788 * nes_destroy_cq
1789 */
1790static int nes_destroy_cq(struct ib_cq *ib_cq)
1791{
1792 struct nes_cq *nescq;
1793 struct nes_device *nesdev;
1794 struct nes_vnic *nesvnic;
1795 struct nes_adapter *nesadapter;
1796 struct nes_hw_cqp_wqe *cqp_wqe;
1797 struct nes_cqp_request *cqp_request;
1798 unsigned long flags;
1799 u32 opcode = 0;
1800 int ret;
1801
1802 if (ib_cq == NULL)
1803 return 0;
1804
1805 nescq = to_nescq(ib_cq);
1806 nesvnic = to_nesvnic(ib_cq->device);
1807 nesdev = nesvnic->nesdev;
1808 nesadapter = nesdev->nesadapter;
1809
1810 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1811
1812 /* Send DestroyCQ request to CQP */
1813 cqp_request = nes_get_cqp_request(nesdev);
1814 if (cqp_request == NULL) {
1815 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1816 return -ENOMEM;
1817 }
1818 cqp_request->waiting = 1;
1819 cqp_wqe = &cqp_request->cqp_wqe;
1820 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1821 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1822 if (nescq->virtual_cq == 1) {
1823 nesadapter->free_256pbl++;
1824 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1825 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001826 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001827 }
1828 } else if (nescq->virtual_cq == 2) {
1829 nesadapter->free_4kpbl++;
1830 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1831 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001832 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001833 }
1834 opcode |= NES_CQP_CQ_4KB_CHUNK;
1835 }
1836
1837 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1838
1839 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1840 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1841 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1842 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001843 if (!nescq->mcrqf)
1844 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1845
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001846 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001847 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001848
1849 /* Wait for CQP */
1850 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1851 nescq->hw_cq.cq_number);
1852 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1853 NES_EVENT_TIMEOUT);
1854 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1855 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1856 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1857 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001858 if (!ret) {
1859 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001860 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001861 ret = -ETIME;
1862 } else if (cqp_request->major_code) {
1863 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001864 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001865 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001866 } else {
1867 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001868 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001869 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001870
1871 if (nescq->cq_mem_size)
1872 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001873 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001874 kfree(nescq);
1875
1876 return ret;
1877}
1878
Don Wood0145f342009-03-06 15:15:00 -08001879/**
1880 * root_256
1881 */
1882static u32 root_256(struct nes_device *nesdev,
1883 struct nes_root_vpbl *root_vpbl,
1884 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001885 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001886{
1887 u64 leaf_pbl;
1888 int i, j, k;
1889
1890 if (pbl_count_4k == 1) {
1891 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1892 512, &new_root->pbl_pbase);
1893
1894 if (new_root->pbl_vbase == NULL)
1895 return 0;
1896
1897 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1898 for (i = 0; i < 16; i++) {
1899 new_root->pbl_vbase[i].pa_low =
1900 cpu_to_le32((u32)leaf_pbl);
1901 new_root->pbl_vbase[i].pa_high =
1902 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1903 leaf_pbl += 256;
1904 }
1905 } else {
1906 for (i = 3; i >= 0; i--) {
1907 j = i * 16;
1908 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1909 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1910 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1911 << 32);
1912 for (k = 1; k < 16; k++) {
1913 leaf_pbl += 256;
1914 root_vpbl->pbl_vbase[j + k].pa_low =
1915 cpu_to_le32((u32)leaf_pbl);
1916 root_vpbl->pbl_vbase[j + k].pa_high =
1917 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1918 }
1919 }
1920 }
1921
1922 return 1;
1923}
1924
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001925
1926/**
1927 * nes_reg_mr
1928 */
1929static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1930 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08001931 dma_addr_t single_buffer, u16 pbl_count_4k,
1932 u16 residual_page_count_4k, int acc, u64 *iova_start,
1933 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001934{
1935 struct nes_hw_cqp_wqe *cqp_wqe;
1936 struct nes_cqp_request *cqp_request;
1937 unsigned long flags;
1938 int ret;
1939 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08001940 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07001941 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08001942 u16 pbl_count = 0;
1943 u8 use_256_pbls = 0;
1944 u8 use_4k_pbls = 0;
1945 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
Or Gerlitz812d8672010-07-20 12:00:20 +00001946 struct nes_root_vpbl new_root = { 0, NULL, NULL };
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001947 u32 opcode = 0;
1948 u16 major_code;
1949
1950 /* Register the region with the adapter */
1951 cqp_request = nes_get_cqp_request(nesdev);
1952 if (cqp_request == NULL) {
1953 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1954 return -ENOMEM;
1955 }
1956 cqp_request->waiting = 1;
1957 cqp_wqe = &cqp_request->cqp_wqe;
1958
Don Wood0145f342009-03-06 15:15:00 -08001959 if (pbl_count_4k) {
1960 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1961
1962 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1963 pbl_count_256 = (pg_cnt + 31) / 32;
1964 if (pg_cnt <= 32) {
1965 if (pbl_count_256 <= nesadapter->free_256pbl)
1966 use_256_pbls = 1;
1967 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1968 use_4k_pbls = 1;
1969 } else if (pg_cnt <= 2048) {
1970 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1971 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1972 use_4k_pbls = 1;
1973 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1974 use_256_pbls = 1;
1975 use_two_level = 1;
1976 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1977 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001978 }
1979 } else {
Don Wood0145f342009-03-06 15:15:00 -08001980 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1981 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001982 }
Don Wood0145f342009-03-06 15:15:00 -08001983
1984 if (use_256_pbls) {
1985 pbl_count = pbl_count_256;
1986 nesadapter->free_256pbl -= pbl_count + use_two_level;
1987 } else if (use_4k_pbls) {
1988 pbl_count = pbl_count_4k;
1989 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1990 } else {
1991 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1992 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1993 nes_free_cqp_request(nesdev, cqp_request);
1994 return -ENOMEM;
1995 }
1996
1997 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001998 }
1999
Don Wood0145f342009-03-06 15:15:00 -08002000 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07002001 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08002002 if (new_root.pbl_pbase != 0)
2003 root_vpbl = &new_root;
2004 } else {
2005 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2006 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
2007 use_256_pbls = 0;
2008
2009 if (pbl_count_4k == 1)
2010 use_two_level = 0;
2011 pbl_count = pbl_count_4k;
2012
2013 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2014 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2015 use_4k_pbls = 1;
2016 }
2017 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2018
2019 if (use_4k_pbls == 0)
2020 return -ENOMEM;
2021 }
2022 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002023
2024 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2025 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2026 if (acc & IB_ACCESS_LOCAL_WRITE)
2027 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2028 if (acc & IB_ACCESS_REMOTE_WRITE)
2029 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2030 if (acc & IB_ACCESS_REMOTE_READ)
2031 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2032 if (acc & IB_ACCESS_MW_BIND)
2033 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2034
2035 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2036 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2037 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2038 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2039
2040 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2041 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2042 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2043 cpu_to_le32(nespd->pd_id & 0x00007fff);
2044 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2045
2046 if (pbl_count == 0) {
2047 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2048 } else {
2049 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2050 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002051 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002052
Don Wood0145f342009-03-06 15:15:00 -08002053 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002054 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2055 }
2056 barrier();
2057
2058 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002059 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002060
2061 /* Wait for CQP */
2062 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2063 NES_EVENT_TIMEOUT);
2064 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2065 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2066 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2067 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002068 nes_put_cqp_request(nesdev, cqp_request);
2069
Don Wooddae5d132009-03-06 15:12:09 -08002070 if ((!ret || major_code) && pbl_count != 0) {
2071 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08002072 if (use_256_pbls)
2073 nesadapter->free_256pbl += pbl_count + use_two_level;
2074 else if (use_4k_pbls)
2075 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08002076 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2077 }
Don Wood0145f342009-03-06 15:15:00 -08002078 if (new_root.pbl_pbase)
2079 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2080 new_root.pbl_pbase);
2081
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002082 if (!ret)
2083 return -ETIME;
2084 else if (major_code)
2085 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002086
Don Wood0145f342009-03-06 15:15:00 -08002087 *actual_pbl_cnt = pbl_count + use_two_level;
2088 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002089 return 0;
2090}
2091
2092
2093/**
2094 * nes_reg_phys_mr
2095 */
2096static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2097 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2098 u64 * iova_start)
2099{
2100 u64 region_length;
2101 struct nes_pd *nespd = to_nespd(ib_pd);
2102 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2103 struct nes_device *nesdev = nesvnic->nesdev;
2104 struct nes_adapter *nesadapter = nesdev->nesadapter;
2105 struct nes_mr *nesmr;
2106 struct ib_mr *ibmr;
2107 struct nes_vpbl vpbl;
2108 struct nes_root_vpbl root_vpbl;
2109 u32 stag;
2110 u32 i;
Don Wood3f32eb12009-04-20 14:53:00 -07002111 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002112 u32 stag_index = 0;
2113 u32 next_stag_index = 0;
2114 u32 driver_key = 0;
2115 u32 root_pbl_index = 0;
2116 u32 cur_pbl_index = 0;
Roland Dreier69d51022010-08-04 14:25:40 -07002117 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002118 int ret = 0;
2119 u16 pbl_count = 0;
2120 u8 single_page = 1;
2121 u8 stag_key = 0;
2122
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002123 region_length = 0;
2124 vpbl.pbl_vbase = NULL;
2125 root_vpbl.pbl_vbase = NULL;
2126 root_vpbl.pbl_pbase = 0;
2127
2128 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2129 stag_key = (u8)next_stag_index;
2130
2131 driver_key = 0;
2132
2133 next_stag_index >>= 8;
2134 next_stag_index %= nesadapter->max_mr;
2135 if (num_phys_buf > (1024*512)) {
2136 return ERR_PTR(-E2BIG);
2137 }
2138
Don Wood3f32eb12009-04-20 14:53:00 -07002139 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2140 return ERR_PTR(-EINVAL);
2141
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002142 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002143 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002144 if (err) {
2145 return ERR_PTR(err);
2146 }
2147
2148 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2149 if (!nesmr) {
2150 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2151 return ERR_PTR(-ENOMEM);
2152 }
2153
2154 for (i = 0; i < num_phys_buf; i++) {
2155
2156 if ((i & 0x01FF) == 0) {
2157 if (root_pbl_index == 1) {
2158 /* Allocate the root PBL */
2159 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2160 &root_vpbl.pbl_pbase);
2161 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2162 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2163 if (!root_vpbl.pbl_vbase) {
2164 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2165 vpbl.pbl_pbase);
2166 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2167 kfree(nesmr);
2168 return ERR_PTR(-ENOMEM);
2169 }
2170 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2171 if (!root_vpbl.leaf_vpbl) {
2172 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2173 root_vpbl.pbl_pbase);
2174 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2175 vpbl.pbl_pbase);
2176 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2177 kfree(nesmr);
2178 return ERR_PTR(-ENOMEM);
2179 }
2180 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2181 root_vpbl.pbl_vbase[0].pa_high =
2182 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2183 root_vpbl.leaf_vpbl[0] = vpbl;
2184 }
2185 /* Allocate a 4K buffer for the PBL */
2186 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2187 &vpbl.pbl_pbase);
2188 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2189 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2190 if (!vpbl.pbl_vbase) {
2191 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2192 ibmr = ERR_PTR(-ENOMEM);
2193 kfree(nesmr);
2194 goto reg_phys_err;
2195 }
2196 /* Fill in the root table */
2197 if (1 <= root_pbl_index) {
2198 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2199 cpu_to_le32((u32)vpbl.pbl_pbase);
2200 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2201 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2202 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2203 }
2204 root_pbl_index++;
2205 cur_pbl_index = 0;
2206 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002207
Don Wood3f32eb12009-04-20 14:53:00 -07002208 mask = !buffer_list[i].size;
2209 if (i != 0)
2210 mask |= buffer_list[i].addr;
2211 if (i != num_phys_buf - 1)
2212 mask |= buffer_list[i].addr + buffer_list[i].size;
2213
2214 if (mask & ~PAGE_MASK) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002215 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
Don Wood3f32eb12009-04-20 14:53:00 -07002216 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002217 ibmr = ERR_PTR(-EINVAL);
2218 kfree(nesmr);
2219 goto reg_phys_err;
2220 }
2221
2222 region_length += buffer_list[i].size;
2223 if ((i != 0) && (single_page)) {
2224 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2225 single_page = 0;
2226 }
Don Wood3f32eb12009-04-20 14:53:00 -07002227 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002228 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2229 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2230 }
2231
2232 stag = stag_index << 8;
2233 stag |= driver_key;
2234 stag += (u32)stag_key;
2235
2236 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2237 " length = 0x%016lX, index = 0x%08X\n",
2238 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2239
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002240 /* Make the leaf PBL the root if only one PBL */
2241 if (root_pbl_index == 1) {
2242 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2243 }
2244
2245 if (single_page) {
2246 pbl_count = 0;
2247 } else {
2248 pbl_count = root_pbl_index;
2249 }
2250 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08002251 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2252 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002253
2254 if (ret == 0) {
2255 nesmr->ibmr.rkey = stag;
2256 nesmr->ibmr.lkey = stag;
2257 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2258 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002259 } else {
2260 kfree(nesmr);
2261 ibmr = ERR_PTR(-ENOMEM);
2262 }
2263
2264 reg_phys_err:
2265 /* free the resources */
2266 if (root_pbl_index == 1) {
2267 /* single PBL case */
2268 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2269 } else {
2270 for (i=0; i<root_pbl_index; i++) {
2271 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2272 root_vpbl.leaf_vpbl[i].pbl_pbase);
2273 }
2274 kfree(root_vpbl.leaf_vpbl);
2275 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2276 root_vpbl.pbl_pbase);
2277 }
2278
2279 return ibmr;
2280}
2281
2282
2283/**
2284 * nes_get_dma_mr
2285 */
2286static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2287{
2288 struct ib_phys_buf bl;
2289 u64 kva = 0;
2290
2291 nes_debug(NES_DBG_MR, "\n");
2292
2293 bl.size = (u64)0xffffffffffULL;
2294 bl.addr = 0;
2295 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2296}
2297
2298
2299/**
2300 * nes_reg_user_mr
2301 */
2302static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2303 u64 virt, int acc, struct ib_udata *udata)
2304{
2305 u64 iova_start;
2306 __le64 *pbl;
2307 u64 region_length;
2308 dma_addr_t last_dma_addr = 0;
2309 dma_addr_t first_dma_addr = 0;
2310 struct nes_pd *nespd = to_nespd(pd);
2311 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2312 struct nes_device *nesdev = nesvnic->nesdev;
2313 struct nes_adapter *nesadapter = nesdev->nesadapter;
2314 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002315 struct scatterlist *sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002316 struct nes_ucontext *nes_ucontext;
2317 struct nes_pbl *nespbl;
2318 struct nes_mr *nesmr;
2319 struct ib_umem *region;
2320 struct nes_mem_reg_req req;
2321 struct nes_vpbl vpbl;
2322 struct nes_root_vpbl root_vpbl;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002323 int entry, page_index;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002324 int page_count = 0;
2325 int err, pbl_depth = 0;
2326 int chunk_pages;
2327 int ret;
2328 u32 stag;
2329 u32 stag_index = 0;
2330 u32 next_stag_index;
2331 u32 driver_key;
2332 u32 root_pbl_index = 0;
2333 u32 cur_pbl_index = 0;
2334 u32 skip_pages;
2335 u16 pbl_count;
2336 u8 single_page = 1;
2337 u8 stag_key;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002338 int first_page = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002339
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002340 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002341 if (IS_ERR(region)) {
2342 return (struct ib_mr *)region;
2343 }
2344
2345 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2346 " offset = %u, page size = %u.\n",
2347 (unsigned long int)start, (unsigned long int)virt, (u32)length,
Haggai Eran406f9e52014-12-11 17:04:12 +02002348 ib_umem_offset(region), region->page_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002349
Haggai Eran406f9e52014-12-11 17:04:12 +02002350 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002351
Julia Lawalle2e435f2011-10-06 09:33:24 -07002352 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2353 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002354 return ERR_PTR(-EFAULT);
Julia Lawalle2e435f2011-10-06 09:33:24 -07002355 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002356 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2357
2358 switch (req.reg_type) {
2359 case IWNES_MEMREG_TYPE_MEM:
2360 pbl_depth = 0;
2361 region_length = 0;
2362 vpbl.pbl_vbase = NULL;
2363 root_vpbl.pbl_vbase = NULL;
2364 root_vpbl.pbl_pbase = 0;
2365
2366 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2367 stag_key = (u8)next_stag_index;
2368
2369 driver_key = next_stag_index & 0x70000000;
2370
2371 next_stag_index >>= 8;
2372 next_stag_index %= nesadapter->max_mr;
2373
2374 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002375 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002376 if (err) {
2377 ib_umem_release(region);
2378 return ERR_PTR(err);
2379 }
2380
2381 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2382 if (!nesmr) {
2383 ib_umem_release(region);
2384 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2385 return ERR_PTR(-ENOMEM);
2386 }
2387 nesmr->region = region;
2388
Yishai Hadaseeb84612014-01-28 13:40:15 +02002389 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2390 if (sg_dma_address(sg) & ~PAGE_MASK) {
2391 ib_umem_release(region);
2392 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2393 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2394 (unsigned int) sg_dma_address(sg));
2395 ibmr = ERR_PTR(-EINVAL);
2396 kfree(nesmr);
2397 goto reg_user_mr_err;
2398 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002399
Yishai Hadaseeb84612014-01-28 13:40:15 +02002400 if (!sg_dma_len(sg)) {
2401 ib_umem_release(region);
2402 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2403 stag_index);
2404 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2405 ibmr = ERR_PTR(-EINVAL);
2406 kfree(nesmr);
2407 goto reg_user_mr_err;
2408 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002409
Yishai Hadaseeb84612014-01-28 13:40:15 +02002410 region_length += sg_dma_len(sg);
2411 chunk_pages = sg_dma_len(sg) >> 12;
2412 region_length -= skip_pages << 12;
2413 for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2414 skip_pages = 0;
Haggai Eran406f9e52014-12-11 17:04:12 +02002415 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
Yishai Hadaseeb84612014-01-28 13:40:15 +02002416 goto enough_pages;
2417 if ((page_count&0x01FF) == 0) {
2418 if (page_count >= 1024 * 512) {
2419 ib_umem_release(region);
2420 nes_free_resource(nesadapter,
2421 nesadapter->allocated_mrs, stag_index);
2422 kfree(nesmr);
2423 ibmr = ERR_PTR(-E2BIG);
2424 goto reg_user_mr_err;
2425 }
2426 if (root_pbl_index == 1) {
2427 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2428 8192, &root_vpbl.pbl_pbase);
2429 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2430 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2431 if (!root_vpbl.pbl_vbase) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002432 ib_umem_release(region);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002433 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2434 vpbl.pbl_pbase);
2435 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2436 stag_index);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002437 kfree(nesmr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002438 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002439 goto reg_user_mr_err;
2440 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002441 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2442 GFP_KERNEL);
2443 if (!root_vpbl.leaf_vpbl) {
2444 ib_umem_release(region);
2445 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2446 root_vpbl.pbl_pbase);
2447 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2448 vpbl.pbl_pbase);
2449 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2450 stag_index);
2451 kfree(nesmr);
2452 ibmr = ERR_PTR(-ENOMEM);
2453 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002454 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002455 root_vpbl.pbl_vbase[0].pa_low =
2456 cpu_to_le32((u32)vpbl.pbl_pbase);
2457 root_vpbl.pbl_vbase[0].pa_high =
2458 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2459 root_vpbl.leaf_vpbl[0] = vpbl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002460 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002461 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2462 &vpbl.pbl_pbase);
2463 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2464 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2465 if (!vpbl.pbl_vbase) {
2466 ib_umem_release(region);
2467 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2468 ibmr = ERR_PTR(-ENOMEM);
2469 kfree(nesmr);
2470 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002471 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002472 if (1 <= root_pbl_index) {
2473 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2474 cpu_to_le32((u32)vpbl.pbl_pbase);
2475 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2476 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2477 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2478 }
2479 root_pbl_index++;
2480 cur_pbl_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002481 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002482 if (single_page) {
2483 if (page_count != 0) {
2484 if ((last_dma_addr+4096) !=
2485 (sg_dma_address(sg)+
2486 (page_index*4096)))
2487 single_page = 0;
2488 last_dma_addr = sg_dma_address(sg)+
2489 (page_index*4096);
2490 } else {
2491 first_dma_addr = sg_dma_address(sg)+
2492 (page_index*4096);
2493 last_dma_addr = first_dma_addr;
2494 }
2495 }
2496
2497 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2498 cpu_to_le32((u32)(sg_dma_address(sg)+
2499 (page_index*4096)));
2500 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2501 cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2502 (page_index*4096))) >> 32)));
2503 cur_pbl_index++;
2504 page_count++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002505 }
2506 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002507
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002508 enough_pages:
2509 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2510 " stag_key=0x%08x\n",
2511 stag_index, driver_key, stag_key);
2512 stag = stag_index << 8;
2513 stag |= driver_key;
2514 stag += (u32)stag_key;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002515
2516 iova_start = virt;
2517 /* Make the leaf PBL the root if only one PBL */
2518 if (root_pbl_index == 1) {
2519 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2520 }
2521
2522 if (single_page) {
2523 pbl_count = 0;
2524 } else {
2525 pbl_count = root_pbl_index;
2526 first_dma_addr = 0;
2527 }
2528 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2529 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2530 stag, (unsigned int)iova_start,
2531 (unsigned int)region_length, stag_index,
2532 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002533 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2534 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2535 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002536
2537 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2538
2539 if (ret == 0) {
2540 nesmr->ibmr.rkey = stag;
2541 nesmr->ibmr.lkey = stag;
2542 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2543 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002544 } else {
2545 ib_umem_release(region);
2546 kfree(nesmr);
2547 ibmr = ERR_PTR(-ENOMEM);
2548 }
2549
2550 reg_user_mr_err:
2551 /* free the resources */
2552 if (root_pbl_index == 1) {
2553 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2554 vpbl.pbl_pbase);
2555 } else {
2556 for (page_index=0; page_index<root_pbl_index; page_index++) {
2557 pci_free_consistent(nesdev->pcidev, 4096,
2558 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2559 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2560 }
2561 kfree(root_vpbl.leaf_vpbl);
2562 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2563 root_vpbl.pbl_pbase);
2564 }
2565
2566 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2567
2568 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002569 case IWNES_MEMREG_TYPE_QP:
2570 case IWNES_MEMREG_TYPE_CQ:
Tatyana Nikolova7d9c1992012-12-06 20:05:02 +00002571 if (!region->length) {
2572 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2573 ib_umem_release(region);
2574 return ERR_PTR(-EINVAL);
2575 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002576 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2577 if (!nespbl) {
2578 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2579 ib_umem_release(region);
2580 return ERR_PTR(-ENOMEM);
2581 }
2582 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2583 if (!nesmr) {
2584 ib_umem_release(region);
2585 kfree(nespbl);
2586 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2587 return ERR_PTR(-ENOMEM);
2588 }
2589 nesmr->region = region;
2590 nes_ucontext = to_nesucontext(pd->uobject->context);
2591 pbl_depth = region->length >> 12;
2592 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2593 nespbl->pbl_size = pbl_depth*sizeof(u64);
2594 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2595 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2596 } else {
2597 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2598 }
2599
2600 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2601 nespbl->pbl_size, pbl_depth);
2602 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2603 &nespbl->pbl_pbase);
2604 if (!pbl) {
2605 ib_umem_release(region);
2606 kfree(nesmr);
2607 kfree(nespbl);
2608 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2609 return ERR_PTR(-ENOMEM);
2610 }
2611
2612 nespbl->pbl_vbase = (u64 *)pbl;
2613 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002614 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002615 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002616 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2617 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002618
Yishai Hadaseeb84612014-01-28 13:40:15 +02002619 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2620 chunk_pages = sg_dma_len(sg) >> 12;
2621 chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2622 if (first_page) {
2623 nespbl->page = sg_page(sg);
2624 first_page = 0;
2625 }
2626
2627 for (page_index = 0; page_index < chunk_pages; page_index++) {
2628 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2629 (sg_dma_address(sg)+
2630 (page_index*4096)));
2631 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2632 (sg_dma_address(sg)+
2633 (page_index*4096)))>>32);
2634 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2635 (unsigned long long)*pbl,
2636 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2637 pbl++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002638 }
2639 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002640
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002641 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2642 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2643 } else {
2644 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2645 }
2646 nesmr->ibmr.rkey = -1;
2647 nesmr->ibmr.lkey = -1;
2648 nesmr->mode = req.reg_type;
2649 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002650 }
2651
Julia Lawalle2e435f2011-10-06 09:33:24 -07002652 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002653 return ERR_PTR(-ENOSYS);
2654}
2655
2656
2657/**
2658 * nes_dereg_mr
2659 */
2660static int nes_dereg_mr(struct ib_mr *ib_mr)
2661{
2662 struct nes_mr *nesmr = to_nesmr(ib_mr);
2663 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2664 struct nes_device *nesdev = nesvnic->nesdev;
2665 struct nes_adapter *nesadapter = nesdev->nesadapter;
2666 struct nes_hw_cqp_wqe *cqp_wqe;
2667 struct nes_cqp_request *cqp_request;
2668 unsigned long flags;
2669 int ret;
2670 u16 major_code;
2671 u16 minor_code;
2672
2673 if (nesmr->region) {
2674 ib_umem_release(nesmr->region);
2675 }
2676 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2677 kfree(nesmr);
2678 return 0;
2679 }
2680
2681 /* Deallocate the region with the adapter */
2682
2683 cqp_request = nes_get_cqp_request(nesdev);
2684 if (cqp_request == NULL) {
2685 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2686 return -ENOMEM;
2687 }
2688 cqp_request->waiting = 1;
2689 cqp_wqe = &cqp_request->cqp_wqe;
2690
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002691 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2692 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2693 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2694 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2695 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2696
2697 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002698 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002699
2700 /* Wait for CQP */
2701 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2702 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2703 NES_EVENT_TIMEOUT);
2704 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2705 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2706 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2707
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002708 major_code = cqp_request->major_code;
2709 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002710
2711 nes_put_cqp_request(nesdev, cqp_request);
2712
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002713 if (!ret) {
2714 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2715 " ib_mr=%p, rkey = 0x%08X\n",
2716 ib_mr, ib_mr->rkey);
2717 return -ETIME;
2718 } else if (major_code) {
2719 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2720 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2721 major_code, minor_code, ib_mr, ib_mr->rkey);
2722 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002723 }
2724
2725 if (nesmr->pbls_used != 0) {
2726 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2727 if (nesmr->pbl_4k) {
2728 nesadapter->free_4kpbl += nesmr->pbls_used;
2729 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2730 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2731 "exceeded the max(%u)\n",
2732 nesadapter->free_4kpbl,
2733 nesadapter->max_4kpbl);
2734 } else {
2735 nesadapter->free_256pbl += nesmr->pbls_used;
2736 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2737 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2738 "exceeded the max(%u)\n",
2739 nesadapter->free_256pbl,
2740 nesadapter->max_256pbl);
2741 }
2742 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2743 }
2744 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2745 (ib_mr->rkey & 0x0fffff00) >> 8);
2746
2747 kfree(nesmr);
2748
2749 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002750}
2751
2752
2753/**
2754 * show_rev
2755 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002756static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2757 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002758{
2759 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002760 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002761 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2762
2763 nes_debug(NES_DBG_INIT, "\n");
2764 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2765}
2766
2767
2768/**
2769 * show_fw_ver
2770 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002771static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2772 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002773{
2774 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002775 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002776 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2777
2778 nes_debug(NES_DBG_INIT, "\n");
Chien Tung0e4562d2009-04-27 13:33:48 -07002779 return sprintf(buf, "%u.%u\n",
2780 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2781 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002782}
2783
2784
2785/**
2786 * show_hca
2787 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002788static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2789 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002790{
2791 nes_debug(NES_DBG_INIT, "\n");
2792 return sprintf(buf, "NES020\n");
2793}
2794
2795
2796/**
2797 * show_board
2798 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002799static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2800 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002801{
2802 nes_debug(NES_DBG_INIT, "\n");
2803 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2804}
2805
2806
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002807static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2808static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2809static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2810static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002811
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002812static struct device_attribute *nes_dev_attributes[] = {
2813 &dev_attr_hw_rev,
2814 &dev_attr_fw_ver,
2815 &dev_attr_hca_type,
2816 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002817};
2818
2819
2820/**
2821 * nes_query_qp
2822 */
2823static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2824 int attr_mask, struct ib_qp_init_attr *init_attr)
2825{
2826 struct nes_qp *nesqp = to_nesqp(ibqp);
2827
2828 nes_debug(NES_DBG_QP, "\n");
2829
2830 attr->qp_access_flags = 0;
2831 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2832 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2833 attr->cap.max_recv_sge = 1;
Chien Tungeadde3a2010-03-25 13:39:50 +00002834 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2835 attr->cap.max_inline_data = 0;
2836 else
2837 attr->cap.max_inline_data = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002838
2839 init_attr->event_handler = nesqp->ibqp.event_handler;
2840 init_attr->qp_context = nesqp->ibqp.qp_context;
2841 init_attr->send_cq = nesqp->ibqp.send_cq;
2842 init_attr->recv_cq = nesqp->ibqp.recv_cq;
Dave Jones4127c362013-09-17 17:45:54 -04002843 init_attr->srq = nesqp->ibqp.srq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002844 init_attr->cap = attr->cap;
2845
2846 return 0;
2847}
2848
2849
2850/**
2851 * nes_hw_modify_qp
2852 */
2853int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002854 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002855{
2856 struct nes_hw_cqp_wqe *cqp_wqe;
2857 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2858 /* struct iw_cm_event cm_event; */
2859 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002860 int ret;
2861 u16 major_code;
2862
2863 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2864 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2865
2866 cqp_request = nes_get_cqp_request(nesdev);
2867 if (cqp_request == NULL) {
2868 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2869 return -ENOMEM;
2870 }
2871 if (wait_completion) {
2872 cqp_request->waiting = 1;
2873 } else {
2874 cqp_request->waiting = 0;
2875 }
2876 cqp_wqe = &cqp_request->cqp_wqe;
2877
2878 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2879 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2880 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2881 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2882 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2883 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2884 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2885
Don Wood8b1c9dc2009-09-05 20:36:38 -07002886 /* If sending a terminate message, fill in the length (in words) */
2887 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2888 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2889 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2890 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2891 }
2892
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002893 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002894 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002895
2896 /* Wait for CQP */
2897 if (wait_completion) {
2898 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2899 nesqp->hwqp.qp_id); */
2900 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2901 NES_EVENT_TIMEOUT);
2902 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2903 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2904 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2905 major_code = cqp_request->major_code;
2906 if (major_code) {
2907 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2908 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2909 nesqp->hwqp.qp_id, cqp_request->major_code,
2910 cqp_request->minor_code, next_iwarp_state);
2911 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002912
2913 nes_put_cqp_request(nesdev, cqp_request);
2914
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002915 if (!ret)
2916 return -ETIME;
2917 else if (major_code)
2918 return -EIO;
2919 else
2920 return 0;
2921 } else {
2922 return 0;
2923 }
2924}
2925
2926
2927/**
2928 * nes_modify_qp
2929 */
2930int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2931 int attr_mask, struct ib_udata *udata)
2932{
2933 struct nes_qp *nesqp = to_nesqp(ibqp);
2934 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2935 struct nes_device *nesdev = nesvnic->nesdev;
2936 /* u32 cqp_head; */
2937 /* u32 counter; */
2938 u32 next_iwarp_state = 0;
2939 int err;
2940 unsigned long qplockflags;
2941 int ret;
2942 u16 original_last_aeq;
2943 u8 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002944 u8 dont_wait = 0;
2945
2946 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2947 " iwarp_state=0x%X, refcount=%d\n",
2948 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2949 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2950
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002951 spin_lock_irqsave(&nesqp->lock, qplockflags);
2952
2953 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2954 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2955 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2956 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2957
2958 if (attr_mask & IB_QP_STATE) {
2959 switch (attr->qp_state) {
2960 case IB_QPS_INIT:
2961 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2962 nesqp->hwqp.qp_id);
2963 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2964 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002965 return -EINVAL;
2966 }
2967 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2968 issue_modify_qp = 1;
2969 break;
2970 case IB_QPS_RTR:
2971 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2972 nesqp->hwqp.qp_id);
2973 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2974 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002975 return -EINVAL;
2976 }
2977 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2978 issue_modify_qp = 1;
2979 break;
2980 case IB_QPS_RTS:
2981 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2982 nesqp->hwqp.qp_id);
2983 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2984 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002985 return -EINVAL;
2986 }
2987 if (nesqp->cm_id == NULL) {
2988 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2989 nesqp->hwqp.qp_id );
2990 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002991 return -EINVAL;
2992 }
2993 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2994 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2995 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2996 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2997 issue_modify_qp = 1;
2998 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2999 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3000 nesqp->hte_added = 1;
3001 break;
3002 case IB_QPS_SQD:
3003 issue_modify_qp = 1;
3004 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3005 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3006 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3007 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003008 return 0;
3009 } else {
3010 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3011 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3012 " ignored due to current iWARP state\n",
3013 nesqp->hwqp.qp_id);
3014 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003015 return -EINVAL;
3016 }
3017 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3018 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3019 " already done based on hw state.\n",
3020 nesqp->hwqp.qp_id);
3021 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003022 }
3023 switch (nesqp->hw_iwarp_state) {
3024 case NES_AEQE_IWARP_STATE_CLOSING:
3025 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Tatyana Nikolovad5fb4762012-10-02 16:38:12 +00003026 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003027 case NES_AEQE_IWARP_STATE_TERMINATE:
3028 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3029 break;
3030 case NES_AEQE_IWARP_STATE_ERROR:
3031 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3032 break;
3033 default:
3034 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003035 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3036 break;
3037 }
3038 }
3039 break;
3040 case IB_QPS_SQE:
3041 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3042 nesqp->hwqp.qp_id);
3043 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3044 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003045 return -EINVAL;
3046 }
3047 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3048 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3049 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3050 issue_modify_qp = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051 break;
3052 case IB_QPS_ERR:
3053 case IB_QPS_RESET:
3054 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3055 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003056 return -EINVAL;
3057 }
3058 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3059 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07003060 if (nesqp->term_flags)
3061 del_timer(&nesqp->terminate_timer);
3062
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003063 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3064 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3065 if (nesqp->hte_added) {
3066 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3067 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3068 nesqp->hte_added = 0;
3069 }
3070 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
Faisal Latifcd6860e2010-07-04 00:17:59 +00003071 (nesdev->iw_status) &&
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003072 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3073 next_iwarp_state |= NES_CQP_QP_RESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003074 } else {
3075 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3076 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3077 dont_wait = 1;
3078 }
3079 issue_modify_qp = 1;
3080 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3081 break;
3082 default:
3083 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003084 return -EINVAL;
3085 break;
3086 }
3087
3088 nesqp->ibqp_state = attr->qp_state;
Tatyana Nikolovaa67b0782012-10-02 16:40:33 +00003089 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3090 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3091 nesqp->iwarp_state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003092 }
3093
3094 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3095 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3096 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3097 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3098 issue_modify_qp = 1;
3099 }
3100 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3101 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3102 issue_modify_qp = 1;
3103 }
3104 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3105 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3106 issue_modify_qp = 1;
3107 }
3108 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3109 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3110 issue_modify_qp = 1;
3111 }
3112
3113 if (nesqp->user_mode) {
3114 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3115 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3116 issue_modify_qp = 1;
3117 }
3118 }
3119
3120 original_last_aeq = nesqp->last_aeq;
3121 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3122
3123 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3124
3125 ret = 0;
3126
3127
3128 if (issue_modify_qp) {
3129 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07003130 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003131 if (ret)
3132 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3133 " failed for QP%u.\n",
3134 next_iwarp_state, nesqp->hwqp.qp_id);
3135
3136 }
3137
3138 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3139 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP 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);
Dan Carpenter349850f2014-02-06 15:48:23 +03003143 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003144 if (dont_wait) {
3145 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3146 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3147 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3148 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3149 original_last_aeq, nesqp->last_aeq);
3150 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003151 spin_lock_irqsave(&nesqp->lock, qplockflags);
3152 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3153 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3154 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3155 nes_cm_disconn(nesqp);
3156 } else {
3157 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3158 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003159 }
3160 } else {
3161 spin_lock_irqsave(&nesqp->lock, qplockflags);
3162 if (nesqp->cm_id) {
3163 /* These two are for the timer thread */
3164 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003165 nesqp->cm_id->add_ref(nesqp->cm_id);
3166 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3167 " need ae to finish up, original_last_aeq = 0x%04X."
3168 " last_aeq = 0x%04X, scheduling timer.\n",
3169 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3170 original_last_aeq, nesqp->last_aeq);
3171 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3172 }
3173 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3174 } else {
3175 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3176 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3177 " need ae to finish up, original_last_aeq = 0x%04X."
3178 " last_aeq = 0x%04X.\n",
3179 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3180 original_last_aeq, nesqp->last_aeq);
3181 }
3182 }
3183 } else {
3184 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3185 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3186 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3187 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003188 }
3189 } else {
3190 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3191 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3192 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3193 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003194 }
3195
3196 err = 0;
3197
3198 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3199 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3200
3201 return err;
3202}
3203
3204
3205/**
3206 * nes_muticast_attach
3207 */
3208static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3209{
3210 nes_debug(NES_DBG_INIT, "\n");
3211 return -ENOSYS;
3212}
3213
3214
3215/**
3216 * nes_multicast_detach
3217 */
3218static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3219{
3220 nes_debug(NES_DBG_INIT, "\n");
3221 return -ENOSYS;
3222}
3223
3224
3225/**
3226 * nes_process_mad
3227 */
3228static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
Ira Weinya97e2d82015-05-31 17:15:30 -04003229 u8 port_num, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
3230 const struct ib_mad *in_mad, struct ib_mad *out_mad)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231{
3232 nes_debug(NES_DBG_INIT, "\n");
3233 return -ENOSYS;
3234}
3235
3236static inline void
3237fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3238{
3239 int sge_index;
3240 int total_payload_length = 0;
3241 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3242 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3243 ib_wr->sg_list[sge_index].addr);
3244 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3245 ib_wr->sg_list[sge_index].length);
3246 if (uselkey)
3247 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3248 (ib_wr->sg_list[sge_index].lkey));
3249 else
3250 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3251
3252 total_payload_length += ib_wr->sg_list[sge_index].length;
3253 }
3254 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3255 total_payload_length);
3256 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3257 total_payload_length);
3258}
3259
3260/**
3261 * nes_post_send
3262 */
3263static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3264 struct ib_send_wr **bad_wr)
3265{
3266 u64 u64temp;
3267 unsigned long flags = 0;
3268 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3269 struct nes_device *nesdev = nesvnic->nesdev;
3270 struct nes_qp *nesqp = to_nesqp(ibqp);
3271 struct nes_hw_qp_wqe *wqe;
Chien Tung649fe4a2009-12-09 13:51:37 -08003272 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003273 u32 qsize = nesqp->hwqp.sq_size;
3274 u32 head;
Chien Tung649fe4a2009-12-09 13:51:37 -08003275 u32 wqe_misc = 0;
3276 u32 wqe_count = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003277 u32 counter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278
Frank Zagoe5dec392009-12-09 13:51:36 -08003279 if (nesqp->ibqp_state > IB_QPS_RTS) {
3280 err = -EINVAL;
3281 goto out;
3282 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003283
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003284 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003285
3286 head = nesqp->hwqp.sq_head;
3287
3288 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003289 /* Check for QP error */
3290 if (nesqp->term_flags) {
3291 err = -EINVAL;
3292 break;
3293 }
3294
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003295 /* Check for SQ overflow */
3296 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003297 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003298 break;
3299 }
3300
3301 wqe = &nesqp->hwqp.sq_vbase[head];
3302 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3303 nesqp->hwqp.qp_id, wqe, head); */
3304 nes_fill_init_qp_wqe(wqe, nesqp, head);
3305 u64temp = (u64)(ib_wr->wr_id);
3306 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3307 u64temp);
Chien Tung649fe4a2009-12-09 13:51:37 -08003308 switch (ib_wr->opcode) {
3309 case IB_WR_SEND:
3310 case IB_WR_SEND_WITH_INV:
3311 if (IB_WR_SEND == ib_wr->opcode) {
3312 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3313 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3314 else
3315 wqe_misc = NES_IWARP_SQ_OP_SEND;
3316 } else {
3317 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3318 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3319 else
3320 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003321
Chien Tung649fe4a2009-12-09 13:51:37 -08003322 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3323 ib_wr->ex.invalidate_rkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003324 }
3325
Chien Tung649fe4a2009-12-09 13:51:37 -08003326 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3327 err = -EINVAL;
3328 break;
3329 }
3330
3331 if (ib_wr->send_flags & IB_SEND_FENCE)
3332 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3333
3334 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3335 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3336 (ib_wr->sg_list[0].length <= 64)) {
3337 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3338 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3339 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3340 ib_wr->sg_list[0].length);
3341 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3342 } else {
3343 fill_wqe_sg_send(wqe, ib_wr, 1);
3344 }
3345
3346 break;
3347 case IB_WR_RDMA_WRITE:
3348 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3349 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3350 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3351 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3352 err = -EINVAL;
3353 break;
3354 }
3355
3356 if (ib_wr->send_flags & IB_SEND_FENCE)
3357 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3358
3359 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3360 ib_wr->wr.rdma.rkey);
3361 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3362 ib_wr->wr.rdma.remote_addr);
3363
3364 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3365 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3366 (ib_wr->sg_list[0].length <= 64)) {
3367 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3368 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3369 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3370 ib_wr->sg_list[0].length);
3371 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3372 } else {
3373 fill_wqe_sg_send(wqe, ib_wr, 1);
3374 }
3375
3376 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3377 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3378 break;
3379 case IB_WR_RDMA_READ:
3380 case IB_WR_RDMA_READ_WITH_INV:
3381 /* iWARP only supports 1 sge for RDMA reads */
3382 if (ib_wr->num_sge > 1) {
3383 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3384 ib_wr->num_sge);
3385 err = -EINVAL;
3386 break;
3387 }
3388 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3389 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3390 } else {
3391 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3392 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3393 ib_wr->ex.invalidate_rkey);
3394 }
3395
3396 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3397 ib_wr->wr.rdma.remote_addr);
3398 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3399 ib_wr->wr.rdma.rkey);
3400 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3401 ib_wr->sg_list->length);
3402 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3403 ib_wr->sg_list->addr);
3404 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3405 ib_wr->sg_list->lkey);
3406 break;
3407 case IB_WR_LOCAL_INV:
3408 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3409 set_wqe_32bit_value(wqe->wqe_words,
3410 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3411 ib_wr->ex.invalidate_rkey);
3412 break;
Chien Tunge293a262009-12-09 15:21:54 -08003413 case IB_WR_FAST_REG_MR:
3414 {
3415 int i;
3416 int flags = ib_wr->wr.fast_reg.access_flags;
3417 struct nes_ib_fast_reg_page_list *pnesfrpl =
3418 container_of(ib_wr->wr.fast_reg.page_list,
3419 struct nes_ib_fast_reg_page_list,
3420 ibfrpl);
3421 u64 *src_page_list = pnesfrpl->ibfrpl.page_list;
3422 u64 *dst_page_list = pnesfrpl->nes_wqe_pbl.kva;
3423
3424 if (ib_wr->wr.fast_reg.page_list_len >
3425 (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3426 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3427 err = -EINVAL;
3428 break;
3429 }
3430 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3431 set_wqe_64bit_value(wqe->wqe_words,
3432 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3433 ib_wr->wr.fast_reg.iova_start);
3434 set_wqe_32bit_value(wqe->wqe_words,
3435 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3436 ib_wr->wr.fast_reg.length);
3437 set_wqe_32bit_value(wqe->wqe_words,
Tatyana Nikolova94f622b2012-01-06 18:15:01 -06003438 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3439 set_wqe_32bit_value(wqe->wqe_words,
Chien Tunge293a262009-12-09 15:21:54 -08003440 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3441 ib_wr->wr.fast_reg.rkey);
3442 /* Set page size: */
3443 if (ib_wr->wr.fast_reg.page_shift == 12) {
3444 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3445 } else if (ib_wr->wr.fast_reg.page_shift == 21) {
3446 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3447 } else {
3448 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3449 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3450 err = -EINVAL;
3451 break;
3452 }
3453 /* Set access_flags */
3454 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3455 if (flags & IB_ACCESS_LOCAL_WRITE)
3456 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3457
3458 if (flags & IB_ACCESS_REMOTE_WRITE)
3459 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3460
3461 if (flags & IB_ACCESS_REMOTE_READ)
3462 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3463
3464 if (flags & IB_ACCESS_MW_BIND)
3465 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3466
3467 /* Fill in PBL info: */
3468 if (ib_wr->wr.fast_reg.page_list_len >
3469 pnesfrpl->ibfrpl.max_page_list_len) {
3470 nes_debug(NES_DBG_IW_TX, "Invalid page list length,"
3471 " ib_wr=%p, value=%u, max=%u\n",
3472 ib_wr, ib_wr->wr.fast_reg.page_list_len,
3473 pnesfrpl->ibfrpl.max_page_list_len);
3474 err = -EINVAL;
3475 break;
3476 }
3477
3478 set_wqe_64bit_value(wqe->wqe_words,
3479 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3480 pnesfrpl->nes_wqe_pbl.paddr);
3481
3482 set_wqe_32bit_value(wqe->wqe_words,
3483 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3484 ib_wr->wr.fast_reg.page_list_len * 8);
3485
3486 for (i = 0; i < ib_wr->wr.fast_reg.page_list_len; i++)
3487 dst_page_list[i] = cpu_to_le64(src_page_list[i]);
3488
Roland Dreier183ae742010-09-27 17:51:33 -07003489 nes_debug(NES_DBG_IW_TX, "SQ_FMR: iova_start: %llx, "
3490 "length: %d, rkey: %0x, pgl_paddr: %llx, "
Chien Tunge293a262009-12-09 15:21:54 -08003491 "page_list_len: %u, wqe_misc: %x\n",
Roland Dreier183ae742010-09-27 17:51:33 -07003492 (unsigned long long) ib_wr->wr.fast_reg.iova_start,
Chien Tunge293a262009-12-09 15:21:54 -08003493 ib_wr->wr.fast_reg.length,
3494 ib_wr->wr.fast_reg.rkey,
Roland Dreier183ae742010-09-27 17:51:33 -07003495 (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr,
Chien Tunge293a262009-12-09 15:21:54 -08003496 ib_wr->wr.fast_reg.page_list_len,
3497 wqe_misc);
3498 break;
3499 }
Chien Tung649fe4a2009-12-09 13:51:37 -08003500 default:
3501 /* error */
3502 err = -EINVAL;
3503 break;
3504 }
3505
Frank Zagoe5dec392009-12-09 13:51:36 -08003506 if (err)
3507 break;
3508
Chien Tungd14152d2009-12-09 15:21:56 -08003509 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003510 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
Chien Tungd14152d2009-12-09 15:21:56 -08003511
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3513
3514 ib_wr = ib_wr->next;
3515 head++;
3516 wqe_count++;
3517 if (head >= qsize)
3518 head = 0;
3519
3520 }
3521
3522 nesqp->hwqp.sq_head = head;
3523 barrier();
3524 while (wqe_count) {
3525 counter = min(wqe_count, ((u32)255));
3526 wqe_count -= counter;
3527 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3528 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3529 }
3530
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003531 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003532
Frank Zagoe5dec392009-12-09 13:51:36 -08003533out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003534 if (err)
3535 *bad_wr = ib_wr;
3536 return err;
3537}
3538
3539
3540/**
3541 * nes_post_recv
3542 */
3543static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3544 struct ib_recv_wr **bad_wr)
3545{
3546 u64 u64temp;
3547 unsigned long flags = 0;
3548 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3549 struct nes_device *nesdev = nesvnic->nesdev;
3550 struct nes_qp *nesqp = to_nesqp(ibqp);
3551 struct nes_hw_qp_wqe *wqe;
3552 int err = 0;
3553 int sge_index;
3554 u32 qsize = nesqp->hwqp.rq_size;
3555 u32 head;
3556 u32 wqe_count = 0;
3557 u32 counter;
3558 u32 total_payload_length;
3559
Frank Zago4293fdc12009-12-09 13:51:36 -08003560 if (nesqp->ibqp_state > IB_QPS_RTS) {
3561 err = -EINVAL;
3562 goto out;
3563 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003564
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003565 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003566
3567 head = nesqp->hwqp.rq_head;
3568
3569 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003570 /* Check for QP error */
3571 if (nesqp->term_flags) {
3572 err = -EINVAL;
3573 break;
3574 }
3575
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003576 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3577 err = -EINVAL;
3578 break;
3579 }
3580 /* Check for RQ overflow */
3581 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003582 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003583 break;
3584 }
3585
3586 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3587 wqe = &nesqp->hwqp.rq_vbase[head];
3588
3589 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3590 nesqp->hwqp.qp_id, wqe, head); */
3591 nes_fill_init_qp_wqe(wqe, nesqp, head);
3592 u64temp = (u64)(ib_wr->wr_id);
3593 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3594 u64temp);
3595 total_payload_length = 0;
3596 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3597 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3598 ib_wr->sg_list[sge_index].addr);
3599 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3600 ib_wr->sg_list[sge_index].length);
3601 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3602 ib_wr->sg_list[sge_index].lkey);
3603
3604 total_payload_length += ib_wr->sg_list[sge_index].length;
3605 }
3606 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3607 total_payload_length);
3608
3609 ib_wr = ib_wr->next;
3610 head++;
3611 wqe_count++;
3612 if (head >= qsize)
3613 head = 0;
3614 }
3615
3616 nesqp->hwqp.rq_head = head;
3617 barrier();
3618 while (wqe_count) {
3619 counter = min(wqe_count, ((u32)255));
3620 wqe_count -= counter;
3621 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3622 }
3623
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003624 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625
Frank Zago4293fdc12009-12-09 13:51:36 -08003626out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003627 if (err)
3628 *bad_wr = ib_wr;
3629 return err;
3630}
3631
3632
3633/**
3634 * nes_poll_cq
3635 */
3636static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3637{
3638 u64 u64temp;
3639 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003640 unsigned long flags = 0;
3641 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3642 struct nes_device *nesdev = nesvnic->nesdev;
3643 struct nes_cq *nescq = to_nescq(ibcq);
3644 struct nes_qp *nesqp;
3645 struct nes_hw_cqe cqe;
3646 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003647 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003648 u32 cq_size;
3649 u32 cqe_count = 0;
3650 u32 wqe_index;
3651 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003652 u32 move_cq_head = 1;
3653 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654
3655 nes_debug(NES_DBG_CQ, "\n");
3656
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003657 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003658
3659 head = nescq->hw_cq.cq_head;
3660 cq_size = nescq->hw_cq.cq_size;
3661
3662 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003663 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3664 NES_CQE_VALID) == 0)
3665 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003666
Don Wood5ee21fe2009-09-05 20:36:37 -07003667 /*
3668 * Make sure we read CQ entry contents *after*
3669 * we've checked the valid bit.
3670 */
3671 rmb();
3672
3673 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003674 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3675 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3676 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3677 /* parse CQE, get completion context from WQE (either rq or sq) */
3678 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3679 ((u64)u32temp);
3680
3681 if (u64temp) {
3682 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003683 memset(entry, 0, sizeof *entry);
3684 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3685 entry->status = IB_WC_SUCCESS;
3686 } else {
Don Wood320cdfd2009-09-05 20:36:39 -07003687 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3688 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3689 entry->status = err_code & 0x0000ffff;
3690
3691 /* The rest of the cqe's will be marked as flushed */
3692 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3693 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3694 NES_IWARP_CQE_MINOR_FLUSH);
3695 } else
3696 entry->status = IB_WC_WR_FLUSH_ERR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003697 }
3698
3699 entry->qp = &nesqp->ibqp;
3700 entry->src_qp = nesqp->hwqp.qp_id;
3701
3702 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3703 if (nesqp->skip_lsmm) {
3704 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003705 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003706 }
3707
3708 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003709 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003710 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003711 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003712 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003713 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003714 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3715
Don Wood6eed5e72009-09-05 20:36:39 -07003716 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003717 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3718 case NES_IWARP_SQ_OP_RDMAW:
3719 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3720 entry->opcode = IB_WC_RDMA_WRITE;
3721 break;
3722 case NES_IWARP_SQ_OP_RDMAR:
3723 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3724 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003725 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003726 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3727 break;
3728 case NES_IWARP_SQ_OP_SENDINV:
3729 case NES_IWARP_SQ_OP_SENDSEINV:
3730 case NES_IWARP_SQ_OP_SEND:
3731 case NES_IWARP_SQ_OP_SENDSE:
3732 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3733 entry->opcode = IB_WC_SEND;
3734 break;
Chien Tung649fe4a2009-12-09 13:51:37 -08003735 case NES_IWARP_SQ_OP_LOCINV:
Tatyana Nikolova4a4b03f2012-01-06 18:17:19 -06003736 entry->opcode = IB_WC_LOCAL_INV;
Chien Tung649fe4a2009-12-09 13:51:37 -08003737 break;
Chien Tunge293a262009-12-09 15:21:54 -08003738 case NES_IWARP_SQ_OP_FAST_REG:
3739 entry->opcode = IB_WC_FAST_REG_MR;
3740 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003741 }
Don Wood6eed5e72009-09-05 20:36:39 -07003742
3743 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3744 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3745 move_cq_head = 0;
3746 wq_tail = nesqp->hwqp.sq_tail;
3747 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003748 } else {
3749 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003750 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003751 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3752 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003753 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003754
3755 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3756 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3757 move_cq_head = 0;
3758 wq_tail = nesqp->hwqp.rq_tail;
3759 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003760 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003761
Don Wood5ee21fe2009-09-05 20:36:37 -07003762 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003763 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003764 cqe_count++;
3765 }
3766
Don Wood6eed5e72009-09-05 20:36:39 -07003767 if (move_cq_head) {
3768 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3769 if (++head >= cq_size)
3770 head = 0;
3771 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003772
Don Wood6eed5e72009-09-05 20:36:39 -07003773 if ((nescq->polled_completions > (cq_size / 2)) ||
3774 (nescq->polled_completions == 255)) {
3775 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003776 " are pending %u of %u.\n",
3777 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003778 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003779 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003780 nescq->polled_completions = 0;
3781 }
3782 } else {
3783 /* Update the wqe index and set status to flush */
3784 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3785 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3786 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3787 cpu_to_le32(wqe_index);
3788 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003789 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003790 }
3791
3792 if (nescq->polled_completions) {
3793 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3794 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3795 nescq->polled_completions = 0;
3796 }
3797
3798 nescq->hw_cq.cq_head = head;
3799 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3800 cqe_count, nescq->hw_cq.cq_number);
3801
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003802 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003803
3804 return cqe_count;
3805}
3806
3807
3808/**
3809 * nes_req_notify_cq
3810 */
3811static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3812 {
3813 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3814 struct nes_device *nesdev = nesvnic->nesdev;
3815 struct nes_cq *nescq = to_nescq(ibcq);
3816 u32 cq_arm;
3817
3818 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3819 nescq->hw_cq.cq_number);
3820
3821 cq_arm = nescq->hw_cq.cq_number;
3822 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3823 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3824 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3825 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3826 else
3827 return -EINVAL;
3828
3829 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3830 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3831
3832 return 0;
3833}
3834
Ira Weiny77386132015-05-13 20:02:58 -04003835static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3836 struct ib_port_immutable *immutable)
3837{
3838 struct ib_port_attr attr;
3839 int err;
3840
3841 err = nes_query_port(ibdev, port_num, &attr);
3842 if (err)
3843 return err;
3844
3845 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3846 immutable->gid_tbl_len = attr.gid_tbl_len;
Ira Weinyf9b22e32015-05-13 20:02:59 -04003847 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
Ira Weiny77386132015-05-13 20:02:58 -04003848
3849 return 0;
3850}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003851
3852/**
3853 * nes_init_ofa_device
3854 */
3855struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3856{
3857 struct nes_ib_device *nesibdev;
3858 struct nes_vnic *nesvnic = netdev_priv(netdev);
3859 struct nes_device *nesdev = nesvnic->nesdev;
3860
3861 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3862 if (nesibdev == NULL) {
3863 return NULL;
3864 }
3865 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3866 nesibdev->ibdev.owner = THIS_MODULE;
3867
3868 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3869 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3870 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3871
3872 nesibdev->ibdev.uverbs_cmd_mask =
3873 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3874 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3875 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3876 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3877 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3878 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3879 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3880 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3881 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3882 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3883 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3884 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3885 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3886 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3887 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3888 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3889 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3890 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3891 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3892 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3893 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3894 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3895
3896 nesibdev->ibdev.phys_port_cnt = 1;
3897 nesibdev->ibdev.num_comp_vectors = 1;
3898 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003899 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003900 nesibdev->ibdev.query_device = nes_query_device;
3901 nesibdev->ibdev.query_port = nes_query_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003902 nesibdev->ibdev.query_pkey = nes_query_pkey;
3903 nesibdev->ibdev.query_gid = nes_query_gid;
3904 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3905 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3906 nesibdev->ibdev.mmap = nes_mmap;
3907 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3908 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3909 nesibdev->ibdev.create_ah = nes_create_ah;
3910 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3911 nesibdev->ibdev.create_qp = nes_create_qp;
3912 nesibdev->ibdev.modify_qp = nes_modify_qp;
3913 nesibdev->ibdev.query_qp = nes_query_qp;
3914 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3915 nesibdev->ibdev.create_cq = nes_create_cq;
3916 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3917 nesibdev->ibdev.poll_cq = nes_poll_cq;
3918 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3919 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3920 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3921 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3922 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3923 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3924 nesibdev->ibdev.bind_mw = nes_bind_mw;
3925
Chien Tunge293a262009-12-09 15:21:54 -08003926 nesibdev->ibdev.alloc_fast_reg_mr = nes_alloc_fast_reg_mr;
3927 nesibdev->ibdev.alloc_fast_reg_page_list = nes_alloc_fast_reg_page_list;
3928 nesibdev->ibdev.free_fast_reg_page_list = nes_free_fast_reg_page_list;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003929
3930 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3931 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3932 nesibdev->ibdev.process_mad = nes_process_mad;
3933
3934 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3935 nesibdev->ibdev.post_send = nes_post_send;
3936 nesibdev->ibdev.post_recv = nes_post_recv;
3937
3938 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3939 if (nesibdev->ibdev.iwcm == NULL) {
3940 ib_dealloc_device(&nesibdev->ibdev);
3941 return NULL;
3942 }
3943 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3944 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3945 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3946 nesibdev->ibdev.iwcm->connect = nes_connect;
3947 nesibdev->ibdev.iwcm->accept = nes_accept;
3948 nesibdev->ibdev.iwcm->reject = nes_reject;
3949 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3950 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
Ira Weiny77386132015-05-13 20:02:58 -04003951 nesibdev->ibdev.get_port_immutable = nes_port_immutable;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003952
3953 return nesibdev;
3954}
3955
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003956
3957/**
3958 * nes_handle_delayed_event
3959 */
3960static void nes_handle_delayed_event(unsigned long data)
3961{
3962 struct nes_vnic *nesvnic = (void *) data;
3963
3964 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3965 struct ib_event event;
3966
3967 event.device = &nesvnic->nesibdev->ibdev;
3968 if (!event.device)
3969 goto stop_timer;
3970 event.event = nesvnic->delayed_event;
3971 event.element.port_num = nesvnic->logical_port + 1;
3972 ib_dispatch_event(&event);
3973 }
3974
3975stop_timer:
3976 nesvnic->event_timer.function = NULL;
3977}
3978
3979
Faisal Latifcd6860e2010-07-04 00:17:59 +00003980void nes_port_ibevent(struct nes_vnic *nesvnic)
3981{
3982 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3983 struct nes_device *nesdev = nesvnic->nesdev;
3984 struct ib_event event;
3985 event.device = &nesibdev->ibdev;
3986 event.element.port_num = nesvnic->logical_port + 1;
3987 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003988
3989 if (!nesvnic->event_timer.function) {
3990 ib_dispatch_event(&event);
3991 nesvnic->last_dispatched_event = event.event;
3992 nesvnic->event_timer.function = nes_handle_delayed_event;
3993 nesvnic->event_timer.data = (unsigned long) nesvnic;
3994 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3995 add_timer(&nesvnic->event_timer);
3996 } else {
3997 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3998 }
3999 nesvnic->delayed_event = event.event;
Faisal Latifcd6860e2010-07-04 00:17:59 +00004000}
4001
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004002
4003/**
4004 * nes_destroy_ofa_device
4005 */
4006void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
4007{
4008 if (nesibdev == NULL)
4009 return;
4010
4011 nes_unregister_ofa_device(nesibdev);
4012
4013 kfree(nesibdev->ibdev.iwcm);
4014 ib_dealloc_device(&nesibdev->ibdev);
4015}
4016
4017
4018/**
4019 * nes_register_ofa_device
4020 */
4021int nes_register_ofa_device(struct nes_ib_device *nesibdev)
4022{
4023 struct nes_vnic *nesvnic = nesibdev->nesvnic;
4024 struct nes_device *nesdev = nesvnic->nesdev;
4025 struct nes_adapter *nesadapter = nesdev->nesadapter;
4026 int i, ret;
4027
Ralph Campbell9a6edb62010-05-06 17:03:25 -07004028 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004029 if (ret) {
4030 return ret;
4031 }
4032
4033 /* Get the resources allocated to this device */
4034 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
4035 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
4036 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
4037 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
4038
Tony Jonesf4e91eb2008-02-22 00:13:36 +01004039 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4040 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004041 if (ret) {
4042 while (i > 0) {
4043 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01004044 device_remove_file(&nesibdev->ibdev.dev,
4045 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004046 }
4047 ib_unregister_device(&nesibdev->ibdev);
4048 return ret;
4049 }
4050 }
4051
4052 nesvnic->of_device_registered = 1;
4053
4054 return 0;
4055}
4056
4057
4058/**
4059 * nes_unregister_ofa_device
4060 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07004061static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004062{
4063 struct nes_vnic *nesvnic = nesibdev->nesvnic;
4064 int i;
4065
Tony Jonesf4e91eb2008-02-22 00:13:36 +01004066 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4067 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004068 }
4069
4070 if (nesvnic->of_device_registered) {
4071 ib_unregister_device(&nesibdev->ibdev);
4072 }
4073
4074 nesvnic->of_device_registered = 0;
4075}