blob: b7179f4ac3a6312d9436503122cc8202495db70f [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Tatyana Nikolovac5488c52011-11-28 14:22:29 -06002 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/random.h>
37#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080039#include <asm/byteorder.h>
40
41#include <rdma/ib_verbs.h>
42#include <rdma/iw_cm.h>
43#include <rdma/ib_user_verbs.h>
44
45#include "nes.h"
46
47#include <rdma/ib_umem.h>
48
49atomic_t mod_qp_timouts;
50atomic_t qps_created;
51atomic_t sw_qps_destroyed;
52
Roland Dreier1a855fbf2008-04-16 21:01:09 -070053static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +030054static int nes_dereg_mr(struct ib_mr *ib_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080055
56/**
57 * nes_alloc_mw
58 */
Matan Barakb2a239d2016-02-29 18:05:29 +020059static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type,
60 struct ib_udata *udata)
Shani Michaeli7083e422013-02-06 16:19:12 +000061{
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062 struct nes_pd *nespd = to_nespd(ibpd);
63 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
64 struct nes_device *nesdev = nesvnic->nesdev;
65 struct nes_adapter *nesadapter = nesdev->nesadapter;
66 struct nes_cqp_request *cqp_request;
67 struct nes_mr *nesmr;
68 struct ib_mw *ibmw;
69 struct nes_hw_cqp_wqe *cqp_wqe;
70 int ret;
71 u32 stag;
72 u32 stag_index = 0;
73 u32 next_stag_index = 0;
74 u32 driver_key = 0;
75 u8 stag_key = 0;
76
Shani Michaeli7083e422013-02-06 16:19:12 +000077 if (type != IB_MW_TYPE_1)
78 return ERR_PTR(-EINVAL);
79
Glenn Streiff3c2d7742008-02-04 20:20:45 -080080 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
81 stag_key = (u8)next_stag_index;
82
83 driver_key = 0;
84
85 next_stag_index >>= 8;
86 next_stag_index %= nesadapter->max_mr;
87
88 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +000089 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080090 if (ret) {
91 return ERR_PTR(ret);
92 }
93
94 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
95 if (!nesmr) {
96 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
97 return ERR_PTR(-ENOMEM);
98 }
99
100 stag = stag_index << 8;
101 stag |= driver_key;
102 stag += (u32)stag_key;
103
104 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
105 stag, stag_index);
106
107 /* Register the region with the adapter */
108 cqp_request = nes_get_cqp_request(nesdev);
109 if (cqp_request == NULL) {
110 kfree(nesmr);
111 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
112 return ERR_PTR(-ENOMEM);
113 }
114
115 cqp_request->waiting = 1;
116 cqp_wqe = &cqp_request->cqp_wqe;
117
118 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
119 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
120 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
121 NES_CQP_STAG_REM_ACC_EN);
122
123 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
124 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
125 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
126
127 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700128 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800129
130 /* Wait for CQP */
131 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
132 NES_EVENT_TIMEOUT);
133 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
134 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
135 stag, ret, cqp_request->major_code, cqp_request->minor_code);
136 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -0700137 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800138 kfree(nesmr);
139 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
140 if (!ret) {
141 return ERR_PTR(-ETIME);
142 } else {
143 return ERR_PTR(-ENOMEM);
144 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800145 }
Roland Dreier1ff66e82008-07-14 23:48:49 -0700146 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800147
148 nesmr->ibmw.rkey = stag;
149 nesmr->mode = IWNES_MEMREG_TYPE_MW;
150 ibmw = &nesmr->ibmw;
151 nesmr->pbl_4k = 0;
152 nesmr->pbls_used = 0;
153
154 return ibmw;
155}
156
157
158/**
159 * nes_dealloc_mw
160 */
161static int nes_dealloc_mw(struct ib_mw *ibmw)
162{
163 struct nes_mr *nesmr = to_nesmw(ibmw);
164 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
165 struct nes_device *nesdev = nesvnic->nesdev;
166 struct nes_adapter *nesadapter = nesdev->nesadapter;
167 struct nes_hw_cqp_wqe *cqp_wqe;
168 struct nes_cqp_request *cqp_request;
169 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800170 int ret;
171
172 /* Deallocate the window with the adapter */
173 cqp_request = nes_get_cqp_request(nesdev);
174 if (cqp_request == NULL) {
175 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
176 return -ENOMEM;
177 }
178 cqp_request->waiting = 1;
179 cqp_wqe = &cqp_request->cqp_wqe;
180 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
181 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
182 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
183
184 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700185 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800186
187 /* Wait for CQP */
188 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
189 ibmw->rkey);
190 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
191 NES_EVENT_TIMEOUT);
192 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
193 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
194 ret, cqp_request->major_code, cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -0700195 if (!ret)
196 err = -ETIME;
197 else if (cqp_request->major_code)
198 err = -EIO;
199
200 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201
202 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
203 (ibmw->rkey & 0x0fffff00) >> 8);
204 kfree(nesmr);
205
206 return err;
207}
208
209
Chien Tunge293a262009-12-09 15:21:54 -0800210/*
211 * nes_alloc_fast_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800212 */
Chien Tunge293a262009-12-09 15:21:54 -0800213static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
214 u32 stag, u32 page_count)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800216 struct nes_hw_cqp_wqe *cqp_wqe;
Chien Tunge293a262009-12-09 15:21:54 -0800217 struct nes_cqp_request *cqp_request;
218 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800219 int ret;
Chien Tunge293a262009-12-09 15:21:54 -0800220 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800221 u32 opcode = 0;
Chien Tunge293a262009-12-09 15:21:54 -0800222 u16 major_code;
223 u64 region_length = page_count * PAGE_SIZE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800224
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800225
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800226 cqp_request = nes_get_cqp_request(nesdev);
227 if (cqp_request == NULL) {
228 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
Chien Tunge293a262009-12-09 15:21:54 -0800229 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800230 }
Chien Tunge293a262009-12-09 15:21:54 -0800231 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
232 "region_length = %llu\n",
233 page_count, region_length);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800234 cqp_request->waiting = 1;
235 cqp_wqe = &cqp_request->cqp_wqe;
236
Chien Tunge293a262009-12-09 15:21:54 -0800237 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
238 if (nesadapter->free_4kpbl > 0) {
239 nesadapter->free_4kpbl--;
240 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
241 } else {
242 /* No 4kpbl's available: */
243 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
244 nes_debug(NES_DBG_MR, "Out of Pbls\n");
245 nes_free_cqp_request(nesdev, cqp_request);
246 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800247 }
248
Chien Tunge293a262009-12-09 15:21:54 -0800249 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
250 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
251 NES_CQP_STAG_REM_ACC_EN;
252 /*
253 * The current OFED API does not support the zero based TO option.
254 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
255 * the API does not support that ability to have the MR set for local
256 * access only when created and not allow the SQ op to override. Given
257 * this the remote enable must be set here.
258 */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800259
260 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
261 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
Chien Tunge293a262009-12-09 15:21:54 -0800262 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800263
Chien Tunge293a262009-12-09 15:21:54 -0800264 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
265 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
266 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
267 cpu_to_le32(nespd->pd_id & 0x00007fff);
268
269 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
270 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
271 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
272 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
273 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
274 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
275 barrier();
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800276
277 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700278 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800279
280 /* Wait for CQP */
Chien Tunge293a262009-12-09 15:21:54 -0800281 ret = wait_event_timeout(cqp_request->waitq,
282 (0 != cqp_request->request_done),
283 NES_EVENT_TIMEOUT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800284
Chien Tunge293a262009-12-09 15:21:54 -0800285 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
286 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
287 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
288 cqp_request->minor_code);
289 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -0700290 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800291
Chien Tunge293a262009-12-09 15:21:54 -0800292 if (!ret || major_code) {
Chien Tung35c6d692008-11-02 21:37:35 -0800293 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Chien Tunge293a262009-12-09 15:21:54 -0800294 nesadapter->free_4kpbl++;
Chien Tung35c6d692008-11-02 21:37:35 -0800295 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
296 }
297
Chien Tunge293a262009-12-09 15:21:54 -0800298 if (!ret)
299 return -ETIME;
300 else if (major_code)
301 return -EIO;
302 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800303}
304
Chien Tunge293a262009-12-09 15:21:54 -0800305/*
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300306 * nes_alloc_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800307 */
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300308static struct ib_mr *nes_alloc_mr(struct ib_pd *ibpd,
309 enum ib_mr_type mr_type,
310 u32 max_num_sg)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800311{
Chien Tunge293a262009-12-09 15:21:54 -0800312 struct nes_pd *nespd = to_nespd(ibpd);
313 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800314 struct nes_device *nesdev = nesvnic->nesdev;
Chien Tung35c6d692008-11-02 21:37:35 -0800315 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800316
Chien Tunge293a262009-12-09 15:21:54 -0800317 u32 next_stag_index;
318 u8 stag_key = 0;
319 u32 driver_key = 0;
320 int err = 0;
321 u32 stag_index = 0;
322 struct nes_mr *nesmr;
323 u32 stag;
324 int ret;
325 struct ib_mr *ibmr;
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300326
327 if (mr_type != IB_MR_TYPE_MEM_REG)
328 return ERR_PTR(-EINVAL);
329
330 if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
331 return ERR_PTR(-E2BIG);
332
Chien Tunge293a262009-12-09 15:21:54 -0800333/*
334 * Note: Set to always use a fixed length single page entry PBL. This is to allow
335 * for the fast_reg_mr operation to always know the size of the PBL.
336 */
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300337 if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
Chien Tunge293a262009-12-09 15:21:54 -0800338 return ERR_PTR(-E2BIG);
339
340 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
341 stag_key = (u8)next_stag_index;
342 next_stag_index >>= 8;
343 next_stag_index %= nesadapter->max_mr;
344
345 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
346 nesadapter->max_mr, &stag_index,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000347 &next_stag_index, NES_RESOURCE_FAST_MR);
Chien Tunge293a262009-12-09 15:21:54 -0800348 if (err)
349 return ERR_PTR(err);
350
351 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
352 if (!nesmr) {
353 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
354 return ERR_PTR(-ENOMEM);
355 }
356
357 stag = stag_index << 8;
358 stag |= driver_key;
359 stag += (u32)stag_key;
360
361 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
362 stag, stag_index);
363
Sagi Grimberge02e4d552015-07-30 10:32:46 +0300364 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_num_sg);
Chien Tunge293a262009-12-09 15:21:54 -0800365
366 if (ret == 0) {
367 nesmr->ibmr.rkey = stag;
368 nesmr->ibmr.lkey = stag;
369 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
370 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800371 } else {
Chien Tunge293a262009-12-09 15:21:54 -0800372 kfree(nesmr);
373 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300374 return ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800375 }
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300376
377 nesmr->pages = pci_alloc_consistent(nesdev->pcidev,
378 max_num_sg * sizeof(u64),
379 &nesmr->paddr);
380 if (!nesmr->paddr)
381 goto err;
382
383 nesmr->max_pages = max_num_sg;
384
Chien Tunge293a262009-12-09 15:21:54 -0800385 return ibmr;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300386
387err:
388 nes_dereg_mr(ibmr);
389
390 return ERR_PTR(-ENOMEM);
391}
392
393static int nes_set_page(struct ib_mr *ibmr, u64 addr)
394{
395 struct nes_mr *nesmr = to_nesmr(ibmr);
396
397 if (unlikely(nesmr->npages == nesmr->max_pages))
398 return -ENOMEM;
399
400 nesmr->pages[nesmr->npages++] = cpu_to_le64(addr);
401
402 return 0;
403}
404
Christoph Hellwigff2ba992016-05-03 18:01:04 +0200405static int nes_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
Bart Van Assche9aa8b322016-05-12 10:49:15 -0700406 int sg_nents, unsigned int *sg_offset)
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +0300407{
408 struct nes_mr *nesmr = to_nesmr(ibmr);
409
410 nesmr->npages = 0;
411
Christoph Hellwigff2ba992016-05-03 18:01:04 +0200412 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, nes_set_page);
Chien Tunge293a262009-12-09 15:21:54 -0800413}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800414
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415/**
416 * nes_query_device
417 */
Matan Barak2528e332015-06-11 16:35:25 +0300418static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
419 struct ib_udata *uhw)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800420{
421 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
422 struct nes_device *nesdev = nesvnic->nesdev;
423 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
424
Matan Barak2528e332015-06-11 16:35:25 +0300425 if (uhw->inlen || uhw->outlen)
426 return -EINVAL;
427
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800428 memset(props, 0, sizeof(*props));
429 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
430
Miroslaw Walukiewiczff0380c2010-07-15 14:53:29 +0000431 props->fw_ver = nesdev->nesadapter->firmware_version;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800432 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
433 props->vendor_id = nesdev->nesadapter->vendor_id;
434 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
435 props->hw_ver = nesdev->nesadapter->hw_rev;
436 props->max_mr_size = 0x80000000;
437 props->max_qp = nesibdev->max_qp;
438 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
439 props->max_sge = nesdev->nesadapter->max_sge;
440 props->max_cq = nesibdev->max_cq;
Chien Tung5924aea2009-12-09 15:21:56 -0800441 props->max_cqe = nesdev->nesadapter->max_cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800442 props->max_mr = nesibdev->max_mr;
443 props->max_mw = nesibdev->max_mr;
444 props->max_pd = nesibdev->max_pd;
445 props->max_sge_rd = 1;
446 switch (nesdev->nesadapter->max_irrq_wr) {
447 case 0:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800448 props->max_qp_rd_atom = 2;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800449 break;
450 case 1:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800451 props->max_qp_rd_atom = 8;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800452 break;
453 case 2:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800454 props->max_qp_rd_atom = 32;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800455 break;
456 case 3:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800457 props->max_qp_rd_atom = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800458 break;
459 default:
460 props->max_qp_rd_atom = 0;
461 }
Faisal Latif66388d62009-06-22 22:52:30 -0700462 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800463 props->atomic_cap = IB_ATOMIC_NONE;
464 props->max_map_per_fmr = 1;
465
466 return 0;
467}
468
469
470/**
471 * nes_query_port
472 */
473static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
474{
Chien Tungcd1d3f72009-09-05 20:36:39 -0700475 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
476 struct net_device *netdev = nesvnic->netdev;
477
Or Gerlitzc4550c62017-01-24 13:02:39 +0200478 /* props being zeroed by the caller, avoid zeroing it here */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479
Chien Tungcd1d3f72009-09-05 20:36:39 -0700480 props->max_mtu = IB_MTU_4096;
481
482 if (netdev->mtu >= 4096)
483 props->active_mtu = IB_MTU_4096;
484 else if (netdev->mtu >= 2048)
485 props->active_mtu = IB_MTU_2048;
486 else if (netdev->mtu >= 1024)
487 props->active_mtu = IB_MTU_1024;
488 else if (netdev->mtu >= 512)
489 props->active_mtu = IB_MTU_512;
490 else
491 props->active_mtu = IB_MTU_256;
492
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800493 props->lid = 1;
494 props->lmc = 0;
495 props->sm_lid = 0;
496 props->sm_sl = 0;
Chien Tung29327722010-08-23 14:32:36 +0000497 if (netif_queue_stopped(netdev))
498 props->state = IB_PORT_DOWN;
499 else if (nesvnic->linkup)
Chien Tungcd1d3f72009-09-05 20:36:39 -0700500 props->state = IB_PORT_ACTIVE;
501 else
502 props->state = IB_PORT_DOWN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800503 props->phys_state = 0;
504 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
505 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
506 props->gid_tbl_len = 1;
507 props->pkey_tbl_len = 1;
508 props->qkey_viol_cntr = 0;
509 props->active_width = IB_WIDTH_4X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200510 props->active_speed = IB_SPEED_SDR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800511 props->max_msg_sz = 0x80000000;
512
513 return 0;
514}
515
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800516/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800517 * nes_query_pkey
518 */
519static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
520{
521 *pkey = 0;
522 return 0;
523}
524
525
526/**
527 * nes_query_gid
528 */
529static int nes_query_gid(struct ib_device *ibdev, u8 port,
530 int index, union ib_gid *gid)
531{
532 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
533
534 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
535 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
536
537 return 0;
538}
539
540
541/**
542 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
543 * of all objects associated with a particular user-mode client.
544 */
545static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
546 struct ib_udata *udata)
547{
548 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
549 struct nes_device *nesdev = nesvnic->nesdev;
550 struct nes_adapter *nesadapter = nesdev->nesadapter;
551 struct nes_alloc_ucontext_req req;
552 struct nes_alloc_ucontext_resp uresp;
553 struct nes_ucontext *nes_ucontext;
554 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
555
556
557 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
558 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
559 return ERR_PTR(-EINVAL);
560 }
561
562 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
563 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
564 req.userspace_ver, NES_ABI_USERSPACE_VER);
565 return ERR_PTR(-EINVAL);
566 }
567
568
569 memset(&uresp, 0, sizeof uresp);
570
571 uresp.max_qps = nesibdev->max_qp;
572 uresp.max_pds = nesibdev->max_pd;
573 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
574 uresp.virtwq = nesadapter->virtwq;
575 uresp.kernel_ver = NES_ABI_KERNEL_VER;
576
577 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
578 if (!nes_ucontext)
579 return ERR_PTR(-ENOMEM);
580
581 nes_ucontext->nesdev = nesdev;
582 nes_ucontext->mmap_wq_offset = uresp.max_pds;
583 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
584 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
585 PAGE_SIZE;
586
587
588 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
589 kfree(nes_ucontext);
590 return ERR_PTR(-EFAULT);
591 }
592
593 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
594 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
595 atomic_set(&nes_ucontext->usecnt, 1);
596 return &nes_ucontext->ibucontext;
597}
598
599
600/**
601 * nes_dealloc_ucontext
602 */
603static int nes_dealloc_ucontext(struct ib_ucontext *context)
604{
605 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
606 /* struct nes_device *nesdev = nesvnic->nesdev; */
607 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
608
609 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
610 return 0;
611 kfree(nes_ucontext);
612 return 0;
613}
614
615
616/**
617 * nes_mmap
618 */
619static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
620{
621 unsigned long index;
622 struct nes_vnic *nesvnic = to_nesvnic(context->device);
623 struct nes_device *nesdev = nesvnic->nesdev;
624 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
625 struct nes_ucontext *nes_ucontext;
626 struct nes_qp *nesqp;
627
628 nes_ucontext = to_nesucontext(context);
629
630
631 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
632 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
633 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
634 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
635 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
636 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
637 return -EFAULT;
638 }
639 nesqp = nes_ucontext->mmap_nesqp[index];
640 if (nesqp == NULL) {
641 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
642 return -EFAULT;
643 }
644 if (remap_pfn_range(vma, vma->vm_start,
645 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
646 vma->vm_end - vma->vm_start,
647 vma->vm_page_prot)) {
648 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
649 return -EAGAIN;
650 }
651 vma->vm_private_data = nesqp;
652 return 0;
653 } else {
654 index = vma->vm_pgoff;
655 if (!test_bit(index, nes_ucontext->allocated_doorbells))
656 return -EFAULT;
657
658 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
659 if (io_remap_pfn_range(vma, vma->vm_start,
660 (nesdev->doorbell_start +
661 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
662 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
663 return -EAGAIN;
664 vma->vm_private_data = nes_ucontext;
665 return 0;
666 }
667
668 return -ENOSYS;
669}
670
671
672/**
673 * nes_alloc_pd
674 */
675static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
676 struct ib_ucontext *context, struct ib_udata *udata)
677{
678 struct nes_pd *nespd;
679 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
680 struct nes_device *nesdev = nesvnic->nesdev;
681 struct nes_adapter *nesadapter = nesdev->nesadapter;
682 struct nes_ucontext *nesucontext;
683 struct nes_alloc_pd_resp uresp;
684 u32 pd_num = 0;
685 int err;
686
687 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
688 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
Eric Dumazet29b44332010-10-11 10:22:12 +0000689 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800690
691 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
Tatyana Nikolova81821642012-09-20 21:08:04 +0000692 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800693 if (err) {
694 return ERR_PTR(err);
695 }
696
697 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
698 if (!nespd) {
699 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
700 return ERR_PTR(-ENOMEM);
701 }
702
703 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
704 nespd, nesvnic->nesibdev->ibdev.name);
705
706 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
707
708 if (context) {
709 nesucontext = to_nesucontext(context);
710 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
711 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
712 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
713 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600714 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800715 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
716 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
717 kfree(nespd);
718 return ERR_PTR(-ENOMEM);
719 }
720
721 uresp.pd_id = nespd->pd_id;
722 uresp.mmap_db_index = nespd->mmap_db_index;
723 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
724 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
725 kfree(nespd);
726 return ERR_PTR(-EFAULT);
727 }
728
729 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
730 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
731 nesucontext->first_free_db = nespd->mmap_db_index + 1;
732 }
733
734 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
735 return &nespd->ibpd;
736}
737
738
739/**
740 * nes_dealloc_pd
741 */
742static int nes_dealloc_pd(struct ib_pd *ibpd)
743{
744 struct nes_ucontext *nesucontext;
745 struct nes_pd *nespd = to_nespd(ibpd);
746 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
747 struct nes_device *nesdev = nesvnic->nesdev;
748 struct nes_adapter *nesadapter = nesdev->nesadapter;
749
750 if ((ibpd->uobject) && (ibpd->uobject->context)) {
751 nesucontext = to_nesucontext(ibpd->uobject->context);
752 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
753 nespd->mmap_db_index);
754 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
755 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
756 if (nesucontext->first_free_db > nespd->mmap_db_index) {
757 nesucontext->first_free_db = nespd->mmap_db_index;
758 }
759 }
760
761 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
762 nespd->pd_id, nespd);
763 nes_free_resource(nesadapter, nesadapter->allocated_pds,
764 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
765 kfree(nespd);
766
767 return 0;
768}
769
770
771/**
772 * nes_create_ah
773 */
Moni Shoua477864c2016-11-23 08:23:24 +0200774static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr,
775 struct ib_udata *udata)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800776{
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 */
Denys Vlasenkoda74bf42016-04-08 20:58:42 +0200984static void nes_free_qp_mem(struct nes_device *nesdev,
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800985 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);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001079 return ERR_PTR(-ENOMEM);
1080 }
1081 u64nesqp = (unsigned long)mem;
1082 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1083 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1084 u64nesqp &= ~u64temp;
1085 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1086 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1087 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1088 nesqp->allocated_buffer = mem;
1089
1090 if (udata) {
1091 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1092 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1093 kfree(nesqp->allocated_buffer);
1094 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
Yann Droneaud9d194d12014-03-10 23:06:27 +01001095 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001096 }
1097 if (req.user_wqe_buffers) {
1098 virt_wqs = 1;
1099 }
Tatyana Nikolova43adff32014-03-11 14:20:17 -05001100 if (req.user_qp_buffer)
1101 nesqp->nesuqp_addr = req.user_qp_buffer;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001102 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1103 nesqp->user_mode = 1;
1104 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1105 if (virt_wqs) {
1106 err = 1;
1107 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1108 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1109 list_del(&nespbl->list);
1110 err = 0;
1111 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1112 nespbl, nespbl->user_base);
1113 break;
1114 }
1115 }
1116 if (err) {
1117 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1118 (long long unsigned int)req.user_wqe_buffers);
1119 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1120 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001121 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001122 }
1123 }
1124
1125 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1126 nesqp->mmap_sq_db_index =
1127 find_next_zero_bit(nes_ucontext->allocated_wqs,
1128 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1129 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1130 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001131 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001132 nes_debug(NES_DBG_QP,
1133 "db index > max user regions, failing create QP\n");
1134 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1135 if (virt_wqs) {
1136 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1137 nespbl->pbl_pbase);
1138 kfree(nespbl);
1139 }
1140 kfree(nesqp->allocated_buffer);
1141 return ERR_PTR(-ENOMEM);
1142 }
1143 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1144 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1145 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1146 } else {
1147 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1148 kfree(nesqp->allocated_buffer);
1149 return ERR_PTR(-EFAULT);
1150 }
1151 }
1152 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1153 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1154 if (err) {
1155 nes_debug(NES_DBG_QP,
1156 "error geting qp mem code = %d\n", err);
1157 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1158 kfree(nesqp->allocated_buffer);
1159 return ERR_PTR(-ENOMEM);
1160 }
1161
1162 nesqp->hwqp.sq_size = sq_size;
1163 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1164 nesqp->hwqp.sq_head = 1;
1165 nesqp->hwqp.rq_size = rq_size;
1166 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1167 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1168 (void *)nesqp->nesqp_context_pbase);
1169 */
1170 nesqp->hwqp.qp_id = qp_num;
1171 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1172 nesqp->nespd = nespd;
1173
1174 nescq = to_nescq(init_attr->send_cq);
1175 nesqp->nesscq = nescq;
1176 nescq = to_nescq(init_attr->recv_cq);
1177 nesqp->nesrcq = nescq;
1178
1179 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1180 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1181 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1182 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1183 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1184 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001185 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001186 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1187 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001188 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001189 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1190 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1191 u64temp = (u64)nesqp->hwqp.sq_pbase;
1192 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1193 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1194
1195
1196 if (!virt_wqs) {
1197 u64temp = (u64)nesqp->hwqp.sq_pbase;
1198 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1199 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1200 u64temp = (u64)nesqp->hwqp.rq_pbase;
1201 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1202 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1203 } else {
1204 u64temp = (u64)nesqp->pbl_pbase;
1205 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1206 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1207 }
1208
1209 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1210 nesvnic->next_qp_nic_index,
1211 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1212 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1213 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1214 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1215 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1216 nesvnic->next_qp_nic_index++;
1217 if ((nesvnic->next_qp_nic_index > 3) ||
1218 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1219 nesvnic->next_qp_nic_index = 0;
1220 }
1221 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1222
1223 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1224 u64temp = (u64)nesqp->hwqp.q2_pbase;
1225 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1226 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1227 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1228 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1229 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
Faisal Latif883c6992010-03-02 17:22:51 -06001230 NES_QPCONTEXT_ORDIRD_AAH |
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001231 ((((u32)nesadapter->max_irrq_wr) <<
1232 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1233 if (disable_mpa_crc) {
1234 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1235 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1236 }
1237
1238
1239 /* Create the QP */
1240 cqp_request = nes_get_cqp_request(nesdev);
1241 if (cqp_request == NULL) {
1242 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1243 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1244 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1245 kfree(nesqp->allocated_buffer);
1246 return ERR_PTR(-ENOMEM);
1247 }
1248 cqp_request->waiting = 1;
1249 cqp_wqe = &cqp_request->cqp_wqe;
1250
1251 if (!virt_wqs) {
1252 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1253 NES_CQP_QP_IWARP_STATE_IDLE;
1254 } else {
1255 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1256 NES_CQP_QP_IWARP_STATE_IDLE;
1257 }
1258 opcode |= NES_CQP_QP_CQS_VALID;
1259 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1260 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1261 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1262
1263 u64temp = (u64)nesqp->nesqp_context_pbase;
1264 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1265
1266 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001267 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001268
1269 /* Wait for CQP */
1270 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1271 nesqp->hwqp.qp_id);
1272 ret = wait_event_timeout(cqp_request->waitq,
1273 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1274 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1275 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1276 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1277 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1278 cqp_request->major_code, cqp_request->minor_code);
1279 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001280 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001281 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1282 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1283 kfree(nesqp->allocated_buffer);
1284 if (!ret) {
1285 return ERR_PTR(-ETIME);
1286 } else {
1287 return ERR_PTR(-EIO);
1288 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001289 }
1290
Roland Dreier1ff66e82008-07-14 23:48:49 -07001291 nes_put_cqp_request(nesdev, cqp_request);
1292
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001293 if (ibpd->uobject) {
1294 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
Dan Carpenter60429632013-07-25 20:04:59 +03001295 uresp.mmap_rq_db_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001296 uresp.actual_sq_size = sq_size;
1297 uresp.actual_rq_size = rq_size;
1298 uresp.qp_id = nesqp->hwqp.qp_id;
1299 uresp.nes_drv_opt = nes_drv_opt;
1300 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1301 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1302 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1303 kfree(nesqp->allocated_buffer);
1304 return ERR_PTR(-EFAULT);
1305 }
1306 }
1307
1308 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1309 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1310 spin_lock_init(&nesqp->lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001311 nes_add_ref(&nesqp->ibqp);
1312 break;
1313 default:
1314 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1315 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001316 }
Tatyana Nikolova19974122016-03-29 12:58:37 -05001317 init_completion(&nesqp->sq_drained);
1318 init_completion(&nesqp->rq_drained);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001319
Chien Tungd14152d2009-12-09 15:21:56 -08001320 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
Tatyana Nikolova7bfcfa52012-12-06 19:58:27 +00001321 init_timer(&nesqp->terminate_timer);
1322 nesqp->terminate_timer.function = nes_terminate_timeout;
1323 nesqp->terminate_timer.data = (unsigned long)nesqp;
Chien Tungd14152d2009-12-09 15:21:56 -08001324
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001325 /* update the QP table */
1326 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1327 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00001328 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001329
1330 return &nesqp->ibqp;
1331}
1332
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001333/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001334 * nes_clean_cq
1335 */
1336static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1337{
1338 u32 cq_head;
1339 u32 lo;
1340 u32 hi;
1341 u64 u64temp;
1342 unsigned long flags = 0;
1343
1344 spin_lock_irqsave(&nescq->lock, flags);
1345
1346 cq_head = nescq->hw_cq.cq_head;
1347 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1348 rmb();
1349 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1350 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1351 u64temp = (((u64)hi) << 32) | ((u64)lo);
1352 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1353 if (u64temp == (u64)(unsigned long)nesqp) {
1354 /* Zero the context value so cqe will be ignored */
1355 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1356 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1357 }
1358
1359 if (++cq_head >= nescq->hw_cq.cq_size)
1360 cq_head = 0;
1361 }
1362
1363 spin_unlock_irqrestore(&nescq->lock, flags);
1364}
1365
1366
1367/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001368 * nes_destroy_qp
1369 */
1370static int nes_destroy_qp(struct ib_qp *ibqp)
1371{
1372 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001373 struct nes_ucontext *nes_ucontext;
1374 struct ib_qp_attr attr;
1375 struct iw_cm_id *cm_id;
1376 struct iw_cm_event cm_event;
Faisal Latif0f0bee82011-09-25 20:34:00 -05001377 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001378
1379 atomic_inc(&sw_qps_destroyed);
1380 nesqp->destroyed = 1;
1381
1382 /* Blow away the connection if it exists. */
1383 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1384 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1385 attr.qp_state = IB_QPS_ERR;
1386 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1387 }
1388
1389 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1390 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1391 cm_id = nesqp->cm_id;
1392 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07001393 cm_event.status = -ETIMEDOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001394 cm_event.local_addr = cm_id->local_addr;
1395 cm_event.remote_addr = cm_id->remote_addr;
1396 cm_event.private_data = NULL;
1397 cm_event.private_data_len = 0;
1398
1399 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1400 "QP%u. cm_id = %p, refcount = %u. \n",
1401 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1402
1403 cm_id->rem_ref(cm_id);
1404 ret = cm_id->event_handler(cm_id, &cm_event);
1405 if (ret)
1406 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1407 }
1408
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001409 if (nesqp->user_mode) {
1410 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1411 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1412 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1413 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1414 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1415 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1416 }
1417 }
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001418 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1419 nesqp->sq_kmapped = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001420 kunmap(nesqp->page);
Faisal Latifd2fa9b262009-12-09 15:54:28 -08001421 }
Don Wood5ee21fe2009-09-05 20:36:37 -07001422 } else {
1423 /* Clean any pending completions from the cq(s) */
1424 if (nesqp->nesscq)
1425 nes_clean_cq(nesqp, nesqp->nesscq);
1426
1427 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1428 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001429 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001430 nes_rem_ref(&nesqp->ibqp);
1431 return 0;
1432}
1433
1434
1435/**
1436 * nes_create_cq
1437 */
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001438static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1439 const struct ib_cq_init_attr *attr,
1440 struct ib_ucontext *context,
1441 struct ib_udata *udata)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001442{
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001443 int entries = attr->cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001444 u64 u64temp;
1445 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1446 struct nes_device *nesdev = nesvnic->nesdev;
1447 struct nes_adapter *nesadapter = nesdev->nesadapter;
1448 struct nes_cq *nescq;
1449 struct nes_ucontext *nes_ucontext = NULL;
1450 struct nes_cqp_request *cqp_request;
1451 void *mem = NULL;
1452 struct nes_hw_cqp_wqe *cqp_wqe;
1453 struct nes_pbl *nespbl = NULL;
1454 struct nes_create_cq_req req;
1455 struct nes_create_cq_resp resp;
1456 u32 cq_num = 0;
1457 u32 opcode = 0;
1458 u32 pbl_entries = 1;
1459 int err;
1460 unsigned long flags;
1461 int ret;
1462
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001463 if (attr->flags)
1464 return ERR_PTR(-EINVAL);
1465
Chien Tung5924aea2009-12-09 15:21:56 -08001466 if (entries > nesadapter->max_cqe)
1467 return ERR_PTR(-EINVAL);
1468
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00001470 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001471 if (err) {
1472 return ERR_PTR(err);
1473 }
1474
1475 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1476 if (!nescq) {
1477 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001478 return ERR_PTR(-ENOMEM);
1479 }
1480
1481 nescq->hw_cq.cq_size = max(entries + 1, 5);
1482 nescq->hw_cq.cq_number = cq_num;
1483 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1484
1485
1486 if (context) {
1487 nes_ucontext = to_nesucontext(context);
1488 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1489 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1490 kfree(nescq);
1491 return ERR_PTR(-EFAULT);
1492 }
1493 nesvnic->mcrq_ucontext = nes_ucontext;
1494 nes_ucontext->mcrqf = req.mcrqf;
1495 if (nes_ucontext->mcrqf) {
1496 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001497 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001498 else if (nes_ucontext->mcrqf & 0x40000000)
1499 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1500 else
1501 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001502 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001503 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1504 }
1505 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1506 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001507 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001508 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1509 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1510 list_del(&nespbl->list);
1511 err = 0;
1512 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1513 nespbl);
1514 break;
1515 }
1516 }
1517 if (err) {
1518 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1519 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001520 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001521 }
1522
1523 pbl_entries = nespbl->pbl_size >> 3;
1524 nescq->cq_mem_size = 0;
1525 } else {
1526 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1527 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1528 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1529
1530 /* allocate the physical buffer space */
Joe Perches9011a672014-08-08 14:24:16 -07001531 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1532 &nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001533 if (!mem) {
1534 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1535 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1536 kfree(nescq);
1537 return ERR_PTR(-ENOMEM);
1538 }
1539
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001540 nescq->hw_cq.cq_vbase = mem;
1541 nescq->hw_cq.cq_head = 0;
1542 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1543 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1544 (u32)nescq->hw_cq.cq_pbase);
1545 }
1546
1547 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1548 spin_lock_init(&nescq->lock);
1549
1550 /* send CreateCQ request to CQP */
1551 cqp_request = nes_get_cqp_request(nesdev);
1552 if (cqp_request == NULL) {
1553 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1554 if (!context)
1555 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1556 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001557 else {
1558 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1559 nespbl->pbl_vbase, nespbl->pbl_pbase);
1560 kfree(nespbl);
1561 }
1562
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001563 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1564 kfree(nescq);
1565 return ERR_PTR(-ENOMEM);
1566 }
1567 cqp_request->waiting = 1;
1568 cqp_wqe = &cqp_request->cqp_wqe;
1569
1570 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1571 NES_CQP_CQ_CHK_OVERFLOW |
1572 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1573
1574 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1575
1576 if (pbl_entries != 1) {
1577 if (pbl_entries > 32) {
1578 /* use 4k pbl */
1579 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1580 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001581 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1582 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001583 if (!context)
1584 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1585 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001586 else {
1587 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1588 nespbl->pbl_vbase, nespbl->pbl_pbase);
1589 kfree(nespbl);
1590 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001591 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1592 kfree(nescq);
1593 return ERR_PTR(-ENOMEM);
1594 } else {
1595 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1596 nescq->virtual_cq = 2;
1597 nesadapter->free_4kpbl--;
1598 }
1599 } else {
1600 /* use 256 byte pbl */
1601 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1602 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001603 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1604 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001605 if (!context)
1606 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1607 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001608 else {
1609 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1610 nespbl->pbl_vbase, nespbl->pbl_pbase);
1611 kfree(nespbl);
1612 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001613 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1614 kfree(nescq);
1615 return ERR_PTR(-ENOMEM);
1616 } else {
1617 opcode |= NES_CQP_CQ_VIRT;
1618 nescq->virtual_cq = 1;
1619 nesadapter->free_256pbl--;
1620 }
1621 }
1622 }
1623
1624 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1625
1626 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1627 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1628 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1629 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1630
1631 if (context) {
1632 if (pbl_entries != 1)
1633 u64temp = (u64)nespbl->pbl_pbase;
1634 else
1635 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1636 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1637 nes_ucontext->mmap_db_index[0]);
1638 } else {
1639 u64temp = (u64)nescq->hw_cq.cq_pbase;
1640 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1641 }
1642 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1643 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1644 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1645 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1646 cpu_to_le32((u32)(u64temp >> 1));
1647 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1648 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1649
1650 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001651 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001652
1653 /* Wait for CQP */
1654 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1655 nescq->hw_cq.cq_number);
1656 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1657 NES_EVENT_TIMEOUT * 2);
1658 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1659 nescq->hw_cq.cq_number, ret);
1660 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001661 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001662 if (!context)
1663 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1664 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001665 else {
1666 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1667 nespbl->pbl_vbase, nespbl->pbl_pbase);
1668 kfree(nespbl);
1669 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001670 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1671 kfree(nescq);
1672 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001673 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001674 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001675
1676 if (context) {
1677 /* free the nespbl */
1678 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1679 nespbl->pbl_pbase);
1680 kfree(nespbl);
1681 resp.cq_id = nescq->hw_cq.cq_number;
1682 resp.cq_size = nescq->hw_cq.cq_size;
1683 resp.mmap_db_index = 0;
Dan Carpenter60429632013-07-25 20:04:59 +03001684 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001685 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1686 kfree(nescq);
1687 return ERR_PTR(-EFAULT);
1688 }
1689 }
1690
1691 return &nescq->ibcq;
1692}
1693
1694
1695/**
1696 * nes_destroy_cq
1697 */
1698static int nes_destroy_cq(struct ib_cq *ib_cq)
1699{
1700 struct nes_cq *nescq;
1701 struct nes_device *nesdev;
1702 struct nes_vnic *nesvnic;
1703 struct nes_adapter *nesadapter;
1704 struct nes_hw_cqp_wqe *cqp_wqe;
1705 struct nes_cqp_request *cqp_request;
1706 unsigned long flags;
1707 u32 opcode = 0;
1708 int ret;
1709
1710 if (ib_cq == NULL)
1711 return 0;
1712
1713 nescq = to_nescq(ib_cq);
1714 nesvnic = to_nesvnic(ib_cq->device);
1715 nesdev = nesvnic->nesdev;
1716 nesadapter = nesdev->nesadapter;
1717
1718 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1719
1720 /* Send DestroyCQ request to CQP */
1721 cqp_request = nes_get_cqp_request(nesdev);
1722 if (cqp_request == NULL) {
1723 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1724 return -ENOMEM;
1725 }
1726 cqp_request->waiting = 1;
1727 cqp_wqe = &cqp_request->cqp_wqe;
1728 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1729 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1730 if (nescq->virtual_cq == 1) {
1731 nesadapter->free_256pbl++;
1732 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1733 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001734 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001735 }
1736 } else if (nescq->virtual_cq == 2) {
1737 nesadapter->free_4kpbl++;
1738 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1739 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001740 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001741 }
1742 opcode |= NES_CQP_CQ_4KB_CHUNK;
1743 }
1744
1745 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1746
1747 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1748 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1749 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1750 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001751 if (!nescq->mcrqf)
1752 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1753
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001754 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001755 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001756
1757 /* Wait for CQP */
1758 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1759 nescq->hw_cq.cq_number);
1760 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1761 NES_EVENT_TIMEOUT);
1762 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1763 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1764 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1765 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001766 if (!ret) {
1767 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001768 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001769 ret = -ETIME;
1770 } else if (cqp_request->major_code) {
1771 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001772 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001773 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001774 } else {
1775 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001776 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001777 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001778
1779 if (nescq->cq_mem_size)
1780 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001781 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001782 kfree(nescq);
1783
1784 return ret;
1785}
1786
Don Wood0145f342009-03-06 15:15:00 -08001787/**
1788 * root_256
1789 */
1790static u32 root_256(struct nes_device *nesdev,
1791 struct nes_root_vpbl *root_vpbl,
1792 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001793 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001794{
1795 u64 leaf_pbl;
1796 int i, j, k;
1797
1798 if (pbl_count_4k == 1) {
1799 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1800 512, &new_root->pbl_pbase);
1801
1802 if (new_root->pbl_vbase == NULL)
1803 return 0;
1804
1805 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1806 for (i = 0; i < 16; i++) {
1807 new_root->pbl_vbase[i].pa_low =
1808 cpu_to_le32((u32)leaf_pbl);
1809 new_root->pbl_vbase[i].pa_high =
1810 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1811 leaf_pbl += 256;
1812 }
1813 } else {
1814 for (i = 3; i >= 0; i--) {
1815 j = i * 16;
1816 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1817 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1818 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1819 << 32);
1820 for (k = 1; k < 16; k++) {
1821 leaf_pbl += 256;
1822 root_vpbl->pbl_vbase[j + k].pa_low =
1823 cpu_to_le32((u32)leaf_pbl);
1824 root_vpbl->pbl_vbase[j + k].pa_high =
1825 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1826 }
1827 }
1828 }
1829
1830 return 1;
1831}
1832
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001833
1834/**
1835 * nes_reg_mr
1836 */
1837static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1838 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08001839 dma_addr_t single_buffer, u16 pbl_count_4k,
1840 u16 residual_page_count_4k, int acc, u64 *iova_start,
1841 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001842{
1843 struct nes_hw_cqp_wqe *cqp_wqe;
1844 struct nes_cqp_request *cqp_request;
1845 unsigned long flags;
1846 int ret;
1847 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08001848 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07001849 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08001850 u16 pbl_count = 0;
1851 u8 use_256_pbls = 0;
1852 u8 use_4k_pbls = 0;
1853 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
Or Gerlitz812d8672010-07-20 12:00:20 +00001854 struct nes_root_vpbl new_root = { 0, NULL, NULL };
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001855 u32 opcode = 0;
1856 u16 major_code;
1857
1858 /* Register the region with the adapter */
1859 cqp_request = nes_get_cqp_request(nesdev);
1860 if (cqp_request == NULL) {
1861 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1862 return -ENOMEM;
1863 }
1864 cqp_request->waiting = 1;
1865 cqp_wqe = &cqp_request->cqp_wqe;
1866
Don Wood0145f342009-03-06 15:15:00 -08001867 if (pbl_count_4k) {
1868 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1869
1870 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1871 pbl_count_256 = (pg_cnt + 31) / 32;
1872 if (pg_cnt <= 32) {
1873 if (pbl_count_256 <= nesadapter->free_256pbl)
1874 use_256_pbls = 1;
1875 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1876 use_4k_pbls = 1;
1877 } else if (pg_cnt <= 2048) {
1878 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1879 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1880 use_4k_pbls = 1;
1881 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1882 use_256_pbls = 1;
1883 use_two_level = 1;
1884 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1885 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001886 }
1887 } else {
Don Wood0145f342009-03-06 15:15:00 -08001888 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1889 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001890 }
Don Wood0145f342009-03-06 15:15:00 -08001891
1892 if (use_256_pbls) {
1893 pbl_count = pbl_count_256;
1894 nesadapter->free_256pbl -= pbl_count + use_two_level;
1895 } else if (use_4k_pbls) {
1896 pbl_count = pbl_count_4k;
1897 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1898 } else {
1899 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1900 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1901 nes_free_cqp_request(nesdev, cqp_request);
1902 return -ENOMEM;
1903 }
1904
1905 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001906 }
1907
Don Wood0145f342009-03-06 15:15:00 -08001908 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07001909 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08001910 if (new_root.pbl_pbase != 0)
1911 root_vpbl = &new_root;
1912 } else {
1913 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1914 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1915 use_256_pbls = 0;
1916
1917 if (pbl_count_4k == 1)
1918 use_two_level = 0;
1919 pbl_count = pbl_count_4k;
1920
1921 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1922 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1923 use_4k_pbls = 1;
1924 }
1925 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1926
1927 if (use_4k_pbls == 0)
1928 return -ENOMEM;
1929 }
1930 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001931
1932 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1933 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1934 if (acc & IB_ACCESS_LOCAL_WRITE)
1935 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1936 if (acc & IB_ACCESS_REMOTE_WRITE)
1937 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1938 if (acc & IB_ACCESS_REMOTE_READ)
1939 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1940 if (acc & IB_ACCESS_MW_BIND)
1941 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1942
1943 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1944 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1945 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1946 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1947
1948 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1949 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1950 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1951 cpu_to_le32(nespd->pd_id & 0x00007fff);
1952 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1953
1954 if (pbl_count == 0) {
1955 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1956 } else {
1957 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1958 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 -08001959 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 -08001960
Don Wood0145f342009-03-06 15:15:00 -08001961 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001962 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1963 }
1964 barrier();
1965
1966 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001967 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001968
1969 /* Wait for CQP */
1970 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1971 NES_EVENT_TIMEOUT);
1972 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1973 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1974 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1975 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07001976 nes_put_cqp_request(nesdev, cqp_request);
1977
Don Wooddae5d132009-03-06 15:12:09 -08001978 if ((!ret || major_code) && pbl_count != 0) {
1979 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08001980 if (use_256_pbls)
1981 nesadapter->free_256pbl += pbl_count + use_two_level;
1982 else if (use_4k_pbls)
1983 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08001984 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1985 }
Don Wood0145f342009-03-06 15:15:00 -08001986 if (new_root.pbl_pbase)
1987 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1988 new_root.pbl_pbase);
1989
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001990 if (!ret)
1991 return -ETIME;
1992 else if (major_code)
1993 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001994
Don Wood0145f342009-03-06 15:15:00 -08001995 *actual_pbl_cnt = pbl_count + use_two_level;
1996 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001997 return 0;
1998}
1999
2000
2001/**
2002 * nes_reg_phys_mr
2003 */
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002004struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
2005 int acc, u64 *iova_start)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002006{
2007 u64 region_length;
2008 struct nes_pd *nespd = to_nespd(ib_pd);
2009 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2010 struct nes_device *nesdev = nesvnic->nesdev;
2011 struct nes_adapter *nesadapter = nesdev->nesadapter;
2012 struct nes_mr *nesmr;
2013 struct ib_mr *ibmr;
2014 struct nes_vpbl vpbl;
2015 struct nes_root_vpbl root_vpbl;
2016 u32 stag;
Don Wood3f32eb12009-04-20 14:53:00 -07002017 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002018 u32 stag_index = 0;
2019 u32 next_stag_index = 0;
2020 u32 driver_key = 0;
Roland Dreier69d51022010-08-04 14:25:40 -07002021 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002022 int ret = 0;
2023 u16 pbl_count = 0;
2024 u8 single_page = 1;
2025 u8 stag_key = 0;
2026
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002027 region_length = 0;
2028 vpbl.pbl_vbase = NULL;
2029 root_vpbl.pbl_vbase = NULL;
2030 root_vpbl.pbl_pbase = 0;
2031
2032 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2033 stag_key = (u8)next_stag_index;
2034
2035 driver_key = 0;
2036
2037 next_stag_index >>= 8;
2038 next_stag_index %= nesadapter->max_mr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002039
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002040 if ((addr ^ *iova_start) & ~PAGE_MASK)
Don Wood3f32eb12009-04-20 14:53:00 -07002041 return ERR_PTR(-EINVAL);
2042
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002043 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002044 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002045 if (err) {
2046 return ERR_PTR(err);
2047 }
2048
2049 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2050 if (!nesmr) {
2051 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2052 return ERR_PTR(-ENOMEM);
2053 }
2054
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002055 /* Allocate a 4K buffer for the PBL */
2056 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2057 &vpbl.pbl_pbase);
2058 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2059 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2060 if (!vpbl.pbl_vbase) {
2061 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2062 ibmr = ERR_PTR(-ENOMEM);
2063 kfree(nesmr);
2064 goto reg_phys_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002065 }
2066
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002067
2068 mask = !size;
2069
2070 if (mask & ~PAGE_MASK) {
2071 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2072 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2073 ibmr = ERR_PTR(-EINVAL);
2074 kfree(nesmr);
2075 goto reg_phys_err;
2076 }
2077
2078 region_length += size;
2079 vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2080 vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2081
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002082 stag = stag_index << 8;
2083 stag |= driver_key;
2084 stag += (u32)stag_key;
2085
2086 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2087 " length = 0x%016lX, index = 0x%08X\n",
2088 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2089
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002090 /* Make the leaf PBL the root if only one PBL */
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002091 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002092
2093 if (single_page) {
2094 pbl_count = 0;
2095 } else {
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002096 pbl_count = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097 }
2098 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002099 addr, pbl_count, 1, acc, iova_start,
Don Wood0145f342009-03-06 15:15:00 -08002100 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002101
2102 if (ret == 0) {
2103 nesmr->ibmr.rkey = stag;
2104 nesmr->ibmr.lkey = stag;
2105 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2106 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002107 } else {
2108 kfree(nesmr);
2109 ibmr = ERR_PTR(-ENOMEM);
2110 }
2111
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002112reg_phys_err:
2113 /* single PBL case */
2114 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002115 return ibmr;
2116}
2117
2118
2119/**
2120 * nes_get_dma_mr
2121 */
2122static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2123{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002124 u64 kva = 0;
2125
2126 nes_debug(NES_DBG_MR, "\n");
2127
Christoph Hellwigf64054d2015-12-23 19:12:50 +01002128 return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002129}
2130
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002131/**
2132 * nes_reg_user_mr
2133 */
2134static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2135 u64 virt, int acc, struct ib_udata *udata)
2136{
2137 u64 iova_start;
2138 __le64 *pbl;
2139 u64 region_length;
2140 dma_addr_t last_dma_addr = 0;
2141 dma_addr_t first_dma_addr = 0;
2142 struct nes_pd *nespd = to_nespd(pd);
2143 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2144 struct nes_device *nesdev = nesvnic->nesdev;
2145 struct nes_adapter *nesadapter = nesdev->nesadapter;
2146 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002147 struct scatterlist *sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002148 struct nes_ucontext *nes_ucontext;
2149 struct nes_pbl *nespbl;
2150 struct nes_mr *nesmr;
2151 struct ib_umem *region;
2152 struct nes_mem_reg_req req;
2153 struct nes_vpbl vpbl;
2154 struct nes_root_vpbl root_vpbl;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002155 int entry, page_index;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002156 int page_count = 0;
2157 int err, pbl_depth = 0;
2158 int chunk_pages;
2159 int ret;
2160 u32 stag;
2161 u32 stag_index = 0;
2162 u32 next_stag_index;
2163 u32 driver_key;
2164 u32 root_pbl_index = 0;
2165 u32 cur_pbl_index = 0;
2166 u32 skip_pages;
2167 u16 pbl_count;
2168 u8 single_page = 1;
2169 u8 stag_key;
Yishai Hadaseeb84612014-01-28 13:40:15 +02002170 int first_page = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002171
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002172 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002173 if (IS_ERR(region)) {
2174 return (struct ib_mr *)region;
2175 }
2176
2177 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2178 " offset = %u, page size = %u.\n",
2179 (unsigned long int)start, (unsigned long int)virt, (u32)length,
Haggai Eran406f9e52014-12-11 17:04:12 +02002180 ib_umem_offset(region), region->page_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002181
Haggai Eran406f9e52014-12-11 17:04:12 +02002182 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002183
Julia Lawalle2e435f2011-10-06 09:33:24 -07002184 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2185 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002186 return ERR_PTR(-EFAULT);
Julia Lawalle2e435f2011-10-06 09:33:24 -07002187 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002188 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2189
2190 switch (req.reg_type) {
2191 case IWNES_MEMREG_TYPE_MEM:
2192 pbl_depth = 0;
2193 region_length = 0;
2194 vpbl.pbl_vbase = NULL;
2195 root_vpbl.pbl_vbase = NULL;
2196 root_vpbl.pbl_pbase = 0;
2197
2198 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2199 stag_key = (u8)next_stag_index;
2200
2201 driver_key = next_stag_index & 0x70000000;
2202
2203 next_stag_index >>= 8;
2204 next_stag_index %= nesadapter->max_mr;
2205
2206 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
Tatyana Nikolova81821642012-09-20 21:08:04 +00002207 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208 if (err) {
2209 ib_umem_release(region);
2210 return ERR_PTR(err);
2211 }
2212
2213 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2214 if (!nesmr) {
2215 ib_umem_release(region);
2216 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2217 return ERR_PTR(-ENOMEM);
2218 }
2219 nesmr->region = region;
2220
Yishai Hadaseeb84612014-01-28 13:40:15 +02002221 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2222 if (sg_dma_address(sg) & ~PAGE_MASK) {
2223 ib_umem_release(region);
2224 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2225 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2226 (unsigned int) sg_dma_address(sg));
2227 ibmr = ERR_PTR(-EINVAL);
2228 kfree(nesmr);
2229 goto reg_user_mr_err;
2230 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231
Yishai Hadaseeb84612014-01-28 13:40:15 +02002232 if (!sg_dma_len(sg)) {
2233 ib_umem_release(region);
2234 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2235 stag_index);
2236 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2237 ibmr = ERR_PTR(-EINVAL);
2238 kfree(nesmr);
2239 goto reg_user_mr_err;
2240 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002241
Yishai Hadaseeb84612014-01-28 13:40:15 +02002242 region_length += sg_dma_len(sg);
2243 chunk_pages = sg_dma_len(sg) >> 12;
2244 region_length -= skip_pages << 12;
2245 for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2246 skip_pages = 0;
Haggai Eran406f9e52014-12-11 17:04:12 +02002247 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
Yishai Hadaseeb84612014-01-28 13:40:15 +02002248 goto enough_pages;
2249 if ((page_count&0x01FF) == 0) {
2250 if (page_count >= 1024 * 512) {
2251 ib_umem_release(region);
2252 nes_free_resource(nesadapter,
2253 nesadapter->allocated_mrs, stag_index);
2254 kfree(nesmr);
2255 ibmr = ERR_PTR(-E2BIG);
2256 goto reg_user_mr_err;
2257 }
2258 if (root_pbl_index == 1) {
2259 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2260 8192, &root_vpbl.pbl_pbase);
2261 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2262 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2263 if (!root_vpbl.pbl_vbase) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264 ib_umem_release(region);
Yishai Hadaseeb84612014-01-28 13:40:15 +02002265 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2266 vpbl.pbl_pbase);
2267 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2268 stag_index);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002269 kfree(nesmr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002270 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002271 goto reg_user_mr_err;
2272 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002273 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2274 GFP_KERNEL);
2275 if (!root_vpbl.leaf_vpbl) {
2276 ib_umem_release(region);
2277 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2278 root_vpbl.pbl_pbase);
2279 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2280 vpbl.pbl_pbase);
2281 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2282 stag_index);
2283 kfree(nesmr);
2284 ibmr = ERR_PTR(-ENOMEM);
2285 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002286 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002287 root_vpbl.pbl_vbase[0].pa_low =
2288 cpu_to_le32((u32)vpbl.pbl_pbase);
2289 root_vpbl.pbl_vbase[0].pa_high =
2290 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2291 root_vpbl.leaf_vpbl[0] = vpbl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002292 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002293 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2294 &vpbl.pbl_pbase);
2295 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2296 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2297 if (!vpbl.pbl_vbase) {
2298 ib_umem_release(region);
2299 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2300 ibmr = ERR_PTR(-ENOMEM);
2301 kfree(nesmr);
2302 goto reg_user_mr_err;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002303 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002304 if (1 <= root_pbl_index) {
2305 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2306 cpu_to_le32((u32)vpbl.pbl_pbase);
2307 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2308 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2309 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2310 }
2311 root_pbl_index++;
2312 cur_pbl_index = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002313 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002314 if (single_page) {
2315 if (page_count != 0) {
2316 if ((last_dma_addr+4096) !=
2317 (sg_dma_address(sg)+
2318 (page_index*4096)))
2319 single_page = 0;
2320 last_dma_addr = sg_dma_address(sg)+
2321 (page_index*4096);
2322 } else {
2323 first_dma_addr = sg_dma_address(sg)+
2324 (page_index*4096);
2325 last_dma_addr = first_dma_addr;
2326 }
2327 }
2328
2329 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2330 cpu_to_le32((u32)(sg_dma_address(sg)+
2331 (page_index*4096)));
2332 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2333 cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2334 (page_index*4096))) >> 32)));
2335 cur_pbl_index++;
2336 page_count++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002337 }
2338 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002339
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002340 enough_pages:
2341 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2342 " stag_key=0x%08x\n",
2343 stag_index, driver_key, stag_key);
2344 stag = stag_index << 8;
2345 stag |= driver_key;
2346 stag += (u32)stag_key;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002347
2348 iova_start = virt;
2349 /* Make the leaf PBL the root if only one PBL */
2350 if (root_pbl_index == 1) {
2351 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2352 }
2353
2354 if (single_page) {
2355 pbl_count = 0;
2356 } else {
2357 pbl_count = root_pbl_index;
2358 first_dma_addr = 0;
2359 }
2360 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2361 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2362 stag, (unsigned int)iova_start,
2363 (unsigned int)region_length, stag_index,
2364 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002365 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2366 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2367 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002368
2369 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2370
2371 if (ret == 0) {
2372 nesmr->ibmr.rkey = stag;
2373 nesmr->ibmr.lkey = stag;
2374 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2375 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002376 } else {
2377 ib_umem_release(region);
2378 kfree(nesmr);
2379 ibmr = ERR_PTR(-ENOMEM);
2380 }
2381
2382 reg_user_mr_err:
2383 /* free the resources */
2384 if (root_pbl_index == 1) {
2385 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2386 vpbl.pbl_pbase);
2387 } else {
2388 for (page_index=0; page_index<root_pbl_index; page_index++) {
2389 pci_free_consistent(nesdev->pcidev, 4096,
2390 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2391 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2392 }
2393 kfree(root_vpbl.leaf_vpbl);
2394 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2395 root_vpbl.pbl_pbase);
2396 }
2397
2398 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2399
2400 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002401 case IWNES_MEMREG_TYPE_QP:
2402 case IWNES_MEMREG_TYPE_CQ:
Tatyana Nikolova7d9c1992012-12-06 20:05:02 +00002403 if (!region->length) {
2404 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2405 ib_umem_release(region);
2406 return ERR_PTR(-EINVAL);
2407 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002408 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2409 if (!nespbl) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002410 ib_umem_release(region);
2411 return ERR_PTR(-ENOMEM);
2412 }
2413 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2414 if (!nesmr) {
2415 ib_umem_release(region);
2416 kfree(nespbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002417 return ERR_PTR(-ENOMEM);
2418 }
2419 nesmr->region = region;
2420 nes_ucontext = to_nesucontext(pd->uobject->context);
2421 pbl_depth = region->length >> 12;
2422 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2423 nespbl->pbl_size = pbl_depth*sizeof(u64);
2424 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2425 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2426 } else {
2427 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2428 }
2429
2430 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2431 nespbl->pbl_size, pbl_depth);
2432 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2433 &nespbl->pbl_pbase);
2434 if (!pbl) {
2435 ib_umem_release(region);
2436 kfree(nesmr);
2437 kfree(nespbl);
2438 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2439 return ERR_PTR(-ENOMEM);
2440 }
2441
2442 nespbl->pbl_vbase = (u64 *)pbl;
2443 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002444 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002445 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002446 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2447 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002448
Yishai Hadaseeb84612014-01-28 13:40:15 +02002449 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2450 chunk_pages = sg_dma_len(sg) >> 12;
2451 chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2452 if (first_page) {
2453 nespbl->page = sg_page(sg);
2454 first_page = 0;
2455 }
2456
2457 for (page_index = 0; page_index < chunk_pages; page_index++) {
2458 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2459 (sg_dma_address(sg)+
2460 (page_index*4096)));
2461 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2462 (sg_dma_address(sg)+
2463 (page_index*4096)))>>32);
2464 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2465 (unsigned long long)*pbl,
2466 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2467 pbl++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002468 }
2469 }
Yishai Hadaseeb84612014-01-28 13:40:15 +02002470
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002471 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2472 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2473 } else {
2474 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2475 }
2476 nesmr->ibmr.rkey = -1;
2477 nesmr->ibmr.lkey = -1;
2478 nesmr->mode = req.reg_type;
2479 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002480 }
2481
Julia Lawalle2e435f2011-10-06 09:33:24 -07002482 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002483 return ERR_PTR(-ENOSYS);
2484}
2485
2486
2487/**
2488 * nes_dereg_mr
2489 */
2490static int nes_dereg_mr(struct ib_mr *ib_mr)
2491{
2492 struct nes_mr *nesmr = to_nesmr(ib_mr);
2493 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2494 struct nes_device *nesdev = nesvnic->nesdev;
2495 struct nes_adapter *nesadapter = nesdev->nesadapter;
2496 struct nes_hw_cqp_wqe *cqp_wqe;
2497 struct nes_cqp_request *cqp_request;
2498 unsigned long flags;
2499 int ret;
2500 u16 major_code;
2501 u16 minor_code;
2502
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03002503
2504 if (nesmr->pages)
2505 pci_free_consistent(nesdev->pcidev,
2506 nesmr->max_pages * sizeof(u64),
2507 nesmr->pages,
2508 nesmr->paddr);
2509
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002510 if (nesmr->region) {
2511 ib_umem_release(nesmr->region);
2512 }
2513 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2514 kfree(nesmr);
2515 return 0;
2516 }
2517
2518 /* Deallocate the region with the adapter */
2519
2520 cqp_request = nes_get_cqp_request(nesdev);
2521 if (cqp_request == NULL) {
2522 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2523 return -ENOMEM;
2524 }
2525 cqp_request->waiting = 1;
2526 cqp_wqe = &cqp_request->cqp_wqe;
2527
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002528 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2529 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2530 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2531 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2532 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2533
2534 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002535 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002536
2537 /* Wait for CQP */
2538 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2539 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2540 NES_EVENT_TIMEOUT);
2541 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2542 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2543 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2544
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002545 major_code = cqp_request->major_code;
2546 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002547
2548 nes_put_cqp_request(nesdev, cqp_request);
2549
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002550 if (!ret) {
2551 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2552 " ib_mr=%p, rkey = 0x%08X\n",
2553 ib_mr, ib_mr->rkey);
2554 return -ETIME;
2555 } else if (major_code) {
2556 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2557 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2558 major_code, minor_code, ib_mr, ib_mr->rkey);
2559 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002560 }
2561
2562 if (nesmr->pbls_used != 0) {
2563 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2564 if (nesmr->pbl_4k) {
2565 nesadapter->free_4kpbl += nesmr->pbls_used;
2566 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2567 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2568 "exceeded the max(%u)\n",
2569 nesadapter->free_4kpbl,
2570 nesadapter->max_4kpbl);
2571 } else {
2572 nesadapter->free_256pbl += nesmr->pbls_used;
2573 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2574 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2575 "exceeded the max(%u)\n",
2576 nesadapter->free_256pbl,
2577 nesadapter->max_256pbl);
2578 }
2579 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2580 }
2581 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2582 (ib_mr->rkey & 0x0fffff00) >> 8);
2583
2584 kfree(nesmr);
2585
2586 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002587}
2588
2589
2590/**
2591 * show_rev
2592 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002593static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2594 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002595{
2596 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002597 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2599
2600 nes_debug(NES_DBG_INIT, "\n");
2601 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2602}
2603
2604
2605/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002606 * show_hca
2607 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002608static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2609 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002610{
2611 nes_debug(NES_DBG_INIT, "\n");
2612 return sprintf(buf, "NES020\n");
2613}
2614
2615
2616/**
2617 * show_board
2618 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002619static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2620 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002621{
2622 nes_debug(NES_DBG_INIT, "\n");
2623 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2624}
2625
2626
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002627static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002628static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2629static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002630
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002631static struct device_attribute *nes_dev_attributes[] = {
2632 &dev_attr_hw_rev,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002633 &dev_attr_hca_type,
2634 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002635};
2636
2637
2638/**
2639 * nes_query_qp
2640 */
2641static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2642 int attr_mask, struct ib_qp_init_attr *init_attr)
2643{
2644 struct nes_qp *nesqp = to_nesqp(ibqp);
2645
2646 nes_debug(NES_DBG_QP, "\n");
2647
2648 attr->qp_access_flags = 0;
2649 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2650 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2651 attr->cap.max_recv_sge = 1;
Chien Tungeadde3a2010-03-25 13:39:50 +00002652 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2653 attr->cap.max_inline_data = 0;
2654 else
2655 attr->cap.max_inline_data = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002656
2657 init_attr->event_handler = nesqp->ibqp.event_handler;
2658 init_attr->qp_context = nesqp->ibqp.qp_context;
2659 init_attr->send_cq = nesqp->ibqp.send_cq;
2660 init_attr->recv_cq = nesqp->ibqp.recv_cq;
Dave Jones4127c362013-09-17 17:45:54 -04002661 init_attr->srq = nesqp->ibqp.srq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002662 init_attr->cap = attr->cap;
2663
2664 return 0;
2665}
2666
2667
2668/**
2669 * nes_hw_modify_qp
2670 */
2671int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002672 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002673{
2674 struct nes_hw_cqp_wqe *cqp_wqe;
2675 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2676 /* struct iw_cm_event cm_event; */
2677 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002678 int ret;
2679 u16 major_code;
2680
2681 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2682 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2683
2684 cqp_request = nes_get_cqp_request(nesdev);
2685 if (cqp_request == NULL) {
2686 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2687 return -ENOMEM;
2688 }
2689 if (wait_completion) {
2690 cqp_request->waiting = 1;
2691 } else {
2692 cqp_request->waiting = 0;
2693 }
2694 cqp_wqe = &cqp_request->cqp_wqe;
2695
2696 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2697 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2698 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2699 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2700 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2701 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2702 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2703
Don Wood8b1c9dc2009-09-05 20:36:38 -07002704 /* If sending a terminate message, fill in the length (in words) */
2705 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2706 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2707 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2708 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2709 }
2710
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002712 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002713
2714 /* Wait for CQP */
2715 if (wait_completion) {
2716 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2717 nesqp->hwqp.qp_id); */
2718 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2719 NES_EVENT_TIMEOUT);
2720 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2721 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2722 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2723 major_code = cqp_request->major_code;
2724 if (major_code) {
2725 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2726 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2727 nesqp->hwqp.qp_id, cqp_request->major_code,
2728 cqp_request->minor_code, next_iwarp_state);
2729 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002730
2731 nes_put_cqp_request(nesdev, cqp_request);
2732
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002733 if (!ret)
2734 return -ETIME;
2735 else if (major_code)
2736 return -EIO;
2737 else
2738 return 0;
2739 } else {
2740 return 0;
2741 }
2742}
2743
2744
2745/**
2746 * nes_modify_qp
2747 */
2748int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2749 int attr_mask, struct ib_udata *udata)
2750{
2751 struct nes_qp *nesqp = to_nesqp(ibqp);
2752 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2753 struct nes_device *nesdev = nesvnic->nesdev;
2754 /* u32 cqp_head; */
2755 /* u32 counter; */
2756 u32 next_iwarp_state = 0;
2757 int err;
2758 unsigned long qplockflags;
2759 int ret;
2760 u16 original_last_aeq;
2761 u8 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002762 u8 dont_wait = 0;
2763
2764 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2765 " iwarp_state=0x%X, refcount=%d\n",
2766 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2767 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2768
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002769 spin_lock_irqsave(&nesqp->lock, qplockflags);
2770
2771 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2772 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2773 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2774 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2775
2776 if (attr_mask & IB_QP_STATE) {
2777 switch (attr->qp_state) {
2778 case IB_QPS_INIT:
2779 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2780 nesqp->hwqp.qp_id);
2781 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2782 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783 return -EINVAL;
2784 }
2785 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2786 issue_modify_qp = 1;
2787 break;
2788 case IB_QPS_RTR:
2789 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2790 nesqp->hwqp.qp_id);
2791 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2792 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002793 return -EINVAL;
2794 }
2795 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2796 issue_modify_qp = 1;
2797 break;
2798 case IB_QPS_RTS:
2799 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2800 nesqp->hwqp.qp_id);
2801 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2802 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002803 return -EINVAL;
2804 }
2805 if (nesqp->cm_id == NULL) {
2806 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2807 nesqp->hwqp.qp_id );
2808 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002809 return -EINVAL;
2810 }
2811 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2812 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2813 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2814 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2815 issue_modify_qp = 1;
2816 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2817 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2818 nesqp->hte_added = 1;
2819 break;
2820 case IB_QPS_SQD:
2821 issue_modify_qp = 1;
2822 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2823 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2824 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2825 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826 return 0;
2827 } else {
2828 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2829 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2830 " ignored due to current iWARP state\n",
2831 nesqp->hwqp.qp_id);
2832 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002833 return -EINVAL;
2834 }
2835 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2836 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2837 " already done based on hw state.\n",
2838 nesqp->hwqp.qp_id);
2839 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002840 }
2841 switch (nesqp->hw_iwarp_state) {
2842 case NES_AEQE_IWARP_STATE_CLOSING:
2843 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Tatyana Nikolovad5fb4762012-10-02 16:38:12 +00002844 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002845 case NES_AEQE_IWARP_STATE_TERMINATE:
2846 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2847 break;
2848 case NES_AEQE_IWARP_STATE_ERROR:
2849 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2850 break;
2851 default:
2852 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002853 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2854 break;
2855 }
2856 }
2857 break;
2858 case IB_QPS_SQE:
2859 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2860 nesqp->hwqp.qp_id);
2861 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2862 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002863 return -EINVAL;
2864 }
2865 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2866 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2867 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2868 issue_modify_qp = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002869 break;
2870 case IB_QPS_ERR:
2871 case IB_QPS_RESET:
2872 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2873 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002874 return -EINVAL;
2875 }
2876 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2877 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07002878 if (nesqp->term_flags)
2879 del_timer(&nesqp->terminate_timer);
2880
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002881 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2882 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2883 if (nesqp->hte_added) {
2884 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2885 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2886 nesqp->hte_added = 0;
2887 }
2888 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
Faisal Latifcd6860e2010-07-04 00:17:59 +00002889 (nesdev->iw_status) &&
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002890 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2891 next_iwarp_state |= NES_CQP_QP_RESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 } else {
2893 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2894 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2895 dont_wait = 1;
2896 }
2897 issue_modify_qp = 1;
2898 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2899 break;
2900 default:
2901 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002902 return -EINVAL;
2903 break;
2904 }
2905
2906 nesqp->ibqp_state = attr->qp_state;
Tatyana Nikolovaa67b0782012-10-02 16:40:33 +00002907 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2908 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2909 nesqp->iwarp_state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002910 }
2911
2912 if (attr_mask & IB_QP_ACCESS_FLAGS) {
2913 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2914 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2915 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2916 issue_modify_qp = 1;
2917 }
2918 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2919 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2920 issue_modify_qp = 1;
2921 }
2922 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2923 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2924 issue_modify_qp = 1;
2925 }
2926 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2927 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2928 issue_modify_qp = 1;
2929 }
2930
2931 if (nesqp->user_mode) {
2932 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2933 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2934 issue_modify_qp = 1;
2935 }
2936 }
2937
2938 original_last_aeq = nesqp->last_aeq;
2939 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2940
2941 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2942
2943 ret = 0;
2944
2945
2946 if (issue_modify_qp) {
2947 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07002948 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002949 if (ret)
2950 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2951 " failed for QP%u.\n",
2952 next_iwarp_state, nesqp->hwqp.qp_id);
2953
2954 }
2955
2956 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2957 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2958 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2959 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2960 original_last_aeq, nesqp->last_aeq);
Dan Carpenter349850f2014-02-06 15:48:23 +03002961 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002962 if (dont_wait) {
2963 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2964 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2965 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2966 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2967 original_last_aeq, nesqp->last_aeq);
2968 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002969 spin_lock_irqsave(&nesqp->lock, qplockflags);
2970 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2971 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2972 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2973 nes_cm_disconn(nesqp);
2974 } else {
2975 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2976 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002977 }
2978 } else {
2979 spin_lock_irqsave(&nesqp->lock, qplockflags);
2980 if (nesqp->cm_id) {
2981 /* These two are for the timer thread */
2982 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002983 nesqp->cm_id->add_ref(nesqp->cm_id);
2984 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2985 " need ae to finish up, original_last_aeq = 0x%04X."
2986 " last_aeq = 0x%04X, scheduling timer.\n",
2987 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2988 original_last_aeq, nesqp->last_aeq);
2989 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2990 }
2991 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2992 } else {
2993 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2994 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2995 " need ae to finish up, original_last_aeq = 0x%04X."
2996 " last_aeq = 0x%04X.\n",
2997 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2998 original_last_aeq, nesqp->last_aeq);
2999 }
3000 }
3001 } else {
3002 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3003 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3004 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3005 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006 }
3007 } else {
3008 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3009 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3010 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3011 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003012 }
3013
3014 err = 0;
3015
3016 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3017 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3018
3019 return err;
3020}
3021
3022
3023/**
3024 * nes_muticast_attach
3025 */
3026static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3027{
3028 nes_debug(NES_DBG_INIT, "\n");
3029 return -ENOSYS;
3030}
3031
3032
3033/**
3034 * nes_multicast_detach
3035 */
3036static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3037{
3038 nes_debug(NES_DBG_INIT, "\n");
3039 return -ENOSYS;
3040}
3041
3042
3043/**
3044 * nes_process_mad
3045 */
3046static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
Ira Weinya97e2d82015-05-31 17:15:30 -04003047 u8 port_num, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -04003048 const struct ib_mad_hdr *in, size_t in_mad_size,
3049 struct ib_mad_hdr *out, size_t *out_mad_size,
3050 u16 *out_mad_pkey_index)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051{
3052 nes_debug(NES_DBG_INIT, "\n");
3053 return -ENOSYS;
3054}
3055
3056static inline void
3057fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3058{
3059 int sge_index;
3060 int total_payload_length = 0;
3061 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3062 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3063 ib_wr->sg_list[sge_index].addr);
3064 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3065 ib_wr->sg_list[sge_index].length);
3066 if (uselkey)
3067 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3068 (ib_wr->sg_list[sge_index].lkey));
3069 else
3070 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3071
3072 total_payload_length += ib_wr->sg_list[sge_index].length;
3073 }
3074 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3075 total_payload_length);
3076 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3077 total_payload_length);
3078}
3079
3080/**
3081 * nes_post_send
3082 */
3083static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3084 struct ib_send_wr **bad_wr)
3085{
3086 u64 u64temp;
3087 unsigned long flags = 0;
3088 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3089 struct nes_device *nesdev = nesvnic->nesdev;
3090 struct nes_qp *nesqp = to_nesqp(ibqp);
3091 struct nes_hw_qp_wqe *wqe;
Chien Tung649fe4a2009-12-09 13:51:37 -08003092 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003093 u32 qsize = nesqp->hwqp.sq_size;
3094 u32 head;
Chien Tung649fe4a2009-12-09 13:51:37 -08003095 u32 wqe_misc = 0;
3096 u32 wqe_count = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003097 u32 counter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003098
Frank Zagoe5dec392009-12-09 13:51:36 -08003099 if (nesqp->ibqp_state > IB_QPS_RTS) {
3100 err = -EINVAL;
3101 goto out;
3102 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003103
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003104 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003105
3106 head = nesqp->hwqp.sq_head;
3107
3108 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003109 /* Check for QP error */
3110 if (nesqp->term_flags) {
3111 err = -EINVAL;
3112 break;
3113 }
3114
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003115 /* Check for SQ overflow */
3116 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003117 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003118 break;
3119 }
3120
3121 wqe = &nesqp->hwqp.sq_vbase[head];
3122 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3123 nesqp->hwqp.qp_id, wqe, head); */
3124 nes_fill_init_qp_wqe(wqe, nesqp, head);
3125 u64temp = (u64)(ib_wr->wr_id);
3126 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3127 u64temp);
Chien Tung649fe4a2009-12-09 13:51:37 -08003128 switch (ib_wr->opcode) {
3129 case IB_WR_SEND:
3130 case IB_WR_SEND_WITH_INV:
3131 if (IB_WR_SEND == ib_wr->opcode) {
3132 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3133 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3134 else
3135 wqe_misc = NES_IWARP_SQ_OP_SEND;
3136 } else {
3137 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3138 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3139 else
3140 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003141
Chien Tung649fe4a2009-12-09 13:51:37 -08003142 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3143 ib_wr->ex.invalidate_rkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003144 }
3145
Chien Tung649fe4a2009-12-09 13:51:37 -08003146 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3147 err = -EINVAL;
3148 break;
3149 }
3150
3151 if (ib_wr->send_flags & IB_SEND_FENCE)
3152 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3153
3154 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3155 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3156 (ib_wr->sg_list[0].length <= 64)) {
3157 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3158 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3159 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3160 ib_wr->sg_list[0].length);
3161 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3162 } else {
3163 fill_wqe_sg_send(wqe, ib_wr, 1);
3164 }
3165
3166 break;
3167 case IB_WR_RDMA_WRITE:
3168 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3169 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3170 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3171 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3172 err = -EINVAL;
3173 break;
3174 }
3175
3176 if (ib_wr->send_flags & IB_SEND_FENCE)
3177 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3178
3179 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003180 rdma_wr(ib_wr)->rkey);
Chien Tung649fe4a2009-12-09 13:51:37 -08003181 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003182 rdma_wr(ib_wr)->remote_addr);
Chien Tung649fe4a2009-12-09 13:51:37 -08003183
3184 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3185 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3186 (ib_wr->sg_list[0].length <= 64)) {
3187 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3188 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3189 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3190 ib_wr->sg_list[0].length);
3191 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3192 } else {
3193 fill_wqe_sg_send(wqe, ib_wr, 1);
3194 }
3195
3196 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3197 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3198 break;
3199 case IB_WR_RDMA_READ:
3200 case IB_WR_RDMA_READ_WITH_INV:
3201 /* iWARP only supports 1 sge for RDMA reads */
3202 if (ib_wr->num_sge > 1) {
3203 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3204 ib_wr->num_sge);
3205 err = -EINVAL;
3206 break;
3207 }
3208 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3209 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3210 } else {
3211 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3212 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3213 ib_wr->ex.invalidate_rkey);
3214 }
3215
3216 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003217 rdma_wr(ib_wr)->remote_addr);
Chien Tung649fe4a2009-12-09 13:51:37 -08003218 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003219 rdma_wr(ib_wr)->rkey);
Chien Tung649fe4a2009-12-09 13:51:37 -08003220 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3221 ib_wr->sg_list->length);
3222 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3223 ib_wr->sg_list->addr);
3224 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3225 ib_wr->sg_list->lkey);
3226 break;
3227 case IB_WR_LOCAL_INV:
3228 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3229 set_wqe_32bit_value(wqe->wqe_words,
3230 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3231 ib_wr->ex.invalidate_rkey);
3232 break;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03003233 case IB_WR_REG_MR:
3234 {
3235 struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3236 int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3237 int flags = reg_wr(ib_wr)->access;
3238
3239 if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3240 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3241 err = -EINVAL;
3242 break;
3243 }
3244 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3245 set_wqe_64bit_value(wqe->wqe_words,
3246 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3247 mr->ibmr.iova);
3248 set_wqe_32bit_value(wqe->wqe_words,
3249 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3250 mr->ibmr.length);
3251 set_wqe_32bit_value(wqe->wqe_words,
3252 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3253 set_wqe_32bit_value(wqe->wqe_words,
3254 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3255 reg_wr(ib_wr)->key);
3256
3257 if (page_shift == 12) {
3258 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3259 } else if (page_shift == 21) {
3260 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3261 } else {
3262 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3263 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3264 err = -EINVAL;
3265 break;
3266 }
3267
3268 /* Set access_flags */
3269 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3270 if (flags & IB_ACCESS_LOCAL_WRITE)
3271 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3272
3273 if (flags & IB_ACCESS_REMOTE_WRITE)
3274 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3275
3276 if (flags & IB_ACCESS_REMOTE_READ)
3277 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3278
3279 if (flags & IB_ACCESS_MW_BIND)
3280 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3281
3282 /* Fill in PBL info: */
3283 set_wqe_64bit_value(wqe->wqe_words,
3284 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3285 mr->paddr);
3286
3287 set_wqe_32bit_value(wqe->wqe_words,
3288 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3289 mr->npages * 8);
3290
3291 nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3292 "length: %d, rkey: %0x, pgl_paddr: %llx, "
3293 "page_list_len: %u, wqe_misc: %x\n",
3294 (unsigned long long) mr->ibmr.iova,
3295 mr->ibmr.length,
3296 reg_wr(ib_wr)->key,
3297 (unsigned long long) mr->paddr,
3298 mr->npages,
3299 wqe_misc);
3300 break;
3301 }
Chien Tung649fe4a2009-12-09 13:51:37 -08003302 default:
3303 /* error */
3304 err = -EINVAL;
3305 break;
3306 }
3307
Frank Zagoe5dec392009-12-09 13:51:36 -08003308 if (err)
3309 break;
3310
Chien Tungd14152d2009-12-09 15:21:56 -08003311 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003312 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
Chien Tungd14152d2009-12-09 15:21:56 -08003313
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003314 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3315
3316 ib_wr = ib_wr->next;
3317 head++;
3318 wqe_count++;
3319 if (head >= qsize)
3320 head = 0;
3321
3322 }
3323
3324 nesqp->hwqp.sq_head = head;
3325 barrier();
3326 while (wqe_count) {
3327 counter = min(wqe_count, ((u32)255));
3328 wqe_count -= counter;
3329 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3330 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3331 }
3332
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003333 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003334
Frank Zagoe5dec392009-12-09 13:51:36 -08003335out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003336 if (err)
3337 *bad_wr = ib_wr;
3338 return err;
3339}
3340
3341
3342/**
3343 * nes_post_recv
3344 */
3345static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3346 struct ib_recv_wr **bad_wr)
3347{
3348 u64 u64temp;
3349 unsigned long flags = 0;
3350 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3351 struct nes_device *nesdev = nesvnic->nesdev;
3352 struct nes_qp *nesqp = to_nesqp(ibqp);
3353 struct nes_hw_qp_wqe *wqe;
3354 int err = 0;
3355 int sge_index;
3356 u32 qsize = nesqp->hwqp.rq_size;
3357 u32 head;
3358 u32 wqe_count = 0;
3359 u32 counter;
3360 u32 total_payload_length;
3361
Frank Zago4293fdc12009-12-09 13:51:36 -08003362 if (nesqp->ibqp_state > IB_QPS_RTS) {
3363 err = -EINVAL;
3364 goto out;
3365 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003366
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003367 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003368
3369 head = nesqp->hwqp.rq_head;
3370
3371 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003372 /* Check for QP error */
3373 if (nesqp->term_flags) {
3374 err = -EINVAL;
3375 break;
3376 }
3377
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003378 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3379 err = -EINVAL;
3380 break;
3381 }
3382 /* Check for RQ overflow */
3383 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
Or Gerlitz831d06c2010-02-04 11:25:07 +00003384 err = -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003385 break;
3386 }
3387
3388 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3389 wqe = &nesqp->hwqp.rq_vbase[head];
3390
3391 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3392 nesqp->hwqp.qp_id, wqe, head); */
3393 nes_fill_init_qp_wqe(wqe, nesqp, head);
3394 u64temp = (u64)(ib_wr->wr_id);
3395 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3396 u64temp);
3397 total_payload_length = 0;
3398 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3399 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3400 ib_wr->sg_list[sge_index].addr);
3401 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3402 ib_wr->sg_list[sge_index].length);
3403 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3404 ib_wr->sg_list[sge_index].lkey);
3405
3406 total_payload_length += ib_wr->sg_list[sge_index].length;
3407 }
3408 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3409 total_payload_length);
3410
3411 ib_wr = ib_wr->next;
3412 head++;
3413 wqe_count++;
3414 if (head >= qsize)
3415 head = 0;
3416 }
3417
3418 nesqp->hwqp.rq_head = head;
3419 barrier();
3420 while (wqe_count) {
3421 counter = min(wqe_count, ((u32)255));
3422 wqe_count -= counter;
3423 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3424 }
3425
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003426 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003427
Frank Zago4293fdc12009-12-09 13:51:36 -08003428out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003429 if (err)
3430 *bad_wr = ib_wr;
3431 return err;
3432}
3433
Tatyana Nikolova19974122016-03-29 12:58:37 -05003434/**
3435 * nes_drain_sq - drain sq
3436 * @ibqp: pointer to ibqp
3437 */
3438static void nes_drain_sq(struct ib_qp *ibqp)
3439{
3440 struct nes_qp *nesqp = to_nesqp(ibqp);
3441
3442 if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3443 wait_for_completion(&nesqp->sq_drained);
3444}
3445
3446/**
3447 * nes_drain_rq - drain rq
3448 * @ibqp: pointer to ibqp
3449 */
3450static void nes_drain_rq(struct ib_qp *ibqp)
3451{
3452 struct nes_qp *nesqp = to_nesqp(ibqp);
3453
3454 if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3455 wait_for_completion(&nesqp->rq_drained);
3456}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003457
3458/**
3459 * nes_poll_cq
3460 */
3461static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3462{
3463 u64 u64temp;
3464 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003465 unsigned long flags = 0;
3466 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3467 struct nes_device *nesdev = nesvnic->nesdev;
3468 struct nes_cq *nescq = to_nescq(ibcq);
3469 struct nes_qp *nesqp;
3470 struct nes_hw_cqe cqe;
3471 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003472 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003473 u32 cq_size;
3474 u32 cqe_count = 0;
3475 u32 wqe_index;
3476 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003477 u32 move_cq_head = 1;
3478 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003479
3480 nes_debug(NES_DBG_CQ, "\n");
3481
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003482 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003483
3484 head = nescq->hw_cq.cq_head;
3485 cq_size = nescq->hw_cq.cq_size;
3486
3487 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003488 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3489 NES_CQE_VALID) == 0)
3490 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003491
Don Wood5ee21fe2009-09-05 20:36:37 -07003492 /*
3493 * Make sure we read CQ entry contents *after*
3494 * we've checked the valid bit.
3495 */
3496 rmb();
3497
3498 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003499 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3500 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3501 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3502 /* parse CQE, get completion context from WQE (either rq or sq) */
3503 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3504 ((u64)u32temp);
3505
3506 if (u64temp) {
3507 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003508 memset(entry, 0, sizeof *entry);
3509 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3510 entry->status = IB_WC_SUCCESS;
3511 } else {
Don Wood320cdfd2009-09-05 20:36:39 -07003512 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3513 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3514 entry->status = err_code & 0x0000ffff;
3515
3516 /* The rest of the cqe's will be marked as flushed */
3517 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3518 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3519 NES_IWARP_CQE_MINOR_FLUSH);
3520 } else
3521 entry->status = IB_WC_WR_FLUSH_ERR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003522 }
3523
3524 entry->qp = &nesqp->ibqp;
3525 entry->src_qp = nesqp->hwqp.qp_id;
3526
3527 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3528 if (nesqp->skip_lsmm) {
3529 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003530 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531 }
3532
3533 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003534 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003535 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003536 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003537 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003538 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003539 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3540
Don Wood6eed5e72009-09-05 20:36:39 -07003541 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003542 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3543 case NES_IWARP_SQ_OP_RDMAW:
3544 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3545 entry->opcode = IB_WC_RDMA_WRITE;
3546 break;
3547 case NES_IWARP_SQ_OP_RDMAR:
3548 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3549 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003550 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3552 break;
3553 case NES_IWARP_SQ_OP_SENDINV:
3554 case NES_IWARP_SQ_OP_SENDSEINV:
3555 case NES_IWARP_SQ_OP_SEND:
3556 case NES_IWARP_SQ_OP_SENDSE:
3557 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3558 entry->opcode = IB_WC_SEND;
3559 break;
Chien Tung649fe4a2009-12-09 13:51:37 -08003560 case NES_IWARP_SQ_OP_LOCINV:
Tatyana Nikolova4a4b03f2012-01-06 18:17:19 -06003561 entry->opcode = IB_WC_LOCAL_INV;
Chien Tung649fe4a2009-12-09 13:51:37 -08003562 break;
Chien Tunge293a262009-12-09 15:21:54 -08003563 case NES_IWARP_SQ_OP_FAST_REG:
Sagi Grimberg13d3e892015-10-13 19:11:48 +03003564 entry->opcode = IB_WC_REG_MR;
Chien Tunge293a262009-12-09 15:21:54 -08003565 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003566 }
Don Wood6eed5e72009-09-05 20:36:39 -07003567
3568 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3569 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3570 move_cq_head = 0;
3571 wq_tail = nesqp->hwqp.sq_tail;
3572 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003573 } else {
3574 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003575 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003576 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3577 ((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 -08003578 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003579
3580 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3581 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3582 move_cq_head = 0;
3583 wq_tail = nesqp->hwqp.rq_tail;
3584 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003585 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003586
Tatyana Nikolova19974122016-03-29 12:58:37 -05003587 if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3588 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3589 complete(&nesqp->sq_drained);
3590 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3591 complete(&nesqp->rq_drained);
3592 }
3593
Don Wood5ee21fe2009-09-05 20:36:37 -07003594 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003595 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003596 cqe_count++;
3597 }
3598
Don Wood6eed5e72009-09-05 20:36:39 -07003599 if (move_cq_head) {
3600 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3601 if (++head >= cq_size)
3602 head = 0;
3603 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003604
Don Wood6eed5e72009-09-05 20:36:39 -07003605 if ((nescq->polled_completions > (cq_size / 2)) ||
3606 (nescq->polled_completions == 255)) {
3607 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003608 " are pending %u of %u.\n",
3609 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003610 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003611 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003612 nescq->polled_completions = 0;
3613 }
3614 } else {
3615 /* Update the wqe index and set status to flush */
3616 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3617 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3618 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3619 cpu_to_le32(wqe_index);
3620 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003621 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003622 }
3623
3624 if (nescq->polled_completions) {
3625 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3626 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3627 nescq->polled_completions = 0;
3628 }
3629
3630 nescq->hw_cq.cq_head = head;
3631 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3632 cqe_count, nescq->hw_cq.cq_number);
3633
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003634 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003635
3636 return cqe_count;
3637}
3638
3639
3640/**
3641 * nes_req_notify_cq
3642 */
3643static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3644 {
3645 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3646 struct nes_device *nesdev = nesvnic->nesdev;
3647 struct nes_cq *nescq = to_nescq(ibcq);
3648 u32 cq_arm;
3649
3650 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3651 nescq->hw_cq.cq_number);
3652
3653 cq_arm = nescq->hw_cq.cq_number;
3654 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3655 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3656 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3657 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3658 else
3659 return -EINVAL;
3660
3661 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3662 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3663
3664 return 0;
3665}
3666
Ira Weiny77386132015-05-13 20:02:58 -04003667static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3668 struct ib_port_immutable *immutable)
3669{
3670 struct ib_port_attr attr;
3671 int err;
3672
Or Gerlitzc4550c62017-01-24 13:02:39 +02003673 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3674
Ira Weiny77386132015-05-13 20:02:58 -04003675 err = nes_query_port(ibdev, port_num, &attr);
3676 if (err)
3677 return err;
3678
3679 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3680 immutable->gid_tbl_len = attr.gid_tbl_len;
3681
3682 return 0;
3683}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003684
Ira Weiny96357452016-06-15 02:22:03 -04003685static void get_dev_fw_str(struct ib_device *dev, char *str,
3686 size_t str_len)
3687{
3688 struct nes_ib_device *nesibdev =
3689 container_of(dev, struct nes_ib_device, ibdev);
3690 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3691
3692 nes_debug(NES_DBG_INIT, "\n");
3693 snprintf(str, str_len, "%u.%u",
3694 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3695 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3696}
3697
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003698/**
3699 * nes_init_ofa_device
3700 */
3701struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3702{
3703 struct nes_ib_device *nesibdev;
3704 struct nes_vnic *nesvnic = netdev_priv(netdev);
3705 struct nes_device *nesdev = nesvnic->nesdev;
3706
3707 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3708 if (nesibdev == NULL) {
3709 return NULL;
3710 }
3711 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3712 nesibdev->ibdev.owner = THIS_MODULE;
3713
3714 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3715 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3716 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3717
3718 nesibdev->ibdev.uverbs_cmd_mask =
3719 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3720 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3721 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3722 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3723 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3724 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3725 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3726 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3727 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3728 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3729 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3730 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3731 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3732 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3733 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3734 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3735 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3736 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3737 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3738 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3739 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3740 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3741
3742 nesibdev->ibdev.phys_port_cnt = 1;
3743 nesibdev->ibdev.num_comp_vectors = 1;
3744 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003745 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003746 nesibdev->ibdev.query_device = nes_query_device;
3747 nesibdev->ibdev.query_port = nes_query_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003748 nesibdev->ibdev.query_pkey = nes_query_pkey;
3749 nesibdev->ibdev.query_gid = nes_query_gid;
3750 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3751 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3752 nesibdev->ibdev.mmap = nes_mmap;
3753 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3754 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3755 nesibdev->ibdev.create_ah = nes_create_ah;
3756 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3757 nesibdev->ibdev.create_qp = nes_create_qp;
3758 nesibdev->ibdev.modify_qp = nes_modify_qp;
3759 nesibdev->ibdev.query_qp = nes_query_qp;
3760 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3761 nesibdev->ibdev.create_cq = nes_create_cq;
3762 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3763 nesibdev->ibdev.poll_cq = nes_poll_cq;
3764 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003765 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3766 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3767 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3768 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003769
Sagi Grimberge02e4d552015-07-30 10:32:46 +03003770 nesibdev->ibdev.alloc_mr = nes_alloc_mr;
Sagi Grimberg0ba24dd2015-10-13 19:11:32 +03003771 nesibdev->ibdev.map_mr_sg = nes_map_mr_sg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003772
3773 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3774 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3775 nesibdev->ibdev.process_mad = nes_process_mad;
3776
3777 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3778 nesibdev->ibdev.post_send = nes_post_send;
3779 nesibdev->ibdev.post_recv = nes_post_recv;
Tatyana Nikolova19974122016-03-29 12:58:37 -05003780 nesibdev->ibdev.drain_sq = nes_drain_sq;
3781 nesibdev->ibdev.drain_rq = nes_drain_rq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003782
3783 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3784 if (nesibdev->ibdev.iwcm == NULL) {
3785 ib_dealloc_device(&nesibdev->ibdev);
3786 return NULL;
3787 }
3788 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3789 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3790 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3791 nesibdev->ibdev.iwcm->connect = nes_connect;
3792 nesibdev->ibdev.iwcm->accept = nes_accept;
3793 nesibdev->ibdev.iwcm->reject = nes_reject;
3794 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3795 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
Ira Weiny77386132015-05-13 20:02:58 -04003796 nesibdev->ibdev.get_port_immutable = nes_port_immutable;
Ira Weiny96357452016-06-15 02:22:03 -04003797 nesibdev->ibdev.get_dev_fw_str = get_dev_fw_str;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003798 memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3799 sizeof(nesibdev->ibdev.iwcm->ifname));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003800
3801 return nesibdev;
3802}
3803
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003804
3805/**
3806 * nes_handle_delayed_event
3807 */
3808static void nes_handle_delayed_event(unsigned long data)
3809{
3810 struct nes_vnic *nesvnic = (void *) data;
3811
3812 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3813 struct ib_event event;
3814
3815 event.device = &nesvnic->nesibdev->ibdev;
3816 if (!event.device)
3817 goto stop_timer;
3818 event.event = nesvnic->delayed_event;
3819 event.element.port_num = nesvnic->logical_port + 1;
3820 ib_dispatch_event(&event);
3821 }
3822
3823stop_timer:
3824 nesvnic->event_timer.function = NULL;
3825}
3826
3827
Faisal Latifcd6860e2010-07-04 00:17:59 +00003828void nes_port_ibevent(struct nes_vnic *nesvnic)
3829{
3830 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3831 struct nes_device *nesdev = nesvnic->nesdev;
3832 struct ib_event event;
3833 event.device = &nesibdev->ibdev;
3834 event.element.port_num = nesvnic->logical_port + 1;
3835 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Maciej Sosnowskiea623452010-11-24 17:29:38 +00003836
3837 if (!nesvnic->event_timer.function) {
3838 ib_dispatch_event(&event);
3839 nesvnic->last_dispatched_event = event.event;
3840 nesvnic->event_timer.function = nes_handle_delayed_event;
3841 nesvnic->event_timer.data = (unsigned long) nesvnic;
3842 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3843 add_timer(&nesvnic->event_timer);
3844 } else {
3845 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3846 }
3847 nesvnic->delayed_event = event.event;
Faisal Latifcd6860e2010-07-04 00:17:59 +00003848}
3849
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003850
3851/**
3852 * nes_destroy_ofa_device
3853 */
3854void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3855{
3856 if (nesibdev == NULL)
3857 return;
3858
3859 nes_unregister_ofa_device(nesibdev);
3860
3861 kfree(nesibdev->ibdev.iwcm);
3862 ib_dealloc_device(&nesibdev->ibdev);
3863}
3864
3865
3866/**
3867 * nes_register_ofa_device
3868 */
3869int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3870{
3871 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3872 struct nes_device *nesdev = nesvnic->nesdev;
3873 struct nes_adapter *nesadapter = nesdev->nesadapter;
3874 int i, ret;
3875
Ralph Campbell9a6edb62010-05-06 17:03:25 -07003876 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003877 if (ret) {
3878 return ret;
3879 }
3880
3881 /* Get the resources allocated to this device */
3882 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3883 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3884 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3885 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3886
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003887 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3888 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003889 if (ret) {
3890 while (i > 0) {
3891 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003892 device_remove_file(&nesibdev->ibdev.dev,
3893 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003894 }
3895 ib_unregister_device(&nesibdev->ibdev);
3896 return ret;
3897 }
3898 }
3899
3900 nesvnic->of_device_registered = 1;
3901
3902 return 0;
3903}
3904
3905
3906/**
3907 * nes_unregister_ofa_device
3908 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003909static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003910{
3911 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3912 int i;
3913
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003914 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3915 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003916 }
3917
3918 if (nesvnic->of_device_registered) {
3919 ib_unregister_device(&nesibdev->ibdev);
3920 }
3921
3922 nesvnic->of_device_registered = 0;
3923}