Linux 3.11-rc3
[cris-mirror.git] / drivers / infiniband / hw / nes / nes_verbs.c
blob8f67fe2e91e623a87878a8790ff9f2e28db0c0b8
1 /*
2 * Copyright (c) 2006 - 2011 Intel Corporation. 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 <linux/slab.h>
39 #include <asm/byteorder.h>
41 #include <rdma/ib_verbs.h>
42 #include <rdma/iw_cm.h>
43 #include <rdma/ib_user_verbs.h>
45 #include "nes.h"
47 #include <rdma/ib_umem.h>
49 atomic_t mod_qp_timouts;
50 atomic_t qps_created;
51 atomic_t sw_qps_destroyed;
53 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
55 /**
56 * nes_alloc_mw
58 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type)
60 struct nes_pd *nespd = to_nespd(ibpd);
61 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
62 struct nes_device *nesdev = nesvnic->nesdev;
63 struct nes_adapter *nesadapter = nesdev->nesadapter;
64 struct nes_cqp_request *cqp_request;
65 struct nes_mr *nesmr;
66 struct ib_mw *ibmw;
67 struct nes_hw_cqp_wqe *cqp_wqe;
68 int ret;
69 u32 stag;
70 u32 stag_index = 0;
71 u32 next_stag_index = 0;
72 u32 driver_key = 0;
73 u8 stag_key = 0;
75 if (type != IB_MW_TYPE_1)
76 return ERR_PTR(-EINVAL);
78 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
79 stag_key = (u8)next_stag_index;
81 driver_key = 0;
83 next_stag_index >>= 8;
84 next_stag_index %= nesadapter->max_mr;
86 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
87 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
88 if (ret) {
89 return ERR_PTR(ret);
92 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
93 if (!nesmr) {
94 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
95 return ERR_PTR(-ENOMEM);
98 stag = stag_index << 8;
99 stag |= driver_key;
100 stag += (u32)stag_key;
102 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
103 stag, stag_index);
105 /* Register the region with the adapter */
106 cqp_request = nes_get_cqp_request(nesdev);
107 if (cqp_request == NULL) {
108 kfree(nesmr);
109 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
110 return ERR_PTR(-ENOMEM);
113 cqp_request->waiting = 1;
114 cqp_wqe = &cqp_request->cqp_wqe;
116 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
117 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
118 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
119 NES_CQP_STAG_REM_ACC_EN);
121 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
122 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
123 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
125 atomic_set(&cqp_request->refcount, 2);
126 nes_post_cqp_request(nesdev, cqp_request);
128 /* Wait for CQP */
129 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
130 NES_EVENT_TIMEOUT);
131 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
132 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
133 stag, ret, cqp_request->major_code, cqp_request->minor_code);
134 if ((!ret) || (cqp_request->major_code)) {
135 nes_put_cqp_request(nesdev, cqp_request);
136 kfree(nesmr);
137 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
138 if (!ret) {
139 return ERR_PTR(-ETIME);
140 } else {
141 return ERR_PTR(-ENOMEM);
144 nes_put_cqp_request(nesdev, cqp_request);
146 nesmr->ibmw.rkey = stag;
147 nesmr->mode = IWNES_MEMREG_TYPE_MW;
148 ibmw = &nesmr->ibmw;
149 nesmr->pbl_4k = 0;
150 nesmr->pbls_used = 0;
152 return ibmw;
157 * nes_dealloc_mw
159 static int nes_dealloc_mw(struct ib_mw *ibmw)
161 struct nes_mr *nesmr = to_nesmw(ibmw);
162 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
163 struct nes_device *nesdev = nesvnic->nesdev;
164 struct nes_adapter *nesadapter = nesdev->nesadapter;
165 struct nes_hw_cqp_wqe *cqp_wqe;
166 struct nes_cqp_request *cqp_request;
167 int err = 0;
168 int ret;
170 /* Deallocate the window with the adapter */
171 cqp_request = nes_get_cqp_request(nesdev);
172 if (cqp_request == NULL) {
173 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
174 return -ENOMEM;
176 cqp_request->waiting = 1;
177 cqp_wqe = &cqp_request->cqp_wqe;
178 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
179 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
180 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
182 atomic_set(&cqp_request->refcount, 2);
183 nes_post_cqp_request(nesdev, cqp_request);
185 /* Wait for CQP */
186 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
187 ibmw->rkey);
188 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
189 NES_EVENT_TIMEOUT);
190 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
191 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
192 ret, cqp_request->major_code, cqp_request->minor_code);
193 if (!ret)
194 err = -ETIME;
195 else if (cqp_request->major_code)
196 err = -EIO;
198 nes_put_cqp_request(nesdev, cqp_request);
200 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
201 (ibmw->rkey & 0x0fffff00) >> 8);
202 kfree(nesmr);
204 return err;
209 * nes_bind_mw
211 static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
212 struct ib_mw_bind *ibmw_bind)
214 u64 u64temp;
215 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
216 struct nes_device *nesdev = nesvnic->nesdev;
217 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
218 struct nes_qp *nesqp = to_nesqp(ibqp);
219 struct nes_hw_qp_wqe *wqe;
220 unsigned long flags = 0;
221 u32 head;
222 u32 wqe_misc = 0;
223 u32 qsize;
225 if (nesqp->ibqp_state > IB_QPS_RTS)
226 return -EINVAL;
228 spin_lock_irqsave(&nesqp->lock, flags);
230 head = nesqp->hwqp.sq_head;
231 qsize = nesqp->hwqp.sq_tail;
233 /* Check for SQ overflow */
234 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
235 spin_unlock_irqrestore(&nesqp->lock, flags);
236 return -ENOMEM;
239 wqe = &nesqp->hwqp.sq_vbase[head];
240 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
241 nes_fill_init_qp_wqe(wqe, nesqp, head);
242 u64temp = ibmw_bind->wr_id;
243 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
244 wqe_misc = NES_IWARP_SQ_OP_BIND;
246 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
248 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
249 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
251 if (ibmw_bind->bind_info.mw_access_flags & IB_ACCESS_REMOTE_WRITE)
252 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
253 if (ibmw_bind->bind_info.mw_access_flags & IB_ACCESS_REMOTE_READ)
254 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
256 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
257 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX,
258 ibmw_bind->bind_info.mr->lkey);
259 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
260 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
261 ibmw_bind->bind_info.length);
262 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
263 u64temp = (u64)ibmw_bind->bind_info.addr;
264 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
266 head++;
267 if (head >= qsize)
268 head = 0;
270 nesqp->hwqp.sq_head = head;
271 barrier();
273 nes_write32(nesdev->regs+NES_WQE_ALLOC,
274 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
276 spin_unlock_irqrestore(&nesqp->lock, flags);
278 return 0;
283 * nes_alloc_fast_mr
285 static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
286 u32 stag, u32 page_count)
288 struct nes_hw_cqp_wqe *cqp_wqe;
289 struct nes_cqp_request *cqp_request;
290 unsigned long flags;
291 int ret;
292 struct nes_adapter *nesadapter = nesdev->nesadapter;
293 u32 opcode = 0;
294 u16 major_code;
295 u64 region_length = page_count * PAGE_SIZE;
298 cqp_request = nes_get_cqp_request(nesdev);
299 if (cqp_request == NULL) {
300 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
301 return -ENOMEM;
303 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
304 "region_length = %llu\n",
305 page_count, region_length);
306 cqp_request->waiting = 1;
307 cqp_wqe = &cqp_request->cqp_wqe;
309 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
310 if (nesadapter->free_4kpbl > 0) {
311 nesadapter->free_4kpbl--;
312 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
313 } else {
314 /* No 4kpbl's available: */
315 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
316 nes_debug(NES_DBG_MR, "Out of Pbls\n");
317 nes_free_cqp_request(nesdev, cqp_request);
318 return -ENOMEM;
321 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
322 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
323 NES_CQP_STAG_REM_ACC_EN;
325 * The current OFED API does not support the zero based TO option.
326 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
327 * the API does not support that ability to have the MR set for local
328 * access only when created and not allow the SQ op to override. Given
329 * this the remote enable must be set here.
332 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
333 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
334 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
336 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
337 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
338 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
339 cpu_to_le32(nespd->pd_id & 0x00007fff);
341 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
342 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
343 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
344 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
345 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
346 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
347 barrier();
349 atomic_set(&cqp_request->refcount, 2);
350 nes_post_cqp_request(nesdev, cqp_request);
352 /* Wait for CQP */
353 ret = wait_event_timeout(cqp_request->waitq,
354 (0 != cqp_request->request_done),
355 NES_EVENT_TIMEOUT);
357 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
358 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
359 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
360 cqp_request->minor_code);
361 major_code = cqp_request->major_code;
362 nes_put_cqp_request(nesdev, cqp_request);
364 if (!ret || major_code) {
365 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
366 nesadapter->free_4kpbl++;
367 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
370 if (!ret)
371 return -ETIME;
372 else if (major_code)
373 return -EIO;
374 return 0;
378 * nes_alloc_fast_reg_mr
380 static struct ib_mr *nes_alloc_fast_reg_mr(struct ib_pd *ibpd, int max_page_list_len)
382 struct nes_pd *nespd = to_nespd(ibpd);
383 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
384 struct nes_device *nesdev = nesvnic->nesdev;
385 struct nes_adapter *nesadapter = nesdev->nesadapter;
387 u32 next_stag_index;
388 u8 stag_key = 0;
389 u32 driver_key = 0;
390 int err = 0;
391 u32 stag_index = 0;
392 struct nes_mr *nesmr;
393 u32 stag;
394 int ret;
395 struct ib_mr *ibmr;
397 * Note: Set to always use a fixed length single page entry PBL. This is to allow
398 * for the fast_reg_mr operation to always know the size of the PBL.
400 if (max_page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
401 return ERR_PTR(-E2BIG);
403 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
404 stag_key = (u8)next_stag_index;
405 next_stag_index >>= 8;
406 next_stag_index %= nesadapter->max_mr;
408 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
409 nesadapter->max_mr, &stag_index,
410 &next_stag_index, NES_RESOURCE_FAST_MR);
411 if (err)
412 return ERR_PTR(err);
414 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
415 if (!nesmr) {
416 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
417 return ERR_PTR(-ENOMEM);
420 stag = stag_index << 8;
421 stag |= driver_key;
422 stag += (u32)stag_key;
424 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
425 stag, stag_index);
427 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_page_list_len);
429 if (ret == 0) {
430 nesmr->ibmr.rkey = stag;
431 nesmr->ibmr.lkey = stag;
432 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
433 ibmr = &nesmr->ibmr;
434 } else {
435 kfree(nesmr);
436 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
437 ibmr = ERR_PTR(-ENOMEM);
439 return ibmr;
443 * nes_alloc_fast_reg_page_list
445 static struct ib_fast_reg_page_list *nes_alloc_fast_reg_page_list(
446 struct ib_device *ibdev,
447 int page_list_len)
449 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
450 struct nes_device *nesdev = nesvnic->nesdev;
451 struct ib_fast_reg_page_list *pifrpl;
452 struct nes_ib_fast_reg_page_list *pnesfrpl;
454 if (page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
455 return ERR_PTR(-E2BIG);
457 * Allocate the ib_fast_reg_page_list structure, the
458 * nes_fast_bpl structure, and the PLB table.
460 pnesfrpl = kmalloc(sizeof(struct nes_ib_fast_reg_page_list) +
461 page_list_len * sizeof(u64), GFP_KERNEL);
463 if (!pnesfrpl)
464 return ERR_PTR(-ENOMEM);
466 pifrpl = &pnesfrpl->ibfrpl;
467 pifrpl->page_list = &pnesfrpl->pbl;
468 pifrpl->max_page_list_len = page_list_len;
470 * Allocate the WQE PBL
472 pnesfrpl->nes_wqe_pbl.kva = pci_alloc_consistent(nesdev->pcidev,
473 page_list_len * sizeof(u64),
474 &pnesfrpl->nes_wqe_pbl.paddr);
476 if (!pnesfrpl->nes_wqe_pbl.kva) {
477 kfree(pnesfrpl);
478 return ERR_PTR(-ENOMEM);
480 nes_debug(NES_DBG_MR, "nes_alloc_fast_reg_pbl: nes_frpl = %p, "
481 "ibfrpl = %p, ibfrpl.page_list = %p, pbl.kva = %p, "
482 "pbl.paddr = %llx\n", pnesfrpl, &pnesfrpl->ibfrpl,
483 pnesfrpl->ibfrpl.page_list, pnesfrpl->nes_wqe_pbl.kva,
484 (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr);
486 return pifrpl;
490 * nes_free_fast_reg_page_list
492 static void nes_free_fast_reg_page_list(struct ib_fast_reg_page_list *pifrpl)
494 struct nes_vnic *nesvnic = to_nesvnic(pifrpl->device);
495 struct nes_device *nesdev = nesvnic->nesdev;
496 struct nes_ib_fast_reg_page_list *pnesfrpl;
498 pnesfrpl = container_of(pifrpl, struct nes_ib_fast_reg_page_list, ibfrpl);
500 * Free the WQE PBL.
502 pci_free_consistent(nesdev->pcidev,
503 pifrpl->max_page_list_len * sizeof(u64),
504 pnesfrpl->nes_wqe_pbl.kva,
505 pnesfrpl->nes_wqe_pbl.paddr);
507 * Free the PBL structure
509 kfree(pnesfrpl);
513 * nes_query_device
515 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
517 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
518 struct nes_device *nesdev = nesvnic->nesdev;
519 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
521 memset(props, 0, sizeof(*props));
522 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
524 props->fw_ver = nesdev->nesadapter->firmware_version;
525 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
526 props->vendor_id = nesdev->nesadapter->vendor_id;
527 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
528 props->hw_ver = nesdev->nesadapter->hw_rev;
529 props->max_mr_size = 0x80000000;
530 props->max_qp = nesibdev->max_qp;
531 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
532 props->max_sge = nesdev->nesadapter->max_sge;
533 props->max_cq = nesibdev->max_cq;
534 props->max_cqe = nesdev->nesadapter->max_cqe;
535 props->max_mr = nesibdev->max_mr;
536 props->max_mw = nesibdev->max_mr;
537 props->max_pd = nesibdev->max_pd;
538 props->max_sge_rd = 1;
539 switch (nesdev->nesadapter->max_irrq_wr) {
540 case 0:
541 props->max_qp_rd_atom = 2;
542 break;
543 case 1:
544 props->max_qp_rd_atom = 8;
545 break;
546 case 2:
547 props->max_qp_rd_atom = 32;
548 break;
549 case 3:
550 props->max_qp_rd_atom = 64;
551 break;
552 default:
553 props->max_qp_rd_atom = 0;
555 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
556 props->atomic_cap = IB_ATOMIC_NONE;
557 props->max_map_per_fmr = 1;
559 return 0;
564 * nes_query_port
566 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
568 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
569 struct net_device *netdev = nesvnic->netdev;
571 memset(props, 0, sizeof(*props));
573 props->max_mtu = IB_MTU_4096;
575 if (netdev->mtu >= 4096)
576 props->active_mtu = IB_MTU_4096;
577 else if (netdev->mtu >= 2048)
578 props->active_mtu = IB_MTU_2048;
579 else if (netdev->mtu >= 1024)
580 props->active_mtu = IB_MTU_1024;
581 else if (netdev->mtu >= 512)
582 props->active_mtu = IB_MTU_512;
583 else
584 props->active_mtu = IB_MTU_256;
586 props->lid = 1;
587 props->lmc = 0;
588 props->sm_lid = 0;
589 props->sm_sl = 0;
590 if (netif_queue_stopped(netdev))
591 props->state = IB_PORT_DOWN;
592 else if (nesvnic->linkup)
593 props->state = IB_PORT_ACTIVE;
594 else
595 props->state = IB_PORT_DOWN;
596 props->phys_state = 0;
597 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
598 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
599 props->gid_tbl_len = 1;
600 props->pkey_tbl_len = 1;
601 props->qkey_viol_cntr = 0;
602 props->active_width = IB_WIDTH_4X;
603 props->active_speed = IB_SPEED_SDR;
604 props->max_msg_sz = 0x80000000;
606 return 0;
611 * nes_query_pkey
613 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
615 *pkey = 0;
616 return 0;
621 * nes_query_gid
623 static int nes_query_gid(struct ib_device *ibdev, u8 port,
624 int index, union ib_gid *gid)
626 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
628 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
629 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
631 return 0;
636 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
637 * of all objects associated with a particular user-mode client.
639 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
640 struct ib_udata *udata)
642 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
643 struct nes_device *nesdev = nesvnic->nesdev;
644 struct nes_adapter *nesadapter = nesdev->nesadapter;
645 struct nes_alloc_ucontext_req req;
646 struct nes_alloc_ucontext_resp uresp;
647 struct nes_ucontext *nes_ucontext;
648 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
651 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
652 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
653 return ERR_PTR(-EINVAL);
656 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
657 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
658 req.userspace_ver, NES_ABI_USERSPACE_VER);
659 return ERR_PTR(-EINVAL);
663 memset(&uresp, 0, sizeof uresp);
665 uresp.max_qps = nesibdev->max_qp;
666 uresp.max_pds = nesibdev->max_pd;
667 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
668 uresp.virtwq = nesadapter->virtwq;
669 uresp.kernel_ver = NES_ABI_KERNEL_VER;
671 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
672 if (!nes_ucontext)
673 return ERR_PTR(-ENOMEM);
675 nes_ucontext->nesdev = nesdev;
676 nes_ucontext->mmap_wq_offset = uresp.max_pds;
677 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
678 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
679 PAGE_SIZE;
682 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
683 kfree(nes_ucontext);
684 return ERR_PTR(-EFAULT);
687 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
688 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
689 atomic_set(&nes_ucontext->usecnt, 1);
690 return &nes_ucontext->ibucontext;
695 * nes_dealloc_ucontext
697 static int nes_dealloc_ucontext(struct ib_ucontext *context)
699 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
700 /* struct nes_device *nesdev = nesvnic->nesdev; */
701 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
703 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
704 return 0;
705 kfree(nes_ucontext);
706 return 0;
711 * nes_mmap
713 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
715 unsigned long index;
716 struct nes_vnic *nesvnic = to_nesvnic(context->device);
717 struct nes_device *nesdev = nesvnic->nesdev;
718 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
719 struct nes_ucontext *nes_ucontext;
720 struct nes_qp *nesqp;
722 nes_ucontext = to_nesucontext(context);
725 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
726 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
727 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
728 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
729 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
730 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
731 return -EFAULT;
733 nesqp = nes_ucontext->mmap_nesqp[index];
734 if (nesqp == NULL) {
735 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
736 return -EFAULT;
738 if (remap_pfn_range(vma, vma->vm_start,
739 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
740 vma->vm_end - vma->vm_start,
741 vma->vm_page_prot)) {
742 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
743 return -EAGAIN;
745 vma->vm_private_data = nesqp;
746 return 0;
747 } else {
748 index = vma->vm_pgoff;
749 if (!test_bit(index, nes_ucontext->allocated_doorbells))
750 return -EFAULT;
752 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
753 if (io_remap_pfn_range(vma, vma->vm_start,
754 (nesdev->doorbell_start +
755 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
756 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
757 return -EAGAIN;
758 vma->vm_private_data = nes_ucontext;
759 return 0;
762 return -ENOSYS;
767 * nes_alloc_pd
769 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
770 struct ib_ucontext *context, struct ib_udata *udata)
772 struct nes_pd *nespd;
773 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
774 struct nes_device *nesdev = nesvnic->nesdev;
775 struct nes_adapter *nesadapter = nesdev->nesadapter;
776 struct nes_ucontext *nesucontext;
777 struct nes_alloc_pd_resp uresp;
778 u32 pd_num = 0;
779 int err;
781 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
782 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
783 netdev_refcnt_read(nesvnic->netdev));
785 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
786 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
787 if (err) {
788 return ERR_PTR(err);
791 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
792 if (!nespd) {
793 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
794 return ERR_PTR(-ENOMEM);
797 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
798 nespd, nesvnic->nesibdev->ibdev.name);
800 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
802 if (context) {
803 nesucontext = to_nesucontext(context);
804 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
805 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
806 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
807 nespd->mmap_db_index, nespd->pd_id);
808 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
809 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
810 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
811 kfree(nespd);
812 return ERR_PTR(-ENOMEM);
815 uresp.pd_id = nespd->pd_id;
816 uresp.mmap_db_index = nespd->mmap_db_index;
817 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
818 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
819 kfree(nespd);
820 return ERR_PTR(-EFAULT);
823 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
824 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
825 nesucontext->first_free_db = nespd->mmap_db_index + 1;
828 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
829 return &nespd->ibpd;
834 * nes_dealloc_pd
836 static int nes_dealloc_pd(struct ib_pd *ibpd)
838 struct nes_ucontext *nesucontext;
839 struct nes_pd *nespd = to_nespd(ibpd);
840 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
841 struct nes_device *nesdev = nesvnic->nesdev;
842 struct nes_adapter *nesadapter = nesdev->nesadapter;
844 if ((ibpd->uobject) && (ibpd->uobject->context)) {
845 nesucontext = to_nesucontext(ibpd->uobject->context);
846 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
847 nespd->mmap_db_index);
848 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
849 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
850 if (nesucontext->first_free_db > nespd->mmap_db_index) {
851 nesucontext->first_free_db = nespd->mmap_db_index;
855 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
856 nespd->pd_id, nespd);
857 nes_free_resource(nesadapter, nesadapter->allocated_pds,
858 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
859 kfree(nespd);
861 return 0;
866 * nes_create_ah
868 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
870 return ERR_PTR(-ENOSYS);
875 * nes_destroy_ah
877 static int nes_destroy_ah(struct ib_ah *ah)
879 return -ENOSYS;
884 * nes_get_encoded_size
886 static inline u8 nes_get_encoded_size(int *size)
888 u8 encoded_size = 0;
889 if (*size <= 32) {
890 *size = 32;
891 encoded_size = 1;
892 } else if (*size <= 128) {
893 *size = 128;
894 encoded_size = 2;
895 } else if (*size <= 512) {
896 *size = 512;
897 encoded_size = 3;
899 return (encoded_size);
905 * nes_setup_virt_qp
907 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
908 struct nes_vnic *nesvnic, int sq_size, int rq_size)
910 unsigned long flags;
911 void *mem;
912 __le64 *pbl = NULL;
913 __le64 *tpbl;
914 __le64 *pblbuffer;
915 struct nes_device *nesdev = nesvnic->nesdev;
916 struct nes_adapter *nesadapter = nesdev->nesadapter;
917 u32 pbl_entries;
918 u8 rq_pbl_entries;
919 u8 sq_pbl_entries;
921 pbl_entries = nespbl->pbl_size >> 3;
922 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
923 nespbl->pbl_size, pbl_entries,
924 (void *)nespbl->pbl_vbase,
925 (unsigned long) nespbl->pbl_pbase);
926 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
927 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
928 /* the first pbl to be fro the rq_vbase... */
929 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
930 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
931 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
932 if (!nespbl->page) {
933 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
934 kfree(nespbl);
935 return -ENOMEM;
938 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
939 nesqp->page = nespbl->page;
940 if (!nesqp->hwqp.sq_vbase) {
941 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
942 kfree(nespbl);
943 return -ENOMEM;
946 /* Now to get to sq.. we need to calculate how many */
947 /* PBL entries were used by the rq.. */
948 pbl += sq_pbl_entries;
949 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
950 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
951 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
953 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
954 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
955 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
956 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
957 if (!nesadapter->free_256pbl) {
958 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
959 nespbl->pbl_pbase);
960 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
961 kunmap(nesqp->page);
962 kfree(nespbl);
963 return -ENOMEM;
965 nesadapter->free_256pbl--;
966 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
968 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
969 pblbuffer = nesqp->pbl_vbase;
970 if (!nesqp->pbl_vbase) {
971 /* memory allocated during nes_reg_user_mr() */
972 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
973 nespbl->pbl_pbase);
974 kfree(nespbl);
975 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
976 nesadapter->free_256pbl++;
977 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
978 kunmap(nesqp->page);
979 return -ENOMEM;
981 memset(nesqp->pbl_vbase, 0, 256);
982 /* fill in the page address in the pbl buffer.. */
983 tpbl = pblbuffer + 16;
984 pbl = (__le64 *)nespbl->pbl_vbase;
985 while (sq_pbl_entries--)
986 *tpbl++ = *pbl++;
987 tpbl = pblbuffer;
988 while (rq_pbl_entries--)
989 *tpbl++ = *pbl++;
991 /* done with memory allocated during nes_reg_user_mr() */
992 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
993 nespbl->pbl_pbase);
994 kfree(nespbl);
996 nesqp->qp_mem_size =
997 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
998 /* Round up to a multiple of a page */
999 nesqp->qp_mem_size += PAGE_SIZE - 1;
1000 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1002 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1003 &nesqp->hwqp.q2_pbase);
1005 if (!mem) {
1006 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1007 nesqp->pbl_vbase = NULL;
1008 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1009 nesadapter->free_256pbl++;
1010 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1011 kunmap(nesqp->page);
1012 return -ENOMEM;
1014 nesqp->sq_kmapped = 1;
1015 nesqp->hwqp.q2_vbase = mem;
1016 mem += 256;
1017 memset(nesqp->hwqp.q2_vbase, 0, 256);
1018 nesqp->nesqp_context = mem;
1019 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1020 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1022 return 0;
1027 * nes_setup_mmap_qp
1029 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1030 int sq_size, int rq_size)
1032 void *mem;
1033 struct nes_device *nesdev = nesvnic->nesdev;
1035 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1036 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1037 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1038 256; /* this is Q2 */
1039 /* Round up to a multiple of a page */
1040 nesqp->qp_mem_size += PAGE_SIZE - 1;
1041 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1043 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1044 &nesqp->hwqp.sq_pbase);
1045 if (!mem)
1046 return -ENOMEM;
1047 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1048 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1049 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1051 memset(mem, 0, nesqp->qp_mem_size);
1053 nesqp->hwqp.sq_vbase = mem;
1054 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1056 nesqp->hwqp.rq_vbase = mem;
1057 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1058 sizeof(struct nes_hw_qp_wqe) * sq_size;
1059 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1061 nesqp->hwqp.q2_vbase = mem;
1062 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1063 sizeof(struct nes_hw_qp_wqe) * rq_size;
1064 mem += 256;
1065 memset(nesqp->hwqp.q2_vbase, 0, 256);
1067 nesqp->nesqp_context = mem;
1068 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1069 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1070 return 0;
1075 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1077 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1078 struct nes_qp *nesqp, int virt_wqs)
1080 unsigned long flags;
1081 struct nes_adapter *nesadapter = nesdev->nesadapter;
1082 if (!virt_wqs) {
1083 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1084 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1085 }else {
1086 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1087 nesadapter->free_256pbl++;
1088 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1089 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1090 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1091 nesqp->pbl_vbase = NULL;
1092 if (nesqp->sq_kmapped) {
1093 nesqp->sq_kmapped = 0;
1094 kunmap(nesqp->page);
1101 * nes_create_qp
1103 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1104 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1106 u64 u64temp= 0;
1107 u64 u64nesqp = 0;
1108 struct nes_pd *nespd = to_nespd(ibpd);
1109 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1110 struct nes_device *nesdev = nesvnic->nesdev;
1111 struct nes_adapter *nesadapter = nesdev->nesadapter;
1112 struct nes_qp *nesqp;
1113 struct nes_cq *nescq;
1114 struct nes_ucontext *nes_ucontext;
1115 struct nes_hw_cqp_wqe *cqp_wqe;
1116 struct nes_cqp_request *cqp_request;
1117 struct nes_create_qp_req req;
1118 struct nes_create_qp_resp uresp;
1119 struct nes_pbl *nespbl = NULL;
1120 u32 qp_num = 0;
1121 u32 opcode = 0;
1122 /* u32 counter = 0; */
1123 void *mem;
1124 unsigned long flags;
1125 int ret;
1126 int err;
1127 int virt_wqs = 0;
1128 int sq_size;
1129 int rq_size;
1130 u8 sq_encoded_size;
1131 u8 rq_encoded_size;
1132 /* int counter; */
1134 if (init_attr->create_flags)
1135 return ERR_PTR(-EINVAL);
1137 atomic_inc(&qps_created);
1138 switch (init_attr->qp_type) {
1139 case IB_QPT_RC:
1140 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1141 init_attr->cap.max_inline_data = 0;
1142 } else {
1143 init_attr->cap.max_inline_data = 64;
1145 sq_size = init_attr->cap.max_send_wr;
1146 rq_size = init_attr->cap.max_recv_wr;
1148 /* check if the encoded sizes are OK or not... */
1149 sq_encoded_size = nes_get_encoded_size(&sq_size);
1150 rq_encoded_size = nes_get_encoded_size(&rq_size);
1152 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1153 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1154 rq_size, sq_size);
1155 return ERR_PTR(-EINVAL);
1158 init_attr->cap.max_send_wr = sq_size -2;
1159 init_attr->cap.max_recv_wr = rq_size -1;
1160 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1162 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1163 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
1164 if (ret) {
1165 return ERR_PTR(ret);
1168 /* Need 512 (actually now 1024) byte alignment on this structure */
1169 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1170 if (!mem) {
1171 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1172 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1173 return ERR_PTR(-ENOMEM);
1175 u64nesqp = (unsigned long)mem;
1176 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1177 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1178 u64nesqp &= ~u64temp;
1179 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1180 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1181 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1182 nesqp->allocated_buffer = mem;
1184 if (udata) {
1185 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1186 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1187 kfree(nesqp->allocated_buffer);
1188 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1189 return NULL;
1191 if (req.user_wqe_buffers) {
1192 virt_wqs = 1;
1194 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1195 nesqp->user_mode = 1;
1196 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1197 if (virt_wqs) {
1198 err = 1;
1199 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1200 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1201 list_del(&nespbl->list);
1202 err = 0;
1203 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1204 nespbl, nespbl->user_base);
1205 break;
1208 if (err) {
1209 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1210 (long long unsigned int)req.user_wqe_buffers);
1211 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1212 kfree(nesqp->allocated_buffer);
1213 return ERR_PTR(-EFAULT);
1217 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1218 nesqp->mmap_sq_db_index =
1219 find_next_zero_bit(nes_ucontext->allocated_wqs,
1220 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1221 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1222 nespd->mmap_db_index); */
1223 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1224 nes_debug(NES_DBG_QP,
1225 "db index > max user regions, failing create QP\n");
1226 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1227 if (virt_wqs) {
1228 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1229 nespbl->pbl_pbase);
1230 kfree(nespbl);
1232 kfree(nesqp->allocated_buffer);
1233 return ERR_PTR(-ENOMEM);
1235 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1236 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1237 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1238 } else {
1239 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1240 kfree(nesqp->allocated_buffer);
1241 return ERR_PTR(-EFAULT);
1244 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1245 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1246 if (err) {
1247 nes_debug(NES_DBG_QP,
1248 "error geting qp mem code = %d\n", err);
1249 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1250 kfree(nesqp->allocated_buffer);
1251 return ERR_PTR(-ENOMEM);
1254 nesqp->hwqp.sq_size = sq_size;
1255 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1256 nesqp->hwqp.sq_head = 1;
1257 nesqp->hwqp.rq_size = rq_size;
1258 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1259 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1260 (void *)nesqp->nesqp_context_pbase);
1262 nesqp->hwqp.qp_id = qp_num;
1263 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1264 nesqp->nespd = nespd;
1266 nescq = to_nescq(init_attr->send_cq);
1267 nesqp->nesscq = nescq;
1268 nescq = to_nescq(init_attr->recv_cq);
1269 nesqp->nesrcq = nescq;
1271 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1272 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1273 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1274 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1275 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1276 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1277 if (!udata) {
1278 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1279 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1281 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1282 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1283 u64temp = (u64)nesqp->hwqp.sq_pbase;
1284 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1285 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1288 if (!virt_wqs) {
1289 u64temp = (u64)nesqp->hwqp.sq_pbase;
1290 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1291 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1292 u64temp = (u64)nesqp->hwqp.rq_pbase;
1293 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1294 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1295 } else {
1296 u64temp = (u64)nesqp->pbl_pbase;
1297 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1298 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1301 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1302 nesvnic->next_qp_nic_index,
1303 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1304 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1305 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1306 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1307 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1308 nesvnic->next_qp_nic_index++;
1309 if ((nesvnic->next_qp_nic_index > 3) ||
1310 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1311 nesvnic->next_qp_nic_index = 0;
1313 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1315 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1316 u64temp = (u64)nesqp->hwqp.q2_pbase;
1317 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1318 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1319 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1320 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1321 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1322 NES_QPCONTEXT_ORDIRD_AAH |
1323 ((((u32)nesadapter->max_irrq_wr) <<
1324 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1325 if (disable_mpa_crc) {
1326 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1327 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1331 /* Create the QP */
1332 cqp_request = nes_get_cqp_request(nesdev);
1333 if (cqp_request == NULL) {
1334 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1335 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1336 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1337 kfree(nesqp->allocated_buffer);
1338 return ERR_PTR(-ENOMEM);
1340 cqp_request->waiting = 1;
1341 cqp_wqe = &cqp_request->cqp_wqe;
1343 if (!virt_wqs) {
1344 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1345 NES_CQP_QP_IWARP_STATE_IDLE;
1346 } else {
1347 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1348 NES_CQP_QP_IWARP_STATE_IDLE;
1350 opcode |= NES_CQP_QP_CQS_VALID;
1351 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1352 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1353 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1355 u64temp = (u64)nesqp->nesqp_context_pbase;
1356 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1358 atomic_set(&cqp_request->refcount, 2);
1359 nes_post_cqp_request(nesdev, cqp_request);
1361 /* Wait for CQP */
1362 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1363 nesqp->hwqp.qp_id);
1364 ret = wait_event_timeout(cqp_request->waitq,
1365 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1366 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1367 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1368 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1369 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1370 cqp_request->major_code, cqp_request->minor_code);
1371 if ((!ret) || (cqp_request->major_code)) {
1372 nes_put_cqp_request(nesdev, cqp_request);
1373 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1374 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1375 kfree(nesqp->allocated_buffer);
1376 if (!ret) {
1377 return ERR_PTR(-ETIME);
1378 } else {
1379 return ERR_PTR(-EIO);
1383 nes_put_cqp_request(nesdev, cqp_request);
1385 if (ibpd->uobject) {
1386 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1387 uresp.actual_sq_size = sq_size;
1388 uresp.actual_rq_size = rq_size;
1389 uresp.qp_id = nesqp->hwqp.qp_id;
1390 uresp.nes_drv_opt = nes_drv_opt;
1391 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1392 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1393 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1394 kfree(nesqp->allocated_buffer);
1395 return ERR_PTR(-EFAULT);
1399 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1400 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1401 spin_lock_init(&nesqp->lock);
1402 nes_add_ref(&nesqp->ibqp);
1403 break;
1404 default:
1405 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1406 return ERR_PTR(-EINVAL);
1409 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1410 init_timer(&nesqp->terminate_timer);
1411 nesqp->terminate_timer.function = nes_terminate_timeout;
1412 nesqp->terminate_timer.data = (unsigned long)nesqp;
1414 /* update the QP table */
1415 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1416 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1417 netdev_refcnt_read(nesvnic->netdev));
1419 return &nesqp->ibqp;
1423 * nes_clean_cq
1425 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1427 u32 cq_head;
1428 u32 lo;
1429 u32 hi;
1430 u64 u64temp;
1431 unsigned long flags = 0;
1433 spin_lock_irqsave(&nescq->lock, flags);
1435 cq_head = nescq->hw_cq.cq_head;
1436 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1437 rmb();
1438 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1439 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1440 u64temp = (((u64)hi) << 32) | ((u64)lo);
1441 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1442 if (u64temp == (u64)(unsigned long)nesqp) {
1443 /* Zero the context value so cqe will be ignored */
1444 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1445 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1448 if (++cq_head >= nescq->hw_cq.cq_size)
1449 cq_head = 0;
1452 spin_unlock_irqrestore(&nescq->lock, flags);
1457 * nes_destroy_qp
1459 static int nes_destroy_qp(struct ib_qp *ibqp)
1461 struct nes_qp *nesqp = to_nesqp(ibqp);
1462 struct nes_ucontext *nes_ucontext;
1463 struct ib_qp_attr attr;
1464 struct iw_cm_id *cm_id;
1465 struct iw_cm_event cm_event;
1466 int ret = 0;
1468 atomic_inc(&sw_qps_destroyed);
1469 nesqp->destroyed = 1;
1471 /* Blow away the connection if it exists. */
1472 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1473 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1474 attr.qp_state = IB_QPS_ERR;
1475 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1478 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1479 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1480 cm_id = nesqp->cm_id;
1481 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1482 cm_event.status = -ETIMEDOUT;
1483 cm_event.local_addr = cm_id->local_addr;
1484 cm_event.remote_addr = cm_id->remote_addr;
1485 cm_event.private_data = NULL;
1486 cm_event.private_data_len = 0;
1488 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1489 "QP%u. cm_id = %p, refcount = %u. \n",
1490 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1492 cm_id->rem_ref(cm_id);
1493 ret = cm_id->event_handler(cm_id, &cm_event);
1494 if (ret)
1495 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1498 if (nesqp->user_mode) {
1499 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1500 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1501 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1502 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1503 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1504 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1507 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1508 nesqp->sq_kmapped = 0;
1509 kunmap(nesqp->page);
1511 } else {
1512 /* Clean any pending completions from the cq(s) */
1513 if (nesqp->nesscq)
1514 nes_clean_cq(nesqp, nesqp->nesscq);
1516 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1517 nes_clean_cq(nesqp, nesqp->nesrcq);
1519 nes_rem_ref(&nesqp->ibqp);
1520 return 0;
1525 * nes_create_cq
1527 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1528 int comp_vector,
1529 struct ib_ucontext *context, struct ib_udata *udata)
1531 u64 u64temp;
1532 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1533 struct nes_device *nesdev = nesvnic->nesdev;
1534 struct nes_adapter *nesadapter = nesdev->nesadapter;
1535 struct nes_cq *nescq;
1536 struct nes_ucontext *nes_ucontext = NULL;
1537 struct nes_cqp_request *cqp_request;
1538 void *mem = NULL;
1539 struct nes_hw_cqp_wqe *cqp_wqe;
1540 struct nes_pbl *nespbl = NULL;
1541 struct nes_create_cq_req req;
1542 struct nes_create_cq_resp resp;
1543 u32 cq_num = 0;
1544 u32 opcode = 0;
1545 u32 pbl_entries = 1;
1546 int err;
1547 unsigned long flags;
1548 int ret;
1550 if (entries > nesadapter->max_cqe)
1551 return ERR_PTR(-EINVAL);
1553 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1554 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1555 if (err) {
1556 return ERR_PTR(err);
1559 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1560 if (!nescq) {
1561 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1562 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1563 return ERR_PTR(-ENOMEM);
1566 nescq->hw_cq.cq_size = max(entries + 1, 5);
1567 nescq->hw_cq.cq_number = cq_num;
1568 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1571 if (context) {
1572 nes_ucontext = to_nesucontext(context);
1573 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1574 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1575 kfree(nescq);
1576 return ERR_PTR(-EFAULT);
1578 nesvnic->mcrq_ucontext = nes_ucontext;
1579 nes_ucontext->mcrqf = req.mcrqf;
1580 if (nes_ucontext->mcrqf) {
1581 if (nes_ucontext->mcrqf & 0x80000000)
1582 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1583 else if (nes_ucontext->mcrqf & 0x40000000)
1584 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1585 else
1586 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1587 nescq->mcrqf = nes_ucontext->mcrqf;
1588 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1590 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1591 (unsigned long)req.user_cq_buffer, entries);
1592 err = 1;
1593 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1594 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1595 list_del(&nespbl->list);
1596 err = 0;
1597 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1598 nespbl);
1599 break;
1602 if (err) {
1603 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1604 kfree(nescq);
1605 return ERR_PTR(-EFAULT);
1608 pbl_entries = nespbl->pbl_size >> 3;
1609 nescq->cq_mem_size = 0;
1610 } else {
1611 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1612 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1613 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1615 /* allocate the physical buffer space */
1616 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1617 &nescq->hw_cq.cq_pbase);
1618 if (!mem) {
1619 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1620 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1621 kfree(nescq);
1622 return ERR_PTR(-ENOMEM);
1625 memset(mem, 0, nescq->cq_mem_size);
1626 nescq->hw_cq.cq_vbase = mem;
1627 nescq->hw_cq.cq_head = 0;
1628 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1629 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1630 (u32)nescq->hw_cq.cq_pbase);
1633 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1634 spin_lock_init(&nescq->lock);
1636 /* send CreateCQ request to CQP */
1637 cqp_request = nes_get_cqp_request(nesdev);
1638 if (cqp_request == NULL) {
1639 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1640 if (!context)
1641 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1642 nescq->hw_cq.cq_pbase);
1643 else {
1644 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1645 nespbl->pbl_vbase, nespbl->pbl_pbase);
1646 kfree(nespbl);
1649 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1650 kfree(nescq);
1651 return ERR_PTR(-ENOMEM);
1653 cqp_request->waiting = 1;
1654 cqp_wqe = &cqp_request->cqp_wqe;
1656 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1657 NES_CQP_CQ_CHK_OVERFLOW |
1658 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1660 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1662 if (pbl_entries != 1) {
1663 if (pbl_entries > 32) {
1664 /* use 4k pbl */
1665 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1666 if (nesadapter->free_4kpbl == 0) {
1667 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1668 nes_free_cqp_request(nesdev, cqp_request);
1669 if (!context)
1670 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1671 nescq->hw_cq.cq_pbase);
1672 else {
1673 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1674 nespbl->pbl_vbase, nespbl->pbl_pbase);
1675 kfree(nespbl);
1677 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1678 kfree(nescq);
1679 return ERR_PTR(-ENOMEM);
1680 } else {
1681 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1682 nescq->virtual_cq = 2;
1683 nesadapter->free_4kpbl--;
1685 } else {
1686 /* use 256 byte pbl */
1687 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1688 if (nesadapter->free_256pbl == 0) {
1689 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1690 nes_free_cqp_request(nesdev, cqp_request);
1691 if (!context)
1692 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1693 nescq->hw_cq.cq_pbase);
1694 else {
1695 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1696 nespbl->pbl_vbase, nespbl->pbl_pbase);
1697 kfree(nespbl);
1699 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1700 kfree(nescq);
1701 return ERR_PTR(-ENOMEM);
1702 } else {
1703 opcode |= NES_CQP_CQ_VIRT;
1704 nescq->virtual_cq = 1;
1705 nesadapter->free_256pbl--;
1710 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1712 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1713 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1714 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1715 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1717 if (context) {
1718 if (pbl_entries != 1)
1719 u64temp = (u64)nespbl->pbl_pbase;
1720 else
1721 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1722 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1723 nes_ucontext->mmap_db_index[0]);
1724 } else {
1725 u64temp = (u64)nescq->hw_cq.cq_pbase;
1726 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1728 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1729 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1730 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1731 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1732 cpu_to_le32((u32)(u64temp >> 1));
1733 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1734 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1736 atomic_set(&cqp_request->refcount, 2);
1737 nes_post_cqp_request(nesdev, cqp_request);
1739 /* Wait for CQP */
1740 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1741 nescq->hw_cq.cq_number);
1742 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1743 NES_EVENT_TIMEOUT * 2);
1744 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1745 nescq->hw_cq.cq_number, ret);
1746 if ((!ret) || (cqp_request->major_code)) {
1747 nes_put_cqp_request(nesdev, cqp_request);
1748 if (!context)
1749 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1750 nescq->hw_cq.cq_pbase);
1751 else {
1752 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1753 nespbl->pbl_vbase, nespbl->pbl_pbase);
1754 kfree(nespbl);
1756 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1757 kfree(nescq);
1758 return ERR_PTR(-EIO);
1760 nes_put_cqp_request(nesdev, cqp_request);
1762 if (context) {
1763 /* free the nespbl */
1764 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1765 nespbl->pbl_pbase);
1766 kfree(nespbl);
1767 resp.cq_id = nescq->hw_cq.cq_number;
1768 resp.cq_size = nescq->hw_cq.cq_size;
1769 resp.mmap_db_index = 0;
1770 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1771 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1772 kfree(nescq);
1773 return ERR_PTR(-EFAULT);
1777 return &nescq->ibcq;
1782 * nes_destroy_cq
1784 static int nes_destroy_cq(struct ib_cq *ib_cq)
1786 struct nes_cq *nescq;
1787 struct nes_device *nesdev;
1788 struct nes_vnic *nesvnic;
1789 struct nes_adapter *nesadapter;
1790 struct nes_hw_cqp_wqe *cqp_wqe;
1791 struct nes_cqp_request *cqp_request;
1792 unsigned long flags;
1793 u32 opcode = 0;
1794 int ret;
1796 if (ib_cq == NULL)
1797 return 0;
1799 nescq = to_nescq(ib_cq);
1800 nesvnic = to_nesvnic(ib_cq->device);
1801 nesdev = nesvnic->nesdev;
1802 nesadapter = nesdev->nesadapter;
1804 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1806 /* Send DestroyCQ request to CQP */
1807 cqp_request = nes_get_cqp_request(nesdev);
1808 if (cqp_request == NULL) {
1809 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1810 return -ENOMEM;
1812 cqp_request->waiting = 1;
1813 cqp_wqe = &cqp_request->cqp_wqe;
1814 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1815 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1816 if (nescq->virtual_cq == 1) {
1817 nesadapter->free_256pbl++;
1818 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1819 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1820 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1822 } else if (nescq->virtual_cq == 2) {
1823 nesadapter->free_4kpbl++;
1824 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1825 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1826 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1828 opcode |= NES_CQP_CQ_4KB_CHUNK;
1831 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1833 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1834 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1835 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1836 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1837 if (!nescq->mcrqf)
1838 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1840 atomic_set(&cqp_request->refcount, 2);
1841 nes_post_cqp_request(nesdev, cqp_request);
1843 /* Wait for CQP */
1844 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1845 nescq->hw_cq.cq_number);
1846 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1847 NES_EVENT_TIMEOUT);
1848 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1849 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1850 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1851 cqp_request->minor_code);
1852 if (!ret) {
1853 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1854 nescq->hw_cq.cq_number);
1855 ret = -ETIME;
1856 } else if (cqp_request->major_code) {
1857 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1858 nescq->hw_cq.cq_number);
1859 ret = -EIO;
1860 } else {
1861 ret = 0;
1863 nes_put_cqp_request(nesdev, cqp_request);
1865 if (nescq->cq_mem_size)
1866 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1867 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1868 kfree(nescq);
1870 return ret;
1874 * root_256
1876 static u32 root_256(struct nes_device *nesdev,
1877 struct nes_root_vpbl *root_vpbl,
1878 struct nes_root_vpbl *new_root,
1879 u16 pbl_count_4k)
1881 u64 leaf_pbl;
1882 int i, j, k;
1884 if (pbl_count_4k == 1) {
1885 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1886 512, &new_root->pbl_pbase);
1888 if (new_root->pbl_vbase == NULL)
1889 return 0;
1891 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1892 for (i = 0; i < 16; i++) {
1893 new_root->pbl_vbase[i].pa_low =
1894 cpu_to_le32((u32)leaf_pbl);
1895 new_root->pbl_vbase[i].pa_high =
1896 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1897 leaf_pbl += 256;
1899 } else {
1900 for (i = 3; i >= 0; i--) {
1901 j = i * 16;
1902 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1903 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1904 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1905 << 32);
1906 for (k = 1; k < 16; k++) {
1907 leaf_pbl += 256;
1908 root_vpbl->pbl_vbase[j + k].pa_low =
1909 cpu_to_le32((u32)leaf_pbl);
1910 root_vpbl->pbl_vbase[j + k].pa_high =
1911 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1916 return 1;
1921 * nes_reg_mr
1923 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1924 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1925 dma_addr_t single_buffer, u16 pbl_count_4k,
1926 u16 residual_page_count_4k, int acc, u64 *iova_start,
1927 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1929 struct nes_hw_cqp_wqe *cqp_wqe;
1930 struct nes_cqp_request *cqp_request;
1931 unsigned long flags;
1932 int ret;
1933 struct nes_adapter *nesadapter = nesdev->nesadapter;
1934 uint pg_cnt = 0;
1935 u16 pbl_count_256 = 0;
1936 u16 pbl_count = 0;
1937 u8 use_256_pbls = 0;
1938 u8 use_4k_pbls = 0;
1939 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1940 struct nes_root_vpbl new_root = { 0, NULL, NULL };
1941 u32 opcode = 0;
1942 u16 major_code;
1944 /* Register the region with the adapter */
1945 cqp_request = nes_get_cqp_request(nesdev);
1946 if (cqp_request == NULL) {
1947 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1948 return -ENOMEM;
1950 cqp_request->waiting = 1;
1951 cqp_wqe = &cqp_request->cqp_wqe;
1953 if (pbl_count_4k) {
1954 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1956 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1957 pbl_count_256 = (pg_cnt + 31) / 32;
1958 if (pg_cnt <= 32) {
1959 if (pbl_count_256 <= nesadapter->free_256pbl)
1960 use_256_pbls = 1;
1961 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1962 use_4k_pbls = 1;
1963 } else if (pg_cnt <= 2048) {
1964 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1965 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1966 use_4k_pbls = 1;
1967 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1968 use_256_pbls = 1;
1969 use_two_level = 1;
1970 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1971 use_4k_pbls = 1;
1973 } else {
1974 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1975 use_4k_pbls = 1;
1978 if (use_256_pbls) {
1979 pbl_count = pbl_count_256;
1980 nesadapter->free_256pbl -= pbl_count + use_two_level;
1981 } else if (use_4k_pbls) {
1982 pbl_count = pbl_count_4k;
1983 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1984 } else {
1985 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1986 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1987 nes_free_cqp_request(nesdev, cqp_request);
1988 return -ENOMEM;
1991 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1994 if (use_256_pbls && use_two_level) {
1995 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1996 if (new_root.pbl_pbase != 0)
1997 root_vpbl = &new_root;
1998 } else {
1999 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2000 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
2001 use_256_pbls = 0;
2003 if (pbl_count_4k == 1)
2004 use_two_level = 0;
2005 pbl_count = pbl_count_4k;
2007 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2008 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2009 use_4k_pbls = 1;
2011 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2013 if (use_4k_pbls == 0)
2014 return -ENOMEM;
2018 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2019 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2020 if (acc & IB_ACCESS_LOCAL_WRITE)
2021 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2022 if (acc & IB_ACCESS_REMOTE_WRITE)
2023 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2024 if (acc & IB_ACCESS_REMOTE_READ)
2025 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2026 if (acc & IB_ACCESS_MW_BIND)
2027 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2029 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2030 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2031 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2032 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2034 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2035 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2036 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2037 cpu_to_le32(nespd->pd_id & 0x00007fff);
2038 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2040 if (pbl_count == 0) {
2041 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2042 } else {
2043 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2044 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2045 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
2047 if (use_4k_pbls)
2048 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2050 barrier();
2052 atomic_set(&cqp_request->refcount, 2);
2053 nes_post_cqp_request(nesdev, cqp_request);
2055 /* Wait for CQP */
2056 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2057 NES_EVENT_TIMEOUT);
2058 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2059 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2060 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2061 major_code = cqp_request->major_code;
2062 nes_put_cqp_request(nesdev, cqp_request);
2064 if ((!ret || major_code) && pbl_count != 0) {
2065 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2066 if (use_256_pbls)
2067 nesadapter->free_256pbl += pbl_count + use_two_level;
2068 else if (use_4k_pbls)
2069 nesadapter->free_4kpbl += pbl_count + use_two_level;
2070 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2072 if (new_root.pbl_pbase)
2073 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2074 new_root.pbl_pbase);
2076 if (!ret)
2077 return -ETIME;
2078 else if (major_code)
2079 return -EIO;
2081 *actual_pbl_cnt = pbl_count + use_two_level;
2082 *used_4k_pbls = use_4k_pbls;
2083 return 0;
2088 * nes_reg_phys_mr
2090 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2091 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2092 u64 * iova_start)
2094 u64 region_length;
2095 struct nes_pd *nespd = to_nespd(ib_pd);
2096 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2097 struct nes_device *nesdev = nesvnic->nesdev;
2098 struct nes_adapter *nesadapter = nesdev->nesadapter;
2099 struct nes_mr *nesmr;
2100 struct ib_mr *ibmr;
2101 struct nes_vpbl vpbl;
2102 struct nes_root_vpbl root_vpbl;
2103 u32 stag;
2104 u32 i;
2105 unsigned long mask;
2106 u32 stag_index = 0;
2107 u32 next_stag_index = 0;
2108 u32 driver_key = 0;
2109 u32 root_pbl_index = 0;
2110 u32 cur_pbl_index = 0;
2111 int err = 0;
2112 int ret = 0;
2113 u16 pbl_count = 0;
2114 u8 single_page = 1;
2115 u8 stag_key = 0;
2117 region_length = 0;
2118 vpbl.pbl_vbase = NULL;
2119 root_vpbl.pbl_vbase = NULL;
2120 root_vpbl.pbl_pbase = 0;
2122 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2123 stag_key = (u8)next_stag_index;
2125 driver_key = 0;
2127 next_stag_index >>= 8;
2128 next_stag_index %= nesadapter->max_mr;
2129 if (num_phys_buf > (1024*512)) {
2130 return ERR_PTR(-E2BIG);
2133 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2134 return ERR_PTR(-EINVAL);
2136 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2137 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
2138 if (err) {
2139 return ERR_PTR(err);
2142 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2143 if (!nesmr) {
2144 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2145 return ERR_PTR(-ENOMEM);
2148 for (i = 0; i < num_phys_buf; i++) {
2150 if ((i & 0x01FF) == 0) {
2151 if (root_pbl_index == 1) {
2152 /* Allocate the root PBL */
2153 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2154 &root_vpbl.pbl_pbase);
2155 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2156 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2157 if (!root_vpbl.pbl_vbase) {
2158 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2159 vpbl.pbl_pbase);
2160 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2161 kfree(nesmr);
2162 return ERR_PTR(-ENOMEM);
2164 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2165 if (!root_vpbl.leaf_vpbl) {
2166 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2167 root_vpbl.pbl_pbase);
2168 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2169 vpbl.pbl_pbase);
2170 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2171 kfree(nesmr);
2172 return ERR_PTR(-ENOMEM);
2174 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2175 root_vpbl.pbl_vbase[0].pa_high =
2176 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2177 root_vpbl.leaf_vpbl[0] = vpbl;
2179 /* Allocate a 4K buffer for the PBL */
2180 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2181 &vpbl.pbl_pbase);
2182 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2183 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2184 if (!vpbl.pbl_vbase) {
2185 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2186 ibmr = ERR_PTR(-ENOMEM);
2187 kfree(nesmr);
2188 goto reg_phys_err;
2190 /* Fill in the root table */
2191 if (1 <= root_pbl_index) {
2192 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2193 cpu_to_le32((u32)vpbl.pbl_pbase);
2194 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2195 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2196 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2198 root_pbl_index++;
2199 cur_pbl_index = 0;
2202 mask = !buffer_list[i].size;
2203 if (i != 0)
2204 mask |= buffer_list[i].addr;
2205 if (i != num_phys_buf - 1)
2206 mask |= buffer_list[i].addr + buffer_list[i].size;
2208 if (mask & ~PAGE_MASK) {
2209 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2210 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2211 ibmr = ERR_PTR(-EINVAL);
2212 kfree(nesmr);
2213 goto reg_phys_err;
2216 region_length += buffer_list[i].size;
2217 if ((i != 0) && (single_page)) {
2218 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2219 single_page = 0;
2221 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK);
2222 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2223 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2226 stag = stag_index << 8;
2227 stag |= driver_key;
2228 stag += (u32)stag_key;
2230 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2231 " length = 0x%016lX, index = 0x%08X\n",
2232 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2234 /* Make the leaf PBL the root if only one PBL */
2235 if (root_pbl_index == 1) {
2236 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2239 if (single_page) {
2240 pbl_count = 0;
2241 } else {
2242 pbl_count = root_pbl_index;
2244 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2245 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2246 &nesmr->pbls_used, &nesmr->pbl_4k);
2248 if (ret == 0) {
2249 nesmr->ibmr.rkey = stag;
2250 nesmr->ibmr.lkey = stag;
2251 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2252 ibmr = &nesmr->ibmr;
2253 } else {
2254 kfree(nesmr);
2255 ibmr = ERR_PTR(-ENOMEM);
2258 reg_phys_err:
2259 /* free the resources */
2260 if (root_pbl_index == 1) {
2261 /* single PBL case */
2262 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2263 } else {
2264 for (i=0; i<root_pbl_index; i++) {
2265 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2266 root_vpbl.leaf_vpbl[i].pbl_pbase);
2268 kfree(root_vpbl.leaf_vpbl);
2269 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2270 root_vpbl.pbl_pbase);
2273 return ibmr;
2278 * nes_get_dma_mr
2280 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2282 struct ib_phys_buf bl;
2283 u64 kva = 0;
2285 nes_debug(NES_DBG_MR, "\n");
2287 bl.size = (u64)0xffffffffffULL;
2288 bl.addr = 0;
2289 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2294 * nes_reg_user_mr
2296 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2297 u64 virt, int acc, struct ib_udata *udata)
2299 u64 iova_start;
2300 __le64 *pbl;
2301 u64 region_length;
2302 dma_addr_t last_dma_addr = 0;
2303 dma_addr_t first_dma_addr = 0;
2304 struct nes_pd *nespd = to_nespd(pd);
2305 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2306 struct nes_device *nesdev = nesvnic->nesdev;
2307 struct nes_adapter *nesadapter = nesdev->nesadapter;
2308 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2309 struct ib_umem_chunk *chunk;
2310 struct nes_ucontext *nes_ucontext;
2311 struct nes_pbl *nespbl;
2312 struct nes_mr *nesmr;
2313 struct ib_umem *region;
2314 struct nes_mem_reg_req req;
2315 struct nes_vpbl vpbl;
2316 struct nes_root_vpbl root_vpbl;
2317 int nmap_index, page_index;
2318 int page_count = 0;
2319 int err, pbl_depth = 0;
2320 int chunk_pages;
2321 int ret;
2322 u32 stag;
2323 u32 stag_index = 0;
2324 u32 next_stag_index;
2325 u32 driver_key;
2326 u32 root_pbl_index = 0;
2327 u32 cur_pbl_index = 0;
2328 u32 skip_pages;
2329 u16 pbl_count;
2330 u8 single_page = 1;
2331 u8 stag_key;
2333 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2334 if (IS_ERR(region)) {
2335 return (struct ib_mr *)region;
2338 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2339 " offset = %u, page size = %u.\n",
2340 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2341 region->offset, region->page_size);
2343 skip_pages = ((u32)region->offset) >> 12;
2345 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2346 ib_umem_release(region);
2347 return ERR_PTR(-EFAULT);
2349 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2351 switch (req.reg_type) {
2352 case IWNES_MEMREG_TYPE_MEM:
2353 pbl_depth = 0;
2354 region_length = 0;
2355 vpbl.pbl_vbase = NULL;
2356 root_vpbl.pbl_vbase = NULL;
2357 root_vpbl.pbl_pbase = 0;
2359 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2360 stag_key = (u8)next_stag_index;
2362 driver_key = next_stag_index & 0x70000000;
2364 next_stag_index >>= 8;
2365 next_stag_index %= nesadapter->max_mr;
2367 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2368 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2369 if (err) {
2370 ib_umem_release(region);
2371 return ERR_PTR(err);
2374 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2375 if (!nesmr) {
2376 ib_umem_release(region);
2377 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2378 return ERR_PTR(-ENOMEM);
2380 nesmr->region = region;
2382 list_for_each_entry(chunk, &region->chunk_list, list) {
2383 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2384 chunk->nents, chunk->nmap);
2385 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2386 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2387 ib_umem_release(region);
2388 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2389 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2390 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2391 ibmr = ERR_PTR(-EINVAL);
2392 kfree(nesmr);
2393 goto reg_user_mr_err;
2396 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2397 ib_umem_release(region);
2398 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2399 stag_index);
2400 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2401 ibmr = ERR_PTR(-EINVAL);
2402 kfree(nesmr);
2403 goto reg_user_mr_err;
2406 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2407 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2408 region_length -= skip_pages << 12;
2409 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2410 skip_pages = 0;
2411 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2412 goto enough_pages;
2413 if ((page_count&0x01FF) == 0) {
2414 if (page_count >= 1024 * 512) {
2415 ib_umem_release(region);
2416 nes_free_resource(nesadapter,
2417 nesadapter->allocated_mrs, stag_index);
2418 kfree(nesmr);
2419 ibmr = ERR_PTR(-E2BIG);
2420 goto reg_user_mr_err;
2422 if (root_pbl_index == 1) {
2423 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2424 8192, &root_vpbl.pbl_pbase);
2425 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2426 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2427 if (!root_vpbl.pbl_vbase) {
2428 ib_umem_release(region);
2429 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2430 vpbl.pbl_pbase);
2431 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2432 stag_index);
2433 kfree(nesmr);
2434 ibmr = ERR_PTR(-ENOMEM);
2435 goto reg_user_mr_err;
2437 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2438 GFP_KERNEL);
2439 if (!root_vpbl.leaf_vpbl) {
2440 ib_umem_release(region);
2441 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2442 root_vpbl.pbl_pbase);
2443 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2444 vpbl.pbl_pbase);
2445 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2446 stag_index);
2447 kfree(nesmr);
2448 ibmr = ERR_PTR(-ENOMEM);
2449 goto reg_user_mr_err;
2451 root_vpbl.pbl_vbase[0].pa_low =
2452 cpu_to_le32((u32)vpbl.pbl_pbase);
2453 root_vpbl.pbl_vbase[0].pa_high =
2454 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2455 root_vpbl.leaf_vpbl[0] = vpbl;
2457 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2458 &vpbl.pbl_pbase);
2459 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2460 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2461 if (!vpbl.pbl_vbase) {
2462 ib_umem_release(region);
2463 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2464 ibmr = ERR_PTR(-ENOMEM);
2465 kfree(nesmr);
2466 goto reg_user_mr_err;
2468 if (1 <= root_pbl_index) {
2469 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2470 cpu_to_le32((u32)vpbl.pbl_pbase);
2471 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2472 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2473 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2475 root_pbl_index++;
2476 cur_pbl_index = 0;
2478 if (single_page) {
2479 if (page_count != 0) {
2480 if ((last_dma_addr+4096) !=
2481 (sg_dma_address(&chunk->page_list[nmap_index])+
2482 (page_index*4096)))
2483 single_page = 0;
2484 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2485 (page_index*4096);
2486 } else {
2487 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2488 (page_index*4096);
2489 last_dma_addr = first_dma_addr;
2493 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2494 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2495 (page_index*4096)));
2496 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2497 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2498 (page_index*4096))) >> 32)));
2499 cur_pbl_index++;
2500 page_count++;
2504 enough_pages:
2505 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2506 " stag_key=0x%08x\n",
2507 stag_index, driver_key, stag_key);
2508 stag = stag_index << 8;
2509 stag |= driver_key;
2510 stag += (u32)stag_key;
2512 iova_start = virt;
2513 /* Make the leaf PBL the root if only one PBL */
2514 if (root_pbl_index == 1) {
2515 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2518 if (single_page) {
2519 pbl_count = 0;
2520 } else {
2521 pbl_count = root_pbl_index;
2522 first_dma_addr = 0;
2524 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2525 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2526 stag, (unsigned int)iova_start,
2527 (unsigned int)region_length, stag_index,
2528 (unsigned long long)region->length, pbl_count);
2529 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2530 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2531 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2533 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2535 if (ret == 0) {
2536 nesmr->ibmr.rkey = stag;
2537 nesmr->ibmr.lkey = stag;
2538 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2539 ibmr = &nesmr->ibmr;
2540 } else {
2541 ib_umem_release(region);
2542 kfree(nesmr);
2543 ibmr = ERR_PTR(-ENOMEM);
2546 reg_user_mr_err:
2547 /* free the resources */
2548 if (root_pbl_index == 1) {
2549 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2550 vpbl.pbl_pbase);
2551 } else {
2552 for (page_index=0; page_index<root_pbl_index; page_index++) {
2553 pci_free_consistent(nesdev->pcidev, 4096,
2554 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2555 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2557 kfree(root_vpbl.leaf_vpbl);
2558 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2559 root_vpbl.pbl_pbase);
2562 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2564 return ibmr;
2565 case IWNES_MEMREG_TYPE_QP:
2566 case IWNES_MEMREG_TYPE_CQ:
2567 if (!region->length) {
2568 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2569 ib_umem_release(region);
2570 return ERR_PTR(-EINVAL);
2572 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2573 if (!nespbl) {
2574 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2575 ib_umem_release(region);
2576 return ERR_PTR(-ENOMEM);
2578 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2579 if (!nesmr) {
2580 ib_umem_release(region);
2581 kfree(nespbl);
2582 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2583 return ERR_PTR(-ENOMEM);
2585 nesmr->region = region;
2586 nes_ucontext = to_nesucontext(pd->uobject->context);
2587 pbl_depth = region->length >> 12;
2588 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2589 nespbl->pbl_size = pbl_depth*sizeof(u64);
2590 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2591 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2592 } else {
2593 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2596 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2597 nespbl->pbl_size, pbl_depth);
2598 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2599 &nespbl->pbl_pbase);
2600 if (!pbl) {
2601 ib_umem_release(region);
2602 kfree(nesmr);
2603 kfree(nespbl);
2604 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2605 return ERR_PTR(-ENOMEM);
2608 nespbl->pbl_vbase = (u64 *)pbl;
2609 nespbl->user_base = start;
2610 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2611 " pbl_vbase=%p user_base=0x%lx\n",
2612 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2613 (void *) nespbl->pbl_vbase, nespbl->user_base);
2615 list_for_each_entry(chunk, &region->chunk_list, list) {
2616 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2617 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2618 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2619 nespbl->page = sg_page(&chunk->page_list[0]);
2620 for (page_index=0; page_index<chunk_pages; page_index++) {
2621 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2622 (sg_dma_address(&chunk->page_list[nmap_index])+
2623 (page_index*4096)));
2624 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2625 (sg_dma_address(&chunk->page_list[nmap_index])+
2626 (page_index*4096)))>>32);
2627 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2628 (unsigned long long)*pbl,
2629 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2630 pbl++;
2634 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2635 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2636 } else {
2637 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2639 nesmr->ibmr.rkey = -1;
2640 nesmr->ibmr.lkey = -1;
2641 nesmr->mode = req.reg_type;
2642 return &nesmr->ibmr;
2645 ib_umem_release(region);
2646 return ERR_PTR(-ENOSYS);
2651 * nes_dereg_mr
2653 static int nes_dereg_mr(struct ib_mr *ib_mr)
2655 struct nes_mr *nesmr = to_nesmr(ib_mr);
2656 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2657 struct nes_device *nesdev = nesvnic->nesdev;
2658 struct nes_adapter *nesadapter = nesdev->nesadapter;
2659 struct nes_hw_cqp_wqe *cqp_wqe;
2660 struct nes_cqp_request *cqp_request;
2661 unsigned long flags;
2662 int ret;
2663 u16 major_code;
2664 u16 minor_code;
2666 if (nesmr->region) {
2667 ib_umem_release(nesmr->region);
2669 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2670 kfree(nesmr);
2671 return 0;
2674 /* Deallocate the region with the adapter */
2676 cqp_request = nes_get_cqp_request(nesdev);
2677 if (cqp_request == NULL) {
2678 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2679 return -ENOMEM;
2681 cqp_request->waiting = 1;
2682 cqp_wqe = &cqp_request->cqp_wqe;
2684 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2685 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2686 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2687 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2688 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2690 atomic_set(&cqp_request->refcount, 2);
2691 nes_post_cqp_request(nesdev, cqp_request);
2693 /* Wait for CQP */
2694 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2695 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2696 NES_EVENT_TIMEOUT);
2697 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2698 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2699 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2701 major_code = cqp_request->major_code;
2702 minor_code = cqp_request->minor_code;
2704 nes_put_cqp_request(nesdev, cqp_request);
2706 if (!ret) {
2707 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2708 " ib_mr=%p, rkey = 0x%08X\n",
2709 ib_mr, ib_mr->rkey);
2710 return -ETIME;
2711 } else if (major_code) {
2712 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2713 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2714 major_code, minor_code, ib_mr, ib_mr->rkey);
2715 return -EIO;
2718 if (nesmr->pbls_used != 0) {
2719 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2720 if (nesmr->pbl_4k) {
2721 nesadapter->free_4kpbl += nesmr->pbls_used;
2722 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2723 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2724 "exceeded the max(%u)\n",
2725 nesadapter->free_4kpbl,
2726 nesadapter->max_4kpbl);
2727 } else {
2728 nesadapter->free_256pbl += nesmr->pbls_used;
2729 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2730 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2731 "exceeded the max(%u)\n",
2732 nesadapter->free_256pbl,
2733 nesadapter->max_256pbl);
2735 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2737 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2738 (ib_mr->rkey & 0x0fffff00) >> 8);
2740 kfree(nesmr);
2742 return 0;
2747 * show_rev
2749 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2750 char *buf)
2752 struct nes_ib_device *nesibdev =
2753 container_of(dev, struct nes_ib_device, ibdev.dev);
2754 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2756 nes_debug(NES_DBG_INIT, "\n");
2757 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2762 * show_fw_ver
2764 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2765 char *buf)
2767 struct nes_ib_device *nesibdev =
2768 container_of(dev, struct nes_ib_device, ibdev.dev);
2769 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2771 nes_debug(NES_DBG_INIT, "\n");
2772 return sprintf(buf, "%u.%u\n",
2773 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2774 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
2779 * show_hca
2781 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2782 char *buf)
2784 nes_debug(NES_DBG_INIT, "\n");
2785 return sprintf(buf, "NES020\n");
2790 * show_board
2792 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2793 char *buf)
2795 nes_debug(NES_DBG_INIT, "\n");
2796 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2800 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2801 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2802 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2803 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2805 static struct device_attribute *nes_dev_attributes[] = {
2806 &dev_attr_hw_rev,
2807 &dev_attr_fw_ver,
2808 &dev_attr_hca_type,
2809 &dev_attr_board_id
2814 * nes_query_qp
2816 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2817 int attr_mask, struct ib_qp_init_attr *init_attr)
2819 struct nes_qp *nesqp = to_nesqp(ibqp);
2821 nes_debug(NES_DBG_QP, "\n");
2823 attr->qp_access_flags = 0;
2824 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2825 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2826 attr->cap.max_recv_sge = 1;
2827 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2828 attr->cap.max_inline_data = 0;
2829 else
2830 attr->cap.max_inline_data = 64;
2832 init_attr->event_handler = nesqp->ibqp.event_handler;
2833 init_attr->qp_context = nesqp->ibqp.qp_context;
2834 init_attr->send_cq = nesqp->ibqp.send_cq;
2835 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2836 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2837 init_attr->cap = attr->cap;
2839 return 0;
2844 * nes_hw_modify_qp
2846 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2847 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2849 struct nes_hw_cqp_wqe *cqp_wqe;
2850 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2851 /* struct iw_cm_event cm_event; */
2852 struct nes_cqp_request *cqp_request;
2853 int ret;
2854 u16 major_code;
2856 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2857 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2859 cqp_request = nes_get_cqp_request(nesdev);
2860 if (cqp_request == NULL) {
2861 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2862 return -ENOMEM;
2864 if (wait_completion) {
2865 cqp_request->waiting = 1;
2866 } else {
2867 cqp_request->waiting = 0;
2869 cqp_wqe = &cqp_request->cqp_wqe;
2871 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2872 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2873 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2874 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2875 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2876 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2877 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2879 /* If sending a terminate message, fill in the length (in words) */
2880 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2881 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2882 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2883 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2886 atomic_set(&cqp_request->refcount, 2);
2887 nes_post_cqp_request(nesdev, cqp_request);
2889 /* Wait for CQP */
2890 if (wait_completion) {
2891 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2892 nesqp->hwqp.qp_id); */
2893 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2894 NES_EVENT_TIMEOUT);
2895 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2896 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2897 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2898 major_code = cqp_request->major_code;
2899 if (major_code) {
2900 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2901 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2902 nesqp->hwqp.qp_id, cqp_request->major_code,
2903 cqp_request->minor_code, next_iwarp_state);
2906 nes_put_cqp_request(nesdev, cqp_request);
2908 if (!ret)
2909 return -ETIME;
2910 else if (major_code)
2911 return -EIO;
2912 else
2913 return 0;
2914 } else {
2915 return 0;
2921 * nes_modify_qp
2923 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2924 int attr_mask, struct ib_udata *udata)
2926 struct nes_qp *nesqp = to_nesqp(ibqp);
2927 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2928 struct nes_device *nesdev = nesvnic->nesdev;
2929 /* u32 cqp_head; */
2930 /* u32 counter; */
2931 u32 next_iwarp_state = 0;
2932 int err;
2933 unsigned long qplockflags;
2934 int ret;
2935 u16 original_last_aeq;
2936 u8 issue_modify_qp = 0;
2937 u8 dont_wait = 0;
2939 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2940 " iwarp_state=0x%X, refcount=%d\n",
2941 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2942 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2944 spin_lock_irqsave(&nesqp->lock, qplockflags);
2946 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2947 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2948 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2949 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2951 if (attr_mask & IB_QP_STATE) {
2952 switch (attr->qp_state) {
2953 case IB_QPS_INIT:
2954 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2955 nesqp->hwqp.qp_id);
2956 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2957 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2958 return -EINVAL;
2960 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2961 issue_modify_qp = 1;
2962 break;
2963 case IB_QPS_RTR:
2964 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2965 nesqp->hwqp.qp_id);
2966 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2967 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2968 return -EINVAL;
2970 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2971 issue_modify_qp = 1;
2972 break;
2973 case IB_QPS_RTS:
2974 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2975 nesqp->hwqp.qp_id);
2976 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2977 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2978 return -EINVAL;
2980 if (nesqp->cm_id == NULL) {
2981 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2982 nesqp->hwqp.qp_id );
2983 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2984 return -EINVAL;
2986 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2987 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2988 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2989 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2990 issue_modify_qp = 1;
2991 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2992 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2993 nesqp->hte_added = 1;
2994 break;
2995 case IB_QPS_SQD:
2996 issue_modify_qp = 1;
2997 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2998 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2999 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3000 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3001 return 0;
3002 } else {
3003 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3004 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3005 " ignored due to current iWARP state\n",
3006 nesqp->hwqp.qp_id);
3007 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3008 return -EINVAL;
3010 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3011 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3012 " already done based on hw state.\n",
3013 nesqp->hwqp.qp_id);
3014 issue_modify_qp = 0;
3016 switch (nesqp->hw_iwarp_state) {
3017 case NES_AEQE_IWARP_STATE_CLOSING:
3018 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3019 break;
3020 case NES_AEQE_IWARP_STATE_TERMINATE:
3021 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3022 break;
3023 case NES_AEQE_IWARP_STATE_ERROR:
3024 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3025 break;
3026 default:
3027 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3028 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3029 break;
3032 break;
3033 case IB_QPS_SQE:
3034 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3035 nesqp->hwqp.qp_id);
3036 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3037 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3038 return -EINVAL;
3040 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3041 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3042 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3043 issue_modify_qp = 1;
3044 break;
3045 case IB_QPS_ERR:
3046 case IB_QPS_RESET:
3047 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3048 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3049 return -EINVAL;
3051 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3052 nesqp->hwqp.qp_id);
3053 if (nesqp->term_flags)
3054 del_timer(&nesqp->terminate_timer);
3056 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3057 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3058 if (nesqp->hte_added) {
3059 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3060 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3061 nesqp->hte_added = 0;
3063 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3064 (nesdev->iw_status) &&
3065 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3066 next_iwarp_state |= NES_CQP_QP_RESET;
3067 } else {
3068 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3069 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3070 dont_wait = 1;
3072 issue_modify_qp = 1;
3073 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3074 break;
3075 default:
3076 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3077 return -EINVAL;
3078 break;
3081 nesqp->ibqp_state = attr->qp_state;
3082 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3083 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3084 nesqp->iwarp_state);
3087 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3088 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3089 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3090 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3091 issue_modify_qp = 1;
3093 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3094 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3095 issue_modify_qp = 1;
3097 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3098 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3099 issue_modify_qp = 1;
3101 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3102 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3103 issue_modify_qp = 1;
3106 if (nesqp->user_mode) {
3107 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3108 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3109 issue_modify_qp = 1;
3113 original_last_aeq = nesqp->last_aeq;
3114 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3116 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3118 ret = 0;
3121 if (issue_modify_qp) {
3122 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3123 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
3124 if (ret)
3125 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3126 " failed for QP%u.\n",
3127 next_iwarp_state, nesqp->hwqp.qp_id);
3131 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3132 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3133 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3134 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3135 original_last_aeq, nesqp->last_aeq);
3136 if ((!ret) ||
3137 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3138 (ret))) {
3139 if (dont_wait) {
3140 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3141 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3142 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3143 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3144 original_last_aeq, nesqp->last_aeq);
3145 /* this one is for the cm_disconnect thread */
3146 spin_lock_irqsave(&nesqp->lock, qplockflags);
3147 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3148 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3149 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3150 nes_cm_disconn(nesqp);
3151 } else {
3152 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3153 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3155 } else {
3156 spin_lock_irqsave(&nesqp->lock, qplockflags);
3157 if (nesqp->cm_id) {
3158 /* These two are for the timer thread */
3159 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3160 nesqp->cm_id->add_ref(nesqp->cm_id);
3161 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3162 " need ae to finish up, original_last_aeq = 0x%04X."
3163 " last_aeq = 0x%04X, scheduling timer.\n",
3164 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3165 original_last_aeq, nesqp->last_aeq);
3166 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3168 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3169 } else {
3170 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3171 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3172 " need ae to finish up, original_last_aeq = 0x%04X."
3173 " last_aeq = 0x%04X.\n",
3174 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3175 original_last_aeq, nesqp->last_aeq);
3178 } else {
3179 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3180 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3181 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3182 original_last_aeq, nesqp->last_aeq);
3184 } else {
3185 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3186 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3187 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3188 original_last_aeq, nesqp->last_aeq);
3191 err = 0;
3193 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3194 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3196 return err;
3201 * nes_muticast_attach
3203 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3205 nes_debug(NES_DBG_INIT, "\n");
3206 return -ENOSYS;
3211 * nes_multicast_detach
3213 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3215 nes_debug(NES_DBG_INIT, "\n");
3216 return -ENOSYS;
3221 * nes_process_mad
3223 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3224 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3225 struct ib_mad *in_mad, struct ib_mad *out_mad)
3227 nes_debug(NES_DBG_INIT, "\n");
3228 return -ENOSYS;
3231 static inline void
3232 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3234 int sge_index;
3235 int total_payload_length = 0;
3236 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3237 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3238 ib_wr->sg_list[sge_index].addr);
3239 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3240 ib_wr->sg_list[sge_index].length);
3241 if (uselkey)
3242 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3243 (ib_wr->sg_list[sge_index].lkey));
3244 else
3245 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3247 total_payload_length += ib_wr->sg_list[sge_index].length;
3249 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3250 total_payload_length);
3251 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3252 total_payload_length);
3256 * nes_post_send
3258 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3259 struct ib_send_wr **bad_wr)
3261 u64 u64temp;
3262 unsigned long flags = 0;
3263 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3264 struct nes_device *nesdev = nesvnic->nesdev;
3265 struct nes_qp *nesqp = to_nesqp(ibqp);
3266 struct nes_hw_qp_wqe *wqe;
3267 int err = 0;
3268 u32 qsize = nesqp->hwqp.sq_size;
3269 u32 head;
3270 u32 wqe_misc = 0;
3271 u32 wqe_count = 0;
3272 u32 counter;
3274 if (nesqp->ibqp_state > IB_QPS_RTS) {
3275 err = -EINVAL;
3276 goto out;
3279 spin_lock_irqsave(&nesqp->lock, flags);
3281 head = nesqp->hwqp.sq_head;
3283 while (ib_wr) {
3284 /* Check for QP error */
3285 if (nesqp->term_flags) {
3286 err = -EINVAL;
3287 break;
3290 /* Check for SQ overflow */
3291 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3292 err = -ENOMEM;
3293 break;
3296 wqe = &nesqp->hwqp.sq_vbase[head];
3297 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3298 nesqp->hwqp.qp_id, wqe, head); */
3299 nes_fill_init_qp_wqe(wqe, nesqp, head);
3300 u64temp = (u64)(ib_wr->wr_id);
3301 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3302 u64temp);
3303 switch (ib_wr->opcode) {
3304 case IB_WR_SEND:
3305 case IB_WR_SEND_WITH_INV:
3306 if (IB_WR_SEND == ib_wr->opcode) {
3307 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3308 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3309 else
3310 wqe_misc = NES_IWARP_SQ_OP_SEND;
3311 } else {
3312 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3313 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3314 else
3315 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
3317 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3318 ib_wr->ex.invalidate_rkey);
3321 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3322 err = -EINVAL;
3323 break;
3326 if (ib_wr->send_flags & IB_SEND_FENCE)
3327 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3329 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3330 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3331 (ib_wr->sg_list[0].length <= 64)) {
3332 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3333 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3334 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3335 ib_wr->sg_list[0].length);
3336 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3337 } else {
3338 fill_wqe_sg_send(wqe, ib_wr, 1);
3341 break;
3342 case IB_WR_RDMA_WRITE:
3343 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3344 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3345 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3346 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3347 err = -EINVAL;
3348 break;
3351 if (ib_wr->send_flags & IB_SEND_FENCE)
3352 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3354 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3355 ib_wr->wr.rdma.rkey);
3356 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3357 ib_wr->wr.rdma.remote_addr);
3359 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3360 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3361 (ib_wr->sg_list[0].length <= 64)) {
3362 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3363 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3364 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3365 ib_wr->sg_list[0].length);
3366 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3367 } else {
3368 fill_wqe_sg_send(wqe, ib_wr, 1);
3371 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3372 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3373 break;
3374 case IB_WR_RDMA_READ:
3375 case IB_WR_RDMA_READ_WITH_INV:
3376 /* iWARP only supports 1 sge for RDMA reads */
3377 if (ib_wr->num_sge > 1) {
3378 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3379 ib_wr->num_sge);
3380 err = -EINVAL;
3381 break;
3383 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3384 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3385 } else {
3386 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3387 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3388 ib_wr->ex.invalidate_rkey);
3391 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3392 ib_wr->wr.rdma.remote_addr);
3393 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3394 ib_wr->wr.rdma.rkey);
3395 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3396 ib_wr->sg_list->length);
3397 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3398 ib_wr->sg_list->addr);
3399 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3400 ib_wr->sg_list->lkey);
3401 break;
3402 case IB_WR_LOCAL_INV:
3403 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3404 set_wqe_32bit_value(wqe->wqe_words,
3405 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3406 ib_wr->ex.invalidate_rkey);
3407 break;
3408 case IB_WR_FAST_REG_MR:
3410 int i;
3411 int flags = ib_wr->wr.fast_reg.access_flags;
3412 struct nes_ib_fast_reg_page_list *pnesfrpl =
3413 container_of(ib_wr->wr.fast_reg.page_list,
3414 struct nes_ib_fast_reg_page_list,
3415 ibfrpl);
3416 u64 *src_page_list = pnesfrpl->ibfrpl.page_list;
3417 u64 *dst_page_list = pnesfrpl->nes_wqe_pbl.kva;
3419 if (ib_wr->wr.fast_reg.page_list_len >
3420 (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3421 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3422 err = -EINVAL;
3423 break;
3425 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3426 set_wqe_64bit_value(wqe->wqe_words,
3427 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3428 ib_wr->wr.fast_reg.iova_start);
3429 set_wqe_32bit_value(wqe->wqe_words,
3430 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3431 ib_wr->wr.fast_reg.length);
3432 set_wqe_32bit_value(wqe->wqe_words,
3433 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3434 set_wqe_32bit_value(wqe->wqe_words,
3435 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3436 ib_wr->wr.fast_reg.rkey);
3437 /* Set page size: */
3438 if (ib_wr->wr.fast_reg.page_shift == 12) {
3439 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3440 } else if (ib_wr->wr.fast_reg.page_shift == 21) {
3441 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3442 } else {
3443 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3444 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3445 err = -EINVAL;
3446 break;
3448 /* Set access_flags */
3449 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3450 if (flags & IB_ACCESS_LOCAL_WRITE)
3451 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3453 if (flags & IB_ACCESS_REMOTE_WRITE)
3454 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3456 if (flags & IB_ACCESS_REMOTE_READ)
3457 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3459 if (flags & IB_ACCESS_MW_BIND)
3460 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3462 /* Fill in PBL info: */
3463 if (ib_wr->wr.fast_reg.page_list_len >
3464 pnesfrpl->ibfrpl.max_page_list_len) {
3465 nes_debug(NES_DBG_IW_TX, "Invalid page list length,"
3466 " ib_wr=%p, value=%u, max=%u\n",
3467 ib_wr, ib_wr->wr.fast_reg.page_list_len,
3468 pnesfrpl->ibfrpl.max_page_list_len);
3469 err = -EINVAL;
3470 break;
3473 set_wqe_64bit_value(wqe->wqe_words,
3474 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3475 pnesfrpl->nes_wqe_pbl.paddr);
3477 set_wqe_32bit_value(wqe->wqe_words,
3478 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3479 ib_wr->wr.fast_reg.page_list_len * 8);
3481 for (i = 0; i < ib_wr->wr.fast_reg.page_list_len; i++)
3482 dst_page_list[i] = cpu_to_le64(src_page_list[i]);
3484 nes_debug(NES_DBG_IW_TX, "SQ_FMR: iova_start: %llx, "
3485 "length: %d, rkey: %0x, pgl_paddr: %llx, "
3486 "page_list_len: %u, wqe_misc: %x\n",
3487 (unsigned long long) ib_wr->wr.fast_reg.iova_start,
3488 ib_wr->wr.fast_reg.length,
3489 ib_wr->wr.fast_reg.rkey,
3490 (unsigned long long) pnesfrpl->nes_wqe_pbl.paddr,
3491 ib_wr->wr.fast_reg.page_list_len,
3492 wqe_misc);
3493 break;
3495 default:
3496 /* error */
3497 err = -EINVAL;
3498 break;
3501 if (err)
3502 break;
3504 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3505 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3507 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3509 ib_wr = ib_wr->next;
3510 head++;
3511 wqe_count++;
3512 if (head >= qsize)
3513 head = 0;
3517 nesqp->hwqp.sq_head = head;
3518 barrier();
3519 while (wqe_count) {
3520 counter = min(wqe_count, ((u32)255));
3521 wqe_count -= counter;
3522 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3523 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3526 spin_unlock_irqrestore(&nesqp->lock, flags);
3528 out:
3529 if (err)
3530 *bad_wr = ib_wr;
3531 return err;
3536 * nes_post_recv
3538 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3539 struct ib_recv_wr **bad_wr)
3541 u64 u64temp;
3542 unsigned long flags = 0;
3543 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3544 struct nes_device *nesdev = nesvnic->nesdev;
3545 struct nes_qp *nesqp = to_nesqp(ibqp);
3546 struct nes_hw_qp_wqe *wqe;
3547 int err = 0;
3548 int sge_index;
3549 u32 qsize = nesqp->hwqp.rq_size;
3550 u32 head;
3551 u32 wqe_count = 0;
3552 u32 counter;
3553 u32 total_payload_length;
3555 if (nesqp->ibqp_state > IB_QPS_RTS) {
3556 err = -EINVAL;
3557 goto out;
3560 spin_lock_irqsave(&nesqp->lock, flags);
3562 head = nesqp->hwqp.rq_head;
3564 while (ib_wr) {
3565 /* Check for QP error */
3566 if (nesqp->term_flags) {
3567 err = -EINVAL;
3568 break;
3571 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3572 err = -EINVAL;
3573 break;
3575 /* Check for RQ overflow */
3576 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3577 err = -ENOMEM;
3578 break;
3581 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3582 wqe = &nesqp->hwqp.rq_vbase[head];
3584 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3585 nesqp->hwqp.qp_id, wqe, head); */
3586 nes_fill_init_qp_wqe(wqe, nesqp, head);
3587 u64temp = (u64)(ib_wr->wr_id);
3588 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3589 u64temp);
3590 total_payload_length = 0;
3591 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3592 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3593 ib_wr->sg_list[sge_index].addr);
3594 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3595 ib_wr->sg_list[sge_index].length);
3596 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3597 ib_wr->sg_list[sge_index].lkey);
3599 total_payload_length += ib_wr->sg_list[sge_index].length;
3601 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3602 total_payload_length);
3604 ib_wr = ib_wr->next;
3605 head++;
3606 wqe_count++;
3607 if (head >= qsize)
3608 head = 0;
3611 nesqp->hwqp.rq_head = head;
3612 barrier();
3613 while (wqe_count) {
3614 counter = min(wqe_count, ((u32)255));
3615 wqe_count -= counter;
3616 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3619 spin_unlock_irqrestore(&nesqp->lock, flags);
3621 out:
3622 if (err)
3623 *bad_wr = ib_wr;
3624 return err;
3629 * nes_poll_cq
3631 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3633 u64 u64temp;
3634 u64 wrid;
3635 unsigned long flags = 0;
3636 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3637 struct nes_device *nesdev = nesvnic->nesdev;
3638 struct nes_cq *nescq = to_nescq(ibcq);
3639 struct nes_qp *nesqp;
3640 struct nes_hw_cqe cqe;
3641 u32 head;
3642 u32 wq_tail = 0;
3643 u32 cq_size;
3644 u32 cqe_count = 0;
3645 u32 wqe_index;
3646 u32 u32temp;
3647 u32 move_cq_head = 1;
3648 u32 err_code;
3650 nes_debug(NES_DBG_CQ, "\n");
3652 spin_lock_irqsave(&nescq->lock, flags);
3654 head = nescq->hw_cq.cq_head;
3655 cq_size = nescq->hw_cq.cq_size;
3657 while (cqe_count < num_entries) {
3658 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3659 NES_CQE_VALID) == 0)
3660 break;
3663 * Make sure we read CQ entry contents *after*
3664 * we've checked the valid bit.
3666 rmb();
3668 cqe = nescq->hw_cq.cq_vbase[head];
3669 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3670 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3671 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3672 /* parse CQE, get completion context from WQE (either rq or sq) */
3673 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3674 ((u64)u32temp);
3676 if (u64temp) {
3677 nesqp = (struct nes_qp *)(unsigned long)u64temp;
3678 memset(entry, 0, sizeof *entry);
3679 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3680 entry->status = IB_WC_SUCCESS;
3681 } else {
3682 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3683 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3684 entry->status = err_code & 0x0000ffff;
3686 /* The rest of the cqe's will be marked as flushed */
3687 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3688 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3689 NES_IWARP_CQE_MINOR_FLUSH);
3690 } else
3691 entry->status = IB_WC_WR_FLUSH_ERR;
3694 entry->qp = &nesqp->ibqp;
3695 entry->src_qp = nesqp->hwqp.qp_id;
3697 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3698 if (nesqp->skip_lsmm) {
3699 nesqp->skip_lsmm = 0;
3700 nesqp->hwqp.sq_tail++;
3703 /* Working on a SQ Completion*/
3704 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3705 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3706 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3707 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3708 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3709 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3711 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3712 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3713 case NES_IWARP_SQ_OP_RDMAW:
3714 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3715 entry->opcode = IB_WC_RDMA_WRITE;
3716 break;
3717 case NES_IWARP_SQ_OP_RDMAR:
3718 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3719 entry->opcode = IB_WC_RDMA_READ;
3720 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3721 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3722 break;
3723 case NES_IWARP_SQ_OP_SENDINV:
3724 case NES_IWARP_SQ_OP_SENDSEINV:
3725 case NES_IWARP_SQ_OP_SEND:
3726 case NES_IWARP_SQ_OP_SENDSE:
3727 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3728 entry->opcode = IB_WC_SEND;
3729 break;
3730 case NES_IWARP_SQ_OP_LOCINV:
3731 entry->opcode = IB_WC_LOCAL_INV;
3732 break;
3733 case NES_IWARP_SQ_OP_FAST_REG:
3734 entry->opcode = IB_WC_FAST_REG_MR;
3735 break;
3738 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3739 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3740 move_cq_head = 0;
3741 wq_tail = nesqp->hwqp.sq_tail;
3743 } else {
3744 /* Working on a RQ Completion*/
3745 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3746 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3747 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3748 entry->opcode = IB_WC_RECV;
3750 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3751 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3752 move_cq_head = 0;
3753 wq_tail = nesqp->hwqp.rq_tail;
3757 entry->wr_id = wrid;
3758 entry++;
3759 cqe_count++;
3762 if (move_cq_head) {
3763 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3764 if (++head >= cq_size)
3765 head = 0;
3766 nescq->polled_completions++;
3768 if ((nescq->polled_completions > (cq_size / 2)) ||
3769 (nescq->polled_completions == 255)) {
3770 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3771 " are pending %u of %u.\n",
3772 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3773 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3774 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3775 nescq->polled_completions = 0;
3777 } else {
3778 /* Update the wqe index and set status to flush */
3779 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3780 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3781 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3782 cpu_to_le32(wqe_index);
3783 move_cq_head = 1; /* ready for next pass */
3787 if (nescq->polled_completions) {
3788 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3789 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3790 nescq->polled_completions = 0;
3793 nescq->hw_cq.cq_head = head;
3794 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3795 cqe_count, nescq->hw_cq.cq_number);
3797 spin_unlock_irqrestore(&nescq->lock, flags);
3799 return cqe_count;
3804 * nes_req_notify_cq
3806 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3808 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3809 struct nes_device *nesdev = nesvnic->nesdev;
3810 struct nes_cq *nescq = to_nescq(ibcq);
3811 u32 cq_arm;
3813 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3814 nescq->hw_cq.cq_number);
3816 cq_arm = nescq->hw_cq.cq_number;
3817 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3818 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3819 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3820 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3821 else
3822 return -EINVAL;
3824 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3825 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3827 return 0;
3832 * nes_init_ofa_device
3834 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3836 struct nes_ib_device *nesibdev;
3837 struct nes_vnic *nesvnic = netdev_priv(netdev);
3838 struct nes_device *nesdev = nesvnic->nesdev;
3840 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3841 if (nesibdev == NULL) {
3842 return NULL;
3844 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3845 nesibdev->ibdev.owner = THIS_MODULE;
3847 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3848 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3849 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3851 nesibdev->ibdev.uverbs_cmd_mask =
3852 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3853 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3854 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3855 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3856 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3857 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3858 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3859 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3860 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3861 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3862 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3863 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3864 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3865 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3866 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3867 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3868 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3869 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3870 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3871 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3872 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3873 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3875 nesibdev->ibdev.phys_port_cnt = 1;
3876 nesibdev->ibdev.num_comp_vectors = 1;
3877 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3878 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3879 nesibdev->ibdev.query_device = nes_query_device;
3880 nesibdev->ibdev.query_port = nes_query_port;
3881 nesibdev->ibdev.query_pkey = nes_query_pkey;
3882 nesibdev->ibdev.query_gid = nes_query_gid;
3883 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3884 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3885 nesibdev->ibdev.mmap = nes_mmap;
3886 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3887 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3888 nesibdev->ibdev.create_ah = nes_create_ah;
3889 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3890 nesibdev->ibdev.create_qp = nes_create_qp;
3891 nesibdev->ibdev.modify_qp = nes_modify_qp;
3892 nesibdev->ibdev.query_qp = nes_query_qp;
3893 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3894 nesibdev->ibdev.create_cq = nes_create_cq;
3895 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3896 nesibdev->ibdev.poll_cq = nes_poll_cq;
3897 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3898 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3899 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3900 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3901 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3902 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3903 nesibdev->ibdev.bind_mw = nes_bind_mw;
3905 nesibdev->ibdev.alloc_fast_reg_mr = nes_alloc_fast_reg_mr;
3906 nesibdev->ibdev.alloc_fast_reg_page_list = nes_alloc_fast_reg_page_list;
3907 nesibdev->ibdev.free_fast_reg_page_list = nes_free_fast_reg_page_list;
3909 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3910 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3911 nesibdev->ibdev.process_mad = nes_process_mad;
3913 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3914 nesibdev->ibdev.post_send = nes_post_send;
3915 nesibdev->ibdev.post_recv = nes_post_recv;
3917 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3918 if (nesibdev->ibdev.iwcm == NULL) {
3919 ib_dealloc_device(&nesibdev->ibdev);
3920 return NULL;
3922 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3923 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3924 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3925 nesibdev->ibdev.iwcm->connect = nes_connect;
3926 nesibdev->ibdev.iwcm->accept = nes_accept;
3927 nesibdev->ibdev.iwcm->reject = nes_reject;
3928 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3929 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3931 return nesibdev;
3936 * nes_handle_delayed_event
3938 static void nes_handle_delayed_event(unsigned long data)
3940 struct nes_vnic *nesvnic = (void *) data;
3942 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3943 struct ib_event event;
3945 event.device = &nesvnic->nesibdev->ibdev;
3946 if (!event.device)
3947 goto stop_timer;
3948 event.event = nesvnic->delayed_event;
3949 event.element.port_num = nesvnic->logical_port + 1;
3950 ib_dispatch_event(&event);
3953 stop_timer:
3954 nesvnic->event_timer.function = NULL;
3958 void nes_port_ibevent(struct nes_vnic *nesvnic)
3960 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3961 struct nes_device *nesdev = nesvnic->nesdev;
3962 struct ib_event event;
3963 event.device = &nesibdev->ibdev;
3964 event.element.port_num = nesvnic->logical_port + 1;
3965 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3967 if (!nesvnic->event_timer.function) {
3968 ib_dispatch_event(&event);
3969 nesvnic->last_dispatched_event = event.event;
3970 nesvnic->event_timer.function = nes_handle_delayed_event;
3971 nesvnic->event_timer.data = (unsigned long) nesvnic;
3972 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3973 add_timer(&nesvnic->event_timer);
3974 } else {
3975 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3977 nesvnic->delayed_event = event.event;
3982 * nes_destroy_ofa_device
3984 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3986 if (nesibdev == NULL)
3987 return;
3989 nes_unregister_ofa_device(nesibdev);
3991 kfree(nesibdev->ibdev.iwcm);
3992 ib_dealloc_device(&nesibdev->ibdev);
3997 * nes_register_ofa_device
3999 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
4001 struct nes_vnic *nesvnic = nesibdev->nesvnic;
4002 struct nes_device *nesdev = nesvnic->nesdev;
4003 struct nes_adapter *nesadapter = nesdev->nesadapter;
4004 int i, ret;
4006 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
4007 if (ret) {
4008 return ret;
4011 /* Get the resources allocated to this device */
4012 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
4013 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
4014 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
4015 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
4017 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4018 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
4019 if (ret) {
4020 while (i > 0) {
4021 i--;
4022 device_remove_file(&nesibdev->ibdev.dev,
4023 nes_dev_attributes[i]);
4025 ib_unregister_device(&nesibdev->ibdev);
4026 return ret;
4030 nesvnic->of_device_registered = 1;
4032 return 0;
4037 * nes_unregister_ofa_device
4039 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
4041 struct nes_vnic *nesvnic = nesibdev->nesvnic;
4042 int i;
4044 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
4045 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
4048 if (nesvnic->of_device_registered) {
4049 ib_unregister_device(&nesibdev->ibdev);
4052 nesvnic->of_device_registered = 0;