blob: 7394224e99b2845bf503f881384a5d7d4d904b0d [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
405static int nes_map_mr_sg(struct ib_mr *ibmr,
406 struct scatterlist *sg,
407 int sg_nents)
408{
409 struct nes_mr *nesmr = to_nesmr(ibmr);
410
411 nesmr->npages = 0;
412
413 return ib_sg_to_pages(ibmr, sg, sg_nents, nes_set_page);
Chien Tunge293a262009-12-09 15:21:54 -0800414}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416/**
417 * nes_query_device
418 */
Matan Barak2528e332015-06-11 16:35:25 +0300419static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
420 struct ib_udata *uhw)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800421{
422 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
423 struct nes_device *nesdev = nesvnic->nesdev;
424 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
425
Matan Barak2528e332015-06-11 16:35:25 +0300426 if (uhw->inlen || uhw->outlen)
427 return -EINVAL;
428
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800429 memset(props, 0, sizeof(*props));
430 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
431
Miroslaw Walukiewiczff0380c2010-07-15 14:53:29 +0000432 props->fw_ver = nesdev->nesadapter->firmware_version;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800433 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
434 props->vendor_id = nesdev->nesadapter->vendor_id;
435 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
436 props->hw_ver = nesdev->nesadapter->hw_rev;
437 props->max_mr_size = 0x80000000;
438 props->max_qp = nesibdev->max_qp;
439 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
440 props->max_sge = nesdev->nesadapter->max_sge;
441 props->max_cq = nesibdev->max_cq;
Chien Tung5924aea2009-12-09 15:21:56 -0800442 props->max_cqe = nesdev->nesadapter->max_cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800443 props->max_mr = nesibdev->max_mr;
444 props->max_mw = nesibdev->max_mr;
445 props->max_pd = nesibdev->max_pd;
446 props->max_sge_rd = 1;
447 switch (nesdev->nesadapter->max_irrq_wr) {
448 case 0:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800449 props->max_qp_rd_atom = 2;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800450 break;
451 case 1:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800452 props->max_qp_rd_atom = 8;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800453 break;
454 case 2:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800455 props->max_qp_rd_atom = 32;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800456 break;
457 case 3:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800458 props->max_qp_rd_atom = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800459 break;
460 default:
461 props->max_qp_rd_atom = 0;
462 }
Faisal Latif66388d62009-06-22 22:52:30 -0700463 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800464 props->atomic_cap = IB_ATOMIC_NONE;
465 props->max_map_per_fmr = 1;
466
467 return 0;
468}
469
470
471/**
472 * nes_query_port
473 */
474static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
475{
Chien Tungcd1d3f72009-09-05 20:36:39 -0700476 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
477 struct net_device *netdev = nesvnic->netdev;
478
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479 memset(props, 0, sizeof(*props));
480
Chien Tungcd1d3f72009-09-05 20:36:39 -0700481 props->max_mtu = IB_MTU_4096;
482
483 if (netdev->mtu >= 4096)
484 props->active_mtu = IB_MTU_4096;
485 else if (netdev->mtu >= 2048)
486 props->active_mtu = IB_MTU_2048;
487 else if (netdev->mtu >= 1024)
488 props->active_mtu = IB_MTU_1024;
489 else if (netdev->mtu >= 512)
490 props->active_mtu = IB_MTU_512;
491 else
492 props->active_mtu = IB_MTU_256;
493
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800494 props->lid = 1;
495 props->lmc = 0;
496 props->sm_lid = 0;
497 props->sm_sl = 0;
Chien Tung29327722010-08-23 14:32:36 +0000498 if (netif_queue_stopped(netdev))
499 props->state = IB_PORT_DOWN;
500 else if (nesvnic->linkup)
Chien Tungcd1d3f72009-09-05 20:36:39 -0700501 props->state = IB_PORT_ACTIVE;
502 else
503 props->state = IB_PORT_DOWN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800504 props->phys_state = 0;
505 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
506 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
507 props->gid_tbl_len = 1;
508 props->pkey_tbl_len = 1;
509 props->qkey_viol_cntr = 0;
510 props->active_width = IB_WIDTH_4X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200511 props->active_speed = IB_SPEED_SDR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800512 props->max_msg_sz = 0x80000000;
513
514 return 0;
515}
516
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800517/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800518 * nes_query_pkey
519 */
520static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
521{
522 *pkey = 0;
523 return 0;
524}
525
526
527/**
528 * nes_query_gid
529 */
530static int nes_query_gid(struct ib_device *ibdev, u8 port,
531 int index, union ib_gid *gid)
532{
533 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
534
535 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
536 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
537
538 return 0;
539}
540
541
542/**
543 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
544 * of all objects associated with a particular user-mode client.
545 */
546static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
547 struct ib_udata *udata)
548{
549 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
550 struct nes_device *nesdev = nesvnic->nesdev;
551 struct nes_adapter *nesadapter = nesdev->nesadapter;
552 struct nes_alloc_ucontext_req req;
553 struct nes_alloc_ucontext_resp uresp;
554 struct nes_ucontext *nes_ucontext;
555 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
556
557
558 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
559 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
560 return ERR_PTR(-EINVAL);
561 }
562
563 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
564 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
565 req.userspace_ver, NES_ABI_USERSPACE_VER);
566 return ERR_PTR(-EINVAL);
567 }
568
569
570 memset(&uresp, 0, sizeof uresp);
571
572 uresp.max_qps = nesibdev->max_qp;
573 uresp.max_pds = nesibdev->max_pd;
574 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
575 uresp.virtwq = nesadapter->virtwq;
576 uresp.kernel_ver = NES_ABI_KERNEL_VER;
577
578 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
579 if (!nes_ucontext)
580 return ERR_PTR(-ENOMEM);
581
582 nes_ucontext->nesdev = nesdev;
583 nes_ucontext->mmap_wq_offset = uresp.max_pds;
584 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
585 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
586 PAGE_SIZE;
587
588
589 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
590 kfree(nes_ucontext);
591 return ERR_PTR(-EFAULT);
592 }
593
594 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
595 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
596 atomic_set(&nes_ucontext->usecnt, 1);
597 return &nes_ucontext->ibucontext;
598}
599
600
601/**
602 * nes_dealloc_ucontext
603 */
604static int nes_dealloc_ucontext(struct ib_ucontext *context)
605{
606 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
607 /* struct nes_device *nesdev = nesvnic->nesdev; */
608 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
609
610 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
611 return 0;
612 kfree(nes_ucontext);
613 return 0;
614}
615
616
617/**
618 * nes_mmap
619 */
620static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
621{
622 unsigned long index;
623 struct nes_vnic *nesvnic = to_nesvnic(context->device);
624 struct nes_device *nesdev = nesvnic->nesdev;
625 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
626 struct nes_ucontext *nes_ucontext;
627 struct nes_qp *nesqp;
628
629 nes_ucontext = to_nesucontext(context);
630
631
632 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
633 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
634 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
635 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
636 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
637 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
638 return -EFAULT;
639 }
640 nesqp = nes_ucontext->mmap_nesqp[index];
641 if (nesqp == NULL) {
642 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
643 return -EFAULT;
644 }
645 if (remap_pfn_range(vma, vma->vm_start,
646 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
647 vma->vm_end - vma->vm_start,
648 vma->vm_page_prot)) {
649 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
650 return -EAGAIN;
651 }
652 vma->vm_private_data = nesqp;
653 return 0;
654 } else {
655 index = vma->vm_pgoff;
656 if (!test_bit(index, nes_ucontext->allocated_doorbells))
657 return -EFAULT;
658
659 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
660 if (io_remap_pfn_range(vma, vma->vm_start,
661 (nesdev->doorbell_start +
662 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
663 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
664 return -EAGAIN;
665 vma->vm_private_data = nes_ucontext;
666 return 0;
667 }
668
669 return -ENOSYS;
670}
671
672
673/**
674 * nes_alloc_pd
675 */
676static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
677 struct ib_ucontext *context, struct ib_udata *udata)
678{
679 struct nes_pd *nespd;
680 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
681 struct nes_device *nesdev = nesvnic->nesdev;
682 struct nes_adapter *nesadapter = nesdev->nesadapter;
683 struct nes_ucontext *nesucontext;
684 struct nes_alloc_pd_resp uresp;
685 u32 pd_num = 0;
686 int err;
687
688 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
689 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
Eric Dumazet29b44332010-10-11 10:22:12 +0000690 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691
692 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000693 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800694 if (err) {
695 return ERR_PTR(err);
696 }
697
698 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
699 if (!nespd) {
700 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
701 return ERR_PTR(-ENOMEM);
702 }
703
704 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
705 nespd, nesvnic->nesibdev->ibdev.name);
706
707 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
708
709 if (context) {
710 nesucontext = to_nesucontext(context);
711 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
712 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
713 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
714 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600715 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800716 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
717 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
718 kfree(nespd);
719 return ERR_PTR(-ENOMEM);
720 }
721
722 uresp.pd_id = nespd->pd_id;
723 uresp.mmap_db_index = nespd->mmap_db_index;
724 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
725 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
726 kfree(nespd);
727 return ERR_PTR(-EFAULT);
728 }
729
730 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
731 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
732 nesucontext->first_free_db = nespd->mmap_db_index + 1;
733 }
734
735 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
736 return &nespd->ibpd;
737}
738
739
740/**
741 * nes_dealloc_pd
742 */
743static int nes_dealloc_pd(struct ib_pd *ibpd)
744{
745 struct nes_ucontext *nesucontext;
746 struct nes_pd *nespd = to_nespd(ibpd);
747 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
748 struct nes_device *nesdev = nesvnic->nesdev;
749 struct nes_adapter *nesadapter = nesdev->nesadapter;
750
751 if ((ibpd->uobject) && (ibpd->uobject->context)) {
752 nesucontext = to_nesucontext(ibpd->uobject->context);
753 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
754 nespd->mmap_db_index);
755 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
756 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
757 if (nesucontext->first_free_db > nespd->mmap_db_index) {
758 nesucontext->first_free_db = nespd->mmap_db_index;
759 }
760 }
761
762 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
763 nespd->pd_id, nespd);
764 nes_free_resource(nesadapter, nesadapter->allocated_pds,
765 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
766 kfree(nespd);
767
768 return 0;
769}
770
771
772/**
773 * nes_create_ah
774 */
775static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
776{
777 return ERR_PTR(-ENOSYS);
778}
779
780
781/**
782 * nes_destroy_ah
783 */
784static int nes_destroy_ah(struct ib_ah *ah)
785{
786 return -ENOSYS;
787}
788
789
790/**
791 * nes_get_encoded_size
792 */
793static inline u8 nes_get_encoded_size(int *size)
794{
795 u8 encoded_size = 0;
796 if (*size <= 32) {
797 *size = 32;
798 encoded_size = 1;
799 } else if (*size <= 128) {
800 *size = 128;
801 encoded_size = 2;
802 } else if (*size <= 512) {
803 *size = 512;
804 encoded_size = 3;
805 }
806 return (encoded_size);
807}
808
809
810
811/**
812 * nes_setup_virt_qp
813 */
814static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
815 struct nes_vnic *nesvnic, int sq_size, int rq_size)
816{
817 unsigned long flags;
818 void *mem;
819 __le64 *pbl = NULL;
820 __le64 *tpbl;
821 __le64 *pblbuffer;
822 struct nes_device *nesdev = nesvnic->nesdev;
823 struct nes_adapter *nesadapter = nesdev->nesadapter;
824 u32 pbl_entries;
825 u8 rq_pbl_entries;
826 u8 sq_pbl_entries;
827
828 pbl_entries = nespbl->pbl_size >> 3;
Roland Dreier71e09572008-04-16 21:01:09 -0700829 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 -0800830 nespbl->pbl_size, pbl_entries,
831 (void *)nespbl->pbl_vbase,
Roland Dreier71e09572008-04-16 21:01:09 -0700832 (unsigned long) nespbl->pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800833 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
834 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
835 /* the first pbl to be fro the rq_vbase... */
836 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
837 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
838 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
839 if (!nespbl->page) {
840 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
841 kfree(nespbl);
842 return -ENOMEM;
843 }
844
845 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
846 nesqp->page = nespbl->page;
847 if (!nesqp->hwqp.sq_vbase) {
848 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
849 kfree(nespbl);
850 return -ENOMEM;
851 }
852
853 /* Now to get to sq.. we need to calculate how many */
854 /* PBL entries were used by the rq.. */
855 pbl += sq_pbl_entries;
856 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
857 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
858 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
859
Roland Dreier71e09572008-04-16 21:01:09 -0700860 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
861 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
862 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
864 if (!nesadapter->free_256pbl) {
865 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
866 nespbl->pbl_pbase);
867 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
868 kunmap(nesqp->page);
869 kfree(nespbl);
870 return -ENOMEM;
871 }
872 nesadapter->free_256pbl--;
873 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
874
875 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
876 pblbuffer = nesqp->pbl_vbase;
877 if (!nesqp->pbl_vbase) {
878 /* memory allocated during nes_reg_user_mr() */
879 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
880 nespbl->pbl_pbase);
881 kfree(nespbl);
882 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
883 nesadapter->free_256pbl++;
884 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
885 kunmap(nesqp->page);
886 return -ENOMEM;
887 }
888 memset(nesqp->pbl_vbase, 0, 256);
889 /* fill in the page address in the pbl buffer.. */
890 tpbl = pblbuffer + 16;
891 pbl = (__le64 *)nespbl->pbl_vbase;
892 while (sq_pbl_entries--)
893 *tpbl++ = *pbl++;
894 tpbl = pblbuffer;
895 while (rq_pbl_entries--)
896 *tpbl++ = *pbl++;
897
898 /* done with memory allocated during nes_reg_user_mr() */
899 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
900 nespbl->pbl_pbase);
901 kfree(nespbl);
902
903 nesqp->qp_mem_size =
904 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
905 /* Round up to a multiple of a page */
906 nesqp->qp_mem_size += PAGE_SIZE - 1;
907 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
908
909 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
910 &nesqp->hwqp.q2_pbase);
911
912 if (!mem) {
913 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
914 nesqp->pbl_vbase = NULL;
915 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
916 nesadapter->free_256pbl++;
917 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
918 kunmap(nesqp->page);
919 return -ENOMEM;
920 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -0800921 nesqp->sq_kmapped = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800922 nesqp->hwqp.q2_vbase = mem;
923 mem += 256;
924 memset(nesqp->hwqp.q2_vbase, 0, 256);
925 nesqp->nesqp_context = mem;
926 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
927 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
928
929 return 0;
930}
931
932
933/**
934 * nes_setup_mmap_qp
935 */
936static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
937 int sq_size, int rq_size)
938{
939 void *mem;
940 struct nes_device *nesdev = nesvnic->nesdev;
941
942 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
943 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
944 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
945 256; /* this is Q2 */
946 /* Round up to a multiple of a page */
947 nesqp->qp_mem_size += PAGE_SIZE - 1;
948 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
949
950 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
951 &nesqp->hwqp.sq_pbase);
952 if (!mem)
953 return -ENOMEM;
954 nes_debug(NES_DBG_QP, "PCI consistent memory for "
955 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
956 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
957
958 memset(mem, 0, nesqp->qp_mem_size);
959
960 nesqp->hwqp.sq_vbase = mem;
961 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
962
963 nesqp->hwqp.rq_vbase = mem;
964 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
965 sizeof(struct nes_hw_qp_wqe) * sq_size;
966 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
967
968 nesqp->hwqp.q2_vbase = mem;
969 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
970 sizeof(struct nes_hw_qp_wqe) * rq_size;
971 mem += 256;
972 memset(nesqp->hwqp.q2_vbase, 0, 256);
973
974 nesqp->nesqp_context = mem;
975 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
976 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
977 return 0;
978}
979
980
981/**
982 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
983 */
984static inline void nes_free_qp_mem(struct nes_device *nesdev,
985 struct nes_qp *nesqp, int virt_wqs)
986{
987 unsigned long flags;
988 struct nes_adapter *nesadapter = nesdev->nesadapter;
989 if (!virt_wqs) {
990 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
991 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
992 }else {
993 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
994 nesadapter->free_256pbl++;
995 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
996 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
997 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
998 nesqp->pbl_vbase = NULL;
Faisal Latifd2fa9b262009-12-09 15:54:28 -0800999 if (nesqp->sq_kmapped) {
1000 nesqp->sq_kmapped = 0;
1001 kunmap(nesqp->page);
1002 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001003 }
1004}
1005
1006
1007/**
1008 * nes_create_qp
1009 */
1010static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1011 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1012{
1013 u64 u64temp= 0;
1014 u64 u64nesqp = 0;
1015 struct nes_pd *nespd = to_nespd(ibpd);
1016 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1017 struct nes_device *nesdev = nesvnic->nesdev;
1018 struct nes_adapter *nesadapter = nesdev->nesadapter;
1019 struct nes_qp *nesqp;
1020 struct nes_cq *nescq;
1021 struct nes_ucontext *nes_ucontext;
1022 struct nes_hw_cqp_wqe *cqp_wqe;
1023 struct nes_cqp_request *cqp_request;
1024 struct nes_create_qp_req req;
1025 struct nes_create_qp_resp uresp;
1026 struct nes_pbl *nespbl = NULL;
1027 u32 qp_num = 0;
1028 u32 opcode = 0;
1029 /* u32 counter = 0; */
1030 void *mem;
1031 unsigned long flags;
1032 int ret;
1033 int err;
1034 int virt_wqs = 0;
1035 int sq_size;
1036 int rq_size;
1037 u8 sq_encoded_size;
1038 u8 rq_encoded_size;
1039 /* int counter; */
1040
Eli Cohenb846f252008-04-16 21:09:27 -07001041 if (init_attr->create_flags)
1042 return ERR_PTR(-EINVAL);
1043
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001044 atomic_inc(&qps_created);
1045 switch (init_attr->qp_type) {
1046 case IB_QPT_RC:
1047 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1048 init_attr->cap.max_inline_data = 0;
1049 } else {
1050 init_attr->cap.max_inline_data = 64;
1051 }
1052 sq_size = init_attr->cap.max_send_wr;
1053 rq_size = init_attr->cap.max_recv_wr;
1054
Glenn Streiff7495ab62008-04-29 13:46:54 -07001055 /* check if the encoded sizes are OK or not... */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001056 sq_encoded_size = nes_get_encoded_size(&sq_size);
1057 rq_encoded_size = nes_get_encoded_size(&rq_size);
1058
1059 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1060 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1061 rq_size, sq_size);
1062 return ERR_PTR(-EINVAL);
1063 }
1064
1065 init_attr->cap.max_send_wr = sq_size -2;
1066 init_attr->cap.max_recv_wr = rq_size -1;
1067 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1068
1069 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001070 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071 if (ret) {
1072 return ERR_PTR(ret);
1073 }
1074
1075 /* Need 512 (actually now 1024) byte alignment on this structure */
1076 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1077 if (!mem) {
1078 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1079 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1080 return ERR_PTR(-ENOMEM);
1081 }
1082 u64nesqp = (unsigned long)mem;
1083 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1084 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1085 u64nesqp &= ~u64temp;
1086 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1087 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1088 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1089 nesqp->allocated_buffer = mem;
1090
1091 if (udata) {
1092 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1093 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1094 kfree(nesqp->allocated_buffer);
1095 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
Yann Droneaud9d194d12014-03-10 23:06:27 +01001096 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001097 }
1098 if (req.user_wqe_buffers) {
1099 virt_wqs = 1;
1100 }
Tatyana Nikolova43adff32014-03-11 14:20:17 -05001101 if (req.user_qp_buffer)
1102 nesqp->nesuqp_addr = req.user_qp_buffer;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001103 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1104 nesqp->user_mode = 1;
1105 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1106 if (virt_wqs) {
1107 err = 1;
1108 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1109 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1110 list_del(&nespbl->list);
1111 err = 0;
1112 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1113 nespbl, nespbl->user_base);
1114 break;
1115 }
1116 }
1117 if (err) {
1118 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1119 (long long unsigned int)req.user_wqe_buffers);
1120 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1121 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001122 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001123 }
1124 }
1125
1126 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1127 nesqp->mmap_sq_db_index =
1128 find_next_zero_bit(nes_ucontext->allocated_wqs,
1129 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1130 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1131 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001132 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001133 nes_debug(NES_DBG_QP,
1134 "db index > max user regions, failing create QP\n");
1135 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1136 if (virt_wqs) {
1137 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1138 nespbl->pbl_pbase);
1139 kfree(nespbl);
1140 }
1141 kfree(nesqp->allocated_buffer);
1142 return ERR_PTR(-ENOMEM);
1143 }
1144 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1145 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1146 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1147 } else {
1148 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1149 kfree(nesqp->allocated_buffer);
1150 return ERR_PTR(-EFAULT);
1151 }
1152 }
1153 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1154 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1155 if (err) {
1156 nes_debug(NES_DBG_QP,
1157 "error geting qp mem code = %d\n", err);
1158 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1159 kfree(nesqp->allocated_buffer);
1160 return ERR_PTR(-ENOMEM);
1161 }
1162
1163 nesqp->hwqp.sq_size = sq_size;
1164 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1165 nesqp->hwqp.sq_head = 1;
1166 nesqp->hwqp.rq_size = rq_size;
1167 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1168 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1169 (void *)nesqp->nesqp_context_pbase);
1170 */
1171 nesqp->hwqp.qp_id = qp_num;
1172 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1173 nesqp->nespd = nespd;
1174
1175 nescq = to_nescq(init_attr->send_cq);
1176 nesqp->nesscq = nescq;
1177 nescq = to_nescq(init_attr->recv_cq);
1178 nesqp->nesrcq = nescq;
1179
1180 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1181 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1182 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1183 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1184 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1185 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001186 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001187 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1188 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001189 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1191 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1192 u64temp = (u64)nesqp->hwqp.sq_pbase;
1193 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1194 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1195
1196
1197 if (!virt_wqs) {
1198 u64temp = (u64)nesqp->hwqp.sq_pbase;
1199 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1200 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1201 u64temp = (u64)nesqp->hwqp.rq_pbase;
1202 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1203 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1204 } else {
1205 u64temp = (u64)nesqp->pbl_pbase;
1206 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1207 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1208 }
1209
1210 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1211 nesvnic->next_qp_nic_index,
1212 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1213 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1214 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1215 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1216 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1217 nesvnic->next_qp_nic_index++;
1218 if ((nesvnic->next_qp_nic_index > 3) ||
1219 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1220 nesvnic->next_qp_nic_index = 0;
1221 }
1222 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1223
1224 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1225 u64temp = (u64)nesqp->hwqp.q2_pbase;
1226 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1227 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1228 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1229 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1230 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
Faisal Latif883c6992010-03-02 17:22:51 -06001231 NES_QPCONTEXT_ORDIRD_AAH |
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001232 ((((u32)nesadapter->max_irrq_wr) <<
1233 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1234 if (disable_mpa_crc) {
1235 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1236 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1237 }
1238
1239
1240 /* Create the QP */
1241 cqp_request = nes_get_cqp_request(nesdev);
1242 if (cqp_request == NULL) {
1243 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1244 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1245 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1246 kfree(nesqp->allocated_buffer);
1247 return ERR_PTR(-ENOMEM);
1248 }
1249 cqp_request->waiting = 1;
1250 cqp_wqe = &cqp_request->cqp_wqe;
1251
1252 if (!virt_wqs) {
1253 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1254 NES_CQP_QP_IWARP_STATE_IDLE;
1255 } else {
1256 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1257 NES_CQP_QP_IWARP_STATE_IDLE;
1258 }
1259 opcode |= NES_CQP_QP_CQS_VALID;
1260 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1261 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1262 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1263
1264 u64temp = (u64)nesqp->nesqp_context_pbase;
1265 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1266
1267 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001268 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001269
1270 /* Wait for CQP */
1271 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1272 nesqp->hwqp.qp_id);
1273 ret = wait_event_timeout(cqp_request->waitq,
1274 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1275 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1276 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1277 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1278 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1279 cqp_request->major_code, cqp_request->minor_code);
1280 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001281 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001282 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1283 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1284 kfree(nesqp->allocated_buffer);
1285 if (!ret) {
1286 return ERR_PTR(-ETIME);
1287 } else {
1288 return ERR_PTR(-EIO);
1289 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001290 }
1291
Roland Dreier1ff66e82008-07-14 23:48:49 -07001292 nes_put_cqp_request(nesdev, cqp_request);
1293
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001294 if (ibpd->uobject) {
1295 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
Dan Carpenter60429632013-07-25 20:04:59 +03001296 uresp.mmap_rq_db_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001297 uresp.actual_sq_size = sq_size;
1298 uresp.actual_rq_size = rq_size;
1299 uresp.qp_id = nesqp->hwqp.qp_id;
1300 uresp.nes_drv_opt = nes_drv_opt;
1301 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1302 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1303 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1304 kfree(nesqp->allocated_buffer);
1305 return ERR_PTR(-EFAULT);
1306 }
1307 }
1308
1309 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1310 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1311 spin_lock_init(&nesqp->lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001312 nes_add_ref(&nesqp->ibqp);
1313 break;
1314 default:
1315 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1316 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001317 }
Tatyana Nikolova19974122016-03-29 12:58:37 -05001318 init_completion(&nesqp->sq_drained);
1319 init_completion(&nesqp->rq_drained);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001320
Chien Tungd14152d2009-12-09 15:21:56 -08001321 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
Tatyana Nikolova7bfcfa52012-12-06 19:58:27 +00001322 init_timer(&nesqp->terminate_timer);
1323 nesqp->terminate_timer.function = nes_terminate_timeout;
1324 nesqp->terminate_timer.data = (unsigned long)nesqp;
Chien Tungd14152d2009-12-09 15:21:56 -08001325
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001326 /* update the QP table */
1327 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1328 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00001329 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001330
1331 return &nesqp->ibqp;
1332}
1333
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001334/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001335 * nes_clean_cq
1336 */
1337static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1338{
1339 u32 cq_head;
1340 u32 lo;
1341 u32 hi;
1342 u64 u64temp;
1343 unsigned long flags = 0;
1344
1345 spin_lock_irqsave(&nescq->lock, flags);
1346
1347 cq_head = nescq->hw_cq.cq_head;
1348 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1349 rmb();
1350 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1351 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1352 u64temp = (((u64)hi) << 32) | ((u64)lo);
1353 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1354 if (u64temp == (u64)(unsigned long)nesqp) {
1355 /* Zero the context value so cqe will be ignored */
1356 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1357 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1358 }
1359
1360 if (++cq_head >= nescq->hw_cq.cq_size)
1361 cq_head = 0;
1362 }
1363
1364 spin_unlock_irqrestore(&nescq->lock, flags);
1365}
1366
1367
1368/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001369 * nes_destroy_qp
1370 */
1371static int nes_destroy_qp(struct ib_qp *ibqp)
1372{
1373 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001374 struct nes_ucontext *nes_ucontext;
1375 struct ib_qp_attr attr;
1376 struct iw_cm_id *cm_id;
1377 struct iw_cm_event cm_event;
Faisal Latif0f0bee82011-09-25 20:34:00 -05001378 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001379
1380 atomic_inc(&sw_qps_destroyed);
1381 nesqp->destroyed = 1;
1382
1383 /* Blow away the connection if it exists. */
1384 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1385 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1386 attr.qp_state = IB_QPS_ERR;
1387 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1388 }
1389
1390 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1391 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1392 cm_id = nesqp->cm_id;
1393 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07001394 cm_event.status = -ETIMEDOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001395 cm_event.local_addr = cm_id->local_addr;
1396 cm_event.remote_addr = cm_id->remote_addr;
1397 cm_event.private_data = NULL;
1398 cm_event.private_data_len = 0;
1399
1400 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1401 "QP%u. cm_id = %p, refcount = %u. \n",
1402 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1403
1404 cm_id->rem_ref(cm_id);
1405 ret = cm_id->event_handler(cm_id, &cm_event);
1406 if (ret)
1407 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1408 }
1409
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001410 if (nesqp->user_mode) {
1411 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1412 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1413 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1414 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1415 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1416 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1417 }
1418 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001419 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1420 nesqp->sq_kmapped = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001421 kunmap(nesqp->page);
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001422 }
Don Wood5ee21fe2009-09-05 20:36:37 -07001423 } else {
1424 /* Clean any pending completions from the cq(s) */
1425 if (nesqp->nesscq)
1426 nes_clean_cq(nesqp, nesqp->nesscq);
1427
1428 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1429 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001430 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001431 nes_rem_ref(&nesqp->ibqp);
1432 return 0;
1433}
1434
1435
1436/**
1437 * nes_create_cq
1438 */
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001439static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1440 const struct ib_cq_init_attr *attr,
1441 struct ib_ucontext *context,
1442 struct ib_udata *udata)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001443{
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001444 int entries = attr->cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001445 u64 u64temp;
1446 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1447 struct nes_device *nesdev = nesvnic->nesdev;
1448 struct nes_adapter *nesadapter = nesdev->nesadapter;
1449 struct nes_cq *nescq;
1450 struct nes_ucontext *nes_ucontext = NULL;
1451 struct nes_cqp_request *cqp_request;
1452 void *mem = NULL;
1453 struct nes_hw_cqp_wqe *cqp_wqe;
1454 struct nes_pbl *nespbl = NULL;
1455 struct nes_create_cq_req req;
1456 struct nes_create_cq_resp resp;
1457 u32 cq_num = 0;
1458 u32 opcode = 0;
1459 u32 pbl_entries = 1;
1460 int err;
1461 unsigned long flags;
1462 int ret;
1463
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001464 if (attr->flags)
1465 return ERR_PTR(-EINVAL);
1466
Chien Tung5924aea2009-12-09 15:21:56 -08001467 if (entries > nesadapter->max_cqe)
1468 return ERR_PTR(-EINVAL);
1469
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001470 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001471 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001472 if (err) {
1473 return ERR_PTR(err);
1474 }
1475
1476 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1477 if (!nescq) {
1478 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1479 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1480 return ERR_PTR(-ENOMEM);
1481 }
1482
1483 nescq->hw_cq.cq_size = max(entries + 1, 5);
1484 nescq->hw_cq.cq_number = cq_num;
1485 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1486
1487
1488 if (context) {
1489 nes_ucontext = to_nesucontext(context);
1490 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1491 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1492 kfree(nescq);
1493 return ERR_PTR(-EFAULT);
1494 }
1495 nesvnic->mcrq_ucontext = nes_ucontext;
1496 nes_ucontext->mcrqf = req.mcrqf;
1497 if (nes_ucontext->mcrqf) {
1498 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001499 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001500 else if (nes_ucontext->mcrqf & 0x40000000)
1501 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1502 else
1503 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001504 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001505 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1506 }
1507 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1508 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001509 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001510 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1511 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1512 list_del(&nespbl->list);
1513 err = 0;
1514 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1515 nespbl);
1516 break;
1517 }
1518 }
1519 if (err) {
1520 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1521 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001522 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001523 }
1524
1525 pbl_entries = nespbl->pbl_size >> 3;
1526 nescq->cq_mem_size = 0;
1527 } else {
1528 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1529 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1530 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1531
1532 /* allocate the physical buffer space */
Joe Perches9011a672014-08-08 14:24:16 -07001533 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1534 &nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001535 if (!mem) {
1536 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1537 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1538 kfree(nescq);
1539 return ERR_PTR(-ENOMEM);
1540 }
1541
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001542 nescq->hw_cq.cq_vbase = mem;
1543 nescq->hw_cq.cq_head = 0;
1544 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1545 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1546 (u32)nescq->hw_cq.cq_pbase);
1547 }
1548
1549 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1550 spin_lock_init(&nescq->lock);
1551
1552 /* send CreateCQ request to CQP */
1553 cqp_request = nes_get_cqp_request(nesdev);
1554 if (cqp_request == NULL) {
1555 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1556 if (!context)
1557 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1558 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001559 else {
1560 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1561 nespbl->pbl_vbase, nespbl->pbl_pbase);
1562 kfree(nespbl);
1563 }
1564
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001565 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1566 kfree(nescq);
1567 return ERR_PTR(-ENOMEM);
1568 }
1569 cqp_request->waiting = 1;
1570 cqp_wqe = &cqp_request->cqp_wqe;
1571
1572 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1573 NES_CQP_CQ_CHK_OVERFLOW |
1574 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1575
1576 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1577
1578 if (pbl_entries != 1) {
1579 if (pbl_entries > 32) {
1580 /* use 4k pbl */
1581 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1582 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001583 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1584 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001585 if (!context)
1586 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1587 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001588 else {
1589 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1590 nespbl->pbl_vbase, nespbl->pbl_pbase);
1591 kfree(nespbl);
1592 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001593 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1594 kfree(nescq);
1595 return ERR_PTR(-ENOMEM);
1596 } else {
1597 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1598 nescq->virtual_cq = 2;
1599 nesadapter->free_4kpbl--;
1600 }
1601 } else {
1602 /* use 256 byte pbl */
1603 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1604 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001605 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1606 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001607 if (!context)
1608 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1609 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001610 else {
1611 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1612 nespbl->pbl_vbase, nespbl->pbl_pbase);
1613 kfree(nespbl);
1614 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001615 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1616 kfree(nescq);
1617 return ERR_PTR(-ENOMEM);
1618 } else {
1619 opcode |= NES_CQP_CQ_VIRT;
1620 nescq->virtual_cq = 1;
1621 nesadapter->free_256pbl--;
1622 }
1623 }
1624 }
1625
1626 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1627
1628 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1629 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1630 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1631 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1632
1633 if (context) {
1634 if (pbl_entries != 1)
1635 u64temp = (u64)nespbl->pbl_pbase;
1636 else
1637 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1638 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1639 nes_ucontext->mmap_db_index[0]);
1640 } else {
1641 u64temp = (u64)nescq->hw_cq.cq_pbase;
1642 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1643 }
1644 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1645 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1646 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1647 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1648 cpu_to_le32((u32)(u64temp >> 1));
1649 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1650 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1651
1652 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001653 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001654
1655 /* Wait for CQP */
1656 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1657 nescq->hw_cq.cq_number);
1658 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1659 NES_EVENT_TIMEOUT * 2);
1660 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1661 nescq->hw_cq.cq_number, ret);
1662 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001663 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001664 if (!context)
1665 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1666 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001667 else {
1668 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1669 nespbl->pbl_vbase, nespbl->pbl_pbase);
1670 kfree(nespbl);
1671 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001672 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1673 kfree(nescq);
1674 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001675 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001676 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001677
1678 if (context) {
1679 /* free the nespbl */
1680 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1681 nespbl->pbl_pbase);
1682 kfree(nespbl);
1683 resp.cq_id = nescq->hw_cq.cq_number;
1684 resp.cq_size = nescq->hw_cq.cq_size;
1685 resp.mmap_db_index = 0;
Dan Carpenter60429632013-07-25 20:04:59 +03001686 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001687 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1688 kfree(nescq);
1689 return ERR_PTR(-EFAULT);
1690 }
1691 }
1692
1693 return &nescq->ibcq;
1694}
1695
1696
1697/**
1698 * nes_destroy_cq
1699 */
1700static int nes_destroy_cq(struct ib_cq *ib_cq)
1701{
1702 struct nes_cq *nescq;
1703 struct nes_device *nesdev;
1704 struct nes_vnic *nesvnic;
1705 struct nes_adapter *nesadapter;
1706 struct nes_hw_cqp_wqe *cqp_wqe;
1707 struct nes_cqp_request *cqp_request;
1708 unsigned long flags;
1709 u32 opcode = 0;
1710 int ret;
1711
1712 if (ib_cq == NULL)
1713 return 0;
1714
1715 nescq = to_nescq(ib_cq);
1716 nesvnic = to_nesvnic(ib_cq->device);
1717 nesdev = nesvnic->nesdev;
1718 nesadapter = nesdev->nesadapter;
1719
1720 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1721
1722 /* Send DestroyCQ request to CQP */
1723 cqp_request = nes_get_cqp_request(nesdev);
1724 if (cqp_request == NULL) {
1725 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1726 return -ENOMEM;
1727 }
1728 cqp_request->waiting = 1;
1729 cqp_wqe = &cqp_request->cqp_wqe;
1730 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1731 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1732 if (nescq->virtual_cq == 1) {
1733 nesadapter->free_256pbl++;
1734 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1735 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001736 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001737 }
1738 } else if (nescq->virtual_cq == 2) {
1739 nesadapter->free_4kpbl++;
1740 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1741 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001742 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001743 }
1744 opcode |= NES_CQP_CQ_4KB_CHUNK;
1745 }
1746
1747 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1748
1749 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1750 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1751 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1752 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001753 if (!nescq->mcrqf)
1754 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1755
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001756 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001757 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001758
1759 /* Wait for CQP */
1760 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1761 nescq->hw_cq.cq_number);
1762 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1763 NES_EVENT_TIMEOUT);
1764 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1765 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1766 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1767 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001768 if (!ret) {
1769 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001770 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001771 ret = -ETIME;
1772 } else if (cqp_request->major_code) {
1773 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001774 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001775 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001776 } else {
1777 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001778 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001779 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001780
1781 if (nescq->cq_mem_size)
1782 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001783 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001784 kfree(nescq);
1785
1786 return ret;
1787}
1788
Don Wood0145f342009-03-06 15:15:00 -08001789/**
1790 * root_256
1791 */
1792static u32 root_256(struct nes_device *nesdev,
1793 struct nes_root_vpbl *root_vpbl,
1794 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001795 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001796{
1797 u64 leaf_pbl;
1798 int i, j, k;
1799
1800 if (pbl_count_4k == 1) {
1801 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1802 512, &new_root->pbl_pbase);
1803
1804 if (new_root->pbl_vbase == NULL)
1805 return 0;
1806
1807 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1808 for (i = 0; i < 16; i++) {
1809 new_root->pbl_vbase[i].pa_low =
1810 cpu_to_le32((u32)leaf_pbl);
1811 new_root->pbl_vbase[i].pa_high =
1812 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1813 leaf_pbl += 256;
1814 }
1815 } else {
1816 for (i = 3; i >= 0; i--) {
1817 j = i * 16;
1818 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1819 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1820 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1821 << 32);
1822 for (k = 1; k < 16; k++) {
1823 leaf_pbl += 256;
1824 root_vpbl->pbl_vbase[j + k].pa_low =
1825 cpu_to_le32((u32)leaf_pbl);
1826 root_vpbl->pbl_vbase[j + k].pa_high =
1827 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1828 }
1829 }
1830 }
1831
1832 return 1;
1833}
1834
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001835
1836/**
1837 * nes_reg_mr
1838 */
1839static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1840 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08001841 dma_addr_t single_buffer, u16 pbl_count_4k,
1842 u16 residual_page_count_4k, int acc, u64 *iova_start,
1843 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001844{
1845 struct nes_hw_cqp_wqe *cqp_wqe;
1846 struct nes_cqp_request *cqp_request;
1847 unsigned long flags;
1848 int ret;
1849 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08001850 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07001851 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08001852 u16 pbl_count = 0;
1853 u8 use_256_pbls = 0;
1854 u8 use_4k_pbls = 0;
1855 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
Or Gerlitz812d8672010-07-20 12:00:20 +00001856 struct nes_root_vpbl new_root = { 0, NULL, NULL };
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001857 u32 opcode = 0;
1858 u16 major_code;
1859
1860 /* Register the region with the adapter */
1861 cqp_request = nes_get_cqp_request(nesdev);
1862 if (cqp_request == NULL) {
1863 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1864 return -ENOMEM;
1865 }
1866 cqp_request->waiting = 1;
1867 cqp_wqe = &cqp_request->cqp_wqe;
1868
Don Wood0145f342009-03-06 15:15:00 -08001869 if (pbl_count_4k) {
1870 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1871
1872 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1873 pbl_count_256 = (pg_cnt + 31) / 32;
1874 if (pg_cnt <= 32) {
1875 if (pbl_count_256 <= nesadapter->free_256pbl)
1876 use_256_pbls = 1;
1877 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1878 use_4k_pbls = 1;
1879 } else if (pg_cnt <= 2048) {
1880 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1881 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1882 use_4k_pbls = 1;
1883 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1884 use_256_pbls = 1;
1885 use_two_level = 1;
1886 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1887 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001888 }
1889 } else {
Don Wood0145f342009-03-06 15:15:00 -08001890 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1891 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001892 }
Don Wood0145f342009-03-06 15:15:00 -08001893
1894 if (use_256_pbls) {
1895 pbl_count = pbl_count_256;
1896 nesadapter->free_256pbl -= pbl_count + use_two_level;
1897 } else if (use_4k_pbls) {
1898 pbl_count = pbl_count_4k;
1899 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1900 } else {
1901 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1902 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1903 nes_free_cqp_request(nesdev, cqp_request);
1904 return -ENOMEM;
1905 }
1906
1907 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001908 }
1909
Don Wood0145f342009-03-06 15:15:00 -08001910 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07001911 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08001912 if (new_root.pbl_pbase != 0)
1913 root_vpbl = &new_root;
1914 } else {
1915 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1916 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1917 use_256_pbls = 0;
1918
1919 if (pbl_count_4k == 1)
1920 use_two_level = 0;
1921 pbl_count = pbl_count_4k;
1922
1923 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1924 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1925 use_4k_pbls = 1;
1926 }
1927 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1928
1929 if (use_4k_pbls == 0)
1930 return -ENOMEM;
1931 }
1932 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001933
1934 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1935 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1936 if (acc & IB_ACCESS_LOCAL_WRITE)
1937 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1938 if (acc & IB_ACCESS_REMOTE_WRITE)
1939 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1940 if (acc & IB_ACCESS_REMOTE_READ)
1941 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1942 if (acc & IB_ACCESS_MW_BIND)
1943 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1944
1945 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1946 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1947 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1948 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1949
1950 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1951 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1952 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1953 cpu_to_le32(nespd->pd_id & 0x00007fff);
1954 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1955
1956 if (pbl_count == 0) {
1957 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1958 } else {
1959 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1960 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 -08001961 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 -08001962
Don Wood0145f342009-03-06 15:15:00 -08001963 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001964 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1965 }
1966 barrier();
1967
1968 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001969 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001970
1971 /* Wait for CQP */
1972 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1973 NES_EVENT_TIMEOUT);
1974 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1975 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1976 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1977 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07001978 nes_put_cqp_request(nesdev, cqp_request);
1979
Don Wooddae5d132009-03-06 15:12:09 -08001980 if ((!ret || major_code) && pbl_count != 0) {
1981 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08001982 if (use_256_pbls)
1983 nesadapter->free_256pbl += pbl_count + use_two_level;
1984 else if (use_4k_pbls)
1985 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08001986 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1987 }
Don Wood0145f342009-03-06 15:15:00 -08001988 if (new_root.pbl_pbase)
1989 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1990 new_root.pbl_pbase);
1991
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001992 if (!ret)
1993 return -ETIME;
1994 else if (major_code)
1995 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001996
Don Wood0145f342009-03-06 15:15:00 -08001997 *actual_pbl_cnt = pbl_count + use_two_level;
1998 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001999 return 0;
2000}
2001
2002
2003/**
2004 * nes_reg_phys_mr
2005 */
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002006struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
2007 int acc, u64 *iova_start)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002008{
2009 u64 region_length;
2010 struct nes_pd *nespd = to_nespd(ib_pd);
2011 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2012 struct nes_device *nesdev = nesvnic->nesdev;
2013 struct nes_adapter *nesadapter = nesdev->nesadapter;
2014 struct nes_mr *nesmr;
2015 struct ib_mr *ibmr;
2016 struct nes_vpbl vpbl;
2017 struct nes_root_vpbl root_vpbl;
2018 u32 stag;
Don Wood3f32eb12009-04-20 14:53:00 -07002019 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002020 u32 stag_index = 0;
2021 u32 next_stag_index = 0;
2022 u32 driver_key = 0;
Roland Dreier69d51022010-08-04 14:25:40 -07002023 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002024 int ret = 0;
2025 u16 pbl_count = 0;
2026 u8 single_page = 1;
2027 u8 stag_key = 0;
2028
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002029 region_length = 0;
2030 vpbl.pbl_vbase = NULL;
2031 root_vpbl.pbl_vbase = NULL;
2032 root_vpbl.pbl_pbase = 0;
2033
2034 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2035 stag_key = (u8)next_stag_index;
2036
2037 driver_key = 0;
2038
2039 next_stag_index >>= 8;
2040 next_stag_index %= nesadapter->max_mr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002041
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002042 if ((addr ^ *iova_start) & ~PAGE_MASK)
Don Wood3f32eb12009-04-20 14:53:00 -07002043 return ERR_PTR(-EINVAL);
2044
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002045 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002046 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002047 if (err) {
2048 return ERR_PTR(err);
2049 }
2050
2051 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2052 if (!nesmr) {
2053 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2054 return ERR_PTR(-ENOMEM);
2055 }
2056
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002057 /* Allocate a 4K buffer for the PBL */
2058 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2059 &vpbl.pbl_pbase);
2060 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2061 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2062 if (!vpbl.pbl_vbase) {
2063 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2064 ibmr = ERR_PTR(-ENOMEM);
2065 kfree(nesmr);
2066 goto reg_phys_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002067 }
2068
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002069
2070 mask = !size;
2071
2072 if (mask & ~PAGE_MASK) {
2073 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2074 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2075 ibmr = ERR_PTR(-EINVAL);
2076 kfree(nesmr);
2077 goto reg_phys_err;
2078 }
2079
2080 region_length += size;
2081 vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2082 vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2083
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002084 stag = stag_index << 8;
2085 stag |= driver_key;
2086 stag += (u32)stag_key;
2087
2088 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2089 " length = 0x%016lX, index = 0x%08X\n",
2090 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2091
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002092 /* Make the leaf PBL the root if only one PBL */
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002093 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002094
2095 if (single_page) {
2096 pbl_count = 0;
2097 } else {
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002098 pbl_count = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002099 }
2100 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002101 addr, pbl_count, 1, acc, iova_start,
Don Wood0145f342009-03-06 15:15:00 -08002102 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002103
2104 if (ret == 0) {
2105 nesmr->ibmr.rkey = stag;
2106 nesmr->ibmr.lkey = stag;
2107 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2108 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002109 } else {
2110 kfree(nesmr);
2111 ibmr = ERR_PTR(-ENOMEM);
2112 }
2113
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002114reg_phys_err:
2115 /* single PBL case */
2116 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002117 return ibmr;
2118}
2119
2120
2121/**
2122 * nes_get_dma_mr
2123 */
2124static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2125{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002126 u64 kva = 0;
2127
2128 nes_debug(NES_DBG_MR, "\n");
2129
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002130 return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002131}
2132
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002133/**
2134 * nes_reg_user_mr
2135 */
2136static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2137 u64 virt, int acc, struct ib_udata *udata)
2138{
2139 u64 iova_start;
2140 __le64 *pbl;
2141 u64 region_length;
2142 dma_addr_t last_dma_addr = 0;
2143 dma_addr_t first_dma_addr = 0;
2144 struct nes_pd *nespd = to_nespd(pd);
2145 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2146 struct nes_device *nesdev = nesvnic->nesdev;
2147 struct nes_adapter *nesadapter = nesdev->nesadapter;
2148 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002149 struct scatterlist *sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002150 struct nes_ucontext *nes_ucontext;
2151 struct nes_pbl *nespbl;
2152 struct nes_mr *nesmr;
2153 struct ib_umem *region;
2154 struct nes_mem_reg_req req;
2155 struct nes_vpbl vpbl;
2156 struct nes_root_vpbl root_vpbl;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002157 int entry, page_index;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002158 int page_count = 0;
2159 int err, pbl_depth = 0;
2160 int chunk_pages;
2161 int ret;
2162 u32 stag;
2163 u32 stag_index = 0;
2164 u32 next_stag_index;
2165 u32 driver_key;
2166 u32 root_pbl_index = 0;
2167 u32 cur_pbl_index = 0;
2168 u32 skip_pages;
2169 u16 pbl_count;
2170 u8 single_page = 1;
2171 u8 stag_key;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002172 int first_page = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002173
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002174 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002175 if (IS_ERR(region)) {
2176 return (struct ib_mr *)region;
2177 }
2178
2179 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2180 " offset = %u, page size = %u.\n",
2181 (unsigned long int)start, (unsigned long int)virt, (u32)length,
Haggai Eran406f9e52014-12-11 17:04:12 +02002182 ib_umem_offset(region), region->page_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002183
Haggai Eran406f9e52014-12-11 17:04:12 +02002184 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002185
Julia Lawalle2e435f2011-10-06 09:33:24 -07002186 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2187 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002188 return ERR_PTR(-EFAULT);
Julia Lawalle2e435f2011-10-06 09:33:24 -07002189 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002190 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2191
2192 switch (req.reg_type) {
2193 case IWNES_MEMREG_TYPE_MEM:
2194 pbl_depth = 0;
2195 region_length = 0;
2196 vpbl.pbl_vbase = NULL;
2197 root_vpbl.pbl_vbase = NULL;
2198 root_vpbl.pbl_pbase = 0;
2199
2200 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2201 stag_key = (u8)next_stag_index;
2202
2203 driver_key = next_stag_index & 0x70000000;
2204
2205 next_stag_index >>= 8;
2206 next_stag_index %= nesadapter->max_mr;
2207
2208 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002209 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002210 if (err) {
2211 ib_umem_release(region);
2212 return ERR_PTR(err);
2213 }
2214
2215 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2216 if (!nesmr) {
2217 ib_umem_release(region);
2218 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2219 return ERR_PTR(-ENOMEM);
2220 }
2221 nesmr->region = region;
2222
Yishai Hadaseeb84612014-01-28 13:40:15 +02002223 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2224 if (sg_dma_address(sg) & ~PAGE_MASK) {
2225 ib_umem_release(region);
2226 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2227 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2228 (unsigned int) sg_dma_address(sg));
2229 ibmr = ERR_PTR(-EINVAL);
2230 kfree(nesmr);
2231 goto reg_user_mr_err;
2232 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002233
Yishai Hadaseeb84612014-01-28 13:40:15 +02002234 if (!sg_dma_len(sg)) {
2235 ib_umem_release(region);
2236 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2237 stag_index);
2238 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2239 ibmr = ERR_PTR(-EINVAL);
2240 kfree(nesmr);
2241 goto reg_user_mr_err;
2242 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002243
Yishai Hadaseeb84612014-01-28 13:40:15 +02002244 region_length += sg_dma_len(sg);
2245 chunk_pages = sg_dma_len(sg) >> 12;
2246 region_length -= skip_pages << 12;
2247 for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2248 skip_pages = 0;
Haggai Eran406f9e52014-12-11 17:04:12 +02002249 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
Yishai Hadaseeb84612014-01-28 13:40:15 +02002250 goto enough_pages;
2251 if ((page_count&0x01FF) == 0) {
2252 if (page_count >= 1024 * 512) {
2253 ib_umem_release(region);
2254 nes_free_resource(nesadapter,
2255 nesadapter->allocated_mrs, stag_index);
2256 kfree(nesmr);
2257 ibmr = ERR_PTR(-E2BIG);
2258 goto reg_user_mr_err;
2259 }
2260 if (root_pbl_index == 1) {
2261 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2262 8192, &root_vpbl.pbl_pbase);
2263 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2264 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2265 if (!root_vpbl.pbl_vbase) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002266 ib_umem_release(region);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002267 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2268 vpbl.pbl_pbase);
2269 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2270 stag_index);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002271 kfree(nesmr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002272 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002273 goto reg_user_mr_err;
2274 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002275 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2276 GFP_KERNEL);
2277 if (!root_vpbl.leaf_vpbl) {
2278 ib_umem_release(region);
2279 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2280 root_vpbl.pbl_pbase);
2281 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2282 vpbl.pbl_pbase);
2283 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2284 stag_index);
2285 kfree(nesmr);
2286 ibmr = ERR_PTR(-ENOMEM);
2287 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002288 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002289 root_vpbl.pbl_vbase[0].pa_low =
2290 cpu_to_le32((u32)vpbl.pbl_pbase);
2291 root_vpbl.pbl_vbase[0].pa_high =
2292 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2293 root_vpbl.leaf_vpbl[0] = vpbl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002294 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002295 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2296 &vpbl.pbl_pbase);
2297 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2298 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2299 if (!vpbl.pbl_vbase) {
2300 ib_umem_release(region);
2301 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2302 ibmr = ERR_PTR(-ENOMEM);
2303 kfree(nesmr);
2304 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002305 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002306 if (1 <= root_pbl_index) {
2307 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2308 cpu_to_le32((u32)vpbl.pbl_pbase);
2309 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2310 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2311 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2312 }
2313 root_pbl_index++;
2314 cur_pbl_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002315 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002316 if (single_page) {
2317 if (page_count != 0) {
2318 if ((last_dma_addr+4096) !=
2319 (sg_dma_address(sg)+
2320 (page_index*4096)))
2321 single_page = 0;
2322 last_dma_addr = sg_dma_address(sg)+
2323 (page_index*4096);
2324 } else {
2325 first_dma_addr = sg_dma_address(sg)+
2326 (page_index*4096);
2327 last_dma_addr = first_dma_addr;
2328 }
2329 }
2330
2331 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2332 cpu_to_le32((u32)(sg_dma_address(sg)+
2333 (page_index*4096)));
2334 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2335 cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2336 (page_index*4096))) >> 32)));
2337 cur_pbl_index++;
2338 page_count++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002339 }
2340 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002341
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002342 enough_pages:
2343 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2344 " stag_key=0x%08x\n",
2345 stag_index, driver_key, stag_key);
2346 stag = stag_index << 8;
2347 stag |= driver_key;
2348 stag += (u32)stag_key;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002349
2350 iova_start = virt;
2351 /* Make the leaf PBL the root if only one PBL */
2352 if (root_pbl_index == 1) {
2353 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2354 }
2355
2356 if (single_page) {
2357 pbl_count = 0;
2358 } else {
2359 pbl_count = root_pbl_index;
2360 first_dma_addr = 0;
2361 }
2362 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2363 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2364 stag, (unsigned int)iova_start,
2365 (unsigned int)region_length, stag_index,
2366 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002367 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2368 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2369 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002370
2371 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2372
2373 if (ret == 0) {
2374 nesmr->ibmr.rkey = stag;
2375 nesmr->ibmr.lkey = stag;
2376 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2377 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002378 } else {
2379 ib_umem_release(region);
2380 kfree(nesmr);
2381 ibmr = ERR_PTR(-ENOMEM);
2382 }
2383
2384 reg_user_mr_err:
2385 /* free the resources */
2386 if (root_pbl_index == 1) {
2387 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2388 vpbl.pbl_pbase);
2389 } else {
2390 for (page_index=0; page_index<root_pbl_index; page_index++) {
2391 pci_free_consistent(nesdev->pcidev, 4096,
2392 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2393 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2394 }
2395 kfree(root_vpbl.leaf_vpbl);
2396 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2397 root_vpbl.pbl_pbase);
2398 }
2399
2400 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2401
2402 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002403 case IWNES_MEMREG_TYPE_QP:
2404 case IWNES_MEMREG_TYPE_CQ:
Tatyana Nikolova7d9c1992012-12-06 20:05:02 +00002405 if (!region->length) {
2406 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2407 ib_umem_release(region);
2408 return ERR_PTR(-EINVAL);
2409 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002410 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2411 if (!nespbl) {
2412 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2413 ib_umem_release(region);
2414 return ERR_PTR(-ENOMEM);
2415 }
2416 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2417 if (!nesmr) {
2418 ib_umem_release(region);
2419 kfree(nespbl);
2420 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2421 return ERR_PTR(-ENOMEM);
2422 }
2423 nesmr->region = region;
2424 nes_ucontext = to_nesucontext(pd->uobject->context);
2425 pbl_depth = region->length >> 12;
2426 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2427 nespbl->pbl_size = pbl_depth*sizeof(u64);
2428 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2429 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2430 } else {
2431 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2432 }
2433
2434 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2435 nespbl->pbl_size, pbl_depth);
2436 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2437 &nespbl->pbl_pbase);
2438 if (!pbl) {
2439 ib_umem_release(region);
2440 kfree(nesmr);
2441 kfree(nespbl);
2442 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2443 return ERR_PTR(-ENOMEM);
2444 }
2445
2446 nespbl->pbl_vbase = (u64 *)pbl;
2447 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002448 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002449 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002450 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2451 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002452
Yishai Hadaseeb84612014-01-28 13:40:15 +02002453 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2454 chunk_pages = sg_dma_len(sg) >> 12;
2455 chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2456 if (first_page) {
2457 nespbl->page = sg_page(sg);
2458 first_page = 0;
2459 }
2460
2461 for (page_index = 0; page_index < chunk_pages; page_index++) {
2462 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2463 (sg_dma_address(sg)+
2464 (page_index*4096)));
2465 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2466 (sg_dma_address(sg)+
2467 (page_index*4096)))>>32);
2468 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2469 (unsigned long long)*pbl,
2470 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2471 pbl++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002472 }
2473 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002474
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002475 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2476 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2477 } else {
2478 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2479 }
2480 nesmr->ibmr.rkey = -1;
2481 nesmr->ibmr.lkey = -1;
2482 nesmr->mode = req.reg_type;
2483 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002484 }
2485
Julia Lawalle2e435f2011-10-06 09:33:24 -07002486 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002487 return ERR_PTR(-ENOSYS);
2488}
2489
2490
2491/**
2492 * nes_dereg_mr
2493 */
2494static int nes_dereg_mr(struct ib_mr *ib_mr)
2495{
2496 struct nes_mr *nesmr = to_nesmr(ib_mr);
2497 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2498 struct nes_device *nesdev = nesvnic->nesdev;
2499 struct nes_adapter *nesadapter = nesdev->nesadapter;
2500 struct nes_hw_cqp_wqe *cqp_wqe;
2501 struct nes_cqp_request *cqp_request;
2502 unsigned long flags;
2503 int ret;
2504 u16 major_code;
2505 u16 minor_code;
2506
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03002507
2508 if (nesmr->pages)
2509 pci_free_consistent(nesdev->pcidev,
2510 nesmr->max_pages * sizeof(u64),
2511 nesmr->pages,
2512 nesmr->paddr);
2513
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002514 if (nesmr->region) {
2515 ib_umem_release(nesmr->region);
2516 }
2517 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2518 kfree(nesmr);
2519 return 0;
2520 }
2521
2522 /* Deallocate the region with the adapter */
2523
2524 cqp_request = nes_get_cqp_request(nesdev);
2525 if (cqp_request == NULL) {
2526 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2527 return -ENOMEM;
2528 }
2529 cqp_request->waiting = 1;
2530 cqp_wqe = &cqp_request->cqp_wqe;
2531
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002532 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2533 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2534 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2535 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2536 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2537
2538 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002539 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002540
2541 /* Wait for CQP */
2542 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2543 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2544 NES_EVENT_TIMEOUT);
2545 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2546 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2547 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2548
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002549 major_code = cqp_request->major_code;
2550 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002551
2552 nes_put_cqp_request(nesdev, cqp_request);
2553
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002554 if (!ret) {
2555 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2556 " ib_mr=%p, rkey = 0x%08X\n",
2557 ib_mr, ib_mr->rkey);
2558 return -ETIME;
2559 } else if (major_code) {
2560 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2561 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2562 major_code, minor_code, ib_mr, ib_mr->rkey);
2563 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002564 }
2565
2566 if (nesmr->pbls_used != 0) {
2567 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2568 if (nesmr->pbl_4k) {
2569 nesadapter->free_4kpbl += nesmr->pbls_used;
2570 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2571 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2572 "exceeded the max(%u)\n",
2573 nesadapter->free_4kpbl,
2574 nesadapter->max_4kpbl);
2575 } else {
2576 nesadapter->free_256pbl += nesmr->pbls_used;
2577 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2578 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2579 "exceeded the max(%u)\n",
2580 nesadapter->free_256pbl,
2581 nesadapter->max_256pbl);
2582 }
2583 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2584 }
2585 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2586 (ib_mr->rkey & 0x0fffff00) >> 8);
2587
2588 kfree(nesmr);
2589
2590 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002591}
2592
2593
2594/**
2595 * show_rev
2596 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002597static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2598 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002599{
2600 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002601 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002602 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2603
2604 nes_debug(NES_DBG_INIT, "\n");
2605 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2606}
2607
2608
2609/**
2610 * show_fw_ver
2611 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002612static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2613 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002614{
2615 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002616 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002617 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2618
2619 nes_debug(NES_DBG_INIT, "\n");
Chien Tung0e4562d2009-04-27 13:33:48 -07002620 return sprintf(buf, "%u.%u\n",
2621 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2622 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623}
2624
2625
2626/**
2627 * show_hca
2628 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002629static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2630 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002631{
2632 nes_debug(NES_DBG_INIT, "\n");
2633 return sprintf(buf, "NES020\n");
2634}
2635
2636
2637/**
2638 * show_board
2639 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002640static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2641 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002642{
2643 nes_debug(NES_DBG_INIT, "\n");
2644 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2645}
2646
2647
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002648static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2649static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2650static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2651static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002652
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002653static struct device_attribute *nes_dev_attributes[] = {
2654 &dev_attr_hw_rev,
2655 &dev_attr_fw_ver,
2656 &dev_attr_hca_type,
2657 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002658};
2659
2660
2661/**
2662 * nes_query_qp
2663 */
2664static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2665 int attr_mask, struct ib_qp_init_attr *init_attr)
2666{
2667 struct nes_qp *nesqp = to_nesqp(ibqp);
2668
2669 nes_debug(NES_DBG_QP, "\n");
2670
2671 attr->qp_access_flags = 0;
2672 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2673 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2674 attr->cap.max_recv_sge = 1;
Chien Tungeadde3a2010-03-25 13:39:50 +00002675 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2676 attr->cap.max_inline_data = 0;
2677 else
2678 attr->cap.max_inline_data = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002679
2680 init_attr->event_handler = nesqp->ibqp.event_handler;
2681 init_attr->qp_context = nesqp->ibqp.qp_context;
2682 init_attr->send_cq = nesqp->ibqp.send_cq;
2683 init_attr->recv_cq = nesqp->ibqp.recv_cq;
Dave Jones4127c362013-09-17 17:45:54 -04002684 init_attr->srq = nesqp->ibqp.srq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002685 init_attr->cap = attr->cap;
2686
2687 return 0;
2688}
2689
2690
2691/**
2692 * nes_hw_modify_qp
2693 */
2694int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002695 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002696{
2697 struct nes_hw_cqp_wqe *cqp_wqe;
2698 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2699 /* struct iw_cm_event cm_event; */
2700 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002701 int ret;
2702 u16 major_code;
2703
2704 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2705 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2706
2707 cqp_request = nes_get_cqp_request(nesdev);
2708 if (cqp_request == NULL) {
2709 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2710 return -ENOMEM;
2711 }
2712 if (wait_completion) {
2713 cqp_request->waiting = 1;
2714 } else {
2715 cqp_request->waiting = 0;
2716 }
2717 cqp_wqe = &cqp_request->cqp_wqe;
2718
2719 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2720 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2721 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2722 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2723 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2724 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2725 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2726
Don Wood8b1c9dc2009-09-05 20:36:38 -07002727 /* If sending a terminate message, fill in the length (in words) */
2728 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2729 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2730 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2731 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2732 }
2733
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002734 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002735 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002736
2737 /* Wait for CQP */
2738 if (wait_completion) {
2739 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2740 nesqp->hwqp.qp_id); */
2741 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2742 NES_EVENT_TIMEOUT);
2743 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2744 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2745 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2746 major_code = cqp_request->major_code;
2747 if (major_code) {
2748 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2749 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2750 nesqp->hwqp.qp_id, cqp_request->major_code,
2751 cqp_request->minor_code, next_iwarp_state);
2752 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002753
2754 nes_put_cqp_request(nesdev, cqp_request);
2755
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002756 if (!ret)
2757 return -ETIME;
2758 else if (major_code)
2759 return -EIO;
2760 else
2761 return 0;
2762 } else {
2763 return 0;
2764 }
2765}
2766
2767
2768/**
2769 * nes_modify_qp
2770 */
2771int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2772 int attr_mask, struct ib_udata *udata)
2773{
2774 struct nes_qp *nesqp = to_nesqp(ibqp);
2775 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2776 struct nes_device *nesdev = nesvnic->nesdev;
2777 /* u32 cqp_head; */
2778 /* u32 counter; */
2779 u32 next_iwarp_state = 0;
2780 int err;
2781 unsigned long qplockflags;
2782 int ret;
2783 u16 original_last_aeq;
2784 u8 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002785 u8 dont_wait = 0;
2786
2787 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2788 " iwarp_state=0x%X, refcount=%d\n",
2789 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2790 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2791
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002792 spin_lock_irqsave(&nesqp->lock, qplockflags);
2793
2794 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2795 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2796 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2797 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2798
2799 if (attr_mask & IB_QP_STATE) {
2800 switch (attr->qp_state) {
2801 case IB_QPS_INIT:
2802 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2803 nesqp->hwqp.qp_id);
2804 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2805 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002806 return -EINVAL;
2807 }
2808 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2809 issue_modify_qp = 1;
2810 break;
2811 case IB_QPS_RTR:
2812 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2813 nesqp->hwqp.qp_id);
2814 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2815 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002816 return -EINVAL;
2817 }
2818 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2819 issue_modify_qp = 1;
2820 break;
2821 case IB_QPS_RTS:
2822 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2823 nesqp->hwqp.qp_id);
2824 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2825 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826 return -EINVAL;
2827 }
2828 if (nesqp->cm_id == NULL) {
2829 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2830 nesqp->hwqp.qp_id );
2831 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002832 return -EINVAL;
2833 }
2834 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2835 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2836 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2837 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2838 issue_modify_qp = 1;
2839 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2840 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2841 nesqp->hte_added = 1;
2842 break;
2843 case IB_QPS_SQD:
2844 issue_modify_qp = 1;
2845 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2846 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2847 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2848 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002849 return 0;
2850 } else {
2851 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2852 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2853 " ignored due to current iWARP state\n",
2854 nesqp->hwqp.qp_id);
2855 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856 return -EINVAL;
2857 }
2858 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2859 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2860 " already done based on hw state.\n",
2861 nesqp->hwqp.qp_id);
2862 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002863 }
2864 switch (nesqp->hw_iwarp_state) {
2865 case NES_AEQE_IWARP_STATE_CLOSING:
2866 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Tatyana Nikolovad5fb4762012-10-02 16:38:12 +00002867 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002868 case NES_AEQE_IWARP_STATE_TERMINATE:
2869 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2870 break;
2871 case NES_AEQE_IWARP_STATE_ERROR:
2872 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2873 break;
2874 default:
2875 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002876 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2877 break;
2878 }
2879 }
2880 break;
2881 case IB_QPS_SQE:
2882 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2883 nesqp->hwqp.qp_id);
2884 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2885 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002886 return -EINVAL;
2887 }
2888 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2889 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2890 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2891 issue_modify_qp = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 break;
2893 case IB_QPS_ERR:
2894 case IB_QPS_RESET:
2895 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2896 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897 return -EINVAL;
2898 }
2899 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2900 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07002901 if (nesqp->term_flags)
2902 del_timer(&nesqp->terminate_timer);
2903
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2905 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2906 if (nesqp->hte_added) {
2907 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2908 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2909 nesqp->hte_added = 0;
2910 }
2911 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
Faisal Latifcd6860e2010-07-04 00:17:59 +00002912 (nesdev->iw_status) &&
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002913 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2914 next_iwarp_state |= NES_CQP_QP_RESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002915 } else {
2916 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2917 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2918 dont_wait = 1;
2919 }
2920 issue_modify_qp = 1;
2921 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2922 break;
2923 default:
2924 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925 return -EINVAL;
2926 break;
2927 }
2928
2929 nesqp->ibqp_state = attr->qp_state;
Tatyana Nikolovaa67b0782012-10-02 16:40:33 +00002930 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2931 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2932 nesqp->iwarp_state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002933 }
2934
2935 if (attr_mask & IB_QP_ACCESS_FLAGS) {
2936 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2937 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2938 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2939 issue_modify_qp = 1;
2940 }
2941 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2942 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2943 issue_modify_qp = 1;
2944 }
2945 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2946 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2947 issue_modify_qp = 1;
2948 }
2949 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2950 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2951 issue_modify_qp = 1;
2952 }
2953
2954 if (nesqp->user_mode) {
2955 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2956 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2957 issue_modify_qp = 1;
2958 }
2959 }
2960
2961 original_last_aeq = nesqp->last_aeq;
2962 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2963
2964 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2965
2966 ret = 0;
2967
2968
2969 if (issue_modify_qp) {
2970 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07002971 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002972 if (ret)
2973 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2974 " failed for QP%u.\n",
2975 next_iwarp_state, nesqp->hwqp.qp_id);
2976
2977 }
2978
2979 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2980 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2981 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2982 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2983 original_last_aeq, nesqp->last_aeq);
Dan Carpenter349850f2014-02-06 15:48:23 +03002984 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002985 if (dont_wait) {
2986 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2987 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2988 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2989 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2990 original_last_aeq, nesqp->last_aeq);
2991 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002992 spin_lock_irqsave(&nesqp->lock, qplockflags);
2993 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2994 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2995 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2996 nes_cm_disconn(nesqp);
2997 } else {
2998 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2999 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003000 }
3001 } else {
3002 spin_lock_irqsave(&nesqp->lock, qplockflags);
3003 if (nesqp->cm_id) {
3004 /* These two are for the timer thread */
3005 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006 nesqp->cm_id->add_ref(nesqp->cm_id);
3007 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3008 " need ae to finish up, original_last_aeq = 0x%04X."
3009 " last_aeq = 0x%04X, scheduling timer.\n",
3010 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3011 original_last_aeq, nesqp->last_aeq);
3012 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3013 }
3014 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3015 } else {
3016 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3017 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3018 " need ae to finish up, original_last_aeq = 0x%04X."
3019 " last_aeq = 0x%04X.\n",
3020 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3021 original_last_aeq, nesqp->last_aeq);
3022 }
3023 }
3024 } else {
3025 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3026 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3027 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3028 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003029 }
3030 } else {
3031 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3032 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3033 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3034 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003035 }
3036
3037 err = 0;
3038
3039 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3040 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3041
3042 return err;
3043}
3044
3045
3046/**
3047 * nes_muticast_attach
3048 */
3049static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3050{
3051 nes_debug(NES_DBG_INIT, "\n");
3052 return -ENOSYS;
3053}
3054
3055
3056/**
3057 * nes_multicast_detach
3058 */
3059static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3060{
3061 nes_debug(NES_DBG_INIT, "\n");
3062 return -ENOSYS;
3063}
3064
3065
3066/**
3067 * nes_process_mad
3068 */
3069static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
Ira Weinya97e2d82015-05-31 17:15:30 -04003070 u8 port_num, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -04003071 const struct ib_mad_hdr *in, size_t in_mad_size,
3072 struct ib_mad_hdr *out, size_t *out_mad_size,
3073 u16 *out_mad_pkey_index)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003074{
3075 nes_debug(NES_DBG_INIT, "\n");
3076 return -ENOSYS;
3077}
3078
3079static inline void
3080fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3081{
3082 int sge_index;
3083 int total_payload_length = 0;
3084 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3085 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3086 ib_wr->sg_list[sge_index].addr);
3087 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3088 ib_wr->sg_list[sge_index].length);
3089 if (uselkey)
3090 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3091 (ib_wr->sg_list[sge_index].lkey));
3092 else
3093 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3094
3095 total_payload_length += ib_wr->sg_list[sge_index].length;
3096 }
3097 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3098 total_payload_length);
3099 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3100 total_payload_length);
3101}
3102
3103/**
3104 * nes_post_send
3105 */
3106static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3107 struct ib_send_wr **bad_wr)
3108{
3109 u64 u64temp;
3110 unsigned long flags = 0;
3111 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3112 struct nes_device *nesdev = nesvnic->nesdev;
3113 struct nes_qp *nesqp = to_nesqp(ibqp);
3114 struct nes_hw_qp_wqe *wqe;
Chien Tung649fe4a2009-12-09 13:51:37 -08003115 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003116 u32 qsize = nesqp->hwqp.sq_size;
3117 u32 head;
Chien Tung649fe4a2009-12-09 13:51:37 -08003118 u32 wqe_misc = 0;
3119 u32 wqe_count = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003120 u32 counter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003121
Frank Zagoe5dec392009-12-09 13:51:36 -08003122 if (nesqp->ibqp_state > IB_QPS_RTS) {
3123 err = -EINVAL;
3124 goto out;
3125 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003127 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003128
3129 head = nesqp->hwqp.sq_head;
3130
3131 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003132 /* Check for QP error */
3133 if (nesqp->term_flags) {
3134 err = -EINVAL;
3135 break;
3136 }
3137
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003138 /* Check for SQ overflow */
3139 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003140 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003141 break;
3142 }
3143
3144 wqe = &nesqp->hwqp.sq_vbase[head];
3145 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3146 nesqp->hwqp.qp_id, wqe, head); */
3147 nes_fill_init_qp_wqe(wqe, nesqp, head);
3148 u64temp = (u64)(ib_wr->wr_id);
3149 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3150 u64temp);
Chien Tung649fe4a2009-12-09 13:51:37 -08003151 switch (ib_wr->opcode) {
3152 case IB_WR_SEND:
3153 case IB_WR_SEND_WITH_INV:
3154 if (IB_WR_SEND == ib_wr->opcode) {
3155 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3156 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3157 else
3158 wqe_misc = NES_IWARP_SQ_OP_SEND;
3159 } else {
3160 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3161 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3162 else
3163 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003164
Chien Tung649fe4a2009-12-09 13:51:37 -08003165 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3166 ib_wr->ex.invalidate_rkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003167 }
3168
Chien Tung649fe4a2009-12-09 13:51:37 -08003169 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3170 err = -EINVAL;
3171 break;
3172 }
3173
3174 if (ib_wr->send_flags & IB_SEND_FENCE)
3175 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3176
3177 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3178 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3179 (ib_wr->sg_list[0].length <= 64)) {
3180 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3181 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3182 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3183 ib_wr->sg_list[0].length);
3184 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3185 } else {
3186 fill_wqe_sg_send(wqe, ib_wr, 1);
3187 }
3188
3189 break;
3190 case IB_WR_RDMA_WRITE:
3191 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3192 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3193 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3194 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3195 err = -EINVAL;
3196 break;
3197 }
3198
3199 if (ib_wr->send_flags & IB_SEND_FENCE)
3200 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3201
3202 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003203 rdma_wr(ib_wr)->rkey);
Chien Tung649fe4a2009-12-09 13:51:37 -08003204 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003205 rdma_wr(ib_wr)->remote_addr);
Chien Tung649fe4a2009-12-09 13:51:37 -08003206
3207 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3208 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3209 (ib_wr->sg_list[0].length <= 64)) {
3210 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3211 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3212 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3213 ib_wr->sg_list[0].length);
3214 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3215 } else {
3216 fill_wqe_sg_send(wqe, ib_wr, 1);
3217 }
3218
3219 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3220 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3221 break;
3222 case IB_WR_RDMA_READ:
3223 case IB_WR_RDMA_READ_WITH_INV:
3224 /* iWARP only supports 1 sge for RDMA reads */
3225 if (ib_wr->num_sge > 1) {
3226 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3227 ib_wr->num_sge);
3228 err = -EINVAL;
3229 break;
3230 }
3231 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3232 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3233 } else {
3234 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3235 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3236 ib_wr->ex.invalidate_rkey);
3237 }
3238
3239 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003240 rdma_wr(ib_wr)->remote_addr);
Chien Tung649fe4a2009-12-09 13:51:37 -08003241 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003242 rdma_wr(ib_wr)->rkey);
Chien Tung649fe4a2009-12-09 13:51:37 -08003243 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3244 ib_wr->sg_list->length);
3245 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3246 ib_wr->sg_list->addr);
3247 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3248 ib_wr->sg_list->lkey);
3249 break;
3250 case IB_WR_LOCAL_INV:
3251 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3252 set_wqe_32bit_value(wqe->wqe_words,
3253 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3254 ib_wr->ex.invalidate_rkey);
3255 break;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03003256 case IB_WR_REG_MR:
3257 {
3258 struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3259 int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3260 int flags = reg_wr(ib_wr)->access;
3261
3262 if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3263 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3264 err = -EINVAL;
3265 break;
3266 }
3267 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3268 set_wqe_64bit_value(wqe->wqe_words,
3269 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3270 mr->ibmr.iova);
3271 set_wqe_32bit_value(wqe->wqe_words,
3272 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3273 mr->ibmr.length);
3274 set_wqe_32bit_value(wqe->wqe_words,
3275 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3276 set_wqe_32bit_value(wqe->wqe_words,
3277 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3278 reg_wr(ib_wr)->key);
3279
3280 if (page_shift == 12) {
3281 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3282 } else if (page_shift == 21) {
3283 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3284 } else {
3285 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3286 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3287 err = -EINVAL;
3288 break;
3289 }
3290
3291 /* Set access_flags */
3292 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3293 if (flags & IB_ACCESS_LOCAL_WRITE)
3294 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3295
3296 if (flags & IB_ACCESS_REMOTE_WRITE)
3297 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3298
3299 if (flags & IB_ACCESS_REMOTE_READ)
3300 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3301
3302 if (flags & IB_ACCESS_MW_BIND)
3303 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3304
3305 /* Fill in PBL info: */
3306 set_wqe_64bit_value(wqe->wqe_words,
3307 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3308 mr->paddr);
3309
3310 set_wqe_32bit_value(wqe->wqe_words,
3311 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3312 mr->npages * 8);
3313
3314 nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3315 "length: %d, rkey: %0x, pgl_paddr: %llx, "
3316 "page_list_len: %u, wqe_misc: %x\n",
3317 (unsigned long long) mr->ibmr.iova,
3318 mr->ibmr.length,
3319 reg_wr(ib_wr)->key,
3320 (unsigned long long) mr->paddr,
3321 mr->npages,
3322 wqe_misc);
3323 break;
3324 }
Chien Tung649fe4a2009-12-09 13:51:37 -08003325 default:
3326 /* error */
3327 err = -EINVAL;
3328 break;
3329 }
3330
Frank Zagoe5dec392009-12-09 13:51:36 -08003331 if (err)
3332 break;
3333
Chien Tungd14152d2009-12-09 15:21:56 -08003334 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003335 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
Chien Tungd14152d2009-12-09 15:21:56 -08003336
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003337 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3338
3339 ib_wr = ib_wr->next;
3340 head++;
3341 wqe_count++;
3342 if (head >= qsize)
3343 head = 0;
3344
3345 }
3346
3347 nesqp->hwqp.sq_head = head;
3348 barrier();
3349 while (wqe_count) {
3350 counter = min(wqe_count, ((u32)255));
3351 wqe_count -= counter;
3352 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3353 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3354 }
3355
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003356 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003357
Frank Zagoe5dec392009-12-09 13:51:36 -08003358out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003359 if (err)
3360 *bad_wr = ib_wr;
3361 return err;
3362}
3363
3364
3365/**
3366 * nes_post_recv
3367 */
3368static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3369 struct ib_recv_wr **bad_wr)
3370{
3371 u64 u64temp;
3372 unsigned long flags = 0;
3373 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3374 struct nes_device *nesdev = nesvnic->nesdev;
3375 struct nes_qp *nesqp = to_nesqp(ibqp);
3376 struct nes_hw_qp_wqe *wqe;
3377 int err = 0;
3378 int sge_index;
3379 u32 qsize = nesqp->hwqp.rq_size;
3380 u32 head;
3381 u32 wqe_count = 0;
3382 u32 counter;
3383 u32 total_payload_length;
3384
Frank Zago4293fdc12009-12-09 13:51:36 -08003385 if (nesqp->ibqp_state > IB_QPS_RTS) {
3386 err = -EINVAL;
3387 goto out;
3388 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003389
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003390 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003391
3392 head = nesqp->hwqp.rq_head;
3393
3394 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003395 /* Check for QP error */
3396 if (nesqp->term_flags) {
3397 err = -EINVAL;
3398 break;
3399 }
3400
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003401 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3402 err = -EINVAL;
3403 break;
3404 }
3405 /* Check for RQ overflow */
3406 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003407 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003408 break;
3409 }
3410
3411 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3412 wqe = &nesqp->hwqp.rq_vbase[head];
3413
3414 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3415 nesqp->hwqp.qp_id, wqe, head); */
3416 nes_fill_init_qp_wqe(wqe, nesqp, head);
3417 u64temp = (u64)(ib_wr->wr_id);
3418 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3419 u64temp);
3420 total_payload_length = 0;
3421 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3422 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3423 ib_wr->sg_list[sge_index].addr);
3424 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3425 ib_wr->sg_list[sge_index].length);
3426 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3427 ib_wr->sg_list[sge_index].lkey);
3428
3429 total_payload_length += ib_wr->sg_list[sge_index].length;
3430 }
3431 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3432 total_payload_length);
3433
3434 ib_wr = ib_wr->next;
3435 head++;
3436 wqe_count++;
3437 if (head >= qsize)
3438 head = 0;
3439 }
3440
3441 nesqp->hwqp.rq_head = head;
3442 barrier();
3443 while (wqe_count) {
3444 counter = min(wqe_count, ((u32)255));
3445 wqe_count -= counter;
3446 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3447 }
3448
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003449 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003450
Frank Zago4293fdc12009-12-09 13:51:36 -08003451out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003452 if (err)
3453 *bad_wr = ib_wr;
3454 return err;
3455}
3456
Tatyana Nikolova19974122016-03-29 12:58:37 -05003457/**
3458 * nes_drain_sq - drain sq
3459 * @ibqp: pointer to ibqp
3460 */
3461static void nes_drain_sq(struct ib_qp *ibqp)
3462{
3463 struct nes_qp *nesqp = to_nesqp(ibqp);
3464
3465 if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3466 wait_for_completion(&nesqp->sq_drained);
3467}
3468
3469/**
3470 * nes_drain_rq - drain rq
3471 * @ibqp: pointer to ibqp
3472 */
3473static void nes_drain_rq(struct ib_qp *ibqp)
3474{
3475 struct nes_qp *nesqp = to_nesqp(ibqp);
3476
3477 if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3478 wait_for_completion(&nesqp->rq_drained);
3479}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003480
3481/**
3482 * nes_poll_cq
3483 */
3484static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3485{
3486 u64 u64temp;
3487 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003488 unsigned long flags = 0;
3489 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3490 struct nes_device *nesdev = nesvnic->nesdev;
3491 struct nes_cq *nescq = to_nescq(ibcq);
3492 struct nes_qp *nesqp;
3493 struct nes_hw_cqe cqe;
3494 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003495 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003496 u32 cq_size;
3497 u32 cqe_count = 0;
3498 u32 wqe_index;
3499 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003500 u32 move_cq_head = 1;
3501 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003502
3503 nes_debug(NES_DBG_CQ, "\n");
3504
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003505 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003506
3507 head = nescq->hw_cq.cq_head;
3508 cq_size = nescq->hw_cq.cq_size;
3509
3510 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003511 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3512 NES_CQE_VALID) == 0)
3513 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003514
Don Wood5ee21fe2009-09-05 20:36:37 -07003515 /*
3516 * Make sure we read CQ entry contents *after*
3517 * we've checked the valid bit.
3518 */
3519 rmb();
3520
3521 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003522 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3523 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3524 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3525 /* parse CQE, get completion context from WQE (either rq or sq) */
3526 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3527 ((u64)u32temp);
3528
3529 if (u64temp) {
3530 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531 memset(entry, 0, sizeof *entry);
3532 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3533 entry->status = IB_WC_SUCCESS;
3534 } else {
Don Wood320cdfd2009-09-05 20:36:39 -07003535 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3536 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3537 entry->status = err_code & 0x0000ffff;
3538
3539 /* The rest of the cqe's will be marked as flushed */
3540 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3541 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3542 NES_IWARP_CQE_MINOR_FLUSH);
3543 } else
3544 entry->status = IB_WC_WR_FLUSH_ERR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003545 }
3546
3547 entry->qp = &nesqp->ibqp;
3548 entry->src_qp = nesqp->hwqp.qp_id;
3549
3550 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3551 if (nesqp->skip_lsmm) {
3552 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003553 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554 }
3555
3556 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003557 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003558 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003559 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003560 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003561 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003562 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3563
Don Wood6eed5e72009-09-05 20:36:39 -07003564 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003565 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3566 case NES_IWARP_SQ_OP_RDMAW:
3567 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3568 entry->opcode = IB_WC_RDMA_WRITE;
3569 break;
3570 case NES_IWARP_SQ_OP_RDMAR:
3571 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3572 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003573 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003574 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3575 break;
3576 case NES_IWARP_SQ_OP_SENDINV:
3577 case NES_IWARP_SQ_OP_SENDSEINV:
3578 case NES_IWARP_SQ_OP_SEND:
3579 case NES_IWARP_SQ_OP_SENDSE:
3580 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3581 entry->opcode = IB_WC_SEND;
3582 break;
Chien Tung649fe4a2009-12-09 13:51:37 -08003583 case NES_IWARP_SQ_OP_LOCINV:
Tatyana Nikolova4a4b03f2012-01-06 18:17:19 -06003584 entry->opcode = IB_WC_LOCAL_INV;
Chien Tung649fe4a2009-12-09 13:51:37 -08003585 break;
Chien Tunge293a262009-12-09 15:21:54 -08003586 case NES_IWARP_SQ_OP_FAST_REG:
Sagi Grimberg13d3e892015-10-13 19:11:48 +03003587 entry->opcode = IB_WC_REG_MR;
Chien Tunge293a262009-12-09 15:21:54 -08003588 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003589 }
Don Wood6eed5e72009-09-05 20:36:39 -07003590
3591 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3592 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3593 move_cq_head = 0;
3594 wq_tail = nesqp->hwqp.sq_tail;
3595 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003596 } else {
3597 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003598 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003599 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3600 ((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 -08003601 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003602
3603 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3604 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3605 move_cq_head = 0;
3606 wq_tail = nesqp->hwqp.rq_tail;
3607 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003608 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003609
Tatyana Nikolova19974122016-03-29 12:58:37 -05003610 if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3611 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3612 complete(&nesqp->sq_drained);
3613 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3614 complete(&nesqp->rq_drained);
3615 }
3616
Don Wood5ee21fe2009-09-05 20:36:37 -07003617 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003618 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003619 cqe_count++;
3620 }
3621
Don Wood6eed5e72009-09-05 20:36:39 -07003622 if (move_cq_head) {
3623 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3624 if (++head >= cq_size)
3625 head = 0;
3626 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003627
Don Wood6eed5e72009-09-05 20:36:39 -07003628 if ((nescq->polled_completions > (cq_size / 2)) ||
3629 (nescq->polled_completions == 255)) {
3630 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003631 " are pending %u of %u.\n",
3632 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003633 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003634 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003635 nescq->polled_completions = 0;
3636 }
3637 } else {
3638 /* Update the wqe index and set status to flush */
3639 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3640 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3641 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3642 cpu_to_le32(wqe_index);
3643 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003644 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003645 }
3646
3647 if (nescq->polled_completions) {
3648 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3649 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3650 nescq->polled_completions = 0;
3651 }
3652
3653 nescq->hw_cq.cq_head = head;
3654 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3655 cqe_count, nescq->hw_cq.cq_number);
3656
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003657 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003658
3659 return cqe_count;
3660}
3661
3662
3663/**
3664 * nes_req_notify_cq
3665 */
3666static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3667 {
3668 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3669 struct nes_device *nesdev = nesvnic->nesdev;
3670 struct nes_cq *nescq = to_nescq(ibcq);
3671 u32 cq_arm;
3672
3673 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3674 nescq->hw_cq.cq_number);
3675
3676 cq_arm = nescq->hw_cq.cq_number;
3677 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3678 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3679 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3680 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3681 else
3682 return -EINVAL;
3683
3684 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3685 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3686
3687 return 0;
3688}
3689
Ira Weiny77386132015-05-13 20:02:58 -04003690static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3691 struct ib_port_immutable *immutable)
3692{
3693 struct ib_port_attr attr;
3694 int err;
3695
3696 err = nes_query_port(ibdev, port_num, &attr);
3697 if (err)
3698 return err;
3699
3700 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3701 immutable->gid_tbl_len = attr.gid_tbl_len;
Ira Weinyf9b22e32015-05-13 20:02:59 -04003702 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
Ira Weiny77386132015-05-13 20:02:58 -04003703
3704 return 0;
3705}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003706
3707/**
3708 * nes_init_ofa_device
3709 */
3710struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3711{
3712 struct nes_ib_device *nesibdev;
3713 struct nes_vnic *nesvnic = netdev_priv(netdev);
3714 struct nes_device *nesdev = nesvnic->nesdev;
3715
3716 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3717 if (nesibdev == NULL) {
3718 return NULL;
3719 }
3720 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3721 nesibdev->ibdev.owner = THIS_MODULE;
3722
3723 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3724 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3725 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3726
3727 nesibdev->ibdev.uverbs_cmd_mask =
3728 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3729 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3730 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3731 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3732 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3733 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3734 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3735 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3736 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3737 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3738 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3739 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3740 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3741 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3742 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3743 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3744 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3745 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3746 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3747 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3748 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3749 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3750
3751 nesibdev->ibdev.phys_port_cnt = 1;
3752 nesibdev->ibdev.num_comp_vectors = 1;
3753 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003754 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003755 nesibdev->ibdev.query_device = nes_query_device;
3756 nesibdev->ibdev.query_port = nes_query_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003757 nesibdev->ibdev.query_pkey = nes_query_pkey;
3758 nesibdev->ibdev.query_gid = nes_query_gid;
3759 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3760 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3761 nesibdev->ibdev.mmap = nes_mmap;
3762 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3763 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3764 nesibdev->ibdev.create_ah = nes_create_ah;
3765 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3766 nesibdev->ibdev.create_qp = nes_create_qp;
3767 nesibdev->ibdev.modify_qp = nes_modify_qp;
3768 nesibdev->ibdev.query_qp = nes_query_qp;
3769 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3770 nesibdev->ibdev.create_cq = nes_create_cq;
3771 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3772 nesibdev->ibdev.poll_cq = nes_poll_cq;
3773 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003774 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3775 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3776 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3777 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003778
Sagi Grimberge02e4d552015-07-30 10:32:46 +03003779 nesibdev->ibdev.alloc_mr = nes_alloc_mr;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03003780 nesibdev->ibdev.map_mr_sg = nes_map_mr_sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003781
3782 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3783 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3784 nesibdev->ibdev.process_mad = nes_process_mad;
3785
3786 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3787 nesibdev->ibdev.post_send = nes_post_send;
3788 nesibdev->ibdev.post_recv = nes_post_recv;
Tatyana Nikolova19974122016-03-29 12:58:37 -05003789 nesibdev->ibdev.drain_sq = nes_drain_sq;
3790 nesibdev->ibdev.drain_rq = nes_drain_rq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003791
3792 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3793 if (nesibdev->ibdev.iwcm == NULL) {
3794 ib_dealloc_device(&nesibdev->ibdev);
3795 return NULL;
3796 }
3797 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3798 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3799 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3800 nesibdev->ibdev.iwcm->connect = nes_connect;
3801 nesibdev->ibdev.iwcm->accept = nes_accept;
3802 nesibdev->ibdev.iwcm->reject = nes_reject;
3803 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3804 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
Ira Weiny77386132015-05-13 20:02:58 -04003805 nesibdev->ibdev.get_port_immutable = nes_port_immutable;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003806 memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3807 sizeof(nesibdev->ibdev.iwcm->ifname));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003808
3809 return nesibdev;
3810}
3811
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003812
3813/**
3814 * nes_handle_delayed_event
3815 */
3816static void nes_handle_delayed_event(unsigned long data)
3817{
3818 struct nes_vnic *nesvnic = (void *) data;
3819
3820 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3821 struct ib_event event;
3822
3823 event.device = &nesvnic->nesibdev->ibdev;
3824 if (!event.device)
3825 goto stop_timer;
3826 event.event = nesvnic->delayed_event;
3827 event.element.port_num = nesvnic->logical_port + 1;
3828 ib_dispatch_event(&event);
3829 }
3830
3831stop_timer:
3832 nesvnic->event_timer.function = NULL;
3833}
3834
3835
Faisal Latifcd6860e2010-07-04 00:17:59 +00003836void nes_port_ibevent(struct nes_vnic *nesvnic)
3837{
3838 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3839 struct nes_device *nesdev = nesvnic->nesdev;
3840 struct ib_event event;
3841 event.device = &nesibdev->ibdev;
3842 event.element.port_num = nesvnic->logical_port + 1;
3843 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003844
3845 if (!nesvnic->event_timer.function) {
3846 ib_dispatch_event(&event);
3847 nesvnic->last_dispatched_event = event.event;
3848 nesvnic->event_timer.function = nes_handle_delayed_event;
3849 nesvnic->event_timer.data = (unsigned long) nesvnic;
3850 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3851 add_timer(&nesvnic->event_timer);
3852 } else {
3853 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3854 }
3855 nesvnic->delayed_event = event.event;
Faisal Latifcd6860e2010-07-04 00:17:59 +00003856}
3857
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003858
3859/**
3860 * nes_destroy_ofa_device
3861 */
3862void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3863{
3864 if (nesibdev == NULL)
3865 return;
3866
3867 nes_unregister_ofa_device(nesibdev);
3868
3869 kfree(nesibdev->ibdev.iwcm);
3870 ib_dealloc_device(&nesibdev->ibdev);
3871}
3872
3873
3874/**
3875 * nes_register_ofa_device
3876 */
3877int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3878{
3879 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3880 struct nes_device *nesdev = nesvnic->nesdev;
3881 struct nes_adapter *nesadapter = nesdev->nesadapter;
3882 int i, ret;
3883
Ralph Campbell9a6edb62010-05-06 17:03:25 -07003884 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003885 if (ret) {
3886 return ret;
3887 }
3888
3889 /* Get the resources allocated to this device */
3890 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3891 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3892 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3893 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3894
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003895 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3896 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003897 if (ret) {
3898 while (i > 0) {
3899 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003900 device_remove_file(&nesibdev->ibdev.dev,
3901 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003902 }
3903 ib_unregister_device(&nesibdev->ibdev);
3904 return ret;
3905 }
3906 }
3907
3908 nesvnic->of_device_registered = 1;
3909
3910 return 0;
3911}
3912
3913
3914/**
3915 * nes_unregister_ofa_device
3916 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003917static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003918{
3919 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3920 int i;
3921
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003922 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3923 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003924 }
3925
3926 if (nesvnic->of_device_registered) {
3927 ib_unregister_device(&nesibdev->ibdev);
3928 }
3929
3930 nesvnic->of_device_registered = 0;
3931}