blob: ad3c891d0f6517bb1ba76a2394725696cdbaa95a [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungcd6853d2009-03-06 15:12:10 -08002 * Copyright (c) 2006 - 2009 Intel-NE, Inc. 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
278/**
279 * nes_alloc_fmr
280 */
281static struct ib_fmr *nes_alloc_fmr(struct ib_pd *ibpd,
282 int ibmr_access_flags,
283 struct ib_fmr_attr *ibfmr_attr)
284{
285 unsigned long flags;
286 struct nes_pd *nespd = to_nespd(ibpd);
287 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
288 struct nes_device *nesdev = nesvnic->nesdev;
289 struct nes_adapter *nesadapter = nesdev->nesadapter;
290 struct nes_fmr *nesfmr;
291 struct nes_cqp_request *cqp_request;
292 struct nes_hw_cqp_wqe *cqp_wqe;
293 int ret;
294 u32 stag;
295 u32 stag_index = 0;
296 u32 next_stag_index = 0;
297 u32 driver_key = 0;
298 u32 opcode = 0;
299 u8 stag_key = 0;
300 int i=0;
301 struct nes_vpbl vpbl;
302
303 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
304 stag_key = (u8)next_stag_index;
305
306 driver_key = 0;
307
308 next_stag_index >>= 8;
309 next_stag_index %= nesadapter->max_mr;
310
311 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
312 nesadapter->max_mr, &stag_index, &next_stag_index);
313 if (ret) {
314 goto failed_resource_alloc;
315 }
316
317 nesfmr = kzalloc(sizeof(*nesfmr), GFP_KERNEL);
318 if (!nesfmr) {
319 ret = -ENOMEM;
320 goto failed_fmr_alloc;
321 }
322
323 nesfmr->nesmr.mode = IWNES_MEMREG_TYPE_FMR;
324 if (ibfmr_attr->max_pages == 1) {
325 /* use zero length PBL */
326 nesfmr->nesmr.pbl_4k = 0;
327 nesfmr->nesmr.pbls_used = 0;
328 } else if (ibfmr_attr->max_pages <= 32) {
329 /* use PBL 256 */
330 nesfmr->nesmr.pbl_4k = 0;
331 nesfmr->nesmr.pbls_used = 1;
332 } else if (ibfmr_attr->max_pages <= 512) {
333 /* use 4K PBLs */
334 nesfmr->nesmr.pbl_4k = 1;
335 nesfmr->nesmr.pbls_used = 1;
336 } else {
337 /* use two level 4K PBLs */
338 /* add support for two level 256B PBLs */
339 nesfmr->nesmr.pbl_4k = 1;
340 nesfmr->nesmr.pbls_used = 1 + (ibfmr_attr->max_pages >> 9) +
341 ((ibfmr_attr->max_pages & 511) ? 1 : 0);
342 }
343 /* Register the region with the adapter */
344 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
345
346 /* track PBL resources */
347 if (nesfmr->nesmr.pbls_used != 0) {
348 if (nesfmr->nesmr.pbl_4k) {
349 if (nesfmr->nesmr.pbls_used > nesadapter->free_4kpbl) {
350 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
351 ret = -ENOMEM;
Chien Tung35c6d692008-11-02 21:37:35 -0800352 goto failed_vpbl_avail;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800353 } else {
354 nesadapter->free_4kpbl -= nesfmr->nesmr.pbls_used;
355 }
356 } else {
357 if (nesfmr->nesmr.pbls_used > nesadapter->free_256pbl) {
358 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
359 ret = -ENOMEM;
Chien Tung35c6d692008-11-02 21:37:35 -0800360 goto failed_vpbl_avail;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800361 } else {
362 nesadapter->free_256pbl -= nesfmr->nesmr.pbls_used;
363 }
364 }
365 }
366
367 /* one level pbl */
368 if (nesfmr->nesmr.pbls_used == 0) {
369 nesfmr->root_vpbl.pbl_vbase = NULL;
370 nes_debug(NES_DBG_MR, "zero level pbl \n");
371 } else if (nesfmr->nesmr.pbls_used == 1) {
372 /* can change it to kmalloc & dma_map_single */
373 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
374 &nesfmr->root_vpbl.pbl_pbase);
375 if (!nesfmr->root_vpbl.pbl_vbase) {
376 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
377 ret = -ENOMEM;
378 goto failed_vpbl_alloc;
379 }
380 nesfmr->leaf_pbl_cnt = 0;
381 nes_debug(NES_DBG_MR, "one level pbl, root_vpbl.pbl_vbase=%p \n",
382 nesfmr->root_vpbl.pbl_vbase);
383 }
384 /* two level pbl */
385 else {
386 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
387 &nesfmr->root_vpbl.pbl_pbase);
388 if (!nesfmr->root_vpbl.pbl_vbase) {
389 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
390 ret = -ENOMEM;
391 goto failed_vpbl_alloc;
392 }
393
Chien Tung35c6d692008-11-02 21:37:35 -0800394 nesfmr->leaf_pbl_cnt = nesfmr->nesmr.pbls_used-1;
395 nesfmr->root_vpbl.leaf_vpbl = kzalloc(sizeof(*nesfmr->root_vpbl.leaf_vpbl)*1024, GFP_ATOMIC);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800396 if (!nesfmr->root_vpbl.leaf_vpbl) {
397 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
398 ret = -ENOMEM;
399 goto failed_leaf_vpbl_alloc;
400 }
401
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800402 nes_debug(NES_DBG_MR, "two level pbl, root_vpbl.pbl_vbase=%p"
403 " leaf_pbl_cnt=%d root_vpbl.leaf_vpbl=%p\n",
404 nesfmr->root_vpbl.pbl_vbase, nesfmr->leaf_pbl_cnt, nesfmr->root_vpbl.leaf_vpbl);
405
406 for (i=0; i<nesfmr->leaf_pbl_cnt; i++)
407 nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase = NULL;
408
409 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
410 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
411 &vpbl.pbl_pbase);
412
413 if (!vpbl.pbl_vbase) {
414 ret = -ENOMEM;
415 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
416 goto failed_leaf_vpbl_pages_alloc;
417 }
418
419 nesfmr->root_vpbl.pbl_vbase[i].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
420 nesfmr->root_vpbl.pbl_vbase[i].pa_high = cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
421 nesfmr->root_vpbl.leaf_vpbl[i] = vpbl;
422
423 nes_debug(NES_DBG_MR, "pbase_low=0x%x, pbase_high=0x%x, vpbl=%p\n",
424 nesfmr->root_vpbl.pbl_vbase[i].pa_low,
425 nesfmr->root_vpbl.pbl_vbase[i].pa_high,
426 &nesfmr->root_vpbl.leaf_vpbl[i]);
427 }
428 }
429 nesfmr->ib_qp = NULL;
430 nesfmr->access_rights =0;
431
432 stag = stag_index << 8;
433 stag |= driver_key;
434 stag += (u32)stag_key;
435
436 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
437 cqp_request = nes_get_cqp_request(nesdev);
438 if (cqp_request == NULL) {
439 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
440 ret = -ENOMEM;
441 goto failed_leaf_vpbl_pages_alloc;
442 }
443 cqp_request->waiting = 1;
444 cqp_wqe = &cqp_request->cqp_wqe;
445
446 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
447 stag, stag_index);
448
449 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
450
451 if (nesfmr->nesmr.pbl_4k == 1)
452 opcode |= NES_CQP_STAG_PBL_BLK_SIZE;
453
454 if (ibmr_access_flags & IB_ACCESS_REMOTE_WRITE) {
455 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE |
456 NES_CQP_STAG_RIGHTS_LOCAL_WRITE | NES_CQP_STAG_REM_ACC_EN;
457 nesfmr->access_rights |=
458 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_RIGHTS_LOCAL_WRITE |
459 NES_CQP_STAG_REM_ACC_EN;
460 }
461
462 if (ibmr_access_flags & IB_ACCESS_REMOTE_READ) {
463 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ |
464 NES_CQP_STAG_RIGHTS_LOCAL_READ | NES_CQP_STAG_REM_ACC_EN;
465 nesfmr->access_rights |=
466 NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_RIGHTS_LOCAL_READ |
467 NES_CQP_STAG_REM_ACC_EN;
468 }
469
470 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
471 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
472 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
473 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
474
475 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX] =
476 cpu_to_le32((nesfmr->nesmr.pbls_used>1) ?
477 (nesfmr->nesmr.pbls_used-1) : nesfmr->nesmr.pbls_used);
478
479 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -0700480 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800481
482 /* Wait for CQP */
483 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
484 NES_EVENT_TIMEOUT);
485 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
486 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
487 stag, ret, cqp_request->major_code, cqp_request->minor_code);
488
489 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -0700490 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800491 ret = (!ret) ? -ETIME : -EIO;
492 goto failed_leaf_vpbl_pages_alloc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800493 }
Roland Dreier1ff66e82008-07-14 23:48:49 -0700494 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800495 nesfmr->nesmr.ibfmr.lkey = stag;
496 nesfmr->nesmr.ibfmr.rkey = stag;
497 nesfmr->attr = *ibfmr_attr;
498
499 return &nesfmr->nesmr.ibfmr;
500
501 failed_leaf_vpbl_pages_alloc:
502 /* unroll all allocated pages */
503 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
504 if (nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase) {
505 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
506 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
507 }
508 }
509 if (nesfmr->root_vpbl.leaf_vpbl)
510 kfree(nesfmr->root_vpbl.leaf_vpbl);
511
512 failed_leaf_vpbl_alloc:
513 if (nesfmr->leaf_pbl_cnt == 0) {
514 if (nesfmr->root_vpbl.pbl_vbase)
515 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
516 nesfmr->root_vpbl.pbl_pbase);
517 } else
518 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
519 nesfmr->root_vpbl.pbl_pbase);
520
521 failed_vpbl_alloc:
Chien Tung35c6d692008-11-02 21:37:35 -0800522 if (nesfmr->nesmr.pbls_used != 0) {
523 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
524 if (nesfmr->nesmr.pbl_4k)
525 nesadapter->free_4kpbl += nesfmr->nesmr.pbls_used;
526 else
527 nesadapter->free_256pbl += nesfmr->nesmr.pbls_used;
528 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
529 }
530
531failed_vpbl_avail:
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800532 kfree(nesfmr);
533
534 failed_fmr_alloc:
535 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
536
537 failed_resource_alloc:
538 return ERR_PTR(ret);
539}
540
541
542/**
543 * nes_dealloc_fmr
544 */
545static int nes_dealloc_fmr(struct ib_fmr *ibfmr)
546{
Chien Tung35c6d692008-11-02 21:37:35 -0800547 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800548 struct nes_mr *nesmr = to_nesmr_from_ibfmr(ibfmr);
549 struct nes_fmr *nesfmr = to_nesfmr(nesmr);
550 struct nes_vnic *nesvnic = to_nesvnic(ibfmr->device);
551 struct nes_device *nesdev = nesvnic->nesdev;
Chien Tung35c6d692008-11-02 21:37:35 -0800552 struct nes_adapter *nesadapter = nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800553 int i = 0;
Don Wooddae5d132009-03-06 15:12:09 -0800554 int rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800555
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800556 /* free the resources */
557 if (nesfmr->leaf_pbl_cnt == 0) {
558 /* single PBL case */
559 if (nesfmr->root_vpbl.pbl_vbase)
560 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
561 nesfmr->root_vpbl.pbl_pbase);
562 } else {
563 for (i = 0; i < nesfmr->leaf_pbl_cnt; i++) {
564 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
565 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
566 }
567 kfree(nesfmr->root_vpbl.leaf_vpbl);
568 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
569 nesfmr->root_vpbl.pbl_pbase);
570 }
Chien Tung35c6d692008-11-02 21:37:35 -0800571 nesmr->ibmw.device = ibfmr->device;
572 nesmr->ibmw.pd = ibfmr->pd;
573 nesmr->ibmw.rkey = ibfmr->rkey;
574 nesmr->ibmw.uobject = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575
Don Wooddae5d132009-03-06 15:12:09 -0800576 rc = nes_dealloc_mw(&nesmr->ibmw);
577
578 if ((rc == 0) && (nesfmr->nesmr.pbls_used != 0)) {
Chien Tung35c6d692008-11-02 21:37:35 -0800579 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
580 if (nesfmr->nesmr.pbl_4k) {
581 nesadapter->free_4kpbl += nesfmr->nesmr.pbls_used;
582 WARN_ON(nesadapter->free_4kpbl > nesadapter->max_4kpbl);
583 } else {
584 nesadapter->free_256pbl += nesfmr->nesmr.pbls_used;
585 WARN_ON(nesadapter->free_256pbl > nesadapter->max_256pbl);
586 }
587 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
588 }
589
Don Wooddae5d132009-03-06 15:12:09 -0800590 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800591}
592
593
594/**
595 * nes_map_phys_fmr
596 */
597static int nes_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
598 int list_len, u64 iova)
599{
600 return 0;
601}
602
603
604/**
605 * nes_unmap_frm
606 */
607static int nes_unmap_fmr(struct list_head *ibfmr_list)
608{
609 return 0;
610}
611
612
613
614/**
615 * nes_query_device
616 */
617static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
618{
619 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
620 struct nes_device *nesdev = nesvnic->nesdev;
621 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
622
623 memset(props, 0, sizeof(*props));
624 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
625
626 props->fw_ver = nesdev->nesadapter->fw_ver;
627 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
628 props->vendor_id = nesdev->nesadapter->vendor_id;
629 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
630 props->hw_ver = nesdev->nesadapter->hw_rev;
631 props->max_mr_size = 0x80000000;
632 props->max_qp = nesibdev->max_qp;
633 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
634 props->max_sge = nesdev->nesadapter->max_sge;
635 props->max_cq = nesibdev->max_cq;
636 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
637 props->max_mr = nesibdev->max_mr;
638 props->max_mw = nesibdev->max_mr;
639 props->max_pd = nesibdev->max_pd;
640 props->max_sge_rd = 1;
641 switch (nesdev->nesadapter->max_irrq_wr) {
642 case 0:
643 props->max_qp_rd_atom = 1;
644 break;
645 case 1:
646 props->max_qp_rd_atom = 4;
647 break;
648 case 2:
649 props->max_qp_rd_atom = 16;
650 break;
651 case 3:
652 props->max_qp_rd_atom = 32;
653 break;
654 default:
655 props->max_qp_rd_atom = 0;
656 }
Faisal Latif66388d62009-06-22 22:52:30 -0700657 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800658 props->atomic_cap = IB_ATOMIC_NONE;
659 props->max_map_per_fmr = 1;
660
661 return 0;
662}
663
664
665/**
666 * nes_query_port
667 */
668static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
669{
670 memset(props, 0, sizeof(*props));
671
672 props->max_mtu = IB_MTU_2048;
673 props->active_mtu = IB_MTU_2048;
674 props->lid = 1;
675 props->lmc = 0;
676 props->sm_lid = 0;
677 props->sm_sl = 0;
678 props->state = IB_PORT_ACTIVE;
679 props->phys_state = 0;
680 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
681 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
682 props->gid_tbl_len = 1;
683 props->pkey_tbl_len = 1;
684 props->qkey_viol_cntr = 0;
685 props->active_width = IB_WIDTH_4X;
686 props->active_speed = 1;
687 props->max_msg_sz = 0x80000000;
688
689 return 0;
690}
691
692
693/**
694 * nes_modify_port
695 */
696static int nes_modify_port(struct ib_device *ibdev, u8 port,
697 int port_modify_mask, struct ib_port_modify *props)
698{
699 return 0;
700}
701
702
703/**
704 * nes_query_pkey
705 */
706static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
707{
708 *pkey = 0;
709 return 0;
710}
711
712
713/**
714 * nes_query_gid
715 */
716static int nes_query_gid(struct ib_device *ibdev, u8 port,
717 int index, union ib_gid *gid)
718{
719 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
720
721 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
722 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
723
724 return 0;
725}
726
727
728/**
729 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
730 * of all objects associated with a particular user-mode client.
731 */
732static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
733 struct ib_udata *udata)
734{
735 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
736 struct nes_device *nesdev = nesvnic->nesdev;
737 struct nes_adapter *nesadapter = nesdev->nesadapter;
738 struct nes_alloc_ucontext_req req;
739 struct nes_alloc_ucontext_resp uresp;
740 struct nes_ucontext *nes_ucontext;
741 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
742
743
744 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
745 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
746 return ERR_PTR(-EINVAL);
747 }
748
749 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
750 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
751 req.userspace_ver, NES_ABI_USERSPACE_VER);
752 return ERR_PTR(-EINVAL);
753 }
754
755
756 memset(&uresp, 0, sizeof uresp);
757
758 uresp.max_qps = nesibdev->max_qp;
759 uresp.max_pds = nesibdev->max_pd;
760 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
761 uresp.virtwq = nesadapter->virtwq;
762 uresp.kernel_ver = NES_ABI_KERNEL_VER;
763
764 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
765 if (!nes_ucontext)
766 return ERR_PTR(-ENOMEM);
767
768 nes_ucontext->nesdev = nesdev;
769 nes_ucontext->mmap_wq_offset = uresp.max_pds;
770 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
771 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
772 PAGE_SIZE;
773
774
775 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
776 kfree(nes_ucontext);
777 return ERR_PTR(-EFAULT);
778 }
779
780 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
781 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
782 atomic_set(&nes_ucontext->usecnt, 1);
783 return &nes_ucontext->ibucontext;
784}
785
786
787/**
788 * nes_dealloc_ucontext
789 */
790static int nes_dealloc_ucontext(struct ib_ucontext *context)
791{
792 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
793 /* struct nes_device *nesdev = nesvnic->nesdev; */
794 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
795
796 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
797 return 0;
798 kfree(nes_ucontext);
799 return 0;
800}
801
802
803/**
804 * nes_mmap
805 */
806static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
807{
808 unsigned long index;
809 struct nes_vnic *nesvnic = to_nesvnic(context->device);
810 struct nes_device *nesdev = nesvnic->nesdev;
811 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
812 struct nes_ucontext *nes_ucontext;
813 struct nes_qp *nesqp;
814
815 nes_ucontext = to_nesucontext(context);
816
817
818 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
819 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
820 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
821 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
822 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
823 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
824 return -EFAULT;
825 }
826 nesqp = nes_ucontext->mmap_nesqp[index];
827 if (nesqp == NULL) {
828 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
829 return -EFAULT;
830 }
831 if (remap_pfn_range(vma, vma->vm_start,
832 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
833 vma->vm_end - vma->vm_start,
834 vma->vm_page_prot)) {
835 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
836 return -EAGAIN;
837 }
838 vma->vm_private_data = nesqp;
839 return 0;
840 } else {
841 index = vma->vm_pgoff;
842 if (!test_bit(index, nes_ucontext->allocated_doorbells))
843 return -EFAULT;
844
845 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
846 if (io_remap_pfn_range(vma, vma->vm_start,
847 (nesdev->doorbell_start +
848 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
849 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
850 return -EAGAIN;
851 vma->vm_private_data = nes_ucontext;
852 return 0;
853 }
854
855 return -ENOSYS;
856}
857
858
859/**
860 * nes_alloc_pd
861 */
862static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
863 struct ib_ucontext *context, struct ib_udata *udata)
864{
865 struct nes_pd *nespd;
866 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
867 struct nes_device *nesdev = nesvnic->nesdev;
868 struct nes_adapter *nesadapter = nesdev->nesadapter;
869 struct nes_ucontext *nesucontext;
870 struct nes_alloc_pd_resp uresp;
871 u32 pd_num = 0;
872 int err;
873
874 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
875 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
876 atomic_read(&nesvnic->netdev->refcnt));
877
878 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
879 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
880 if (err) {
881 return ERR_PTR(err);
882 }
883
884 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
885 if (!nespd) {
886 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
887 return ERR_PTR(-ENOMEM);
888 }
889
890 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
891 nespd, nesvnic->nesibdev->ibdev.name);
892
893 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
894
895 if (context) {
896 nesucontext = to_nesucontext(context);
897 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
898 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
899 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
900 nespd->mmap_db_index, nespd->pd_id);
Adrian Bunk65b07ec2008-02-21 08:01:18 -0600901 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
903 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
904 kfree(nespd);
905 return ERR_PTR(-ENOMEM);
906 }
907
908 uresp.pd_id = nespd->pd_id;
909 uresp.mmap_db_index = nespd->mmap_db_index;
910 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
911 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
912 kfree(nespd);
913 return ERR_PTR(-EFAULT);
914 }
915
916 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
917 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
918 nesucontext->first_free_db = nespd->mmap_db_index + 1;
919 }
920
921 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
922 return &nespd->ibpd;
923}
924
925
926/**
927 * nes_dealloc_pd
928 */
929static int nes_dealloc_pd(struct ib_pd *ibpd)
930{
931 struct nes_ucontext *nesucontext;
932 struct nes_pd *nespd = to_nespd(ibpd);
933 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
934 struct nes_device *nesdev = nesvnic->nesdev;
935 struct nes_adapter *nesadapter = nesdev->nesadapter;
936
937 if ((ibpd->uobject) && (ibpd->uobject->context)) {
938 nesucontext = to_nesucontext(ibpd->uobject->context);
939 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
940 nespd->mmap_db_index);
941 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
942 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
943 if (nesucontext->first_free_db > nespd->mmap_db_index) {
944 nesucontext->first_free_db = nespd->mmap_db_index;
945 }
946 }
947
948 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
949 nespd->pd_id, nespd);
950 nes_free_resource(nesadapter, nesadapter->allocated_pds,
951 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
952 kfree(nespd);
953
954 return 0;
955}
956
957
958/**
959 * nes_create_ah
960 */
961static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
962{
963 return ERR_PTR(-ENOSYS);
964}
965
966
967/**
968 * nes_destroy_ah
969 */
970static int nes_destroy_ah(struct ib_ah *ah)
971{
972 return -ENOSYS;
973}
974
975
976/**
977 * nes_get_encoded_size
978 */
979static inline u8 nes_get_encoded_size(int *size)
980{
981 u8 encoded_size = 0;
982 if (*size <= 32) {
983 *size = 32;
984 encoded_size = 1;
985 } else if (*size <= 128) {
986 *size = 128;
987 encoded_size = 2;
988 } else if (*size <= 512) {
989 *size = 512;
990 encoded_size = 3;
991 }
992 return (encoded_size);
993}
994
995
996
997/**
998 * nes_setup_virt_qp
999 */
1000static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
1001 struct nes_vnic *nesvnic, int sq_size, int rq_size)
1002{
1003 unsigned long flags;
1004 void *mem;
1005 __le64 *pbl = NULL;
1006 __le64 *tpbl;
1007 __le64 *pblbuffer;
1008 struct nes_device *nesdev = nesvnic->nesdev;
1009 struct nes_adapter *nesadapter = nesdev->nesadapter;
1010 u32 pbl_entries;
1011 u8 rq_pbl_entries;
1012 u8 sq_pbl_entries;
1013
1014 pbl_entries = nespbl->pbl_size >> 3;
Roland Dreier71e09572008-04-16 21:01:09 -07001015 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 -08001016 nespbl->pbl_size, pbl_entries,
1017 (void *)nespbl->pbl_vbase,
Roland Dreier71e09572008-04-16 21:01:09 -07001018 (unsigned long) nespbl->pbl_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001019 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
1020 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
1021 /* the first pbl to be fro the rq_vbase... */
1022 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1023 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1024 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1025 if (!nespbl->page) {
1026 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
1027 kfree(nespbl);
1028 return -ENOMEM;
1029 }
1030
1031 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
1032 nesqp->page = nespbl->page;
1033 if (!nesqp->hwqp.sq_vbase) {
1034 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
1035 kfree(nespbl);
1036 return -ENOMEM;
1037 }
1038
1039 /* Now to get to sq.. we need to calculate how many */
1040 /* PBL entries were used by the rq.. */
1041 pbl += sq_pbl_entries;
1042 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1043 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
1044 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
1045
Roland Dreier71e09572008-04-16 21:01:09 -07001046 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
1047 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
1048 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001049 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1050 if (!nesadapter->free_256pbl) {
1051 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1052 nespbl->pbl_pbase);
1053 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1054 kunmap(nesqp->page);
1055 kfree(nespbl);
1056 return -ENOMEM;
1057 }
1058 nesadapter->free_256pbl--;
1059 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1060
1061 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
1062 pblbuffer = nesqp->pbl_vbase;
1063 if (!nesqp->pbl_vbase) {
1064 /* memory allocated during nes_reg_user_mr() */
1065 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1066 nespbl->pbl_pbase);
1067 kfree(nespbl);
1068 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1069 nesadapter->free_256pbl++;
1070 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1071 kunmap(nesqp->page);
1072 return -ENOMEM;
1073 }
1074 memset(nesqp->pbl_vbase, 0, 256);
1075 /* fill in the page address in the pbl buffer.. */
1076 tpbl = pblbuffer + 16;
1077 pbl = (__le64 *)nespbl->pbl_vbase;
1078 while (sq_pbl_entries--)
1079 *tpbl++ = *pbl++;
1080 tpbl = pblbuffer;
1081 while (rq_pbl_entries--)
1082 *tpbl++ = *pbl++;
1083
1084 /* done with memory allocated during nes_reg_user_mr() */
1085 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1086 nespbl->pbl_pbase);
1087 kfree(nespbl);
1088
1089 nesqp->qp_mem_size =
1090 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1091 /* Round up to a multiple of a page */
1092 nesqp->qp_mem_size += PAGE_SIZE - 1;
1093 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1094
1095 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1096 &nesqp->hwqp.q2_pbase);
1097
1098 if (!mem) {
1099 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1100 nesqp->pbl_vbase = NULL;
1101 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1102 nesadapter->free_256pbl++;
1103 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1104 kunmap(nesqp->page);
1105 return -ENOMEM;
1106 }
1107 nesqp->hwqp.q2_vbase = mem;
1108 mem += 256;
1109 memset(nesqp->hwqp.q2_vbase, 0, 256);
1110 nesqp->nesqp_context = mem;
1111 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1112 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1113
1114 return 0;
1115}
1116
1117
1118/**
1119 * nes_setup_mmap_qp
1120 */
1121static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1122 int sq_size, int rq_size)
1123{
1124 void *mem;
1125 struct nes_device *nesdev = nesvnic->nesdev;
1126
1127 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1128 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1129 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1130 256; /* this is Q2 */
1131 /* Round up to a multiple of a page */
1132 nesqp->qp_mem_size += PAGE_SIZE - 1;
1133 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1134
1135 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1136 &nesqp->hwqp.sq_pbase);
1137 if (!mem)
1138 return -ENOMEM;
1139 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1140 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1141 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1142
1143 memset(mem, 0, nesqp->qp_mem_size);
1144
1145 nesqp->hwqp.sq_vbase = mem;
1146 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1147
1148 nesqp->hwqp.rq_vbase = mem;
1149 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1150 sizeof(struct nes_hw_qp_wqe) * sq_size;
1151 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1152
1153 nesqp->hwqp.q2_vbase = mem;
1154 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1155 sizeof(struct nes_hw_qp_wqe) * rq_size;
1156 mem += 256;
1157 memset(nesqp->hwqp.q2_vbase, 0, 256);
1158
1159 nesqp->nesqp_context = mem;
1160 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1161 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1162 return 0;
1163}
1164
1165
1166/**
1167 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1168 */
1169static inline void nes_free_qp_mem(struct nes_device *nesdev,
1170 struct nes_qp *nesqp, int virt_wqs)
1171{
1172 unsigned long flags;
1173 struct nes_adapter *nesadapter = nesdev->nesadapter;
1174 if (!virt_wqs) {
1175 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1176 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1177 }else {
1178 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1179 nesadapter->free_256pbl++;
1180 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1181 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1182 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1183 nesqp->pbl_vbase = NULL;
1184 kunmap(nesqp->page);
1185 }
1186}
1187
1188
1189/**
1190 * nes_create_qp
1191 */
1192static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1193 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1194{
1195 u64 u64temp= 0;
1196 u64 u64nesqp = 0;
1197 struct nes_pd *nespd = to_nespd(ibpd);
1198 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1199 struct nes_device *nesdev = nesvnic->nesdev;
1200 struct nes_adapter *nesadapter = nesdev->nesadapter;
1201 struct nes_qp *nesqp;
1202 struct nes_cq *nescq;
1203 struct nes_ucontext *nes_ucontext;
1204 struct nes_hw_cqp_wqe *cqp_wqe;
1205 struct nes_cqp_request *cqp_request;
1206 struct nes_create_qp_req req;
1207 struct nes_create_qp_resp uresp;
1208 struct nes_pbl *nespbl = NULL;
1209 u32 qp_num = 0;
1210 u32 opcode = 0;
1211 /* u32 counter = 0; */
1212 void *mem;
1213 unsigned long flags;
1214 int ret;
1215 int err;
1216 int virt_wqs = 0;
1217 int sq_size;
1218 int rq_size;
1219 u8 sq_encoded_size;
1220 u8 rq_encoded_size;
1221 /* int counter; */
1222
Eli Cohenb846f252008-04-16 21:09:27 -07001223 if (init_attr->create_flags)
1224 return ERR_PTR(-EINVAL);
1225
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001226 atomic_inc(&qps_created);
1227 switch (init_attr->qp_type) {
1228 case IB_QPT_RC:
1229 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1230 init_attr->cap.max_inline_data = 0;
1231 } else {
1232 init_attr->cap.max_inline_data = 64;
1233 }
1234 sq_size = init_attr->cap.max_send_wr;
1235 rq_size = init_attr->cap.max_recv_wr;
1236
Glenn Streiff7495ab62008-04-29 13:46:54 -07001237 /* check if the encoded sizes are OK or not... */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001238 sq_encoded_size = nes_get_encoded_size(&sq_size);
1239 rq_encoded_size = nes_get_encoded_size(&rq_size);
1240
1241 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1242 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1243 rq_size, sq_size);
1244 return ERR_PTR(-EINVAL);
1245 }
1246
1247 init_attr->cap.max_send_wr = sq_size -2;
1248 init_attr->cap.max_recv_wr = rq_size -1;
1249 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1250
1251 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1252 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1253 if (ret) {
1254 return ERR_PTR(ret);
1255 }
1256
1257 /* Need 512 (actually now 1024) byte alignment on this structure */
1258 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1259 if (!mem) {
1260 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1261 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1262 return ERR_PTR(-ENOMEM);
1263 }
1264 u64nesqp = (unsigned long)mem;
1265 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1266 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1267 u64nesqp &= ~u64temp;
1268 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1269 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1270 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1271 nesqp->allocated_buffer = mem;
1272
1273 if (udata) {
1274 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1275 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1276 kfree(nesqp->allocated_buffer);
1277 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1278 return NULL;
1279 }
1280 if (req.user_wqe_buffers) {
1281 virt_wqs = 1;
1282 }
1283 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1284 nesqp->user_mode = 1;
1285 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1286 if (virt_wqs) {
1287 err = 1;
1288 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1289 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1290 list_del(&nespbl->list);
1291 err = 0;
1292 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1293 nespbl, nespbl->user_base);
1294 break;
1295 }
1296 }
1297 if (err) {
1298 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1299 (long long unsigned int)req.user_wqe_buffers);
1300 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1301 kfree(nesqp->allocated_buffer);
Chien Tung9300c0c2008-02-21 07:51:17 -06001302 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001303 }
1304 }
1305
1306 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1307 nesqp->mmap_sq_db_index =
1308 find_next_zero_bit(nes_ucontext->allocated_wqs,
1309 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1310 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1311 nespd->mmap_db_index); */
Roland Dreier51af33e2008-02-18 10:33:59 -08001312 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001313 nes_debug(NES_DBG_QP,
1314 "db index > max user regions, failing create QP\n");
1315 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1316 if (virt_wqs) {
1317 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1318 nespbl->pbl_pbase);
1319 kfree(nespbl);
1320 }
1321 kfree(nesqp->allocated_buffer);
1322 return ERR_PTR(-ENOMEM);
1323 }
1324 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1325 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1326 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1327 } else {
1328 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1329 kfree(nesqp->allocated_buffer);
1330 return ERR_PTR(-EFAULT);
1331 }
1332 }
1333 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1334 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1335 if (err) {
1336 nes_debug(NES_DBG_QP,
1337 "error geting qp mem code = %d\n", err);
1338 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1339 kfree(nesqp->allocated_buffer);
1340 return ERR_PTR(-ENOMEM);
1341 }
1342
1343 nesqp->hwqp.sq_size = sq_size;
1344 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1345 nesqp->hwqp.sq_head = 1;
1346 nesqp->hwqp.rq_size = rq_size;
1347 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1348 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1349 (void *)nesqp->nesqp_context_pbase);
1350 */
1351 nesqp->hwqp.qp_id = qp_num;
1352 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1353 nesqp->nespd = nespd;
1354
1355 nescq = to_nescq(init_attr->send_cq);
1356 nesqp->nesscq = nescq;
1357 nescq = to_nescq(init_attr->recv_cq);
1358 nesqp->nesrcq = nescq;
1359
1360 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1361 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1362 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1363 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1364 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1365 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001366 if (!udata) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001367 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1368 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
Faisal Latifc12e56e2009-03-12 14:34:59 -07001369 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001370 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1371 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1372 u64temp = (u64)nesqp->hwqp.sq_pbase;
1373 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1374 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1375
1376
1377 if (!virt_wqs) {
1378 u64temp = (u64)nesqp->hwqp.sq_pbase;
1379 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1380 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1381 u64temp = (u64)nesqp->hwqp.rq_pbase;
1382 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1383 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1384 } else {
1385 u64temp = (u64)nesqp->pbl_pbase;
1386 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1387 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1388 }
1389
1390 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1391 nesvnic->next_qp_nic_index,
1392 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1393 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1394 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1395 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1396 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1397 nesvnic->next_qp_nic_index++;
1398 if ((nesvnic->next_qp_nic_index > 3) ||
1399 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1400 nesvnic->next_qp_nic_index = 0;
1401 }
1402 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1403
1404 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1405 u64temp = (u64)nesqp->hwqp.q2_pbase;
1406 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1407 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1408 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1409 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1410 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1411 ((((u32)nesadapter->max_irrq_wr) <<
1412 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1413 if (disable_mpa_crc) {
1414 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1415 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1416 }
1417
1418
1419 /* Create the QP */
1420 cqp_request = nes_get_cqp_request(nesdev);
1421 if (cqp_request == NULL) {
1422 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1423 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1424 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1425 kfree(nesqp->allocated_buffer);
1426 return ERR_PTR(-ENOMEM);
1427 }
1428 cqp_request->waiting = 1;
1429 cqp_wqe = &cqp_request->cqp_wqe;
1430
1431 if (!virt_wqs) {
1432 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1433 NES_CQP_QP_IWARP_STATE_IDLE;
1434 } else {
1435 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1436 NES_CQP_QP_IWARP_STATE_IDLE;
1437 }
1438 opcode |= NES_CQP_QP_CQS_VALID;
1439 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1440 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1441 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1442
1443 u64temp = (u64)nesqp->nesqp_context_pbase;
1444 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1445
1446 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001447 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001448
1449 /* Wait for CQP */
1450 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1451 nesqp->hwqp.qp_id);
1452 ret = wait_event_timeout(cqp_request->waitq,
1453 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1454 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1455 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1456 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1457 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1458 cqp_request->major_code, cqp_request->minor_code);
1459 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001460 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001461 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1462 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1463 kfree(nesqp->allocated_buffer);
1464 if (!ret) {
1465 return ERR_PTR(-ETIME);
1466 } else {
1467 return ERR_PTR(-EIO);
1468 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469 }
1470
Roland Dreier1ff66e82008-07-14 23:48:49 -07001471 nes_put_cqp_request(nesdev, cqp_request);
1472
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001473 if (ibpd->uobject) {
1474 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1475 uresp.actual_sq_size = sq_size;
1476 uresp.actual_rq_size = rq_size;
1477 uresp.qp_id = nesqp->hwqp.qp_id;
1478 uresp.nes_drv_opt = nes_drv_opt;
1479 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1480 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1481 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1482 kfree(nesqp->allocated_buffer);
1483 return ERR_PTR(-EFAULT);
1484 }
1485 }
1486
1487 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1488 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1489 spin_lock_init(&nesqp->lock);
1490 init_waitqueue_head(&nesqp->state_waitq);
1491 init_waitqueue_head(&nesqp->kick_waitq);
1492 nes_add_ref(&nesqp->ibqp);
1493 break;
1494 default:
1495 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1496 return ERR_PTR(-EINVAL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001497 }
1498
1499 /* update the QP table */
1500 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1501 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1502 atomic_read(&nesvnic->netdev->refcnt));
1503
1504 return &nesqp->ibqp;
1505}
1506
1507
1508/**
Don Wood5ee21fe2009-09-05 20:36:37 -07001509 * nes_clean_cq
1510 */
1511static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1512{
1513 u32 cq_head;
1514 u32 lo;
1515 u32 hi;
1516 u64 u64temp;
1517 unsigned long flags = 0;
1518
1519 spin_lock_irqsave(&nescq->lock, flags);
1520
1521 cq_head = nescq->hw_cq.cq_head;
1522 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1523 rmb();
1524 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1525 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1526 u64temp = (((u64)hi) << 32) | ((u64)lo);
1527 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1528 if (u64temp == (u64)(unsigned long)nesqp) {
1529 /* Zero the context value so cqe will be ignored */
1530 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1531 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1532 }
1533
1534 if (++cq_head >= nescq->hw_cq.cq_size)
1535 cq_head = 0;
1536 }
1537
1538 spin_unlock_irqrestore(&nescq->lock, flags);
1539}
1540
1541
1542/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001543 * nes_destroy_qp
1544 */
1545static int nes_destroy_qp(struct ib_qp *ibqp)
1546{
1547 struct nes_qp *nesqp = to_nesqp(ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001548 struct nes_ucontext *nes_ucontext;
1549 struct ib_qp_attr attr;
1550 struct iw_cm_id *cm_id;
1551 struct iw_cm_event cm_event;
1552 int ret;
1553
1554 atomic_inc(&sw_qps_destroyed);
1555 nesqp->destroyed = 1;
1556
1557 /* Blow away the connection if it exists. */
1558 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1559 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1560 attr.qp_state = IB_QPS_ERR;
1561 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1562 }
1563
1564 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1565 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1566 cm_id = nesqp->cm_id;
1567 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1568 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1569 cm_event.local_addr = cm_id->local_addr;
1570 cm_event.remote_addr = cm_id->remote_addr;
1571 cm_event.private_data = NULL;
1572 cm_event.private_data_len = 0;
1573
1574 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1575 "QP%u. cm_id = %p, refcount = %u. \n",
1576 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1577
1578 cm_id->rem_ref(cm_id);
1579 ret = cm_id->event_handler(cm_id, &cm_event);
1580 if (ret)
1581 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1582 }
1583
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001584 if (nesqp->user_mode) {
1585 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1586 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1587 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1588 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1589 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1590 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1591 }
1592 }
1593 if (nesqp->pbl_pbase)
1594 kunmap(nesqp->page);
Don Wood5ee21fe2009-09-05 20:36:37 -07001595 } else {
1596 /* Clean any pending completions from the cq(s) */
1597 if (nesqp->nesscq)
1598 nes_clean_cq(nesqp, nesqp->nesscq);
1599
1600 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1601 nes_clean_cq(nesqp, nesqp->nesrcq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001602 }
1603
1604 nes_rem_ref(&nesqp->ibqp);
1605 return 0;
1606}
1607
1608
1609/**
1610 * nes_create_cq
1611 */
1612static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1613 int comp_vector,
1614 struct ib_ucontext *context, struct ib_udata *udata)
1615{
1616 u64 u64temp;
1617 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1618 struct nes_device *nesdev = nesvnic->nesdev;
1619 struct nes_adapter *nesadapter = nesdev->nesadapter;
1620 struct nes_cq *nescq;
1621 struct nes_ucontext *nes_ucontext = NULL;
1622 struct nes_cqp_request *cqp_request;
1623 void *mem = NULL;
1624 struct nes_hw_cqp_wqe *cqp_wqe;
1625 struct nes_pbl *nespbl = NULL;
1626 struct nes_create_cq_req req;
1627 struct nes_create_cq_resp resp;
1628 u32 cq_num = 0;
1629 u32 opcode = 0;
1630 u32 pbl_entries = 1;
1631 int err;
1632 unsigned long flags;
1633 int ret;
1634
1635 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1636 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1637 if (err) {
1638 return ERR_PTR(err);
1639 }
1640
1641 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1642 if (!nescq) {
1643 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1644 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1645 return ERR_PTR(-ENOMEM);
1646 }
1647
1648 nescq->hw_cq.cq_size = max(entries + 1, 5);
1649 nescq->hw_cq.cq_number = cq_num;
1650 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1651
1652
1653 if (context) {
1654 nes_ucontext = to_nesucontext(context);
1655 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1656 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1657 kfree(nescq);
1658 return ERR_PTR(-EFAULT);
1659 }
1660 nesvnic->mcrq_ucontext = nes_ucontext;
1661 nes_ucontext->mcrqf = req.mcrqf;
1662 if (nes_ucontext->mcrqf) {
1663 if (nes_ucontext->mcrqf & 0x80000000)
Vadim Makhervaks2e369542008-11-02 21:39:17 -08001664 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001665 else if (nes_ucontext->mcrqf & 0x40000000)
1666 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1667 else
1668 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001669 nescq->mcrqf = nes_ucontext->mcrqf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001670 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1671 }
1672 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1673 (unsigned long)req.user_cq_buffer, entries);
Chien Tung9300c0c2008-02-21 07:51:17 -06001674 err = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001675 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1676 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1677 list_del(&nespbl->list);
1678 err = 0;
1679 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1680 nespbl);
1681 break;
1682 }
1683 }
1684 if (err) {
1685 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1686 kfree(nescq);
Chien Tung9300c0c2008-02-21 07:51:17 -06001687 return ERR_PTR(-EFAULT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001688 }
1689
1690 pbl_entries = nespbl->pbl_size >> 3;
1691 nescq->cq_mem_size = 0;
1692 } else {
1693 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1694 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1695 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1696
1697 /* allocate the physical buffer space */
1698 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1699 &nescq->hw_cq.cq_pbase);
1700 if (!mem) {
1701 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1702 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1703 kfree(nescq);
1704 return ERR_PTR(-ENOMEM);
1705 }
1706
1707 memset(mem, 0, nescq->cq_mem_size);
1708 nescq->hw_cq.cq_vbase = mem;
1709 nescq->hw_cq.cq_head = 0;
1710 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1711 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1712 (u32)nescq->hw_cq.cq_pbase);
1713 }
1714
1715 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1716 spin_lock_init(&nescq->lock);
1717
1718 /* send CreateCQ request to CQP */
1719 cqp_request = nes_get_cqp_request(nesdev);
1720 if (cqp_request == NULL) {
1721 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1722 if (!context)
1723 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1724 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001725 else {
1726 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1727 nespbl->pbl_vbase, nespbl->pbl_pbase);
1728 kfree(nespbl);
1729 }
1730
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001731 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1732 kfree(nescq);
1733 return ERR_PTR(-ENOMEM);
1734 }
1735 cqp_request->waiting = 1;
1736 cqp_wqe = &cqp_request->cqp_wqe;
1737
1738 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1739 NES_CQP_CQ_CHK_OVERFLOW |
1740 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1741
1742 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1743
1744 if (pbl_entries != 1) {
1745 if (pbl_entries > 32) {
1746 /* use 4k pbl */
1747 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1748 if (nesadapter->free_4kpbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001749 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1750 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001751 if (!context)
1752 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1753 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001754 else {
1755 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1756 nespbl->pbl_vbase, nespbl->pbl_pbase);
1757 kfree(nespbl);
1758 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001759 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1760 kfree(nescq);
1761 return ERR_PTR(-ENOMEM);
1762 } else {
1763 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1764 nescq->virtual_cq = 2;
1765 nesadapter->free_4kpbl--;
1766 }
1767 } else {
1768 /* use 256 byte pbl */
1769 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1770 if (nesadapter->free_256pbl == 0) {
Faisal Latifc5d321e2008-11-21 20:50:38 -06001771 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1772 nes_free_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001773 if (!context)
1774 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1775 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001776 else {
1777 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1778 nespbl->pbl_vbase, nespbl->pbl_pbase);
1779 kfree(nespbl);
1780 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001781 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1782 kfree(nescq);
1783 return ERR_PTR(-ENOMEM);
1784 } else {
1785 opcode |= NES_CQP_CQ_VIRT;
1786 nescq->virtual_cq = 1;
1787 nesadapter->free_256pbl--;
1788 }
1789 }
1790 }
1791
1792 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1793
1794 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1795 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1796 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1797 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1798
1799 if (context) {
1800 if (pbl_entries != 1)
1801 u64temp = (u64)nespbl->pbl_pbase;
1802 else
1803 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1804 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1805 nes_ucontext->mmap_db_index[0]);
1806 } else {
1807 u64temp = (u64)nescq->hw_cq.cq_pbase;
1808 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1809 }
1810 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1811 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1812 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1813 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1814 cpu_to_le32((u32)(u64temp >> 1));
1815 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1816 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1817
1818 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001819 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001820
1821 /* Wait for CQP */
1822 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1823 nescq->hw_cq.cq_number);
1824 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1825 NES_EVENT_TIMEOUT * 2);
1826 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1827 nescq->hw_cq.cq_number, ret);
1828 if ((!ret) || (cqp_request->major_code)) {
Roland Dreier1ff66e82008-07-14 23:48:49 -07001829 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001830 if (!context)
1831 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1832 nescq->hw_cq.cq_pbase);
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001833 else {
1834 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1835 nespbl->pbl_vbase, nespbl->pbl_pbase);
1836 kfree(nespbl);
1837 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001838 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1839 kfree(nescq);
1840 return ERR_PTR(-EIO);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001841 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001842 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001843
1844 if (context) {
1845 /* free the nespbl */
1846 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1847 nespbl->pbl_pbase);
1848 kfree(nespbl);
1849 resp.cq_id = nescq->hw_cq.cq_number;
1850 resp.cq_size = nescq->hw_cq.cq_size;
1851 resp.mmap_db_index = 0;
1852 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1853 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1854 kfree(nescq);
1855 return ERR_PTR(-EFAULT);
1856 }
1857 }
1858
1859 return &nescq->ibcq;
1860}
1861
1862
1863/**
1864 * nes_destroy_cq
1865 */
1866static int nes_destroy_cq(struct ib_cq *ib_cq)
1867{
1868 struct nes_cq *nescq;
1869 struct nes_device *nesdev;
1870 struct nes_vnic *nesvnic;
1871 struct nes_adapter *nesadapter;
1872 struct nes_hw_cqp_wqe *cqp_wqe;
1873 struct nes_cqp_request *cqp_request;
1874 unsigned long flags;
1875 u32 opcode = 0;
1876 int ret;
1877
1878 if (ib_cq == NULL)
1879 return 0;
1880
1881 nescq = to_nescq(ib_cq);
1882 nesvnic = to_nesvnic(ib_cq->device);
1883 nesdev = nesvnic->nesdev;
1884 nesadapter = nesdev->nesadapter;
1885
1886 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1887
1888 /* Send DestroyCQ request to CQP */
1889 cqp_request = nes_get_cqp_request(nesdev);
1890 if (cqp_request == NULL) {
1891 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1892 return -ENOMEM;
1893 }
1894 cqp_request->waiting = 1;
1895 cqp_wqe = &cqp_request->cqp_wqe;
1896 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1897 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1898 if (nescq->virtual_cq == 1) {
1899 nesadapter->free_256pbl++;
1900 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1901 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001902 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001903 }
1904 } else if (nescq->virtual_cq == 2) {
1905 nesadapter->free_4kpbl++;
1906 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1907 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001908 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001909 }
1910 opcode |= NES_CQP_CQ_4KB_CHUNK;
1911 }
1912
1913 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1914
1915 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1916 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1917 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1918 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
Miroslaw Walukiewicz5d1af5c2009-04-21 16:16:48 -07001919 if (!nescq->mcrqf)
1920 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1921
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001922 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07001923 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001924
1925 /* Wait for CQP */
1926 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1927 nescq->hw_cq.cq_number);
1928 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1929 NES_EVENT_TIMEOUT);
1930 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1931 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1932 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1933 cqp_request->minor_code);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001934 if (!ret) {
1935 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001936 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001937 ret = -ETIME;
1938 } else if (cqp_request->major_code) {
1939 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001940 nescq->hw_cq.cq_number);
Roland Dreier1ff66e82008-07-14 23:48:49 -07001941 ret = -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001942 } else {
1943 ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001944 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07001945 nes_put_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001946
1947 if (nescq->cq_mem_size)
1948 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
Roland Dreier31d1e342008-04-23 11:55:45 -07001949 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001950 kfree(nescq);
1951
1952 return ret;
1953}
1954
Don Wood0145f342009-03-06 15:15:00 -08001955/**
1956 * root_256
1957 */
1958static u32 root_256(struct nes_device *nesdev,
1959 struct nes_root_vpbl *root_vpbl,
1960 struct nes_root_vpbl *new_root,
Matt Kraaicc005fa2009-04-21 10:43:21 -07001961 u16 pbl_count_4k)
Don Wood0145f342009-03-06 15:15:00 -08001962{
1963 u64 leaf_pbl;
1964 int i, j, k;
1965
1966 if (pbl_count_4k == 1) {
1967 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1968 512, &new_root->pbl_pbase);
1969
1970 if (new_root->pbl_vbase == NULL)
1971 return 0;
1972
1973 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1974 for (i = 0; i < 16; i++) {
1975 new_root->pbl_vbase[i].pa_low =
1976 cpu_to_le32((u32)leaf_pbl);
1977 new_root->pbl_vbase[i].pa_high =
1978 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1979 leaf_pbl += 256;
1980 }
1981 } else {
1982 for (i = 3; i >= 0; i--) {
1983 j = i * 16;
1984 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1985 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1986 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1987 << 32);
1988 for (k = 1; k < 16; k++) {
1989 leaf_pbl += 256;
1990 root_vpbl->pbl_vbase[j + k].pa_low =
1991 cpu_to_le32((u32)leaf_pbl);
1992 root_vpbl->pbl_vbase[j + k].pa_high =
1993 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1994 }
1995 }
1996 }
1997
1998 return 1;
1999}
2000
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002001
2002/**
2003 * nes_reg_mr
2004 */
2005static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
2006 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08002007 dma_addr_t single_buffer, u16 pbl_count_4k,
2008 u16 residual_page_count_4k, int acc, u64 *iova_start,
2009 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002010{
2011 struct nes_hw_cqp_wqe *cqp_wqe;
2012 struct nes_cqp_request *cqp_request;
2013 unsigned long flags;
2014 int ret;
2015 struct nes_adapter *nesadapter = nesdev->nesadapter;
Don Wood0145f342009-03-06 15:15:00 -08002016 uint pg_cnt = 0;
Chien Tung1af92222009-04-20 14:50:36 -07002017 u16 pbl_count_256 = 0;
Don Wood0145f342009-03-06 15:15:00 -08002018 u16 pbl_count = 0;
2019 u8 use_256_pbls = 0;
2020 u8 use_4k_pbls = 0;
2021 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
2022 struct nes_root_vpbl new_root = {0, 0, 0};
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002023 u32 opcode = 0;
2024 u16 major_code;
2025
2026 /* Register the region with the adapter */
2027 cqp_request = nes_get_cqp_request(nesdev);
2028 if (cqp_request == NULL) {
2029 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2030 return -ENOMEM;
2031 }
2032 cqp_request->waiting = 1;
2033 cqp_wqe = &cqp_request->cqp_wqe;
2034
Don Wood0145f342009-03-06 15:15:00 -08002035 if (pbl_count_4k) {
2036 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2037
2038 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
2039 pbl_count_256 = (pg_cnt + 31) / 32;
2040 if (pg_cnt <= 32) {
2041 if (pbl_count_256 <= nesadapter->free_256pbl)
2042 use_256_pbls = 1;
2043 else if (pbl_count_4k <= nesadapter->free_4kpbl)
2044 use_4k_pbls = 1;
2045 } else if (pg_cnt <= 2048) {
2046 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
2047 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
2048 use_4k_pbls = 1;
2049 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
2050 use_256_pbls = 1;
2051 use_two_level = 1;
2052 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2053 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002054 }
2055 } else {
Don Wood0145f342009-03-06 15:15:00 -08002056 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
2057 use_4k_pbls = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002058 }
Don Wood0145f342009-03-06 15:15:00 -08002059
2060 if (use_256_pbls) {
2061 pbl_count = pbl_count_256;
2062 nesadapter->free_256pbl -= pbl_count + use_two_level;
2063 } else if (use_4k_pbls) {
2064 pbl_count = pbl_count_4k;
2065 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2066 } else {
2067 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2068 nes_debug(NES_DBG_MR, "Out of Pbls\n");
2069 nes_free_cqp_request(nesdev, cqp_request);
2070 return -ENOMEM;
2071 }
2072
2073 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002074 }
2075
Don Wood0145f342009-03-06 15:15:00 -08002076 if (use_256_pbls && use_two_level) {
Matt Kraaicc005fa2009-04-21 10:43:21 -07002077 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
Don Wood0145f342009-03-06 15:15:00 -08002078 if (new_root.pbl_pbase != 0)
2079 root_vpbl = &new_root;
2080 } else {
2081 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2082 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
2083 use_256_pbls = 0;
2084
2085 if (pbl_count_4k == 1)
2086 use_two_level = 0;
2087 pbl_count = pbl_count_4k;
2088
2089 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2090 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2091 use_4k_pbls = 1;
2092 }
2093 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2094
2095 if (use_4k_pbls == 0)
2096 return -ENOMEM;
2097 }
2098 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002099
2100 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2101 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2102 if (acc & IB_ACCESS_LOCAL_WRITE)
2103 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2104 if (acc & IB_ACCESS_REMOTE_WRITE)
2105 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2106 if (acc & IB_ACCESS_REMOTE_READ)
2107 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2108 if (acc & IB_ACCESS_MW_BIND)
2109 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2110
2111 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2112 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2113 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2114 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2115
2116 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2117 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2118 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2119 cpu_to_le32(nespd->pd_id & 0x00007fff);
2120 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2121
2122 if (pbl_count == 0) {
2123 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2124 } else {
2125 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2126 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 -08002127 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 -08002128
Don Wood0145f342009-03-06 15:15:00 -08002129 if (use_4k_pbls)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002130 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2131 }
2132 barrier();
2133
2134 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002135 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002136
2137 /* Wait for CQP */
2138 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2139 NES_EVENT_TIMEOUT);
2140 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2141 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2142 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2143 major_code = cqp_request->major_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002144 nes_put_cqp_request(nesdev, cqp_request);
2145
Don Wooddae5d132009-03-06 15:12:09 -08002146 if ((!ret || major_code) && pbl_count != 0) {
2147 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
Don Wood0145f342009-03-06 15:15:00 -08002148 if (use_256_pbls)
2149 nesadapter->free_256pbl += pbl_count + use_two_level;
2150 else if (use_4k_pbls)
2151 nesadapter->free_4kpbl += pbl_count + use_two_level;
Don Wooddae5d132009-03-06 15:12:09 -08002152 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2153 }
Don Wood0145f342009-03-06 15:15:00 -08002154 if (new_root.pbl_pbase)
2155 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2156 new_root.pbl_pbase);
2157
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002158 if (!ret)
2159 return -ETIME;
2160 else if (major_code)
2161 return -EIO;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002162
Don Wood0145f342009-03-06 15:15:00 -08002163 *actual_pbl_cnt = pbl_count + use_two_level;
2164 *used_4k_pbls = use_4k_pbls;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002165 return 0;
2166}
2167
2168
2169/**
2170 * nes_reg_phys_mr
2171 */
2172static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2173 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2174 u64 * iova_start)
2175{
2176 u64 region_length;
2177 struct nes_pd *nespd = to_nespd(ib_pd);
2178 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2179 struct nes_device *nesdev = nesvnic->nesdev;
2180 struct nes_adapter *nesadapter = nesdev->nesadapter;
2181 struct nes_mr *nesmr;
2182 struct ib_mr *ibmr;
2183 struct nes_vpbl vpbl;
2184 struct nes_root_vpbl root_vpbl;
2185 u32 stag;
2186 u32 i;
Don Wood3f32eb12009-04-20 14:53:00 -07002187 unsigned long mask;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002188 u32 stag_index = 0;
2189 u32 next_stag_index = 0;
2190 u32 driver_key = 0;
2191 u32 root_pbl_index = 0;
2192 u32 cur_pbl_index = 0;
2193 int err = 0, pbl_depth = 0;
2194 int ret = 0;
2195 u16 pbl_count = 0;
2196 u8 single_page = 1;
2197 u8 stag_key = 0;
2198
2199 pbl_depth = 0;
2200 region_length = 0;
2201 vpbl.pbl_vbase = NULL;
2202 root_vpbl.pbl_vbase = NULL;
2203 root_vpbl.pbl_pbase = 0;
2204
2205 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2206 stag_key = (u8)next_stag_index;
2207
2208 driver_key = 0;
2209
2210 next_stag_index >>= 8;
2211 next_stag_index %= nesadapter->max_mr;
2212 if (num_phys_buf > (1024*512)) {
2213 return ERR_PTR(-E2BIG);
2214 }
2215
Don Wood3f32eb12009-04-20 14:53:00 -07002216 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2217 return ERR_PTR(-EINVAL);
2218
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002219 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2220 &stag_index, &next_stag_index);
2221 if (err) {
2222 return ERR_PTR(err);
2223 }
2224
2225 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2226 if (!nesmr) {
2227 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2228 return ERR_PTR(-ENOMEM);
2229 }
2230
2231 for (i = 0; i < num_phys_buf; i++) {
2232
2233 if ((i & 0x01FF) == 0) {
2234 if (root_pbl_index == 1) {
2235 /* Allocate the root PBL */
2236 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2237 &root_vpbl.pbl_pbase);
2238 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2239 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2240 if (!root_vpbl.pbl_vbase) {
2241 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2242 vpbl.pbl_pbase);
2243 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2244 kfree(nesmr);
2245 return ERR_PTR(-ENOMEM);
2246 }
2247 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2248 if (!root_vpbl.leaf_vpbl) {
2249 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2250 root_vpbl.pbl_pbase);
2251 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2252 vpbl.pbl_pbase);
2253 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2254 kfree(nesmr);
2255 return ERR_PTR(-ENOMEM);
2256 }
2257 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2258 root_vpbl.pbl_vbase[0].pa_high =
2259 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2260 root_vpbl.leaf_vpbl[0] = vpbl;
2261 }
2262 /* Allocate a 4K buffer for the PBL */
2263 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2264 &vpbl.pbl_pbase);
2265 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2266 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2267 if (!vpbl.pbl_vbase) {
2268 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2269 ibmr = ERR_PTR(-ENOMEM);
2270 kfree(nesmr);
2271 goto reg_phys_err;
2272 }
2273 /* Fill in the root table */
2274 if (1 <= root_pbl_index) {
2275 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2276 cpu_to_le32((u32)vpbl.pbl_pbase);
2277 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2278 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2279 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2280 }
2281 root_pbl_index++;
2282 cur_pbl_index = 0;
2283 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002284
Don Wood3f32eb12009-04-20 14:53:00 -07002285 mask = !buffer_list[i].size;
2286 if (i != 0)
2287 mask |= buffer_list[i].addr;
2288 if (i != num_phys_buf - 1)
2289 mask |= buffer_list[i].addr + buffer_list[i].size;
2290
2291 if (mask & ~PAGE_MASK) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002292 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
Don Wood3f32eb12009-04-20 14:53:00 -07002293 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002294 ibmr = ERR_PTR(-EINVAL);
2295 kfree(nesmr);
2296 goto reg_phys_err;
2297 }
2298
2299 region_length += buffer_list[i].size;
2300 if ((i != 0) && (single_page)) {
2301 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2302 single_page = 0;
2303 }
Don Wood3f32eb12009-04-20 14:53:00 -07002304 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 -08002305 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2306 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2307 }
2308
2309 stag = stag_index << 8;
2310 stag |= driver_key;
2311 stag += (u32)stag_key;
2312
2313 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2314 " length = 0x%016lX, index = 0x%08X\n",
2315 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2316
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002317 /* Make the leaf PBL the root if only one PBL */
2318 if (root_pbl_index == 1) {
2319 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2320 }
2321
2322 if (single_page) {
2323 pbl_count = 0;
2324 } else {
2325 pbl_count = root_pbl_index;
2326 }
2327 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
Don Wood0145f342009-03-06 15:15:00 -08002328 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2329 &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002330
2331 if (ret == 0) {
2332 nesmr->ibmr.rkey = stag;
2333 nesmr->ibmr.lkey = stag;
2334 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2335 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002336 } else {
2337 kfree(nesmr);
2338 ibmr = ERR_PTR(-ENOMEM);
2339 }
2340
2341 reg_phys_err:
2342 /* free the resources */
2343 if (root_pbl_index == 1) {
2344 /* single PBL case */
2345 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2346 } else {
2347 for (i=0; i<root_pbl_index; i++) {
2348 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2349 root_vpbl.leaf_vpbl[i].pbl_pbase);
2350 }
2351 kfree(root_vpbl.leaf_vpbl);
2352 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2353 root_vpbl.pbl_pbase);
2354 }
2355
2356 return ibmr;
2357}
2358
2359
2360/**
2361 * nes_get_dma_mr
2362 */
2363static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2364{
2365 struct ib_phys_buf bl;
2366 u64 kva = 0;
2367
2368 nes_debug(NES_DBG_MR, "\n");
2369
2370 bl.size = (u64)0xffffffffffULL;
2371 bl.addr = 0;
2372 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2373}
2374
2375
2376/**
2377 * nes_reg_user_mr
2378 */
2379static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2380 u64 virt, int acc, struct ib_udata *udata)
2381{
2382 u64 iova_start;
2383 __le64 *pbl;
2384 u64 region_length;
2385 dma_addr_t last_dma_addr = 0;
2386 dma_addr_t first_dma_addr = 0;
2387 struct nes_pd *nespd = to_nespd(pd);
2388 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2389 struct nes_device *nesdev = nesvnic->nesdev;
2390 struct nes_adapter *nesadapter = nesdev->nesadapter;
2391 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2392 struct ib_umem_chunk *chunk;
2393 struct nes_ucontext *nes_ucontext;
2394 struct nes_pbl *nespbl;
2395 struct nes_mr *nesmr;
2396 struct ib_umem *region;
2397 struct nes_mem_reg_req req;
2398 struct nes_vpbl vpbl;
2399 struct nes_root_vpbl root_vpbl;
2400 int nmap_index, page_index;
2401 int page_count = 0;
2402 int err, pbl_depth = 0;
2403 int chunk_pages;
2404 int ret;
2405 u32 stag;
2406 u32 stag_index = 0;
2407 u32 next_stag_index;
2408 u32 driver_key;
2409 u32 root_pbl_index = 0;
2410 u32 cur_pbl_index = 0;
2411 u32 skip_pages;
2412 u16 pbl_count;
2413 u8 single_page = 1;
2414 u8 stag_key;
2415
Arthur Kepnercb9fbc52008-04-29 01:00:34 -07002416 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002417 if (IS_ERR(region)) {
2418 return (struct ib_mr *)region;
2419 }
2420
2421 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2422 " offset = %u, page size = %u.\n",
2423 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2424 region->offset, region->page_size);
2425
2426 skip_pages = ((u32)region->offset) >> 12;
2427
2428 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2429 return ERR_PTR(-EFAULT);
2430 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2431
2432 switch (req.reg_type) {
2433 case IWNES_MEMREG_TYPE_MEM:
2434 pbl_depth = 0;
2435 region_length = 0;
2436 vpbl.pbl_vbase = NULL;
2437 root_vpbl.pbl_vbase = NULL;
2438 root_vpbl.pbl_pbase = 0;
2439
2440 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2441 stag_key = (u8)next_stag_index;
2442
2443 driver_key = next_stag_index & 0x70000000;
2444
2445 next_stag_index >>= 8;
2446 next_stag_index %= nesadapter->max_mr;
2447
2448 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2449 nesadapter->max_mr, &stag_index, &next_stag_index);
2450 if (err) {
2451 ib_umem_release(region);
2452 return ERR_PTR(err);
2453 }
2454
2455 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2456 if (!nesmr) {
2457 ib_umem_release(region);
2458 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2459 return ERR_PTR(-ENOMEM);
2460 }
2461 nesmr->region = region;
2462
2463 list_for_each_entry(chunk, &region->chunk_list, list) {
2464 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2465 chunk->nents, chunk->nmap);
2466 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2467 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2468 ib_umem_release(region);
2469 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2470 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2471 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2472 ibmr = ERR_PTR(-EINVAL);
2473 kfree(nesmr);
2474 goto reg_user_mr_err;
2475 }
2476
2477 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2478 ib_umem_release(region);
2479 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2480 stag_index);
2481 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2482 ibmr = ERR_PTR(-EINVAL);
2483 kfree(nesmr);
2484 goto reg_user_mr_err;
2485 }
2486
2487 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2488 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2489 region_length -= skip_pages << 12;
2490 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2491 skip_pages = 0;
2492 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2493 goto enough_pages;
2494 if ((page_count&0x01FF) == 0) {
Roland Dreier24797a32008-06-10 12:29:49 -07002495 if (page_count >= 1024 * 512) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002496 ib_umem_release(region);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002497 nes_free_resource(nesadapter,
2498 nesadapter->allocated_mrs, stag_index);
2499 kfree(nesmr);
2500 ibmr = ERR_PTR(-E2BIG);
2501 goto reg_user_mr_err;
2502 }
2503 if (root_pbl_index == 1) {
2504 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2505 8192, &root_vpbl.pbl_pbase);
2506 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2507 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2508 if (!root_vpbl.pbl_vbase) {
2509 ib_umem_release(region);
2510 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2511 vpbl.pbl_pbase);
2512 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2513 stag_index);
2514 kfree(nesmr);
2515 ibmr = ERR_PTR(-ENOMEM);
2516 goto reg_user_mr_err;
2517 }
2518 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2519 GFP_KERNEL);
2520 if (!root_vpbl.leaf_vpbl) {
2521 ib_umem_release(region);
2522 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2523 root_vpbl.pbl_pbase);
2524 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2525 vpbl.pbl_pbase);
2526 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2527 stag_index);
2528 kfree(nesmr);
2529 ibmr = ERR_PTR(-ENOMEM);
2530 goto reg_user_mr_err;
2531 }
2532 root_vpbl.pbl_vbase[0].pa_low =
2533 cpu_to_le32((u32)vpbl.pbl_pbase);
2534 root_vpbl.pbl_vbase[0].pa_high =
2535 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2536 root_vpbl.leaf_vpbl[0] = vpbl;
2537 }
2538 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2539 &vpbl.pbl_pbase);
2540 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2541 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2542 if (!vpbl.pbl_vbase) {
2543 ib_umem_release(region);
2544 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2545 ibmr = ERR_PTR(-ENOMEM);
2546 kfree(nesmr);
2547 goto reg_user_mr_err;
2548 }
2549 if (1 <= root_pbl_index) {
2550 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2551 cpu_to_le32((u32)vpbl.pbl_pbase);
2552 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2553 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2554 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2555 }
2556 root_pbl_index++;
2557 cur_pbl_index = 0;
2558 }
2559 if (single_page) {
2560 if (page_count != 0) {
2561 if ((last_dma_addr+4096) !=
2562 (sg_dma_address(&chunk->page_list[nmap_index])+
2563 (page_index*4096)))
2564 single_page = 0;
2565 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2566 (page_index*4096);
2567 } else {
2568 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2569 (page_index*4096);
2570 last_dma_addr = first_dma_addr;
2571 }
2572 }
2573
2574 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2575 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2576 (page_index*4096)));
2577 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2578 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2579 (page_index*4096))) >> 32)));
2580 cur_pbl_index++;
2581 page_count++;
2582 }
2583 }
2584 }
2585 enough_pages:
2586 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2587 " stag_key=0x%08x\n",
2588 stag_index, driver_key, stag_key);
2589 stag = stag_index << 8;
2590 stag |= driver_key;
2591 stag += (u32)stag_key;
2592 if (stag == 0) {
2593 stag = 1;
2594 }
2595
2596 iova_start = virt;
2597 /* Make the leaf PBL the root if only one PBL */
2598 if (root_pbl_index == 1) {
2599 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2600 }
2601
2602 if (single_page) {
2603 pbl_count = 0;
2604 } else {
2605 pbl_count = root_pbl_index;
2606 first_dma_addr = 0;
2607 }
2608 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2609 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2610 stag, (unsigned int)iova_start,
2611 (unsigned int)region_length, stag_index,
2612 (unsigned long long)region->length, pbl_count);
Don Wood0145f342009-03-06 15:15:00 -08002613 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2614 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2615 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002616
2617 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2618
2619 if (ret == 0) {
2620 nesmr->ibmr.rkey = stag;
2621 nesmr->ibmr.lkey = stag;
2622 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2623 ibmr = &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002624 } else {
2625 ib_umem_release(region);
2626 kfree(nesmr);
2627 ibmr = ERR_PTR(-ENOMEM);
2628 }
2629
2630 reg_user_mr_err:
2631 /* free the resources */
2632 if (root_pbl_index == 1) {
2633 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2634 vpbl.pbl_pbase);
2635 } else {
2636 for (page_index=0; page_index<root_pbl_index; page_index++) {
2637 pci_free_consistent(nesdev->pcidev, 4096,
2638 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2639 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2640 }
2641 kfree(root_vpbl.leaf_vpbl);
2642 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2643 root_vpbl.pbl_pbase);
2644 }
2645
2646 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2647
2648 return ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002649 case IWNES_MEMREG_TYPE_QP:
2650 case IWNES_MEMREG_TYPE_CQ:
2651 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2652 if (!nespbl) {
2653 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2654 ib_umem_release(region);
2655 return ERR_PTR(-ENOMEM);
2656 }
2657 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2658 if (!nesmr) {
2659 ib_umem_release(region);
2660 kfree(nespbl);
2661 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2662 return ERR_PTR(-ENOMEM);
2663 }
2664 nesmr->region = region;
2665 nes_ucontext = to_nesucontext(pd->uobject->context);
2666 pbl_depth = region->length >> 12;
2667 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2668 nespbl->pbl_size = pbl_depth*sizeof(u64);
2669 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2670 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2671 } else {
2672 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2673 }
2674
2675 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2676 nespbl->pbl_size, pbl_depth);
2677 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2678 &nespbl->pbl_pbase);
2679 if (!pbl) {
2680 ib_umem_release(region);
2681 kfree(nesmr);
2682 kfree(nespbl);
2683 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2684 return ERR_PTR(-ENOMEM);
2685 }
2686
2687 nespbl->pbl_vbase = (u64 *)pbl;
2688 nespbl->user_base = start;
Roland Dreier71e09572008-04-16 21:01:09 -07002689 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002690 " pbl_vbase=%p user_base=0x%lx\n",
Roland Dreier71e09572008-04-16 21:01:09 -07002691 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2692 (void *) nespbl->pbl_vbase, nespbl->user_base);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002693
2694 list_for_each_entry(chunk, &region->chunk_list, list) {
2695 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2696 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2697 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2698 nespbl->page = sg_page(&chunk->page_list[0]);
2699 for (page_index=0; page_index<chunk_pages; page_index++) {
2700 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2701 (sg_dma_address(&chunk->page_list[nmap_index])+
2702 (page_index*4096)));
2703 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2704 (sg_dma_address(&chunk->page_list[nmap_index])+
2705 (page_index*4096)))>>32);
2706 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2707 (unsigned long long)*pbl,
2708 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2709 pbl++;
2710 }
2711 }
2712 }
2713 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2714 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2715 } else {
2716 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2717 }
2718 nesmr->ibmr.rkey = -1;
2719 nesmr->ibmr.lkey = -1;
2720 nesmr->mode = req.reg_type;
2721 return &nesmr->ibmr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002722 }
2723
2724 return ERR_PTR(-ENOSYS);
2725}
2726
2727
2728/**
2729 * nes_dereg_mr
2730 */
2731static int nes_dereg_mr(struct ib_mr *ib_mr)
2732{
2733 struct nes_mr *nesmr = to_nesmr(ib_mr);
2734 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2735 struct nes_device *nesdev = nesvnic->nesdev;
2736 struct nes_adapter *nesadapter = nesdev->nesadapter;
2737 struct nes_hw_cqp_wqe *cqp_wqe;
2738 struct nes_cqp_request *cqp_request;
2739 unsigned long flags;
2740 int ret;
2741 u16 major_code;
2742 u16 minor_code;
2743
2744 if (nesmr->region) {
2745 ib_umem_release(nesmr->region);
2746 }
2747 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2748 kfree(nesmr);
2749 return 0;
2750 }
2751
2752 /* Deallocate the region with the adapter */
2753
2754 cqp_request = nes_get_cqp_request(nesdev);
2755 if (cqp_request == NULL) {
2756 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2757 return -ENOMEM;
2758 }
2759 cqp_request->waiting = 1;
2760 cqp_wqe = &cqp_request->cqp_wqe;
2761
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002762 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2763 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2764 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2765 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2766 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2767
2768 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002769 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002770
2771 /* Wait for CQP */
2772 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2773 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2774 NES_EVENT_TIMEOUT);
2775 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2776 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2777 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2778
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002779 major_code = cqp_request->major_code;
2780 minor_code = cqp_request->minor_code;
Roland Dreier1ff66e82008-07-14 23:48:49 -07002781
2782 nes_put_cqp_request(nesdev, cqp_request);
2783
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002784 if (!ret) {
2785 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2786 " ib_mr=%p, rkey = 0x%08X\n",
2787 ib_mr, ib_mr->rkey);
2788 return -ETIME;
2789 } else if (major_code) {
2790 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2791 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2792 major_code, minor_code, ib_mr, ib_mr->rkey);
2793 return -EIO;
Don Wooddae5d132009-03-06 15:12:09 -08002794 }
2795
2796 if (nesmr->pbls_used != 0) {
2797 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2798 if (nesmr->pbl_4k) {
2799 nesadapter->free_4kpbl += nesmr->pbls_used;
2800 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2801 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2802 "exceeded the max(%u)\n",
2803 nesadapter->free_4kpbl,
2804 nesadapter->max_4kpbl);
2805 } else {
2806 nesadapter->free_256pbl += nesmr->pbls_used;
2807 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2808 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2809 "exceeded the max(%u)\n",
2810 nesadapter->free_256pbl,
2811 nesadapter->max_256pbl);
2812 }
2813 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2814 }
2815 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2816 (ib_mr->rkey & 0x0fffff00) >> 8);
2817
2818 kfree(nesmr);
2819
2820 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002821}
2822
2823
2824/**
2825 * show_rev
2826 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002827static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2828 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002829{
2830 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002831 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002832 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2833
2834 nes_debug(NES_DBG_INIT, "\n");
2835 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2836}
2837
2838
2839/**
2840 * show_fw_ver
2841 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002842static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2843 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002844{
2845 struct nes_ib_device *nesibdev =
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002846 container_of(dev, struct nes_ib_device, ibdev.dev);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002847 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2848
2849 nes_debug(NES_DBG_INIT, "\n");
Chien Tung0e4562d2009-04-27 13:33:48 -07002850 return sprintf(buf, "%u.%u\n",
2851 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2852 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002853}
2854
2855
2856/**
2857 * show_hca
2858 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002859static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2860 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002861{
2862 nes_debug(NES_DBG_INIT, "\n");
2863 return sprintf(buf, "NES020\n");
2864}
2865
2866
2867/**
2868 * show_board
2869 */
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002870static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2871 char *buf)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002872{
2873 nes_debug(NES_DBG_INIT, "\n");
2874 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2875}
2876
2877
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002878static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2879static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2880static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2881static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002882
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002883static struct device_attribute *nes_dev_attributes[] = {
2884 &dev_attr_hw_rev,
2885 &dev_attr_fw_ver,
2886 &dev_attr_hca_type,
2887 &dev_attr_board_id
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002888};
2889
2890
2891/**
2892 * nes_query_qp
2893 */
2894static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2895 int attr_mask, struct ib_qp_init_attr *init_attr)
2896{
2897 struct nes_qp *nesqp = to_nesqp(ibqp);
2898
2899 nes_debug(NES_DBG_QP, "\n");
2900
2901 attr->qp_access_flags = 0;
2902 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2903 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2904 attr->cap.max_recv_sge = 1;
2905 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2906 init_attr->cap.max_inline_data = 0;
2907 } else {
2908 init_attr->cap.max_inline_data = 64;
2909 }
2910
2911 init_attr->event_handler = nesqp->ibqp.event_handler;
2912 init_attr->qp_context = nesqp->ibqp.qp_context;
2913 init_attr->send_cq = nesqp->ibqp.send_cq;
2914 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2915 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2916 init_attr->cap = attr->cap;
2917
2918 return 0;
2919}
2920
2921
2922/**
2923 * nes_hw_modify_qp
2924 */
2925int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
Don Wood8b1c9dc2009-09-05 20:36:38 -07002926 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927{
2928 struct nes_hw_cqp_wqe *cqp_wqe;
2929 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2930 /* struct iw_cm_event cm_event; */
2931 struct nes_cqp_request *cqp_request;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002932 int ret;
2933 u16 major_code;
2934
2935 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2936 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2937
2938 cqp_request = nes_get_cqp_request(nesdev);
2939 if (cqp_request == NULL) {
2940 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2941 return -ENOMEM;
2942 }
2943 if (wait_completion) {
2944 cqp_request->waiting = 1;
2945 } else {
2946 cqp_request->waiting = 0;
2947 }
2948 cqp_wqe = &cqp_request->cqp_wqe;
2949
2950 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2951 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2952 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2953 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2954 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2955 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2956 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2957
Don Wood8b1c9dc2009-09-05 20:36:38 -07002958 /* If sending a terminate message, fill in the length (in words) */
2959 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2960 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2961 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2962 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2963 }
2964
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002965 atomic_set(&cqp_request->refcount, 2);
Roland Dreier8294f292008-07-14 23:48:49 -07002966 nes_post_cqp_request(nesdev, cqp_request);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002967
2968 /* Wait for CQP */
2969 if (wait_completion) {
2970 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2971 nesqp->hwqp.qp_id); */
2972 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2973 NES_EVENT_TIMEOUT);
2974 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2975 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2976 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2977 major_code = cqp_request->major_code;
2978 if (major_code) {
2979 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2980 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2981 nesqp->hwqp.qp_id, cqp_request->major_code,
2982 cqp_request->minor_code, next_iwarp_state);
2983 }
Roland Dreier1ff66e82008-07-14 23:48:49 -07002984
2985 nes_put_cqp_request(nesdev, cqp_request);
2986
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002987 if (!ret)
2988 return -ETIME;
2989 else if (major_code)
2990 return -EIO;
2991 else
2992 return 0;
2993 } else {
2994 return 0;
2995 }
2996}
2997
2998
2999/**
3000 * nes_modify_qp
3001 */
3002int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3003 int attr_mask, struct ib_udata *udata)
3004{
3005 struct nes_qp *nesqp = to_nesqp(ibqp);
3006 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3007 struct nes_device *nesdev = nesvnic->nesdev;
3008 /* u32 cqp_head; */
3009 /* u32 counter; */
3010 u32 next_iwarp_state = 0;
3011 int err;
3012 unsigned long qplockflags;
3013 int ret;
3014 u16 original_last_aeq;
3015 u8 issue_modify_qp = 0;
3016 u8 issue_disconnect = 0;
3017 u8 dont_wait = 0;
3018
3019 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
3020 " iwarp_state=0x%X, refcount=%d\n",
3021 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
3022 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
3023
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003024 spin_lock_irqsave(&nesqp->lock, qplockflags);
3025
3026 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
3027 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
3028 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
3029 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
3030
3031 if (attr_mask & IB_QP_STATE) {
3032 switch (attr->qp_state) {
3033 case IB_QPS_INIT:
3034 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
3035 nesqp->hwqp.qp_id);
3036 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3037 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003038 return -EINVAL;
3039 }
3040 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3041 issue_modify_qp = 1;
3042 break;
3043 case IB_QPS_RTR:
3044 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
3045 nesqp->hwqp.qp_id);
3046 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3047 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003048 return -EINVAL;
3049 }
3050 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3051 issue_modify_qp = 1;
3052 break;
3053 case IB_QPS_RTS:
3054 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
3055 nesqp->hwqp.qp_id);
3056 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
3057 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003058 return -EINVAL;
3059 }
3060 if (nesqp->cm_id == NULL) {
3061 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
3062 nesqp->hwqp.qp_id );
3063 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003064 return -EINVAL;
3065 }
3066 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
3067 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
3068 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
3069 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
3070 issue_modify_qp = 1;
3071 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
3072 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3073 nesqp->hte_added = 1;
3074 break;
3075 case IB_QPS_SQD:
3076 issue_modify_qp = 1;
3077 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3078 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3079 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3080 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003081 return 0;
3082 } else {
3083 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3084 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3085 " ignored due to current iWARP state\n",
3086 nesqp->hwqp.qp_id);
3087 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088 return -EINVAL;
3089 }
3090 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3091 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3092 " already done based on hw state.\n",
3093 nesqp->hwqp.qp_id);
3094 issue_modify_qp = 0;
3095 nesqp->in_disconnect = 0;
3096 }
3097 switch (nesqp->hw_iwarp_state) {
3098 case NES_AEQE_IWARP_STATE_CLOSING:
3099 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3100 case NES_AEQE_IWARP_STATE_TERMINATE:
3101 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3102 break;
3103 case NES_AEQE_IWARP_STATE_ERROR:
3104 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3105 break;
3106 default:
3107 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3108 nesqp->in_disconnect = 1;
3109 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3110 break;
3111 }
3112 }
3113 break;
3114 case IB_QPS_SQE:
3115 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3116 nesqp->hwqp.qp_id);
3117 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3118 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003119 return -EINVAL;
3120 }
3121 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3122 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3123 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3124 issue_modify_qp = 1;
3125 nesqp->in_disconnect = 1;
3126 break;
3127 case IB_QPS_ERR:
3128 case IB_QPS_RESET:
3129 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3130 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003131 return -EINVAL;
3132 }
3133 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3134 nesqp->hwqp.qp_id);
Don Wood8b1c9dc2009-09-05 20:36:38 -07003135 if (nesqp->term_flags)
3136 del_timer(&nesqp->terminate_timer);
3137
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003138 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3139 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3140 if (nesqp->hte_added) {
3141 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3142 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3143 nesqp->hte_added = 0;
3144 }
3145 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3146 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3147 next_iwarp_state |= NES_CQP_QP_RESET;
3148 nesqp->in_disconnect = 1;
3149 } else {
3150 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3151 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3152 dont_wait = 1;
3153 }
3154 issue_modify_qp = 1;
3155 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3156 break;
3157 default:
3158 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003159 return -EINVAL;
3160 break;
3161 }
3162
3163 nesqp->ibqp_state = attr->qp_state;
3164 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3165 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3166 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3167 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3168 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3169 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3170 nesqp->iwarp_state);
3171 issue_disconnect = 1;
3172 } else {
3173 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3174 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3175 nesqp->iwarp_state);
3176 }
3177 }
3178
3179 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3180 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3181 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3182 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3183 issue_modify_qp = 1;
3184 }
3185 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3186 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3187 issue_modify_qp = 1;
3188 }
3189 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3190 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3191 issue_modify_qp = 1;
3192 }
3193 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3194 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3195 issue_modify_qp = 1;
3196 }
3197
3198 if (nesqp->user_mode) {
3199 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3200 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3201 issue_modify_qp = 1;
3202 }
3203 }
3204
3205 original_last_aeq = nesqp->last_aeq;
3206 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3207
3208 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3209
3210 ret = 0;
3211
3212
3213 if (issue_modify_qp) {
3214 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
Don Wood8b1c9dc2009-09-05 20:36:38 -07003215 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003216 if (ret)
3217 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3218 " failed for QP%u.\n",
3219 next_iwarp_state, nesqp->hwqp.qp_id);
3220
3221 }
3222
3223 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3224 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3225 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3226 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3227 original_last_aeq, nesqp->last_aeq);
3228 if ((!ret) ||
3229 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3230 (ret))) {
3231 if (dont_wait) {
3232 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3233 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3234 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3235 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3236 original_last_aeq, nesqp->last_aeq);
3237 /* this one is for the cm_disconnect thread */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003238 spin_lock_irqsave(&nesqp->lock, qplockflags);
3239 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3240 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3241 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3242 nes_cm_disconn(nesqp);
3243 } else {
3244 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3245 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003246 }
3247 } else {
3248 spin_lock_irqsave(&nesqp->lock, qplockflags);
3249 if (nesqp->cm_id) {
3250 /* These two are for the timer thread */
3251 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003252 nesqp->cm_id->add_ref(nesqp->cm_id);
3253 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3254 " need ae to finish up, original_last_aeq = 0x%04X."
3255 " last_aeq = 0x%04X, scheduling timer.\n",
3256 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3257 original_last_aeq, nesqp->last_aeq);
3258 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3259 }
3260 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3261 } else {
3262 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3263 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3264 " need ae to finish up, original_last_aeq = 0x%04X."
3265 " last_aeq = 0x%04X.\n",
3266 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3267 original_last_aeq, nesqp->last_aeq);
3268 }
3269 }
3270 } else {
3271 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3272 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3273 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3274 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003275 }
3276 } else {
3277 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3278 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3279 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3280 original_last_aeq, nesqp->last_aeq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003281 }
3282
3283 err = 0;
3284
3285 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3286 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3287
3288 return err;
3289}
3290
3291
3292/**
3293 * nes_muticast_attach
3294 */
3295static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3296{
3297 nes_debug(NES_DBG_INIT, "\n");
3298 return -ENOSYS;
3299}
3300
3301
3302/**
3303 * nes_multicast_detach
3304 */
3305static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3306{
3307 nes_debug(NES_DBG_INIT, "\n");
3308 return -ENOSYS;
3309}
3310
3311
3312/**
3313 * nes_process_mad
3314 */
3315static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3316 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3317 struct ib_mad *in_mad, struct ib_mad *out_mad)
3318{
3319 nes_debug(NES_DBG_INIT, "\n");
3320 return -ENOSYS;
3321}
3322
3323static inline void
3324fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3325{
3326 int sge_index;
3327 int total_payload_length = 0;
3328 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3329 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3330 ib_wr->sg_list[sge_index].addr);
3331 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3332 ib_wr->sg_list[sge_index].length);
3333 if (uselkey)
3334 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3335 (ib_wr->sg_list[sge_index].lkey));
3336 else
3337 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3338
3339 total_payload_length += ib_wr->sg_list[sge_index].length;
3340 }
3341 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3342 total_payload_length);
3343 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3344 total_payload_length);
3345}
3346
3347/**
3348 * nes_post_send
3349 */
3350static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3351 struct ib_send_wr **bad_wr)
3352{
3353 u64 u64temp;
3354 unsigned long flags = 0;
3355 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3356 struct nes_device *nesdev = nesvnic->nesdev;
3357 struct nes_qp *nesqp = to_nesqp(ibqp);
3358 struct nes_hw_qp_wqe *wqe;
3359 int err;
3360 u32 qsize = nesqp->hwqp.sq_size;
3361 u32 head;
3362 u32 wqe_misc;
3363 u32 wqe_count;
3364 u32 counter;
3365 u32 total_payload_length;
3366
3367 err = 0;
3368 wqe_misc = 0;
3369 wqe_count = 0;
3370 total_payload_length = 0;
3371
3372 if (nesqp->ibqp_state > IB_QPS_RTS)
3373 return -EINVAL;
3374
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003375 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003376
3377 head = nesqp->hwqp.sq_head;
3378
3379 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003380 /* Check for QP error */
3381 if (nesqp->term_flags) {
3382 err = -EINVAL;
3383 break;
3384 }
3385
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003386 /* Check for SQ overflow */
3387 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3388 err = -EINVAL;
3389 break;
3390 }
3391
3392 wqe = &nesqp->hwqp.sq_vbase[head];
3393 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3394 nesqp->hwqp.qp_id, wqe, head); */
3395 nes_fill_init_qp_wqe(wqe, nesqp, head);
3396 u64temp = (u64)(ib_wr->wr_id);
3397 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3398 u64temp);
3399 switch (ib_wr->opcode) {
3400 case IB_WR_SEND:
3401 if (ib_wr->send_flags & IB_SEND_SOLICITED) {
3402 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3403 } else {
3404 wqe_misc = NES_IWARP_SQ_OP_SEND;
3405 }
3406 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3407 err = -EINVAL;
3408 break;
3409 }
3410 if (ib_wr->send_flags & IB_SEND_FENCE) {
3411 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3412 }
3413 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3414 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3415 (ib_wr->sg_list[0].length <= 64)) {
3416 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3417 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3418 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3419 ib_wr->sg_list[0].length);
3420 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3421 } else {
3422 fill_wqe_sg_send(wqe, ib_wr, 1);
3423 }
3424
3425 break;
3426 case IB_WR_RDMA_WRITE:
3427 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3428 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3429 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3430 ib_wr->num_sge,
3431 nesdev->nesadapter->max_sge);
3432 err = -EINVAL;
3433 break;
3434 }
3435 if (ib_wr->send_flags & IB_SEND_FENCE) {
3436 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3437 }
3438
3439 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3440 ib_wr->wr.rdma.rkey);
3441 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3442 ib_wr->wr.rdma.remote_addr);
3443
3444 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3445 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3446 (ib_wr->sg_list[0].length <= 64)) {
3447 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3448 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3449 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3450 ib_wr->sg_list[0].length);
3451 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3452 } else {
3453 fill_wqe_sg_send(wqe, ib_wr, 1);
3454 }
3455 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3456 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3457 break;
3458 case IB_WR_RDMA_READ:
3459 /* iWARP only supports 1 sge for RDMA reads */
3460 if (ib_wr->num_sge > 1) {
3461 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3462 ib_wr->num_sge);
3463 err = -EINVAL;
3464 break;
3465 }
3466 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3467 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3468 ib_wr->wr.rdma.remote_addr);
3469 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3470 ib_wr->wr.rdma.rkey);
3471 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3472 ib_wr->sg_list->length);
3473 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3474 ib_wr->sg_list->addr);
3475 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3476 ib_wr->sg_list->lkey);
3477 break;
3478 default:
3479 /* error */
3480 err = -EINVAL;
3481 break;
3482 }
3483
3484 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3485 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3486 }
3487 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3488
3489 ib_wr = ib_wr->next;
3490 head++;
3491 wqe_count++;
3492 if (head >= qsize)
3493 head = 0;
3494
3495 }
3496
3497 nesqp->hwqp.sq_head = head;
3498 barrier();
3499 while (wqe_count) {
3500 counter = min(wqe_count, ((u32)255));
3501 wqe_count -= counter;
3502 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3503 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3504 }
3505
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003506 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003507
3508 if (err)
3509 *bad_wr = ib_wr;
3510 return err;
3511}
3512
3513
3514/**
3515 * nes_post_recv
3516 */
3517static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3518 struct ib_recv_wr **bad_wr)
3519{
3520 u64 u64temp;
3521 unsigned long flags = 0;
3522 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3523 struct nes_device *nesdev = nesvnic->nesdev;
3524 struct nes_qp *nesqp = to_nesqp(ibqp);
3525 struct nes_hw_qp_wqe *wqe;
3526 int err = 0;
3527 int sge_index;
3528 u32 qsize = nesqp->hwqp.rq_size;
3529 u32 head;
3530 u32 wqe_count = 0;
3531 u32 counter;
3532 u32 total_payload_length;
3533
3534 if (nesqp->ibqp_state > IB_QPS_RTS)
3535 return -EINVAL;
3536
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003537 spin_lock_irqsave(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003538
3539 head = nesqp->hwqp.rq_head;
3540
3541 while (ib_wr) {
Don Wood8b1c9dc2009-09-05 20:36:38 -07003542 /* Check for QP error */
3543 if (nesqp->term_flags) {
3544 err = -EINVAL;
3545 break;
3546 }
3547
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3549 err = -EINVAL;
3550 break;
3551 }
3552 /* Check for RQ overflow */
3553 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3554 err = -EINVAL;
3555 break;
3556 }
3557
3558 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3559 wqe = &nesqp->hwqp.rq_vbase[head];
3560
3561 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3562 nesqp->hwqp.qp_id, wqe, head); */
3563 nes_fill_init_qp_wqe(wqe, nesqp, head);
3564 u64temp = (u64)(ib_wr->wr_id);
3565 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3566 u64temp);
3567 total_payload_length = 0;
3568 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3569 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3570 ib_wr->sg_list[sge_index].addr);
3571 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3572 ib_wr->sg_list[sge_index].length);
3573 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3574 ib_wr->sg_list[sge_index].lkey);
3575
3576 total_payload_length += ib_wr->sg_list[sge_index].length;
3577 }
3578 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3579 total_payload_length);
3580
3581 ib_wr = ib_wr->next;
3582 head++;
3583 wqe_count++;
3584 if (head >= qsize)
3585 head = 0;
3586 }
3587
3588 nesqp->hwqp.rq_head = head;
3589 barrier();
3590 while (wqe_count) {
3591 counter = min(wqe_count, ((u32)255));
3592 wqe_count -= counter;
3593 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3594 }
3595
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003596 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003597
3598 if (err)
3599 *bad_wr = ib_wr;
3600 return err;
3601}
3602
3603
3604/**
3605 * nes_poll_cq
3606 */
3607static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3608{
3609 u64 u64temp;
3610 u64 wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003611 unsigned long flags = 0;
3612 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3613 struct nes_device *nesdev = nesvnic->nesdev;
3614 struct nes_cq *nescq = to_nescq(ibcq);
3615 struct nes_qp *nesqp;
3616 struct nes_hw_cqe cqe;
3617 u32 head;
Don Wood6eed5e72009-09-05 20:36:39 -07003618 u32 wq_tail = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003619 u32 cq_size;
3620 u32 cqe_count = 0;
3621 u32 wqe_index;
3622 u32 u32temp;
Don Wood6eed5e72009-09-05 20:36:39 -07003623 u32 move_cq_head = 1;
3624 u32 err_code;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625
3626 nes_debug(NES_DBG_CQ, "\n");
3627
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003628 spin_lock_irqsave(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003629
3630 head = nescq->hw_cq.cq_head;
3631 cq_size = nescq->hw_cq.cq_size;
3632
3633 while (cqe_count < num_entries) {
Don Wood5ee21fe2009-09-05 20:36:37 -07003634 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3635 NES_CQE_VALID) == 0)
3636 break;
Roland Dreier31d1e342008-04-23 11:55:45 -07003637
Don Wood5ee21fe2009-09-05 20:36:37 -07003638 /*
3639 * Make sure we read CQ entry contents *after*
3640 * we've checked the valid bit.
3641 */
3642 rmb();
3643
3644 cqe = nescq->hw_cq.cq_vbase[head];
Don Wood5ee21fe2009-09-05 20:36:37 -07003645 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3646 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3647 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3648 /* parse CQE, get completion context from WQE (either rq or sq) */
3649 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3650 ((u64)u32temp);
3651
3652 if (u64temp) {
3653 nesqp = (struct nes_qp *)(unsigned long)u64temp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654 memset(entry, 0, sizeof *entry);
3655 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3656 entry->status = IB_WC_SUCCESS;
3657 } else {
3658 entry->status = IB_WC_WR_FLUSH_ERR;
3659 }
3660
3661 entry->qp = &nesqp->ibqp;
3662 entry->src_qp = nesqp->hwqp.qp_id;
3663
3664 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3665 if (nesqp->skip_lsmm) {
3666 nesqp->skip_lsmm = 0;
Don Wood5ee21fe2009-09-05 20:36:37 -07003667 nesqp->hwqp.sq_tail++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003668 }
3669
3670 /* Working on a SQ Completion*/
Don Wood6eed5e72009-09-05 20:36:39 -07003671 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003672 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
Don Wood6eed5e72009-09-05 20:36:39 -07003673 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003674 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
Don Wood6eed5e72009-09-05 20:36:39 -07003675 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003676 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3677
Don Wood6eed5e72009-09-05 20:36:39 -07003678 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003679 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3680 case NES_IWARP_SQ_OP_RDMAW:
3681 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3682 entry->opcode = IB_WC_RDMA_WRITE;
3683 break;
3684 case NES_IWARP_SQ_OP_RDMAR:
3685 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3686 entry->opcode = IB_WC_RDMA_READ;
Don Wood6eed5e72009-09-05 20:36:39 -07003687 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003688 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3689 break;
3690 case NES_IWARP_SQ_OP_SENDINV:
3691 case NES_IWARP_SQ_OP_SENDSEINV:
3692 case NES_IWARP_SQ_OP_SEND:
3693 case NES_IWARP_SQ_OP_SENDSE:
3694 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3695 entry->opcode = IB_WC_SEND;
3696 break;
3697 }
Don Wood6eed5e72009-09-05 20:36:39 -07003698
3699 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3700 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3701 move_cq_head = 0;
3702 wq_tail = nesqp->hwqp.sq_tail;
3703 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003704 } else {
3705 /* Working on a RQ Completion*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003706 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
Don Wood6eed5e72009-09-05 20:36:39 -07003707 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3708 ((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 -08003709 entry->opcode = IB_WC_RECV;
Don Wood6eed5e72009-09-05 20:36:39 -07003710
3711 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3712 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3713 move_cq_head = 0;
3714 wq_tail = nesqp->hwqp.rq_tail;
3715 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003716 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003717
Don Wood5ee21fe2009-09-05 20:36:37 -07003718 entry->wr_id = wrid;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003719 entry++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003720 cqe_count++;
3721 }
3722
Don Wood6eed5e72009-09-05 20:36:39 -07003723 if (move_cq_head) {
3724 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3725 if (++head >= cq_size)
3726 head = 0;
3727 nescq->polled_completions++;
Don Wood5ee21fe2009-09-05 20:36:37 -07003728
Don Wood6eed5e72009-09-05 20:36:39 -07003729 if ((nescq->polled_completions > (cq_size / 2)) ||
3730 (nescq->polled_completions == 255)) {
3731 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
Don Wood5ee21fe2009-09-05 20:36:37 -07003732 " are pending %u of %u.\n",
3733 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
Don Wood6eed5e72009-09-05 20:36:39 -07003734 nes_write32(nesdev->regs+NES_CQE_ALLOC,
Don Wood5ee21fe2009-09-05 20:36:37 -07003735 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
Don Wood6eed5e72009-09-05 20:36:39 -07003736 nescq->polled_completions = 0;
3737 }
3738 } else {
3739 /* Update the wqe index and set status to flush */
3740 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3741 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3742 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3743 cpu_to_le32(wqe_index);
3744 move_cq_head = 1; /* ready for next pass */
Don Wood5ee21fe2009-09-05 20:36:37 -07003745 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003746 }
3747
3748 if (nescq->polled_completions) {
3749 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3750 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3751 nescq->polled_completions = 0;
3752 }
3753
3754 nescq->hw_cq.cq_head = head;
3755 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3756 cqe_count, nescq->hw_cq.cq_number);
3757
Ilpo Järvinen5880ff12008-10-30 13:39:43 +02003758 spin_unlock_irqrestore(&nescq->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003759
3760 return cqe_count;
3761}
3762
3763
3764/**
3765 * nes_req_notify_cq
3766 */
3767static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3768 {
3769 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3770 struct nes_device *nesdev = nesvnic->nesdev;
3771 struct nes_cq *nescq = to_nescq(ibcq);
3772 u32 cq_arm;
3773
3774 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3775 nescq->hw_cq.cq_number);
3776
3777 cq_arm = nescq->hw_cq.cq_number;
3778 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3779 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3780 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3781 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3782 else
3783 return -EINVAL;
3784
3785 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3786 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3787
3788 return 0;
3789}
3790
3791
3792/**
3793 * nes_init_ofa_device
3794 */
3795struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3796{
3797 struct nes_ib_device *nesibdev;
3798 struct nes_vnic *nesvnic = netdev_priv(netdev);
3799 struct nes_device *nesdev = nesvnic->nesdev;
3800
3801 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3802 if (nesibdev == NULL) {
3803 return NULL;
3804 }
3805 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3806 nesibdev->ibdev.owner = THIS_MODULE;
3807
3808 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3809 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3810 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3811
3812 nesibdev->ibdev.uverbs_cmd_mask =
3813 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3814 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3815 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3816 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3817 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3818 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3819 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3820 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3821 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3822 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3823 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3824 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3825 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3826 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3827 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3828 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3829 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3830 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3831 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3832 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3833 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3834 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3835
3836 nesibdev->ibdev.phys_port_cnt = 1;
3837 nesibdev->ibdev.num_comp_vectors = 1;
3838 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003839 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003840 nesibdev->ibdev.query_device = nes_query_device;
3841 nesibdev->ibdev.query_port = nes_query_port;
3842 nesibdev->ibdev.modify_port = nes_modify_port;
3843 nesibdev->ibdev.query_pkey = nes_query_pkey;
3844 nesibdev->ibdev.query_gid = nes_query_gid;
3845 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3846 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3847 nesibdev->ibdev.mmap = nes_mmap;
3848 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3849 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3850 nesibdev->ibdev.create_ah = nes_create_ah;
3851 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3852 nesibdev->ibdev.create_qp = nes_create_qp;
3853 nesibdev->ibdev.modify_qp = nes_modify_qp;
3854 nesibdev->ibdev.query_qp = nes_query_qp;
3855 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3856 nesibdev->ibdev.create_cq = nes_create_cq;
3857 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3858 nesibdev->ibdev.poll_cq = nes_poll_cq;
3859 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3860 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3861 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3862 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3863 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3864 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3865 nesibdev->ibdev.bind_mw = nes_bind_mw;
3866
3867 nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3868 nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3869 nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3870 nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3871
3872 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3873 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3874 nesibdev->ibdev.process_mad = nes_process_mad;
3875
3876 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3877 nesibdev->ibdev.post_send = nes_post_send;
3878 nesibdev->ibdev.post_recv = nes_post_recv;
3879
3880 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3881 if (nesibdev->ibdev.iwcm == NULL) {
3882 ib_dealloc_device(&nesibdev->ibdev);
3883 return NULL;
3884 }
3885 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3886 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3887 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3888 nesibdev->ibdev.iwcm->connect = nes_connect;
3889 nesibdev->ibdev.iwcm->accept = nes_accept;
3890 nesibdev->ibdev.iwcm->reject = nes_reject;
3891 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3892 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3893
3894 return nesibdev;
3895}
3896
3897
3898/**
3899 * nes_destroy_ofa_device
3900 */
3901void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3902{
3903 if (nesibdev == NULL)
3904 return;
3905
3906 nes_unregister_ofa_device(nesibdev);
3907
3908 kfree(nesibdev->ibdev.iwcm);
3909 ib_dealloc_device(&nesibdev->ibdev);
3910}
3911
3912
3913/**
3914 * nes_register_ofa_device
3915 */
3916int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3917{
3918 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3919 struct nes_device *nesdev = nesvnic->nesdev;
3920 struct nes_adapter *nesadapter = nesdev->nesadapter;
3921 int i, ret;
3922
3923 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3924 if (ret) {
3925 return ret;
3926 }
3927
3928 /* Get the resources allocated to this device */
3929 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3930 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3931 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3932 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3933
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003934 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3935 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003936 if (ret) {
3937 while (i > 0) {
3938 i--;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003939 device_remove_file(&nesibdev->ibdev.dev,
3940 nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003941 }
3942 ib_unregister_device(&nesibdev->ibdev);
3943 return ret;
3944 }
3945 }
3946
3947 nesvnic->of_device_registered = 1;
3948
3949 return 0;
3950}
3951
3952
3953/**
3954 * nes_unregister_ofa_device
3955 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003956static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003957{
3958 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3959 int i;
3960
Tony Jonesf4e91eb2008-02-22 00:13:36 +01003961 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3962 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003963 }
3964
3965 if (nesvnic->of_device_registered) {
3966 ib_unregister_device(&nesibdev->ibdev);
3967 }
3968
3969 nesvnic->of_device_registered = 0;
3970}