Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / infiniband / hw / nes / nes_verbs.c
blob3436a9e98f93fc1e7f9f0cca6fcabda38e496bfb
1 /*
2 * Copyright (c) 2006 - 2008 NetEffect, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
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>
40 #include <rdma/ib_verbs.h>
41 #include <rdma/iw_cm.h>
42 #include <rdma/ib_user_verbs.h>
44 #include "nes.h"
46 #include <rdma/ib_umem.h>
48 atomic_t mod_qp_timouts;
49 atomic_t qps_created;
50 atomic_t sw_qps_destroyed;
53 /**
54 * nes_alloc_mw
56 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
57 unsigned long flags;
58 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;
73 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
74 stag_key = (u8)next_stag_index;
76 driver_key = 0;
78 next_stag_index >>= 8;
79 next_stag_index %= nesadapter->max_mr;
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);
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);
93 stag = stag_index << 8;
94 stag |= driver_key;
95 stag += (u32)stag_key;
97 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
98 stag, stag_index);
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);
108 cqp_request->waiting = 1;
109 cqp_wqe = &cqp_request->cqp_wqe;
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);
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);
120 atomic_set(&cqp_request->refcount, 2);
121 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
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)) {
130 if (atomic_dec_and_test(&cqp_request->refcount)) {
131 if (cqp_request->dynamic) {
132 kfree(cqp_request);
133 } else {
134 spin_lock_irqsave(&nesdev->cqp.lock, flags);
135 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
136 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
139 kfree(nesmr);
140 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
141 if (!ret) {
142 return ERR_PTR(-ETIME);
143 } else {
144 return ERR_PTR(-ENOMEM);
146 } else {
147 if (atomic_dec_and_test(&cqp_request->refcount)) {
148 if (cqp_request->dynamic) {
149 kfree(cqp_request);
150 } else {
151 spin_lock_irqsave(&nesdev->cqp.lock, flags);
152 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
153 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
158 nesmr->ibmw.rkey = stag;
159 nesmr->mode = IWNES_MEMREG_TYPE_MW;
160 ibmw = &nesmr->ibmw;
161 nesmr->pbl_4k = 0;
162 nesmr->pbls_used = 0;
164 return ibmw;
169 * nes_dealloc_mw
171 static int nes_dealloc_mw(struct ib_mw *ibmw)
173 struct nes_mr *nesmr = to_nesmw(ibmw);
174 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
175 struct nes_device *nesdev = nesvnic->nesdev;
176 struct nes_adapter *nesadapter = nesdev->nesadapter;
177 struct nes_hw_cqp_wqe *cqp_wqe;
178 struct nes_cqp_request *cqp_request;
179 int err = 0;
180 unsigned long flags;
181 int ret;
183 /* Deallocate the window with the adapter */
184 cqp_request = nes_get_cqp_request(nesdev);
185 if (cqp_request == NULL) {
186 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
187 return -ENOMEM;
189 cqp_request->waiting = 1;
190 cqp_wqe = &cqp_request->cqp_wqe;
191 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
192 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
193 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
195 atomic_set(&cqp_request->refcount, 2);
196 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
198 /* Wait for CQP */
199 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
200 ibmw->rkey);
201 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
202 NES_EVENT_TIMEOUT);
203 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
204 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
205 ret, cqp_request->major_code, cqp_request->minor_code);
206 if ((!ret) || (cqp_request->major_code)) {
207 if (atomic_dec_and_test(&cqp_request->refcount)) {
208 if (cqp_request->dynamic) {
209 kfree(cqp_request);
210 } else {
211 spin_lock_irqsave(&nesdev->cqp.lock, flags);
212 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
213 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
216 if (!ret) {
217 err = -ETIME;
218 } else {
219 err = -EIO;
221 } else {
222 if (atomic_dec_and_test(&cqp_request->refcount)) {
223 if (cqp_request->dynamic) {
224 kfree(cqp_request);
225 } else {
226 spin_lock_irqsave(&nesdev->cqp.lock, flags);
227 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
228 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
233 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
234 (ibmw->rkey & 0x0fffff00) >> 8);
235 kfree(nesmr);
237 return err;
242 * nes_bind_mw
244 static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
245 struct ib_mw_bind *ibmw_bind)
247 u64 u64temp;
248 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
249 struct nes_device *nesdev = nesvnic->nesdev;
250 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
251 struct nes_qp *nesqp = to_nesqp(ibqp);
252 struct nes_hw_qp_wqe *wqe;
253 unsigned long flags = 0;
254 u32 head;
255 u32 wqe_misc = 0;
256 u32 qsize;
258 if (nesqp->ibqp_state > IB_QPS_RTS)
259 return -EINVAL;
261 spin_lock_irqsave(&nesqp->lock, flags);
263 head = nesqp->hwqp.sq_head;
264 qsize = nesqp->hwqp.sq_tail;
266 /* Check for SQ overflow */
267 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
268 spin_unlock_irqrestore(&nesqp->lock, flags);
269 return -EINVAL;
272 wqe = &nesqp->hwqp.sq_vbase[head];
273 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
274 nes_fill_init_qp_wqe(wqe, nesqp, head);
275 u64temp = ibmw_bind->wr_id;
276 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
277 wqe_misc = NES_IWARP_SQ_OP_BIND;
279 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
281 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
282 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
284 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) {
285 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
287 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) {
288 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
291 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
292 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey);
293 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
294 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
295 ibmw_bind->length);
296 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
297 u64temp = (u64)ibmw_bind->addr;
298 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
300 head++;
301 if (head >= qsize)
302 head = 0;
304 nesqp->hwqp.sq_head = head;
305 barrier();
307 nes_write32(nesdev->regs+NES_WQE_ALLOC,
308 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
310 spin_unlock_irqrestore(&nesqp->lock, flags);
312 return 0;
317 * nes_alloc_fmr
319 static struct ib_fmr *nes_alloc_fmr(struct ib_pd *ibpd,
320 int ibmr_access_flags,
321 struct ib_fmr_attr *ibfmr_attr)
323 unsigned long flags;
324 struct nes_pd *nespd = to_nespd(ibpd);
325 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
326 struct nes_device *nesdev = nesvnic->nesdev;
327 struct nes_adapter *nesadapter = nesdev->nesadapter;
328 struct nes_fmr *nesfmr;
329 struct nes_cqp_request *cqp_request;
330 struct nes_hw_cqp_wqe *cqp_wqe;
331 int ret;
332 u32 stag;
333 u32 stag_index = 0;
334 u32 next_stag_index = 0;
335 u32 driver_key = 0;
336 u32 opcode = 0;
337 u8 stag_key = 0;
338 int i=0;
339 struct nes_vpbl vpbl;
341 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
342 stag_key = (u8)next_stag_index;
344 driver_key = 0;
346 next_stag_index >>= 8;
347 next_stag_index %= nesadapter->max_mr;
349 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
350 nesadapter->max_mr, &stag_index, &next_stag_index);
351 if (ret) {
352 goto failed_resource_alloc;
355 nesfmr = kzalloc(sizeof(*nesfmr), GFP_KERNEL);
356 if (!nesfmr) {
357 ret = -ENOMEM;
358 goto failed_fmr_alloc;
361 nesfmr->nesmr.mode = IWNES_MEMREG_TYPE_FMR;
362 if (ibfmr_attr->max_pages == 1) {
363 /* use zero length PBL */
364 nesfmr->nesmr.pbl_4k = 0;
365 nesfmr->nesmr.pbls_used = 0;
366 } else if (ibfmr_attr->max_pages <= 32) {
367 /* use PBL 256 */
368 nesfmr->nesmr.pbl_4k = 0;
369 nesfmr->nesmr.pbls_used = 1;
370 } else if (ibfmr_attr->max_pages <= 512) {
371 /* use 4K PBLs */
372 nesfmr->nesmr.pbl_4k = 1;
373 nesfmr->nesmr.pbls_used = 1;
374 } else {
375 /* use two level 4K PBLs */
376 /* add support for two level 256B PBLs */
377 nesfmr->nesmr.pbl_4k = 1;
378 nesfmr->nesmr.pbls_used = 1 + (ibfmr_attr->max_pages >> 9) +
379 ((ibfmr_attr->max_pages & 511) ? 1 : 0);
381 /* Register the region with the adapter */
382 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
384 /* track PBL resources */
385 if (nesfmr->nesmr.pbls_used != 0) {
386 if (nesfmr->nesmr.pbl_4k) {
387 if (nesfmr->nesmr.pbls_used > nesadapter->free_4kpbl) {
388 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
389 ret = -ENOMEM;
390 goto failed_vpbl_alloc;
391 } else {
392 nesadapter->free_4kpbl -= nesfmr->nesmr.pbls_used;
394 } else {
395 if (nesfmr->nesmr.pbls_used > nesadapter->free_256pbl) {
396 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
397 ret = -ENOMEM;
398 goto failed_vpbl_alloc;
399 } else {
400 nesadapter->free_256pbl -= nesfmr->nesmr.pbls_used;
405 /* one level pbl */
406 if (nesfmr->nesmr.pbls_used == 0) {
407 nesfmr->root_vpbl.pbl_vbase = NULL;
408 nes_debug(NES_DBG_MR, "zero level pbl \n");
409 } else if (nesfmr->nesmr.pbls_used == 1) {
410 /* can change it to kmalloc & dma_map_single */
411 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
412 &nesfmr->root_vpbl.pbl_pbase);
413 if (!nesfmr->root_vpbl.pbl_vbase) {
414 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
415 ret = -ENOMEM;
416 goto failed_vpbl_alloc;
418 nesfmr->leaf_pbl_cnt = 0;
419 nes_debug(NES_DBG_MR, "one level pbl, root_vpbl.pbl_vbase=%p \n",
420 nesfmr->root_vpbl.pbl_vbase);
422 /* two level pbl */
423 else {
424 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
425 &nesfmr->root_vpbl.pbl_pbase);
426 if (!nesfmr->root_vpbl.pbl_vbase) {
427 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
428 ret = -ENOMEM;
429 goto failed_vpbl_alloc;
432 nesfmr->root_vpbl.leaf_vpbl = kzalloc(sizeof(*nesfmr->root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
433 if (!nesfmr->root_vpbl.leaf_vpbl) {
434 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
435 ret = -ENOMEM;
436 goto failed_leaf_vpbl_alloc;
439 nesfmr->leaf_pbl_cnt = nesfmr->nesmr.pbls_used-1;
440 nes_debug(NES_DBG_MR, "two level pbl, root_vpbl.pbl_vbase=%p"
441 " leaf_pbl_cnt=%d root_vpbl.leaf_vpbl=%p\n",
442 nesfmr->root_vpbl.pbl_vbase, nesfmr->leaf_pbl_cnt, nesfmr->root_vpbl.leaf_vpbl);
444 for (i=0; i<nesfmr->leaf_pbl_cnt; i++)
445 nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase = NULL;
447 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
448 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
449 &vpbl.pbl_pbase);
451 if (!vpbl.pbl_vbase) {
452 ret = -ENOMEM;
453 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
454 goto failed_leaf_vpbl_pages_alloc;
457 nesfmr->root_vpbl.pbl_vbase[i].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
458 nesfmr->root_vpbl.pbl_vbase[i].pa_high = cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
459 nesfmr->root_vpbl.leaf_vpbl[i] = vpbl;
461 nes_debug(NES_DBG_MR, "pbase_low=0x%x, pbase_high=0x%x, vpbl=%p\n",
462 nesfmr->root_vpbl.pbl_vbase[i].pa_low,
463 nesfmr->root_vpbl.pbl_vbase[i].pa_high,
464 &nesfmr->root_vpbl.leaf_vpbl[i]);
467 nesfmr->ib_qp = NULL;
468 nesfmr->access_rights =0;
470 stag = stag_index << 8;
471 stag |= driver_key;
472 stag += (u32)stag_key;
474 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
475 cqp_request = nes_get_cqp_request(nesdev);
476 if (cqp_request == NULL) {
477 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
478 ret = -ENOMEM;
479 goto failed_leaf_vpbl_pages_alloc;
481 cqp_request->waiting = 1;
482 cqp_wqe = &cqp_request->cqp_wqe;
484 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
485 stag, stag_index);
487 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
489 if (nesfmr->nesmr.pbl_4k == 1)
490 opcode |= NES_CQP_STAG_PBL_BLK_SIZE;
492 if (ibmr_access_flags & IB_ACCESS_REMOTE_WRITE) {
493 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE |
494 NES_CQP_STAG_RIGHTS_LOCAL_WRITE | NES_CQP_STAG_REM_ACC_EN;
495 nesfmr->access_rights |=
496 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_RIGHTS_LOCAL_WRITE |
497 NES_CQP_STAG_REM_ACC_EN;
500 if (ibmr_access_flags & IB_ACCESS_REMOTE_READ) {
501 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ |
502 NES_CQP_STAG_RIGHTS_LOCAL_READ | NES_CQP_STAG_REM_ACC_EN;
503 nesfmr->access_rights |=
504 NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_RIGHTS_LOCAL_READ |
505 NES_CQP_STAG_REM_ACC_EN;
508 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
509 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
510 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
511 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
513 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX] =
514 cpu_to_le32((nesfmr->nesmr.pbls_used>1) ?
515 (nesfmr->nesmr.pbls_used-1) : nesfmr->nesmr.pbls_used);
517 atomic_set(&cqp_request->refcount, 2);
518 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
520 /* Wait for CQP */
521 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
522 NES_EVENT_TIMEOUT);
523 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
524 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
525 stag, ret, cqp_request->major_code, cqp_request->minor_code);
527 if ((!ret) || (cqp_request->major_code)) {
528 if (atomic_dec_and_test(&cqp_request->refcount)) {
529 if (cqp_request->dynamic) {
530 kfree(cqp_request);
531 } else {
532 spin_lock_irqsave(&nesdev->cqp.lock, flags);
533 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
534 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
537 ret = (!ret) ? -ETIME : -EIO;
538 goto failed_leaf_vpbl_pages_alloc;
539 } else {
540 if (atomic_dec_and_test(&cqp_request->refcount)) {
541 if (cqp_request->dynamic) {
542 kfree(cqp_request);
543 } else {
544 spin_lock_irqsave(&nesdev->cqp.lock, flags);
545 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
546 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
551 nesfmr->nesmr.ibfmr.lkey = stag;
552 nesfmr->nesmr.ibfmr.rkey = stag;
553 nesfmr->attr = *ibfmr_attr;
555 return &nesfmr->nesmr.ibfmr;
557 failed_leaf_vpbl_pages_alloc:
558 /* unroll all allocated pages */
559 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
560 if (nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase) {
561 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
562 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
565 if (nesfmr->root_vpbl.leaf_vpbl)
566 kfree(nesfmr->root_vpbl.leaf_vpbl);
568 failed_leaf_vpbl_alloc:
569 if (nesfmr->leaf_pbl_cnt == 0) {
570 if (nesfmr->root_vpbl.pbl_vbase)
571 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
572 nesfmr->root_vpbl.pbl_pbase);
573 } else
574 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
575 nesfmr->root_vpbl.pbl_pbase);
577 failed_vpbl_alloc:
578 kfree(nesfmr);
580 failed_fmr_alloc:
581 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
583 failed_resource_alloc:
584 return ERR_PTR(ret);
589 * nes_dealloc_fmr
591 static int nes_dealloc_fmr(struct ib_fmr *ibfmr)
593 struct nes_mr *nesmr = to_nesmr_from_ibfmr(ibfmr);
594 struct nes_fmr *nesfmr = to_nesfmr(nesmr);
595 struct nes_vnic *nesvnic = to_nesvnic(ibfmr->device);
596 struct nes_device *nesdev = nesvnic->nesdev;
597 struct nes_mr temp_nesmr = *nesmr;
598 int i = 0;
600 temp_nesmr.ibmw.device = ibfmr->device;
601 temp_nesmr.ibmw.pd = ibfmr->pd;
602 temp_nesmr.ibmw.rkey = ibfmr->rkey;
603 temp_nesmr.ibmw.uobject = NULL;
605 /* free the resources */
606 if (nesfmr->leaf_pbl_cnt == 0) {
607 /* single PBL case */
608 if (nesfmr->root_vpbl.pbl_vbase)
609 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
610 nesfmr->root_vpbl.pbl_pbase);
611 } else {
612 for (i = 0; i < nesfmr->leaf_pbl_cnt; i++) {
613 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
614 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
616 kfree(nesfmr->root_vpbl.leaf_vpbl);
617 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
618 nesfmr->root_vpbl.pbl_pbase);
621 return nes_dealloc_mw(&temp_nesmr.ibmw);
626 * nes_map_phys_fmr
628 static int nes_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
629 int list_len, u64 iova)
631 return 0;
636 * nes_unmap_frm
638 static int nes_unmap_fmr(struct list_head *ibfmr_list)
640 return 0;
646 * nes_query_device
648 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
650 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
651 struct nes_device *nesdev = nesvnic->nesdev;
652 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
654 memset(props, 0, sizeof(*props));
655 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
657 props->fw_ver = nesdev->nesadapter->fw_ver;
658 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
659 props->vendor_id = nesdev->nesadapter->vendor_id;
660 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
661 props->hw_ver = nesdev->nesadapter->hw_rev;
662 props->max_mr_size = 0x80000000;
663 props->max_qp = nesibdev->max_qp;
664 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
665 props->max_sge = nesdev->nesadapter->max_sge;
666 props->max_cq = nesibdev->max_cq;
667 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
668 props->max_mr = nesibdev->max_mr;
669 props->max_mw = nesibdev->max_mr;
670 props->max_pd = nesibdev->max_pd;
671 props->max_sge_rd = 1;
672 switch (nesdev->nesadapter->max_irrq_wr) {
673 case 0:
674 props->max_qp_rd_atom = 1;
675 break;
676 case 1:
677 props->max_qp_rd_atom = 4;
678 break;
679 case 2:
680 props->max_qp_rd_atom = 16;
681 break;
682 case 3:
683 props->max_qp_rd_atom = 32;
684 break;
685 default:
686 props->max_qp_rd_atom = 0;
688 props->max_qp_init_rd_atom = props->max_qp_wr;
689 props->atomic_cap = IB_ATOMIC_NONE;
690 props->max_map_per_fmr = 1;
692 return 0;
697 * nes_query_port
699 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
701 memset(props, 0, sizeof(*props));
703 props->max_mtu = IB_MTU_2048;
704 props->active_mtu = IB_MTU_2048;
705 props->lid = 1;
706 props->lmc = 0;
707 props->sm_lid = 0;
708 props->sm_sl = 0;
709 props->state = IB_PORT_ACTIVE;
710 props->phys_state = 0;
711 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
712 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
713 props->gid_tbl_len = 1;
714 props->pkey_tbl_len = 1;
715 props->qkey_viol_cntr = 0;
716 props->active_width = IB_WIDTH_4X;
717 props->active_speed = 1;
718 props->max_msg_sz = 0x80000000;
720 return 0;
725 * nes_modify_port
727 static int nes_modify_port(struct ib_device *ibdev, u8 port,
728 int port_modify_mask, struct ib_port_modify *props)
730 return 0;
735 * nes_query_pkey
737 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
739 *pkey = 0;
740 return 0;
745 * nes_query_gid
747 static int nes_query_gid(struct ib_device *ibdev, u8 port,
748 int index, union ib_gid *gid)
750 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
752 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
753 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
755 return 0;
760 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
761 * of all objects associated with a particular user-mode client.
763 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
764 struct ib_udata *udata)
766 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
767 struct nes_device *nesdev = nesvnic->nesdev;
768 struct nes_adapter *nesadapter = nesdev->nesadapter;
769 struct nes_alloc_ucontext_req req;
770 struct nes_alloc_ucontext_resp uresp;
771 struct nes_ucontext *nes_ucontext;
772 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
775 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
776 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
777 return ERR_PTR(-EINVAL);
780 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
781 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
782 req.userspace_ver, NES_ABI_USERSPACE_VER);
783 return ERR_PTR(-EINVAL);
787 memset(&uresp, 0, sizeof uresp);
789 uresp.max_qps = nesibdev->max_qp;
790 uresp.max_pds = nesibdev->max_pd;
791 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
792 uresp.virtwq = nesadapter->virtwq;
793 uresp.kernel_ver = NES_ABI_KERNEL_VER;
795 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
796 if (!nes_ucontext)
797 return ERR_PTR(-ENOMEM);
799 nes_ucontext->nesdev = nesdev;
800 nes_ucontext->mmap_wq_offset = uresp.max_pds;
801 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
802 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
803 PAGE_SIZE;
806 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
807 kfree(nes_ucontext);
808 return ERR_PTR(-EFAULT);
811 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
812 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
813 atomic_set(&nes_ucontext->usecnt, 1);
814 return &nes_ucontext->ibucontext;
819 * nes_dealloc_ucontext
821 static int nes_dealloc_ucontext(struct ib_ucontext *context)
823 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
824 /* struct nes_device *nesdev = nesvnic->nesdev; */
825 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
827 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
828 return 0;
829 kfree(nes_ucontext);
830 return 0;
835 * nes_mmap
837 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
839 unsigned long index;
840 struct nes_vnic *nesvnic = to_nesvnic(context->device);
841 struct nes_device *nesdev = nesvnic->nesdev;
842 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
843 struct nes_ucontext *nes_ucontext;
844 struct nes_qp *nesqp;
846 nes_ucontext = to_nesucontext(context);
849 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
850 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
851 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
852 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
853 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
854 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
855 return -EFAULT;
857 nesqp = nes_ucontext->mmap_nesqp[index];
858 if (nesqp == NULL) {
859 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
860 return -EFAULT;
862 if (remap_pfn_range(vma, vma->vm_start,
863 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
864 vma->vm_end - vma->vm_start,
865 vma->vm_page_prot)) {
866 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
867 return -EAGAIN;
869 vma->vm_private_data = nesqp;
870 return 0;
871 } else {
872 index = vma->vm_pgoff;
873 if (!test_bit(index, nes_ucontext->allocated_doorbells))
874 return -EFAULT;
876 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
877 if (io_remap_pfn_range(vma, vma->vm_start,
878 (nesdev->doorbell_start +
879 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
880 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
881 return -EAGAIN;
882 vma->vm_private_data = nes_ucontext;
883 return 0;
886 return -ENOSYS;
891 * nes_alloc_pd
893 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
894 struct ib_ucontext *context, struct ib_udata *udata)
896 struct nes_pd *nespd;
897 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
898 struct nes_device *nesdev = nesvnic->nesdev;
899 struct nes_adapter *nesadapter = nesdev->nesadapter;
900 struct nes_ucontext *nesucontext;
901 struct nes_alloc_pd_resp uresp;
902 u32 pd_num = 0;
903 int err;
905 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
906 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
907 atomic_read(&nesvnic->netdev->refcnt));
909 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
910 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
911 if (err) {
912 return ERR_PTR(err);
915 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
916 if (!nespd) {
917 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
918 return ERR_PTR(-ENOMEM);
921 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
922 nespd, nesvnic->nesibdev->ibdev.name);
924 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
926 if (context) {
927 nesucontext = to_nesucontext(context);
928 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
929 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
930 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
931 nespd->mmap_db_index, nespd->pd_id);
932 <<<<<<< HEAD:drivers/infiniband/hw/nes/nes_verbs.c
933 if (nespd->mmap_db_index > NES_MAX_USER_DB_REGIONS) {
934 =======
935 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
936 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/nes/nes_verbs.c
937 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
938 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
939 kfree(nespd);
940 return ERR_PTR(-ENOMEM);
943 uresp.pd_id = nespd->pd_id;
944 uresp.mmap_db_index = nespd->mmap_db_index;
945 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
946 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
947 kfree(nespd);
948 return ERR_PTR(-EFAULT);
951 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
952 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
953 nesucontext->first_free_db = nespd->mmap_db_index + 1;
956 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
957 return &nespd->ibpd;
962 * nes_dealloc_pd
964 static int nes_dealloc_pd(struct ib_pd *ibpd)
966 struct nes_ucontext *nesucontext;
967 struct nes_pd *nespd = to_nespd(ibpd);
968 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
969 struct nes_device *nesdev = nesvnic->nesdev;
970 struct nes_adapter *nesadapter = nesdev->nesadapter;
972 if ((ibpd->uobject) && (ibpd->uobject->context)) {
973 nesucontext = to_nesucontext(ibpd->uobject->context);
974 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
975 nespd->mmap_db_index);
976 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
977 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
978 if (nesucontext->first_free_db > nespd->mmap_db_index) {
979 nesucontext->first_free_db = nespd->mmap_db_index;
983 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
984 nespd->pd_id, nespd);
985 nes_free_resource(nesadapter, nesadapter->allocated_pds,
986 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
987 kfree(nespd);
989 return 0;
994 * nes_create_ah
996 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
998 return ERR_PTR(-ENOSYS);
1003 * nes_destroy_ah
1005 static int nes_destroy_ah(struct ib_ah *ah)
1007 return -ENOSYS;
1012 * nes_get_encoded_size
1014 static inline u8 nes_get_encoded_size(int *size)
1016 u8 encoded_size = 0;
1017 if (*size <= 32) {
1018 *size = 32;
1019 encoded_size = 1;
1020 } else if (*size <= 128) {
1021 *size = 128;
1022 encoded_size = 2;
1023 } else if (*size <= 512) {
1024 *size = 512;
1025 encoded_size = 3;
1027 return (encoded_size);
1033 * nes_setup_virt_qp
1035 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
1036 struct nes_vnic *nesvnic, int sq_size, int rq_size)
1038 unsigned long flags;
1039 void *mem;
1040 __le64 *pbl = NULL;
1041 __le64 *tpbl;
1042 __le64 *pblbuffer;
1043 struct nes_device *nesdev = nesvnic->nesdev;
1044 struct nes_adapter *nesadapter = nesdev->nesadapter;
1045 u32 pbl_entries;
1046 u8 rq_pbl_entries;
1047 u8 sq_pbl_entries;
1049 pbl_entries = nespbl->pbl_size >> 3;
1050 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%p\n",
1051 nespbl->pbl_size, pbl_entries,
1052 (void *)nespbl->pbl_vbase,
1053 (void *)nespbl->pbl_pbase);
1054 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
1055 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
1056 /* the first pbl to be fro the rq_vbase... */
1057 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1058 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1059 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1060 if (!nespbl->page) {
1061 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
1062 kfree(nespbl);
1063 return -ENOMEM;
1066 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
1067 nesqp->page = nespbl->page;
1068 if (!nesqp->hwqp.sq_vbase) {
1069 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
1070 kfree(nespbl);
1071 return -ENOMEM;
1074 /* Now to get to sq.. we need to calculate how many */
1075 /* PBL entries were used by the rq.. */
1076 pbl += sq_pbl_entries;
1077 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1078 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
1079 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
1081 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%p rq_vbase=%p rq_pbase=%p\n",
1082 nesqp->hwqp.sq_vbase, (void *)nesqp->hwqp.sq_pbase,
1083 nesqp->hwqp.rq_vbase, (void *)nesqp->hwqp.rq_pbase);
1084 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1085 if (!nesadapter->free_256pbl) {
1086 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1087 nespbl->pbl_pbase);
1088 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1089 kunmap(nesqp->page);
1090 kfree(nespbl);
1091 return -ENOMEM;
1093 nesadapter->free_256pbl--;
1094 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1096 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
1097 pblbuffer = nesqp->pbl_vbase;
1098 if (!nesqp->pbl_vbase) {
1099 /* memory allocated during nes_reg_user_mr() */
1100 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1101 nespbl->pbl_pbase);
1102 kfree(nespbl);
1103 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1104 nesadapter->free_256pbl++;
1105 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1106 kunmap(nesqp->page);
1107 return -ENOMEM;
1109 memset(nesqp->pbl_vbase, 0, 256);
1110 /* fill in the page address in the pbl buffer.. */
1111 tpbl = pblbuffer + 16;
1112 pbl = (__le64 *)nespbl->pbl_vbase;
1113 while (sq_pbl_entries--)
1114 *tpbl++ = *pbl++;
1115 tpbl = pblbuffer;
1116 while (rq_pbl_entries--)
1117 *tpbl++ = *pbl++;
1119 /* done with memory allocated during nes_reg_user_mr() */
1120 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1121 nespbl->pbl_pbase);
1122 kfree(nespbl);
1124 nesqp->qp_mem_size =
1125 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1126 /* Round up to a multiple of a page */
1127 nesqp->qp_mem_size += PAGE_SIZE - 1;
1128 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1130 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1131 &nesqp->hwqp.q2_pbase);
1133 if (!mem) {
1134 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1135 nesqp->pbl_vbase = NULL;
1136 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1137 nesadapter->free_256pbl++;
1138 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1139 kunmap(nesqp->page);
1140 return -ENOMEM;
1142 nesqp->hwqp.q2_vbase = mem;
1143 mem += 256;
1144 memset(nesqp->hwqp.q2_vbase, 0, 256);
1145 nesqp->nesqp_context = mem;
1146 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1147 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1149 return 0;
1154 * nes_setup_mmap_qp
1156 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1157 int sq_size, int rq_size)
1159 void *mem;
1160 struct nes_device *nesdev = nesvnic->nesdev;
1162 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1163 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1164 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1165 256; /* this is Q2 */
1166 /* Round up to a multiple of a page */
1167 nesqp->qp_mem_size += PAGE_SIZE - 1;
1168 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1170 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1171 &nesqp->hwqp.sq_pbase);
1172 if (!mem)
1173 return -ENOMEM;
1174 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1175 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1176 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1178 memset(mem, 0, nesqp->qp_mem_size);
1180 nesqp->hwqp.sq_vbase = mem;
1181 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1183 nesqp->hwqp.rq_vbase = mem;
1184 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1185 sizeof(struct nes_hw_qp_wqe) * sq_size;
1186 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1188 nesqp->hwqp.q2_vbase = mem;
1189 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1190 sizeof(struct nes_hw_qp_wqe) * rq_size;
1191 mem += 256;
1192 memset(nesqp->hwqp.q2_vbase, 0, 256);
1194 nesqp->nesqp_context = mem;
1195 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1196 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1197 return 0;
1202 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1204 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1205 struct nes_qp *nesqp, int virt_wqs)
1207 unsigned long flags;
1208 struct nes_adapter *nesadapter = nesdev->nesadapter;
1209 if (!virt_wqs) {
1210 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1211 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1212 }else {
1213 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1214 nesadapter->free_256pbl++;
1215 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1216 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1217 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1218 nesqp->pbl_vbase = NULL;
1219 kunmap(nesqp->page);
1225 * nes_create_qp
1227 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1228 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1230 u64 u64temp= 0;
1231 u64 u64nesqp = 0;
1232 struct nes_pd *nespd = to_nespd(ibpd);
1233 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1234 struct nes_device *nesdev = nesvnic->nesdev;
1235 struct nes_adapter *nesadapter = nesdev->nesadapter;
1236 struct nes_qp *nesqp;
1237 struct nes_cq *nescq;
1238 struct nes_ucontext *nes_ucontext;
1239 struct nes_hw_cqp_wqe *cqp_wqe;
1240 struct nes_cqp_request *cqp_request;
1241 struct nes_create_qp_req req;
1242 struct nes_create_qp_resp uresp;
1243 struct nes_pbl *nespbl = NULL;
1244 u32 qp_num = 0;
1245 u32 opcode = 0;
1246 /* u32 counter = 0; */
1247 void *mem;
1248 unsigned long flags;
1249 int ret;
1250 int err;
1251 int virt_wqs = 0;
1252 int sq_size;
1253 int rq_size;
1254 u8 sq_encoded_size;
1255 u8 rq_encoded_size;
1256 /* int counter; */
1258 atomic_inc(&qps_created);
1259 switch (init_attr->qp_type) {
1260 case IB_QPT_RC:
1261 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1262 init_attr->cap.max_inline_data = 0;
1263 } else {
1264 init_attr->cap.max_inline_data = 64;
1266 sq_size = init_attr->cap.max_send_wr;
1267 rq_size = init_attr->cap.max_recv_wr;
1269 // check if the encoded sizes are OK or not...
1270 sq_encoded_size = nes_get_encoded_size(&sq_size);
1271 rq_encoded_size = nes_get_encoded_size(&rq_size);
1273 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1274 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1275 rq_size, sq_size);
1276 return ERR_PTR(-EINVAL);
1279 init_attr->cap.max_send_wr = sq_size -2;
1280 init_attr->cap.max_recv_wr = rq_size -1;
1281 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1283 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1284 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1285 if (ret) {
1286 return ERR_PTR(ret);
1289 /* Need 512 (actually now 1024) byte alignment on this structure */
1290 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1291 if (!mem) {
1292 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1293 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1294 return ERR_PTR(-ENOMEM);
1296 u64nesqp = (unsigned long)mem;
1297 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1298 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1299 u64nesqp &= ~u64temp;
1300 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1301 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1302 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1303 nesqp->allocated_buffer = mem;
1305 if (udata) {
1306 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1307 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1308 kfree(nesqp->allocated_buffer);
1309 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1310 return NULL;
1312 if (req.user_wqe_buffers) {
1313 virt_wqs = 1;
1315 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1316 nesqp->user_mode = 1;
1317 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1318 if (virt_wqs) {
1319 err = 1;
1320 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1321 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1322 list_del(&nespbl->list);
1323 err = 0;
1324 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1325 nespbl, nespbl->user_base);
1326 break;
1329 if (err) {
1330 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1331 (long long unsigned int)req.user_wqe_buffers);
1332 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1333 kfree(nesqp->allocated_buffer);
1334 <<<<<<< HEAD:drivers/infiniband/hw/nes/nes_verbs.c
1335 return ERR_PTR(-ENOMEM);
1336 =======
1337 return ERR_PTR(-EFAULT);
1338 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/nes/nes_verbs.c
1342 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1343 nesqp->mmap_sq_db_index =
1344 find_next_zero_bit(nes_ucontext->allocated_wqs,
1345 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1346 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1347 nespd->mmap_db_index); */
1348 <<<<<<< HEAD:drivers/infiniband/hw/nes/nes_verbs.c
1349 if (nesqp->mmap_sq_db_index > NES_MAX_USER_WQ_REGIONS) {
1350 =======
1351 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1352 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/nes/nes_verbs.c
1353 nes_debug(NES_DBG_QP,
1354 "db index > max user regions, failing create QP\n");
1355 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1356 if (virt_wqs) {
1357 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1358 nespbl->pbl_pbase);
1359 kfree(nespbl);
1361 kfree(nesqp->allocated_buffer);
1362 return ERR_PTR(-ENOMEM);
1364 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1365 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1366 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1367 } else {
1368 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1369 kfree(nesqp->allocated_buffer);
1370 return ERR_PTR(-EFAULT);
1373 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1374 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1375 if (err) {
1376 nes_debug(NES_DBG_QP,
1377 "error geting qp mem code = %d\n", err);
1378 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1379 kfree(nesqp->allocated_buffer);
1380 return ERR_PTR(-ENOMEM);
1383 nesqp->hwqp.sq_size = sq_size;
1384 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1385 nesqp->hwqp.sq_head = 1;
1386 nesqp->hwqp.rq_size = rq_size;
1387 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1388 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1389 (void *)nesqp->nesqp_context_pbase);
1391 nesqp->hwqp.qp_id = qp_num;
1392 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1393 nesqp->nespd = nespd;
1395 nescq = to_nescq(init_attr->send_cq);
1396 nesqp->nesscq = nescq;
1397 nescq = to_nescq(init_attr->recv_cq);
1398 nesqp->nesrcq = nescq;
1400 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1401 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1402 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1403 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1404 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1405 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1406 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1407 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1408 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1409 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1410 u64temp = (u64)nesqp->hwqp.sq_pbase;
1411 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1412 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1415 if (!virt_wqs) {
1416 u64temp = (u64)nesqp->hwqp.sq_pbase;
1417 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1418 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1419 u64temp = (u64)nesqp->hwqp.rq_pbase;
1420 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1421 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1422 } else {
1423 u64temp = (u64)nesqp->pbl_pbase;
1424 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1425 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1428 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1429 nesvnic->next_qp_nic_index,
1430 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1431 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1432 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1433 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1434 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1435 nesvnic->next_qp_nic_index++;
1436 if ((nesvnic->next_qp_nic_index > 3) ||
1437 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1438 nesvnic->next_qp_nic_index = 0;
1440 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1442 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1443 u64temp = (u64)nesqp->hwqp.q2_pbase;
1444 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1445 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1446 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1447 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1448 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1449 ((((u32)nesadapter->max_irrq_wr) <<
1450 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1451 if (disable_mpa_crc) {
1452 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1453 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1457 /* Create the QP */
1458 cqp_request = nes_get_cqp_request(nesdev);
1459 if (cqp_request == NULL) {
1460 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1461 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1462 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1463 kfree(nesqp->allocated_buffer);
1464 return ERR_PTR(-ENOMEM);
1466 cqp_request->waiting = 1;
1467 cqp_wqe = &cqp_request->cqp_wqe;
1469 if (!virt_wqs) {
1470 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1471 NES_CQP_QP_IWARP_STATE_IDLE;
1472 } else {
1473 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1474 NES_CQP_QP_IWARP_STATE_IDLE;
1476 opcode |= NES_CQP_QP_CQS_VALID;
1477 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1478 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1479 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1481 u64temp = (u64)nesqp->nesqp_context_pbase;
1482 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1484 atomic_set(&cqp_request->refcount, 2);
1485 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1487 /* Wait for CQP */
1488 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1489 nesqp->hwqp.qp_id);
1490 ret = wait_event_timeout(cqp_request->waitq,
1491 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1492 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1493 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1494 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1495 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1496 cqp_request->major_code, cqp_request->minor_code);
1497 if ((!ret) || (cqp_request->major_code)) {
1498 if (atomic_dec_and_test(&cqp_request->refcount)) {
1499 if (cqp_request->dynamic) {
1500 kfree(cqp_request);
1501 } else {
1502 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1503 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1504 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1507 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1508 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1509 kfree(nesqp->allocated_buffer);
1510 if (!ret) {
1511 return ERR_PTR(-ETIME);
1512 } else {
1513 return ERR_PTR(-EIO);
1515 } else {
1516 if (atomic_dec_and_test(&cqp_request->refcount)) {
1517 if (cqp_request->dynamic) {
1518 kfree(cqp_request);
1519 } else {
1520 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1521 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1522 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1527 if (ibpd->uobject) {
1528 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1529 uresp.actual_sq_size = sq_size;
1530 uresp.actual_rq_size = rq_size;
1531 uresp.qp_id = nesqp->hwqp.qp_id;
1532 uresp.nes_drv_opt = nes_drv_opt;
1533 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1534 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1535 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1536 kfree(nesqp->allocated_buffer);
1537 return ERR_PTR(-EFAULT);
1541 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1542 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1543 spin_lock_init(&nesqp->lock);
1544 init_waitqueue_head(&nesqp->state_waitq);
1545 init_waitqueue_head(&nesqp->kick_waitq);
1546 nes_add_ref(&nesqp->ibqp);
1547 break;
1548 default:
1549 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1550 return ERR_PTR(-EINVAL);
1551 break;
1554 /* update the QP table */
1555 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1556 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1557 atomic_read(&nesvnic->netdev->refcnt));
1559 return &nesqp->ibqp;
1564 * nes_destroy_qp
1566 static int nes_destroy_qp(struct ib_qp *ibqp)
1568 struct nes_qp *nesqp = to_nesqp(ibqp);
1569 /* struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); */
1570 struct nes_ucontext *nes_ucontext;
1571 struct ib_qp_attr attr;
1572 struct iw_cm_id *cm_id;
1573 struct iw_cm_event cm_event;
1574 int ret;
1576 atomic_inc(&sw_qps_destroyed);
1577 nesqp->destroyed = 1;
1579 /* Blow away the connection if it exists. */
1580 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1581 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1582 attr.qp_state = IB_QPS_ERR;
1583 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1586 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1587 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1588 cm_id = nesqp->cm_id;
1589 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1590 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1591 cm_event.local_addr = cm_id->local_addr;
1592 cm_event.remote_addr = cm_id->remote_addr;
1593 cm_event.private_data = NULL;
1594 cm_event.private_data_len = 0;
1596 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1597 "QP%u. cm_id = %p, refcount = %u. \n",
1598 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1600 cm_id->rem_ref(cm_id);
1601 ret = cm_id->event_handler(cm_id, &cm_event);
1602 if (ret)
1603 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1607 if (nesqp->user_mode) {
1608 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1609 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1610 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1611 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1612 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1613 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1616 if (nesqp->pbl_pbase)
1617 kunmap(nesqp->page);
1620 nes_rem_ref(&nesqp->ibqp);
1621 return 0;
1626 * nes_create_cq
1628 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1629 int comp_vector,
1630 struct ib_ucontext *context, struct ib_udata *udata)
1632 u64 u64temp;
1633 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1634 struct nes_device *nesdev = nesvnic->nesdev;
1635 struct nes_adapter *nesadapter = nesdev->nesadapter;
1636 struct nes_cq *nescq;
1637 struct nes_ucontext *nes_ucontext = NULL;
1638 struct nes_cqp_request *cqp_request;
1639 void *mem = NULL;
1640 struct nes_hw_cqp_wqe *cqp_wqe;
1641 struct nes_pbl *nespbl = NULL;
1642 struct nes_create_cq_req req;
1643 struct nes_create_cq_resp resp;
1644 u32 cq_num = 0;
1645 u32 opcode = 0;
1646 u32 pbl_entries = 1;
1647 int err;
1648 unsigned long flags;
1649 int ret;
1651 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1652 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1653 if (err) {
1654 return ERR_PTR(err);
1657 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1658 if (!nescq) {
1659 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1660 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1661 return ERR_PTR(-ENOMEM);
1664 nescq->hw_cq.cq_size = max(entries + 1, 5);
1665 nescq->hw_cq.cq_number = cq_num;
1666 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1669 if (context) {
1670 nes_ucontext = to_nesucontext(context);
1671 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1672 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1673 kfree(nescq);
1674 return ERR_PTR(-EFAULT);
1676 nesvnic->mcrq_ucontext = nes_ucontext;
1677 nes_ucontext->mcrqf = req.mcrqf;
1678 if (nes_ucontext->mcrqf) {
1679 if (nes_ucontext->mcrqf & 0x80000000)
1680 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 12 + (nes_ucontext->mcrqf & 0xf) - 1;
1681 else if (nes_ucontext->mcrqf & 0x40000000)
1682 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1683 else
1684 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1685 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1687 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1688 (unsigned long)req.user_cq_buffer, entries);
1689 <<<<<<< HEAD:drivers/infiniband/hw/nes/nes_verbs.c
1690 =======
1691 err = 1;
1692 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/nes/nes_verbs.c
1693 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1694 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1695 list_del(&nespbl->list);
1696 err = 0;
1697 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1698 nespbl);
1699 break;
1702 if (err) {
1703 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1704 kfree(nescq);
1705 <<<<<<< HEAD:drivers/infiniband/hw/nes/nes_verbs.c
1706 return ERR_PTR(err);
1707 =======
1708 return ERR_PTR(-EFAULT);
1709 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/nes/nes_verbs.c
1712 pbl_entries = nespbl->pbl_size >> 3;
1713 nescq->cq_mem_size = 0;
1714 } else {
1715 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1716 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1717 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1719 /* allocate the physical buffer space */
1720 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1721 &nescq->hw_cq.cq_pbase);
1722 if (!mem) {
1723 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1724 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1725 kfree(nescq);
1726 return ERR_PTR(-ENOMEM);
1729 memset(mem, 0, nescq->cq_mem_size);
1730 nescq->hw_cq.cq_vbase = mem;
1731 nescq->hw_cq.cq_head = 0;
1732 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1733 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1734 (u32)nescq->hw_cq.cq_pbase);
1737 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1738 spin_lock_init(&nescq->lock);
1740 /* send CreateCQ request to CQP */
1741 cqp_request = nes_get_cqp_request(nesdev);
1742 if (cqp_request == NULL) {
1743 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1744 if (!context)
1745 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1746 nescq->hw_cq.cq_pbase);
1747 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1748 kfree(nescq);
1749 return ERR_PTR(-ENOMEM);
1751 cqp_request->waiting = 1;
1752 cqp_wqe = &cqp_request->cqp_wqe;
1754 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1755 NES_CQP_CQ_CHK_OVERFLOW |
1756 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1758 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1760 if (pbl_entries != 1) {
1761 if (pbl_entries > 32) {
1762 /* use 4k pbl */
1763 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1764 if (nesadapter->free_4kpbl == 0) {
1765 if (cqp_request->dynamic) {
1766 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1767 kfree(cqp_request);
1768 } else {
1769 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1770 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1772 if (!context)
1773 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1774 nescq->hw_cq.cq_pbase);
1775 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1776 kfree(nescq);
1777 return ERR_PTR(-ENOMEM);
1778 } else {
1779 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1780 nescq->virtual_cq = 2;
1781 nesadapter->free_4kpbl--;
1783 } else {
1784 /* use 256 byte pbl */
1785 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1786 if (nesadapter->free_256pbl == 0) {
1787 if (cqp_request->dynamic) {
1788 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1789 kfree(cqp_request);
1790 } else {
1791 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1792 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1794 if (!context)
1795 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1796 nescq->hw_cq.cq_pbase);
1797 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1798 kfree(nescq);
1799 return ERR_PTR(-ENOMEM);
1800 } else {
1801 opcode |= NES_CQP_CQ_VIRT;
1802 nescq->virtual_cq = 1;
1803 nesadapter->free_256pbl--;
1808 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1810 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1811 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1812 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1813 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1815 if (context) {
1816 if (pbl_entries != 1)
1817 u64temp = (u64)nespbl->pbl_pbase;
1818 else
1819 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1820 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1821 nes_ucontext->mmap_db_index[0]);
1822 } else {
1823 u64temp = (u64)nescq->hw_cq.cq_pbase;
1824 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1826 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1827 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1828 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1829 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1830 cpu_to_le32((u32)(u64temp >> 1));
1831 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1832 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1834 atomic_set(&cqp_request->refcount, 2);
1835 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1837 /* Wait for CQP */
1838 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1839 nescq->hw_cq.cq_number);
1840 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1841 NES_EVENT_TIMEOUT * 2);
1842 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1843 nescq->hw_cq.cq_number, ret);
1844 if ((!ret) || (cqp_request->major_code)) {
1845 if (atomic_dec_and_test(&cqp_request->refcount)) {
1846 if (cqp_request->dynamic) {
1847 kfree(cqp_request);
1848 } else {
1849 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1850 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1851 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1854 <<<<<<< HEAD:drivers/infiniband/hw/nes/nes_verbs.c
1855 nes_debug(NES_DBG_CQ, "iWARP CQ%u create timeout expired, major code = 0x%04X,"
1856 " minor code = 0x%04X\n",
1857 nescq->hw_cq.cq_number, cqp_request->major_code, cqp_request->minor_code);
1858 =======
1859 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/nes/nes_verbs.c
1860 if (!context)
1861 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1862 nescq->hw_cq.cq_pbase);
1863 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1864 kfree(nescq);
1865 return ERR_PTR(-EIO);
1866 } else {
1867 if (atomic_dec_and_test(&cqp_request->refcount)) {
1868 if (cqp_request->dynamic) {
1869 kfree(cqp_request);
1870 } else {
1871 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1872 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1873 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1878 if (context) {
1879 /* free the nespbl */
1880 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1881 nespbl->pbl_pbase);
1882 kfree(nespbl);
1883 resp.cq_id = nescq->hw_cq.cq_number;
1884 resp.cq_size = nescq->hw_cq.cq_size;
1885 resp.mmap_db_index = 0;
1886 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1887 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1888 kfree(nescq);
1889 return ERR_PTR(-EFAULT);
1893 return &nescq->ibcq;
1898 * nes_destroy_cq
1900 static int nes_destroy_cq(struct ib_cq *ib_cq)
1902 struct nes_cq *nescq;
1903 struct nes_device *nesdev;
1904 struct nes_vnic *nesvnic;
1905 struct nes_adapter *nesadapter;
1906 struct nes_hw_cqp_wqe *cqp_wqe;
1907 struct nes_cqp_request *cqp_request;
1908 unsigned long flags;
1909 u32 opcode = 0;
1910 int ret;
1912 if (ib_cq == NULL)
1913 return 0;
1915 nescq = to_nescq(ib_cq);
1916 nesvnic = to_nesvnic(ib_cq->device);
1917 nesdev = nesvnic->nesdev;
1918 nesadapter = nesdev->nesadapter;
1920 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1922 /* Send DestroyCQ request to CQP */
1923 cqp_request = nes_get_cqp_request(nesdev);
1924 if (cqp_request == NULL) {
1925 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1926 return -ENOMEM;
1928 cqp_request->waiting = 1;
1929 cqp_wqe = &cqp_request->cqp_wqe;
1930 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1931 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1932 if (nescq->virtual_cq == 1) {
1933 nesadapter->free_256pbl++;
1934 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1935 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1936 __FUNCTION__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1938 } else if (nescq->virtual_cq == 2) {
1939 nesadapter->free_4kpbl++;
1940 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1941 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1942 __FUNCTION__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1944 opcode |= NES_CQP_CQ_4KB_CHUNK;
1947 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1949 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1950 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1951 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1952 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1953 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1954 atomic_set(&cqp_request->refcount, 2);
1955 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1957 /* Wait for CQP */
1958 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1959 nescq->hw_cq.cq_number);
1960 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1961 NES_EVENT_TIMEOUT);
1962 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1963 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1964 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1965 cqp_request->minor_code);
1966 if ((!ret) || (cqp_request->major_code)) {
1967 if (atomic_dec_and_test(&cqp_request->refcount)) {
1968 if (cqp_request->dynamic) {
1969 kfree(cqp_request);
1970 } else {
1971 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1972 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1973 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1976 if (!ret) {
1977 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1978 nescq->hw_cq.cq_number);
1979 ret = -ETIME;
1980 } else {
1981 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1982 nescq->hw_cq.cq_number);
1983 ret = -EIO;
1985 } else {
1986 ret = 0;
1987 if (atomic_dec_and_test(&cqp_request->refcount)) {
1988 if (cqp_request->dynamic) {
1989 kfree(cqp_request);
1990 } else {
1991 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1992 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1993 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1998 if (nescq->cq_mem_size)
1999 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
2000 (void *)nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
2001 kfree(nescq);
2003 return ret;
2008 * nes_reg_mr
2010 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
2011 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
2012 dma_addr_t single_buffer, u16 pbl_count, u16 residual_page_count,
2013 int acc, u64 *iova_start)
2015 struct nes_hw_cqp_wqe *cqp_wqe;
2016 struct nes_cqp_request *cqp_request;
2017 unsigned long flags;
2018 int ret;
2019 struct nes_adapter *nesadapter = nesdev->nesadapter;
2020 /* int count; */
2021 u32 opcode = 0;
2022 u16 major_code;
2024 /* Register the region with the adapter */
2025 cqp_request = nes_get_cqp_request(nesdev);
2026 if (cqp_request == NULL) {
2027 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2028 return -ENOMEM;
2030 cqp_request->waiting = 1;
2031 cqp_wqe = &cqp_request->cqp_wqe;
2033 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2034 /* track PBL resources */
2035 if (pbl_count != 0) {
2036 if (pbl_count > 1) {
2037 /* Two level PBL */
2038 if ((pbl_count+1) > nesadapter->free_4kpbl) {
2039 nes_debug(NES_DBG_MR, "Out of 4KB Pbls for two level request.\n");
2040 if (cqp_request->dynamic) {
2041 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2042 kfree(cqp_request);
2043 } else {
2044 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2045 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2047 return -ENOMEM;
2048 } else {
2049 nesadapter->free_4kpbl -= pbl_count+1;
2051 } else if (residual_page_count > 32) {
2052 if (pbl_count > nesadapter->free_4kpbl) {
2053 nes_debug(NES_DBG_MR, "Out of 4KB Pbls.\n");
2054 if (cqp_request->dynamic) {
2055 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2056 kfree(cqp_request);
2057 } else {
2058 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2059 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2061 return -ENOMEM;
2062 } else {
2063 nesadapter->free_4kpbl -= pbl_count;
2065 } else {
2066 if (pbl_count > nesadapter->free_256pbl) {
2067 nes_debug(NES_DBG_MR, "Out of 256B Pbls.\n");
2068 if (cqp_request->dynamic) {
2069 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2070 kfree(cqp_request);
2071 } else {
2072 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2073 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2075 return -ENOMEM;
2076 } else {
2077 nesadapter->free_256pbl -= pbl_count;
2082 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2084 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2085 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2086 if (acc & IB_ACCESS_LOCAL_WRITE)
2087 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2088 if (acc & IB_ACCESS_REMOTE_WRITE)
2089 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2090 if (acc & IB_ACCESS_REMOTE_READ)
2091 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2092 if (acc & IB_ACCESS_MW_BIND)
2093 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2095 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2096 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2097 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2098 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2100 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2101 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2102 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2103 cpu_to_le32(nespd->pd_id & 0x00007fff);
2104 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2106 if (pbl_count == 0) {
2107 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2108 } else {
2109 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2110 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2111 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX,
2112 (((pbl_count - 1) * 4096) + (residual_page_count*8)));
2114 if ((pbl_count > 1) || (residual_page_count > 32))
2115 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2117 barrier();
2119 atomic_set(&cqp_request->refcount, 2);
2120 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2122 /* Wait for CQP */
2123 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2124 NES_EVENT_TIMEOUT);
2125 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2126 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2127 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2128 major_code = cqp_request->major_code;
2129 if (atomic_dec_and_test(&cqp_request->refcount)) {
2130 if (cqp_request->dynamic) {
2131 kfree(cqp_request);
2132 } else {
2133 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2134 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2135 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2138 if (!ret)
2139 return -ETIME;
2140 else if (major_code)
2141 return -EIO;
2142 else
2143 return 0;
2145 return 0;
2150 * nes_reg_phys_mr
2152 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2153 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2154 u64 * iova_start)
2156 u64 region_length;
2157 struct nes_pd *nespd = to_nespd(ib_pd);
2158 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2159 struct nes_device *nesdev = nesvnic->nesdev;
2160 struct nes_adapter *nesadapter = nesdev->nesadapter;
2161 struct nes_mr *nesmr;
2162 struct ib_mr *ibmr;
2163 struct nes_vpbl vpbl;
2164 struct nes_root_vpbl root_vpbl;
2165 u32 stag;
2166 u32 i;
2167 u32 stag_index = 0;
2168 u32 next_stag_index = 0;
2169 u32 driver_key = 0;
2170 u32 root_pbl_index = 0;
2171 u32 cur_pbl_index = 0;
2172 int err = 0, pbl_depth = 0;
2173 int ret = 0;
2174 u16 pbl_count = 0;
2175 u8 single_page = 1;
2176 u8 stag_key = 0;
2178 pbl_depth = 0;
2179 region_length = 0;
2180 vpbl.pbl_vbase = NULL;
2181 root_vpbl.pbl_vbase = NULL;
2182 root_vpbl.pbl_pbase = 0;
2184 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2185 stag_key = (u8)next_stag_index;
2187 driver_key = 0;
2189 next_stag_index >>= 8;
2190 next_stag_index %= nesadapter->max_mr;
2191 if (num_phys_buf > (1024*512)) {
2192 return ERR_PTR(-E2BIG);
2195 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2196 &stag_index, &next_stag_index);
2197 if (err) {
2198 return ERR_PTR(err);
2201 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2202 if (!nesmr) {
2203 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2204 return ERR_PTR(-ENOMEM);
2207 for (i = 0; i < num_phys_buf; i++) {
2209 if ((i & 0x01FF) == 0) {
2210 if (root_pbl_index == 1) {
2211 /* Allocate the root PBL */
2212 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2213 &root_vpbl.pbl_pbase);
2214 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2215 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2216 if (!root_vpbl.pbl_vbase) {
2217 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2218 vpbl.pbl_pbase);
2219 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2220 kfree(nesmr);
2221 return ERR_PTR(-ENOMEM);
2223 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2224 if (!root_vpbl.leaf_vpbl) {
2225 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2226 root_vpbl.pbl_pbase);
2227 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2228 vpbl.pbl_pbase);
2229 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2230 kfree(nesmr);
2231 return ERR_PTR(-ENOMEM);
2233 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2234 root_vpbl.pbl_vbase[0].pa_high =
2235 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2236 root_vpbl.leaf_vpbl[0] = vpbl;
2238 /* Allocate a 4K buffer for the PBL */
2239 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2240 &vpbl.pbl_pbase);
2241 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2242 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2243 if (!vpbl.pbl_vbase) {
2244 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2245 ibmr = ERR_PTR(-ENOMEM);
2246 kfree(nesmr);
2247 goto reg_phys_err;
2249 /* Fill in the root table */
2250 if (1 <= root_pbl_index) {
2251 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2252 cpu_to_le32((u32)vpbl.pbl_pbase);
2253 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2254 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2255 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2257 root_pbl_index++;
2258 cur_pbl_index = 0;
2260 if (buffer_list[i].addr & ~PAGE_MASK) {
2261 /* TODO: Unwind allocated buffers */
2262 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2263 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2264 (unsigned int) buffer_list[i].addr);
2265 ibmr = ERR_PTR(-EINVAL);
2266 kfree(nesmr);
2267 goto reg_phys_err;
2270 if (!buffer_list[i].size) {
2271 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2272 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2273 ibmr = ERR_PTR(-EINVAL);
2274 kfree(nesmr);
2275 goto reg_phys_err;
2278 region_length += buffer_list[i].size;
2279 if ((i != 0) && (single_page)) {
2280 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2281 single_page = 0;
2283 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr);
2284 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2285 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2288 stag = stag_index << 8;
2289 stag |= driver_key;
2290 stag += (u32)stag_key;
2292 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2293 " length = 0x%016lX, index = 0x%08X\n",
2294 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2296 region_length -= (*iova_start)&PAGE_MASK;
2298 /* Make the leaf PBL the root if only one PBL */
2299 if (root_pbl_index == 1) {
2300 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2303 if (single_page) {
2304 pbl_count = 0;
2305 } else {
2306 pbl_count = root_pbl_index;
2308 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2309 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start);
2311 if (ret == 0) {
2312 nesmr->ibmr.rkey = stag;
2313 nesmr->ibmr.lkey = stag;
2314 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2315 ibmr = &nesmr->ibmr;
2316 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2317 nesmr->pbls_used = pbl_count;
2318 if (pbl_count > 1) {
2319 nesmr->pbls_used++;
2321 } else {
2322 kfree(nesmr);
2323 ibmr = ERR_PTR(-ENOMEM);
2326 reg_phys_err:
2327 /* free the resources */
2328 if (root_pbl_index == 1) {
2329 /* single PBL case */
2330 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2331 } else {
2332 for (i=0; i<root_pbl_index; i++) {
2333 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2334 root_vpbl.leaf_vpbl[i].pbl_pbase);
2336 kfree(root_vpbl.leaf_vpbl);
2337 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2338 root_vpbl.pbl_pbase);
2341 return ibmr;
2346 * nes_get_dma_mr
2348 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2350 struct ib_phys_buf bl;
2351 u64 kva = 0;
2353 nes_debug(NES_DBG_MR, "\n");
2355 bl.size = (u64)0xffffffffffULL;
2356 bl.addr = 0;
2357 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2362 * nes_reg_user_mr
2364 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2365 u64 virt, int acc, struct ib_udata *udata)
2367 u64 iova_start;
2368 __le64 *pbl;
2369 u64 region_length;
2370 dma_addr_t last_dma_addr = 0;
2371 dma_addr_t first_dma_addr = 0;
2372 struct nes_pd *nespd = to_nespd(pd);
2373 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2374 struct nes_device *nesdev = nesvnic->nesdev;
2375 struct nes_adapter *nesadapter = nesdev->nesadapter;
2376 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2377 struct ib_umem_chunk *chunk;
2378 struct nes_ucontext *nes_ucontext;
2379 struct nes_pbl *nespbl;
2380 struct nes_mr *nesmr;
2381 struct ib_umem *region;
2382 struct nes_mem_reg_req req;
2383 struct nes_vpbl vpbl;
2384 struct nes_root_vpbl root_vpbl;
2385 int nmap_index, page_index;
2386 int page_count = 0;
2387 int err, pbl_depth = 0;
2388 int chunk_pages;
2389 int ret;
2390 u32 stag;
2391 u32 stag_index = 0;
2392 u32 next_stag_index;
2393 u32 driver_key;
2394 u32 root_pbl_index = 0;
2395 u32 cur_pbl_index = 0;
2396 u32 skip_pages;
2397 u16 pbl_count;
2398 u8 single_page = 1;
2399 u8 stag_key;
2401 region = ib_umem_get(pd->uobject->context, start, length, acc);
2402 if (IS_ERR(region)) {
2403 return (struct ib_mr *)region;
2406 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2407 " offset = %u, page size = %u.\n",
2408 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2409 region->offset, region->page_size);
2411 skip_pages = ((u32)region->offset) >> 12;
2413 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2414 return ERR_PTR(-EFAULT);
2415 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2417 switch (req.reg_type) {
2418 case IWNES_MEMREG_TYPE_MEM:
2419 pbl_depth = 0;
2420 region_length = 0;
2421 vpbl.pbl_vbase = NULL;
2422 root_vpbl.pbl_vbase = NULL;
2423 root_vpbl.pbl_pbase = 0;
2425 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2426 stag_key = (u8)next_stag_index;
2428 driver_key = next_stag_index & 0x70000000;
2430 next_stag_index >>= 8;
2431 next_stag_index %= nesadapter->max_mr;
2433 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2434 nesadapter->max_mr, &stag_index, &next_stag_index);
2435 if (err) {
2436 ib_umem_release(region);
2437 return ERR_PTR(err);
2440 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2441 if (!nesmr) {
2442 ib_umem_release(region);
2443 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2444 return ERR_PTR(-ENOMEM);
2446 nesmr->region = region;
2448 list_for_each_entry(chunk, &region->chunk_list, list) {
2449 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2450 chunk->nents, chunk->nmap);
2451 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2452 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2453 ib_umem_release(region);
2454 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2455 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2456 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2457 ibmr = ERR_PTR(-EINVAL);
2458 kfree(nesmr);
2459 goto reg_user_mr_err;
2462 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2463 ib_umem_release(region);
2464 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2465 stag_index);
2466 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2467 ibmr = ERR_PTR(-EINVAL);
2468 kfree(nesmr);
2469 goto reg_user_mr_err;
2472 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2473 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2474 region_length -= skip_pages << 12;
2475 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2476 skip_pages = 0;
2477 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2478 goto enough_pages;
2479 if ((page_count&0x01FF) == 0) {
2480 if (page_count>(1024*512)) {
2481 ib_umem_release(region);
2482 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2483 vpbl.pbl_pbase);
2484 nes_free_resource(nesadapter,
2485 nesadapter->allocated_mrs, stag_index);
2486 kfree(nesmr);
2487 ibmr = ERR_PTR(-E2BIG);
2488 goto reg_user_mr_err;
2490 if (root_pbl_index == 1) {
2491 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2492 8192, &root_vpbl.pbl_pbase);
2493 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2494 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2495 if (!root_vpbl.pbl_vbase) {
2496 ib_umem_release(region);
2497 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2498 vpbl.pbl_pbase);
2499 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2500 stag_index);
2501 kfree(nesmr);
2502 ibmr = ERR_PTR(-ENOMEM);
2503 goto reg_user_mr_err;
2505 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2506 GFP_KERNEL);
2507 if (!root_vpbl.leaf_vpbl) {
2508 ib_umem_release(region);
2509 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2510 root_vpbl.pbl_pbase);
2511 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2512 vpbl.pbl_pbase);
2513 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2514 stag_index);
2515 kfree(nesmr);
2516 ibmr = ERR_PTR(-ENOMEM);
2517 goto reg_user_mr_err;
2519 root_vpbl.pbl_vbase[0].pa_low =
2520 cpu_to_le32((u32)vpbl.pbl_pbase);
2521 root_vpbl.pbl_vbase[0].pa_high =
2522 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2523 root_vpbl.leaf_vpbl[0] = vpbl;
2525 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2526 &vpbl.pbl_pbase);
2527 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2528 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2529 if (!vpbl.pbl_vbase) {
2530 ib_umem_release(region);
2531 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2532 ibmr = ERR_PTR(-ENOMEM);
2533 kfree(nesmr);
2534 goto reg_user_mr_err;
2536 if (1 <= root_pbl_index) {
2537 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2538 cpu_to_le32((u32)vpbl.pbl_pbase);
2539 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2540 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2541 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2543 root_pbl_index++;
2544 cur_pbl_index = 0;
2546 if (single_page) {
2547 if (page_count != 0) {
2548 if ((last_dma_addr+4096) !=
2549 (sg_dma_address(&chunk->page_list[nmap_index])+
2550 (page_index*4096)))
2551 single_page = 0;
2552 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2553 (page_index*4096);
2554 } else {
2555 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2556 (page_index*4096);
2557 last_dma_addr = first_dma_addr;
2561 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2562 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2563 (page_index*4096)));
2564 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2565 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2566 (page_index*4096))) >> 32)));
2567 cur_pbl_index++;
2568 page_count++;
2572 enough_pages:
2573 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2574 " stag_key=0x%08x\n",
2575 stag_index, driver_key, stag_key);
2576 stag = stag_index << 8;
2577 stag |= driver_key;
2578 stag += (u32)stag_key;
2579 if (stag == 0) {
2580 stag = 1;
2583 iova_start = virt;
2584 /* Make the leaf PBL the root if only one PBL */
2585 if (root_pbl_index == 1) {
2586 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2589 if (single_page) {
2590 pbl_count = 0;
2591 } else {
2592 pbl_count = root_pbl_index;
2593 first_dma_addr = 0;
2595 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2596 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2597 stag, (unsigned int)iova_start,
2598 (unsigned int)region_length, stag_index,
2599 (unsigned long long)region->length, pbl_count);
2600 ret = nes_reg_mr( nesdev, nespd, stag, region->length, &root_vpbl,
2601 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc, &iova_start);
2603 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2605 if (ret == 0) {
2606 nesmr->ibmr.rkey = stag;
2607 nesmr->ibmr.lkey = stag;
2608 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2609 ibmr = &nesmr->ibmr;
2610 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2611 nesmr->pbls_used = pbl_count;
2612 if (pbl_count > 1) {
2613 nesmr->pbls_used++;
2615 } else {
2616 ib_umem_release(region);
2617 kfree(nesmr);
2618 ibmr = ERR_PTR(-ENOMEM);
2621 reg_user_mr_err:
2622 /* free the resources */
2623 if (root_pbl_index == 1) {
2624 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2625 vpbl.pbl_pbase);
2626 } else {
2627 for (page_index=0; page_index<root_pbl_index; page_index++) {
2628 pci_free_consistent(nesdev->pcidev, 4096,
2629 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2630 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2632 kfree(root_vpbl.leaf_vpbl);
2633 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2634 root_vpbl.pbl_pbase);
2637 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2639 return ibmr;
2640 break;
2641 case IWNES_MEMREG_TYPE_QP:
2642 case IWNES_MEMREG_TYPE_CQ:
2643 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2644 if (!nespbl) {
2645 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2646 ib_umem_release(region);
2647 return ERR_PTR(-ENOMEM);
2649 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2650 if (!nesmr) {
2651 ib_umem_release(region);
2652 kfree(nespbl);
2653 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2654 return ERR_PTR(-ENOMEM);
2656 nesmr->region = region;
2657 nes_ucontext = to_nesucontext(pd->uobject->context);
2658 pbl_depth = region->length >> 12;
2659 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2660 nespbl->pbl_size = pbl_depth*sizeof(u64);
2661 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2662 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2663 } else {
2664 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2667 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2668 nespbl->pbl_size, pbl_depth);
2669 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2670 &nespbl->pbl_pbase);
2671 if (!pbl) {
2672 ib_umem_release(region);
2673 kfree(nesmr);
2674 kfree(nespbl);
2675 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2676 return ERR_PTR(-ENOMEM);
2679 nespbl->pbl_vbase = (u64 *)pbl;
2680 nespbl->user_base = start;
2681 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%p,"
2682 " pbl_vbase=%p user_base=0x%lx\n",
2683 nespbl->pbl_size, (void *)nespbl->pbl_pbase,
2684 (void*)nespbl->pbl_vbase, nespbl->user_base);
2686 list_for_each_entry(chunk, &region->chunk_list, list) {
2687 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2688 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2689 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2690 nespbl->page = sg_page(&chunk->page_list[0]);
2691 for (page_index=0; page_index<chunk_pages; page_index++) {
2692 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2693 (sg_dma_address(&chunk->page_list[nmap_index])+
2694 (page_index*4096)));
2695 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2696 (sg_dma_address(&chunk->page_list[nmap_index])+
2697 (page_index*4096)))>>32);
2698 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2699 (unsigned long long)*pbl,
2700 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2701 pbl++;
2705 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2706 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2707 } else {
2708 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2710 nesmr->ibmr.rkey = -1;
2711 nesmr->ibmr.lkey = -1;
2712 nesmr->mode = req.reg_type;
2713 return &nesmr->ibmr;
2714 break;
2717 return ERR_PTR(-ENOSYS);
2722 * nes_dereg_mr
2724 static int nes_dereg_mr(struct ib_mr *ib_mr)
2726 struct nes_mr *nesmr = to_nesmr(ib_mr);
2727 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2728 struct nes_device *nesdev = nesvnic->nesdev;
2729 struct nes_adapter *nesadapter = nesdev->nesadapter;
2730 struct nes_hw_cqp_wqe *cqp_wqe;
2731 struct nes_cqp_request *cqp_request;
2732 unsigned long flags;
2733 int ret;
2734 u16 major_code;
2735 u16 minor_code;
2737 if (nesmr->region) {
2738 ib_umem_release(nesmr->region);
2740 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2741 kfree(nesmr);
2742 return 0;
2745 /* Deallocate the region with the adapter */
2747 cqp_request = nes_get_cqp_request(nesdev);
2748 if (cqp_request == NULL) {
2749 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2750 return -ENOMEM;
2752 cqp_request->waiting = 1;
2753 cqp_wqe = &cqp_request->cqp_wqe;
2755 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2756 if (nesmr->pbls_used != 0) {
2757 if (nesmr->pbl_4k) {
2758 nesadapter->free_4kpbl += nesmr->pbls_used;
2759 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
2760 printk(KERN_ERR PFX "free 4KB PBLs(%u) has exceeded the max(%u)\n",
2761 nesadapter->free_4kpbl, nesadapter->max_4kpbl);
2763 } else {
2764 nesadapter->free_256pbl += nesmr->pbls_used;
2765 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
2766 printk(KERN_ERR PFX "free 256B PBLs(%u) has exceeded the max(%u)\n",
2767 nesadapter->free_256pbl, nesadapter->max_256pbl);
2772 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2773 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2774 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2775 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2776 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2777 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2779 atomic_set(&cqp_request->refcount, 2);
2780 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2782 /* Wait for CQP */
2783 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2784 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2785 NES_EVENT_TIMEOUT);
2786 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2787 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2788 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2790 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2791 (ib_mr->rkey & 0x0fffff00) >> 8);
2793 kfree(nesmr);
2795 major_code = cqp_request->major_code;
2796 minor_code = cqp_request->minor_code;
2797 if (atomic_dec_and_test(&cqp_request->refcount)) {
2798 if (cqp_request->dynamic) {
2799 kfree(cqp_request);
2800 } else {
2801 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2802 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2803 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2806 if (!ret) {
2807 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2808 " ib_mr=%p, rkey = 0x%08X\n",
2809 ib_mr, ib_mr->rkey);
2810 return -ETIME;
2811 } else if (major_code) {
2812 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2813 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2814 major_code, minor_code, ib_mr, ib_mr->rkey);
2815 return -EIO;
2816 } else
2817 return 0;
2822 * show_rev
2824 static ssize_t show_rev(struct class_device *cdev, char *buf)
2826 struct nes_ib_device *nesibdev =
2827 container_of(cdev, struct nes_ib_device, ibdev.class_dev);
2828 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2830 nes_debug(NES_DBG_INIT, "\n");
2831 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2836 * show_fw_ver
2838 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
2840 struct nes_ib_device *nesibdev =
2841 container_of(cdev, struct nes_ib_device, ibdev.class_dev);
2842 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2844 nes_debug(NES_DBG_INIT, "\n");
2845 return sprintf(buf, "%x.%x.%x\n",
2846 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 32),
2847 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 16) & 0xffff,
2848 (int)(nesvnic->nesdev->nesadapter->fw_ver & 0xffff));
2853 * show_hca
2855 static ssize_t show_hca(struct class_device *cdev, char *buf)
2857 nes_debug(NES_DBG_INIT, "\n");
2858 return sprintf(buf, "NES020\n");
2863 * show_board
2865 static ssize_t show_board(struct class_device *cdev, char *buf)
2867 nes_debug(NES_DBG_INIT, "\n");
2868 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2872 static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2873 static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2874 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2875 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2877 static struct class_device_attribute *nes_class_attributes[] = {
2878 &class_device_attr_hw_rev,
2879 &class_device_attr_fw_ver,
2880 &class_device_attr_hca_type,
2881 &class_device_attr_board_id
2886 * nes_query_qp
2888 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2889 int attr_mask, struct ib_qp_init_attr *init_attr)
2891 struct nes_qp *nesqp = to_nesqp(ibqp);
2893 nes_debug(NES_DBG_QP, "\n");
2895 attr->qp_access_flags = 0;
2896 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2897 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2898 attr->cap.max_recv_sge = 1;
2899 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2900 init_attr->cap.max_inline_data = 0;
2901 } else {
2902 init_attr->cap.max_inline_data = 64;
2905 init_attr->event_handler = nesqp->ibqp.event_handler;
2906 init_attr->qp_context = nesqp->ibqp.qp_context;
2907 init_attr->send_cq = nesqp->ibqp.send_cq;
2908 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2909 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2910 init_attr->cap = attr->cap;
2912 return 0;
2917 * nes_hw_modify_qp
2919 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2920 u32 next_iwarp_state, u32 wait_completion)
2922 struct nes_hw_cqp_wqe *cqp_wqe;
2923 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2924 /* struct iw_cm_event cm_event; */
2925 struct nes_cqp_request *cqp_request;
2926 unsigned long flags;
2927 int ret;
2928 u16 major_code;
2930 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2931 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2933 cqp_request = nes_get_cqp_request(nesdev);
2934 if (cqp_request == NULL) {
2935 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2936 return -ENOMEM;
2938 if (wait_completion) {
2939 cqp_request->waiting = 1;
2940 } else {
2941 cqp_request->waiting = 0;
2943 cqp_wqe = &cqp_request->cqp_wqe;
2945 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2946 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2947 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2948 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2949 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2950 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2951 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2953 atomic_set(&cqp_request->refcount, 2);
2954 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2956 /* Wait for CQP */
2957 if (wait_completion) {
2958 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2959 nesqp->hwqp.qp_id); */
2960 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2961 NES_EVENT_TIMEOUT);
2962 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2963 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2964 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2965 major_code = cqp_request->major_code;
2966 if (major_code) {
2967 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2968 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2969 nesqp->hwqp.qp_id, cqp_request->major_code,
2970 cqp_request->minor_code, next_iwarp_state);
2972 if (atomic_dec_and_test(&cqp_request->refcount)) {
2973 if (cqp_request->dynamic) {
2974 kfree(cqp_request);
2975 } else {
2976 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2977 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2978 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2981 if (!ret)
2982 return -ETIME;
2983 else if (major_code)
2984 return -EIO;
2985 else
2986 return 0;
2987 } else {
2988 return 0;
2994 * nes_modify_qp
2996 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2997 int attr_mask, struct ib_udata *udata)
2999 struct nes_qp *nesqp = to_nesqp(ibqp);
3000 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3001 struct nes_device *nesdev = nesvnic->nesdev;
3002 /* u32 cqp_head; */
3003 /* u32 counter; */
3004 u32 next_iwarp_state = 0;
3005 int err;
3006 unsigned long qplockflags;
3007 int ret;
3008 u16 original_last_aeq;
3009 u8 issue_modify_qp = 0;
3010 u8 issue_disconnect = 0;
3011 u8 dont_wait = 0;
3013 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
3014 " iwarp_state=0x%X, refcount=%d\n",
3015 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
3016 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
3018 nes_add_ref(&nesqp->ibqp);
3019 spin_lock_irqsave(&nesqp->lock, qplockflags);
3021 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
3022 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
3023 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
3024 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
3026 if (attr_mask & IB_QP_STATE) {
3027 switch (attr->qp_state) {
3028 case IB_QPS_INIT:
3029 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
3030 nesqp->hwqp.qp_id);
3031 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3032 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3033 nes_rem_ref(&nesqp->ibqp);
3034 return -EINVAL;
3036 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3037 issue_modify_qp = 1;
3038 break;
3039 case IB_QPS_RTR:
3040 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
3041 nesqp->hwqp.qp_id);
3042 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3043 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3044 nes_rem_ref(&nesqp->ibqp);
3045 return -EINVAL;
3047 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3048 issue_modify_qp = 1;
3049 break;
3050 case IB_QPS_RTS:
3051 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
3052 nesqp->hwqp.qp_id);
3053 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
3054 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3055 nes_rem_ref(&nesqp->ibqp);
3056 return -EINVAL;
3058 if (nesqp->cm_id == NULL) {
3059 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
3060 nesqp->hwqp.qp_id );
3061 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3062 nes_rem_ref(&nesqp->ibqp);
3063 return -EINVAL;
3065 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
3066 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
3067 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
3068 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
3069 issue_modify_qp = 1;
3070 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
3071 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3072 nesqp->hte_added = 1;
3073 break;
3074 case IB_QPS_SQD:
3075 issue_modify_qp = 1;
3076 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3077 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3078 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3079 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3080 nes_rem_ref(&nesqp->ibqp);
3081 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);
3088 nes_rem_ref(&nesqp->ibqp);
3089 return -EINVAL;
3091 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3092 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3093 " already done based on hw state.\n",
3094 nesqp->hwqp.qp_id);
3095 issue_modify_qp = 0;
3096 nesqp->in_disconnect = 0;
3098 switch (nesqp->hw_iwarp_state) {
3099 case NES_AEQE_IWARP_STATE_CLOSING:
3100 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3101 case NES_AEQE_IWARP_STATE_TERMINATE:
3102 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3103 break;
3104 case NES_AEQE_IWARP_STATE_ERROR:
3105 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3106 break;
3107 default:
3108 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3109 nesqp->in_disconnect = 1;
3110 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3111 break;
3114 break;
3115 case IB_QPS_SQE:
3116 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3117 nesqp->hwqp.qp_id);
3118 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3119 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3120 nes_rem_ref(&nesqp->ibqp);
3121 return -EINVAL;
3123 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3124 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3125 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3126 issue_modify_qp = 1;
3127 nesqp->in_disconnect = 1;
3128 break;
3129 case IB_QPS_ERR:
3130 case IB_QPS_RESET:
3131 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3132 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3133 nes_rem_ref(&nesqp->ibqp);
3134 return -EINVAL;
3136 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3137 nesqp->hwqp.qp_id);
3138 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;
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;
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);
3159 nes_rem_ref(&nesqp->ibqp);
3160 return -EINVAL;
3161 break;
3164 nesqp->ibqp_state = attr->qp_state;
3165 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3166 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3167 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3168 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3169 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3170 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3171 nesqp->iwarp_state);
3172 issue_disconnect = 1;
3173 } else {
3174 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3175 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3176 nesqp->iwarp_state);
3180 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3181 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3182 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3183 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3184 issue_modify_qp = 1;
3186 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3187 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3188 issue_modify_qp = 1;
3190 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3191 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3192 issue_modify_qp = 1;
3194 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3195 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3196 issue_modify_qp = 1;
3199 if (nesqp->user_mode) {
3200 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3201 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3202 issue_modify_qp = 1;
3206 original_last_aeq = nesqp->last_aeq;
3207 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3209 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3211 ret = 0;
3214 if (issue_modify_qp) {
3215 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3216 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 1);
3217 if (ret)
3218 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3219 " failed for QP%u.\n",
3220 next_iwarp_state, nesqp->hwqp.qp_id);
3224 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3225 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3226 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3227 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3228 original_last_aeq, nesqp->last_aeq);
3229 if ((!ret) ||
3230 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3231 (ret))) {
3232 if (dont_wait) {
3233 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3234 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3235 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3236 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3237 original_last_aeq, nesqp->last_aeq);
3238 /* this one is for the cm_disconnect thread */
3239 nes_add_ref(&nesqp->ibqp);
3240 spin_lock_irqsave(&nesqp->lock, qplockflags);
3241 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3242 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3243 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3244 nes_cm_disconn(nesqp);
3245 } else {
3246 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3247 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3248 nes_rem_ref(&nesqp->ibqp);
3250 } else {
3251 spin_lock_irqsave(&nesqp->lock, qplockflags);
3252 if (nesqp->cm_id) {
3253 /* These two are for the timer thread */
3254 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3255 nes_add_ref(&nesqp->ibqp);
3256 nesqp->cm_id->add_ref(nesqp->cm_id);
3257 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3258 " need ae to finish up, original_last_aeq = 0x%04X."
3259 " last_aeq = 0x%04X, scheduling timer.\n",
3260 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3261 original_last_aeq, nesqp->last_aeq);
3262 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3264 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3265 } else {
3266 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3267 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3268 " need ae to finish up, original_last_aeq = 0x%04X."
3269 " last_aeq = 0x%04X.\n",
3270 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3271 original_last_aeq, nesqp->last_aeq);
3274 } else {
3275 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3276 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3277 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3278 original_last_aeq, nesqp->last_aeq);
3279 nes_rem_ref(&nesqp->ibqp);
3281 } else {
3282 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3283 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3284 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3285 original_last_aeq, nesqp->last_aeq);
3286 nes_rem_ref(&nesqp->ibqp);
3289 err = 0;
3291 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3292 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3294 return err;
3299 * nes_muticast_attach
3301 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3303 nes_debug(NES_DBG_INIT, "\n");
3304 return -ENOSYS;
3309 * nes_multicast_detach
3311 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3313 nes_debug(NES_DBG_INIT, "\n");
3314 return -ENOSYS;
3319 * nes_process_mad
3321 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3322 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3323 struct ib_mad *in_mad, struct ib_mad *out_mad)
3325 nes_debug(NES_DBG_INIT, "\n");
3326 return -ENOSYS;
3329 static inline void
3330 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3332 int sge_index;
3333 int total_payload_length = 0;
3334 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3335 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3336 ib_wr->sg_list[sge_index].addr);
3337 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3338 ib_wr->sg_list[sge_index].length);
3339 if (uselkey)
3340 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3341 (ib_wr->sg_list[sge_index].lkey));
3342 else
3343 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3345 total_payload_length += ib_wr->sg_list[sge_index].length;
3347 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3348 total_payload_length);
3349 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3350 total_payload_length);
3354 * nes_post_send
3356 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3357 struct ib_send_wr **bad_wr)
3359 u64 u64temp;
3360 unsigned long flags = 0;
3361 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3362 struct nes_device *nesdev = nesvnic->nesdev;
3363 struct nes_qp *nesqp = to_nesqp(ibqp);
3364 struct nes_hw_qp_wqe *wqe;
3365 int err;
3366 u32 qsize = nesqp->hwqp.sq_size;
3367 u32 head;
3368 u32 wqe_misc;
3369 u32 wqe_count;
3370 u32 counter;
3371 u32 total_payload_length;
3373 err = 0;
3374 wqe_misc = 0;
3375 wqe_count = 0;
3376 total_payload_length = 0;
3378 if (nesqp->ibqp_state > IB_QPS_RTS)
3379 return -EINVAL;
3381 spin_lock_irqsave(&nesqp->lock, flags);
3383 head = nesqp->hwqp.sq_head;
3385 while (ib_wr) {
3386 /* Check for SQ overflow */
3387 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3388 err = -EINVAL;
3389 break;
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;
3406 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3407 err = -EINVAL;
3408 break;
3410 if (ib_wr->send_flags & IB_SEND_FENCE) {
3411 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
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);
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;
3435 if (ib_wr->send_flags & IB_SEND_FENCE) {
3436 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
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);
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);
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;
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;
3484 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3485 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3487 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3489 ib_wr = ib_wr->next;
3490 head++;
3491 wqe_count++;
3492 if (head >= qsize)
3493 head = 0;
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);
3506 spin_unlock_irqrestore(&nesqp->lock, flags);
3508 if (err)
3509 *bad_wr = ib_wr;
3510 return err;
3515 * nes_post_recv
3517 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3518 struct ib_recv_wr **bad_wr)
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;
3534 if (nesqp->ibqp_state > IB_QPS_RTS)
3535 return -EINVAL;
3537 spin_lock_irqsave(&nesqp->lock, flags);
3539 head = nesqp->hwqp.rq_head;
3541 while (ib_wr) {
3542 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3543 err = -EINVAL;
3544 break;
3546 /* Check for RQ overflow */
3547 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3548 err = -EINVAL;
3549 break;
3552 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3553 wqe = &nesqp->hwqp.rq_vbase[head];
3555 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3556 nesqp->hwqp.qp_id, wqe, head); */
3557 nes_fill_init_qp_wqe(wqe, nesqp, head);
3558 u64temp = (u64)(ib_wr->wr_id);
3559 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3560 u64temp);
3561 total_payload_length = 0;
3562 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3563 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3564 ib_wr->sg_list[sge_index].addr);
3565 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3566 ib_wr->sg_list[sge_index].length);
3567 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3568 ib_wr->sg_list[sge_index].lkey);
3570 total_payload_length += ib_wr->sg_list[sge_index].length;
3572 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3573 total_payload_length);
3575 ib_wr = ib_wr->next;
3576 head++;
3577 wqe_count++;
3578 if (head >= qsize)
3579 head = 0;
3582 nesqp->hwqp.rq_head = head;
3583 barrier();
3584 while (wqe_count) {
3585 counter = min(wqe_count, ((u32)255));
3586 wqe_count -= counter;
3587 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3590 spin_unlock_irqrestore(&nesqp->lock, flags);
3592 if (err)
3593 *bad_wr = ib_wr;
3594 return err;
3599 * nes_poll_cq
3601 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3603 u64 u64temp;
3604 u64 wrid;
3605 /* u64 u64temp; */
3606 unsigned long flags = 0;
3607 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3608 struct nes_device *nesdev = nesvnic->nesdev;
3609 struct nes_cq *nescq = to_nescq(ibcq);
3610 struct nes_qp *nesqp;
3611 struct nes_hw_cqe cqe;
3612 u32 head;
3613 u32 wq_tail;
3614 u32 cq_size;
3615 u32 cqe_count = 0;
3616 u32 wqe_index;
3617 u32 u32temp;
3618 /* u32 counter; */
3620 nes_debug(NES_DBG_CQ, "\n");
3622 spin_lock_irqsave(&nescq->lock, flags);
3624 head = nescq->hw_cq.cq_head;
3625 cq_size = nescq->hw_cq.cq_size;
3627 while (cqe_count < num_entries) {
3628 if (le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3629 NES_CQE_VALID) {
3630 cqe = nescq->hw_cq.cq_vbase[head];
3631 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3632 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3633 wqe_index = u32temp &
3634 (nesdev->nesadapter->max_qp_wr - 1);
3635 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3636 /* parse CQE, get completion context from WQE (either rq or sq */
3637 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3638 ((u64)u32temp);
3639 nesqp = *((struct nes_qp **)&u64temp);
3640 memset(entry, 0, sizeof *entry);
3641 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3642 entry->status = IB_WC_SUCCESS;
3643 } else {
3644 entry->status = IB_WC_WR_FLUSH_ERR;
3647 entry->qp = &nesqp->ibqp;
3648 entry->src_qp = nesqp->hwqp.qp_id;
3650 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3651 if (nesqp->skip_lsmm) {
3652 nesqp->skip_lsmm = 0;
3653 wq_tail = nesqp->hwqp.sq_tail++;
3656 /* Working on a SQ Completion*/
3657 wq_tail = wqe_index;
3658 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3659 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3660 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3661 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3662 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3663 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3664 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3666 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3667 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3668 case NES_IWARP_SQ_OP_RDMAW:
3669 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3670 entry->opcode = IB_WC_RDMA_WRITE;
3671 break;
3672 case NES_IWARP_SQ_OP_RDMAR:
3673 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3674 entry->opcode = IB_WC_RDMA_READ;
3675 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3676 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3677 break;
3678 case NES_IWARP_SQ_OP_SENDINV:
3679 case NES_IWARP_SQ_OP_SENDSEINV:
3680 case NES_IWARP_SQ_OP_SEND:
3681 case NES_IWARP_SQ_OP_SENDSE:
3682 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3683 entry->opcode = IB_WC_SEND;
3684 break;
3686 } else {
3687 /* Working on a RQ Completion*/
3688 wq_tail = wqe_index;
3689 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3690 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3691 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3692 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3693 entry->opcode = IB_WC_RECV;
3695 entry->wr_id = wrid;
3697 if (++head >= cq_size)
3698 head = 0;
3699 cqe_count++;
3700 nescq->polled_completions++;
3701 if ((nescq->polled_completions > (cq_size / 2)) ||
3702 (nescq->polled_completions == 255)) {
3703 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3704 " are pending %u of %u.\n",
3705 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3706 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3707 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3708 nescq->polled_completions = 0;
3710 entry++;
3711 } else
3712 break;
3715 if (nescq->polled_completions) {
3716 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3717 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3718 nescq->polled_completions = 0;
3721 nescq->hw_cq.cq_head = head;
3722 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3723 cqe_count, nescq->hw_cq.cq_number);
3725 spin_unlock_irqrestore(&nescq->lock, flags);
3727 return cqe_count;
3732 * nes_req_notify_cq
3734 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3736 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3737 struct nes_device *nesdev = nesvnic->nesdev;
3738 struct nes_cq *nescq = to_nescq(ibcq);
3739 u32 cq_arm;
3741 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3742 nescq->hw_cq.cq_number);
3744 cq_arm = nescq->hw_cq.cq_number;
3745 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3746 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3747 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3748 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3749 else
3750 return -EINVAL;
3752 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3753 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3755 return 0;
3760 * nes_init_ofa_device
3762 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3764 struct nes_ib_device *nesibdev;
3765 struct nes_vnic *nesvnic = netdev_priv(netdev);
3766 struct nes_device *nesdev = nesvnic->nesdev;
3768 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3769 if (nesibdev == NULL) {
3770 return NULL;
3772 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3773 nesibdev->ibdev.owner = THIS_MODULE;
3775 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3776 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3777 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3779 nesibdev->ibdev.uverbs_cmd_mask =
3780 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3781 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3782 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3783 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3784 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3785 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3786 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3787 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3788 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3789 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3790 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3791 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3792 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3793 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3794 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3795 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3796 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3797 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3798 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3799 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3800 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3801 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3803 nesibdev->ibdev.phys_port_cnt = 1;
3804 nesibdev->ibdev.num_comp_vectors = 1;
3805 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3806 nesibdev->ibdev.class_dev.dev = &nesdev->pcidev->dev;
3807 nesibdev->ibdev.query_device = nes_query_device;
3808 nesibdev->ibdev.query_port = nes_query_port;
3809 nesibdev->ibdev.modify_port = nes_modify_port;
3810 nesibdev->ibdev.query_pkey = nes_query_pkey;
3811 nesibdev->ibdev.query_gid = nes_query_gid;
3812 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3813 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3814 nesibdev->ibdev.mmap = nes_mmap;
3815 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3816 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3817 nesibdev->ibdev.create_ah = nes_create_ah;
3818 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3819 nesibdev->ibdev.create_qp = nes_create_qp;
3820 nesibdev->ibdev.modify_qp = nes_modify_qp;
3821 nesibdev->ibdev.query_qp = nes_query_qp;
3822 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3823 nesibdev->ibdev.create_cq = nes_create_cq;
3824 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3825 nesibdev->ibdev.poll_cq = nes_poll_cq;
3826 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3827 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3828 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3829 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3830 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3831 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3832 nesibdev->ibdev.bind_mw = nes_bind_mw;
3834 nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3835 nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3836 nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3837 nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3839 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3840 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3841 nesibdev->ibdev.process_mad = nes_process_mad;
3843 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3844 nesibdev->ibdev.post_send = nes_post_send;
3845 nesibdev->ibdev.post_recv = nes_post_recv;
3847 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3848 if (nesibdev->ibdev.iwcm == NULL) {
3849 ib_dealloc_device(&nesibdev->ibdev);
3850 return NULL;
3852 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3853 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3854 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3855 nesibdev->ibdev.iwcm->connect = nes_connect;
3856 nesibdev->ibdev.iwcm->accept = nes_accept;
3857 nesibdev->ibdev.iwcm->reject = nes_reject;
3858 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3859 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3861 return nesibdev;
3866 * nes_destroy_ofa_device
3868 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3870 if (nesibdev == NULL)
3871 return;
3873 nes_unregister_ofa_device(nesibdev);
3875 kfree(nesibdev->ibdev.iwcm);
3876 ib_dealloc_device(&nesibdev->ibdev);
3881 * nes_register_ofa_device
3883 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3885 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3886 struct nes_device *nesdev = nesvnic->nesdev;
3887 struct nes_adapter *nesadapter = nesdev->nesadapter;
3888 int i, ret;
3890 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3891 if (ret) {
3892 return ret;
3895 /* Get the resources allocated to this device */
3896 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3897 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3898 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3899 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3901 for (i = 0; i < ARRAY_SIZE(nes_class_attributes); ++i) {
3902 ret = class_device_create_file(&nesibdev->ibdev.class_dev, nes_class_attributes[i]);
3903 if (ret) {
3904 while (i > 0) {
3905 i--;
3906 class_device_remove_file(&nesibdev->ibdev.class_dev,
3907 nes_class_attributes[i]);
3909 ib_unregister_device(&nesibdev->ibdev);
3910 return ret;
3914 nesvnic->of_device_registered = 1;
3916 return 0;
3921 * nes_unregister_ofa_device
3923 void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3925 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3926 int i;
3928 if (nesibdev == NULL)
3929 return;
3931 for (i = 0; i < ARRAY_SIZE(nes_class_attributes); ++i) {
3932 class_device_remove_file(&nesibdev->ibdev.class_dev, nes_class_attributes[i]);
3935 if (nesvnic->of_device_registered) {
3936 ib_unregister_device(&nesibdev->ibdev);
3939 nesvnic->of_device_registered = 0;