blob: 25dcd7573df9c1b24640c20b4ad3c4ec0ecb2a98 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Tatyana Nikolovac5488c52011-11-28 14:22:29 -06002 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/random.h>
37#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080039#include <asm/byteorder.h>
40
41#include <rdma/ib_verbs.h>
42#include <rdma/iw_cm.h>
43#include <rdma/ib_user_verbs.h>
44
45#include "nes.h"
46
47#include <rdma/ib_umem.h>
48
49atomic_t mod_qp_timouts;
50atomic_t qps_created;
51atomic_t sw_qps_destroyed;
52
Roland Dreier1a855fbf2008-04-16 21:01:09 -070053static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +030054static int nes_dereg_mr(struct ib_mr *ib_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080055
56/**
57 * nes_alloc_mw
58 */
Matan Barakb2a239d2016-02-29 18:05:29 +020059static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type,
60 struct ib_udata *udata)
Shani Michaeli7083e422013-02-06 16:19:12 +000061{
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062 struct nes_pd *nespd = to_nespd(ibpd);
63 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
64 struct nes_device *nesdev = nesvnic->nesdev;
65 struct nes_adapter *nesadapter = nesdev->nesadapter;
66 struct nes_cqp_request *cqp_request;
67 struct nes_mr *nesmr;
68 struct ib_mw *ibmw;
69 struct nes_hw_cqp_wqe *cqp_wqe;
70 int ret;
71 u32 stag;
72 u32 stag_index = 0;
73 u32 next_stag_index = 0;
74 u32 driver_key = 0;
75 u8 stag_key = 0;
76
Shani Michaeli7083e422013-02-06 16:19:12 +000077 if (type != IB_MW_TYPE_1)
78 return ERR_PTR(-EINVAL);
79
Glenn Streiff3c2d7742008-02-04 20:20:45 -080080 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
81 stag_key = (u8)next_stag_index;
82
83 driver_key = 0;
84
85 next_stag_index >>= 8;
86 next_stag_index %= nesadapter->max_mr;
87
88 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +000089 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080090 if (ret) {
91 return ERR_PTR(ret);
92 }
93
94 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
95 if (!nesmr) {
96 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
97 return ERR_PTR(-ENOMEM);
98 }
99
100 stag = stag_index << 8;
101 stag |= driver_key;
102 stag += (u32)stag_key;
103
104 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
105 stag, stag_index);
106
107 /* Register the region with the adapter */
108 cqp_request = nes_get_cqp_request(nesdev);
109 if (cqp_request == NULL) {
110 kfree(nesmr);
111 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
112 return ERR_PTR(-ENOMEM);
113 }
114
115 cqp_request->waiting = 1;
116 cqp_wqe = &cqp_request->cqp_wqe;
117
118 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
119 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
120 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
121 NES_CQP_STAG_REM_ACC_EN);
122
123 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
124 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
125 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
126
127 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700128 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800129
130 /* Wait for CQP */
131 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
132 NES_EVENT_TIMEOUT);
133 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
134 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
135 stag, ret, cqp_request->major_code, cqp_request->minor_code);
136 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -0700137 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800138 kfree(nesmr);
139 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
140 if (!ret) {
141 return ERR_PTR(-ETIME);
142 } else {
143 return ERR_PTR(-ENOMEM);
144 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800145 }
Roland Dreier1ff66e82008-07-14 23:48:49 -0700146 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800147
148 nesmr->ibmw.rkey = stag;
149 nesmr->mode = IWNES_MEMREG_TYPE_MW;
150 ibmw = &nesmr->ibmw;
151 nesmr->pbl_4k = 0;
152 nesmr->pbls_used = 0;
153
154 return ibmw;
155}
156
157
158/**
159 * nes_dealloc_mw
160 */
161static int nes_dealloc_mw(struct ib_mw *ibmw)
162{
163 struct nes_mr *nesmr = to_nesmw(ibmw);
164 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
165 struct nes_device *nesdev = nesvnic->nesdev;
166 struct nes_adapter *nesadapter = nesdev->nesadapter;
167 struct nes_hw_cqp_wqe *cqp_wqe;
168 struct nes_cqp_request *cqp_request;
169 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800170 int ret;
171
172 /* Deallocate the window with the adapter */
173 cqp_request = nes_get_cqp_request(nesdev);
174 if (cqp_request == NULL) {
175 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
176 return -ENOMEM;
177 }
178 cqp_request->waiting = 1;
179 cqp_wqe = &cqp_request->cqp_wqe;
180 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
181 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
182 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
183
184 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700185 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800186
187 /* Wait for CQP */
188 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
189 ibmw->rkey);
190 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
191 NES_EVENT_TIMEOUT);
192 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
193 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
194 ret, cqp_request->major_code, cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -0700195 if (!ret)
196 err = -ETIME;
197 else if (cqp_request->major_code)
198 err = -EIO;
199
200 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201
202 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
203 (ibmw->rkey & 0x0fffff00) >> 8);
204 kfree(nesmr);
205
206 return err;
207}
208
209
Chien Tunge293a262009-12-09 15:21:54 -0800210/*
211 * nes_alloc_fast_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800212 */
Chien Tunge293a262009-12-09 15:21:54 -0800213static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
214 u32 stag, u32 page_count)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800216 struct nes_hw_cqp_wqe *cqp_wqe;
Chien Tunge293a262009-12-09 15:21:54 -0800217 struct nes_cqp_request *cqp_request;
218 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800219 int ret;
Chien Tunge293a262009-12-09 15:21:54 -0800220 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800221 u32 opcode = 0;
Chien Tunge293a262009-12-09 15:21:54 -0800222 u16 major_code;
223 u64 region_length = page_count * PAGE_SIZE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800224
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800225
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800226 cqp_request = nes_get_cqp_request(nesdev);
227 if (cqp_request == NULL) {
228 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
Chien Tunge293a262009-12-09 15:21:54 -0800229 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800230 }
Chien Tunge293a262009-12-09 15:21:54 -0800231 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
232 "region_length = %llu\n",
233 page_count, region_length);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800234 cqp_request->waiting = 1;
235 cqp_wqe = &cqp_request->cqp_wqe;
236
Chien Tunge293a262009-12-09 15:21:54 -0800237 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
238 if (nesadapter->free_4kpbl > 0) {
239 nesadapter->free_4kpbl--;
240 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
241 } else {
242 /* No 4kpbl's available: */
243 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
244 nes_debug(NES_DBG_MR, "Out of Pbls\n");
245 nes_free_cqp_request(nesdev, cqp_request);
246 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800247 }
248
Chien Tunge293a262009-12-09 15:21:54 -0800249 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
250 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
251 NES_CQP_STAG_REM_ACC_EN;
252 /*
253 * The current OFED API does not support the zero based TO option.
254 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
255 * the API does not support that ability to have the MR set for local
256 * access only when created and not allow the SQ op to override. Given
257 * this the remote enable must be set here.
258 */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800259
260 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
261 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
Chien Tunge293a262009-12-09 15:21:54 -0800262 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800263
Chien Tunge293a262009-12-09 15:21:54 -0800264 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
265 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
266 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
267 cpu_to_le32(nespd->pd_id & 0x00007fff);
268
269 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
270 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
271 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
272 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
273 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
274 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
275 barrier();
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800276
277 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700278 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800279
280 /* Wait for CQP */
Chien Tunge293a262009-12-09 15:21:54 -0800281 ret = wait_event_timeout(cqp_request->waitq,
282 (0 != cqp_request->request_done),
283 NES_EVENT_TIMEOUT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800284
Chien Tunge293a262009-12-09 15:21:54 -0800285 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
286 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
287 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
288 cqp_request->minor_code);
289 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -0700290 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800291
Chien Tunge293a262009-12-09 15:21:54 -0800292 if (!ret || major_code) {
Chien Tung35c6d692008-11-02 21:37:35 -0800293 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Chien Tunge293a262009-12-09 15:21:54 -0800294 nesadapter->free_4kpbl++;
Chien Tung35c6d692008-11-02 21:37:35 -0800295 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
296 }
297
Chien Tunge293a262009-12-09 15:21:54 -0800298 if (!ret)
299 return -ETIME;
300 else if (major_code)
301 return -EIO;
302 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800303}
304
Chien Tunge293a262009-12-09 15:21:54 -0800305/*
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300306 * nes_alloc_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800307 */
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300308static struct ib_mr *nes_alloc_mr(struct ib_pd *ibpd,
309 enum ib_mr_type mr_type,
310 u32 max_num_sg)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800311{
Chien Tunge293a262009-12-09 15:21:54 -0800312 struct nes_pd *nespd = to_nespd(ibpd);
313 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800314 struct nes_device *nesdev = nesvnic->nesdev;
Chien Tung35c6d692008-11-02 21:37:35 -0800315 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800316
Chien Tunge293a262009-12-09 15:21:54 -0800317 u32 next_stag_index;
318 u8 stag_key = 0;
319 u32 driver_key = 0;
320 int err = 0;
321 u32 stag_index = 0;
322 struct nes_mr *nesmr;
323 u32 stag;
324 int ret;
325 struct ib_mr *ibmr;
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300326
327 if (mr_type != IB_MR_TYPE_MEM_REG)
328 return ERR_PTR(-EINVAL);
329
330 if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
331 return ERR_PTR(-E2BIG);
332
Chien Tunge293a262009-12-09 15:21:54 -0800333/*
334 * Note: Set to always use a fixed length single page entry PBL. This is to allow
335 * for the fast_reg_mr operation to always know the size of the PBL.
336 */
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300337 if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
Chien Tunge293a262009-12-09 15:21:54 -0800338 return ERR_PTR(-E2BIG);
339
340 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
341 stag_key = (u8)next_stag_index;
342 next_stag_index >>= 8;
343 next_stag_index %= nesadapter->max_mr;
344
345 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
346 nesadapter->max_mr, &stag_index,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000347 &next_stag_index, NES_RESOURCE_FAST_MR);
Chien Tunge293a262009-12-09 15:21:54 -0800348 if (err)
349 return ERR_PTR(err);
350
351 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
352 if (!nesmr) {
353 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
354 return ERR_PTR(-ENOMEM);
355 }
356
357 stag = stag_index << 8;
358 stag |= driver_key;
359 stag += (u32)stag_key;
360
361 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
362 stag, stag_index);
363
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300364 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_num_sg);
Chien Tunge293a262009-12-09 15:21:54 -0800365
366 if (ret == 0) {
367 nesmr->ibmr.rkey = stag;
368 nesmr->ibmr.lkey = stag;
369 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
370 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800371 } else {
Chien Tunge293a262009-12-09 15:21:54 -0800372 kfree(nesmr);
373 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300374 return ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800375 }
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300376
377 nesmr->pages = pci_alloc_consistent(nesdev->pcidev,
378 max_num_sg * sizeof(u64),
379 &nesmr->paddr);
380 if (!nesmr->paddr)
381 goto err;
382
383 nesmr->max_pages = max_num_sg;
384
Chien Tunge293a262009-12-09 15:21:54 -0800385 return ibmr;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300386
387err:
388 nes_dereg_mr(ibmr);
389
390 return ERR_PTR(-ENOMEM);
391}
392
393static int nes_set_page(struct ib_mr *ibmr, u64 addr)
394{
395 struct nes_mr *nesmr = to_nesmr(ibmr);
396
397 if (unlikely(nesmr->npages == nesmr->max_pages))
398 return -ENOMEM;
399
400 nesmr->pages[nesmr->npages++] = cpu_to_le64(addr);
401
402 return 0;
403}
404
Christoph Hellwigff2ba992016-05-03 18:01:04 +0200405static int nes_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
Bart Van Assche9aa8b322016-05-12 10:49:15 -0700406 int sg_nents, unsigned int *sg_offset)
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300407{
408 struct nes_mr *nesmr = to_nesmr(ibmr);
409
410 nesmr->npages = 0;
411
Christoph Hellwigff2ba992016-05-03 18:01:04 +0200412 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, nes_set_page);
Chien Tunge293a262009-12-09 15:21:54 -0800413}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800414
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415/**
416 * nes_query_device
417 */
Matan Barak2528e332015-06-11 16:35:25 +0300418static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
419 struct ib_udata *uhw)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800420{
421 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
422 struct nes_device *nesdev = nesvnic->nesdev;
423 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
424
Matan Barak2528e332015-06-11 16:35:25 +0300425 if (uhw->inlen || uhw->outlen)
426 return -EINVAL;
427
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800428 memset(props, 0, sizeof(*props));
429 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
430
Miroslaw Walukiewiczff0380c2010-07-15 14:53:29 +0000431 props->fw_ver = nesdev->nesadapter->firmware_version;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800432 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
433 props->vendor_id = nesdev->nesadapter->vendor_id;
434 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
435 props->hw_ver = nesdev->nesadapter->hw_rev;
436 props->max_mr_size = 0x80000000;
437 props->max_qp = nesibdev->max_qp;
438 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
439 props->max_sge = nesdev->nesadapter->max_sge;
440 props->max_cq = nesibdev->max_cq;
Chien Tung5924aea2009-12-09 15:21:56 -0800441 props->max_cqe = nesdev->nesadapter->max_cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800442 props->max_mr = nesibdev->max_mr;
443 props->max_mw = nesibdev->max_mr;
444 props->max_pd = nesibdev->max_pd;
445 props->max_sge_rd = 1;
446 switch (nesdev->nesadapter->max_irrq_wr) {
447 case 0:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800448 props->max_qp_rd_atom = 2;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800449 break;
450 case 1:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800451 props->max_qp_rd_atom = 8;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800452 break;
453 case 2:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800454 props->max_qp_rd_atom = 32;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800455 break;
456 case 3:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800457 props->max_qp_rd_atom = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800458 break;
459 default:
460 props->max_qp_rd_atom = 0;
461 }
Faisal Latif66388d62009-06-22 22:52:30 -0700462 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800463 props->atomic_cap = IB_ATOMIC_NONE;
464 props->max_map_per_fmr = 1;
465
466 return 0;
467}
468
469
470/**
471 * nes_query_port
472 */
473static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
474{
Chien Tungcd1d3f72009-09-05 20:36:39 -0700475 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
476 struct net_device *netdev = nesvnic->netdev;
477
Or Gerlitzc4550c62017-01-24 13:02:39 +0200478 /* props being zeroed by the caller, avoid zeroing it here */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479
Chien Tungcd1d3f72009-09-05 20:36:39 -0700480 props->max_mtu = IB_MTU_4096;
Amrani, Ramd3f4aad2016-12-26 08:40:57 +0200481 props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
Chien Tungcd1d3f72009-09-05 20:36:39 -0700482
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800483 props->lid = 1;
484 props->lmc = 0;
485 props->sm_lid = 0;
486 props->sm_sl = 0;
Chien Tung29327722010-08-23 14:32:36 +0000487 if (netif_queue_stopped(netdev))
488 props->state = IB_PORT_DOWN;
489 else if (nesvnic->linkup)
Chien Tungcd1d3f72009-09-05 20:36:39 -0700490 props->state = IB_PORT_ACTIVE;
491 else
492 props->state = IB_PORT_DOWN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800493 props->phys_state = 0;
494 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
495 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
496 props->gid_tbl_len = 1;
497 props->pkey_tbl_len = 1;
498 props->qkey_viol_cntr = 0;
499 props->active_width = IB_WIDTH_4X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200500 props->active_speed = IB_SPEED_SDR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800501 props->max_msg_sz = 0x80000000;
502
503 return 0;
504}
505
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800507 * nes_query_pkey
508 */
509static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
510{
511 *pkey = 0;
512 return 0;
513}
514
515
516/**
517 * nes_query_gid
518 */
519static int nes_query_gid(struct ib_device *ibdev, u8 port,
520 int index, union ib_gid *gid)
521{
522 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
523
524 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
525 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
526
527 return 0;
528}
529
530
531/**
532 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
533 * of all objects associated with a particular user-mode client.
534 */
535static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
536 struct ib_udata *udata)
537{
538 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
539 struct nes_device *nesdev = nesvnic->nesdev;
540 struct nes_adapter *nesadapter = nesdev->nesadapter;
541 struct nes_alloc_ucontext_req req;
542 struct nes_alloc_ucontext_resp uresp;
543 struct nes_ucontext *nes_ucontext;
544 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
545
546
547 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
548 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
549 return ERR_PTR(-EINVAL);
550 }
551
552 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
553 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
554 req.userspace_ver, NES_ABI_USERSPACE_VER);
555 return ERR_PTR(-EINVAL);
556 }
557
558
559 memset(&uresp, 0, sizeof uresp);
560
561 uresp.max_qps = nesibdev->max_qp;
562 uresp.max_pds = nesibdev->max_pd;
563 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
564 uresp.virtwq = nesadapter->virtwq;
565 uresp.kernel_ver = NES_ABI_KERNEL_VER;
566
567 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
568 if (!nes_ucontext)
569 return ERR_PTR(-ENOMEM);
570
571 nes_ucontext->nesdev = nesdev;
572 nes_ucontext->mmap_wq_offset = uresp.max_pds;
573 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
574 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
575 PAGE_SIZE;
576
577
578 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
579 kfree(nes_ucontext);
580 return ERR_PTR(-EFAULT);
581 }
582
583 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
584 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
585 atomic_set(&nes_ucontext->usecnt, 1);
586 return &nes_ucontext->ibucontext;
587}
588
589
590/**
591 * nes_dealloc_ucontext
592 */
593static int nes_dealloc_ucontext(struct ib_ucontext *context)
594{
595 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
596 /* struct nes_device *nesdev = nesvnic->nesdev; */
597 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
598
599 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
600 return 0;
601 kfree(nes_ucontext);
602 return 0;
603}
604
605
606/**
607 * nes_mmap
608 */
609static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
610{
611 unsigned long index;
612 struct nes_vnic *nesvnic = to_nesvnic(context->device);
613 struct nes_device *nesdev = nesvnic->nesdev;
614 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
615 struct nes_ucontext *nes_ucontext;
616 struct nes_qp *nesqp;
617
618 nes_ucontext = to_nesucontext(context);
619
620
621 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
622 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
623 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
624 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
625 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
626 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
627 return -EFAULT;
628 }
629 nesqp = nes_ucontext->mmap_nesqp[index];
630 if (nesqp == NULL) {
631 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
632 return -EFAULT;
633 }
634 if (remap_pfn_range(vma, vma->vm_start,
635 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
636 vma->vm_end - vma->vm_start,
637 vma->vm_page_prot)) {
638 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
639 return -EAGAIN;
640 }
641 vma->vm_private_data = nesqp;
642 return 0;
643 } else {
644 index = vma->vm_pgoff;
645 if (!test_bit(index, nes_ucontext->allocated_doorbells))
646 return -EFAULT;
647
648 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
649 if (io_remap_pfn_range(vma, vma->vm_start,
650 (nesdev->doorbell_start +
651 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
652 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
653 return -EAGAIN;
654 vma->vm_private_data = nes_ucontext;
655 return 0;
656 }
657
658 return -ENOSYS;
659}
660
661
662/**
663 * nes_alloc_pd
664 */
665static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
666 struct ib_ucontext *context, struct ib_udata *udata)
667{
668 struct nes_pd *nespd;
669 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
670 struct nes_device *nesdev = nesvnic->nesdev;
671 struct nes_adapter *nesadapter = nesdev->nesadapter;
672 struct nes_ucontext *nesucontext;
673 struct nes_alloc_pd_resp uresp;
674 u32 pd_num = 0;
675 int err;
676
677 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
678 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
Eric Dumazet29b44332010-10-11 10:22:12 +0000679 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800680
681 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000682 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800683 if (err) {
684 return ERR_PTR(err);
685 }
686
687 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
688 if (!nespd) {
689 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
690 return ERR_PTR(-ENOMEM);
691 }
692
693 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
694 nespd, nesvnic->nesibdev->ibdev.name);
695
696 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
697
698 if (context) {
699 nesucontext = to_nesucontext(context);
700 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
701 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
702 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
703 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600704 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
706 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
707 kfree(nespd);
708 return ERR_PTR(-ENOMEM);
709 }
710
711 uresp.pd_id = nespd->pd_id;
712 uresp.mmap_db_index = nespd->mmap_db_index;
713 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
714 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
715 kfree(nespd);
716 return ERR_PTR(-EFAULT);
717 }
718
719 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
720 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
721 nesucontext->first_free_db = nespd->mmap_db_index + 1;
722 }
723
724 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
725 return &nespd->ibpd;
726}
727
728
729/**
730 * nes_dealloc_pd
731 */
732static int nes_dealloc_pd(struct ib_pd *ibpd)
733{
734 struct nes_ucontext *nesucontext;
735 struct nes_pd *nespd = to_nespd(ibpd);
736 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
737 struct nes_device *nesdev = nesvnic->nesdev;
738 struct nes_adapter *nesadapter = nesdev->nesadapter;
739
740 if ((ibpd->uobject) && (ibpd->uobject->context)) {
741 nesucontext = to_nesucontext(ibpd->uobject->context);
742 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
743 nespd->mmap_db_index);
744 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
745 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
746 if (nesucontext->first_free_db > nespd->mmap_db_index) {
747 nesucontext->first_free_db = nespd->mmap_db_index;
748 }
749 }
750
751 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
752 nespd->pd_id, nespd);
753 nes_free_resource(nesadapter, nesadapter->allocated_pds,
754 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
755 kfree(nespd);
756
757 return 0;
758}
759
760
761/**
762 * nes_create_ah
763 */
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400764static struct ib_ah *nes_create_ah(struct ib_pd *pd,
765 struct rdma_ah_attr *ah_attr,
Moni Shoua477864c2016-11-23 08:23:24 +0200766 struct ib_udata *udata)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800767{
768 return ERR_PTR(-ENOSYS);
769}
770
771
772/**
773 * nes_destroy_ah
774 */
775static int nes_destroy_ah(struct ib_ah *ah)
776{
777 return -ENOSYS;
778}
779
780
781/**
782 * nes_get_encoded_size
783 */
784static inline u8 nes_get_encoded_size(int *size)
785{
786 u8 encoded_size = 0;
787 if (*size <= 32) {
788 *size = 32;
789 encoded_size = 1;
790 } else if (*size <= 128) {
791 *size = 128;
792 encoded_size = 2;
793 } else if (*size <= 512) {
794 *size = 512;
795 encoded_size = 3;
796 }
797 return (encoded_size);
798}
799
800
801
802/**
803 * nes_setup_virt_qp
804 */
805static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
806 struct nes_vnic *nesvnic, int sq_size, int rq_size)
807{
808 unsigned long flags;
809 void *mem;
810 __le64 *pbl = NULL;
811 __le64 *tpbl;
812 __le64 *pblbuffer;
813 struct nes_device *nesdev = nesvnic->nesdev;
814 struct nes_adapter *nesadapter = nesdev->nesadapter;
815 u32 pbl_entries;
816 u8 rq_pbl_entries;
817 u8 sq_pbl_entries;
818
819 pbl_entries = nespbl->pbl_size >> 3;
Roland Dreier71e09572008-04-16 21:01:09 -0700820 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 -0800821 nespbl->pbl_size, pbl_entries,
822 (void *)nespbl->pbl_vbase,
Roland Dreier71e09572008-04-16 21:01:09 -0700823 (unsigned long) nespbl->pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800824 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
825 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
826 /* the first pbl to be fro the rq_vbase... */
827 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
828 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
829 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
830 if (!nespbl->page) {
831 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
832 kfree(nespbl);
833 return -ENOMEM;
834 }
835
836 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
837 nesqp->page = nespbl->page;
838 if (!nesqp->hwqp.sq_vbase) {
839 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
840 kfree(nespbl);
841 return -ENOMEM;
842 }
843
844 /* Now to get to sq.. we need to calculate how many */
845 /* PBL entries were used by the rq.. */
846 pbl += sq_pbl_entries;
847 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
848 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
849 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
850
Roland Dreier71e09572008-04-16 21:01:09 -0700851 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
852 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
853 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800854 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
855 if (!nesadapter->free_256pbl) {
856 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
857 nespbl->pbl_pbase);
858 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
859 kunmap(nesqp->page);
860 kfree(nespbl);
861 return -ENOMEM;
862 }
863 nesadapter->free_256pbl--;
864 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
865
866 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
867 pblbuffer = nesqp->pbl_vbase;
868 if (!nesqp->pbl_vbase) {
869 /* memory allocated during nes_reg_user_mr() */
870 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
871 nespbl->pbl_pbase);
872 kfree(nespbl);
873 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
874 nesadapter->free_256pbl++;
875 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
876 kunmap(nesqp->page);
877 return -ENOMEM;
878 }
879 memset(nesqp->pbl_vbase, 0, 256);
880 /* fill in the page address in the pbl buffer.. */
881 tpbl = pblbuffer + 16;
882 pbl = (__le64 *)nespbl->pbl_vbase;
883 while (sq_pbl_entries--)
884 *tpbl++ = *pbl++;
885 tpbl = pblbuffer;
886 while (rq_pbl_entries--)
887 *tpbl++ = *pbl++;
888
889 /* done with memory allocated during nes_reg_user_mr() */
890 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
891 nespbl->pbl_pbase);
892 kfree(nespbl);
893
894 nesqp->qp_mem_size =
895 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
896 /* Round up to a multiple of a page */
897 nesqp->qp_mem_size += PAGE_SIZE - 1;
898 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
899
900 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
901 &nesqp->hwqp.q2_pbase);
902
903 if (!mem) {
904 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
905 nesqp->pbl_vbase = NULL;
906 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
907 nesadapter->free_256pbl++;
908 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
909 kunmap(nesqp->page);
910 return -ENOMEM;
911 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -0800912 nesqp->sq_kmapped = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800913 nesqp->hwqp.q2_vbase = mem;
914 mem += 256;
915 memset(nesqp->hwqp.q2_vbase, 0, 256);
916 nesqp->nesqp_context = mem;
917 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
918 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
919
920 return 0;
921}
922
923
924/**
925 * nes_setup_mmap_qp
926 */
927static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
928 int sq_size, int rq_size)
929{
930 void *mem;
931 struct nes_device *nesdev = nesvnic->nesdev;
932
933 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
934 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
935 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
936 256; /* this is Q2 */
937 /* Round up to a multiple of a page */
938 nesqp->qp_mem_size += PAGE_SIZE - 1;
939 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
940
941 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
942 &nesqp->hwqp.sq_pbase);
943 if (!mem)
944 return -ENOMEM;
945 nes_debug(NES_DBG_QP, "PCI consistent memory for "
946 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
947 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
948
949 memset(mem, 0, nesqp->qp_mem_size);
950
951 nesqp->hwqp.sq_vbase = mem;
952 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
953
954 nesqp->hwqp.rq_vbase = mem;
955 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
956 sizeof(struct nes_hw_qp_wqe) * sq_size;
957 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
958
959 nesqp->hwqp.q2_vbase = mem;
960 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
961 sizeof(struct nes_hw_qp_wqe) * rq_size;
962 mem += 256;
963 memset(nesqp->hwqp.q2_vbase, 0, 256);
964
965 nesqp->nesqp_context = mem;
966 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
967 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
968 return 0;
969}
970
971
972/**
973 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
974 */
Denys Vlasenkoda74bf42016-04-08 20:58:42 +0200975static void nes_free_qp_mem(struct nes_device *nesdev,
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800976 struct nes_qp *nesqp, int virt_wqs)
977{
978 unsigned long flags;
979 struct nes_adapter *nesadapter = nesdev->nesadapter;
980 if (!virt_wqs) {
981 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
982 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
983 }else {
984 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
985 nesadapter->free_256pbl++;
986 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
987 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
988 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
989 nesqp->pbl_vbase = NULL;
Faisal Latifd2fa9b262009-12-09 15:54:28 -0800990 if (nesqp->sq_kmapped) {
991 nesqp->sq_kmapped = 0;
992 kunmap(nesqp->page);
993 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800994 }
995}
996
997
998/**
999 * nes_create_qp
1000 */
1001static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1002 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1003{
1004 u64 u64temp= 0;
1005 u64 u64nesqp = 0;
1006 struct nes_pd *nespd = to_nespd(ibpd);
1007 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1008 struct nes_device *nesdev = nesvnic->nesdev;
1009 struct nes_adapter *nesadapter = nesdev->nesadapter;
1010 struct nes_qp *nesqp;
1011 struct nes_cq *nescq;
1012 struct nes_ucontext *nes_ucontext;
1013 struct nes_hw_cqp_wqe *cqp_wqe;
1014 struct nes_cqp_request *cqp_request;
1015 struct nes_create_qp_req req;
1016 struct nes_create_qp_resp uresp;
1017 struct nes_pbl *nespbl = NULL;
1018 u32 qp_num = 0;
1019 u32 opcode = 0;
1020 /* u32 counter = 0; */
1021 void *mem;
1022 unsigned long flags;
1023 int ret;
1024 int err;
1025 int virt_wqs = 0;
1026 int sq_size;
1027 int rq_size;
1028 u8 sq_encoded_size;
1029 u8 rq_encoded_size;
1030 /* int counter; */
1031
Eli Cohenb846f252008-04-16 21:09:27 -07001032 if (init_attr->create_flags)
1033 return ERR_PTR(-EINVAL);
1034
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001035 atomic_inc(&qps_created);
1036 switch (init_attr->qp_type) {
1037 case IB_QPT_RC:
1038 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1039 init_attr->cap.max_inline_data = 0;
1040 } else {
1041 init_attr->cap.max_inline_data = 64;
1042 }
1043 sq_size = init_attr->cap.max_send_wr;
1044 rq_size = init_attr->cap.max_recv_wr;
1045
Glenn Streiff7495ab62008-04-29 13:46:54 -07001046 /* check if the encoded sizes are OK or not... */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047 sq_encoded_size = nes_get_encoded_size(&sq_size);
1048 rq_encoded_size = nes_get_encoded_size(&rq_size);
1049
1050 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1051 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1052 rq_size, sq_size);
1053 return ERR_PTR(-EINVAL);
1054 }
1055
1056 init_attr->cap.max_send_wr = sq_size -2;
1057 init_attr->cap.max_recv_wr = rq_size -1;
1058 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1059
1060 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001061 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001062 if (ret) {
1063 return ERR_PTR(ret);
1064 }
1065
1066 /* Need 512 (actually now 1024) byte alignment on this structure */
1067 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1068 if (!mem) {
1069 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001070 return ERR_PTR(-ENOMEM);
1071 }
1072 u64nesqp = (unsigned long)mem;
1073 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1074 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1075 u64nesqp &= ~u64temp;
1076 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1077 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1078 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1079 nesqp->allocated_buffer = mem;
1080
1081 if (udata) {
1082 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1083 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1084 kfree(nesqp->allocated_buffer);
1085 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
Yann Droneaud9d194d12014-03-10 23:06:27 +01001086 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087 }
1088 if (req.user_wqe_buffers) {
1089 virt_wqs = 1;
1090 }
Tatyana Nikolova43adff32014-03-11 14:20:17 -05001091 if (req.user_qp_buffer)
1092 nesqp->nesuqp_addr = req.user_qp_buffer;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1094 nesqp->user_mode = 1;
1095 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1096 if (virt_wqs) {
1097 err = 1;
1098 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1099 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1100 list_del(&nespbl->list);
1101 err = 0;
1102 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1103 nespbl, nespbl->user_base);
1104 break;
1105 }
1106 }
1107 if (err) {
1108 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1109 (long long unsigned int)req.user_wqe_buffers);
1110 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1111 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001112 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001113 }
1114 }
1115
1116 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1117 nesqp->mmap_sq_db_index =
1118 find_next_zero_bit(nes_ucontext->allocated_wqs,
1119 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1120 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1121 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001122 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001123 nes_debug(NES_DBG_QP,
1124 "db index > max user regions, failing create QP\n");
1125 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1126 if (virt_wqs) {
1127 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1128 nespbl->pbl_pbase);
1129 kfree(nespbl);
1130 }
1131 kfree(nesqp->allocated_buffer);
1132 return ERR_PTR(-ENOMEM);
1133 }
1134 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1135 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1136 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1137 } else {
1138 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1139 kfree(nesqp->allocated_buffer);
1140 return ERR_PTR(-EFAULT);
1141 }
1142 }
1143 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1144 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1145 if (err) {
1146 nes_debug(NES_DBG_QP,
1147 "error geting qp mem code = %d\n", err);
1148 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1149 kfree(nesqp->allocated_buffer);
1150 return ERR_PTR(-ENOMEM);
1151 }
1152
1153 nesqp->hwqp.sq_size = sq_size;
1154 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1155 nesqp->hwqp.sq_head = 1;
1156 nesqp->hwqp.rq_size = rq_size;
1157 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1158 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1159 (void *)nesqp->nesqp_context_pbase);
1160 */
1161 nesqp->hwqp.qp_id = qp_num;
1162 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1163 nesqp->nespd = nespd;
1164
1165 nescq = to_nescq(init_attr->send_cq);
1166 nesqp->nesscq = nescq;
1167 nescq = to_nescq(init_attr->recv_cq);
1168 nesqp->nesrcq = nescq;
1169
1170 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1171 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1172 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1173 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1174 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1175 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001176 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001177 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1178 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001179 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001180 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1181 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1182 u64temp = (u64)nesqp->hwqp.sq_pbase;
1183 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1184 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1185
1186
1187 if (!virt_wqs) {
1188 u64temp = (u64)nesqp->hwqp.sq_pbase;
1189 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1190 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1191 u64temp = (u64)nesqp->hwqp.rq_pbase;
1192 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1193 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1194 } else {
1195 u64temp = (u64)nesqp->pbl_pbase;
1196 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1197 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1198 }
1199
1200 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1201 nesvnic->next_qp_nic_index,
1202 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1203 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1204 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1205 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1206 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1207 nesvnic->next_qp_nic_index++;
1208 if ((nesvnic->next_qp_nic_index > 3) ||
1209 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1210 nesvnic->next_qp_nic_index = 0;
1211 }
1212 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1213
1214 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1215 u64temp = (u64)nesqp->hwqp.q2_pbase;
1216 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1217 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1218 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1219 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1220 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
Faisal Latif883c6992010-03-02 17:22:51 -06001221 NES_QPCONTEXT_ORDIRD_AAH |
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001222 ((((u32)nesadapter->max_irrq_wr) <<
1223 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1224 if (disable_mpa_crc) {
1225 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1226 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1227 }
1228
1229
1230 /* Create the QP */
1231 cqp_request = nes_get_cqp_request(nesdev);
1232 if (cqp_request == NULL) {
1233 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1234 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1235 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1236 kfree(nesqp->allocated_buffer);
1237 return ERR_PTR(-ENOMEM);
1238 }
1239 cqp_request->waiting = 1;
1240 cqp_wqe = &cqp_request->cqp_wqe;
1241
1242 if (!virt_wqs) {
1243 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1244 NES_CQP_QP_IWARP_STATE_IDLE;
1245 } else {
1246 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1247 NES_CQP_QP_IWARP_STATE_IDLE;
1248 }
1249 opcode |= NES_CQP_QP_CQS_VALID;
1250 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1251 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1252 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1253
1254 u64temp = (u64)nesqp->nesqp_context_pbase;
1255 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1256
1257 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001258 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001259
1260 /* Wait for CQP */
1261 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1262 nesqp->hwqp.qp_id);
1263 ret = wait_event_timeout(cqp_request->waitq,
1264 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1265 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1266 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1267 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1268 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1269 cqp_request->major_code, cqp_request->minor_code);
1270 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001271 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001272 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1273 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1274 kfree(nesqp->allocated_buffer);
1275 if (!ret) {
1276 return ERR_PTR(-ETIME);
1277 } else {
1278 return ERR_PTR(-EIO);
1279 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001280 }
1281
Roland Dreier1ff66e82008-07-14 23:48:49 -07001282 nes_put_cqp_request(nesdev, cqp_request);
1283
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001284 if (ibpd->uobject) {
1285 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
Dan Carpenter60429632013-07-25 20:04:59 +03001286 uresp.mmap_rq_db_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001287 uresp.actual_sq_size = sq_size;
1288 uresp.actual_rq_size = rq_size;
1289 uresp.qp_id = nesqp->hwqp.qp_id;
1290 uresp.nes_drv_opt = nes_drv_opt;
1291 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1292 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1293 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1294 kfree(nesqp->allocated_buffer);
1295 return ERR_PTR(-EFAULT);
1296 }
1297 }
1298
1299 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1300 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1301 spin_lock_init(&nesqp->lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001302 nes_add_ref(&nesqp->ibqp);
1303 break;
1304 default:
1305 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1306 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001307 }
Tatyana Nikolova19974122016-03-29 12:58:37 -05001308 init_completion(&nesqp->sq_drained);
1309 init_completion(&nesqp->rq_drained);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001310
Chien Tungd14152d2009-12-09 15:21:56 -08001311 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
Geliang Tang79d9df52017-04-22 09:32:55 +08001312 setup_timer(&nesqp->terminate_timer, nes_terminate_timeout,
1313 (unsigned long)nesqp);
Chien Tungd14152d2009-12-09 15:21:56 -08001314
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001315 /* update the QP table */
1316 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1317 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00001318 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001319
1320 return &nesqp->ibqp;
1321}
1322
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001323/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001324 * nes_clean_cq
1325 */
1326static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1327{
1328 u32 cq_head;
1329 u32 lo;
1330 u32 hi;
1331 u64 u64temp;
1332 unsigned long flags = 0;
1333
1334 spin_lock_irqsave(&nescq->lock, flags);
1335
1336 cq_head = nescq->hw_cq.cq_head;
1337 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1338 rmb();
1339 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1340 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1341 u64temp = (((u64)hi) << 32) | ((u64)lo);
1342 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1343 if (u64temp == (u64)(unsigned long)nesqp) {
1344 /* Zero the context value so cqe will be ignored */
1345 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1346 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1347 }
1348
1349 if (++cq_head >= nescq->hw_cq.cq_size)
1350 cq_head = 0;
1351 }
1352
1353 spin_unlock_irqrestore(&nescq->lock, flags);
1354}
1355
1356
1357/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001358 * nes_destroy_qp
1359 */
1360static int nes_destroy_qp(struct ib_qp *ibqp)
1361{
1362 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001363 struct nes_ucontext *nes_ucontext;
1364 struct ib_qp_attr attr;
1365 struct iw_cm_id *cm_id;
1366 struct iw_cm_event cm_event;
Faisal Latif0f0bee82011-09-25 20:34:00 -05001367 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001368
1369 atomic_inc(&sw_qps_destroyed);
1370 nesqp->destroyed = 1;
1371
1372 /* Blow away the connection if it exists. */
1373 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1374 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1375 attr.qp_state = IB_QPS_ERR;
1376 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1377 }
1378
1379 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1380 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1381 cm_id = nesqp->cm_id;
1382 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07001383 cm_event.status = -ETIMEDOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001384 cm_event.local_addr = cm_id->local_addr;
1385 cm_event.remote_addr = cm_id->remote_addr;
1386 cm_event.private_data = NULL;
1387 cm_event.private_data_len = 0;
1388
1389 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1390 "QP%u. cm_id = %p, refcount = %u. \n",
1391 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1392
1393 cm_id->rem_ref(cm_id);
1394 ret = cm_id->event_handler(cm_id, &cm_event);
1395 if (ret)
1396 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1397 }
1398
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001399 if (nesqp->user_mode) {
1400 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1401 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1402 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1403 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1404 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1405 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1406 }
1407 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001408 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1409 nesqp->sq_kmapped = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001410 kunmap(nesqp->page);
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001411 }
Don Wood5ee21fe2009-09-05 20:36:37 -07001412 } else {
1413 /* Clean any pending completions from the cq(s) */
1414 if (nesqp->nesscq)
1415 nes_clean_cq(nesqp, nesqp->nesscq);
1416
1417 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1418 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001419 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001420 nes_rem_ref(&nesqp->ibqp);
1421 return 0;
1422}
1423
1424
1425/**
1426 * nes_create_cq
1427 */
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001428static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1429 const struct ib_cq_init_attr *attr,
1430 struct ib_ucontext *context,
1431 struct ib_udata *udata)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001432{
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001433 int entries = attr->cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001434 u64 u64temp;
1435 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1436 struct nes_device *nesdev = nesvnic->nesdev;
1437 struct nes_adapter *nesadapter = nesdev->nesadapter;
1438 struct nes_cq *nescq;
1439 struct nes_ucontext *nes_ucontext = NULL;
1440 struct nes_cqp_request *cqp_request;
1441 void *mem = NULL;
1442 struct nes_hw_cqp_wqe *cqp_wqe;
1443 struct nes_pbl *nespbl = NULL;
1444 struct nes_create_cq_req req;
1445 struct nes_create_cq_resp resp;
1446 u32 cq_num = 0;
1447 u32 opcode = 0;
1448 u32 pbl_entries = 1;
1449 int err;
1450 unsigned long flags;
1451 int ret;
1452
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001453 if (attr->flags)
1454 return ERR_PTR(-EINVAL);
1455
Chien Tung5924aea2009-12-09 15:21:56 -08001456 if (entries > nesadapter->max_cqe)
1457 return ERR_PTR(-EINVAL);
1458
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001459 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001460 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001461 if (err) {
1462 return ERR_PTR(err);
1463 }
1464
1465 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1466 if (!nescq) {
1467 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001468 return ERR_PTR(-ENOMEM);
1469 }
1470
1471 nescq->hw_cq.cq_size = max(entries + 1, 5);
1472 nescq->hw_cq.cq_number = cq_num;
1473 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1474
1475
1476 if (context) {
1477 nes_ucontext = to_nesucontext(context);
1478 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1479 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1480 kfree(nescq);
1481 return ERR_PTR(-EFAULT);
1482 }
1483 nesvnic->mcrq_ucontext = nes_ucontext;
1484 nes_ucontext->mcrqf = req.mcrqf;
1485 if (nes_ucontext->mcrqf) {
1486 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001487 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001488 else if (nes_ucontext->mcrqf & 0x40000000)
1489 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1490 else
1491 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001492 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001493 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1494 }
1495 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1496 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001497 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001498 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1499 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1500 list_del(&nespbl->list);
1501 err = 0;
1502 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1503 nespbl);
1504 break;
1505 }
1506 }
1507 if (err) {
1508 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1509 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001510 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001511 }
1512
1513 pbl_entries = nespbl->pbl_size >> 3;
1514 nescq->cq_mem_size = 0;
1515 } else {
1516 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1517 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1518 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1519
1520 /* allocate the physical buffer space */
Joe Perches9011a672014-08-08 14:24:16 -07001521 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1522 &nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001523 if (!mem) {
1524 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1525 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1526 kfree(nescq);
1527 return ERR_PTR(-ENOMEM);
1528 }
1529
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001530 nescq->hw_cq.cq_vbase = mem;
1531 nescq->hw_cq.cq_head = 0;
1532 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1533 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1534 (u32)nescq->hw_cq.cq_pbase);
1535 }
1536
1537 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1538 spin_lock_init(&nescq->lock);
1539
1540 /* send CreateCQ request to CQP */
1541 cqp_request = nes_get_cqp_request(nesdev);
1542 if (cqp_request == NULL) {
1543 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1544 if (!context)
1545 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1546 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001547 else {
1548 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1549 nespbl->pbl_vbase, nespbl->pbl_pbase);
1550 kfree(nespbl);
1551 }
1552
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001553 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1554 kfree(nescq);
1555 return ERR_PTR(-ENOMEM);
1556 }
1557 cqp_request->waiting = 1;
1558 cqp_wqe = &cqp_request->cqp_wqe;
1559
1560 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1561 NES_CQP_CQ_CHK_OVERFLOW |
1562 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1563
1564 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1565
1566 if (pbl_entries != 1) {
1567 if (pbl_entries > 32) {
1568 /* use 4k pbl */
1569 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1570 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001571 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1572 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001573 if (!context)
1574 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1575 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001576 else {
1577 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1578 nespbl->pbl_vbase, nespbl->pbl_pbase);
1579 kfree(nespbl);
1580 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001581 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1582 kfree(nescq);
1583 return ERR_PTR(-ENOMEM);
1584 } else {
1585 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1586 nescq->virtual_cq = 2;
1587 nesadapter->free_4kpbl--;
1588 }
1589 } else {
1590 /* use 256 byte pbl */
1591 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1592 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001593 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1594 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001595 if (!context)
1596 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1597 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001598 else {
1599 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1600 nespbl->pbl_vbase, nespbl->pbl_pbase);
1601 kfree(nespbl);
1602 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001603 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1604 kfree(nescq);
1605 return ERR_PTR(-ENOMEM);
1606 } else {
1607 opcode |= NES_CQP_CQ_VIRT;
1608 nescq->virtual_cq = 1;
1609 nesadapter->free_256pbl--;
1610 }
1611 }
1612 }
1613
1614 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1615
1616 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1617 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1618 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1619 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1620
1621 if (context) {
1622 if (pbl_entries != 1)
1623 u64temp = (u64)nespbl->pbl_pbase;
1624 else
1625 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1626 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1627 nes_ucontext->mmap_db_index[0]);
1628 } else {
1629 u64temp = (u64)nescq->hw_cq.cq_pbase;
1630 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1631 }
1632 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1633 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1634 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1635 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1636 cpu_to_le32((u32)(u64temp >> 1));
1637 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1638 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1639
1640 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001641 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001642
1643 /* Wait for CQP */
1644 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1645 nescq->hw_cq.cq_number);
1646 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1647 NES_EVENT_TIMEOUT * 2);
1648 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1649 nescq->hw_cq.cq_number, ret);
1650 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001651 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001652 if (!context)
1653 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1654 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001655 else {
1656 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1657 nespbl->pbl_vbase, nespbl->pbl_pbase);
1658 kfree(nespbl);
1659 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001660 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1661 kfree(nescq);
1662 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001663 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001664 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001665
1666 if (context) {
1667 /* free the nespbl */
1668 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1669 nespbl->pbl_pbase);
1670 kfree(nespbl);
1671 resp.cq_id = nescq->hw_cq.cq_number;
1672 resp.cq_size = nescq->hw_cq.cq_size;
1673 resp.mmap_db_index = 0;
Dan Carpenter60429632013-07-25 20:04:59 +03001674 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001675 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1676 kfree(nescq);
1677 return ERR_PTR(-EFAULT);
1678 }
1679 }
1680
1681 return &nescq->ibcq;
1682}
1683
1684
1685/**
1686 * nes_destroy_cq
1687 */
1688static int nes_destroy_cq(struct ib_cq *ib_cq)
1689{
1690 struct nes_cq *nescq;
1691 struct nes_device *nesdev;
1692 struct nes_vnic *nesvnic;
1693 struct nes_adapter *nesadapter;
1694 struct nes_hw_cqp_wqe *cqp_wqe;
1695 struct nes_cqp_request *cqp_request;
1696 unsigned long flags;
1697 u32 opcode = 0;
1698 int ret;
1699
1700 if (ib_cq == NULL)
1701 return 0;
1702
1703 nescq = to_nescq(ib_cq);
1704 nesvnic = to_nesvnic(ib_cq->device);
1705 nesdev = nesvnic->nesdev;
1706 nesadapter = nesdev->nesadapter;
1707
1708 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1709
1710 /* Send DestroyCQ request to CQP */
1711 cqp_request = nes_get_cqp_request(nesdev);
1712 if (cqp_request == NULL) {
1713 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1714 return -ENOMEM;
1715 }
1716 cqp_request->waiting = 1;
1717 cqp_wqe = &cqp_request->cqp_wqe;
1718 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1719 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1720 if (nescq->virtual_cq == 1) {
1721 nesadapter->free_256pbl++;
1722 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1723 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001724 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001725 }
1726 } else if (nescq->virtual_cq == 2) {
1727 nesadapter->free_4kpbl++;
1728 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1729 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001730 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001731 }
1732 opcode |= NES_CQP_CQ_4KB_CHUNK;
1733 }
1734
1735 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1736
1737 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1738 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1739 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1740 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001741 if (!nescq->mcrqf)
1742 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1743
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001744 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001745 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001746
1747 /* Wait for CQP */
1748 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1749 nescq->hw_cq.cq_number);
1750 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1751 NES_EVENT_TIMEOUT);
1752 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1753 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1754 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1755 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001756 if (!ret) {
1757 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001758 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001759 ret = -ETIME;
1760 } else if (cqp_request->major_code) {
1761 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001762 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001763 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001764 } else {
1765 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001766 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001767 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001768
1769 if (nescq->cq_mem_size)
1770 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001771 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001772 kfree(nescq);
1773
1774 return ret;
1775}
1776
Don Wood0145f342009-03-06 15:15:00 -08001777/**
1778 * root_256
1779 */
1780static u32 root_256(struct nes_device *nesdev,
1781 struct nes_root_vpbl *root_vpbl,
1782 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001783 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001784{
1785 u64 leaf_pbl;
1786 int i, j, k;
1787
1788 if (pbl_count_4k == 1) {
1789 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1790 512, &new_root->pbl_pbase);
1791
1792 if (new_root->pbl_vbase == NULL)
1793 return 0;
1794
1795 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1796 for (i = 0; i < 16; i++) {
1797 new_root->pbl_vbase[i].pa_low =
1798 cpu_to_le32((u32)leaf_pbl);
1799 new_root->pbl_vbase[i].pa_high =
1800 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1801 leaf_pbl += 256;
1802 }
1803 } else {
1804 for (i = 3; i >= 0; i--) {
1805 j = i * 16;
1806 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1807 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1808 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1809 << 32);
1810 for (k = 1; k < 16; k++) {
1811 leaf_pbl += 256;
1812 root_vpbl->pbl_vbase[j + k].pa_low =
1813 cpu_to_le32((u32)leaf_pbl);
1814 root_vpbl->pbl_vbase[j + k].pa_high =
1815 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1816 }
1817 }
1818 }
1819
1820 return 1;
1821}
1822
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001823
1824/**
1825 * nes_reg_mr
1826 */
1827static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1828 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08001829 dma_addr_t single_buffer, u16 pbl_count_4k,
1830 u16 residual_page_count_4k, int acc, u64 *iova_start,
1831 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001832{
1833 struct nes_hw_cqp_wqe *cqp_wqe;
1834 struct nes_cqp_request *cqp_request;
1835 unsigned long flags;
1836 int ret;
1837 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08001838 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07001839 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08001840 u16 pbl_count = 0;
1841 u8 use_256_pbls = 0;
1842 u8 use_4k_pbls = 0;
1843 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
Or Gerlitz812d8672010-07-20 12:00:20 +00001844 struct nes_root_vpbl new_root = { 0, NULL, NULL };
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001845 u32 opcode = 0;
1846 u16 major_code;
1847
1848 /* Register the region with the adapter */
1849 cqp_request = nes_get_cqp_request(nesdev);
1850 if (cqp_request == NULL) {
1851 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1852 return -ENOMEM;
1853 }
1854 cqp_request->waiting = 1;
1855 cqp_wqe = &cqp_request->cqp_wqe;
1856
Don Wood0145f342009-03-06 15:15:00 -08001857 if (pbl_count_4k) {
1858 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1859
1860 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1861 pbl_count_256 = (pg_cnt + 31) / 32;
1862 if (pg_cnt <= 32) {
1863 if (pbl_count_256 <= nesadapter->free_256pbl)
1864 use_256_pbls = 1;
1865 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1866 use_4k_pbls = 1;
1867 } else if (pg_cnt <= 2048) {
1868 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1869 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1870 use_4k_pbls = 1;
1871 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1872 use_256_pbls = 1;
1873 use_two_level = 1;
1874 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1875 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001876 }
1877 } else {
Don Wood0145f342009-03-06 15:15:00 -08001878 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1879 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001880 }
Don Wood0145f342009-03-06 15:15:00 -08001881
1882 if (use_256_pbls) {
1883 pbl_count = pbl_count_256;
1884 nesadapter->free_256pbl -= pbl_count + use_two_level;
1885 } else if (use_4k_pbls) {
1886 pbl_count = pbl_count_4k;
1887 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1888 } else {
1889 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1890 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1891 nes_free_cqp_request(nesdev, cqp_request);
1892 return -ENOMEM;
1893 }
1894
1895 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001896 }
1897
Don Wood0145f342009-03-06 15:15:00 -08001898 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07001899 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08001900 if (new_root.pbl_pbase != 0)
1901 root_vpbl = &new_root;
1902 } else {
1903 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1904 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1905 use_256_pbls = 0;
1906
1907 if (pbl_count_4k == 1)
1908 use_two_level = 0;
1909 pbl_count = pbl_count_4k;
1910
1911 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1912 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1913 use_4k_pbls = 1;
1914 }
1915 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1916
1917 if (use_4k_pbls == 0)
1918 return -ENOMEM;
1919 }
1920 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001921
1922 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1923 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1924 if (acc & IB_ACCESS_LOCAL_WRITE)
1925 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1926 if (acc & IB_ACCESS_REMOTE_WRITE)
1927 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1928 if (acc & IB_ACCESS_REMOTE_READ)
1929 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1930 if (acc & IB_ACCESS_MW_BIND)
1931 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1932
1933 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1934 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1935 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1936 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1937
1938 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1939 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1940 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1941 cpu_to_le32(nespd->pd_id & 0x00007fff);
1942 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1943
1944 if (pbl_count == 0) {
1945 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1946 } else {
1947 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1948 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 -08001949 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 -08001950
Don Wood0145f342009-03-06 15:15:00 -08001951 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001952 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1953 }
1954 barrier();
1955
1956 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001957 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001958
1959 /* Wait for CQP */
1960 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1961 NES_EVENT_TIMEOUT);
1962 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1963 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1964 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1965 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07001966 nes_put_cqp_request(nesdev, cqp_request);
1967
Don Wooddae5d132009-03-06 15:12:09 -08001968 if ((!ret || major_code) && pbl_count != 0) {
1969 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08001970 if (use_256_pbls)
1971 nesadapter->free_256pbl += pbl_count + use_two_level;
1972 else if (use_4k_pbls)
1973 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08001974 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1975 }
Don Wood0145f342009-03-06 15:15:00 -08001976 if (new_root.pbl_pbase)
1977 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1978 new_root.pbl_pbase);
1979
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001980 if (!ret)
1981 return -ETIME;
1982 else if (major_code)
1983 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001984
Don Wood0145f342009-03-06 15:15:00 -08001985 *actual_pbl_cnt = pbl_count + use_two_level;
1986 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001987 return 0;
1988}
1989
1990
1991/**
1992 * nes_reg_phys_mr
1993 */
Christoph Hellwigf64054d2015-12-23 19:12:50 +01001994struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
1995 int acc, u64 *iova_start)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001996{
1997 u64 region_length;
1998 struct nes_pd *nespd = to_nespd(ib_pd);
1999 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2000 struct nes_device *nesdev = nesvnic->nesdev;
2001 struct nes_adapter *nesadapter = nesdev->nesadapter;
2002 struct nes_mr *nesmr;
2003 struct ib_mr *ibmr;
2004 struct nes_vpbl vpbl;
2005 struct nes_root_vpbl root_vpbl;
2006 u32 stag;
Don Wood3f32eb12009-04-20 14:53:00 -07002007 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002008 u32 stag_index = 0;
2009 u32 next_stag_index = 0;
2010 u32 driver_key = 0;
Roland Dreier69d51022010-08-04 14:25:40 -07002011 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002012 int ret = 0;
2013 u16 pbl_count = 0;
2014 u8 single_page = 1;
2015 u8 stag_key = 0;
2016
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002017 region_length = 0;
2018 vpbl.pbl_vbase = NULL;
2019 root_vpbl.pbl_vbase = NULL;
2020 root_vpbl.pbl_pbase = 0;
2021
2022 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2023 stag_key = (u8)next_stag_index;
2024
2025 driver_key = 0;
2026
2027 next_stag_index >>= 8;
2028 next_stag_index %= nesadapter->max_mr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002029
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002030 if ((addr ^ *iova_start) & ~PAGE_MASK)
Don Wood3f32eb12009-04-20 14:53:00 -07002031 return ERR_PTR(-EINVAL);
2032
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002033 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002034 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002035 if (err) {
2036 return ERR_PTR(err);
2037 }
2038
2039 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2040 if (!nesmr) {
2041 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2042 return ERR_PTR(-ENOMEM);
2043 }
2044
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002045 /* Allocate a 4K buffer for the PBL */
2046 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2047 &vpbl.pbl_pbase);
2048 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2049 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2050 if (!vpbl.pbl_vbase) {
2051 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2052 ibmr = ERR_PTR(-ENOMEM);
2053 kfree(nesmr);
2054 goto reg_phys_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002055 }
2056
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002057
2058 mask = !size;
2059
2060 if (mask & ~PAGE_MASK) {
2061 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2062 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2063 ibmr = ERR_PTR(-EINVAL);
2064 kfree(nesmr);
2065 goto reg_phys_err;
2066 }
2067
2068 region_length += size;
2069 vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2070 vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2071
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002072 stag = stag_index << 8;
2073 stag |= driver_key;
2074 stag += (u32)stag_key;
2075
2076 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2077 " length = 0x%016lX, index = 0x%08X\n",
2078 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2079
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002080 /* Make the leaf PBL the root if only one PBL */
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002081 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002082
2083 if (single_page) {
2084 pbl_count = 0;
2085 } else {
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002086 pbl_count = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002087 }
2088 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002089 addr, pbl_count, 1, acc, iova_start,
Don Wood0145f342009-03-06 15:15:00 -08002090 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002091
2092 if (ret == 0) {
2093 nesmr->ibmr.rkey = stag;
2094 nesmr->ibmr.lkey = stag;
2095 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2096 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097 } else {
2098 kfree(nesmr);
2099 ibmr = ERR_PTR(-ENOMEM);
2100 }
2101
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002102reg_phys_err:
2103 /* single PBL case */
2104 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002105 return ibmr;
2106}
2107
2108
2109/**
2110 * nes_get_dma_mr
2111 */
2112static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2113{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002114 u64 kva = 0;
2115
2116 nes_debug(NES_DBG_MR, "\n");
2117
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002118 return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002119}
2120
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002121/**
2122 * nes_reg_user_mr
2123 */
2124static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2125 u64 virt, int acc, struct ib_udata *udata)
2126{
2127 u64 iova_start;
2128 __le64 *pbl;
2129 u64 region_length;
2130 dma_addr_t last_dma_addr = 0;
2131 dma_addr_t first_dma_addr = 0;
2132 struct nes_pd *nespd = to_nespd(pd);
2133 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2134 struct nes_device *nesdev = nesvnic->nesdev;
2135 struct nes_adapter *nesadapter = nesdev->nesadapter;
2136 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002137 struct scatterlist *sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002138 struct nes_ucontext *nes_ucontext;
2139 struct nes_pbl *nespbl;
2140 struct nes_mr *nesmr;
2141 struct ib_umem *region;
2142 struct nes_mem_reg_req req;
2143 struct nes_vpbl vpbl;
2144 struct nes_root_vpbl root_vpbl;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002145 int entry, page_index;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002146 int page_count = 0;
2147 int err, pbl_depth = 0;
2148 int chunk_pages;
2149 int ret;
2150 u32 stag;
2151 u32 stag_index = 0;
2152 u32 next_stag_index;
2153 u32 driver_key;
2154 u32 root_pbl_index = 0;
2155 u32 cur_pbl_index = 0;
2156 u32 skip_pages;
2157 u16 pbl_count;
2158 u8 single_page = 1;
2159 u8 stag_key;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002160 int first_page = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002161
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002162 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002163 if (IS_ERR(region)) {
2164 return (struct ib_mr *)region;
2165 }
2166
2167 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
Artemy Kovalyov3e7e1192017-04-05 09:23:50 +03002168 " offset = %u, page size = %lu.\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002169 (unsigned long int)start, (unsigned long int)virt, (u32)length,
Artemy Kovalyov3e7e1192017-04-05 09:23:50 +03002170 ib_umem_offset(region), BIT(region->page_shift));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002171
Haggai Eran406f9e52014-12-11 17:04:12 +02002172 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002173
Julia Lawalle2e435f2011-10-06 09:33:24 -07002174 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2175 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002176 return ERR_PTR(-EFAULT);
Julia Lawalle2e435f2011-10-06 09:33:24 -07002177 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002178 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2179
2180 switch (req.reg_type) {
2181 case IWNES_MEMREG_TYPE_MEM:
2182 pbl_depth = 0;
2183 region_length = 0;
2184 vpbl.pbl_vbase = NULL;
2185 root_vpbl.pbl_vbase = NULL;
2186 root_vpbl.pbl_pbase = 0;
2187
2188 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2189 stag_key = (u8)next_stag_index;
2190
2191 driver_key = next_stag_index & 0x70000000;
2192
2193 next_stag_index >>= 8;
2194 next_stag_index %= nesadapter->max_mr;
2195
2196 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002197 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198 if (err) {
2199 ib_umem_release(region);
2200 return ERR_PTR(err);
2201 }
2202
2203 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2204 if (!nesmr) {
2205 ib_umem_release(region);
2206 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2207 return ERR_PTR(-ENOMEM);
2208 }
2209 nesmr->region = region;
2210
Yishai Hadaseeb84612014-01-28 13:40:15 +02002211 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2212 if (sg_dma_address(sg) & ~PAGE_MASK) {
2213 ib_umem_release(region);
2214 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2215 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2216 (unsigned int) sg_dma_address(sg));
2217 ibmr = ERR_PTR(-EINVAL);
2218 kfree(nesmr);
2219 goto reg_user_mr_err;
2220 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002221
Yishai Hadaseeb84612014-01-28 13:40:15 +02002222 if (!sg_dma_len(sg)) {
2223 ib_umem_release(region);
2224 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2225 stag_index);
2226 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2227 ibmr = ERR_PTR(-EINVAL);
2228 kfree(nesmr);
2229 goto reg_user_mr_err;
2230 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231
Yishai Hadaseeb84612014-01-28 13:40:15 +02002232 region_length += sg_dma_len(sg);
2233 chunk_pages = sg_dma_len(sg) >> 12;
2234 region_length -= skip_pages << 12;
2235 for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2236 skip_pages = 0;
Haggai Eran406f9e52014-12-11 17:04:12 +02002237 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
Yishai Hadaseeb84612014-01-28 13:40:15 +02002238 goto enough_pages;
2239 if ((page_count&0x01FF) == 0) {
2240 if (page_count >= 1024 * 512) {
2241 ib_umem_release(region);
2242 nes_free_resource(nesadapter,
2243 nesadapter->allocated_mrs, stag_index);
2244 kfree(nesmr);
2245 ibmr = ERR_PTR(-E2BIG);
2246 goto reg_user_mr_err;
2247 }
2248 if (root_pbl_index == 1) {
2249 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2250 8192, &root_vpbl.pbl_pbase);
2251 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2252 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2253 if (!root_vpbl.pbl_vbase) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002254 ib_umem_release(region);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002255 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2256 vpbl.pbl_pbase);
2257 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2258 stag_index);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002259 kfree(nesmr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002260 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002261 goto reg_user_mr_err;
2262 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002263 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2264 GFP_KERNEL);
2265 if (!root_vpbl.leaf_vpbl) {
2266 ib_umem_release(region);
2267 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2268 root_vpbl.pbl_pbase);
2269 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2270 vpbl.pbl_pbase);
2271 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2272 stag_index);
2273 kfree(nesmr);
2274 ibmr = ERR_PTR(-ENOMEM);
2275 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002276 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002277 root_vpbl.pbl_vbase[0].pa_low =
2278 cpu_to_le32((u32)vpbl.pbl_pbase);
2279 root_vpbl.pbl_vbase[0].pa_high =
2280 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2281 root_vpbl.leaf_vpbl[0] = vpbl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002282 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002283 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2284 &vpbl.pbl_pbase);
2285 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2286 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2287 if (!vpbl.pbl_vbase) {
2288 ib_umem_release(region);
2289 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2290 ibmr = ERR_PTR(-ENOMEM);
2291 kfree(nesmr);
2292 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002293 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002294 if (1 <= root_pbl_index) {
2295 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2296 cpu_to_le32((u32)vpbl.pbl_pbase);
2297 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2298 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2299 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2300 }
2301 root_pbl_index++;
2302 cur_pbl_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002303 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002304 if (single_page) {
2305 if (page_count != 0) {
2306 if ((last_dma_addr+4096) !=
2307 (sg_dma_address(sg)+
2308 (page_index*4096)))
2309 single_page = 0;
2310 last_dma_addr = sg_dma_address(sg)+
2311 (page_index*4096);
2312 } else {
2313 first_dma_addr = sg_dma_address(sg)+
2314 (page_index*4096);
2315 last_dma_addr = first_dma_addr;
2316 }
2317 }
2318
2319 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2320 cpu_to_le32((u32)(sg_dma_address(sg)+
2321 (page_index*4096)));
2322 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2323 cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2324 (page_index*4096))) >> 32)));
2325 cur_pbl_index++;
2326 page_count++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002327 }
2328 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002329
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002330 enough_pages:
2331 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2332 " stag_key=0x%08x\n",
2333 stag_index, driver_key, stag_key);
2334 stag = stag_index << 8;
2335 stag |= driver_key;
2336 stag += (u32)stag_key;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002337
2338 iova_start = virt;
2339 /* Make the leaf PBL the root if only one PBL */
2340 if (root_pbl_index == 1) {
2341 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2342 }
2343
2344 if (single_page) {
2345 pbl_count = 0;
2346 } else {
2347 pbl_count = root_pbl_index;
2348 first_dma_addr = 0;
2349 }
2350 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2351 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2352 stag, (unsigned int)iova_start,
2353 (unsigned int)region_length, stag_index,
2354 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002355 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2356 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2357 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002358
2359 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2360
2361 if (ret == 0) {
2362 nesmr->ibmr.rkey = stag;
2363 nesmr->ibmr.lkey = stag;
2364 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2365 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002366 } else {
2367 ib_umem_release(region);
2368 kfree(nesmr);
2369 ibmr = ERR_PTR(-ENOMEM);
2370 }
2371
2372 reg_user_mr_err:
2373 /* free the resources */
2374 if (root_pbl_index == 1) {
2375 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2376 vpbl.pbl_pbase);
2377 } else {
2378 for (page_index=0; page_index<root_pbl_index; page_index++) {
2379 pci_free_consistent(nesdev->pcidev, 4096,
2380 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2381 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2382 }
2383 kfree(root_vpbl.leaf_vpbl);
2384 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2385 root_vpbl.pbl_pbase);
2386 }
2387
2388 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2389
2390 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002391 case IWNES_MEMREG_TYPE_QP:
2392 case IWNES_MEMREG_TYPE_CQ:
Tatyana Nikolova7d9c1992012-12-06 20:05:02 +00002393 if (!region->length) {
2394 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2395 ib_umem_release(region);
2396 return ERR_PTR(-EINVAL);
2397 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002398 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2399 if (!nespbl) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002400 ib_umem_release(region);
2401 return ERR_PTR(-ENOMEM);
2402 }
2403 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2404 if (!nesmr) {
2405 ib_umem_release(region);
2406 kfree(nespbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002407 return ERR_PTR(-ENOMEM);
2408 }
2409 nesmr->region = region;
2410 nes_ucontext = to_nesucontext(pd->uobject->context);
2411 pbl_depth = region->length >> 12;
2412 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2413 nespbl->pbl_size = pbl_depth*sizeof(u64);
2414 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2415 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2416 } else {
2417 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2418 }
2419
2420 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2421 nespbl->pbl_size, pbl_depth);
2422 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2423 &nespbl->pbl_pbase);
2424 if (!pbl) {
2425 ib_umem_release(region);
2426 kfree(nesmr);
2427 kfree(nespbl);
2428 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2429 return ERR_PTR(-ENOMEM);
2430 }
2431
2432 nespbl->pbl_vbase = (u64 *)pbl;
2433 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002434 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002435 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002436 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2437 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002438
Yishai Hadaseeb84612014-01-28 13:40:15 +02002439 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2440 chunk_pages = sg_dma_len(sg) >> 12;
2441 chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2442 if (first_page) {
2443 nespbl->page = sg_page(sg);
2444 first_page = 0;
2445 }
2446
2447 for (page_index = 0; page_index < chunk_pages; page_index++) {
2448 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2449 (sg_dma_address(sg)+
2450 (page_index*4096)));
2451 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2452 (sg_dma_address(sg)+
2453 (page_index*4096)))>>32);
2454 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2455 (unsigned long long)*pbl,
2456 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2457 pbl++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002458 }
2459 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002460
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002461 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2462 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2463 } else {
2464 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2465 }
2466 nesmr->ibmr.rkey = -1;
2467 nesmr->ibmr.lkey = -1;
2468 nesmr->mode = req.reg_type;
2469 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002470 }
2471
Julia Lawalle2e435f2011-10-06 09:33:24 -07002472 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002473 return ERR_PTR(-ENOSYS);
2474}
2475
2476
2477/**
2478 * nes_dereg_mr
2479 */
2480static int nes_dereg_mr(struct ib_mr *ib_mr)
2481{
2482 struct nes_mr *nesmr = to_nesmr(ib_mr);
2483 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2484 struct nes_device *nesdev = nesvnic->nesdev;
2485 struct nes_adapter *nesadapter = nesdev->nesadapter;
2486 struct nes_hw_cqp_wqe *cqp_wqe;
2487 struct nes_cqp_request *cqp_request;
2488 unsigned long flags;
2489 int ret;
2490 u16 major_code;
2491 u16 minor_code;
2492
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03002493
2494 if (nesmr->pages)
2495 pci_free_consistent(nesdev->pcidev,
2496 nesmr->max_pages * sizeof(u64),
2497 nesmr->pages,
2498 nesmr->paddr);
2499
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002500 if (nesmr->region) {
2501 ib_umem_release(nesmr->region);
2502 }
2503 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2504 kfree(nesmr);
2505 return 0;
2506 }
2507
2508 /* Deallocate the region with the adapter */
2509
2510 cqp_request = nes_get_cqp_request(nesdev);
2511 if (cqp_request == NULL) {
2512 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2513 return -ENOMEM;
2514 }
2515 cqp_request->waiting = 1;
2516 cqp_wqe = &cqp_request->cqp_wqe;
2517
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002518 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2519 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2520 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2521 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2522 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2523
2524 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002525 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002526
2527 /* Wait for CQP */
2528 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2529 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2530 NES_EVENT_TIMEOUT);
2531 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2532 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2533 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2534
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002535 major_code = cqp_request->major_code;
2536 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002537
2538 nes_put_cqp_request(nesdev, cqp_request);
2539
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002540 if (!ret) {
2541 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2542 " ib_mr=%p, rkey = 0x%08X\n",
2543 ib_mr, ib_mr->rkey);
2544 return -ETIME;
2545 } else if (major_code) {
2546 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2547 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2548 major_code, minor_code, ib_mr, ib_mr->rkey);
2549 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002550 }
2551
2552 if (nesmr->pbls_used != 0) {
2553 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2554 if (nesmr->pbl_4k) {
2555 nesadapter->free_4kpbl += nesmr->pbls_used;
2556 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2557 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2558 "exceeded the max(%u)\n",
2559 nesadapter->free_4kpbl,
2560 nesadapter->max_4kpbl);
2561 } else {
2562 nesadapter->free_256pbl += nesmr->pbls_used;
2563 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2564 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2565 "exceeded the max(%u)\n",
2566 nesadapter->free_256pbl,
2567 nesadapter->max_256pbl);
2568 }
2569 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2570 }
2571 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2572 (ib_mr->rkey & 0x0fffff00) >> 8);
2573
2574 kfree(nesmr);
2575
2576 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002577}
2578
2579
2580/**
2581 * show_rev
2582 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002583static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2584 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002585{
2586 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002587 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002588 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2589
2590 nes_debug(NES_DBG_INIT, "\n");
2591 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2592}
2593
2594
2595/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002596 * show_hca
2597 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002598static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2599 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002600{
2601 nes_debug(NES_DBG_INIT, "\n");
2602 return sprintf(buf, "NES020\n");
2603}
2604
2605
2606/**
2607 * show_board
2608 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002609static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2610 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002611{
2612 nes_debug(NES_DBG_INIT, "\n");
2613 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2614}
2615
2616
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002617static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002618static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2619static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002621static struct device_attribute *nes_dev_attributes[] = {
2622 &dev_attr_hw_rev,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002623 &dev_attr_hca_type,
2624 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002625};
2626
2627
2628/**
2629 * nes_query_qp
2630 */
2631static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2632 int attr_mask, struct ib_qp_init_attr *init_attr)
2633{
2634 struct nes_qp *nesqp = to_nesqp(ibqp);
2635
2636 nes_debug(NES_DBG_QP, "\n");
2637
2638 attr->qp_access_flags = 0;
2639 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2640 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2641 attr->cap.max_recv_sge = 1;
Chien Tungeadde3a2010-03-25 13:39:50 +00002642 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2643 attr->cap.max_inline_data = 0;
2644 else
2645 attr->cap.max_inline_data = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002646
2647 init_attr->event_handler = nesqp->ibqp.event_handler;
2648 init_attr->qp_context = nesqp->ibqp.qp_context;
2649 init_attr->send_cq = nesqp->ibqp.send_cq;
2650 init_attr->recv_cq = nesqp->ibqp.recv_cq;
Dave Jones4127c362013-09-17 17:45:54 -04002651 init_attr->srq = nesqp->ibqp.srq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002652 init_attr->cap = attr->cap;
2653
2654 return 0;
2655}
2656
2657
2658/**
2659 * nes_hw_modify_qp
2660 */
2661int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002662 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002663{
2664 struct nes_hw_cqp_wqe *cqp_wqe;
2665 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2666 /* struct iw_cm_event cm_event; */
2667 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002668 int ret;
2669 u16 major_code;
2670
2671 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2672 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2673
2674 cqp_request = nes_get_cqp_request(nesdev);
2675 if (cqp_request == NULL) {
2676 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2677 return -ENOMEM;
2678 }
2679 if (wait_completion) {
2680 cqp_request->waiting = 1;
2681 } else {
2682 cqp_request->waiting = 0;
2683 }
2684 cqp_wqe = &cqp_request->cqp_wqe;
2685
2686 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2687 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2688 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2689 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2690 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2691 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2692 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2693
Don Wood8b1c9dc2009-09-05 20:36:38 -07002694 /* If sending a terminate message, fill in the length (in words) */
2695 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2696 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2697 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2698 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2699 }
2700
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002701 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002702 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002703
2704 /* Wait for CQP */
2705 if (wait_completion) {
2706 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2707 nesqp->hwqp.qp_id); */
2708 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2709 NES_EVENT_TIMEOUT);
2710 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2711 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2712 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2713 major_code = cqp_request->major_code;
2714 if (major_code) {
2715 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2716 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2717 nesqp->hwqp.qp_id, cqp_request->major_code,
2718 cqp_request->minor_code, next_iwarp_state);
2719 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002720
2721 nes_put_cqp_request(nesdev, cqp_request);
2722
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002723 if (!ret)
2724 return -ETIME;
2725 else if (major_code)
2726 return -EIO;
2727 else
2728 return 0;
2729 } else {
2730 return 0;
2731 }
2732}
2733
2734
2735/**
2736 * nes_modify_qp
2737 */
2738int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2739 int attr_mask, struct ib_udata *udata)
2740{
2741 struct nes_qp *nesqp = to_nesqp(ibqp);
2742 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2743 struct nes_device *nesdev = nesvnic->nesdev;
2744 /* u32 cqp_head; */
2745 /* u32 counter; */
2746 u32 next_iwarp_state = 0;
2747 int err;
2748 unsigned long qplockflags;
2749 int ret;
2750 u16 original_last_aeq;
2751 u8 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002752 u8 dont_wait = 0;
2753
2754 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2755 " iwarp_state=0x%X, refcount=%d\n",
2756 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2757 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2758
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002759 spin_lock_irqsave(&nesqp->lock, qplockflags);
2760
2761 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2762 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2763 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2764 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2765
2766 if (attr_mask & IB_QP_STATE) {
2767 switch (attr->qp_state) {
2768 case IB_QPS_INIT:
2769 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2770 nesqp->hwqp.qp_id);
2771 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2772 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002773 return -EINVAL;
2774 }
2775 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2776 issue_modify_qp = 1;
2777 break;
2778 case IB_QPS_RTR:
2779 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2780 nesqp->hwqp.qp_id);
2781 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2782 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783 return -EINVAL;
2784 }
2785 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2786 issue_modify_qp = 1;
2787 break;
2788 case IB_QPS_RTS:
2789 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2790 nesqp->hwqp.qp_id);
2791 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2792 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002793 return -EINVAL;
2794 }
2795 if (nesqp->cm_id == NULL) {
2796 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2797 nesqp->hwqp.qp_id );
2798 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002799 return -EINVAL;
2800 }
2801 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2802 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2803 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2804 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2805 issue_modify_qp = 1;
2806 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2807 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2808 nesqp->hte_added = 1;
2809 break;
2810 case IB_QPS_SQD:
2811 issue_modify_qp = 1;
2812 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2813 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2814 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2815 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002816 return 0;
2817 } else {
2818 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2819 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2820 " ignored due to current iWARP state\n",
2821 nesqp->hwqp.qp_id);
2822 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002823 return -EINVAL;
2824 }
2825 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2826 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2827 " already done based on hw state.\n",
2828 nesqp->hwqp.qp_id);
2829 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830 }
2831 switch (nesqp->hw_iwarp_state) {
2832 case NES_AEQE_IWARP_STATE_CLOSING:
2833 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Tatyana Nikolovad5fb4762012-10-02 16:38:12 +00002834 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002835 case NES_AEQE_IWARP_STATE_TERMINATE:
2836 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2837 break;
2838 case NES_AEQE_IWARP_STATE_ERROR:
2839 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2840 break;
2841 default:
2842 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002843 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2844 break;
2845 }
2846 }
2847 break;
2848 case IB_QPS_SQE:
2849 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2850 nesqp->hwqp.qp_id);
2851 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2852 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002853 return -EINVAL;
2854 }
2855 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2856 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2857 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2858 issue_modify_qp = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002859 break;
2860 case IB_QPS_ERR:
2861 case IB_QPS_RESET:
2862 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2863 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002864 return -EINVAL;
2865 }
2866 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2867 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07002868 if (nesqp->term_flags)
2869 del_timer(&nesqp->terminate_timer);
2870
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2872 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2873 if (nesqp->hte_added) {
2874 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2875 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2876 nesqp->hte_added = 0;
2877 }
2878 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
Faisal Latifcd6860e2010-07-04 00:17:59 +00002879 (nesdev->iw_status) &&
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2881 next_iwarp_state |= NES_CQP_QP_RESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002882 } else {
2883 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2884 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2885 dont_wait = 1;
2886 }
2887 issue_modify_qp = 1;
2888 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2889 break;
2890 default:
2891 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 return -EINVAL;
2893 break;
2894 }
2895
2896 nesqp->ibqp_state = attr->qp_state;
Tatyana Nikolovaa67b0782012-10-02 16:40:33 +00002897 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2898 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2899 nesqp->iwarp_state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002900 }
2901
2902 if (attr_mask & IB_QP_ACCESS_FLAGS) {
2903 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2904 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2905 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2906 issue_modify_qp = 1;
2907 }
2908 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2909 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2910 issue_modify_qp = 1;
2911 }
2912 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2913 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2914 issue_modify_qp = 1;
2915 }
2916 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2917 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2918 issue_modify_qp = 1;
2919 }
2920
2921 if (nesqp->user_mode) {
2922 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2923 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2924 issue_modify_qp = 1;
2925 }
2926 }
2927
2928 original_last_aeq = nesqp->last_aeq;
2929 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2930
2931 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2932
2933 ret = 0;
2934
2935
2936 if (issue_modify_qp) {
2937 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07002938 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002939 if (ret)
2940 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2941 " failed for QP%u.\n",
2942 next_iwarp_state, nesqp->hwqp.qp_id);
2943
2944 }
2945
2946 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2947 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2948 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2949 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2950 original_last_aeq, nesqp->last_aeq);
Dan Carpenter349850f2014-02-06 15:48:23 +03002951 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002952 if (dont_wait) {
2953 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2954 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2955 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2956 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2957 original_last_aeq, nesqp->last_aeq);
2958 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002959 spin_lock_irqsave(&nesqp->lock, qplockflags);
2960 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2961 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2962 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2963 nes_cm_disconn(nesqp);
2964 } else {
2965 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2966 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002967 }
2968 } else {
2969 spin_lock_irqsave(&nesqp->lock, qplockflags);
2970 if (nesqp->cm_id) {
2971 /* These two are for the timer thread */
2972 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002973 nesqp->cm_id->add_ref(nesqp->cm_id);
2974 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2975 " need ae to finish up, original_last_aeq = 0x%04X."
2976 " last_aeq = 0x%04X, scheduling timer.\n",
2977 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2978 original_last_aeq, nesqp->last_aeq);
2979 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2980 }
2981 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2982 } else {
2983 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2984 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2985 " need ae to finish up, original_last_aeq = 0x%04X."
2986 " last_aeq = 0x%04X.\n",
2987 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2988 original_last_aeq, nesqp->last_aeq);
2989 }
2990 }
2991 } else {
2992 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2993 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2994 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2995 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002996 }
2997 } else {
2998 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2999 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3000 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3001 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 }
3003
3004 err = 0;
3005
3006 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3007 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3008
3009 return err;
3010}
3011
3012
3013/**
3014 * nes_muticast_attach
3015 */
3016static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3017{
3018 nes_debug(NES_DBG_INIT, "\n");
3019 return -ENOSYS;
3020}
3021
3022
3023/**
3024 * nes_multicast_detach
3025 */
3026static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3027{
3028 nes_debug(NES_DBG_INIT, "\n");
3029 return -ENOSYS;
3030}
3031
3032
3033/**
3034 * nes_process_mad
3035 */
3036static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
Ira Weinya97e2d82015-05-31 17:15:30 -04003037 u8 port_num, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -04003038 const struct ib_mad_hdr *in, size_t in_mad_size,
3039 struct ib_mad_hdr *out, size_t *out_mad_size,
3040 u16 *out_mad_pkey_index)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003041{
3042 nes_debug(NES_DBG_INIT, "\n");
3043 return -ENOSYS;
3044}
3045
3046static inline void
3047fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3048{
3049 int sge_index;
3050 int total_payload_length = 0;
3051 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3052 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3053 ib_wr->sg_list[sge_index].addr);
3054 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3055 ib_wr->sg_list[sge_index].length);
3056 if (uselkey)
3057 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3058 (ib_wr->sg_list[sge_index].lkey));
3059 else
3060 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3061
3062 total_payload_length += ib_wr->sg_list[sge_index].length;
3063 }
3064 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3065 total_payload_length);
3066 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3067 total_payload_length);
3068}
3069
3070/**
3071 * nes_post_send
3072 */
3073static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3074 struct ib_send_wr **bad_wr)
3075{
3076 u64 u64temp;
3077 unsigned long flags = 0;
3078 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3079 struct nes_device *nesdev = nesvnic->nesdev;
3080 struct nes_qp *nesqp = to_nesqp(ibqp);
3081 struct nes_hw_qp_wqe *wqe;
Chien Tung649fe4a2009-12-09 13:51:37 -08003082 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003083 u32 qsize = nesqp->hwqp.sq_size;
3084 u32 head;
Chien Tung649fe4a2009-12-09 13:51:37 -08003085 u32 wqe_misc = 0;
3086 u32 wqe_count = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003087 u32 counter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088
Frank Zagoe5dec392009-12-09 13:51:36 -08003089 if (nesqp->ibqp_state > IB_QPS_RTS) {
3090 err = -EINVAL;
3091 goto out;
3092 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003093
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003094 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003095
3096 head = nesqp->hwqp.sq_head;
3097
3098 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003099 /* Check for QP error */
3100 if (nesqp->term_flags) {
3101 err = -EINVAL;
3102 break;
3103 }
3104
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003105 /* Check for SQ overflow */
3106 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003107 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003108 break;
3109 }
3110
3111 wqe = &nesqp->hwqp.sq_vbase[head];
3112 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3113 nesqp->hwqp.qp_id, wqe, head); */
3114 nes_fill_init_qp_wqe(wqe, nesqp, head);
3115 u64temp = (u64)(ib_wr->wr_id);
3116 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3117 u64temp);
Chien Tung649fe4a2009-12-09 13:51:37 -08003118 switch (ib_wr->opcode) {
3119 case IB_WR_SEND:
3120 case IB_WR_SEND_WITH_INV:
3121 if (IB_WR_SEND == ib_wr->opcode) {
3122 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3123 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3124 else
3125 wqe_misc = NES_IWARP_SQ_OP_SEND;
3126 } else {
3127 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3128 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3129 else
3130 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003131
Chien Tung649fe4a2009-12-09 13:51:37 -08003132 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3133 ib_wr->ex.invalidate_rkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003134 }
3135
Chien Tung649fe4a2009-12-09 13:51:37 -08003136 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3137 err = -EINVAL;
3138 break;
3139 }
3140
3141 if (ib_wr->send_flags & IB_SEND_FENCE)
3142 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3143
3144 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3145 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3146 (ib_wr->sg_list[0].length <= 64)) {
3147 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3148 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3149 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3150 ib_wr->sg_list[0].length);
3151 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3152 } else {
3153 fill_wqe_sg_send(wqe, ib_wr, 1);
3154 }
3155
3156 break;
3157 case IB_WR_RDMA_WRITE:
3158 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3159 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3160 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3161 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3162 err = -EINVAL;
3163 break;
3164 }
3165
3166 if (ib_wr->send_flags & IB_SEND_FENCE)
3167 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3168
3169 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003170 rdma_wr(ib_wr)->rkey);
Chien Tung649fe4a2009-12-09 13:51:37 -08003171 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003172 rdma_wr(ib_wr)->remote_addr);
Chien Tung649fe4a2009-12-09 13:51:37 -08003173
3174 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3175 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3176 (ib_wr->sg_list[0].length <= 64)) {
3177 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3178 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3179 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3180 ib_wr->sg_list[0].length);
3181 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3182 } else {
3183 fill_wqe_sg_send(wqe, ib_wr, 1);
3184 }
3185
3186 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3187 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3188 break;
3189 case IB_WR_RDMA_READ:
3190 case IB_WR_RDMA_READ_WITH_INV:
3191 /* iWARP only supports 1 sge for RDMA reads */
3192 if (ib_wr->num_sge > 1) {
3193 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3194 ib_wr->num_sge);
3195 err = -EINVAL;
3196 break;
3197 }
3198 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3199 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3200 } else {
3201 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3202 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3203 ib_wr->ex.invalidate_rkey);
3204 }
3205
3206 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003207 rdma_wr(ib_wr)->remote_addr);
Chien Tung649fe4a2009-12-09 13:51:37 -08003208 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003209 rdma_wr(ib_wr)->rkey);
Chien Tung649fe4a2009-12-09 13:51:37 -08003210 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3211 ib_wr->sg_list->length);
3212 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3213 ib_wr->sg_list->addr);
3214 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3215 ib_wr->sg_list->lkey);
3216 break;
3217 case IB_WR_LOCAL_INV:
3218 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3219 set_wqe_32bit_value(wqe->wqe_words,
3220 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3221 ib_wr->ex.invalidate_rkey);
3222 break;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03003223 case IB_WR_REG_MR:
3224 {
3225 struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3226 int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3227 int flags = reg_wr(ib_wr)->access;
3228
3229 if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3230 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3231 err = -EINVAL;
3232 break;
3233 }
3234 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3235 set_wqe_64bit_value(wqe->wqe_words,
3236 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3237 mr->ibmr.iova);
3238 set_wqe_32bit_value(wqe->wqe_words,
3239 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3240 mr->ibmr.length);
3241 set_wqe_32bit_value(wqe->wqe_words,
3242 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3243 set_wqe_32bit_value(wqe->wqe_words,
3244 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3245 reg_wr(ib_wr)->key);
3246
3247 if (page_shift == 12) {
3248 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3249 } else if (page_shift == 21) {
3250 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3251 } else {
3252 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3253 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3254 err = -EINVAL;
3255 break;
3256 }
3257
3258 /* Set access_flags */
3259 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3260 if (flags & IB_ACCESS_LOCAL_WRITE)
3261 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3262
3263 if (flags & IB_ACCESS_REMOTE_WRITE)
3264 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3265
3266 if (flags & IB_ACCESS_REMOTE_READ)
3267 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3268
3269 if (flags & IB_ACCESS_MW_BIND)
3270 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3271
3272 /* Fill in PBL info: */
3273 set_wqe_64bit_value(wqe->wqe_words,
3274 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3275 mr->paddr);
3276
3277 set_wqe_32bit_value(wqe->wqe_words,
3278 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3279 mr->npages * 8);
3280
3281 nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3282 "length: %d, rkey: %0x, pgl_paddr: %llx, "
3283 "page_list_len: %u, wqe_misc: %x\n",
3284 (unsigned long long) mr->ibmr.iova,
3285 mr->ibmr.length,
3286 reg_wr(ib_wr)->key,
3287 (unsigned long long) mr->paddr,
3288 mr->npages,
3289 wqe_misc);
3290 break;
3291 }
Chien Tung649fe4a2009-12-09 13:51:37 -08003292 default:
3293 /* error */
3294 err = -EINVAL;
3295 break;
3296 }
3297
Frank Zagoe5dec392009-12-09 13:51:36 -08003298 if (err)
3299 break;
3300
Chien Tungd14152d2009-12-09 15:21:56 -08003301 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003302 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
Chien Tungd14152d2009-12-09 15:21:56 -08003303
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003304 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3305
3306 ib_wr = ib_wr->next;
3307 head++;
3308 wqe_count++;
3309 if (head >= qsize)
3310 head = 0;
3311
3312 }
3313
3314 nesqp->hwqp.sq_head = head;
3315 barrier();
3316 while (wqe_count) {
3317 counter = min(wqe_count, ((u32)255));
3318 wqe_count -= counter;
3319 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3320 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3321 }
3322
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003323 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003324
Frank Zagoe5dec392009-12-09 13:51:36 -08003325out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003326 if (err)
3327 *bad_wr = ib_wr;
3328 return err;
3329}
3330
3331
3332/**
3333 * nes_post_recv
3334 */
3335static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3336 struct ib_recv_wr **bad_wr)
3337{
3338 u64 u64temp;
3339 unsigned long flags = 0;
3340 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3341 struct nes_device *nesdev = nesvnic->nesdev;
3342 struct nes_qp *nesqp = to_nesqp(ibqp);
3343 struct nes_hw_qp_wqe *wqe;
3344 int err = 0;
3345 int sge_index;
3346 u32 qsize = nesqp->hwqp.rq_size;
3347 u32 head;
3348 u32 wqe_count = 0;
3349 u32 counter;
3350 u32 total_payload_length;
3351
Frank Zago4293fdc12009-12-09 13:51:36 -08003352 if (nesqp->ibqp_state > IB_QPS_RTS) {
3353 err = -EINVAL;
3354 goto out;
3355 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003356
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003357 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003358
3359 head = nesqp->hwqp.rq_head;
3360
3361 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003362 /* Check for QP error */
3363 if (nesqp->term_flags) {
3364 err = -EINVAL;
3365 break;
3366 }
3367
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003368 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3369 err = -EINVAL;
3370 break;
3371 }
3372 /* Check for RQ overflow */
3373 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003374 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003375 break;
3376 }
3377
3378 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3379 wqe = &nesqp->hwqp.rq_vbase[head];
3380
3381 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3382 nesqp->hwqp.qp_id, wqe, head); */
3383 nes_fill_init_qp_wqe(wqe, nesqp, head);
3384 u64temp = (u64)(ib_wr->wr_id);
3385 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3386 u64temp);
3387 total_payload_length = 0;
3388 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3389 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3390 ib_wr->sg_list[sge_index].addr);
3391 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3392 ib_wr->sg_list[sge_index].length);
3393 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3394 ib_wr->sg_list[sge_index].lkey);
3395
3396 total_payload_length += ib_wr->sg_list[sge_index].length;
3397 }
3398 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3399 total_payload_length);
3400
3401 ib_wr = ib_wr->next;
3402 head++;
3403 wqe_count++;
3404 if (head >= qsize)
3405 head = 0;
3406 }
3407
3408 nesqp->hwqp.rq_head = head;
3409 barrier();
3410 while (wqe_count) {
3411 counter = min(wqe_count, ((u32)255));
3412 wqe_count -= counter;
3413 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3414 }
3415
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003416 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003417
Frank Zago4293fdc12009-12-09 13:51:36 -08003418out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003419 if (err)
3420 *bad_wr = ib_wr;
3421 return err;
3422}
3423
Tatyana Nikolova19974122016-03-29 12:58:37 -05003424/**
3425 * nes_drain_sq - drain sq
3426 * @ibqp: pointer to ibqp
3427 */
3428static void nes_drain_sq(struct ib_qp *ibqp)
3429{
3430 struct nes_qp *nesqp = to_nesqp(ibqp);
3431
3432 if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3433 wait_for_completion(&nesqp->sq_drained);
3434}
3435
3436/**
3437 * nes_drain_rq - drain rq
3438 * @ibqp: pointer to ibqp
3439 */
3440static void nes_drain_rq(struct ib_qp *ibqp)
3441{
3442 struct nes_qp *nesqp = to_nesqp(ibqp);
3443
3444 if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3445 wait_for_completion(&nesqp->rq_drained);
3446}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003447
3448/**
3449 * nes_poll_cq
3450 */
3451static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3452{
3453 u64 u64temp;
3454 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455 unsigned long flags = 0;
3456 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3457 struct nes_device *nesdev = nesvnic->nesdev;
3458 struct nes_cq *nescq = to_nescq(ibcq);
3459 struct nes_qp *nesqp;
3460 struct nes_hw_cqe cqe;
3461 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003462 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003463 u32 cq_size;
3464 u32 cqe_count = 0;
3465 u32 wqe_index;
3466 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003467 u32 move_cq_head = 1;
3468 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003469
3470 nes_debug(NES_DBG_CQ, "\n");
3471
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003472 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003473
3474 head = nescq->hw_cq.cq_head;
3475 cq_size = nescq->hw_cq.cq_size;
3476
3477 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003478 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3479 NES_CQE_VALID) == 0)
3480 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003481
Don Wood5ee21fe2009-09-05 20:36:37 -07003482 /*
3483 * Make sure we read CQ entry contents *after*
3484 * we've checked the valid bit.
3485 */
3486 rmb();
3487
3488 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003489 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3490 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3491 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3492 /* parse CQE, get completion context from WQE (either rq or sq) */
3493 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3494 ((u64)u32temp);
3495
3496 if (u64temp) {
3497 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003498 memset(entry, 0, sizeof *entry);
3499 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3500 entry->status = IB_WC_SUCCESS;
3501 } else {
Don Wood320cdfd2009-09-05 20:36:39 -07003502 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3503 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3504 entry->status = err_code & 0x0000ffff;
3505
3506 /* The rest of the cqe's will be marked as flushed */
3507 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3508 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3509 NES_IWARP_CQE_MINOR_FLUSH);
3510 } else
3511 entry->status = IB_WC_WR_FLUSH_ERR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512 }
3513
3514 entry->qp = &nesqp->ibqp;
3515 entry->src_qp = nesqp->hwqp.qp_id;
3516
3517 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3518 if (nesqp->skip_lsmm) {
3519 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003520 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003521 }
3522
3523 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003524 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003525 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003526 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003527 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003528 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003529 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3530
Don Wood6eed5e72009-09-05 20:36:39 -07003531 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003532 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3533 case NES_IWARP_SQ_OP_RDMAW:
3534 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3535 entry->opcode = IB_WC_RDMA_WRITE;
3536 break;
3537 case NES_IWARP_SQ_OP_RDMAR:
3538 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3539 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003540 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003541 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3542 break;
3543 case NES_IWARP_SQ_OP_SENDINV:
3544 case NES_IWARP_SQ_OP_SENDSEINV:
3545 case NES_IWARP_SQ_OP_SEND:
3546 case NES_IWARP_SQ_OP_SENDSE:
3547 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3548 entry->opcode = IB_WC_SEND;
3549 break;
Chien Tung649fe4a2009-12-09 13:51:37 -08003550 case NES_IWARP_SQ_OP_LOCINV:
Tatyana Nikolova4a4b03f2012-01-06 18:17:19 -06003551 entry->opcode = IB_WC_LOCAL_INV;
Chien Tung649fe4a2009-12-09 13:51:37 -08003552 break;
Chien Tunge293a262009-12-09 15:21:54 -08003553 case NES_IWARP_SQ_OP_FAST_REG:
Sagi Grimberg13d3e892015-10-13 19:11:48 +03003554 entry->opcode = IB_WC_REG_MR;
Chien Tunge293a262009-12-09 15:21:54 -08003555 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003556 }
Don Wood6eed5e72009-09-05 20:36:39 -07003557
3558 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3559 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3560 move_cq_head = 0;
3561 wq_tail = nesqp->hwqp.sq_tail;
3562 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003563 } else {
3564 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003565 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003566 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3567 ((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 -08003568 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003569
3570 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3571 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3572 move_cq_head = 0;
3573 wq_tail = nesqp->hwqp.rq_tail;
3574 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003575 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003576
Tatyana Nikolova19974122016-03-29 12:58:37 -05003577 if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3578 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3579 complete(&nesqp->sq_drained);
3580 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3581 complete(&nesqp->rq_drained);
3582 }
3583
Don Wood5ee21fe2009-09-05 20:36:37 -07003584 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003585 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003586 cqe_count++;
3587 }
3588
Don Wood6eed5e72009-09-05 20:36:39 -07003589 if (move_cq_head) {
3590 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3591 if (++head >= cq_size)
3592 head = 0;
3593 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003594
Don Wood6eed5e72009-09-05 20:36:39 -07003595 if ((nescq->polled_completions > (cq_size / 2)) ||
3596 (nescq->polled_completions == 255)) {
3597 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003598 " are pending %u of %u.\n",
3599 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003600 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003601 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003602 nescq->polled_completions = 0;
3603 }
3604 } else {
3605 /* Update the wqe index and set status to flush */
3606 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3607 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3608 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3609 cpu_to_le32(wqe_index);
3610 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003611 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003612 }
3613
3614 if (nescq->polled_completions) {
3615 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3616 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3617 nescq->polled_completions = 0;
3618 }
3619
3620 nescq->hw_cq.cq_head = head;
3621 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3622 cqe_count, nescq->hw_cq.cq_number);
3623
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003624 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625
3626 return cqe_count;
3627}
3628
3629
3630/**
3631 * nes_req_notify_cq
3632 */
3633static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3634 {
3635 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3636 struct nes_device *nesdev = nesvnic->nesdev;
3637 struct nes_cq *nescq = to_nescq(ibcq);
3638 u32 cq_arm;
3639
3640 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3641 nescq->hw_cq.cq_number);
3642
3643 cq_arm = nescq->hw_cq.cq_number;
3644 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3645 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3646 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3647 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3648 else
3649 return -EINVAL;
3650
3651 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3652 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3653
3654 return 0;
3655}
3656
Ira Weiny77386132015-05-13 20:02:58 -04003657static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3658 struct ib_port_immutable *immutable)
3659{
3660 struct ib_port_attr attr;
3661 int err;
3662
Or Gerlitzc4550c62017-01-24 13:02:39 +02003663 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3664
Ira Weiny77386132015-05-13 20:02:58 -04003665 err = nes_query_port(ibdev, port_num, &attr);
3666 if (err)
3667 return err;
3668
3669 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3670 immutable->gid_tbl_len = attr.gid_tbl_len;
3671
3672 return 0;
3673}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003674
Ira Weiny96357452016-06-15 02:22:03 -04003675static void get_dev_fw_str(struct ib_device *dev, char *str,
3676 size_t str_len)
3677{
3678 struct nes_ib_device *nesibdev =
3679 container_of(dev, struct nes_ib_device, ibdev);
3680 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3681
3682 nes_debug(NES_DBG_INIT, "\n");
3683 snprintf(str, str_len, "%u.%u",
3684 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3685 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3686}
3687
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003688/**
3689 * nes_init_ofa_device
3690 */
3691struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3692{
3693 struct nes_ib_device *nesibdev;
3694 struct nes_vnic *nesvnic = netdev_priv(netdev);
3695 struct nes_device *nesdev = nesvnic->nesdev;
3696
3697 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3698 if (nesibdev == NULL) {
3699 return NULL;
3700 }
3701 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3702 nesibdev->ibdev.owner = THIS_MODULE;
3703
3704 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3705 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3706 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3707
3708 nesibdev->ibdev.uverbs_cmd_mask =
3709 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3710 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3711 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3712 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3713 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3714 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3715 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3716 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3717 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3718 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3719 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3720 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3721 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3722 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3723 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3724 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3725 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3726 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3727 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3728 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3729 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3730 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3731
3732 nesibdev->ibdev.phys_port_cnt = 1;
3733 nesibdev->ibdev.num_comp_vectors = 1;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003734 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003735 nesibdev->ibdev.query_device = nes_query_device;
3736 nesibdev->ibdev.query_port = nes_query_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003737 nesibdev->ibdev.query_pkey = nes_query_pkey;
3738 nesibdev->ibdev.query_gid = nes_query_gid;
3739 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3740 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3741 nesibdev->ibdev.mmap = nes_mmap;
3742 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3743 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3744 nesibdev->ibdev.create_ah = nes_create_ah;
3745 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3746 nesibdev->ibdev.create_qp = nes_create_qp;
3747 nesibdev->ibdev.modify_qp = nes_modify_qp;
3748 nesibdev->ibdev.query_qp = nes_query_qp;
3749 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3750 nesibdev->ibdev.create_cq = nes_create_cq;
3751 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3752 nesibdev->ibdev.poll_cq = nes_poll_cq;
3753 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003754 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3755 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3756 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3757 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003758
Sagi Grimberge02e4d552015-07-30 10:32:46 +03003759 nesibdev->ibdev.alloc_mr = nes_alloc_mr;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03003760 nesibdev->ibdev.map_mr_sg = nes_map_mr_sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003761
3762 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3763 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3764 nesibdev->ibdev.process_mad = nes_process_mad;
3765
3766 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3767 nesibdev->ibdev.post_send = nes_post_send;
3768 nesibdev->ibdev.post_recv = nes_post_recv;
Tatyana Nikolova19974122016-03-29 12:58:37 -05003769 nesibdev->ibdev.drain_sq = nes_drain_sq;
3770 nesibdev->ibdev.drain_rq = nes_drain_rq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003771
3772 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3773 if (nesibdev->ibdev.iwcm == NULL) {
3774 ib_dealloc_device(&nesibdev->ibdev);
3775 return NULL;
3776 }
3777 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3778 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3779 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3780 nesibdev->ibdev.iwcm->connect = nes_connect;
3781 nesibdev->ibdev.iwcm->accept = nes_accept;
3782 nesibdev->ibdev.iwcm->reject = nes_reject;
3783 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3784 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
Ira Weiny77386132015-05-13 20:02:58 -04003785 nesibdev->ibdev.get_port_immutable = nes_port_immutable;
Ira Weiny96357452016-06-15 02:22:03 -04003786 nesibdev->ibdev.get_dev_fw_str = get_dev_fw_str;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003787 memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3788 sizeof(nesibdev->ibdev.iwcm->ifname));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003789
3790 return nesibdev;
3791}
3792
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003793
3794/**
3795 * nes_handle_delayed_event
3796 */
3797static void nes_handle_delayed_event(unsigned long data)
3798{
3799 struct nes_vnic *nesvnic = (void *) data;
3800
3801 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3802 struct ib_event event;
3803
3804 event.device = &nesvnic->nesibdev->ibdev;
3805 if (!event.device)
3806 goto stop_timer;
3807 event.event = nesvnic->delayed_event;
3808 event.element.port_num = nesvnic->logical_port + 1;
3809 ib_dispatch_event(&event);
3810 }
3811
3812stop_timer:
3813 nesvnic->event_timer.function = NULL;
3814}
3815
3816
Faisal Latifcd6860e2010-07-04 00:17:59 +00003817void nes_port_ibevent(struct nes_vnic *nesvnic)
3818{
3819 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3820 struct nes_device *nesdev = nesvnic->nesdev;
3821 struct ib_event event;
3822 event.device = &nesibdev->ibdev;
3823 event.element.port_num = nesvnic->logical_port + 1;
3824 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003825
3826 if (!nesvnic->event_timer.function) {
3827 ib_dispatch_event(&event);
3828 nesvnic->last_dispatched_event = event.event;
3829 nesvnic->event_timer.function = nes_handle_delayed_event;
3830 nesvnic->event_timer.data = (unsigned long) nesvnic;
3831 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3832 add_timer(&nesvnic->event_timer);
3833 } else {
3834 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3835 }
3836 nesvnic->delayed_event = event.event;
Faisal Latifcd6860e2010-07-04 00:17:59 +00003837}
3838
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003839
3840/**
3841 * nes_destroy_ofa_device
3842 */
3843void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3844{
3845 if (nesibdev == NULL)
3846 return;
3847
3848 nes_unregister_ofa_device(nesibdev);
3849
3850 kfree(nesibdev->ibdev.iwcm);
3851 ib_dealloc_device(&nesibdev->ibdev);
3852}
3853
3854
3855/**
3856 * nes_register_ofa_device
3857 */
3858int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3859{
3860 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3861 struct nes_device *nesdev = nesvnic->nesdev;
3862 struct nes_adapter *nesadapter = nesdev->nesadapter;
3863 int i, ret;
3864
Ralph Campbell9a6edb62010-05-06 17:03:25 -07003865 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003866 if (ret) {
3867 return ret;
3868 }
3869
3870 /* Get the resources allocated to this device */
3871 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3872 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3873 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3874 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3875
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003876 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3877 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003878 if (ret) {
3879 while (i > 0) {
3880 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003881 device_remove_file(&nesibdev->ibdev.dev,
3882 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003883 }
3884 ib_unregister_device(&nesibdev->ibdev);
3885 return ret;
3886 }
3887 }
3888
3889 nesvnic->of_device_registered = 1;
3890
3891 return 0;
3892}
3893
3894
3895/**
3896 * nes_unregister_ofa_device
3897 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003898static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003899{
3900 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3901 int i;
3902
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003903 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3904 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003905 }
3906
3907 if (nesvnic->of_device_registered) {
3908 ib_unregister_device(&nesibdev->ibdev);
3909 }
3910
3911 nesvnic->of_device_registered = 0;
3912}