blob: 67a87cb9d7734f3ef6dfb5e63df3f177cf2238be [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungfa6c87d2009-12-09 15:21:56 -08002 * Copyright (c) 2006 - 2009 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>
38#include <asm/byteorder.h>
39
40#include <rdma/ib_verbs.h>
41#include <rdma/iw_cm.h>
42#include <rdma/ib_user_verbs.h>
43
44#include "nes.h"
45
46#include <rdma/ib_umem.h>
47
48atomic_t mod_qp_timouts;
49atomic_t qps_created;
50atomic_t sw_qps_destroyed;
51
Roland Dreier1a855fbf2008-04-16 21:01:09 -070052static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080053
54/**
55 * nes_alloc_mw
56 */
57static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -080058 struct nes_pd *nespd = to_nespd(ibpd);
59 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
60 struct nes_device *nesdev = nesvnic->nesdev;
61 struct nes_adapter *nesadapter = nesdev->nesadapter;
62 struct nes_cqp_request *cqp_request;
63 struct nes_mr *nesmr;
64 struct ib_mw *ibmw;
65 struct nes_hw_cqp_wqe *cqp_wqe;
66 int ret;
67 u32 stag;
68 u32 stag_index = 0;
69 u32 next_stag_index = 0;
70 u32 driver_key = 0;
71 u8 stag_key = 0;
72
73 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
74 stag_key = (u8)next_stag_index;
75
76 driver_key = 0;
77
78 next_stag_index >>= 8;
79 next_stag_index %= nesadapter->max_mr;
80
81 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
82 nesadapter->max_mr, &stag_index, &next_stag_index);
83 if (ret) {
84 return ERR_PTR(ret);
85 }
86
87 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
88 if (!nesmr) {
89 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
90 return ERR_PTR(-ENOMEM);
91 }
92
93 stag = stag_index << 8;
94 stag |= driver_key;
95 stag += (u32)stag_key;
96
97 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
98 stag, stag_index);
99
100 /* Register the region with the adapter */
101 cqp_request = nes_get_cqp_request(nesdev);
102 if (cqp_request == NULL) {
103 kfree(nesmr);
104 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
105 return ERR_PTR(-ENOMEM);
106 }
107
108 cqp_request->waiting = 1;
109 cqp_wqe = &cqp_request->cqp_wqe;
110
111 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
112 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
113 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
114 NES_CQP_STAG_REM_ACC_EN);
115
116 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
117 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
118 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
119
120 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700121 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800122
123 /* Wait for CQP */
124 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
125 NES_EVENT_TIMEOUT);
126 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
127 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
128 stag, ret, cqp_request->major_code, cqp_request->minor_code);
129 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -0700130 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131 kfree(nesmr);
132 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
133 if (!ret) {
134 return ERR_PTR(-ETIME);
135 } else {
136 return ERR_PTR(-ENOMEM);
137 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800138 }
Roland Dreier1ff66e82008-07-14 23:48:49 -0700139 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800140
141 nesmr->ibmw.rkey = stag;
142 nesmr->mode = IWNES_MEMREG_TYPE_MW;
143 ibmw = &nesmr->ibmw;
144 nesmr->pbl_4k = 0;
145 nesmr->pbls_used = 0;
146
147 return ibmw;
148}
149
150
151/**
152 * nes_dealloc_mw
153 */
154static int nes_dealloc_mw(struct ib_mw *ibmw)
155{
156 struct nes_mr *nesmr = to_nesmw(ibmw);
157 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
158 struct nes_device *nesdev = nesvnic->nesdev;
159 struct nes_adapter *nesadapter = nesdev->nesadapter;
160 struct nes_hw_cqp_wqe *cqp_wqe;
161 struct nes_cqp_request *cqp_request;
162 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800163 int ret;
164
165 /* Deallocate the window with the adapter */
166 cqp_request = nes_get_cqp_request(nesdev);
167 if (cqp_request == NULL) {
168 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
169 return -ENOMEM;
170 }
171 cqp_request->waiting = 1;
172 cqp_wqe = &cqp_request->cqp_wqe;
173 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
174 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
175 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
176
177 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700178 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800179
180 /* Wait for CQP */
181 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
182 ibmw->rkey);
183 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
184 NES_EVENT_TIMEOUT);
185 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
186 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
187 ret, cqp_request->major_code, cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -0700188 if (!ret)
189 err = -ETIME;
190 else if (cqp_request->major_code)
191 err = -EIO;
192
193 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800194
195 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
196 (ibmw->rkey & 0x0fffff00) >> 8);
197 kfree(nesmr);
198
199 return err;
200}
201
202
203/**
204 * nes_bind_mw
205 */
206static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
207 struct ib_mw_bind *ibmw_bind)
208{
209 u64 u64temp;
210 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
211 struct nes_device *nesdev = nesvnic->nesdev;
212 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
213 struct nes_qp *nesqp = to_nesqp(ibqp);
214 struct nes_hw_qp_wqe *wqe;
215 unsigned long flags = 0;
216 u32 head;
217 u32 wqe_misc = 0;
218 u32 qsize;
219
220 if (nesqp->ibqp_state > IB_QPS_RTS)
221 return -EINVAL;
222
Ilpo Järvinen5880ff12008-10-30 13:39:43 +0200223 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800224
225 head = nesqp->hwqp.sq_head;
226 qsize = nesqp->hwqp.sq_tail;
227
228 /* Check for SQ overflow */
229 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
Ilpo Järvinen5880ff12008-10-30 13:39:43 +0200230 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800231 return -EINVAL;
232 }
233
234 wqe = &nesqp->hwqp.sq_vbase[head];
235 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
236 nes_fill_init_qp_wqe(wqe, nesqp, head);
237 u64temp = ibmw_bind->wr_id;
238 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
239 wqe_misc = NES_IWARP_SQ_OP_BIND;
240
241 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
242
243 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
244 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
245
246 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) {
247 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
248 }
249 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) {
250 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
251 }
252
253 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
254 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey);
255 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
256 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
257 ibmw_bind->length);
258 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
259 u64temp = (u64)ibmw_bind->addr;
260 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
261
262 head++;
263 if (head >= qsize)
264 head = 0;
265
266 nesqp->hwqp.sq_head = head;
267 barrier();
268
269 nes_write32(nesdev->regs+NES_WQE_ALLOC,
270 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
271
Ilpo Järvinen5880ff12008-10-30 13:39:43 +0200272 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800273
274 return 0;
275}
276
277
Chien Tunge293a262009-12-09 15:21:54 -0800278/*
279 * nes_alloc_fast_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800280 */
Chien Tunge293a262009-12-09 15:21:54 -0800281static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
282 u32 stag, u32 page_count)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800283{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800284 struct nes_hw_cqp_wqe *cqp_wqe;
Chien Tunge293a262009-12-09 15:21:54 -0800285 struct nes_cqp_request *cqp_request;
286 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800287 int ret;
Chien Tunge293a262009-12-09 15:21:54 -0800288 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800289 u32 opcode = 0;
Chien Tunge293a262009-12-09 15:21:54 -0800290 u16 major_code;
291 u64 region_length = page_count * PAGE_SIZE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800292
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800293
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800294 cqp_request = nes_get_cqp_request(nesdev);
295 if (cqp_request == NULL) {
296 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
Chien Tunge293a262009-12-09 15:21:54 -0800297 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800298 }
Chien Tunge293a262009-12-09 15:21:54 -0800299 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
300 "region_length = %llu\n",
301 page_count, region_length);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800302 cqp_request->waiting = 1;
303 cqp_wqe = &cqp_request->cqp_wqe;
304
Chien Tunge293a262009-12-09 15:21:54 -0800305 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
306 if (nesadapter->free_4kpbl > 0) {
307 nesadapter->free_4kpbl--;
308 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
309 } else {
310 /* No 4kpbl's available: */
311 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
312 nes_debug(NES_DBG_MR, "Out of Pbls\n");
313 nes_free_cqp_request(nesdev, cqp_request);
314 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800315 }
316
Chien Tunge293a262009-12-09 15:21:54 -0800317 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
318 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
319 NES_CQP_STAG_REM_ACC_EN;
320 /*
321 * The current OFED API does not support the zero based TO option.
322 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
323 * the API does not support that ability to have the MR set for local
324 * access only when created and not allow the SQ op to override. Given
325 * this the remote enable must be set here.
326 */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800327
328 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
329 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
Chien Tunge293a262009-12-09 15:21:54 -0800330 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800331
Chien Tunge293a262009-12-09 15:21:54 -0800332 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
333 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
334 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
335 cpu_to_le32(nespd->pd_id & 0x00007fff);
336
337 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
338 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
339 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
340 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
341 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
342 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
343 barrier();
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800344
345 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700346 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800347
348 /* Wait for CQP */
Chien Tunge293a262009-12-09 15:21:54 -0800349 ret = wait_event_timeout(cqp_request->waitq,
350 (0 != cqp_request->request_done),
351 NES_EVENT_TIMEOUT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800352
Chien Tunge293a262009-12-09 15:21:54 -0800353 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
354 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
355 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
356 cqp_request->minor_code);
357 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -0700358 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800359
Chien Tunge293a262009-12-09 15:21:54 -0800360 if (!ret || major_code) {
Chien Tung35c6d692008-11-02 21:37:35 -0800361 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Chien Tunge293a262009-12-09 15:21:54 -0800362 nesadapter->free_4kpbl++;
Chien Tung35c6d692008-11-02 21:37:35 -0800363 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
364 }
365
Chien Tunge293a262009-12-09 15:21:54 -0800366 if (!ret)
367 return -ETIME;
368 else if (major_code)
369 return -EIO;
370 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800371}
372
Chien Tunge293a262009-12-09 15:21:54 -0800373/*
374 * nes_alloc_fast_reg_mr
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800375 */
Chien Tunge293a262009-12-09 15:21:54 -0800376struct ib_mr *nes_alloc_fast_reg_mr(struct ib_pd *ibpd, int max_page_list_len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800377{
Chien Tunge293a262009-12-09 15:21:54 -0800378 struct nes_pd *nespd = to_nespd(ibpd);
379 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800380 struct nes_device *nesdev = nesvnic->nesdev;
Chien Tung35c6d692008-11-02 21:37:35 -0800381 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800382
Chien Tunge293a262009-12-09 15:21:54 -0800383 u32 next_stag_index;
384 u8 stag_key = 0;
385 u32 driver_key = 0;
386 int err = 0;
387 u32 stag_index = 0;
388 struct nes_mr *nesmr;
389 u32 stag;
390 int ret;
391 struct ib_mr *ibmr;
392/*
393 * Note: Set to always use a fixed length single page entry PBL. This is to allow
394 * for the fast_reg_mr operation to always know the size of the PBL.
395 */
396 if (max_page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
397 return ERR_PTR(-E2BIG);
398
399 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
400 stag_key = (u8)next_stag_index;
401 next_stag_index >>= 8;
402 next_stag_index %= nesadapter->max_mr;
403
404 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
405 nesadapter->max_mr, &stag_index,
406 &next_stag_index);
407 if (err)
408 return ERR_PTR(err);
409
410 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
411 if (!nesmr) {
412 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
413 return ERR_PTR(-ENOMEM);
414 }
415
416 stag = stag_index << 8;
417 stag |= driver_key;
418 stag += (u32)stag_key;
419
420 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
421 stag, stag_index);
422
423 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_page_list_len);
424
425 if (ret == 0) {
426 nesmr->ibmr.rkey = stag;
427 nesmr->ibmr.lkey = stag;
428 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
429 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800430 } else {
Chien Tunge293a262009-12-09 15:21:54 -0800431 kfree(nesmr);
432 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
433 ibmr = ERR_PTR(-ENOMEM);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800434 }
Chien Tunge293a262009-12-09 15:21:54 -0800435 return ibmr;
436}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800437
Chien Tunge293a262009-12-09 15:21:54 -0800438/*
439 * nes_alloc_fast_reg_page_list
440 */
441static struct ib_fast_reg_page_list *nes_alloc_fast_reg_page_list(
442 struct ib_device *ibdev,
443 int page_list_len)
444{
445 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
446 struct nes_device *nesdev = nesvnic->nesdev;
447 struct ib_fast_reg_page_list *pifrpl;
448 struct nes_ib_fast_reg_page_list *pnesfrpl;
Don Wooddae5d132009-03-06 15:12:09 -0800449
Chien Tunge293a262009-12-09 15:21:54 -0800450 if (page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
451 return ERR_PTR(-E2BIG);
452 /*
453 * Allocate the ib_fast_reg_page_list structure, the
454 * nes_fast_bpl structure, and the PLB table.
455 */
456 pnesfrpl = kmalloc(sizeof(struct nes_ib_fast_reg_page_list) +
457 page_list_len * sizeof(u64), GFP_KERNEL);
458
459 if (!pnesfrpl)
460 return ERR_PTR(-ENOMEM);
461
462 pifrpl = &pnesfrpl->ibfrpl;
463 pifrpl->page_list = &pnesfrpl->pbl;
464 pifrpl->max_page_list_len = page_list_len;
465 /*
466 * Allocate the WQE PBL
467 */
468 pnesfrpl->nes_wqe_pbl.kva = pci_alloc_consistent(nesdev->pcidev,
469 page_list_len * sizeof(u64),
470 &pnesfrpl->nes_wqe_pbl.paddr);
471
472 if (!pnesfrpl->nes_wqe_pbl.kva) {
473 kfree(pnesfrpl);
474 return ERR_PTR(-ENOMEM);
Chien Tung35c6d692008-11-02 21:37:35 -0800475 }
Chien Tunge293a262009-12-09 15:21:54 -0800476 nes_debug(NES_DBG_MR, "nes_alloc_fast_reg_pbl: nes_frpl = %p, "
477 "ibfrpl = %p, ibfrpl.page_list = %p, pbl.kva = %p, "
478 "pbl.paddr= %p\n", pnesfrpl, &pnesfrpl->ibfrpl,
479 pnesfrpl->ibfrpl.page_list, pnesfrpl->nes_wqe_pbl.kva,
480 (void *)pnesfrpl->nes_wqe_pbl.paddr);
Chien Tung35c6d692008-11-02 21:37:35 -0800481
Chien Tunge293a262009-12-09 15:21:54 -0800482 return pifrpl;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800483}
484
Chien Tunge293a262009-12-09 15:21:54 -0800485/*
486 * nes_free_fast_reg_page_list
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487 */
Chien Tunge293a262009-12-09 15:21:54 -0800488static void nes_free_fast_reg_page_list(struct ib_fast_reg_page_list *pifrpl)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489{
Chien Tunge293a262009-12-09 15:21:54 -0800490 struct nes_vnic *nesvnic = to_nesvnic(pifrpl->device);
491 struct nes_device *nesdev = nesvnic->nesdev;
492 struct nes_ib_fast_reg_page_list *pnesfrpl;
493
494 pnesfrpl = container_of(pifrpl, struct nes_ib_fast_reg_page_list, ibfrpl);
495 /*
496 * Free the WQE PBL.
497 */
498 pci_free_consistent(nesdev->pcidev,
499 pifrpl->max_page_list_len * sizeof(u64),
500 pnesfrpl->nes_wqe_pbl.kva,
501 pnesfrpl->nes_wqe_pbl.paddr);
502 /*
503 * Free the PBL structure
504 */
505 kfree(pnesfrpl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506}
507
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800508/**
509 * nes_query_device
510 */
511static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
512{
513 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
514 struct nes_device *nesdev = nesvnic->nesdev;
515 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
516
517 memset(props, 0, sizeof(*props));
518 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
519
520 props->fw_ver = nesdev->nesadapter->fw_ver;
521 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
522 props->vendor_id = nesdev->nesadapter->vendor_id;
523 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
524 props->hw_ver = nesdev->nesadapter->hw_rev;
525 props->max_mr_size = 0x80000000;
526 props->max_qp = nesibdev->max_qp;
527 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
528 props->max_sge = nesdev->nesadapter->max_sge;
529 props->max_cq = nesibdev->max_cq;
Chien Tung5924aea2009-12-09 15:21:56 -0800530 props->max_cqe = nesdev->nesadapter->max_cqe;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800531 props->max_mr = nesibdev->max_mr;
532 props->max_mw = nesibdev->max_mr;
533 props->max_pd = nesibdev->max_pd;
534 props->max_sge_rd = 1;
535 switch (nesdev->nesadapter->max_irrq_wr) {
536 case 0:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800537 props->max_qp_rd_atom = 2;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800538 break;
539 case 1:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800540 props->max_qp_rd_atom = 8;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800541 break;
542 case 2:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800543 props->max_qp_rd_atom = 32;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800544 break;
545 case 3:
Faisal Latif8ac7f6e2009-12-09 15:53:46 -0800546 props->max_qp_rd_atom = 64;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800547 break;
548 default:
549 props->max_qp_rd_atom = 0;
550 }
Faisal Latif66388d62009-06-22 22:52:30 -0700551 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800552 props->atomic_cap = IB_ATOMIC_NONE;
553 props->max_map_per_fmr = 1;
554
555 return 0;
556}
557
558
559/**
560 * nes_query_port
561 */
562static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
563{
Chien Tungcd1d3f72009-09-05 20:36:39 -0700564 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
565 struct net_device *netdev = nesvnic->netdev;
566
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800567 memset(props, 0, sizeof(*props));
568
Chien Tungcd1d3f72009-09-05 20:36:39 -0700569 props->max_mtu = IB_MTU_4096;
570
571 if (netdev->mtu >= 4096)
572 props->active_mtu = IB_MTU_4096;
573 else if (netdev->mtu >= 2048)
574 props->active_mtu = IB_MTU_2048;
575 else if (netdev->mtu >= 1024)
576 props->active_mtu = IB_MTU_1024;
577 else if (netdev->mtu >= 512)
578 props->active_mtu = IB_MTU_512;
579 else
580 props->active_mtu = IB_MTU_256;
581
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800582 props->lid = 1;
583 props->lmc = 0;
584 props->sm_lid = 0;
585 props->sm_sl = 0;
Chien Tungcd1d3f72009-09-05 20:36:39 -0700586 if (nesvnic->linkup)
587 props->state = IB_PORT_ACTIVE;
588 else
589 props->state = IB_PORT_DOWN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800590 props->phys_state = 0;
591 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
592 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
593 props->gid_tbl_len = 1;
594 props->pkey_tbl_len = 1;
595 props->qkey_viol_cntr = 0;
596 props->active_width = IB_WIDTH_4X;
597 props->active_speed = 1;
598 props->max_msg_sz = 0x80000000;
599
600 return 0;
601}
602
603
604/**
605 * nes_modify_port
606 */
607static int nes_modify_port(struct ib_device *ibdev, u8 port,
608 int port_modify_mask, struct ib_port_modify *props)
609{
610 return 0;
611}
612
613
614/**
615 * nes_query_pkey
616 */
617static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
618{
619 *pkey = 0;
620 return 0;
621}
622
623
624/**
625 * nes_query_gid
626 */
627static int nes_query_gid(struct ib_device *ibdev, u8 port,
628 int index, union ib_gid *gid)
629{
630 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
631
632 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
633 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
634
635 return 0;
636}
637
638
639/**
640 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
641 * of all objects associated with a particular user-mode client.
642 */
643static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
644 struct ib_udata *udata)
645{
646 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
647 struct nes_device *nesdev = nesvnic->nesdev;
648 struct nes_adapter *nesadapter = nesdev->nesadapter;
649 struct nes_alloc_ucontext_req req;
650 struct nes_alloc_ucontext_resp uresp;
651 struct nes_ucontext *nes_ucontext;
652 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
653
654
655 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
656 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
657 return ERR_PTR(-EINVAL);
658 }
659
660 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
661 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
662 req.userspace_ver, NES_ABI_USERSPACE_VER);
663 return ERR_PTR(-EINVAL);
664 }
665
666
667 memset(&uresp, 0, sizeof uresp);
668
669 uresp.max_qps = nesibdev->max_qp;
670 uresp.max_pds = nesibdev->max_pd;
671 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
672 uresp.virtwq = nesadapter->virtwq;
673 uresp.kernel_ver = NES_ABI_KERNEL_VER;
674
675 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
676 if (!nes_ucontext)
677 return ERR_PTR(-ENOMEM);
678
679 nes_ucontext->nesdev = nesdev;
680 nes_ucontext->mmap_wq_offset = uresp.max_pds;
681 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
682 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
683 PAGE_SIZE;
684
685
686 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
687 kfree(nes_ucontext);
688 return ERR_PTR(-EFAULT);
689 }
690
691 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
692 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
693 atomic_set(&nes_ucontext->usecnt, 1);
694 return &nes_ucontext->ibucontext;
695}
696
697
698/**
699 * nes_dealloc_ucontext
700 */
701static int nes_dealloc_ucontext(struct ib_ucontext *context)
702{
703 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
704 /* struct nes_device *nesdev = nesvnic->nesdev; */
705 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
706
707 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
708 return 0;
709 kfree(nes_ucontext);
710 return 0;
711}
712
713
714/**
715 * nes_mmap
716 */
717static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
718{
719 unsigned long index;
720 struct nes_vnic *nesvnic = to_nesvnic(context->device);
721 struct nes_device *nesdev = nesvnic->nesdev;
722 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
723 struct nes_ucontext *nes_ucontext;
724 struct nes_qp *nesqp;
725
726 nes_ucontext = to_nesucontext(context);
727
728
729 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
730 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
731 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
732 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
733 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
734 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
735 return -EFAULT;
736 }
737 nesqp = nes_ucontext->mmap_nesqp[index];
738 if (nesqp == NULL) {
739 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
740 return -EFAULT;
741 }
742 if (remap_pfn_range(vma, vma->vm_start,
743 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
744 vma->vm_end - vma->vm_start,
745 vma->vm_page_prot)) {
746 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
747 return -EAGAIN;
748 }
749 vma->vm_private_data = nesqp;
750 return 0;
751 } else {
752 index = vma->vm_pgoff;
753 if (!test_bit(index, nes_ucontext->allocated_doorbells))
754 return -EFAULT;
755
756 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
757 if (io_remap_pfn_range(vma, vma->vm_start,
758 (nesdev->doorbell_start +
759 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
760 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
761 return -EAGAIN;
762 vma->vm_private_data = nes_ucontext;
763 return 0;
764 }
765
766 return -ENOSYS;
767}
768
769
770/**
771 * nes_alloc_pd
772 */
773static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
774 struct ib_ucontext *context, struct ib_udata *udata)
775{
776 struct nes_pd *nespd;
777 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
778 struct nes_device *nesdev = nesvnic->nesdev;
779 struct nes_adapter *nesadapter = nesdev->nesadapter;
780 struct nes_ucontext *nesucontext;
781 struct nes_alloc_pd_resp uresp;
782 u32 pd_num = 0;
783 int err;
784
785 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
786 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
787 atomic_read(&nesvnic->netdev->refcnt));
788
789 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
790 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
791 if (err) {
792 return ERR_PTR(err);
793 }
794
795 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
796 if (!nespd) {
797 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
798 return ERR_PTR(-ENOMEM);
799 }
800
801 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
802 nespd, nesvnic->nesibdev->ibdev.name);
803
804 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
805
806 if (context) {
807 nesucontext = to_nesucontext(context);
808 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
809 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
810 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
811 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600812 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800813 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
814 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
815 kfree(nespd);
816 return ERR_PTR(-ENOMEM);
817 }
818
819 uresp.pd_id = nespd->pd_id;
820 uresp.mmap_db_index = nespd->mmap_db_index;
821 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
822 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
823 kfree(nespd);
824 return ERR_PTR(-EFAULT);
825 }
826
827 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
828 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
829 nesucontext->first_free_db = nespd->mmap_db_index + 1;
830 }
831
832 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
833 return &nespd->ibpd;
834}
835
836
837/**
838 * nes_dealloc_pd
839 */
840static int nes_dealloc_pd(struct ib_pd *ibpd)
841{
842 struct nes_ucontext *nesucontext;
843 struct nes_pd *nespd = to_nespd(ibpd);
844 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
845 struct nes_device *nesdev = nesvnic->nesdev;
846 struct nes_adapter *nesadapter = nesdev->nesadapter;
847
848 if ((ibpd->uobject) && (ibpd->uobject->context)) {
849 nesucontext = to_nesucontext(ibpd->uobject->context);
850 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
851 nespd->mmap_db_index);
852 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
853 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
854 if (nesucontext->first_free_db > nespd->mmap_db_index) {
855 nesucontext->first_free_db = nespd->mmap_db_index;
856 }
857 }
858
859 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
860 nespd->pd_id, nespd);
861 nes_free_resource(nesadapter, nesadapter->allocated_pds,
862 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
863 kfree(nespd);
864
865 return 0;
866}
867
868
869/**
870 * nes_create_ah
871 */
872static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
873{
874 return ERR_PTR(-ENOSYS);
875}
876
877
878/**
879 * nes_destroy_ah
880 */
881static int nes_destroy_ah(struct ib_ah *ah)
882{
883 return -ENOSYS;
884}
885
886
887/**
888 * nes_get_encoded_size
889 */
890static inline u8 nes_get_encoded_size(int *size)
891{
892 u8 encoded_size = 0;
893 if (*size <= 32) {
894 *size = 32;
895 encoded_size = 1;
896 } else if (*size <= 128) {
897 *size = 128;
898 encoded_size = 2;
899 } else if (*size <= 512) {
900 *size = 512;
901 encoded_size = 3;
902 }
903 return (encoded_size);
904}
905
906
907
908/**
909 * nes_setup_virt_qp
910 */
911static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
912 struct nes_vnic *nesvnic, int sq_size, int rq_size)
913{
914 unsigned long flags;
915 void *mem;
916 __le64 *pbl = NULL;
917 __le64 *tpbl;
918 __le64 *pblbuffer;
919 struct nes_device *nesdev = nesvnic->nesdev;
920 struct nes_adapter *nesadapter = nesdev->nesadapter;
921 u32 pbl_entries;
922 u8 rq_pbl_entries;
923 u8 sq_pbl_entries;
924
925 pbl_entries = nespbl->pbl_size >> 3;
Roland Dreier71e09572008-04-16 21:01:09 -0700926 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 -0800927 nespbl->pbl_size, pbl_entries,
928 (void *)nespbl->pbl_vbase,
Roland Dreier71e09572008-04-16 21:01:09 -0700929 (unsigned long) nespbl->pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800930 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
931 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
932 /* the first pbl to be fro the rq_vbase... */
933 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
934 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
935 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
936 if (!nespbl->page) {
937 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
938 kfree(nespbl);
939 return -ENOMEM;
940 }
941
942 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
943 nesqp->page = nespbl->page;
944 if (!nesqp->hwqp.sq_vbase) {
945 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
946 kfree(nespbl);
947 return -ENOMEM;
948 }
949
950 /* Now to get to sq.. we need to calculate how many */
951 /* PBL entries were used by the rq.. */
952 pbl += sq_pbl_entries;
953 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
954 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
955 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
956
Roland Dreier71e09572008-04-16 21:01:09 -0700957 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
958 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
959 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800960 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
961 if (!nesadapter->free_256pbl) {
962 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
963 nespbl->pbl_pbase);
964 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
965 kunmap(nesqp->page);
966 kfree(nespbl);
967 return -ENOMEM;
968 }
969 nesadapter->free_256pbl--;
970 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
971
972 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
973 pblbuffer = nesqp->pbl_vbase;
974 if (!nesqp->pbl_vbase) {
975 /* memory allocated during nes_reg_user_mr() */
976 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
977 nespbl->pbl_pbase);
978 kfree(nespbl);
979 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
980 nesadapter->free_256pbl++;
981 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
982 kunmap(nesqp->page);
983 return -ENOMEM;
984 }
985 memset(nesqp->pbl_vbase, 0, 256);
986 /* fill in the page address in the pbl buffer.. */
987 tpbl = pblbuffer + 16;
988 pbl = (__le64 *)nespbl->pbl_vbase;
989 while (sq_pbl_entries--)
990 *tpbl++ = *pbl++;
991 tpbl = pblbuffer;
992 while (rq_pbl_entries--)
993 *tpbl++ = *pbl++;
994
995 /* done with memory allocated during nes_reg_user_mr() */
996 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
997 nespbl->pbl_pbase);
998 kfree(nespbl);
999
1000 nesqp->qp_mem_size =
1001 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1002 /* Round up to a multiple of a page */
1003 nesqp->qp_mem_size += PAGE_SIZE - 1;
1004 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1005
1006 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1007 &nesqp->hwqp.q2_pbase);
1008
1009 if (!mem) {
1010 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1011 nesqp->pbl_vbase = NULL;
1012 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1013 nesadapter->free_256pbl++;
1014 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1015 kunmap(nesqp->page);
1016 return -ENOMEM;
1017 }
1018 nesqp->hwqp.q2_vbase = mem;
1019 mem += 256;
1020 memset(nesqp->hwqp.q2_vbase, 0, 256);
1021 nesqp->nesqp_context = mem;
1022 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1023 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1024
1025 return 0;
1026}
1027
1028
1029/**
1030 * nes_setup_mmap_qp
1031 */
1032static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1033 int sq_size, int rq_size)
1034{
1035 void *mem;
1036 struct nes_device *nesdev = nesvnic->nesdev;
1037
1038 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1039 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1040 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1041 256; /* this is Q2 */
1042 /* Round up to a multiple of a page */
1043 nesqp->qp_mem_size += PAGE_SIZE - 1;
1044 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1045
1046 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1047 &nesqp->hwqp.sq_pbase);
1048 if (!mem)
1049 return -ENOMEM;
1050 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1051 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1052 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1053
1054 memset(mem, 0, nesqp->qp_mem_size);
1055
1056 nesqp->hwqp.sq_vbase = mem;
1057 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1058
1059 nesqp->hwqp.rq_vbase = mem;
1060 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1061 sizeof(struct nes_hw_qp_wqe) * sq_size;
1062 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1063
1064 nesqp->hwqp.q2_vbase = mem;
1065 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1066 sizeof(struct nes_hw_qp_wqe) * rq_size;
1067 mem += 256;
1068 memset(nesqp->hwqp.q2_vbase, 0, 256);
1069
1070 nesqp->nesqp_context = mem;
1071 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1072 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1073 return 0;
1074}
1075
1076
1077/**
1078 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1079 */
1080static inline void nes_free_qp_mem(struct nes_device *nesdev,
1081 struct nes_qp *nesqp, int virt_wqs)
1082{
1083 unsigned long flags;
1084 struct nes_adapter *nesadapter = nesdev->nesadapter;
1085 if (!virt_wqs) {
1086 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1087 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1088 }else {
1089 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1090 nesadapter->free_256pbl++;
1091 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1092 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1093 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1094 nesqp->pbl_vbase = NULL;
1095 kunmap(nesqp->page);
1096 }
1097}
1098
1099
1100/**
1101 * nes_create_qp
1102 */
1103static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1104 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1105{
1106 u64 u64temp= 0;
1107 u64 u64nesqp = 0;
1108 struct nes_pd *nespd = to_nespd(ibpd);
1109 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1110 struct nes_device *nesdev = nesvnic->nesdev;
1111 struct nes_adapter *nesadapter = nesdev->nesadapter;
1112 struct nes_qp *nesqp;
1113 struct nes_cq *nescq;
1114 struct nes_ucontext *nes_ucontext;
1115 struct nes_hw_cqp_wqe *cqp_wqe;
1116 struct nes_cqp_request *cqp_request;
1117 struct nes_create_qp_req req;
1118 struct nes_create_qp_resp uresp;
1119 struct nes_pbl *nespbl = NULL;
1120 u32 qp_num = 0;
1121 u32 opcode = 0;
1122 /* u32 counter = 0; */
1123 void *mem;
1124 unsigned long flags;
1125 int ret;
1126 int err;
1127 int virt_wqs = 0;
1128 int sq_size;
1129 int rq_size;
1130 u8 sq_encoded_size;
1131 u8 rq_encoded_size;
1132 /* int counter; */
1133
Eli Cohenb846f252008-04-16 21:09:27 -07001134 if (init_attr->create_flags)
1135 return ERR_PTR(-EINVAL);
1136
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001137 atomic_inc(&qps_created);
1138 switch (init_attr->qp_type) {
1139 case IB_QPT_RC:
1140 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1141 init_attr->cap.max_inline_data = 0;
1142 } else {
1143 init_attr->cap.max_inline_data = 64;
1144 }
1145 sq_size = init_attr->cap.max_send_wr;
1146 rq_size = init_attr->cap.max_recv_wr;
1147
Glenn Streiff7495ab62008-04-29 13:46:54 -07001148 /* check if the encoded sizes are OK or not... */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001149 sq_encoded_size = nes_get_encoded_size(&sq_size);
1150 rq_encoded_size = nes_get_encoded_size(&rq_size);
1151
1152 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1153 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1154 rq_size, sq_size);
1155 return ERR_PTR(-EINVAL);
1156 }
1157
1158 init_attr->cap.max_send_wr = sq_size -2;
1159 init_attr->cap.max_recv_wr = rq_size -1;
1160 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1161
1162 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1163 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1164 if (ret) {
1165 return ERR_PTR(ret);
1166 }
1167
1168 /* Need 512 (actually now 1024) byte alignment on this structure */
1169 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1170 if (!mem) {
1171 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1172 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1173 return ERR_PTR(-ENOMEM);
1174 }
1175 u64nesqp = (unsigned long)mem;
1176 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1177 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1178 u64nesqp &= ~u64temp;
1179 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1180 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1181 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1182 nesqp->allocated_buffer = mem;
1183
1184 if (udata) {
1185 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1186 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1187 kfree(nesqp->allocated_buffer);
1188 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1189 return NULL;
1190 }
1191 if (req.user_wqe_buffers) {
1192 virt_wqs = 1;
1193 }
1194 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1195 nesqp->user_mode = 1;
1196 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1197 if (virt_wqs) {
1198 err = 1;
1199 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1200 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1201 list_del(&nespbl->list);
1202 err = 0;
1203 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1204 nespbl, nespbl->user_base);
1205 break;
1206 }
1207 }
1208 if (err) {
1209 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1210 (long long unsigned int)req.user_wqe_buffers);
1211 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1212 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001213 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001214 }
1215 }
1216
1217 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1218 nesqp->mmap_sq_db_index =
1219 find_next_zero_bit(nes_ucontext->allocated_wqs,
1220 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1221 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1222 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001223 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001224 nes_debug(NES_DBG_QP,
1225 "db index > max user regions, failing create QP\n");
1226 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1227 if (virt_wqs) {
1228 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1229 nespbl->pbl_pbase);
1230 kfree(nespbl);
1231 }
1232 kfree(nesqp->allocated_buffer);
1233 return ERR_PTR(-ENOMEM);
1234 }
1235 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1236 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1237 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1238 } else {
1239 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1240 kfree(nesqp->allocated_buffer);
1241 return ERR_PTR(-EFAULT);
1242 }
1243 }
1244 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1245 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1246 if (err) {
1247 nes_debug(NES_DBG_QP,
1248 "error geting qp mem code = %d\n", err);
1249 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1250 kfree(nesqp->allocated_buffer);
1251 return ERR_PTR(-ENOMEM);
1252 }
1253
1254 nesqp->hwqp.sq_size = sq_size;
1255 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1256 nesqp->hwqp.sq_head = 1;
1257 nesqp->hwqp.rq_size = rq_size;
1258 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1259 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1260 (void *)nesqp->nesqp_context_pbase);
1261 */
1262 nesqp->hwqp.qp_id = qp_num;
1263 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1264 nesqp->nespd = nespd;
1265
1266 nescq = to_nescq(init_attr->send_cq);
1267 nesqp->nesscq = nescq;
1268 nescq = to_nescq(init_attr->recv_cq);
1269 nesqp->nesrcq = nescq;
1270
1271 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1272 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1273 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1274 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1275 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1276 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001277 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001278 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1279 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001280 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001281 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1282 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1283 u64temp = (u64)nesqp->hwqp.sq_pbase;
1284 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1285 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1286
1287
1288 if (!virt_wqs) {
1289 u64temp = (u64)nesqp->hwqp.sq_pbase;
1290 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1291 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1292 u64temp = (u64)nesqp->hwqp.rq_pbase;
1293 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1294 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1295 } else {
1296 u64temp = (u64)nesqp->pbl_pbase;
1297 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1298 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1299 }
1300
1301 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1302 nesvnic->next_qp_nic_index,
1303 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1304 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1305 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1306 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1307 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1308 nesvnic->next_qp_nic_index++;
1309 if ((nesvnic->next_qp_nic_index > 3) ||
1310 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1311 nesvnic->next_qp_nic_index = 0;
1312 }
1313 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1314
1315 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1316 u64temp = (u64)nesqp->hwqp.q2_pbase;
1317 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1318 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1319 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1320 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1321 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1322 ((((u32)nesadapter->max_irrq_wr) <<
1323 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1324 if (disable_mpa_crc) {
1325 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1326 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1327 }
1328
1329
1330 /* Create the QP */
1331 cqp_request = nes_get_cqp_request(nesdev);
1332 if (cqp_request == NULL) {
1333 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1334 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1335 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1336 kfree(nesqp->allocated_buffer);
1337 return ERR_PTR(-ENOMEM);
1338 }
1339 cqp_request->waiting = 1;
1340 cqp_wqe = &cqp_request->cqp_wqe;
1341
1342 if (!virt_wqs) {
1343 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1344 NES_CQP_QP_IWARP_STATE_IDLE;
1345 } else {
1346 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1347 NES_CQP_QP_IWARP_STATE_IDLE;
1348 }
1349 opcode |= NES_CQP_QP_CQS_VALID;
1350 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1351 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1352 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1353
1354 u64temp = (u64)nesqp->nesqp_context_pbase;
1355 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1356
1357 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001358 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001359
1360 /* Wait for CQP */
1361 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1362 nesqp->hwqp.qp_id);
1363 ret = wait_event_timeout(cqp_request->waitq,
1364 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1365 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1366 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1367 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1368 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1369 cqp_request->major_code, cqp_request->minor_code);
1370 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001371 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001372 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1373 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1374 kfree(nesqp->allocated_buffer);
1375 if (!ret) {
1376 return ERR_PTR(-ETIME);
1377 } else {
1378 return ERR_PTR(-EIO);
1379 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001380 }
1381
Roland Dreier1ff66e82008-07-14 23:48:49 -07001382 nes_put_cqp_request(nesdev, cqp_request);
1383
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001384 if (ibpd->uobject) {
1385 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1386 uresp.actual_sq_size = sq_size;
1387 uresp.actual_rq_size = rq_size;
1388 uresp.qp_id = nesqp->hwqp.qp_id;
1389 uresp.nes_drv_opt = nes_drv_opt;
1390 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1391 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1392 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1393 kfree(nesqp->allocated_buffer);
1394 return ERR_PTR(-EFAULT);
1395 }
1396 }
1397
1398 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1399 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1400 spin_lock_init(&nesqp->lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001401 nes_add_ref(&nesqp->ibqp);
1402 break;
1403 default:
1404 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1405 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001406 }
1407
Chien Tungd14152d2009-12-09 15:21:56 -08001408 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1409
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001410 /* update the QP table */
1411 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1412 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1413 atomic_read(&nesvnic->netdev->refcnt));
1414
1415 return &nesqp->ibqp;
1416}
1417
1418
1419/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001420 * nes_clean_cq
1421 */
1422static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1423{
1424 u32 cq_head;
1425 u32 lo;
1426 u32 hi;
1427 u64 u64temp;
1428 unsigned long flags = 0;
1429
1430 spin_lock_irqsave(&nescq->lock, flags);
1431
1432 cq_head = nescq->hw_cq.cq_head;
1433 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1434 rmb();
1435 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1436 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1437 u64temp = (((u64)hi) << 32) | ((u64)lo);
1438 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1439 if (u64temp == (u64)(unsigned long)nesqp) {
1440 /* Zero the context value so cqe will be ignored */
1441 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1442 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1443 }
1444
1445 if (++cq_head >= nescq->hw_cq.cq_size)
1446 cq_head = 0;
1447 }
1448
1449 spin_unlock_irqrestore(&nescq->lock, flags);
1450}
1451
1452
1453/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001454 * nes_destroy_qp
1455 */
1456static int nes_destroy_qp(struct ib_qp *ibqp)
1457{
1458 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001459 struct nes_ucontext *nes_ucontext;
1460 struct ib_qp_attr attr;
1461 struct iw_cm_id *cm_id;
1462 struct iw_cm_event cm_event;
1463 int ret;
1464
1465 atomic_inc(&sw_qps_destroyed);
1466 nesqp->destroyed = 1;
1467
1468 /* Blow away the connection if it exists. */
1469 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1470 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1471 attr.qp_state = IB_QPS_ERR;
1472 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1473 }
1474
1475 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1476 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1477 cm_id = nesqp->cm_id;
1478 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1479 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1480 cm_event.local_addr = cm_id->local_addr;
1481 cm_event.remote_addr = cm_id->remote_addr;
1482 cm_event.private_data = NULL;
1483 cm_event.private_data_len = 0;
1484
1485 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1486 "QP%u. cm_id = %p, refcount = %u. \n",
1487 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1488
1489 cm_id->rem_ref(cm_id);
1490 ret = cm_id->event_handler(cm_id, &cm_event);
1491 if (ret)
1492 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1493 }
1494
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001495 if (nesqp->user_mode) {
1496 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1497 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1498 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1499 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1500 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1501 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1502 }
1503 }
1504 if (nesqp->pbl_pbase)
1505 kunmap(nesqp->page);
Don Wood5ee21fe2009-09-05 20:36:37 -07001506 } else {
1507 /* Clean any pending completions from the cq(s) */
1508 if (nesqp->nesscq)
1509 nes_clean_cq(nesqp, nesqp->nesscq);
1510
1511 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1512 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001513 }
1514
1515 nes_rem_ref(&nesqp->ibqp);
1516 return 0;
1517}
1518
1519
1520/**
1521 * nes_create_cq
1522 */
1523static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1524 int comp_vector,
1525 struct ib_ucontext *context, struct ib_udata *udata)
1526{
1527 u64 u64temp;
1528 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1529 struct nes_device *nesdev = nesvnic->nesdev;
1530 struct nes_adapter *nesadapter = nesdev->nesadapter;
1531 struct nes_cq *nescq;
1532 struct nes_ucontext *nes_ucontext = NULL;
1533 struct nes_cqp_request *cqp_request;
1534 void *mem = NULL;
1535 struct nes_hw_cqp_wqe *cqp_wqe;
1536 struct nes_pbl *nespbl = NULL;
1537 struct nes_create_cq_req req;
1538 struct nes_create_cq_resp resp;
1539 u32 cq_num = 0;
1540 u32 opcode = 0;
1541 u32 pbl_entries = 1;
1542 int err;
1543 unsigned long flags;
1544 int ret;
1545
Chien Tung5924aea2009-12-09 15:21:56 -08001546 if (entries > nesadapter->max_cqe)
1547 return ERR_PTR(-EINVAL);
1548
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001549 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1550 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1551 if (err) {
1552 return ERR_PTR(err);
1553 }
1554
1555 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1556 if (!nescq) {
1557 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1558 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1559 return ERR_PTR(-ENOMEM);
1560 }
1561
1562 nescq->hw_cq.cq_size = max(entries + 1, 5);
1563 nescq->hw_cq.cq_number = cq_num;
1564 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1565
1566
1567 if (context) {
1568 nes_ucontext = to_nesucontext(context);
1569 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1570 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1571 kfree(nescq);
1572 return ERR_PTR(-EFAULT);
1573 }
1574 nesvnic->mcrq_ucontext = nes_ucontext;
1575 nes_ucontext->mcrqf = req.mcrqf;
1576 if (nes_ucontext->mcrqf) {
1577 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001578 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001579 else if (nes_ucontext->mcrqf & 0x40000000)
1580 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1581 else
1582 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001583 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001584 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1585 }
1586 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1587 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001588 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001589 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1590 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1591 list_del(&nespbl->list);
1592 err = 0;
1593 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1594 nespbl);
1595 break;
1596 }
1597 }
1598 if (err) {
1599 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1600 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001601 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001602 }
1603
1604 pbl_entries = nespbl->pbl_size >> 3;
1605 nescq->cq_mem_size = 0;
1606 } else {
1607 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1608 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1609 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1610
1611 /* allocate the physical buffer space */
1612 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1613 &nescq->hw_cq.cq_pbase);
1614 if (!mem) {
1615 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1616 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1617 kfree(nescq);
1618 return ERR_PTR(-ENOMEM);
1619 }
1620
1621 memset(mem, 0, nescq->cq_mem_size);
1622 nescq->hw_cq.cq_vbase = mem;
1623 nescq->hw_cq.cq_head = 0;
1624 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1625 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1626 (u32)nescq->hw_cq.cq_pbase);
1627 }
1628
1629 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1630 spin_lock_init(&nescq->lock);
1631
1632 /* send CreateCQ request to CQP */
1633 cqp_request = nes_get_cqp_request(nesdev);
1634 if (cqp_request == NULL) {
1635 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1636 if (!context)
1637 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1638 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001639 else {
1640 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1641 nespbl->pbl_vbase, nespbl->pbl_pbase);
1642 kfree(nespbl);
1643 }
1644
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001645 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1646 kfree(nescq);
1647 return ERR_PTR(-ENOMEM);
1648 }
1649 cqp_request->waiting = 1;
1650 cqp_wqe = &cqp_request->cqp_wqe;
1651
1652 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1653 NES_CQP_CQ_CHK_OVERFLOW |
1654 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1655
1656 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1657
1658 if (pbl_entries != 1) {
1659 if (pbl_entries > 32) {
1660 /* use 4k pbl */
1661 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1662 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001663 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1664 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001665 if (!context)
1666 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1667 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001668 else {
1669 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1670 nespbl->pbl_vbase, nespbl->pbl_pbase);
1671 kfree(nespbl);
1672 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001673 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1674 kfree(nescq);
1675 return ERR_PTR(-ENOMEM);
1676 } else {
1677 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1678 nescq->virtual_cq = 2;
1679 nesadapter->free_4kpbl--;
1680 }
1681 } else {
1682 /* use 256 byte pbl */
1683 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1684 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001685 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1686 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001687 if (!context)
1688 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1689 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001690 else {
1691 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1692 nespbl->pbl_vbase, nespbl->pbl_pbase);
1693 kfree(nespbl);
1694 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001695 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1696 kfree(nescq);
1697 return ERR_PTR(-ENOMEM);
1698 } else {
1699 opcode |= NES_CQP_CQ_VIRT;
1700 nescq->virtual_cq = 1;
1701 nesadapter->free_256pbl--;
1702 }
1703 }
1704 }
1705
1706 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1707
1708 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1709 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1710 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1711 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1712
1713 if (context) {
1714 if (pbl_entries != 1)
1715 u64temp = (u64)nespbl->pbl_pbase;
1716 else
1717 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1718 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1719 nes_ucontext->mmap_db_index[0]);
1720 } else {
1721 u64temp = (u64)nescq->hw_cq.cq_pbase;
1722 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1723 }
1724 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1725 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1726 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1727 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1728 cpu_to_le32((u32)(u64temp >> 1));
1729 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1730 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1731
1732 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001733 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001734
1735 /* Wait for CQP */
1736 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1737 nescq->hw_cq.cq_number);
1738 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1739 NES_EVENT_TIMEOUT * 2);
1740 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1741 nescq->hw_cq.cq_number, ret);
1742 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001743 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001744 if (!context)
1745 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1746 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001747 else {
1748 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1749 nespbl->pbl_vbase, nespbl->pbl_pbase);
1750 kfree(nespbl);
1751 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001752 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1753 kfree(nescq);
1754 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001755 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001756 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001757
1758 if (context) {
1759 /* free the nespbl */
1760 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1761 nespbl->pbl_pbase);
1762 kfree(nespbl);
1763 resp.cq_id = nescq->hw_cq.cq_number;
1764 resp.cq_size = nescq->hw_cq.cq_size;
1765 resp.mmap_db_index = 0;
1766 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1767 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1768 kfree(nescq);
1769 return ERR_PTR(-EFAULT);
1770 }
1771 }
1772
1773 return &nescq->ibcq;
1774}
1775
1776
1777/**
1778 * nes_destroy_cq
1779 */
1780static int nes_destroy_cq(struct ib_cq *ib_cq)
1781{
1782 struct nes_cq *nescq;
1783 struct nes_device *nesdev;
1784 struct nes_vnic *nesvnic;
1785 struct nes_adapter *nesadapter;
1786 struct nes_hw_cqp_wqe *cqp_wqe;
1787 struct nes_cqp_request *cqp_request;
1788 unsigned long flags;
1789 u32 opcode = 0;
1790 int ret;
1791
1792 if (ib_cq == NULL)
1793 return 0;
1794
1795 nescq = to_nescq(ib_cq);
1796 nesvnic = to_nesvnic(ib_cq->device);
1797 nesdev = nesvnic->nesdev;
1798 nesadapter = nesdev->nesadapter;
1799
1800 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1801
1802 /* Send DestroyCQ request to CQP */
1803 cqp_request = nes_get_cqp_request(nesdev);
1804 if (cqp_request == NULL) {
1805 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1806 return -ENOMEM;
1807 }
1808 cqp_request->waiting = 1;
1809 cqp_wqe = &cqp_request->cqp_wqe;
1810 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1811 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1812 if (nescq->virtual_cq == 1) {
1813 nesadapter->free_256pbl++;
1814 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1815 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001816 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001817 }
1818 } else if (nescq->virtual_cq == 2) {
1819 nesadapter->free_4kpbl++;
1820 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1821 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001822 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001823 }
1824 opcode |= NES_CQP_CQ_4KB_CHUNK;
1825 }
1826
1827 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1828
1829 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1830 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1831 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1832 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001833 if (!nescq->mcrqf)
1834 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1835
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001836 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001837 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001838
1839 /* Wait for CQP */
1840 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1841 nescq->hw_cq.cq_number);
1842 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1843 NES_EVENT_TIMEOUT);
1844 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1845 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1846 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1847 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001848 if (!ret) {
1849 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001850 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001851 ret = -ETIME;
1852 } else if (cqp_request->major_code) {
1853 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001854 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001855 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001856 } else {
1857 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001858 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001859 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001860
1861 if (nescq->cq_mem_size)
1862 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001863 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001864 kfree(nescq);
1865
1866 return ret;
1867}
1868
Don Wood0145f342009-03-06 15:15:00 -08001869/**
1870 * root_256
1871 */
1872static u32 root_256(struct nes_device *nesdev,
1873 struct nes_root_vpbl *root_vpbl,
1874 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001875 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001876{
1877 u64 leaf_pbl;
1878 int i, j, k;
1879
1880 if (pbl_count_4k == 1) {
1881 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1882 512, &new_root->pbl_pbase);
1883
1884 if (new_root->pbl_vbase == NULL)
1885 return 0;
1886
1887 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1888 for (i = 0; i < 16; i++) {
1889 new_root->pbl_vbase[i].pa_low =
1890 cpu_to_le32((u32)leaf_pbl);
1891 new_root->pbl_vbase[i].pa_high =
1892 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1893 leaf_pbl += 256;
1894 }
1895 } else {
1896 for (i = 3; i >= 0; i--) {
1897 j = i * 16;
1898 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1899 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1900 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1901 << 32);
1902 for (k = 1; k < 16; k++) {
1903 leaf_pbl += 256;
1904 root_vpbl->pbl_vbase[j + k].pa_low =
1905 cpu_to_le32((u32)leaf_pbl);
1906 root_vpbl->pbl_vbase[j + k].pa_high =
1907 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1908 }
1909 }
1910 }
1911
1912 return 1;
1913}
1914
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001915
1916/**
1917 * nes_reg_mr
1918 */
1919static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1920 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08001921 dma_addr_t single_buffer, u16 pbl_count_4k,
1922 u16 residual_page_count_4k, int acc, u64 *iova_start,
1923 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001924{
1925 struct nes_hw_cqp_wqe *cqp_wqe;
1926 struct nes_cqp_request *cqp_request;
1927 unsigned long flags;
1928 int ret;
1929 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08001930 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07001931 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08001932 u16 pbl_count = 0;
1933 u8 use_256_pbls = 0;
1934 u8 use_4k_pbls = 0;
1935 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1936 struct nes_root_vpbl new_root = {0, 0, 0};
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001937 u32 opcode = 0;
1938 u16 major_code;
1939
1940 /* Register the region with the adapter */
1941 cqp_request = nes_get_cqp_request(nesdev);
1942 if (cqp_request == NULL) {
1943 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1944 return -ENOMEM;
1945 }
1946 cqp_request->waiting = 1;
1947 cqp_wqe = &cqp_request->cqp_wqe;
1948
Don Wood0145f342009-03-06 15:15:00 -08001949 if (pbl_count_4k) {
1950 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1951
1952 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1953 pbl_count_256 = (pg_cnt + 31) / 32;
1954 if (pg_cnt <= 32) {
1955 if (pbl_count_256 <= nesadapter->free_256pbl)
1956 use_256_pbls = 1;
1957 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1958 use_4k_pbls = 1;
1959 } else if (pg_cnt <= 2048) {
1960 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1961 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1962 use_4k_pbls = 1;
1963 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1964 use_256_pbls = 1;
1965 use_two_level = 1;
1966 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1967 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001968 }
1969 } else {
Don Wood0145f342009-03-06 15:15:00 -08001970 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1971 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001972 }
Don Wood0145f342009-03-06 15:15:00 -08001973
1974 if (use_256_pbls) {
1975 pbl_count = pbl_count_256;
1976 nesadapter->free_256pbl -= pbl_count + use_two_level;
1977 } else if (use_4k_pbls) {
1978 pbl_count = pbl_count_4k;
1979 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1980 } else {
1981 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1982 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1983 nes_free_cqp_request(nesdev, cqp_request);
1984 return -ENOMEM;
1985 }
1986
1987 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001988 }
1989
Don Wood0145f342009-03-06 15:15:00 -08001990 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07001991 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08001992 if (new_root.pbl_pbase != 0)
1993 root_vpbl = &new_root;
1994 } else {
1995 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1996 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1997 use_256_pbls = 0;
1998
1999 if (pbl_count_4k == 1)
2000 use_two_level = 0;
2001 pbl_count = pbl_count_4k;
2002
2003 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2004 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2005 use_4k_pbls = 1;
2006 }
2007 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2008
2009 if (use_4k_pbls == 0)
2010 return -ENOMEM;
2011 }
2012 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002013
2014 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2015 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2016 if (acc & IB_ACCESS_LOCAL_WRITE)
2017 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2018 if (acc & IB_ACCESS_REMOTE_WRITE)
2019 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2020 if (acc & IB_ACCESS_REMOTE_READ)
2021 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2022 if (acc & IB_ACCESS_MW_BIND)
2023 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2024
2025 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2026 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2027 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2028 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2029
2030 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2031 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2032 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2033 cpu_to_le32(nespd->pd_id & 0x00007fff);
2034 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2035
2036 if (pbl_count == 0) {
2037 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2038 } else {
2039 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2040 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 -08002041 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 -08002042
Don Wood0145f342009-03-06 15:15:00 -08002043 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002044 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2045 }
2046 barrier();
2047
2048 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002049 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002050
2051 /* Wait for CQP */
2052 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2053 NES_EVENT_TIMEOUT);
2054 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2055 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2056 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2057 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002058 nes_put_cqp_request(nesdev, cqp_request);
2059
Don Wooddae5d132009-03-06 15:12:09 -08002060 if ((!ret || major_code) && pbl_count != 0) {
2061 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08002062 if (use_256_pbls)
2063 nesadapter->free_256pbl += pbl_count + use_two_level;
2064 else if (use_4k_pbls)
2065 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08002066 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2067 }
Don Wood0145f342009-03-06 15:15:00 -08002068 if (new_root.pbl_pbase)
2069 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2070 new_root.pbl_pbase);
2071
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002072 if (!ret)
2073 return -ETIME;
2074 else if (major_code)
2075 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002076
Don Wood0145f342009-03-06 15:15:00 -08002077 *actual_pbl_cnt = pbl_count + use_two_level;
2078 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002079 return 0;
2080}
2081
2082
2083/**
2084 * nes_reg_phys_mr
2085 */
2086static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2087 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2088 u64 * iova_start)
2089{
2090 u64 region_length;
2091 struct nes_pd *nespd = to_nespd(ib_pd);
2092 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2093 struct nes_device *nesdev = nesvnic->nesdev;
2094 struct nes_adapter *nesadapter = nesdev->nesadapter;
2095 struct nes_mr *nesmr;
2096 struct ib_mr *ibmr;
2097 struct nes_vpbl vpbl;
2098 struct nes_root_vpbl root_vpbl;
2099 u32 stag;
2100 u32 i;
Don Wood3f32eb12009-04-20 14:53:00 -07002101 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002102 u32 stag_index = 0;
2103 u32 next_stag_index = 0;
2104 u32 driver_key = 0;
2105 u32 root_pbl_index = 0;
2106 u32 cur_pbl_index = 0;
2107 int err = 0, pbl_depth = 0;
2108 int ret = 0;
2109 u16 pbl_count = 0;
2110 u8 single_page = 1;
2111 u8 stag_key = 0;
2112
2113 pbl_depth = 0;
2114 region_length = 0;
2115 vpbl.pbl_vbase = NULL;
2116 root_vpbl.pbl_vbase = NULL;
2117 root_vpbl.pbl_pbase = 0;
2118
2119 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2120 stag_key = (u8)next_stag_index;
2121
2122 driver_key = 0;
2123
2124 next_stag_index >>= 8;
2125 next_stag_index %= nesadapter->max_mr;
2126 if (num_phys_buf > (1024*512)) {
2127 return ERR_PTR(-E2BIG);
2128 }
2129
Don Wood3f32eb12009-04-20 14:53:00 -07002130 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2131 return ERR_PTR(-EINVAL);
2132
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002133 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2134 &stag_index, &next_stag_index);
2135 if (err) {
2136 return ERR_PTR(err);
2137 }
2138
2139 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2140 if (!nesmr) {
2141 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2142 return ERR_PTR(-ENOMEM);
2143 }
2144
2145 for (i = 0; i < num_phys_buf; i++) {
2146
2147 if ((i & 0x01FF) == 0) {
2148 if (root_pbl_index == 1) {
2149 /* Allocate the root PBL */
2150 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2151 &root_vpbl.pbl_pbase);
2152 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2153 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2154 if (!root_vpbl.pbl_vbase) {
2155 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2156 vpbl.pbl_pbase);
2157 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2158 kfree(nesmr);
2159 return ERR_PTR(-ENOMEM);
2160 }
2161 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2162 if (!root_vpbl.leaf_vpbl) {
2163 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2164 root_vpbl.pbl_pbase);
2165 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2166 vpbl.pbl_pbase);
2167 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2168 kfree(nesmr);
2169 return ERR_PTR(-ENOMEM);
2170 }
2171 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2172 root_vpbl.pbl_vbase[0].pa_high =
2173 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2174 root_vpbl.leaf_vpbl[0] = vpbl;
2175 }
2176 /* Allocate a 4K buffer for the PBL */
2177 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2178 &vpbl.pbl_pbase);
2179 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2180 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2181 if (!vpbl.pbl_vbase) {
2182 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2183 ibmr = ERR_PTR(-ENOMEM);
2184 kfree(nesmr);
2185 goto reg_phys_err;
2186 }
2187 /* Fill in the root table */
2188 if (1 <= root_pbl_index) {
2189 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2190 cpu_to_le32((u32)vpbl.pbl_pbase);
2191 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2192 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2193 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2194 }
2195 root_pbl_index++;
2196 cur_pbl_index = 0;
2197 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198
Don Wood3f32eb12009-04-20 14:53:00 -07002199 mask = !buffer_list[i].size;
2200 if (i != 0)
2201 mask |= buffer_list[i].addr;
2202 if (i != num_phys_buf - 1)
2203 mask |= buffer_list[i].addr + buffer_list[i].size;
2204
2205 if (mask & ~PAGE_MASK) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002206 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
Don Wood3f32eb12009-04-20 14:53:00 -07002207 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208 ibmr = ERR_PTR(-EINVAL);
2209 kfree(nesmr);
2210 goto reg_phys_err;
2211 }
2212
2213 region_length += buffer_list[i].size;
2214 if ((i != 0) && (single_page)) {
2215 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2216 single_page = 0;
2217 }
Don Wood3f32eb12009-04-20 14:53:00 -07002218 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002219 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2220 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2221 }
2222
2223 stag = stag_index << 8;
2224 stag |= driver_key;
2225 stag += (u32)stag_key;
2226
2227 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2228 " length = 0x%016lX, index = 0x%08X\n",
2229 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2230
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231 /* Make the leaf PBL the root if only one PBL */
2232 if (root_pbl_index == 1) {
2233 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2234 }
2235
2236 if (single_page) {
2237 pbl_count = 0;
2238 } else {
2239 pbl_count = root_pbl_index;
2240 }
2241 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08002242 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2243 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002244
2245 if (ret == 0) {
2246 nesmr->ibmr.rkey = stag;
2247 nesmr->ibmr.lkey = stag;
2248 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2249 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002250 } else {
2251 kfree(nesmr);
2252 ibmr = ERR_PTR(-ENOMEM);
2253 }
2254
2255 reg_phys_err:
2256 /* free the resources */
2257 if (root_pbl_index == 1) {
2258 /* single PBL case */
2259 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2260 } else {
2261 for (i=0; i<root_pbl_index; i++) {
2262 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2263 root_vpbl.leaf_vpbl[i].pbl_pbase);
2264 }
2265 kfree(root_vpbl.leaf_vpbl);
2266 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2267 root_vpbl.pbl_pbase);
2268 }
2269
2270 return ibmr;
2271}
2272
2273
2274/**
2275 * nes_get_dma_mr
2276 */
2277static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2278{
2279 struct ib_phys_buf bl;
2280 u64 kva = 0;
2281
2282 nes_debug(NES_DBG_MR, "\n");
2283
2284 bl.size = (u64)0xffffffffffULL;
2285 bl.addr = 0;
2286 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2287}
2288
2289
2290/**
2291 * nes_reg_user_mr
2292 */
2293static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2294 u64 virt, int acc, struct ib_udata *udata)
2295{
2296 u64 iova_start;
2297 __le64 *pbl;
2298 u64 region_length;
2299 dma_addr_t last_dma_addr = 0;
2300 dma_addr_t first_dma_addr = 0;
2301 struct nes_pd *nespd = to_nespd(pd);
2302 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2303 struct nes_device *nesdev = nesvnic->nesdev;
2304 struct nes_adapter *nesadapter = nesdev->nesadapter;
2305 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2306 struct ib_umem_chunk *chunk;
2307 struct nes_ucontext *nes_ucontext;
2308 struct nes_pbl *nespbl;
2309 struct nes_mr *nesmr;
2310 struct ib_umem *region;
2311 struct nes_mem_reg_req req;
2312 struct nes_vpbl vpbl;
2313 struct nes_root_vpbl root_vpbl;
2314 int nmap_index, page_index;
2315 int page_count = 0;
2316 int err, pbl_depth = 0;
2317 int chunk_pages;
2318 int ret;
2319 u32 stag;
2320 u32 stag_index = 0;
2321 u32 next_stag_index;
2322 u32 driver_key;
2323 u32 root_pbl_index = 0;
2324 u32 cur_pbl_index = 0;
2325 u32 skip_pages;
2326 u16 pbl_count;
2327 u8 single_page = 1;
2328 u8 stag_key;
2329
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002330 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002331 if (IS_ERR(region)) {
2332 return (struct ib_mr *)region;
2333 }
2334
2335 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2336 " offset = %u, page size = %u.\n",
2337 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2338 region->offset, region->page_size);
2339
2340 skip_pages = ((u32)region->offset) >> 12;
2341
2342 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2343 return ERR_PTR(-EFAULT);
2344 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2345
2346 switch (req.reg_type) {
2347 case IWNES_MEMREG_TYPE_MEM:
2348 pbl_depth = 0;
2349 region_length = 0;
2350 vpbl.pbl_vbase = NULL;
2351 root_vpbl.pbl_vbase = NULL;
2352 root_vpbl.pbl_pbase = 0;
2353
2354 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2355 stag_key = (u8)next_stag_index;
2356
2357 driver_key = next_stag_index & 0x70000000;
2358
2359 next_stag_index >>= 8;
2360 next_stag_index %= nesadapter->max_mr;
2361
2362 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2363 nesadapter->max_mr, &stag_index, &next_stag_index);
2364 if (err) {
2365 ib_umem_release(region);
2366 return ERR_PTR(err);
2367 }
2368
2369 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2370 if (!nesmr) {
2371 ib_umem_release(region);
2372 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2373 return ERR_PTR(-ENOMEM);
2374 }
2375 nesmr->region = region;
2376
2377 list_for_each_entry(chunk, &region->chunk_list, list) {
2378 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2379 chunk->nents, chunk->nmap);
2380 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2381 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2382 ib_umem_release(region);
2383 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2384 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2385 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2386 ibmr = ERR_PTR(-EINVAL);
2387 kfree(nesmr);
2388 goto reg_user_mr_err;
2389 }
2390
2391 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2392 ib_umem_release(region);
2393 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2394 stag_index);
2395 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2396 ibmr = ERR_PTR(-EINVAL);
2397 kfree(nesmr);
2398 goto reg_user_mr_err;
2399 }
2400
2401 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2402 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2403 region_length -= skip_pages << 12;
2404 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2405 skip_pages = 0;
2406 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2407 goto enough_pages;
2408 if ((page_count&0x01FF) == 0) {
Roland Dreier24797a32008-06-10 12:29:49 -07002409 if (page_count >= 1024 * 512) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002410 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002411 nes_free_resource(nesadapter,
2412 nesadapter->allocated_mrs, stag_index);
2413 kfree(nesmr);
2414 ibmr = ERR_PTR(-E2BIG);
2415 goto reg_user_mr_err;
2416 }
2417 if (root_pbl_index == 1) {
2418 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2419 8192, &root_vpbl.pbl_pbase);
2420 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2421 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2422 if (!root_vpbl.pbl_vbase) {
2423 ib_umem_release(region);
2424 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2425 vpbl.pbl_pbase);
2426 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2427 stag_index);
2428 kfree(nesmr);
2429 ibmr = ERR_PTR(-ENOMEM);
2430 goto reg_user_mr_err;
2431 }
2432 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2433 GFP_KERNEL);
2434 if (!root_vpbl.leaf_vpbl) {
2435 ib_umem_release(region);
2436 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2437 root_vpbl.pbl_pbase);
2438 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2439 vpbl.pbl_pbase);
2440 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2441 stag_index);
2442 kfree(nesmr);
2443 ibmr = ERR_PTR(-ENOMEM);
2444 goto reg_user_mr_err;
2445 }
2446 root_vpbl.pbl_vbase[0].pa_low =
2447 cpu_to_le32((u32)vpbl.pbl_pbase);
2448 root_vpbl.pbl_vbase[0].pa_high =
2449 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2450 root_vpbl.leaf_vpbl[0] = vpbl;
2451 }
2452 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2453 &vpbl.pbl_pbase);
2454 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2455 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2456 if (!vpbl.pbl_vbase) {
2457 ib_umem_release(region);
2458 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2459 ibmr = ERR_PTR(-ENOMEM);
2460 kfree(nesmr);
2461 goto reg_user_mr_err;
2462 }
2463 if (1 <= root_pbl_index) {
2464 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2465 cpu_to_le32((u32)vpbl.pbl_pbase);
2466 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2467 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2468 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2469 }
2470 root_pbl_index++;
2471 cur_pbl_index = 0;
2472 }
2473 if (single_page) {
2474 if (page_count != 0) {
2475 if ((last_dma_addr+4096) !=
2476 (sg_dma_address(&chunk->page_list[nmap_index])+
2477 (page_index*4096)))
2478 single_page = 0;
2479 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2480 (page_index*4096);
2481 } else {
2482 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2483 (page_index*4096);
2484 last_dma_addr = first_dma_addr;
2485 }
2486 }
2487
2488 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2489 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2490 (page_index*4096)));
2491 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2492 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2493 (page_index*4096))) >> 32)));
2494 cur_pbl_index++;
2495 page_count++;
2496 }
2497 }
2498 }
2499 enough_pages:
2500 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2501 " stag_key=0x%08x\n",
2502 stag_index, driver_key, stag_key);
2503 stag = stag_index << 8;
2504 stag |= driver_key;
2505 stag += (u32)stag_key;
2506 if (stag == 0) {
2507 stag = 1;
2508 }
2509
2510 iova_start = virt;
2511 /* Make the leaf PBL the root if only one PBL */
2512 if (root_pbl_index == 1) {
2513 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2514 }
2515
2516 if (single_page) {
2517 pbl_count = 0;
2518 } else {
2519 pbl_count = root_pbl_index;
2520 first_dma_addr = 0;
2521 }
2522 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2523 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2524 stag, (unsigned int)iova_start,
2525 (unsigned int)region_length, stag_index,
2526 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002527 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2528 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2529 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002530
2531 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2532
2533 if (ret == 0) {
2534 nesmr->ibmr.rkey = stag;
2535 nesmr->ibmr.lkey = stag;
2536 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2537 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002538 } else {
2539 ib_umem_release(region);
2540 kfree(nesmr);
2541 ibmr = ERR_PTR(-ENOMEM);
2542 }
2543
2544 reg_user_mr_err:
2545 /* free the resources */
2546 if (root_pbl_index == 1) {
2547 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2548 vpbl.pbl_pbase);
2549 } else {
2550 for (page_index=0; page_index<root_pbl_index; page_index++) {
2551 pci_free_consistent(nesdev->pcidev, 4096,
2552 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2553 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2554 }
2555 kfree(root_vpbl.leaf_vpbl);
2556 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2557 root_vpbl.pbl_pbase);
2558 }
2559
2560 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2561
2562 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002563 case IWNES_MEMREG_TYPE_QP:
2564 case IWNES_MEMREG_TYPE_CQ:
2565 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2566 if (!nespbl) {
2567 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2568 ib_umem_release(region);
2569 return ERR_PTR(-ENOMEM);
2570 }
2571 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2572 if (!nesmr) {
2573 ib_umem_release(region);
2574 kfree(nespbl);
2575 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2576 return ERR_PTR(-ENOMEM);
2577 }
2578 nesmr->region = region;
2579 nes_ucontext = to_nesucontext(pd->uobject->context);
2580 pbl_depth = region->length >> 12;
2581 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2582 nespbl->pbl_size = pbl_depth*sizeof(u64);
2583 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2584 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2585 } else {
2586 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2587 }
2588
2589 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2590 nespbl->pbl_size, pbl_depth);
2591 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2592 &nespbl->pbl_pbase);
2593 if (!pbl) {
2594 ib_umem_release(region);
2595 kfree(nesmr);
2596 kfree(nespbl);
2597 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2598 return ERR_PTR(-ENOMEM);
2599 }
2600
2601 nespbl->pbl_vbase = (u64 *)pbl;
2602 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002603 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002604 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002605 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2606 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002607
2608 list_for_each_entry(chunk, &region->chunk_list, list) {
2609 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2610 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2611 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2612 nespbl->page = sg_page(&chunk->page_list[0]);
2613 for (page_index=0; page_index<chunk_pages; page_index++) {
2614 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2615 (sg_dma_address(&chunk->page_list[nmap_index])+
2616 (page_index*4096)));
2617 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2618 (sg_dma_address(&chunk->page_list[nmap_index])+
2619 (page_index*4096)))>>32);
2620 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2621 (unsigned long long)*pbl,
2622 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2623 pbl++;
2624 }
2625 }
2626 }
2627 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2628 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2629 } else {
2630 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2631 }
2632 nesmr->ibmr.rkey = -1;
2633 nesmr->ibmr.lkey = -1;
2634 nesmr->mode = req.reg_type;
2635 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002636 }
2637
2638 return ERR_PTR(-ENOSYS);
2639}
2640
2641
2642/**
2643 * nes_dereg_mr
2644 */
2645static int nes_dereg_mr(struct ib_mr *ib_mr)
2646{
2647 struct nes_mr *nesmr = to_nesmr(ib_mr);
2648 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2649 struct nes_device *nesdev = nesvnic->nesdev;
2650 struct nes_adapter *nesadapter = nesdev->nesadapter;
2651 struct nes_hw_cqp_wqe *cqp_wqe;
2652 struct nes_cqp_request *cqp_request;
2653 unsigned long flags;
2654 int ret;
2655 u16 major_code;
2656 u16 minor_code;
2657
2658 if (nesmr->region) {
2659 ib_umem_release(nesmr->region);
2660 }
2661 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2662 kfree(nesmr);
2663 return 0;
2664 }
2665
2666 /* Deallocate the region with the adapter */
2667
2668 cqp_request = nes_get_cqp_request(nesdev);
2669 if (cqp_request == NULL) {
2670 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2671 return -ENOMEM;
2672 }
2673 cqp_request->waiting = 1;
2674 cqp_wqe = &cqp_request->cqp_wqe;
2675
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002676 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2677 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2678 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2679 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2680 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2681
2682 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002683 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002684
2685 /* Wait for CQP */
2686 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2687 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2688 NES_EVENT_TIMEOUT);
2689 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2690 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2691 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2692
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002693 major_code = cqp_request->major_code;
2694 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002695
2696 nes_put_cqp_request(nesdev, cqp_request);
2697
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002698 if (!ret) {
2699 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2700 " ib_mr=%p, rkey = 0x%08X\n",
2701 ib_mr, ib_mr->rkey);
2702 return -ETIME;
2703 } else if (major_code) {
2704 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2705 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2706 major_code, minor_code, ib_mr, ib_mr->rkey);
2707 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002708 }
2709
2710 if (nesmr->pbls_used != 0) {
2711 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2712 if (nesmr->pbl_4k) {
2713 nesadapter->free_4kpbl += nesmr->pbls_used;
2714 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2715 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2716 "exceeded the max(%u)\n",
2717 nesadapter->free_4kpbl,
2718 nesadapter->max_4kpbl);
2719 } else {
2720 nesadapter->free_256pbl += nesmr->pbls_used;
2721 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2722 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2723 "exceeded the max(%u)\n",
2724 nesadapter->free_256pbl,
2725 nesadapter->max_256pbl);
2726 }
2727 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2728 }
2729 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2730 (ib_mr->rkey & 0x0fffff00) >> 8);
2731
2732 kfree(nesmr);
2733
2734 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002735}
2736
2737
2738/**
2739 * show_rev
2740 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002741static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2742 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002743{
2744 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002745 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002746 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2747
2748 nes_debug(NES_DBG_INIT, "\n");
2749 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2750}
2751
2752
2753/**
2754 * show_fw_ver
2755 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002756static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2757 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002758{
2759 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002760 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002761 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2762
2763 nes_debug(NES_DBG_INIT, "\n");
Chien Tung0e4562d2009-04-27 13:33:48 -07002764 return sprintf(buf, "%u.%u\n",
2765 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2766 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002767}
2768
2769
2770/**
2771 * show_hca
2772 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002773static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2774 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775{
2776 nes_debug(NES_DBG_INIT, "\n");
2777 return sprintf(buf, "NES020\n");
2778}
2779
2780
2781/**
2782 * show_board
2783 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002784static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2785 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002786{
2787 nes_debug(NES_DBG_INIT, "\n");
2788 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2789}
2790
2791
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002792static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2793static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2794static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2795static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002796
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002797static struct device_attribute *nes_dev_attributes[] = {
2798 &dev_attr_hw_rev,
2799 &dev_attr_fw_ver,
2800 &dev_attr_hca_type,
2801 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002802};
2803
2804
2805/**
2806 * nes_query_qp
2807 */
2808static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2809 int attr_mask, struct ib_qp_init_attr *init_attr)
2810{
2811 struct nes_qp *nesqp = to_nesqp(ibqp);
2812
2813 nes_debug(NES_DBG_QP, "\n");
2814
2815 attr->qp_access_flags = 0;
2816 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2817 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2818 attr->cap.max_recv_sge = 1;
2819 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2820 init_attr->cap.max_inline_data = 0;
2821 } else {
2822 init_attr->cap.max_inline_data = 64;
2823 }
2824
2825 init_attr->event_handler = nesqp->ibqp.event_handler;
2826 init_attr->qp_context = nesqp->ibqp.qp_context;
2827 init_attr->send_cq = nesqp->ibqp.send_cq;
2828 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2829 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2830 init_attr->cap = attr->cap;
2831
2832 return 0;
2833}
2834
2835
2836/**
2837 * nes_hw_modify_qp
2838 */
2839int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002840 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002841{
2842 struct nes_hw_cqp_wqe *cqp_wqe;
2843 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2844 /* struct iw_cm_event cm_event; */
2845 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002846 int ret;
2847 u16 major_code;
2848
2849 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2850 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2851
2852 cqp_request = nes_get_cqp_request(nesdev);
2853 if (cqp_request == NULL) {
2854 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2855 return -ENOMEM;
2856 }
2857 if (wait_completion) {
2858 cqp_request->waiting = 1;
2859 } else {
2860 cqp_request->waiting = 0;
2861 }
2862 cqp_wqe = &cqp_request->cqp_wqe;
2863
2864 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2865 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2866 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2867 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2868 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2869 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2870 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2871
Don Wood8b1c9dc2009-09-05 20:36:38 -07002872 /* If sending a terminate message, fill in the length (in words) */
2873 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2874 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2875 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2876 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2877 }
2878
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002879 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002880 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002881
2882 /* Wait for CQP */
2883 if (wait_completion) {
2884 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2885 nesqp->hwqp.qp_id); */
2886 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2887 NES_EVENT_TIMEOUT);
2888 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2889 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2890 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2891 major_code = cqp_request->major_code;
2892 if (major_code) {
2893 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2894 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2895 nesqp->hwqp.qp_id, cqp_request->major_code,
2896 cqp_request->minor_code, next_iwarp_state);
2897 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002898
2899 nes_put_cqp_request(nesdev, cqp_request);
2900
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002901 if (!ret)
2902 return -ETIME;
2903 else if (major_code)
2904 return -EIO;
2905 else
2906 return 0;
2907 } else {
2908 return 0;
2909 }
2910}
2911
2912
2913/**
2914 * nes_modify_qp
2915 */
2916int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2917 int attr_mask, struct ib_udata *udata)
2918{
2919 struct nes_qp *nesqp = to_nesqp(ibqp);
2920 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2921 struct nes_device *nesdev = nesvnic->nesdev;
2922 /* u32 cqp_head; */
2923 /* u32 counter; */
2924 u32 next_iwarp_state = 0;
2925 int err;
2926 unsigned long qplockflags;
2927 int ret;
2928 u16 original_last_aeq;
2929 u8 issue_modify_qp = 0;
2930 u8 issue_disconnect = 0;
2931 u8 dont_wait = 0;
2932
2933 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2934 " iwarp_state=0x%X, refcount=%d\n",
2935 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2936 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2937
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002938 spin_lock_irqsave(&nesqp->lock, qplockflags);
2939
2940 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2941 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2942 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2943 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2944
2945 if (attr_mask & IB_QP_STATE) {
2946 switch (attr->qp_state) {
2947 case IB_QPS_INIT:
2948 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2949 nesqp->hwqp.qp_id);
2950 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2951 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002952 return -EINVAL;
2953 }
2954 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2955 issue_modify_qp = 1;
2956 break;
2957 case IB_QPS_RTR:
2958 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2959 nesqp->hwqp.qp_id);
2960 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2961 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002962 return -EINVAL;
2963 }
2964 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2965 issue_modify_qp = 1;
2966 break;
2967 case IB_QPS_RTS:
2968 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2969 nesqp->hwqp.qp_id);
2970 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2971 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002972 return -EINVAL;
2973 }
2974 if (nesqp->cm_id == NULL) {
2975 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2976 nesqp->hwqp.qp_id );
2977 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002978 return -EINVAL;
2979 }
2980 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2981 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2982 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2983 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2984 issue_modify_qp = 1;
2985 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2986 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2987 nesqp->hte_added = 1;
2988 break;
2989 case IB_QPS_SQD:
2990 issue_modify_qp = 1;
2991 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2992 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2993 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2994 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002995 return 0;
2996 } else {
2997 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2998 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2999 " ignored due to current iWARP state\n",
3000 nesqp->hwqp.qp_id);
3001 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 return -EINVAL;
3003 }
3004 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3005 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3006 " already done based on hw state.\n",
3007 nesqp->hwqp.qp_id);
3008 issue_modify_qp = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003009 }
3010 switch (nesqp->hw_iwarp_state) {
3011 case NES_AEQE_IWARP_STATE_CLOSING:
3012 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3013 case NES_AEQE_IWARP_STATE_TERMINATE:
3014 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3015 break;
3016 case NES_AEQE_IWARP_STATE_ERROR:
3017 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3018 break;
3019 default:
3020 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003021 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3022 break;
3023 }
3024 }
3025 break;
3026 case IB_QPS_SQE:
3027 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3028 nesqp->hwqp.qp_id);
3029 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3030 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003031 return -EINVAL;
3032 }
3033 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3034 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3035 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3036 issue_modify_qp = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003037 break;
3038 case IB_QPS_ERR:
3039 case IB_QPS_RESET:
3040 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3041 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003042 return -EINVAL;
3043 }
3044 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3045 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07003046 if (nesqp->term_flags)
3047 del_timer(&nesqp->terminate_timer);
3048
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003049 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3050 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3051 if (nesqp->hte_added) {
3052 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3053 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3054 nesqp->hte_added = 0;
3055 }
3056 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3057 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3058 next_iwarp_state |= NES_CQP_QP_RESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003059 } else {
3060 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3061 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3062 dont_wait = 1;
3063 }
3064 issue_modify_qp = 1;
3065 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3066 break;
3067 default:
3068 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003069 return -EINVAL;
3070 break;
3071 }
3072
3073 nesqp->ibqp_state = attr->qp_state;
3074 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3075 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3076 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3077 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3078 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3079 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3080 nesqp->iwarp_state);
3081 issue_disconnect = 1;
3082 } else {
3083 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3084 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3085 nesqp->iwarp_state);
3086 }
3087 }
3088
3089 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3090 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3091 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3092 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3093 issue_modify_qp = 1;
3094 }
3095 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3096 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3097 issue_modify_qp = 1;
3098 }
3099 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3100 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3101 issue_modify_qp = 1;
3102 }
3103 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3104 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3105 issue_modify_qp = 1;
3106 }
3107
3108 if (nesqp->user_mode) {
3109 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3110 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3111 issue_modify_qp = 1;
3112 }
3113 }
3114
3115 original_last_aeq = nesqp->last_aeq;
3116 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3117
3118 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3119
3120 ret = 0;
3121
3122
3123 if (issue_modify_qp) {
3124 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07003125 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126 if (ret)
3127 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3128 " failed for QP%u.\n",
3129 next_iwarp_state, nesqp->hwqp.qp_id);
3130
3131 }
3132
3133 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3134 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3135 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3136 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3137 original_last_aeq, nesqp->last_aeq);
3138 if ((!ret) ||
3139 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3140 (ret))) {
3141 if (dont_wait) {
3142 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3143 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3144 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3145 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3146 original_last_aeq, nesqp->last_aeq);
3147 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003148 spin_lock_irqsave(&nesqp->lock, qplockflags);
3149 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3150 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3151 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3152 nes_cm_disconn(nesqp);
3153 } else {
3154 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3155 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003156 }
3157 } else {
3158 spin_lock_irqsave(&nesqp->lock, qplockflags);
3159 if (nesqp->cm_id) {
3160 /* These two are for the timer thread */
3161 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003162 nesqp->cm_id->add_ref(nesqp->cm_id);
3163 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3164 " need ae to finish up, original_last_aeq = 0x%04X."
3165 " last_aeq = 0x%04X, scheduling timer.\n",
3166 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3167 original_last_aeq, nesqp->last_aeq);
3168 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3169 }
3170 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3171 } else {
3172 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3173 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3174 " need ae to finish up, original_last_aeq = 0x%04X."
3175 " last_aeq = 0x%04X.\n",
3176 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3177 original_last_aeq, nesqp->last_aeq);
3178 }
3179 }
3180 } else {
3181 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3182 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3183 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3184 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003185 }
3186 } else {
3187 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3188 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3189 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3190 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003191 }
3192
3193 err = 0;
3194
3195 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3196 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3197
3198 return err;
3199}
3200
3201
3202/**
3203 * nes_muticast_attach
3204 */
3205static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3206{
3207 nes_debug(NES_DBG_INIT, "\n");
3208 return -ENOSYS;
3209}
3210
3211
3212/**
3213 * nes_multicast_detach
3214 */
3215static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3216{
3217 nes_debug(NES_DBG_INIT, "\n");
3218 return -ENOSYS;
3219}
3220
3221
3222/**
3223 * nes_process_mad
3224 */
3225static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3226 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3227 struct ib_mad *in_mad, struct ib_mad *out_mad)
3228{
3229 nes_debug(NES_DBG_INIT, "\n");
3230 return -ENOSYS;
3231}
3232
3233static inline void
3234fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3235{
3236 int sge_index;
3237 int total_payload_length = 0;
3238 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3239 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3240 ib_wr->sg_list[sge_index].addr);
3241 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3242 ib_wr->sg_list[sge_index].length);
3243 if (uselkey)
3244 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3245 (ib_wr->sg_list[sge_index].lkey));
3246 else
3247 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3248
3249 total_payload_length += ib_wr->sg_list[sge_index].length;
3250 }
3251 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3252 total_payload_length);
3253 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3254 total_payload_length);
3255}
3256
3257/**
3258 * nes_post_send
3259 */
3260static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3261 struct ib_send_wr **bad_wr)
3262{
3263 u64 u64temp;
3264 unsigned long flags = 0;
3265 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3266 struct nes_device *nesdev = nesvnic->nesdev;
3267 struct nes_qp *nesqp = to_nesqp(ibqp);
3268 struct nes_hw_qp_wqe *wqe;
Chien Tung649fe4a2009-12-09 13:51:37 -08003269 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003270 u32 qsize = nesqp->hwqp.sq_size;
3271 u32 head;
Chien Tung649fe4a2009-12-09 13:51:37 -08003272 u32 wqe_misc = 0;
3273 u32 wqe_count = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003274 u32 counter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003275
Frank Zagoe5dec392009-12-09 13:51:36 -08003276 if (nesqp->ibqp_state > IB_QPS_RTS) {
3277 err = -EINVAL;
3278 goto out;
3279 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003281 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003282
3283 head = nesqp->hwqp.sq_head;
3284
3285 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003286 /* Check for QP error */
3287 if (nesqp->term_flags) {
3288 err = -EINVAL;
3289 break;
3290 }
3291
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003292 /* Check for SQ overflow */
3293 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3294 err = -EINVAL;
3295 break;
3296 }
3297
3298 wqe = &nesqp->hwqp.sq_vbase[head];
3299 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3300 nesqp->hwqp.qp_id, wqe, head); */
3301 nes_fill_init_qp_wqe(wqe, nesqp, head);
3302 u64temp = (u64)(ib_wr->wr_id);
3303 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3304 u64temp);
Chien Tung649fe4a2009-12-09 13:51:37 -08003305 switch (ib_wr->opcode) {
3306 case IB_WR_SEND:
3307 case IB_WR_SEND_WITH_INV:
3308 if (IB_WR_SEND == ib_wr->opcode) {
3309 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3310 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3311 else
3312 wqe_misc = NES_IWARP_SQ_OP_SEND;
3313 } else {
3314 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3315 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3316 else
3317 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318
Chien Tung649fe4a2009-12-09 13:51:37 -08003319 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3320 ib_wr->ex.invalidate_rkey);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003321 }
3322
Chien Tung649fe4a2009-12-09 13:51:37 -08003323 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3324 err = -EINVAL;
3325 break;
3326 }
3327
3328 if (ib_wr->send_flags & IB_SEND_FENCE)
3329 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3330
3331 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3332 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3333 (ib_wr->sg_list[0].length <= 64)) {
3334 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3335 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3336 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3337 ib_wr->sg_list[0].length);
3338 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3339 } else {
3340 fill_wqe_sg_send(wqe, ib_wr, 1);
3341 }
3342
3343 break;
3344 case IB_WR_RDMA_WRITE:
3345 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3346 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3347 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3348 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3349 err = -EINVAL;
3350 break;
3351 }
3352
3353 if (ib_wr->send_flags & IB_SEND_FENCE)
3354 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3355
3356 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3357 ib_wr->wr.rdma.rkey);
3358 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3359 ib_wr->wr.rdma.remote_addr);
3360
3361 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3362 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3363 (ib_wr->sg_list[0].length <= 64)) {
3364 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3365 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3366 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3367 ib_wr->sg_list[0].length);
3368 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3369 } else {
3370 fill_wqe_sg_send(wqe, ib_wr, 1);
3371 }
3372
3373 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3374 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3375 break;
3376 case IB_WR_RDMA_READ:
3377 case IB_WR_RDMA_READ_WITH_INV:
3378 /* iWARP only supports 1 sge for RDMA reads */
3379 if (ib_wr->num_sge > 1) {
3380 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3381 ib_wr->num_sge);
3382 err = -EINVAL;
3383 break;
3384 }
3385 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3386 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3387 } else {
3388 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3389 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3390 ib_wr->ex.invalidate_rkey);
3391 }
3392
3393 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3394 ib_wr->wr.rdma.remote_addr);
3395 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3396 ib_wr->wr.rdma.rkey);
3397 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3398 ib_wr->sg_list->length);
3399 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3400 ib_wr->sg_list->addr);
3401 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3402 ib_wr->sg_list->lkey);
3403 break;
3404 case IB_WR_LOCAL_INV:
3405 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3406 set_wqe_32bit_value(wqe->wqe_words,
3407 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3408 ib_wr->ex.invalidate_rkey);
3409 break;
Chien Tunge293a262009-12-09 15:21:54 -08003410 case IB_WR_FAST_REG_MR:
3411 {
3412 int i;
3413 int flags = ib_wr->wr.fast_reg.access_flags;
3414 struct nes_ib_fast_reg_page_list *pnesfrpl =
3415 container_of(ib_wr->wr.fast_reg.page_list,
3416 struct nes_ib_fast_reg_page_list,
3417 ibfrpl);
3418 u64 *src_page_list = pnesfrpl->ibfrpl.page_list;
3419 u64 *dst_page_list = pnesfrpl->nes_wqe_pbl.kva;
3420
3421 if (ib_wr->wr.fast_reg.page_list_len >
3422 (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3423 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3424 err = -EINVAL;
3425 break;
3426 }
3427 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3428 set_wqe_64bit_value(wqe->wqe_words,
3429 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3430 ib_wr->wr.fast_reg.iova_start);
3431 set_wqe_32bit_value(wqe->wqe_words,
3432 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3433 ib_wr->wr.fast_reg.length);
3434 set_wqe_32bit_value(wqe->wqe_words,
3435 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3436 ib_wr->wr.fast_reg.rkey);
3437 /* Set page size: */
3438 if (ib_wr->wr.fast_reg.page_shift == 12) {
3439 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3440 } else if (ib_wr->wr.fast_reg.page_shift == 21) {
3441 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3442 } else {
3443 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3444 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3445 err = -EINVAL;
3446 break;
3447 }
3448 /* Set access_flags */
3449 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3450 if (flags & IB_ACCESS_LOCAL_WRITE)
3451 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3452
3453 if (flags & IB_ACCESS_REMOTE_WRITE)
3454 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3455
3456 if (flags & IB_ACCESS_REMOTE_READ)
3457 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3458
3459 if (flags & IB_ACCESS_MW_BIND)
3460 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3461
3462 /* Fill in PBL info: */
3463 if (ib_wr->wr.fast_reg.page_list_len >
3464 pnesfrpl->ibfrpl.max_page_list_len) {
3465 nes_debug(NES_DBG_IW_TX, "Invalid page list length,"
3466 " ib_wr=%p, value=%u, max=%u\n",
3467 ib_wr, ib_wr->wr.fast_reg.page_list_len,
3468 pnesfrpl->ibfrpl.max_page_list_len);
3469 err = -EINVAL;
3470 break;
3471 }
3472
3473 set_wqe_64bit_value(wqe->wqe_words,
3474 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3475 pnesfrpl->nes_wqe_pbl.paddr);
3476
3477 set_wqe_32bit_value(wqe->wqe_words,
3478 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3479 ib_wr->wr.fast_reg.page_list_len * 8);
3480
3481 for (i = 0; i < ib_wr->wr.fast_reg.page_list_len; i++)
3482 dst_page_list[i] = cpu_to_le64(src_page_list[i]);
3483
3484 nes_debug(NES_DBG_IW_TX, "SQ_FMR: iova_start: %p, "
3485 "length: %d, rkey: %0x, pgl_paddr: %p, "
3486 "page_list_len: %u, wqe_misc: %x\n",
3487 (void *)ib_wr->wr.fast_reg.iova_start,
3488 ib_wr->wr.fast_reg.length,
3489 ib_wr->wr.fast_reg.rkey,
3490 (void *)pnesfrpl->nes_wqe_pbl.paddr,
3491 ib_wr->wr.fast_reg.page_list_len,
3492 wqe_misc);
3493 break;
3494 }
Chien Tung649fe4a2009-12-09 13:51:37 -08003495 default:
3496 /* error */
3497 err = -EINVAL;
3498 break;
3499 }
3500
Frank Zagoe5dec392009-12-09 13:51:36 -08003501 if (err)
3502 break;
3503
Chien Tungd14152d2009-12-09 15:21:56 -08003504 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003505 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
Chien Tungd14152d2009-12-09 15:21:56 -08003506
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003507 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3508
3509 ib_wr = ib_wr->next;
3510 head++;
3511 wqe_count++;
3512 if (head >= qsize)
3513 head = 0;
3514
3515 }
3516
3517 nesqp->hwqp.sq_head = head;
3518 barrier();
3519 while (wqe_count) {
3520 counter = min(wqe_count, ((u32)255));
3521 wqe_count -= counter;
3522 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3523 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3524 }
3525
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003526 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003527
Frank Zagoe5dec392009-12-09 13:51:36 -08003528out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003529 if (err)
3530 *bad_wr = ib_wr;
3531 return err;
3532}
3533
3534
3535/**
3536 * nes_post_recv
3537 */
3538static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3539 struct ib_recv_wr **bad_wr)
3540{
3541 u64 u64temp;
3542 unsigned long flags = 0;
3543 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3544 struct nes_device *nesdev = nesvnic->nesdev;
3545 struct nes_qp *nesqp = to_nesqp(ibqp);
3546 struct nes_hw_qp_wqe *wqe;
3547 int err = 0;
3548 int sge_index;
3549 u32 qsize = nesqp->hwqp.rq_size;
3550 u32 head;
3551 u32 wqe_count = 0;
3552 u32 counter;
3553 u32 total_payload_length;
3554
Frank Zago4293fdc12009-12-09 13:51:36 -08003555 if (nesqp->ibqp_state > IB_QPS_RTS) {
3556 err = -EINVAL;
3557 goto out;
3558 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003559
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003560 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003561
3562 head = nesqp->hwqp.rq_head;
3563
3564 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003565 /* Check for QP error */
3566 if (nesqp->term_flags) {
3567 err = -EINVAL;
3568 break;
3569 }
3570
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003571 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3572 err = -EINVAL;
3573 break;
3574 }
3575 /* Check for RQ overflow */
3576 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3577 err = -EINVAL;
3578 break;
3579 }
3580
3581 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3582 wqe = &nesqp->hwqp.rq_vbase[head];
3583
3584 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3585 nesqp->hwqp.qp_id, wqe, head); */
3586 nes_fill_init_qp_wqe(wqe, nesqp, head);
3587 u64temp = (u64)(ib_wr->wr_id);
3588 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3589 u64temp);
3590 total_payload_length = 0;
3591 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3592 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3593 ib_wr->sg_list[sge_index].addr);
3594 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3595 ib_wr->sg_list[sge_index].length);
3596 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3597 ib_wr->sg_list[sge_index].lkey);
3598
3599 total_payload_length += ib_wr->sg_list[sge_index].length;
3600 }
3601 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3602 total_payload_length);
3603
3604 ib_wr = ib_wr->next;
3605 head++;
3606 wqe_count++;
3607 if (head >= qsize)
3608 head = 0;
3609 }
3610
3611 nesqp->hwqp.rq_head = head;
3612 barrier();
3613 while (wqe_count) {
3614 counter = min(wqe_count, ((u32)255));
3615 wqe_count -= counter;
3616 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3617 }
3618
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003619 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003620
Frank Zago4293fdc12009-12-09 13:51:36 -08003621out:
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003622 if (err)
3623 *bad_wr = ib_wr;
3624 return err;
3625}
3626
3627
3628/**
3629 * nes_poll_cq
3630 */
3631static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3632{
3633 u64 u64temp;
3634 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003635 unsigned long flags = 0;
3636 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3637 struct nes_device *nesdev = nesvnic->nesdev;
3638 struct nes_cq *nescq = to_nescq(ibcq);
3639 struct nes_qp *nesqp;
3640 struct nes_hw_cqe cqe;
3641 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003642 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003643 u32 cq_size;
3644 u32 cqe_count = 0;
3645 u32 wqe_index;
3646 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003647 u32 move_cq_head = 1;
3648 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003649
3650 nes_debug(NES_DBG_CQ, "\n");
3651
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003652 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003653
3654 head = nescq->hw_cq.cq_head;
3655 cq_size = nescq->hw_cq.cq_size;
3656
3657 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003658 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3659 NES_CQE_VALID) == 0)
3660 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003661
Don Wood5ee21fe2009-09-05 20:36:37 -07003662 /*
3663 * Make sure we read CQ entry contents *after*
3664 * we've checked the valid bit.
3665 */
3666 rmb();
3667
3668 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003669 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3670 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3671 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3672 /* parse CQE, get completion context from WQE (either rq or sq) */
3673 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3674 ((u64)u32temp);
3675
3676 if (u64temp) {
3677 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003678 memset(entry, 0, sizeof *entry);
3679 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3680 entry->status = IB_WC_SUCCESS;
3681 } else {
Don Wood320cdfd2009-09-05 20:36:39 -07003682 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3683 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3684 entry->status = err_code & 0x0000ffff;
3685
3686 /* The rest of the cqe's will be marked as flushed */
3687 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3688 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3689 NES_IWARP_CQE_MINOR_FLUSH);
3690 } else
3691 entry->status = IB_WC_WR_FLUSH_ERR;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003692 }
3693
3694 entry->qp = &nesqp->ibqp;
3695 entry->src_qp = nesqp->hwqp.qp_id;
3696
3697 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3698 if (nesqp->skip_lsmm) {
3699 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003700 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003701 }
3702
3703 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003704 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003705 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003706 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003707 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003708 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003709 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3710
Don Wood6eed5e72009-09-05 20:36:39 -07003711 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003712 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3713 case NES_IWARP_SQ_OP_RDMAW:
3714 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3715 entry->opcode = IB_WC_RDMA_WRITE;
3716 break;
3717 case NES_IWARP_SQ_OP_RDMAR:
3718 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3719 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003720 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003721 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3722 break;
3723 case NES_IWARP_SQ_OP_SENDINV:
3724 case NES_IWARP_SQ_OP_SENDSEINV:
3725 case NES_IWARP_SQ_OP_SEND:
3726 case NES_IWARP_SQ_OP_SENDSE:
3727 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3728 entry->opcode = IB_WC_SEND;
3729 break;
Chien Tung649fe4a2009-12-09 13:51:37 -08003730 case NES_IWARP_SQ_OP_LOCINV:
3731 entry->opcode = IB_WR_LOCAL_INV;
3732 break;
Chien Tunge293a262009-12-09 15:21:54 -08003733 case NES_IWARP_SQ_OP_FAST_REG:
3734 entry->opcode = IB_WC_FAST_REG_MR;
3735 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003736 }
Don Wood6eed5e72009-09-05 20:36:39 -07003737
3738 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3739 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3740 move_cq_head = 0;
3741 wq_tail = nesqp->hwqp.sq_tail;
3742 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003743 } else {
3744 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003745 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003746 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3747 ((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 -08003748 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003749
3750 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3751 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3752 move_cq_head = 0;
3753 wq_tail = nesqp->hwqp.rq_tail;
3754 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003755 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003756
Don Wood5ee21fe2009-09-05 20:36:37 -07003757 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003758 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003759 cqe_count++;
3760 }
3761
Don Wood6eed5e72009-09-05 20:36:39 -07003762 if (move_cq_head) {
3763 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3764 if (++head >= cq_size)
3765 head = 0;
3766 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003767
Don Wood6eed5e72009-09-05 20:36:39 -07003768 if ((nescq->polled_completions > (cq_size / 2)) ||
3769 (nescq->polled_completions == 255)) {
3770 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003771 " are pending %u of %u.\n",
3772 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003773 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003774 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003775 nescq->polled_completions = 0;
3776 }
3777 } else {
3778 /* Update the wqe index and set status to flush */
3779 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3780 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3781 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3782 cpu_to_le32(wqe_index);
3783 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003784 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003785 }
3786
3787 if (nescq->polled_completions) {
3788 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3789 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3790 nescq->polled_completions = 0;
3791 }
3792
3793 nescq->hw_cq.cq_head = head;
3794 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3795 cqe_count, nescq->hw_cq.cq_number);
3796
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003797 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003798
3799 return cqe_count;
3800}
3801
3802
3803/**
3804 * nes_req_notify_cq
3805 */
3806static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3807 {
3808 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3809 struct nes_device *nesdev = nesvnic->nesdev;
3810 struct nes_cq *nescq = to_nescq(ibcq);
3811 u32 cq_arm;
3812
3813 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3814 nescq->hw_cq.cq_number);
3815
3816 cq_arm = nescq->hw_cq.cq_number;
3817 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3818 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3819 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3820 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3821 else
3822 return -EINVAL;
3823
3824 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3825 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3826
3827 return 0;
3828}
3829
3830
3831/**
3832 * nes_init_ofa_device
3833 */
3834struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3835{
3836 struct nes_ib_device *nesibdev;
3837 struct nes_vnic *nesvnic = netdev_priv(netdev);
3838 struct nes_device *nesdev = nesvnic->nesdev;
3839
3840 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3841 if (nesibdev == NULL) {
3842 return NULL;
3843 }
3844 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3845 nesibdev->ibdev.owner = THIS_MODULE;
3846
3847 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3848 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3849 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3850
3851 nesibdev->ibdev.uverbs_cmd_mask =
3852 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3853 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3854 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3855 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3856 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3857 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3858 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3859 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3860 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3861 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3862 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3863 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3864 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3865 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3866 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3867 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3868 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3869 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3870 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3871 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3872 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3873 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3874
3875 nesibdev->ibdev.phys_port_cnt = 1;
3876 nesibdev->ibdev.num_comp_vectors = 1;
3877 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003878 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003879 nesibdev->ibdev.query_device = nes_query_device;
3880 nesibdev->ibdev.query_port = nes_query_port;
3881 nesibdev->ibdev.modify_port = nes_modify_port;
3882 nesibdev->ibdev.query_pkey = nes_query_pkey;
3883 nesibdev->ibdev.query_gid = nes_query_gid;
3884 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3885 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3886 nesibdev->ibdev.mmap = nes_mmap;
3887 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3888 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3889 nesibdev->ibdev.create_ah = nes_create_ah;
3890 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3891 nesibdev->ibdev.create_qp = nes_create_qp;
3892 nesibdev->ibdev.modify_qp = nes_modify_qp;
3893 nesibdev->ibdev.query_qp = nes_query_qp;
3894 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3895 nesibdev->ibdev.create_cq = nes_create_cq;
3896 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3897 nesibdev->ibdev.poll_cq = nes_poll_cq;
3898 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3899 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3900 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3901 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3902 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3903 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3904 nesibdev->ibdev.bind_mw = nes_bind_mw;
3905
Chien Tunge293a262009-12-09 15:21:54 -08003906 nesibdev->ibdev.alloc_fast_reg_mr = nes_alloc_fast_reg_mr;
3907 nesibdev->ibdev.alloc_fast_reg_page_list = nes_alloc_fast_reg_page_list;
3908 nesibdev->ibdev.free_fast_reg_page_list = nes_free_fast_reg_page_list;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003909
3910 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3911 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3912 nesibdev->ibdev.process_mad = nes_process_mad;
3913
3914 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3915 nesibdev->ibdev.post_send = nes_post_send;
3916 nesibdev->ibdev.post_recv = nes_post_recv;
3917
3918 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3919 if (nesibdev->ibdev.iwcm == NULL) {
3920 ib_dealloc_device(&nesibdev->ibdev);
3921 return NULL;
3922 }
3923 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3924 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3925 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3926 nesibdev->ibdev.iwcm->connect = nes_connect;
3927 nesibdev->ibdev.iwcm->accept = nes_accept;
3928 nesibdev->ibdev.iwcm->reject = nes_reject;
3929 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3930 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3931
3932 return nesibdev;
3933}
3934
3935
3936/**
3937 * nes_destroy_ofa_device
3938 */
3939void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3940{
3941 if (nesibdev == NULL)
3942 return;
3943
3944 nes_unregister_ofa_device(nesibdev);
3945
3946 kfree(nesibdev->ibdev.iwcm);
3947 ib_dealloc_device(&nesibdev->ibdev);
3948}
3949
3950
3951/**
3952 * nes_register_ofa_device
3953 */
3954int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3955{
3956 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3957 struct nes_device *nesdev = nesvnic->nesdev;
3958 struct nes_adapter *nesadapter = nesdev->nesadapter;
3959 int i, ret;
3960
3961 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3962 if (ret) {
3963 return ret;
3964 }
3965
3966 /* Get the resources allocated to this device */
3967 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3968 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3969 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3970 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3971
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003972 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3973 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003974 if (ret) {
3975 while (i > 0) {
3976 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003977 device_remove_file(&nesibdev->ibdev.dev,
3978 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003979 }
3980 ib_unregister_device(&nesibdev->ibdev);
3981 return ret;
3982 }
3983 }
3984
3985 nesvnic->of_device_registered = 1;
3986
3987 return 0;
3988}
3989
3990
3991/**
3992 * nes_unregister_ofa_device
3993 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003994static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003995{
3996 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3997 int i;
3998
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003999 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4000 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004001 }
4002
4003 if (nesvnic->of_device_registered) {
4004 ib_unregister_device(&nesibdev->ibdev);
4005 }
4006
4007 nesvnic->of_device_registered = 0;
4008}