blob: c0d0296e7a003089dec6d8d180de2bb6dd05964d [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 Dreier1a855fb2008-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
609
610/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800611 * nes_query_pkey
612 */
613static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
614{
615 *pkey = 0;
616 return 0;
617}
618
619
620/**
621 * nes_query_gid
622 */
623static int nes_query_gid(struct ib_device *ibdev, u8 port,
624 int index, union ib_gid *gid)
625{
626 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
627
628 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
629 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
630
631 return 0;
632}
633
634
635/**
636 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
637 * of all objects associated with a particular user-mode client.
638 */
639static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
640 struct ib_udata *udata)
641{
642 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
643 struct nes_device *nesdev = nesvnic->nesdev;
644 struct nes_adapter *nesadapter = nesdev->nesadapter;
645 struct nes_alloc_ucontext_req req;
646 struct nes_alloc_ucontext_resp uresp;
647 struct nes_ucontext *nes_ucontext;
648 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
649
650
651 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
652 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
653 return ERR_PTR(-EINVAL);
654 }
655
656 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
657 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
658 req.userspace_ver, NES_ABI_USERSPACE_VER);
659 return ERR_PTR(-EINVAL);
660 }
661
662
663 memset(&uresp, 0, sizeof uresp);
664
665 uresp.max_qps = nesibdev->max_qp;
666 uresp.max_pds = nesibdev->max_pd;
667 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
668 uresp.virtwq = nesadapter->virtwq;
669 uresp.kernel_ver = NES_ABI_KERNEL_VER;
670
671 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
672 if (!nes_ucontext)
673 return ERR_PTR(-ENOMEM);
674
675 nes_ucontext->nesdev = nesdev;
676 nes_ucontext->mmap_wq_offset = uresp.max_pds;
677 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
678 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
679 PAGE_SIZE;
680
681
682 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
683 kfree(nes_ucontext);
684 return ERR_PTR(-EFAULT);
685 }
686
687 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
688 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
689 atomic_set(&nes_ucontext->usecnt, 1);
690 return &nes_ucontext->ibucontext;
691}
692
693
694/**
695 * nes_dealloc_ucontext
696 */
697static int nes_dealloc_ucontext(struct ib_ucontext *context)
698{
699 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
700 /* struct nes_device *nesdev = nesvnic->nesdev; */
701 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
702
703 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
704 return 0;
705 kfree(nes_ucontext);
706 return 0;
707}
708
709
710/**
711 * nes_mmap
712 */
713static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
714{
715 unsigned long index;
716 struct nes_vnic *nesvnic = to_nesvnic(context->device);
717 struct nes_device *nesdev = nesvnic->nesdev;
718 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
719 struct nes_ucontext *nes_ucontext;
720 struct nes_qp *nesqp;
721
722 nes_ucontext = to_nesucontext(context);
723
724
725 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
726 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
727 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
728 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
729 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
730 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
731 return -EFAULT;
732 }
733 nesqp = nes_ucontext->mmap_nesqp[index];
734 if (nesqp == NULL) {
735 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
736 return -EFAULT;
737 }
738 if (remap_pfn_range(vma, vma->vm_start,
739 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
740 vma->vm_end - vma->vm_start,
741 vma->vm_page_prot)) {
742 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
743 return -EAGAIN;
744 }
745 vma->vm_private_data = nesqp;
746 return 0;
747 } else {
748 index = vma->vm_pgoff;
749 if (!test_bit(index, nes_ucontext->allocated_doorbells))
750 return -EFAULT;
751
752 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
753 if (io_remap_pfn_range(vma, vma->vm_start,
754 (nesdev->doorbell_start +
755 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
756 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
757 return -EAGAIN;
758 vma->vm_private_data = nes_ucontext;
759 return 0;
760 }
761
762 return -ENOSYS;
763}
764
765
766/**
767 * nes_alloc_pd
768 */
769static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
770 struct ib_ucontext *context, struct ib_udata *udata)
771{
772 struct nes_pd *nespd;
773 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
774 struct nes_device *nesdev = nesvnic->nesdev;
775 struct nes_adapter *nesadapter = nesdev->nesadapter;
776 struct nes_ucontext *nesucontext;
777 struct nes_alloc_pd_resp uresp;
778 u32 pd_num = 0;
779 int err;
780
781 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
782 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
Eric Dumazet29b44332010-10-11 10:22:12 +0000783 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800784
785 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000786 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800787 if (err) {
788 return ERR_PTR(err);
789 }
790
791 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
792 if (!nespd) {
793 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
794 return ERR_PTR(-ENOMEM);
795 }
796
797 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
798 nespd, nesvnic->nesibdev->ibdev.name);
799
800 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
801
802 if (context) {
803 nesucontext = to_nesucontext(context);
804 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
805 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
806 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
807 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600808 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800809 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
810 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
811 kfree(nespd);
812 return ERR_PTR(-ENOMEM);
813 }
814
815 uresp.pd_id = nespd->pd_id;
816 uresp.mmap_db_index = nespd->mmap_db_index;
817 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
818 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
819 kfree(nespd);
820 return ERR_PTR(-EFAULT);
821 }
822
823 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
824 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
825 nesucontext->first_free_db = nespd->mmap_db_index + 1;
826 }
827
828 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
829 return &nespd->ibpd;
830}
831
832
833/**
834 * nes_dealloc_pd
835 */
836static int nes_dealloc_pd(struct ib_pd *ibpd)
837{
838 struct nes_ucontext *nesucontext;
839 struct nes_pd *nespd = to_nespd(ibpd);
840 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
841 struct nes_device *nesdev = nesvnic->nesdev;
842 struct nes_adapter *nesadapter = nesdev->nesadapter;
843
844 if ((ibpd->uobject) && (ibpd->uobject->context)) {
845 nesucontext = to_nesucontext(ibpd->uobject->context);
846 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
847 nespd->mmap_db_index);
848 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
849 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
850 if (nesucontext->first_free_db > nespd->mmap_db_index) {
851 nesucontext->first_free_db = nespd->mmap_db_index;
852 }
853 }
854
855 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
856 nespd->pd_id, nespd);
857 nes_free_resource(nesadapter, nesadapter->allocated_pds,
858 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
859 kfree(nespd);
860
861 return 0;
862}
863
864
865/**
866 * nes_create_ah
867 */
868static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
869{
870 return ERR_PTR(-ENOSYS);
871}
872
873
874/**
875 * nes_destroy_ah
876 */
877static int nes_destroy_ah(struct ib_ah *ah)
878{
879 return -ENOSYS;
880}
881
882
883/**
884 * nes_get_encoded_size
885 */
886static inline u8 nes_get_encoded_size(int *size)
887{
888 u8 encoded_size = 0;
889 if (*size <= 32) {
890 *size = 32;
891 encoded_size = 1;
892 } else if (*size <= 128) {
893 *size = 128;
894 encoded_size = 2;
895 } else if (*size <= 512) {
896 *size = 512;
897 encoded_size = 3;
898 }
899 return (encoded_size);
900}
901
902
903
904/**
905 * nes_setup_virt_qp
906 */
907static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
908 struct nes_vnic *nesvnic, int sq_size, int rq_size)
909{
910 unsigned long flags;
911 void *mem;
912 __le64 *pbl = NULL;
913 __le64 *tpbl;
914 __le64 *pblbuffer;
915 struct nes_device *nesdev = nesvnic->nesdev;
916 struct nes_adapter *nesadapter = nesdev->nesadapter;
917 u32 pbl_entries;
918 u8 rq_pbl_entries;
919 u8 sq_pbl_entries;
920
921 pbl_entries = nespbl->pbl_size >> 3;
Roland Dreier71e09572008-04-16 21:01:09 -0700922 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 -0800923 nespbl->pbl_size, pbl_entries,
924 (void *)nespbl->pbl_vbase,
Roland Dreier71e09572008-04-16 21:01:09 -0700925 (unsigned long) nespbl->pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800926 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
927 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
928 /* the first pbl to be fro the rq_vbase... */
929 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
930 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
931 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
932 if (!nespbl->page) {
933 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
934 kfree(nespbl);
935 return -ENOMEM;
936 }
937
938 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
939 nesqp->page = nespbl->page;
940 if (!nesqp->hwqp.sq_vbase) {
941 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
942 kfree(nespbl);
943 return -ENOMEM;
944 }
945
946 /* Now to get to sq.. we need to calculate how many */
947 /* PBL entries were used by the rq.. */
948 pbl += sq_pbl_entries;
949 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
950 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
951 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
952
Roland Dreier71e09572008-04-16 21:01:09 -0700953 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
954 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
955 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800956 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
957 if (!nesadapter->free_256pbl) {
958 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
959 nespbl->pbl_pbase);
960 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
961 kunmap(nesqp->page);
962 kfree(nespbl);
963 return -ENOMEM;
964 }
965 nesadapter->free_256pbl--;
966 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
967
968 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
969 pblbuffer = nesqp->pbl_vbase;
970 if (!nesqp->pbl_vbase) {
971 /* memory allocated during nes_reg_user_mr() */
972 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
973 nespbl->pbl_pbase);
974 kfree(nespbl);
975 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
976 nesadapter->free_256pbl++;
977 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
978 kunmap(nesqp->page);
979 return -ENOMEM;
980 }
981 memset(nesqp->pbl_vbase, 0, 256);
982 /* fill in the page address in the pbl buffer.. */
983 tpbl = pblbuffer + 16;
984 pbl = (__le64 *)nespbl->pbl_vbase;
985 while (sq_pbl_entries--)
986 *tpbl++ = *pbl++;
987 tpbl = pblbuffer;
988 while (rq_pbl_entries--)
989 *tpbl++ = *pbl++;
990
991 /* done with memory allocated during nes_reg_user_mr() */
992 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
993 nespbl->pbl_pbase);
994 kfree(nespbl);
995
996 nesqp->qp_mem_size =
997 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
998 /* Round up to a multiple of a page */
999 nesqp->qp_mem_size += PAGE_SIZE - 1;
1000 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1001
1002 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1003 &nesqp->hwqp.q2_pbase);
1004
1005 if (!mem) {
1006 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1007 nesqp->pbl_vbase = NULL;
1008 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1009 nesadapter->free_256pbl++;
1010 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1011 kunmap(nesqp->page);
1012 return -ENOMEM;
1013 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001014 nesqp->sq_kmapped = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001015 nesqp->hwqp.q2_vbase = mem;
1016 mem += 256;
1017 memset(nesqp->hwqp.q2_vbase, 0, 256);
1018 nesqp->nesqp_context = mem;
1019 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1020 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1021
1022 return 0;
1023}
1024
1025
1026/**
1027 * nes_setup_mmap_qp
1028 */
1029static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1030 int sq_size, int rq_size)
1031{
1032 void *mem;
1033 struct nes_device *nesdev = nesvnic->nesdev;
1034
1035 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1036 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1037 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1038 256; /* this is Q2 */
1039 /* Round up to a multiple of a page */
1040 nesqp->qp_mem_size += PAGE_SIZE - 1;
1041 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1042
1043 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1044 &nesqp->hwqp.sq_pbase);
1045 if (!mem)
1046 return -ENOMEM;
1047 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1048 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1049 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1050
1051 memset(mem, 0, nesqp->qp_mem_size);
1052
1053 nesqp->hwqp.sq_vbase = mem;
1054 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1055
1056 nesqp->hwqp.rq_vbase = mem;
1057 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1058 sizeof(struct nes_hw_qp_wqe) * sq_size;
1059 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1060
1061 nesqp->hwqp.q2_vbase = mem;
1062 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1063 sizeof(struct nes_hw_qp_wqe) * rq_size;
1064 mem += 256;
1065 memset(nesqp->hwqp.q2_vbase, 0, 256);
1066
1067 nesqp->nesqp_context = mem;
1068 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1069 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1070 return 0;
1071}
1072
1073
1074/**
1075 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1076 */
1077static inline void nes_free_qp_mem(struct nes_device *nesdev,
1078 struct nes_qp *nesqp, int virt_wqs)
1079{
1080 unsigned long flags;
1081 struct nes_adapter *nesadapter = nesdev->nesadapter;
1082 if (!virt_wqs) {
1083 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1084 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1085 }else {
1086 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1087 nesadapter->free_256pbl++;
1088 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1089 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1090 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1091 nesqp->pbl_vbase = NULL;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001092 if (nesqp->sq_kmapped) {
1093 nesqp->sq_kmapped = 0;
1094 kunmap(nesqp->page);
1095 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001096 }
1097}
1098
1099
1100/**
1101 * nes_create_qp
1102 */
1103static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1104 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1105{
1106 u64 u64temp= 0;
1107 u64 u64nesqp = 0;
1108 struct nes_pd *nespd = to_nespd(ibpd);
1109 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1110 struct nes_device *nesdev = nesvnic->nesdev;
1111 struct nes_adapter *nesadapter = nesdev->nesadapter;
1112 struct nes_qp *nesqp;
1113 struct nes_cq *nescq;
1114 struct nes_ucontext *nes_ucontext;
1115 struct nes_hw_cqp_wqe *cqp_wqe;
1116 struct nes_cqp_request *cqp_request;
1117 struct nes_create_qp_req req;
1118 struct nes_create_qp_resp uresp;
1119 struct nes_pbl *nespbl = NULL;
1120 u32 qp_num = 0;
1121 u32 opcode = 0;
1122 /* u32 counter = 0; */
1123 void *mem;
1124 unsigned long flags;
1125 int ret;
1126 int err;
1127 int virt_wqs = 0;
1128 int sq_size;
1129 int rq_size;
1130 u8 sq_encoded_size;
1131 u8 rq_encoded_size;
1132 /* int counter; */
1133
Eli Cohenb846f252008-04-16 21:09:27 -07001134 if (init_attr->create_flags)
1135 return ERR_PTR(-EINVAL);
1136
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001137 atomic_inc(&qps_created);
1138 switch (init_attr->qp_type) {
1139 case IB_QPT_RC:
1140 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1141 init_attr->cap.max_inline_data = 0;
1142 } else {
1143 init_attr->cap.max_inline_data = 64;
1144 }
1145 sq_size = init_attr->cap.max_send_wr;
1146 rq_size = init_attr->cap.max_recv_wr;
1147
Glenn Streiff7495ab62008-04-29 13:46:54 -07001148 /* check if the encoded sizes are OK or not... */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001149 sq_encoded_size = nes_get_encoded_size(&sq_size);
1150 rq_encoded_size = nes_get_encoded_size(&rq_size);
1151
1152 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1153 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1154 rq_size, sq_size);
1155 return ERR_PTR(-EINVAL);
1156 }
1157
1158 init_attr->cap.max_send_wr = sq_size -2;
1159 init_attr->cap.max_recv_wr = rq_size -1;
1160 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1161
1162 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001163 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001164 if (ret) {
1165 return ERR_PTR(ret);
1166 }
1167
1168 /* Need 512 (actually now 1024) byte alignment on this structure */
1169 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1170 if (!mem) {
1171 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1172 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1173 return ERR_PTR(-ENOMEM);
1174 }
1175 u64nesqp = (unsigned long)mem;
1176 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1177 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1178 u64nesqp &= ~u64temp;
1179 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1180 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1181 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1182 nesqp->allocated_buffer = mem;
1183
1184 if (udata) {
1185 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1186 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1187 kfree(nesqp->allocated_buffer);
1188 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
Yann Droneaud9d194d12014-03-10 23:06:27 +01001189 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190 }
1191 if (req.user_wqe_buffers) {
1192 virt_wqs = 1;
1193 }
Tatyana Nikolova43adff32014-03-11 14:20:17 -05001194 if (req.user_qp_buffer)
1195 nesqp->nesuqp_addr = req.user_qp_buffer;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001196 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1197 nesqp->user_mode = 1;
1198 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1199 if (virt_wqs) {
1200 err = 1;
1201 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1202 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1203 list_del(&nespbl->list);
1204 err = 0;
1205 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1206 nespbl, nespbl->user_base);
1207 break;
1208 }
1209 }
1210 if (err) {
1211 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1212 (long long unsigned int)req.user_wqe_buffers);
1213 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1214 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001215 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001216 }
1217 }
1218
1219 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1220 nesqp->mmap_sq_db_index =
1221 find_next_zero_bit(nes_ucontext->allocated_wqs,
1222 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1223 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1224 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001225 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001226 nes_debug(NES_DBG_QP,
1227 "db index > max user regions, failing create QP\n");
1228 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1229 if (virt_wqs) {
1230 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1231 nespbl->pbl_pbase);
1232 kfree(nespbl);
1233 }
1234 kfree(nesqp->allocated_buffer);
1235 return ERR_PTR(-ENOMEM);
1236 }
1237 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1238 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1239 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1240 } else {
1241 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1242 kfree(nesqp->allocated_buffer);
1243 return ERR_PTR(-EFAULT);
1244 }
1245 }
1246 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1247 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1248 if (err) {
1249 nes_debug(NES_DBG_QP,
1250 "error geting qp mem code = %d\n", err);
1251 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1252 kfree(nesqp->allocated_buffer);
1253 return ERR_PTR(-ENOMEM);
1254 }
1255
1256 nesqp->hwqp.sq_size = sq_size;
1257 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1258 nesqp->hwqp.sq_head = 1;
1259 nesqp->hwqp.rq_size = rq_size;
1260 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1261 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1262 (void *)nesqp->nesqp_context_pbase);
1263 */
1264 nesqp->hwqp.qp_id = qp_num;
1265 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1266 nesqp->nespd = nespd;
1267
1268 nescq = to_nescq(init_attr->send_cq);
1269 nesqp->nesscq = nescq;
1270 nescq = to_nescq(init_attr->recv_cq);
1271 nesqp->nesrcq = nescq;
1272
1273 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1274 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1275 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1276 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1277 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1278 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001279 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001280 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1281 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001282 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1284 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1285 u64temp = (u64)nesqp->hwqp.sq_pbase;
1286 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1287 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1288
1289
1290 if (!virt_wqs) {
1291 u64temp = (u64)nesqp->hwqp.sq_pbase;
1292 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1293 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1294 u64temp = (u64)nesqp->hwqp.rq_pbase;
1295 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1296 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1297 } else {
1298 u64temp = (u64)nesqp->pbl_pbase;
1299 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1300 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1301 }
1302
1303 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1304 nesvnic->next_qp_nic_index,
1305 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1306 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1307 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1308 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1309 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1310 nesvnic->next_qp_nic_index++;
1311 if ((nesvnic->next_qp_nic_index > 3) ||
1312 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1313 nesvnic->next_qp_nic_index = 0;
1314 }
1315 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1316
1317 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1318 u64temp = (u64)nesqp->hwqp.q2_pbase;
1319 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1320 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1321 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1322 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1323 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
Faisal Latif883c6992010-03-02 17:22:51 -06001324 NES_QPCONTEXT_ORDIRD_AAH |
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001325 ((((u32)nesadapter->max_irrq_wr) <<
1326 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1327 if (disable_mpa_crc) {
1328 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1329 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1330 }
1331
1332
1333 /* Create the QP */
1334 cqp_request = nes_get_cqp_request(nesdev);
1335 if (cqp_request == NULL) {
1336 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1337 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1338 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1339 kfree(nesqp->allocated_buffer);
1340 return ERR_PTR(-ENOMEM);
1341 }
1342 cqp_request->waiting = 1;
1343 cqp_wqe = &cqp_request->cqp_wqe;
1344
1345 if (!virt_wqs) {
1346 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1347 NES_CQP_QP_IWARP_STATE_IDLE;
1348 } else {
1349 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1350 NES_CQP_QP_IWARP_STATE_IDLE;
1351 }
1352 opcode |= NES_CQP_QP_CQS_VALID;
1353 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1354 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1355 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1356
1357 u64temp = (u64)nesqp->nesqp_context_pbase;
1358 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1359
1360 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001361 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001362
1363 /* Wait for CQP */
1364 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1365 nesqp->hwqp.qp_id);
1366 ret = wait_event_timeout(cqp_request->waitq,
1367 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1368 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1369 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1370 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1371 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1372 cqp_request->major_code, cqp_request->minor_code);
1373 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001374 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001375 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1376 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1377 kfree(nesqp->allocated_buffer);
1378 if (!ret) {
1379 return ERR_PTR(-ETIME);
1380 } else {
1381 return ERR_PTR(-EIO);
1382 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001383 }
1384
Roland Dreier1ff66e82008-07-14 23:48:49 -07001385 nes_put_cqp_request(nesdev, cqp_request);
1386
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001387 if (ibpd->uobject) {
1388 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
Dan Carpenter60429632013-07-25 20:04:59 +03001389 uresp.mmap_rq_db_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001390 uresp.actual_sq_size = sq_size;
1391 uresp.actual_rq_size = rq_size;
1392 uresp.qp_id = nesqp->hwqp.qp_id;
1393 uresp.nes_drv_opt = nes_drv_opt;
1394 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1395 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1396 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1397 kfree(nesqp->allocated_buffer);
1398 return ERR_PTR(-EFAULT);
1399 }
1400 }
1401
1402 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1403 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1404 spin_lock_init(&nesqp->lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001405 nes_add_ref(&nesqp->ibqp);
1406 break;
1407 default:
1408 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1409 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001410 }
1411
Chien Tungd14152d2009-12-09 15:21:56 -08001412 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
Tatyana Nikolova7bfcfa52012-12-06 19:58:27 +00001413 init_timer(&nesqp->terminate_timer);
1414 nesqp->terminate_timer.function = nes_terminate_timeout;
1415 nesqp->terminate_timer.data = (unsigned long)nesqp;
Chien Tungd14152d2009-12-09 15:21:56 -08001416
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001417 /* update the QP table */
1418 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1419 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00001420 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001421
1422 return &nesqp->ibqp;
1423}
1424
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001425/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001426 * nes_clean_cq
1427 */
1428static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1429{
1430 u32 cq_head;
1431 u32 lo;
1432 u32 hi;
1433 u64 u64temp;
1434 unsigned long flags = 0;
1435
1436 spin_lock_irqsave(&nescq->lock, flags);
1437
1438 cq_head = nescq->hw_cq.cq_head;
1439 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1440 rmb();
1441 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1442 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1443 u64temp = (((u64)hi) << 32) | ((u64)lo);
1444 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1445 if (u64temp == (u64)(unsigned long)nesqp) {
1446 /* Zero the context value so cqe will be ignored */
1447 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1448 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1449 }
1450
1451 if (++cq_head >= nescq->hw_cq.cq_size)
1452 cq_head = 0;
1453 }
1454
1455 spin_unlock_irqrestore(&nescq->lock, flags);
1456}
1457
1458
1459/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001460 * nes_destroy_qp
1461 */
1462static int nes_destroy_qp(struct ib_qp *ibqp)
1463{
1464 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001465 struct nes_ucontext *nes_ucontext;
1466 struct ib_qp_attr attr;
1467 struct iw_cm_id *cm_id;
1468 struct iw_cm_event cm_event;
Faisal Latif0f0bee82011-09-25 20:34:00 -05001469 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001470
1471 atomic_inc(&sw_qps_destroyed);
1472 nesqp->destroyed = 1;
1473
1474 /* Blow away the connection if it exists. */
1475 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1476 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1477 attr.qp_state = IB_QPS_ERR;
1478 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1479 }
1480
1481 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1482 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1483 cm_id = nesqp->cm_id;
1484 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07001485 cm_event.status = -ETIMEDOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001486 cm_event.local_addr = cm_id->local_addr;
1487 cm_event.remote_addr = cm_id->remote_addr;
1488 cm_event.private_data = NULL;
1489 cm_event.private_data_len = 0;
1490
1491 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1492 "QP%u. cm_id = %p, refcount = %u. \n",
1493 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1494
1495 cm_id->rem_ref(cm_id);
1496 ret = cm_id->event_handler(cm_id, &cm_event);
1497 if (ret)
1498 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1499 }
1500
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001501 if (nesqp->user_mode) {
1502 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1503 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1504 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1505 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1506 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1507 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1508 }
1509 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001510 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1511 nesqp->sq_kmapped = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001512 kunmap(nesqp->page);
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001513 }
Don Wood5ee21fe2009-09-05 20:36:37 -07001514 } else {
1515 /* Clean any pending completions from the cq(s) */
1516 if (nesqp->nesscq)
1517 nes_clean_cq(nesqp, nesqp->nesscq);
1518
1519 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1520 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001521 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001522 nes_rem_ref(&nesqp->ibqp);
1523 return 0;
1524}
1525
1526
1527/**
1528 * nes_create_cq
1529 */
1530static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1531 int comp_vector,
1532 struct ib_ucontext *context, struct ib_udata *udata)
1533{
1534 u64 u64temp;
1535 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1536 struct nes_device *nesdev = nesvnic->nesdev;
1537 struct nes_adapter *nesadapter = nesdev->nesadapter;
1538 struct nes_cq *nescq;
1539 struct nes_ucontext *nes_ucontext = NULL;
1540 struct nes_cqp_request *cqp_request;
1541 void *mem = NULL;
1542 struct nes_hw_cqp_wqe *cqp_wqe;
1543 struct nes_pbl *nespbl = NULL;
1544 struct nes_create_cq_req req;
1545 struct nes_create_cq_resp resp;
1546 u32 cq_num = 0;
1547 u32 opcode = 0;
1548 u32 pbl_entries = 1;
1549 int err;
1550 unsigned long flags;
1551 int ret;
1552
Chien Tung5924aea2009-12-09 15:21:56 -08001553 if (entries > nesadapter->max_cqe)
1554 return ERR_PTR(-EINVAL);
1555
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001556 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001557 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001558 if (err) {
1559 return ERR_PTR(err);
1560 }
1561
1562 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1563 if (!nescq) {
1564 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1565 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1566 return ERR_PTR(-ENOMEM);
1567 }
1568
1569 nescq->hw_cq.cq_size = max(entries + 1, 5);
1570 nescq->hw_cq.cq_number = cq_num;
1571 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1572
1573
1574 if (context) {
1575 nes_ucontext = to_nesucontext(context);
1576 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1577 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1578 kfree(nescq);
1579 return ERR_PTR(-EFAULT);
1580 }
1581 nesvnic->mcrq_ucontext = nes_ucontext;
1582 nes_ucontext->mcrqf = req.mcrqf;
1583 if (nes_ucontext->mcrqf) {
1584 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001585 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001586 else if (nes_ucontext->mcrqf & 0x40000000)
1587 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1588 else
1589 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001590 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001591 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1592 }
1593 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1594 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001595 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001596 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1597 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1598 list_del(&nespbl->list);
1599 err = 0;
1600 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1601 nespbl);
1602 break;
1603 }
1604 }
1605 if (err) {
1606 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1607 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001608 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001609 }
1610
1611 pbl_entries = nespbl->pbl_size >> 3;
1612 nescq->cq_mem_size = 0;
1613 } else {
1614 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1615 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1616 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1617
1618 /* allocate the physical buffer space */
Joe Perches9011a672014-08-08 14:24:16 -07001619 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1620 &nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001621 if (!mem) {
1622 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1623 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1624 kfree(nescq);
1625 return ERR_PTR(-ENOMEM);
1626 }
1627
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001628 nescq->hw_cq.cq_vbase = mem;
1629 nescq->hw_cq.cq_head = 0;
1630 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1631 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1632 (u32)nescq->hw_cq.cq_pbase);
1633 }
1634
1635 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1636 spin_lock_init(&nescq->lock);
1637
1638 /* send CreateCQ request to CQP */
1639 cqp_request = nes_get_cqp_request(nesdev);
1640 if (cqp_request == NULL) {
1641 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1642 if (!context)
1643 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1644 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001645 else {
1646 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1647 nespbl->pbl_vbase, nespbl->pbl_pbase);
1648 kfree(nespbl);
1649 }
1650
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001651 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1652 kfree(nescq);
1653 return ERR_PTR(-ENOMEM);
1654 }
1655 cqp_request->waiting = 1;
1656 cqp_wqe = &cqp_request->cqp_wqe;
1657
1658 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1659 NES_CQP_CQ_CHK_OVERFLOW |
1660 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1661
1662 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1663
1664 if (pbl_entries != 1) {
1665 if (pbl_entries > 32) {
1666 /* use 4k pbl */
1667 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1668 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001669 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1670 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001671 if (!context)
1672 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1673 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001674 else {
1675 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1676 nespbl->pbl_vbase, nespbl->pbl_pbase);
1677 kfree(nespbl);
1678 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001679 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1680 kfree(nescq);
1681 return ERR_PTR(-ENOMEM);
1682 } else {
1683 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1684 nescq->virtual_cq = 2;
1685 nesadapter->free_4kpbl--;
1686 }
1687 } else {
1688 /* use 256 byte pbl */
1689 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1690 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001691 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1692 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001693 if (!context)
1694 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1695 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001696 else {
1697 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1698 nespbl->pbl_vbase, nespbl->pbl_pbase);
1699 kfree(nespbl);
1700 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001701 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1702 kfree(nescq);
1703 return ERR_PTR(-ENOMEM);
1704 } else {
1705 opcode |= NES_CQP_CQ_VIRT;
1706 nescq->virtual_cq = 1;
1707 nesadapter->free_256pbl--;
1708 }
1709 }
1710 }
1711
1712 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1713
1714 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1715 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1716 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1717 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1718
1719 if (context) {
1720 if (pbl_entries != 1)
1721 u64temp = (u64)nespbl->pbl_pbase;
1722 else
1723 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1724 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1725 nes_ucontext->mmap_db_index[0]);
1726 } else {
1727 u64temp = (u64)nescq->hw_cq.cq_pbase;
1728 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1729 }
1730 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1731 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1732 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1733 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1734 cpu_to_le32((u32)(u64temp >> 1));
1735 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1736 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1737
1738 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001739 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001740
1741 /* Wait for CQP */
1742 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1743 nescq->hw_cq.cq_number);
1744 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1745 NES_EVENT_TIMEOUT * 2);
1746 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1747 nescq->hw_cq.cq_number, ret);
1748 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001749 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001750 if (!context)
1751 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1752 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001753 else {
1754 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1755 nespbl->pbl_vbase, nespbl->pbl_pbase);
1756 kfree(nespbl);
1757 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001758 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1759 kfree(nescq);
1760 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001761 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001762 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001763
1764 if (context) {
1765 /* free the nespbl */
1766 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1767 nespbl->pbl_pbase);
1768 kfree(nespbl);
1769 resp.cq_id = nescq->hw_cq.cq_number;
1770 resp.cq_size = nescq->hw_cq.cq_size;
1771 resp.mmap_db_index = 0;
Dan Carpenter60429632013-07-25 20:04:59 +03001772 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001773 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1774 kfree(nescq);
1775 return ERR_PTR(-EFAULT);
1776 }
1777 }
1778
1779 return &nescq->ibcq;
1780}
1781
1782
1783/**
1784 * nes_destroy_cq
1785 */
1786static int nes_destroy_cq(struct ib_cq *ib_cq)
1787{
1788 struct nes_cq *nescq;
1789 struct nes_device *nesdev;
1790 struct nes_vnic *nesvnic;
1791 struct nes_adapter *nesadapter;
1792 struct nes_hw_cqp_wqe *cqp_wqe;
1793 struct nes_cqp_request *cqp_request;
1794 unsigned long flags;
1795 u32 opcode = 0;
1796 int ret;
1797
1798 if (ib_cq == NULL)
1799 return 0;
1800
1801 nescq = to_nescq(ib_cq);
1802 nesvnic = to_nesvnic(ib_cq->device);
1803 nesdev = nesvnic->nesdev;
1804 nesadapter = nesdev->nesadapter;
1805
1806 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1807
1808 /* Send DestroyCQ request to CQP */
1809 cqp_request = nes_get_cqp_request(nesdev);
1810 if (cqp_request == NULL) {
1811 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1812 return -ENOMEM;
1813 }
1814 cqp_request->waiting = 1;
1815 cqp_wqe = &cqp_request->cqp_wqe;
1816 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1817 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1818 if (nescq->virtual_cq == 1) {
1819 nesadapter->free_256pbl++;
1820 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1821 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001822 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001823 }
1824 } else if (nescq->virtual_cq == 2) {
1825 nesadapter->free_4kpbl++;
1826 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1827 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001828 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001829 }
1830 opcode |= NES_CQP_CQ_4KB_CHUNK;
1831 }
1832
1833 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1834
1835 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1836 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1837 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1838 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001839 if (!nescq->mcrqf)
1840 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1841
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001842 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001843 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001844
1845 /* Wait for CQP */
1846 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1847 nescq->hw_cq.cq_number);
1848 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1849 NES_EVENT_TIMEOUT);
1850 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1851 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1852 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1853 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001854 if (!ret) {
1855 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001856 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001857 ret = -ETIME;
1858 } else if (cqp_request->major_code) {
1859 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001860 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001861 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001862 } else {
1863 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001864 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001865 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001866
1867 if (nescq->cq_mem_size)
1868 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001869 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001870 kfree(nescq);
1871
1872 return ret;
1873}
1874
Don Wood0145f342009-03-06 15:15:00 -08001875/**
1876 * root_256
1877 */
1878static u32 root_256(struct nes_device *nesdev,
1879 struct nes_root_vpbl *root_vpbl,
1880 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001881 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001882{
1883 u64 leaf_pbl;
1884 int i, j, k;
1885
1886 if (pbl_count_4k == 1) {
1887 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1888 512, &new_root->pbl_pbase);
1889
1890 if (new_root->pbl_vbase == NULL)
1891 return 0;
1892
1893 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1894 for (i = 0; i < 16; i++) {
1895 new_root->pbl_vbase[i].pa_low =
1896 cpu_to_le32((u32)leaf_pbl);
1897 new_root->pbl_vbase[i].pa_high =
1898 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1899 leaf_pbl += 256;
1900 }
1901 } else {
1902 for (i = 3; i >= 0; i--) {
1903 j = i * 16;
1904 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1905 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1906 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1907 << 32);
1908 for (k = 1; k < 16; k++) {
1909 leaf_pbl += 256;
1910 root_vpbl->pbl_vbase[j + k].pa_low =
1911 cpu_to_le32((u32)leaf_pbl);
1912 root_vpbl->pbl_vbase[j + k].pa_high =
1913 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1914 }
1915 }
1916 }
1917
1918 return 1;
1919}
1920
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001921
1922/**
1923 * nes_reg_mr
1924 */
1925static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1926 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08001927 dma_addr_t single_buffer, u16 pbl_count_4k,
1928 u16 residual_page_count_4k, int acc, u64 *iova_start,
1929 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001930{
1931 struct nes_hw_cqp_wqe *cqp_wqe;
1932 struct nes_cqp_request *cqp_request;
1933 unsigned long flags;
1934 int ret;
1935 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08001936 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07001937 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08001938 u16 pbl_count = 0;
1939 u8 use_256_pbls = 0;
1940 u8 use_4k_pbls = 0;
1941 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
Or Gerlitz812d8672010-07-20 12:00:20 +00001942 struct nes_root_vpbl new_root = { 0, NULL, NULL };
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001943 u32 opcode = 0;
1944 u16 major_code;
1945
1946 /* Register the region with the adapter */
1947 cqp_request = nes_get_cqp_request(nesdev);
1948 if (cqp_request == NULL) {
1949 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1950 return -ENOMEM;
1951 }
1952 cqp_request->waiting = 1;
1953 cqp_wqe = &cqp_request->cqp_wqe;
1954
Don Wood0145f342009-03-06 15:15:00 -08001955 if (pbl_count_4k) {
1956 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1957
1958 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1959 pbl_count_256 = (pg_cnt + 31) / 32;
1960 if (pg_cnt <= 32) {
1961 if (pbl_count_256 <= nesadapter->free_256pbl)
1962 use_256_pbls = 1;
1963 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1964 use_4k_pbls = 1;
1965 } else if (pg_cnt <= 2048) {
1966 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1967 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1968 use_4k_pbls = 1;
1969 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1970 use_256_pbls = 1;
1971 use_two_level = 1;
1972 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1973 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001974 }
1975 } else {
Don Wood0145f342009-03-06 15:15:00 -08001976 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1977 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001978 }
Don Wood0145f342009-03-06 15:15:00 -08001979
1980 if (use_256_pbls) {
1981 pbl_count = pbl_count_256;
1982 nesadapter->free_256pbl -= pbl_count + use_two_level;
1983 } else if (use_4k_pbls) {
1984 pbl_count = pbl_count_4k;
1985 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1986 } else {
1987 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1988 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1989 nes_free_cqp_request(nesdev, cqp_request);
1990 return -ENOMEM;
1991 }
1992
1993 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001994 }
1995
Don Wood0145f342009-03-06 15:15:00 -08001996 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07001997 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08001998 if (new_root.pbl_pbase != 0)
1999 root_vpbl = &new_root;
2000 } else {
2001 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2002 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
2003 use_256_pbls = 0;
2004
2005 if (pbl_count_4k == 1)
2006 use_two_level = 0;
2007 pbl_count = pbl_count_4k;
2008
2009 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2010 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2011 use_4k_pbls = 1;
2012 }
2013 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2014
2015 if (use_4k_pbls == 0)
2016 return -ENOMEM;
2017 }
2018 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002019
2020 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2021 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2022 if (acc & IB_ACCESS_LOCAL_WRITE)
2023 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2024 if (acc & IB_ACCESS_REMOTE_WRITE)
2025 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2026 if (acc & IB_ACCESS_REMOTE_READ)
2027 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2028 if (acc & IB_ACCESS_MW_BIND)
2029 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2030
2031 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2032 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2033 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2034 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2035
2036 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2037 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2038 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2039 cpu_to_le32(nespd->pd_id & 0x00007fff);
2040 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2041
2042 if (pbl_count == 0) {
2043 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2044 } else {
2045 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2046 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 -08002047 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 -08002048
Don Wood0145f342009-03-06 15:15:00 -08002049 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002050 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2051 }
2052 barrier();
2053
2054 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002055 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002056
2057 /* Wait for CQP */
2058 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2059 NES_EVENT_TIMEOUT);
2060 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2061 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2062 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2063 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002064 nes_put_cqp_request(nesdev, cqp_request);
2065
Don Wooddae5d132009-03-06 15:12:09 -08002066 if ((!ret || major_code) && pbl_count != 0) {
2067 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08002068 if (use_256_pbls)
2069 nesadapter->free_256pbl += pbl_count + use_two_level;
2070 else if (use_4k_pbls)
2071 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08002072 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2073 }
Don Wood0145f342009-03-06 15:15:00 -08002074 if (new_root.pbl_pbase)
2075 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2076 new_root.pbl_pbase);
2077
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002078 if (!ret)
2079 return -ETIME;
2080 else if (major_code)
2081 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002082
Don Wood0145f342009-03-06 15:15:00 -08002083 *actual_pbl_cnt = pbl_count + use_two_level;
2084 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002085 return 0;
2086}
2087
2088
2089/**
2090 * nes_reg_phys_mr
2091 */
2092static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2093 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2094 u64 * iova_start)
2095{
2096 u64 region_length;
2097 struct nes_pd *nespd = to_nespd(ib_pd);
2098 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2099 struct nes_device *nesdev = nesvnic->nesdev;
2100 struct nes_adapter *nesadapter = nesdev->nesadapter;
2101 struct nes_mr *nesmr;
2102 struct ib_mr *ibmr;
2103 struct nes_vpbl vpbl;
2104 struct nes_root_vpbl root_vpbl;
2105 u32 stag;
2106 u32 i;
Don Wood3f32eb12009-04-20 14:53:00 -07002107 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002108 u32 stag_index = 0;
2109 u32 next_stag_index = 0;
2110 u32 driver_key = 0;
2111 u32 root_pbl_index = 0;
2112 u32 cur_pbl_index = 0;
Roland Dreier69d51022010-08-04 14:25:40 -07002113 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002114 int ret = 0;
2115 u16 pbl_count = 0;
2116 u8 single_page = 1;
2117 u8 stag_key = 0;
2118
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002119 region_length = 0;
2120 vpbl.pbl_vbase = NULL;
2121 root_vpbl.pbl_vbase = NULL;
2122 root_vpbl.pbl_pbase = 0;
2123
2124 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2125 stag_key = (u8)next_stag_index;
2126
2127 driver_key = 0;
2128
2129 next_stag_index >>= 8;
2130 next_stag_index %= nesadapter->max_mr;
2131 if (num_phys_buf > (1024*512)) {
2132 return ERR_PTR(-E2BIG);
2133 }
2134
Don Wood3f32eb12009-04-20 14:53:00 -07002135 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2136 return ERR_PTR(-EINVAL);
2137
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002138 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002139 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002140 if (err) {
2141 return ERR_PTR(err);
2142 }
2143
2144 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2145 if (!nesmr) {
2146 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2147 return ERR_PTR(-ENOMEM);
2148 }
2149
2150 for (i = 0; i < num_phys_buf; i++) {
2151
2152 if ((i & 0x01FF) == 0) {
2153 if (root_pbl_index == 1) {
2154 /* Allocate the root PBL */
2155 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2156 &root_vpbl.pbl_pbase);
2157 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2158 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2159 if (!root_vpbl.pbl_vbase) {
2160 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2161 vpbl.pbl_pbase);
2162 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2163 kfree(nesmr);
2164 return ERR_PTR(-ENOMEM);
2165 }
2166 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2167 if (!root_vpbl.leaf_vpbl) {
2168 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2169 root_vpbl.pbl_pbase);
2170 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2171 vpbl.pbl_pbase);
2172 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2173 kfree(nesmr);
2174 return ERR_PTR(-ENOMEM);
2175 }
2176 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2177 root_vpbl.pbl_vbase[0].pa_high =
2178 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2179 root_vpbl.leaf_vpbl[0] = vpbl;
2180 }
2181 /* Allocate a 4K buffer for the PBL */
2182 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2183 &vpbl.pbl_pbase);
2184 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2185 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2186 if (!vpbl.pbl_vbase) {
2187 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2188 ibmr = ERR_PTR(-ENOMEM);
2189 kfree(nesmr);
2190 goto reg_phys_err;
2191 }
2192 /* Fill in the root table */
2193 if (1 <= root_pbl_index) {
2194 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2195 cpu_to_le32((u32)vpbl.pbl_pbase);
2196 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2197 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2198 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2199 }
2200 root_pbl_index++;
2201 cur_pbl_index = 0;
2202 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002203
Don Wood3f32eb12009-04-20 14:53:00 -07002204 mask = !buffer_list[i].size;
2205 if (i != 0)
2206 mask |= buffer_list[i].addr;
2207 if (i != num_phys_buf - 1)
2208 mask |= buffer_list[i].addr + buffer_list[i].size;
2209
2210 if (mask & ~PAGE_MASK) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002211 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
Don Wood3f32eb12009-04-20 14:53:00 -07002212 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002213 ibmr = ERR_PTR(-EINVAL);
2214 kfree(nesmr);
2215 goto reg_phys_err;
2216 }
2217
2218 region_length += buffer_list[i].size;
2219 if ((i != 0) && (single_page)) {
2220 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2221 single_page = 0;
2222 }
Don Wood3f32eb12009-04-20 14:53:00 -07002223 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 -08002224 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2225 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2226 }
2227
2228 stag = stag_index << 8;
2229 stag |= driver_key;
2230 stag += (u32)stag_key;
2231
2232 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2233 " length = 0x%016lX, index = 0x%08X\n",
2234 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2235
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002236 /* Make the leaf PBL the root if only one PBL */
2237 if (root_pbl_index == 1) {
2238 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2239 }
2240
2241 if (single_page) {
2242 pbl_count = 0;
2243 } else {
2244 pbl_count = root_pbl_index;
2245 }
2246 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08002247 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2248 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249
2250 if (ret == 0) {
2251 nesmr->ibmr.rkey = stag;
2252 nesmr->ibmr.lkey = stag;
2253 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2254 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002255 } else {
2256 kfree(nesmr);
2257 ibmr = ERR_PTR(-ENOMEM);
2258 }
2259
2260 reg_phys_err:
2261 /* free the resources */
2262 if (root_pbl_index == 1) {
2263 /* single PBL case */
2264 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2265 } else {
2266 for (i=0; i<root_pbl_index; i++) {
2267 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2268 root_vpbl.leaf_vpbl[i].pbl_pbase);
2269 }
2270 kfree(root_vpbl.leaf_vpbl);
2271 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2272 root_vpbl.pbl_pbase);
2273 }
2274
2275 return ibmr;
2276}
2277
2278
2279/**
2280 * nes_get_dma_mr
2281 */
2282static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2283{
2284 struct ib_phys_buf bl;
2285 u64 kva = 0;
2286
2287 nes_debug(NES_DBG_MR, "\n");
2288
2289 bl.size = (u64)0xffffffffffULL;
2290 bl.addr = 0;
2291 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2292}
2293
2294
2295/**
2296 * nes_reg_user_mr
2297 */
2298static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2299 u64 virt, int acc, struct ib_udata *udata)
2300{
2301 u64 iova_start;
2302 __le64 *pbl;
2303 u64 region_length;
2304 dma_addr_t last_dma_addr = 0;
2305 dma_addr_t first_dma_addr = 0;
2306 struct nes_pd *nespd = to_nespd(pd);
2307 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2308 struct nes_device *nesdev = nesvnic->nesdev;
2309 struct nes_adapter *nesadapter = nesdev->nesadapter;
2310 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002311 struct scatterlist *sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002312 struct nes_ucontext *nes_ucontext;
2313 struct nes_pbl *nespbl;
2314 struct nes_mr *nesmr;
2315 struct ib_umem *region;
2316 struct nes_mem_reg_req req;
2317 struct nes_vpbl vpbl;
2318 struct nes_root_vpbl root_vpbl;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002319 int entry, page_index;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002320 int page_count = 0;
2321 int err, pbl_depth = 0;
2322 int chunk_pages;
2323 int ret;
2324 u32 stag;
2325 u32 stag_index = 0;
2326 u32 next_stag_index;
2327 u32 driver_key;
2328 u32 root_pbl_index = 0;
2329 u32 cur_pbl_index = 0;
2330 u32 skip_pages;
2331 u16 pbl_count;
2332 u8 single_page = 1;
2333 u8 stag_key;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002334 int first_page = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002335
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002336 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002337 if (IS_ERR(region)) {
2338 return (struct ib_mr *)region;
2339 }
2340
2341 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2342 " offset = %u, page size = %u.\n",
2343 (unsigned long int)start, (unsigned long int)virt, (u32)length,
Haggai Eran406f9e52014-12-11 17:04:12 +02002344 ib_umem_offset(region), region->page_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002345
Haggai Eran406f9e52014-12-11 17:04:12 +02002346 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002347
Julia Lawalle2e435f2011-10-06 09:33:24 -07002348 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2349 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002350 return ERR_PTR(-EFAULT);
Julia Lawalle2e435f2011-10-06 09:33:24 -07002351 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002352 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2353
2354 switch (req.reg_type) {
2355 case IWNES_MEMREG_TYPE_MEM:
2356 pbl_depth = 0;
2357 region_length = 0;
2358 vpbl.pbl_vbase = NULL;
2359 root_vpbl.pbl_vbase = NULL;
2360 root_vpbl.pbl_pbase = 0;
2361
2362 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2363 stag_key = (u8)next_stag_index;
2364
2365 driver_key = next_stag_index & 0x70000000;
2366
2367 next_stag_index >>= 8;
2368 next_stag_index %= nesadapter->max_mr;
2369
2370 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002371 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002372 if (err) {
2373 ib_umem_release(region);
2374 return ERR_PTR(err);
2375 }
2376
2377 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2378 if (!nesmr) {
2379 ib_umem_release(region);
2380 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2381 return ERR_PTR(-ENOMEM);
2382 }
2383 nesmr->region = region;
2384
Yishai Hadaseeb84612014-01-28 13:40:15 +02002385 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2386 if (sg_dma_address(sg) & ~PAGE_MASK) {
2387 ib_umem_release(region);
2388 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2389 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2390 (unsigned int) sg_dma_address(sg));
2391 ibmr = ERR_PTR(-EINVAL);
2392 kfree(nesmr);
2393 goto reg_user_mr_err;
2394 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002395
Yishai Hadaseeb84612014-01-28 13:40:15 +02002396 if (!sg_dma_len(sg)) {
2397 ib_umem_release(region);
2398 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2399 stag_index);
2400 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2401 ibmr = ERR_PTR(-EINVAL);
2402 kfree(nesmr);
2403 goto reg_user_mr_err;
2404 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002405
Yishai Hadaseeb84612014-01-28 13:40:15 +02002406 region_length += sg_dma_len(sg);
2407 chunk_pages = sg_dma_len(sg) >> 12;
2408 region_length -= skip_pages << 12;
2409 for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2410 skip_pages = 0;
Haggai Eran406f9e52014-12-11 17:04:12 +02002411 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
Yishai Hadaseeb84612014-01-28 13:40:15 +02002412 goto enough_pages;
2413 if ((page_count&0x01FF) == 0) {
2414 if (page_count >= 1024 * 512) {
2415 ib_umem_release(region);
2416 nes_free_resource(nesadapter,
2417 nesadapter->allocated_mrs, stag_index);
2418 kfree(nesmr);
2419 ibmr = ERR_PTR(-E2BIG);
2420 goto reg_user_mr_err;
2421 }
2422 if (root_pbl_index == 1) {
2423 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2424 8192, &root_vpbl.pbl_pbase);
2425 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2426 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2427 if (!root_vpbl.pbl_vbase) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002428 ib_umem_release(region);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002429 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2430 vpbl.pbl_pbase);
2431 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2432 stag_index);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002433 kfree(nesmr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002434 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002435 goto reg_user_mr_err;
2436 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002437 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2438 GFP_KERNEL);
2439 if (!root_vpbl.leaf_vpbl) {
2440 ib_umem_release(region);
2441 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2442 root_vpbl.pbl_pbase);
2443 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2444 vpbl.pbl_pbase);
2445 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2446 stag_index);
2447 kfree(nesmr);
2448 ibmr = ERR_PTR(-ENOMEM);
2449 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002450 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002451 root_vpbl.pbl_vbase[0].pa_low =
2452 cpu_to_le32((u32)vpbl.pbl_pbase);
2453 root_vpbl.pbl_vbase[0].pa_high =
2454 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2455 root_vpbl.leaf_vpbl[0] = vpbl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002456 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002457 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2458 &vpbl.pbl_pbase);
2459 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2460 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2461 if (!vpbl.pbl_vbase) {
2462 ib_umem_release(region);
2463 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2464 ibmr = ERR_PTR(-ENOMEM);
2465 kfree(nesmr);
2466 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002467 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002468 if (1 <= root_pbl_index) {
2469 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2470 cpu_to_le32((u32)vpbl.pbl_pbase);
2471 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2472 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2473 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2474 }
2475 root_pbl_index++;
2476 cur_pbl_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002477 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002478 if (single_page) {
2479 if (page_count != 0) {
2480 if ((last_dma_addr+4096) !=
2481 (sg_dma_address(sg)+
2482 (page_index*4096)))
2483 single_page = 0;
2484 last_dma_addr = sg_dma_address(sg)+
2485 (page_index*4096);
2486 } else {
2487 first_dma_addr = sg_dma_address(sg)+
2488 (page_index*4096);
2489 last_dma_addr = first_dma_addr;
2490 }
2491 }
2492
2493 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2494 cpu_to_le32((u32)(sg_dma_address(sg)+
2495 (page_index*4096)));
2496 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2497 cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2498 (page_index*4096))) >> 32)));
2499 cur_pbl_index++;
2500 page_count++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002501 }
2502 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002503
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002504 enough_pages:
2505 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2506 " stag_key=0x%08x\n",
2507 stag_index, driver_key, stag_key);
2508 stag = stag_index << 8;
2509 stag |= driver_key;
2510 stag += (u32)stag_key;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002511
2512 iova_start = virt;
2513 /* Make the leaf PBL the root if only one PBL */
2514 if (root_pbl_index == 1) {
2515 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2516 }
2517
2518 if (single_page) {
2519 pbl_count = 0;
2520 } else {
2521 pbl_count = root_pbl_index;
2522 first_dma_addr = 0;
2523 }
2524 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2525 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2526 stag, (unsigned int)iova_start,
2527 (unsigned int)region_length, stag_index,
2528 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002529 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2530 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2531 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002532
2533 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2534
2535 if (ret == 0) {
2536 nesmr->ibmr.rkey = stag;
2537 nesmr->ibmr.lkey = stag;
2538 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2539 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002540 } else {
2541 ib_umem_release(region);
2542 kfree(nesmr);
2543 ibmr = ERR_PTR(-ENOMEM);
2544 }
2545
2546 reg_user_mr_err:
2547 /* free the resources */
2548 if (root_pbl_index == 1) {
2549 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2550 vpbl.pbl_pbase);
2551 } else {
2552 for (page_index=0; page_index<root_pbl_index; page_index++) {
2553 pci_free_consistent(nesdev->pcidev, 4096,
2554 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2555 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2556 }
2557 kfree(root_vpbl.leaf_vpbl);
2558 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2559 root_vpbl.pbl_pbase);
2560 }
2561
2562 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2563
2564 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002565 case IWNES_MEMREG_TYPE_QP:
2566 case IWNES_MEMREG_TYPE_CQ:
Tatyana Nikolova7d9c1992012-12-06 20:05:02 +00002567 if (!region->length) {
2568 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2569 ib_umem_release(region);
2570 return ERR_PTR(-EINVAL);
2571 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002572 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2573 if (!nespbl) {
2574 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2575 ib_umem_release(region);
2576 return ERR_PTR(-ENOMEM);
2577 }
2578 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2579 if (!nesmr) {
2580 ib_umem_release(region);
2581 kfree(nespbl);
2582 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2583 return ERR_PTR(-ENOMEM);
2584 }
2585 nesmr->region = region;
2586 nes_ucontext = to_nesucontext(pd->uobject->context);
2587 pbl_depth = region->length >> 12;
2588 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2589 nespbl->pbl_size = pbl_depth*sizeof(u64);
2590 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2591 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2592 } else {
2593 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2594 }
2595
2596 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2597 nespbl->pbl_size, pbl_depth);
2598 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2599 &nespbl->pbl_pbase);
2600 if (!pbl) {
2601 ib_umem_release(region);
2602 kfree(nesmr);
2603 kfree(nespbl);
2604 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2605 return ERR_PTR(-ENOMEM);
2606 }
2607
2608 nespbl->pbl_vbase = (u64 *)pbl;
2609 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002610 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002611 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002612 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2613 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002614
Yishai Hadaseeb84612014-01-28 13:40:15 +02002615 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2616 chunk_pages = sg_dma_len(sg) >> 12;
2617 chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2618 if (first_page) {
2619 nespbl->page = sg_page(sg);
2620 first_page = 0;
2621 }
2622
2623 for (page_index = 0; page_index < chunk_pages; page_index++) {
2624 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2625 (sg_dma_address(sg)+
2626 (page_index*4096)));
2627 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2628 (sg_dma_address(sg)+
2629 (page_index*4096)))>>32);
2630 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2631 (unsigned long long)*pbl,
2632 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2633 pbl++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002634 }
2635 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002636
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002637 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2638 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2639 } else {
2640 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2641 }
2642 nesmr->ibmr.rkey = -1;
2643 nesmr->ibmr.lkey = -1;
2644 nesmr->mode = req.reg_type;
2645 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002646 }
2647
Julia Lawalle2e435f2011-10-06 09:33:24 -07002648 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002649 return ERR_PTR(-ENOSYS);
2650}
2651
2652
2653/**
2654 * nes_dereg_mr
2655 */
2656static int nes_dereg_mr(struct ib_mr *ib_mr)
2657{
2658 struct nes_mr *nesmr = to_nesmr(ib_mr);
2659 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2660 struct nes_device *nesdev = nesvnic->nesdev;
2661 struct nes_adapter *nesadapter = nesdev->nesadapter;
2662 struct nes_hw_cqp_wqe *cqp_wqe;
2663 struct nes_cqp_request *cqp_request;
2664 unsigned long flags;
2665 int ret;
2666 u16 major_code;
2667 u16 minor_code;
2668
2669 if (nesmr->region) {
2670 ib_umem_release(nesmr->region);
2671 }
2672 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2673 kfree(nesmr);
2674 return 0;
2675 }
2676
2677 /* Deallocate the region with the adapter */
2678
2679 cqp_request = nes_get_cqp_request(nesdev);
2680 if (cqp_request == NULL) {
2681 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2682 return -ENOMEM;
2683 }
2684 cqp_request->waiting = 1;
2685 cqp_wqe = &cqp_request->cqp_wqe;
2686
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002687 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2688 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2689 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2690 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2691 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2692
2693 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002694 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002695
2696 /* Wait for CQP */
2697 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2698 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2699 NES_EVENT_TIMEOUT);
2700 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2701 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2702 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2703
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002704 major_code = cqp_request->major_code;
2705 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002706
2707 nes_put_cqp_request(nesdev, cqp_request);
2708
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002709 if (!ret) {
2710 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2711 " ib_mr=%p, rkey = 0x%08X\n",
2712 ib_mr, ib_mr->rkey);
2713 return -ETIME;
2714 } else if (major_code) {
2715 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2716 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2717 major_code, minor_code, ib_mr, ib_mr->rkey);
2718 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002719 }
2720
2721 if (nesmr->pbls_used != 0) {
2722 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2723 if (nesmr->pbl_4k) {
2724 nesadapter->free_4kpbl += nesmr->pbls_used;
2725 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2726 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2727 "exceeded the max(%u)\n",
2728 nesadapter->free_4kpbl,
2729 nesadapter->max_4kpbl);
2730 } else {
2731 nesadapter->free_256pbl += nesmr->pbls_used;
2732 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2733 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2734 "exceeded the max(%u)\n",
2735 nesadapter->free_256pbl,
2736 nesadapter->max_256pbl);
2737 }
2738 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2739 }
2740 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2741 (ib_mr->rkey & 0x0fffff00) >> 8);
2742
2743 kfree(nesmr);
2744
2745 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002746}
2747
2748
2749/**
2750 * show_rev
2751 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002752static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2753 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002754{
2755 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002756 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002757 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2758
2759 nes_debug(NES_DBG_INIT, "\n");
2760 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2761}
2762
2763
2764/**
2765 * show_fw_ver
2766 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002767static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2768 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002769{
2770 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002771 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002772 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2773
2774 nes_debug(NES_DBG_INIT, "\n");
Chien Tung0e4562d2009-04-27 13:33:48 -07002775 return sprintf(buf, "%u.%u\n",
2776 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2777 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002778}
2779
2780
2781/**
2782 * show_hca
2783 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002784static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2785 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002786{
2787 nes_debug(NES_DBG_INIT, "\n");
2788 return sprintf(buf, "NES020\n");
2789}
2790
2791
2792/**
2793 * show_board
2794 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002795static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2796 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002797{
2798 nes_debug(NES_DBG_INIT, "\n");
2799 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2800}
2801
2802
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002803static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2804static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2805static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2806static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002807
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002808static struct device_attribute *nes_dev_attributes[] = {
2809 &dev_attr_hw_rev,
2810 &dev_attr_fw_ver,
2811 &dev_attr_hca_type,
2812 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002813};
2814
2815
2816/**
2817 * nes_query_qp
2818 */
2819static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2820 int attr_mask, struct ib_qp_init_attr *init_attr)
2821{
2822 struct nes_qp *nesqp = to_nesqp(ibqp);
2823
2824 nes_debug(NES_DBG_QP, "\n");
2825
2826 attr->qp_access_flags = 0;
2827 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2828 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2829 attr->cap.max_recv_sge = 1;
Chien Tungeadde3a2010-03-25 13:39:50 +00002830 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2831 attr->cap.max_inline_data = 0;
2832 else
2833 attr->cap.max_inline_data = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002834
2835 init_attr->event_handler = nesqp->ibqp.event_handler;
2836 init_attr->qp_context = nesqp->ibqp.qp_context;
2837 init_attr->send_cq = nesqp->ibqp.send_cq;
2838 init_attr->recv_cq = nesqp->ibqp.recv_cq;
Dave Jones4127c362013-09-17 17:45:54 -04002839 init_attr->srq = nesqp->ibqp.srq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002840 init_attr->cap = attr->cap;
2841
2842 return 0;
2843}
2844
2845
2846/**
2847 * nes_hw_modify_qp
2848 */
2849int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002850 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002851{
2852 struct nes_hw_cqp_wqe *cqp_wqe;
2853 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2854 /* struct iw_cm_event cm_event; */
2855 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856 int ret;
2857 u16 major_code;
2858
2859 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2860 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2861
2862 cqp_request = nes_get_cqp_request(nesdev);
2863 if (cqp_request == NULL) {
2864 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2865 return -ENOMEM;
2866 }
2867 if (wait_completion) {
2868 cqp_request->waiting = 1;
2869 } else {
2870 cqp_request->waiting = 0;
2871 }
2872 cqp_wqe = &cqp_request->cqp_wqe;
2873
2874 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2875 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2876 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2877 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2878 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2879 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2880 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2881
Don Wood8b1c9dc2009-09-05 20:36:38 -07002882 /* If sending a terminate message, fill in the length (in words) */
2883 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2884 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2885 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2886 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2887 }
2888
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002889 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002890 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002891
2892 /* Wait for CQP */
2893 if (wait_completion) {
2894 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2895 nesqp->hwqp.qp_id); */
2896 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2897 NES_EVENT_TIMEOUT);
2898 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2899 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2900 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2901 major_code = cqp_request->major_code;
2902 if (major_code) {
2903 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2904 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2905 nesqp->hwqp.qp_id, cqp_request->major_code,
2906 cqp_request->minor_code, next_iwarp_state);
2907 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002908
2909 nes_put_cqp_request(nesdev, cqp_request);
2910
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002911 if (!ret)
2912 return -ETIME;
2913 else if (major_code)
2914 return -EIO;
2915 else
2916 return 0;
2917 } else {
2918 return 0;
2919 }
2920}
2921
2922
2923/**
2924 * nes_modify_qp
2925 */
2926int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2927 int attr_mask, struct ib_udata *udata)
2928{
2929 struct nes_qp *nesqp = to_nesqp(ibqp);
2930 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2931 struct nes_device *nesdev = nesvnic->nesdev;
2932 /* u32 cqp_head; */
2933 /* u32 counter; */
2934 u32 next_iwarp_state = 0;
2935 int err;
2936 unsigned long qplockflags;
2937 int ret;
2938 u16 original_last_aeq;
2939 u8 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940 u8 dont_wait = 0;
2941
2942 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2943 " iwarp_state=0x%X, refcount=%d\n",
2944 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2945 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2946
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002947 spin_lock_irqsave(&nesqp->lock, qplockflags);
2948
2949 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2950 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2951 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2952 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2953
2954 if (attr_mask & IB_QP_STATE) {
2955 switch (attr->qp_state) {
2956 case IB_QPS_INIT:
2957 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2958 nesqp->hwqp.qp_id);
2959 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2960 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002961 return -EINVAL;
2962 }
2963 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2964 issue_modify_qp = 1;
2965 break;
2966 case IB_QPS_RTR:
2967 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2968 nesqp->hwqp.qp_id);
2969 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2970 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002971 return -EINVAL;
2972 }
2973 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2974 issue_modify_qp = 1;
2975 break;
2976 case IB_QPS_RTS:
2977 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2978 nesqp->hwqp.qp_id);
2979 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2980 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002981 return -EINVAL;
2982 }
2983 if (nesqp->cm_id == NULL) {
2984 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2985 nesqp->hwqp.qp_id );
2986 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002987 return -EINVAL;
2988 }
2989 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2990 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2991 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2992 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2993 issue_modify_qp = 1;
2994 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2995 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2996 nesqp->hte_added = 1;
2997 break;
2998 case IB_QPS_SQD:
2999 issue_modify_qp = 1;
3000 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3001 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3002 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3003 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003004 return 0;
3005 } else {
3006 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3007 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3008 " ignored due to current iWARP state\n",
3009 nesqp->hwqp.qp_id);
3010 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003011 return -EINVAL;
3012 }
3013 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3014 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3015 " already done based on hw state.\n",
3016 nesqp->hwqp.qp_id);
3017 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003018 }
3019 switch (nesqp->hw_iwarp_state) {
3020 case NES_AEQE_IWARP_STATE_CLOSING:
3021 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Tatyana Nikolovad5fb4762012-10-02 16:38:12 +00003022 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 case NES_AEQE_IWARP_STATE_TERMINATE:
3024 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3025 break;
3026 case NES_AEQE_IWARP_STATE_ERROR:
3027 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3028 break;
3029 default:
3030 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003031 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3032 break;
3033 }
3034 }
3035 break;
3036 case IB_QPS_SQE:
3037 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3038 nesqp->hwqp.qp_id);
3039 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3040 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003041 return -EINVAL;
3042 }
3043 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3044 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3045 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3046 issue_modify_qp = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003047 break;
3048 case IB_QPS_ERR:
3049 case IB_QPS_RESET:
3050 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3051 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003052 return -EINVAL;
3053 }
3054 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3055 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07003056 if (nesqp->term_flags)
3057 del_timer(&nesqp->terminate_timer);
3058
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003059 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3060 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3061 if (nesqp->hte_added) {
3062 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3063 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3064 nesqp->hte_added = 0;
3065 }
3066 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
Faisal Latifcd6860e2010-07-04 00:17:59 +00003067 (nesdev->iw_status) &&
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003068 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3069 next_iwarp_state |= NES_CQP_QP_RESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003070 } else {
3071 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3072 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3073 dont_wait = 1;
3074 }
3075 issue_modify_qp = 1;
3076 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3077 break;
3078 default:
3079 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003080 return -EINVAL;
3081 break;
3082 }
3083
3084 nesqp->ibqp_state = attr->qp_state;
Tatyana Nikolovaa67b0782012-10-02 16:40:33 +00003085 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3086 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3087 nesqp->iwarp_state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088 }
3089
3090 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3091 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3092 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3093 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3094 issue_modify_qp = 1;
3095 }
3096 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3097 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3098 issue_modify_qp = 1;
3099 }
3100 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3101 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3102 issue_modify_qp = 1;
3103 }
3104 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3105 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3106 issue_modify_qp = 1;
3107 }
3108
3109 if (nesqp->user_mode) {
3110 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3111 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3112 issue_modify_qp = 1;
3113 }
3114 }
3115
3116 original_last_aeq = nesqp->last_aeq;
3117 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3118
3119 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3120
3121 ret = 0;
3122
3123
3124 if (issue_modify_qp) {
3125 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07003126 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127 if (ret)
3128 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3129 " failed for QP%u.\n",
3130 next_iwarp_state, nesqp->hwqp.qp_id);
3131
3132 }
3133
3134 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3135 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3136 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3137 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3138 original_last_aeq, nesqp->last_aeq);
Dan Carpenter349850f2014-02-06 15:48:23 +03003139 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003140 if (dont_wait) {
3141 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3142 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3143 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3144 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3145 original_last_aeq, nesqp->last_aeq);
3146 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003147 spin_lock_irqsave(&nesqp->lock, qplockflags);
3148 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3149 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3150 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3151 nes_cm_disconn(nesqp);
3152 } else {
3153 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3154 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003155 }
3156 } else {
3157 spin_lock_irqsave(&nesqp->lock, qplockflags);
3158 if (nesqp->cm_id) {
3159 /* These two are for the timer thread */
3160 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003161 nesqp->cm_id->add_ref(nesqp->cm_id);
3162 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3163 " need ae to finish up, original_last_aeq = 0x%04X."
3164 " last_aeq = 0x%04X, scheduling timer.\n",
3165 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3166 original_last_aeq, nesqp->last_aeq);
3167 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3168 }
3169 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3170 } else {
3171 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3172 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3173 " need ae to finish up, original_last_aeq = 0x%04X."
3174 " last_aeq = 0x%04X.\n",
3175 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3176 original_last_aeq, nesqp->last_aeq);
3177 }
3178 }
3179 } else {
3180 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3181 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3182 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3183 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003184 }
3185 } else {
3186 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3187 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3188 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3189 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003190 }
3191
3192 err = 0;
3193
3194 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3195 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3196
3197 return err;
3198}
3199
3200
3201/**
3202 * nes_muticast_attach
3203 */
3204static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3205{
3206 nes_debug(NES_DBG_INIT, "\n");
3207 return -ENOSYS;
3208}
3209
3210
3211/**
3212 * nes_multicast_detach
3213 */
3214static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3215{
3216 nes_debug(NES_DBG_INIT, "\n");
3217 return -ENOSYS;
3218}
3219
3220
3221/**
3222 * nes_process_mad
3223 */
3224static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3225 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3226 struct ib_mad *in_mad, struct ib_mad *out_mad)
3227{
3228 nes_debug(NES_DBG_INIT, "\n");
3229 return -ENOSYS;
3230}
3231
3232static inline void
3233fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3234{
3235 int sge_index;
3236 int total_payload_length = 0;
3237 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3238 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3239 ib_wr->sg_list[sge_index].addr);
3240 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3241 ib_wr->sg_list[sge_index].length);
3242 if (uselkey)
3243 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3244 (ib_wr->sg_list[sge_index].lkey));
3245 else
3246 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3247
3248 total_payload_length += ib_wr->sg_list[sge_index].length;
3249 }
3250 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3251 total_payload_length);
3252 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3253 total_payload_length);
3254}
3255
3256/**
3257 * nes_post_send
3258 */
3259static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3260 struct ib_send_wr **bad_wr)
3261{
3262 u64 u64temp;
3263 unsigned long flags = 0;
3264 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3265 struct nes_device *nesdev = nesvnic->nesdev;
3266 struct nes_qp *nesqp = to_nesqp(ibqp);
3267 struct nes_hw_qp_wqe *wqe;
Chien Tung649fe4a2009-12-09 13:51:37 -08003268 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003269 u32 qsize = nesqp->hwqp.sq_size;
3270 u32 head;
Chien Tung649fe4a2009-12-09 13:51:37 -08003271 u32 wqe_misc = 0;
3272 u32 wqe_count = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003273 u32 counter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003274
Frank Zagoe5dec392009-12-09 13:51:36 -08003275 if (nesqp->ibqp_state > IB_QPS_RTS) {
3276 err = -EINVAL;
3277 goto out;
3278 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003279
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003280 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003281
3282 head = nesqp->hwqp.sq_head;
3283
3284 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003285 /* Check for QP error */
3286 if (nesqp->term_flags) {
3287 err = -EINVAL;
3288 break;
3289 }
3290
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 /* Check for SQ overflow */
3292 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003293 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003294 break;
3295 }
3296
3297 wqe = &nesqp->hwqp.sq_vbase[head];
3298 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3299 nesqp->hwqp.qp_id, wqe, head); */
3300 nes_fill_init_qp_wqe(wqe, nesqp, head);
3301 u64temp = (u64)(ib_wr->wr_id);
3302 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3303 u64temp);
Chien Tung649fe4a2009-12-09 13:51:37 -08003304 switch (ib_wr->opcode) {
3305 case IB_WR_SEND:
3306 case IB_WR_SEND_WITH_INV:
3307 if (IB_WR_SEND == ib_wr->opcode) {
3308 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3309 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3310 else
3311 wqe_misc = NES_IWARP_SQ_OP_SEND;
3312 } else {
3313 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3314 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3315 else
3316 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317
Chien Tung649fe4a2009-12-09 13:51:37 -08003318 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3319 ib_wr->ex.invalidate_rkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003320 }
3321
Chien Tung649fe4a2009-12-09 13:51:37 -08003322 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3323 err = -EINVAL;
3324 break;
3325 }
3326
3327 if (ib_wr->send_flags & IB_SEND_FENCE)
3328 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3329
3330 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3331 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3332 (ib_wr->sg_list[0].length <= 64)) {
3333 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3334 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3335 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3336 ib_wr->sg_list[0].length);
3337 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3338 } else {
3339 fill_wqe_sg_send(wqe, ib_wr, 1);
3340 }
3341
3342 break;
3343 case IB_WR_RDMA_WRITE:
3344 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3345 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3346 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3347 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3348 err = -EINVAL;
3349 break;
3350 }
3351
3352 if (ib_wr->send_flags & IB_SEND_FENCE)
3353 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3354
3355 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3356 ib_wr->wr.rdma.rkey);
3357 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3358 ib_wr->wr.rdma.remote_addr);
3359
3360 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3361 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3362 (ib_wr->sg_list[0].length <= 64)) {
3363 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3364 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3365 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3366 ib_wr->sg_list[0].length);
3367 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3368 } else {
3369 fill_wqe_sg_send(wqe, ib_wr, 1);
3370 }
3371
3372 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3373 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3374 break;
3375 case IB_WR_RDMA_READ:
3376 case IB_WR_RDMA_READ_WITH_INV:
3377 /* iWARP only supports 1 sge for RDMA reads */
3378 if (ib_wr->num_sge > 1) {
3379 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3380 ib_wr->num_sge);
3381 err = -EINVAL;
3382 break;
3383 }
3384 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3385 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3386 } else {
3387 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3388 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3389 ib_wr->ex.invalidate_rkey);
3390 }
3391
3392 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3393 ib_wr->wr.rdma.remote_addr);
3394 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3395 ib_wr->wr.rdma.rkey);
3396 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3397 ib_wr->sg_list->length);
3398 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3399 ib_wr->sg_list->addr);
3400 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3401 ib_wr->sg_list->lkey);
3402 break;
3403 case IB_WR_LOCAL_INV:
3404 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3405 set_wqe_32bit_value(wqe->wqe_words,
3406 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3407 ib_wr->ex.invalidate_rkey);
3408 break;
Chien Tunge293a262009-12-09 15:21:54 -08003409 case IB_WR_FAST_REG_MR:
3410 {
3411 int i;
3412 int flags = ib_wr->wr.fast_reg.access_flags;
3413 struct nes_ib_fast_reg_page_list *pnesfrpl =
3414 container_of(ib_wr->wr.fast_reg.page_list,
3415 struct nes_ib_fast_reg_page_list,
3416 ibfrpl);
3417 u64 *src_page_list = pnesfrpl->ibfrpl.page_list;
3418 u64 *dst_page_list = pnesfrpl->nes_wqe_pbl.kva;
3419
3420 if (ib_wr->wr.fast_reg.page_list_len >
3421 (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3422 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3423 err = -EINVAL;
3424 break;
3425 }
3426 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3427 set_wqe_64bit_value(wqe->wqe_words,
3428 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3429 ib_wr->wr.fast_reg.iova_start);
3430 set_wqe_32bit_value(wqe->wqe_words,
3431 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3432 ib_wr->wr.fast_reg.length);
3433 set_wqe_32bit_value(wqe->wqe_words,
Tatyana Nikolova94f622b2012-01-06 18:15:01 -06003434 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3435 set_wqe_32bit_value(wqe->wqe_words,
Chien Tunge293a262009-12-09 15:21:54 -08003436 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3437 ib_wr->wr.fast_reg.rkey);
3438 /* Set page size: */
3439 if (ib_wr->wr.fast_reg.page_shift == 12) {
3440 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3441 } else if (ib_wr->wr.fast_reg.page_shift == 21) {
3442 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3443 } else {
3444 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3445 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3446 err = -EINVAL;
3447 break;
3448 }
3449 /* Set access_flags */
3450 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3451 if (flags & IB_ACCESS_LOCAL_WRITE)
3452 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3453
3454 if (flags & IB_ACCESS_REMOTE_WRITE)
3455 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3456
3457 if (flags & IB_ACCESS_REMOTE_READ)
3458 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3459
3460 if (flags & IB_ACCESS_MW_BIND)
3461 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3462
3463 /* Fill in PBL info: */
3464 if (ib_wr->wr.fast_reg.page_list_len >
3465 pnesfrpl->ibfrpl.max_page_list_len) {
3466 nes_debug(NES_DBG_IW_TX, "Invalid page list length,"
3467 " ib_wr=%p, value=%u, max=%u\n",
3468 ib_wr, ib_wr->wr.fast_reg.page_list_len,
3469 pnesfrpl->ibfrpl.max_page_list_len);
3470 err = -EINVAL;
3471 break;
3472 }
3473
3474 set_wqe_64bit_value(wqe->wqe_words,
3475 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3476 pnesfrpl->nes_wqe_pbl.paddr);
3477
3478 set_wqe_32bit_value(wqe->wqe_words,
3479 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3480 ib_wr->wr.fast_reg.page_list_len * 8);
3481
3482 for (i = 0; i < ib_wr->wr.fast_reg.page_list_len; i++)
3483 dst_page_list[i] = cpu_to_le64(src_page_list[i]);
3484
Roland Dreier183ae742010-09-27 17:51:33 -07003485 nes_debug(NES_DBG_IW_TX, "SQ_FMR: iova_start: %llx, "
3486 "length: %d, rkey: %0x, pgl_paddr: %llx, "
Chien Tunge293a262009-12-09 15:21:54 -08003487 "page_list_len: %u, wqe_misc: %x\n",
Roland Dreier183ae742010-09-27 17:51:33 -07003488 (unsigned long long) ib_wr->wr.fast_reg.iova_start,
Chien Tunge293a262009-12-09 15:21:54 -08003489 ib_wr->wr.fast_reg.length,
3490 ib_wr->wr.fast_reg.rkey,
Roland Dreier183ae742010-09-27 17:51:33 -07003491 (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr,
Chien Tunge293a262009-12-09 15:21:54 -08003492 ib_wr->wr.fast_reg.page_list_len,
3493 wqe_misc);
3494 break;
3495 }
Chien Tung649fe4a2009-12-09 13:51:37 -08003496 default:
3497 /* error */
3498 err = -EINVAL;
3499 break;
3500 }
3501
Frank Zagoe5dec392009-12-09 13:51:36 -08003502 if (err)
3503 break;
3504
Chien Tungd14152d2009-12-09 15:21:56 -08003505 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003506 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
Chien Tungd14152d2009-12-09 15:21:56 -08003507
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003508 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3509
3510 ib_wr = ib_wr->next;
3511 head++;
3512 wqe_count++;
3513 if (head >= qsize)
3514 head = 0;
3515
3516 }
3517
3518 nesqp->hwqp.sq_head = head;
3519 barrier();
3520 while (wqe_count) {
3521 counter = min(wqe_count, ((u32)255));
3522 wqe_count -= counter;
3523 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3524 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3525 }
3526
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003527 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003528
Frank Zagoe5dec392009-12-09 13:51:36 -08003529out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003530 if (err)
3531 *bad_wr = ib_wr;
3532 return err;
3533}
3534
3535
3536/**
3537 * nes_post_recv
3538 */
3539static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3540 struct ib_recv_wr **bad_wr)
3541{
3542 u64 u64temp;
3543 unsigned long flags = 0;
3544 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3545 struct nes_device *nesdev = nesvnic->nesdev;
3546 struct nes_qp *nesqp = to_nesqp(ibqp);
3547 struct nes_hw_qp_wqe *wqe;
3548 int err = 0;
3549 int sge_index;
3550 u32 qsize = nesqp->hwqp.rq_size;
3551 u32 head;
3552 u32 wqe_count = 0;
3553 u32 counter;
3554 u32 total_payload_length;
3555
Frank Zago4293fdc12009-12-09 13:51:36 -08003556 if (nesqp->ibqp_state > IB_QPS_RTS) {
3557 err = -EINVAL;
3558 goto out;
3559 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003560
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003561 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003562
3563 head = nesqp->hwqp.rq_head;
3564
3565 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003566 /* Check for QP error */
3567 if (nesqp->term_flags) {
3568 err = -EINVAL;
3569 break;
3570 }
3571
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003572 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3573 err = -EINVAL;
3574 break;
3575 }
3576 /* Check for RQ overflow */
3577 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003578 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003579 break;
3580 }
3581
3582 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3583 wqe = &nesqp->hwqp.rq_vbase[head];
3584
3585 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3586 nesqp->hwqp.qp_id, wqe, head); */
3587 nes_fill_init_qp_wqe(wqe, nesqp, head);
3588 u64temp = (u64)(ib_wr->wr_id);
3589 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3590 u64temp);
3591 total_payload_length = 0;
3592 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3593 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3594 ib_wr->sg_list[sge_index].addr);
3595 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3596 ib_wr->sg_list[sge_index].length);
3597 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3598 ib_wr->sg_list[sge_index].lkey);
3599
3600 total_payload_length += ib_wr->sg_list[sge_index].length;
3601 }
3602 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3603 total_payload_length);
3604
3605 ib_wr = ib_wr->next;
3606 head++;
3607 wqe_count++;
3608 if (head >= qsize)
3609 head = 0;
3610 }
3611
3612 nesqp->hwqp.rq_head = head;
3613 barrier();
3614 while (wqe_count) {
3615 counter = min(wqe_count, ((u32)255));
3616 wqe_count -= counter;
3617 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3618 }
3619
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003620 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003621
Frank Zago4293fdc12009-12-09 13:51:36 -08003622out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003623 if (err)
3624 *bad_wr = ib_wr;
3625 return err;
3626}
3627
3628
3629/**
3630 * nes_poll_cq
3631 */
3632static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3633{
3634 u64 u64temp;
3635 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003636 unsigned long flags = 0;
3637 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3638 struct nes_device *nesdev = nesvnic->nesdev;
3639 struct nes_cq *nescq = to_nescq(ibcq);
3640 struct nes_qp *nesqp;
3641 struct nes_hw_cqe cqe;
3642 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003643 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003644 u32 cq_size;
3645 u32 cqe_count = 0;
3646 u32 wqe_index;
3647 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003648 u32 move_cq_head = 1;
3649 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003650
3651 nes_debug(NES_DBG_CQ, "\n");
3652
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003653 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654
3655 head = nescq->hw_cq.cq_head;
3656 cq_size = nescq->hw_cq.cq_size;
3657
3658 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003659 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3660 NES_CQE_VALID) == 0)
3661 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003662
Don Wood5ee21fe2009-09-05 20:36:37 -07003663 /*
3664 * Make sure we read CQ entry contents *after*
3665 * we've checked the valid bit.
3666 */
3667 rmb();
3668
3669 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003670 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3671 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3672 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3673 /* parse CQE, get completion context from WQE (either rq or sq) */
3674 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3675 ((u64)u32temp);
3676
3677 if (u64temp) {
3678 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003679 memset(entry, 0, sizeof *entry);
3680 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3681 entry->status = IB_WC_SUCCESS;
3682 } else {
Don Wood320cdfd2009-09-05 20:36:39 -07003683 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3684 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3685 entry->status = err_code & 0x0000ffff;
3686
3687 /* The rest of the cqe's will be marked as flushed */
3688 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3689 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3690 NES_IWARP_CQE_MINOR_FLUSH);
3691 } else
3692 entry->status = IB_WC_WR_FLUSH_ERR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003693 }
3694
3695 entry->qp = &nesqp->ibqp;
3696 entry->src_qp = nesqp->hwqp.qp_id;
3697
3698 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3699 if (nesqp->skip_lsmm) {
3700 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003701 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003702 }
3703
3704 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003705 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003706 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003707 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003708 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003709 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003710 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3711
Don Wood6eed5e72009-09-05 20:36:39 -07003712 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003713 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3714 case NES_IWARP_SQ_OP_RDMAW:
3715 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3716 entry->opcode = IB_WC_RDMA_WRITE;
3717 break;
3718 case NES_IWARP_SQ_OP_RDMAR:
3719 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3720 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003721 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003722 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3723 break;
3724 case NES_IWARP_SQ_OP_SENDINV:
3725 case NES_IWARP_SQ_OP_SENDSEINV:
3726 case NES_IWARP_SQ_OP_SEND:
3727 case NES_IWARP_SQ_OP_SENDSE:
3728 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3729 entry->opcode = IB_WC_SEND;
3730 break;
Chien Tung649fe4a2009-12-09 13:51:37 -08003731 case NES_IWARP_SQ_OP_LOCINV:
Tatyana Nikolova4a4b03f2012-01-06 18:17:19 -06003732 entry->opcode = IB_WC_LOCAL_INV;
Chien Tung649fe4a2009-12-09 13:51:37 -08003733 break;
Chien Tunge293a262009-12-09 15:21:54 -08003734 case NES_IWARP_SQ_OP_FAST_REG:
3735 entry->opcode = IB_WC_FAST_REG_MR;
3736 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003737 }
Don Wood6eed5e72009-09-05 20:36:39 -07003738
3739 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3740 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3741 move_cq_head = 0;
3742 wq_tail = nesqp->hwqp.sq_tail;
3743 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003744 } else {
3745 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003746 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003747 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3748 ((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 -08003749 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003750
3751 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3752 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3753 move_cq_head = 0;
3754 wq_tail = nesqp->hwqp.rq_tail;
3755 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003756 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003757
Don Wood5ee21fe2009-09-05 20:36:37 -07003758 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003759 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003760 cqe_count++;
3761 }
3762
Don Wood6eed5e72009-09-05 20:36:39 -07003763 if (move_cq_head) {
3764 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3765 if (++head >= cq_size)
3766 head = 0;
3767 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003768
Don Wood6eed5e72009-09-05 20:36:39 -07003769 if ((nescq->polled_completions > (cq_size / 2)) ||
3770 (nescq->polled_completions == 255)) {
3771 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003772 " are pending %u of %u.\n",
3773 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003774 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003775 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003776 nescq->polled_completions = 0;
3777 }
3778 } else {
3779 /* Update the wqe index and set status to flush */
3780 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3781 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3782 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3783 cpu_to_le32(wqe_index);
3784 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003785 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003786 }
3787
3788 if (nescq->polled_completions) {
3789 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3790 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3791 nescq->polled_completions = 0;
3792 }
3793
3794 nescq->hw_cq.cq_head = head;
3795 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3796 cqe_count, nescq->hw_cq.cq_number);
3797
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003798 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003799
3800 return cqe_count;
3801}
3802
3803
3804/**
3805 * nes_req_notify_cq
3806 */
3807static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3808 {
3809 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3810 struct nes_device *nesdev = nesvnic->nesdev;
3811 struct nes_cq *nescq = to_nescq(ibcq);
3812 u32 cq_arm;
3813
3814 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3815 nescq->hw_cq.cq_number);
3816
3817 cq_arm = nescq->hw_cq.cq_number;
3818 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3819 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3820 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3821 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3822 else
3823 return -EINVAL;
3824
3825 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3826 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3827
3828 return 0;
3829}
3830
3831
3832/**
3833 * nes_init_ofa_device
3834 */
3835struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3836{
3837 struct nes_ib_device *nesibdev;
3838 struct nes_vnic *nesvnic = netdev_priv(netdev);
3839 struct nes_device *nesdev = nesvnic->nesdev;
3840
3841 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3842 if (nesibdev == NULL) {
3843 return NULL;
3844 }
3845 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3846 nesibdev->ibdev.owner = THIS_MODULE;
3847
3848 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3849 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3850 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3851
3852 nesibdev->ibdev.uverbs_cmd_mask =
3853 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3854 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3855 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3856 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3857 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3858 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3859 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3860 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3861 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3862 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3863 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3864 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3865 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3866 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3867 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3868 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3869 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3870 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3871 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3872 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3873 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3874 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3875
3876 nesibdev->ibdev.phys_port_cnt = 1;
3877 nesibdev->ibdev.num_comp_vectors = 1;
3878 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003879 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003880 nesibdev->ibdev.query_device = nes_query_device;
3881 nesibdev->ibdev.query_port = nes_query_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003882 nesibdev->ibdev.query_pkey = nes_query_pkey;
3883 nesibdev->ibdev.query_gid = nes_query_gid;
3884 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3885 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3886 nesibdev->ibdev.mmap = nes_mmap;
3887 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3888 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3889 nesibdev->ibdev.create_ah = nes_create_ah;
3890 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3891 nesibdev->ibdev.create_qp = nes_create_qp;
3892 nesibdev->ibdev.modify_qp = nes_modify_qp;
3893 nesibdev->ibdev.query_qp = nes_query_qp;
3894 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3895 nesibdev->ibdev.create_cq = nes_create_cq;
3896 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3897 nesibdev->ibdev.poll_cq = nes_poll_cq;
3898 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3899 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3900 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3901 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3902 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3903 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3904 nesibdev->ibdev.bind_mw = nes_bind_mw;
3905
Chien Tunge293a262009-12-09 15:21:54 -08003906 nesibdev->ibdev.alloc_fast_reg_mr = nes_alloc_fast_reg_mr;
3907 nesibdev->ibdev.alloc_fast_reg_page_list = nes_alloc_fast_reg_page_list;
3908 nesibdev->ibdev.free_fast_reg_page_list = nes_free_fast_reg_page_list;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003909
3910 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3911 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3912 nesibdev->ibdev.process_mad = nes_process_mad;
3913
3914 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3915 nesibdev->ibdev.post_send = nes_post_send;
3916 nesibdev->ibdev.post_recv = nes_post_recv;
3917
3918 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3919 if (nesibdev->ibdev.iwcm == NULL) {
3920 ib_dealloc_device(&nesibdev->ibdev);
3921 return NULL;
3922 }
3923 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3924 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3925 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3926 nesibdev->ibdev.iwcm->connect = nes_connect;
3927 nesibdev->ibdev.iwcm->accept = nes_accept;
3928 nesibdev->ibdev.iwcm->reject = nes_reject;
3929 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3930 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3931
3932 return nesibdev;
3933}
3934
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003935
3936/**
3937 * nes_handle_delayed_event
3938 */
3939static void nes_handle_delayed_event(unsigned long data)
3940{
3941 struct nes_vnic *nesvnic = (void *) data;
3942
3943 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3944 struct ib_event event;
3945
3946 event.device = &nesvnic->nesibdev->ibdev;
3947 if (!event.device)
3948 goto stop_timer;
3949 event.event = nesvnic->delayed_event;
3950 event.element.port_num = nesvnic->logical_port + 1;
3951 ib_dispatch_event(&event);
3952 }
3953
3954stop_timer:
3955 nesvnic->event_timer.function = NULL;
3956}
3957
3958
Faisal Latifcd6860e2010-07-04 00:17:59 +00003959void nes_port_ibevent(struct nes_vnic *nesvnic)
3960{
3961 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3962 struct nes_device *nesdev = nesvnic->nesdev;
3963 struct ib_event event;
3964 event.device = &nesibdev->ibdev;
3965 event.element.port_num = nesvnic->logical_port + 1;
3966 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003967
3968 if (!nesvnic->event_timer.function) {
3969 ib_dispatch_event(&event);
3970 nesvnic->last_dispatched_event = event.event;
3971 nesvnic->event_timer.function = nes_handle_delayed_event;
3972 nesvnic->event_timer.data = (unsigned long) nesvnic;
3973 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3974 add_timer(&nesvnic->event_timer);
3975 } else {
3976 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3977 }
3978 nesvnic->delayed_event = event.event;
Faisal Latifcd6860e2010-07-04 00:17:59 +00003979}
3980
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003981
3982/**
3983 * nes_destroy_ofa_device
3984 */
3985void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3986{
3987 if (nesibdev == NULL)
3988 return;
3989
3990 nes_unregister_ofa_device(nesibdev);
3991
3992 kfree(nesibdev->ibdev.iwcm);
3993 ib_dealloc_device(&nesibdev->ibdev);
3994}
3995
3996
3997/**
3998 * nes_register_ofa_device
3999 */
4000int nes_register_ofa_device(struct nes_ib_device *nesibdev)
4001{
4002 struct nes_vnic *nesvnic = nesibdev->nesvnic;
4003 struct nes_device *nesdev = nesvnic->nesdev;
4004 struct nes_adapter *nesadapter = nesdev->nesadapter;
4005 int i, ret;
4006
Ralph Campbell9a6edb62010-05-06 17:03:25 -07004007 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004008 if (ret) {
4009 return ret;
4010 }
4011
4012 /* Get the resources allocated to this device */
4013 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
4014 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
4015 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
4016 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
4017
Tony Jonesf4e91eb2008-02-22 00:13:36 +01004018 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4019 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004020 if (ret) {
4021 while (i > 0) {
4022 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01004023 device_remove_file(&nesibdev->ibdev.dev,
4024 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004025 }
4026 ib_unregister_device(&nesibdev->ibdev);
4027 return ret;
4028 }
4029 }
4030
4031 nesvnic->of_device_registered = 1;
4032
4033 return 0;
4034}
4035
4036
4037/**
4038 * nes_unregister_ofa_device
4039 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07004040static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004041{
4042 struct nes_vnic *nesvnic = nesibdev->nesvnic;
4043 int i;
4044
Tony Jonesf4e91eb2008-02-22 00:13:36 +01004045 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4046 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004047 }
4048
4049 if (nesvnic->of_device_registered) {
4050 ib_unregister_device(&nesibdev->ibdev);
4051 }
4052
4053 nesvnic->of_device_registered = 0;
4054}