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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
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>
47 #include <rdma/ib_umem.h>
49 atomic_t mod_qp_timouts
;
51 atomic_t sw_qps_destroyed
;
53 static void nes_unregister_ofa_device(struct nes_ib_device
*nesibdev
);
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
;
67 struct nes_hw_cqp_wqe
*cqp_wqe
;
71 u32 next_stag_index
= 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
;
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
);
92 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
94 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
95 return ERR_PTR(-ENOMEM
);
98 stag
= stag_index
<< 8;
100 stag
+= (u32
)stag_key
;
102 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, index = 0x%08X\n",
105 /* Register the region with the adapter */
106 cqp_request
= nes_get_cqp_request(nesdev
);
107 if (cqp_request
== NULL
) {
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
);
129 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
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
);
137 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
139 return ERR_PTR(-ETIME
);
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
;
150 nesmr
->pbls_used
= 0;
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
;
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");
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
);
186 nes_debug(NES_DBG_MR
, "Waiting for deallocate STag 0x%08X to complete.\n",
188 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
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
);
195 else if (cqp_request
->major_code
)
198 nes_put_cqp_request(nesdev
, cqp_request
);
200 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
201 (ibmw
->rkey
& 0x0fffff00) >> 8);
211 static int nes_bind_mw(struct ib_qp
*ibqp
, struct ib_mw
*ibmw
,
212 struct ib_mw_bind
*ibmw_bind
)
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;
225 if (nesqp
->ibqp_state
> IB_QPS_RTS
)
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
);
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
);
270 nesqp
->hwqp
.sq_head
= head
;
273 nes_write32(nesdev
->regs
+NES_WQE_ALLOC
,
274 (1 << 24) | 0x00800000 | nesqp
->hwqp
.qp_id
);
276 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
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
;
292 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
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");
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
);
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
);
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
);
349 atomic_set(&cqp_request
->refcount
, 2);
350 nes_post_cqp_request(nesdev
, cqp_request
);
353 ret
= wait_event_timeout(cqp_request
->waitq
,
354 (0 != cqp_request
->request_done
),
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
);
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
;
392 struct nes_mr
*nesmr
;
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
);
414 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
416 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
417 return ERR_PTR(-ENOMEM
);
420 stag
= stag_index
<< 8;
422 stag
+= (u32
)stag_key
;
424 nes_debug(NES_DBG_MR
, "Allocating STag 0x%08X index = 0x%08X\n",
427 ret
= alloc_fast_reg_mr(nesdev
, nespd
, stag
, max_page_list_len
);
430 nesmr
->ibmr
.rkey
= stag
;
431 nesmr
->ibmr
.lkey
= stag
;
432 nesmr
->mode
= IWNES_MEMREG_TYPE_FMEM
;
436 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
437 ibmr
= ERR_PTR(-ENOMEM
);
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
,
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
);
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
) {
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
);
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
);
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
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
) {
541 props
->max_qp_rd_atom
= 2;
544 props
->max_qp_rd_atom
= 8;
547 props
->max_qp_rd_atom
= 32;
550 props
->max_qp_rd_atom
= 64;
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;
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
;
584 props
->active_mtu
= IB_MTU_256
;
590 if (netif_queue_stopped(netdev
))
591 props
->state
= IB_PORT_DOWN
;
592 else if (nesvnic
->linkup
)
593 props
->state
= IB_PORT_ACTIVE
;
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;
613 static int nes_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
, u16
*pkey
)
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);
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
);
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) /
682 if (ib_copy_to_udata(udata
, &uresp
, sizeof uresp
)) {
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
))
713 static int nes_mmap(struct ib_ucontext
*context
, struct vm_area_struct
*vma
)
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
);
733 nesqp
= nes_ucontext
->mmap_nesqp
[index
];
735 nes_debug(NES_DBG_MMAP
, "wq %lu has a NULL QP base.\n", index
);
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");
745 vma
->vm_private_data
= nesqp
;
748 index
= vma
->vm_pgoff
;
749 if (!test_bit(index
, nes_ucontext
->allocated_doorbells
))
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
))
758 vma
->vm_private_data
= nes_ucontext
;
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
;
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
);
791 nespd
= kzalloc(sizeof (struct nes_pd
), GFP_KERNEL
);
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
;
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
);
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
);
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
);
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));
868 static struct ib_ah
*nes_create_ah(struct ib_pd
*pd
, struct ib_ah_attr
*ah_attr
)
870 return ERR_PTR(-ENOSYS
);
877 static int nes_destroy_ah(struct ib_ah
*ah
)
884 * nes_get_encoded_size
886 static inline u8
nes_get_encoded_size(int *size
)
892 } else if (*size
<= 128) {
895 } else if (*size
<= 512) {
899 return (encoded_size
);
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
)
915 struct nes_device
*nesdev
= nesvnic
->nesdev
;
916 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
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);
933 nes_debug(NES_DBG_QP
, "QP nespbl->page is NULL \n");
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");
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
,
960 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
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
,
975 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
976 nesadapter
->free_256pbl
++;
977 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
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
--)
988 while (rq_pbl_entries
--)
991 /* done with memory allocated during nes_reg_user_mr() */
992 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
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
);
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
);
1014 nesqp
->sq_kmapped
= 1;
1015 nesqp
->hwqp
.q2_vbase
= mem
;
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;
1029 static int nes_setup_mmap_qp(struct nes_qp
*nesqp
, struct nes_vnic
*nesvnic
,
1030 int sq_size
, int rq_size
)
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
);
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
;
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
));
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
;
1083 pci_free_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
1084 nesqp
->hwqp
.sq_vbase
, nesqp
->hwqp
.sq_pbase
);
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
);
1103 static struct ib_qp
*nes_create_qp(struct ib_pd
*ibpd
,
1104 struct ib_qp_init_attr
*init_attr
, struct ib_udata
*udata
)
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
;
1122 /* u32 counter = 0; */
1124 unsigned long flags
;
1134 if (init_attr
->create_flags
)
1135 return ERR_PTR(-EINVAL
);
1137 atomic_inc(&qps_created
);
1138 switch (init_attr
->qp_type
) {
1140 if (nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) {
1141 init_attr
->cap
.max_inline_data
= 0;
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",
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
);
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
);
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
;
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");
1191 if (req
.user_wqe_buffers
) {
1194 if ((ibpd
->uobject
) && (ibpd
->uobject
->context
)) {
1195 nesqp
->user_mode
= 1;
1196 nes_ucontext
= to_nesucontext(ibpd
->uobject
->context
);
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
);
1203 nes_debug(NES_DBG_QP
, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1204 nespbl
, nespbl
->user_base
);
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
);
1228 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
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;
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
);
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
);
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));
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));
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
);
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
;
1344 opcode
= NES_CQP_CREATE_QP
| NES_CQP_QP_TYPE_IWARP
|
1345 NES_CQP_QP_IWARP_STATE_IDLE
;
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
);
1362 nes_debug(NES_DBG_QP
, "Waiting for create iWARP QP%u to complete.\n",
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
);
1377 return ERR_PTR(-ETIME
);
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
.mmap_rq_db_index
= 0;
1388 uresp
.actual_sq_size
= sq_size
;
1389 uresp
.actual_rq_size
= rq_size
;
1390 uresp
.qp_id
= nesqp
->hwqp
.qp_id
;
1391 uresp
.nes_drv_opt
= nes_drv_opt
;
1392 if (ib_copy_to_udata(udata
, &uresp
, sizeof uresp
)) {
1393 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1394 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1395 kfree(nesqp
->allocated_buffer
);
1396 return ERR_PTR(-EFAULT
);
1400 nes_debug(NES_DBG_QP
, "QP%u structure located @%p.Size = %u.\n",
1401 nesqp
->hwqp
.qp_id
, nesqp
, (u32
)sizeof(*nesqp
));
1402 spin_lock_init(&nesqp
->lock
);
1403 nes_add_ref(&nesqp
->ibqp
);
1406 nes_debug(NES_DBG_QP
, "Invalid QP type: %d\n", init_attr
->qp_type
);
1407 return ERR_PTR(-EINVAL
);
1410 nesqp
->sig_all
= (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
);
1411 init_timer(&nesqp
->terminate_timer
);
1412 nesqp
->terminate_timer
.function
= nes_terminate_timeout
;
1413 nesqp
->terminate_timer
.data
= (unsigned long)nesqp
;
1415 /* update the QP table */
1416 nesdev
->nesadapter
->qp_table
[nesqp
->hwqp
.qp_id
-NES_FIRST_QPN
] = nesqp
;
1417 nes_debug(NES_DBG_QP
, "netdev refcnt=%u\n",
1418 netdev_refcnt_read(nesvnic
->netdev
));
1420 return &nesqp
->ibqp
;
1426 static void nes_clean_cq(struct nes_qp
*nesqp
, struct nes_cq
*nescq
)
1432 unsigned long flags
= 0;
1434 spin_lock_irqsave(&nescq
->lock
, flags
);
1436 cq_head
= nescq
->hw_cq
.cq_head
;
1437 while (le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_OPCODE_IDX
]) & NES_CQE_VALID
) {
1439 lo
= le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
1440 hi
= le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
]);
1441 u64temp
= (((u64
)hi
) << 32) | ((u64
)lo
);
1442 u64temp
&= ~(NES_SW_CONTEXT_ALIGN
-1);
1443 if (u64temp
== (u64
)(unsigned long)nesqp
) {
1444 /* Zero the context value so cqe will be ignored */
1445 nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
] = 0;
1446 nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
] = 0;
1449 if (++cq_head
>= nescq
->hw_cq
.cq_size
)
1453 spin_unlock_irqrestore(&nescq
->lock
, flags
);
1460 static int nes_destroy_qp(struct ib_qp
*ibqp
)
1462 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
1463 struct nes_ucontext
*nes_ucontext
;
1464 struct ib_qp_attr attr
;
1465 struct iw_cm_id
*cm_id
;
1466 struct iw_cm_event cm_event
;
1469 atomic_inc(&sw_qps_destroyed
);
1470 nesqp
->destroyed
= 1;
1472 /* Blow away the connection if it exists. */
1473 if (nesqp
->ibqp_state
>= IB_QPS_INIT
&& nesqp
->ibqp_state
<= IB_QPS_RTS
) {
1474 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1475 attr
.qp_state
= IB_QPS_ERR
;
1476 nes_modify_qp(&nesqp
->ibqp
, &attr
, IB_QP_STATE
, NULL
);
1479 if (((nesqp
->ibqp_state
== IB_QPS_INIT
) ||
1480 (nesqp
->ibqp_state
== IB_QPS_RTR
)) && (nesqp
->cm_id
)) {
1481 cm_id
= nesqp
->cm_id
;
1482 cm_event
.event
= IW_CM_EVENT_CONNECT_REPLY
;
1483 cm_event
.status
= -ETIMEDOUT
;
1484 cm_event
.local_addr
= cm_id
->local_addr
;
1485 cm_event
.remote_addr
= cm_id
->remote_addr
;
1486 cm_event
.private_data
= NULL
;
1487 cm_event
.private_data_len
= 0;
1489 nes_debug(NES_DBG_QP
, "Generating a CM Timeout Event for "
1490 "QP%u. cm_id = %p, refcount = %u. \n",
1491 nesqp
->hwqp
.qp_id
, cm_id
, atomic_read(&nesqp
->refcount
));
1493 cm_id
->rem_ref(cm_id
);
1494 ret
= cm_id
->event_handler(cm_id
, &cm_event
);
1496 nes_debug(NES_DBG_QP
, "OFA CM event_handler returned, ret=%d\n", ret
);
1499 if (nesqp
->user_mode
) {
1500 if ((ibqp
->uobject
)&&(ibqp
->uobject
->context
)) {
1501 nes_ucontext
= to_nesucontext(ibqp
->uobject
->context
);
1502 clear_bit(nesqp
->mmap_sq_db_index
, nes_ucontext
->allocated_wqs
);
1503 nes_ucontext
->mmap_nesqp
[nesqp
->mmap_sq_db_index
] = NULL
;
1504 if (nes_ucontext
->first_free_wq
> nesqp
->mmap_sq_db_index
) {
1505 nes_ucontext
->first_free_wq
= nesqp
->mmap_sq_db_index
;
1508 if (nesqp
->pbl_pbase
&& nesqp
->sq_kmapped
) {
1509 nesqp
->sq_kmapped
= 0;
1510 kunmap(nesqp
->page
);
1513 /* Clean any pending completions from the cq(s) */
1515 nes_clean_cq(nesqp
, nesqp
->nesscq
);
1517 if ((nesqp
->nesrcq
) && (nesqp
->nesrcq
!= nesqp
->nesscq
))
1518 nes_clean_cq(nesqp
, nesqp
->nesrcq
);
1520 nes_rem_ref(&nesqp
->ibqp
);
1528 static struct ib_cq
*nes_create_cq(struct ib_device
*ibdev
, int entries
,
1530 struct ib_ucontext
*context
, struct ib_udata
*udata
)
1533 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
1534 struct nes_device
*nesdev
= nesvnic
->nesdev
;
1535 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1536 struct nes_cq
*nescq
;
1537 struct nes_ucontext
*nes_ucontext
= NULL
;
1538 struct nes_cqp_request
*cqp_request
;
1540 struct nes_hw_cqp_wqe
*cqp_wqe
;
1541 struct nes_pbl
*nespbl
= NULL
;
1542 struct nes_create_cq_req req
;
1543 struct nes_create_cq_resp resp
;
1546 u32 pbl_entries
= 1;
1548 unsigned long flags
;
1551 if (entries
> nesadapter
->max_cqe
)
1552 return ERR_PTR(-EINVAL
);
1554 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_cqs
,
1555 nesadapter
->max_cq
, &cq_num
, &nesadapter
->next_cq
, NES_RESOURCE_CQ
);
1557 return ERR_PTR(err
);
1560 nescq
= kzalloc(sizeof(struct nes_cq
), GFP_KERNEL
);
1562 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1563 nes_debug(NES_DBG_CQ
, "Unable to allocate nes_cq struct\n");
1564 return ERR_PTR(-ENOMEM
);
1567 nescq
->hw_cq
.cq_size
= max(entries
+ 1, 5);
1568 nescq
->hw_cq
.cq_number
= cq_num
;
1569 nescq
->ibcq
.cqe
= nescq
->hw_cq
.cq_size
- 1;
1573 nes_ucontext
= to_nesucontext(context
);
1574 if (ib_copy_from_udata(&req
, udata
, sizeof (struct nes_create_cq_req
))) {
1575 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1577 return ERR_PTR(-EFAULT
);
1579 nesvnic
->mcrq_ucontext
= nes_ucontext
;
1580 nes_ucontext
->mcrqf
= req
.mcrqf
;
1581 if (nes_ucontext
->mcrqf
) {
1582 if (nes_ucontext
->mcrqf
& 0x80000000)
1583 nescq
->hw_cq
.cq_number
= nesvnic
->nic
.qp_id
+ 28 + 2 * ((nes_ucontext
->mcrqf
& 0xf) - 1);
1584 else if (nes_ucontext
->mcrqf
& 0x40000000)
1585 nescq
->hw_cq
.cq_number
= nes_ucontext
->mcrqf
& 0xffff;
1587 nescq
->hw_cq
.cq_number
= nesvnic
->mcrq_qp_id
+ nes_ucontext
->mcrqf
-1;
1588 nescq
->mcrqf
= nes_ucontext
->mcrqf
;
1589 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1591 nes_debug(NES_DBG_CQ
, "CQ Virtual Address = %08lX, size = %u.\n",
1592 (unsigned long)req
.user_cq_buffer
, entries
);
1594 list_for_each_entry(nespbl
, &nes_ucontext
->cq_reg_mem_list
, list
) {
1595 if (nespbl
->user_base
== (unsigned long )req
.user_cq_buffer
) {
1596 list_del(&nespbl
->list
);
1598 nes_debug(NES_DBG_CQ
, "Found PBL for virtual CQ. nespbl=%p.\n",
1604 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1606 return ERR_PTR(-EFAULT
);
1609 pbl_entries
= nespbl
->pbl_size
>> 3;
1610 nescq
->cq_mem_size
= 0;
1612 nescq
->cq_mem_size
= nescq
->hw_cq
.cq_size
* sizeof(struct nes_hw_cqe
);
1613 nes_debug(NES_DBG_CQ
, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1614 entries
, nescq
->cq_mem_size
, nescq
->hw_cq
.cq_number
);
1616 /* allocate the physical buffer space */
1617 mem
= pci_alloc_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
,
1618 &nescq
->hw_cq
.cq_pbase
);
1620 printk(KERN_ERR PFX
"Unable to allocate pci memory for cq\n");
1621 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1623 return ERR_PTR(-ENOMEM
);
1626 memset(mem
, 0, nescq
->cq_mem_size
);
1627 nescq
->hw_cq
.cq_vbase
= mem
;
1628 nescq
->hw_cq
.cq_head
= 0;
1629 nes_debug(NES_DBG_CQ
, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1630 nescq
->hw_cq
.cq_number
, nescq
->hw_cq
.cq_vbase
,
1631 (u32
)nescq
->hw_cq
.cq_pbase
);
1634 nescq
->hw_cq
.ce_handler
= nes_iwarp_ce_handler
;
1635 spin_lock_init(&nescq
->lock
);
1637 /* send CreateCQ request to CQP */
1638 cqp_request
= nes_get_cqp_request(nesdev
);
1639 if (cqp_request
== NULL
) {
1640 nes_debug(NES_DBG_CQ
, "Failed to get a cqp_request.\n");
1642 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1643 nescq
->hw_cq
.cq_pbase
);
1645 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1646 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1650 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1652 return ERR_PTR(-ENOMEM
);
1654 cqp_request
->waiting
= 1;
1655 cqp_wqe
= &cqp_request
->cqp_wqe
;
1657 opcode
= NES_CQP_CREATE_CQ
| NES_CQP_CQ_CEQ_VALID
|
1658 NES_CQP_CQ_CHK_OVERFLOW
|
1659 NES_CQP_CQ_CEQE_MASK
| ((u32
)nescq
->hw_cq
.cq_size
<< 16);
1661 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1663 if (pbl_entries
!= 1) {
1664 if (pbl_entries
> 32) {
1666 nes_debug(NES_DBG_CQ
, "pbl_entries=%u, use a 4k PBL\n", pbl_entries
);
1667 if (nesadapter
->free_4kpbl
== 0) {
1668 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1669 nes_free_cqp_request(nesdev
, cqp_request
);
1671 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1672 nescq
->hw_cq
.cq_pbase
);
1674 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1675 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1678 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1680 return ERR_PTR(-ENOMEM
);
1682 opcode
|= (NES_CQP_CQ_VIRT
| NES_CQP_CQ_4KB_CHUNK
);
1683 nescq
->virtual_cq
= 2;
1684 nesadapter
->free_4kpbl
--;
1687 /* use 256 byte pbl */
1688 nes_debug(NES_DBG_CQ
, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries
);
1689 if (nesadapter
->free_256pbl
== 0) {
1690 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1691 nes_free_cqp_request(nesdev
, cqp_request
);
1693 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1694 nescq
->hw_cq
.cq_pbase
);
1696 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1697 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1700 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1702 return ERR_PTR(-ENOMEM
);
1704 opcode
|= NES_CQP_CQ_VIRT
;
1705 nescq
->virtual_cq
= 1;
1706 nesadapter
->free_256pbl
--;
1711 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1713 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1714 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1715 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
,
1716 (nescq
->hw_cq
.cq_number
| ((u32
)nesdev
->ceq_index
<< 16)));
1719 if (pbl_entries
!= 1)
1720 u64temp
= (u64
)nespbl
->pbl_pbase
;
1722 u64temp
= le64_to_cpu(nespbl
->pbl_vbase
[0]);
1723 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX
,
1724 nes_ucontext
->mmap_db_index
[0]);
1726 u64temp
= (u64
)nescq
->hw_cq
.cq_pbase
;
1727 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX
] = 0;
1729 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_CQ_WQE_PBL_LOW_IDX
, u64temp
);
1730 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX
] = 0;
1731 u64temp
= (u64
)(unsigned long)&nescq
->hw_cq
;
1732 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX
] =
1733 cpu_to_le32((u32
)(u64temp
>> 1));
1734 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX
] =
1735 cpu_to_le32(((u32
)((u64temp
) >> 33)) & 0x7FFFFFFF);
1737 atomic_set(&cqp_request
->refcount
, 2);
1738 nes_post_cqp_request(nesdev
, cqp_request
);
1741 nes_debug(NES_DBG_CQ
, "Waiting for create iWARP CQ%u to complete.\n",
1742 nescq
->hw_cq
.cq_number
);
1743 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1744 NES_EVENT_TIMEOUT
* 2);
1745 nes_debug(NES_DBG_CQ
, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1746 nescq
->hw_cq
.cq_number
, ret
);
1747 if ((!ret
) || (cqp_request
->major_code
)) {
1748 nes_put_cqp_request(nesdev
, cqp_request
);
1750 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1751 nescq
->hw_cq
.cq_pbase
);
1753 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1754 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1757 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1759 return ERR_PTR(-EIO
);
1761 nes_put_cqp_request(nesdev
, cqp_request
);
1764 /* free the nespbl */
1765 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
1768 resp
.cq_id
= nescq
->hw_cq
.cq_number
;
1769 resp
.cq_size
= nescq
->hw_cq
.cq_size
;
1770 resp
.mmap_db_index
= 0;
1771 if (ib_copy_to_udata(udata
, &resp
, sizeof resp
- sizeof resp
.reserved
)) {
1772 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1774 return ERR_PTR(-EFAULT
);
1778 return &nescq
->ibcq
;
1785 static int nes_destroy_cq(struct ib_cq
*ib_cq
)
1787 struct nes_cq
*nescq
;
1788 struct nes_device
*nesdev
;
1789 struct nes_vnic
*nesvnic
;
1790 struct nes_adapter
*nesadapter
;
1791 struct nes_hw_cqp_wqe
*cqp_wqe
;
1792 struct nes_cqp_request
*cqp_request
;
1793 unsigned long flags
;
1800 nescq
= to_nescq(ib_cq
);
1801 nesvnic
= to_nesvnic(ib_cq
->device
);
1802 nesdev
= nesvnic
->nesdev
;
1803 nesadapter
= nesdev
->nesadapter
;
1805 nes_debug(NES_DBG_CQ
, "Destroy CQ%u\n", nescq
->hw_cq
.cq_number
);
1807 /* Send DestroyCQ request to CQP */
1808 cqp_request
= nes_get_cqp_request(nesdev
);
1809 if (cqp_request
== NULL
) {
1810 nes_debug(NES_DBG_CQ
, "Failed to get a cqp_request.\n");
1813 cqp_request
->waiting
= 1;
1814 cqp_wqe
= &cqp_request
->cqp_wqe
;
1815 opcode
= NES_CQP_DESTROY_CQ
| (nescq
->hw_cq
.cq_size
<< 16);
1816 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1817 if (nescq
->virtual_cq
== 1) {
1818 nesadapter
->free_256pbl
++;
1819 if (nesadapter
->free_256pbl
> nesadapter
->max_256pbl
) {
1820 printk(KERN_ERR PFX
"%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1821 __func__
, nesadapter
->free_256pbl
, nesadapter
->max_256pbl
);
1823 } else if (nescq
->virtual_cq
== 2) {
1824 nesadapter
->free_4kpbl
++;
1825 if (nesadapter
->free_4kpbl
> nesadapter
->max_4kpbl
) {
1826 printk(KERN_ERR PFX
"%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1827 __func__
, nesadapter
->free_4kpbl
, nesadapter
->max_4kpbl
);
1829 opcode
|= NES_CQP_CQ_4KB_CHUNK
;
1832 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1834 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1835 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1836 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
,
1837 (nescq
->hw_cq
.cq_number
| ((u32
)PCI_FUNC(nesdev
->pcidev
->devfn
) << 16)));
1839 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, nescq
->hw_cq
.cq_number
);
1841 atomic_set(&cqp_request
->refcount
, 2);
1842 nes_post_cqp_request(nesdev
, cqp_request
);
1845 nes_debug(NES_DBG_CQ
, "Waiting for destroy iWARP CQ%u to complete.\n",
1846 nescq
->hw_cq
.cq_number
);
1847 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1849 nes_debug(NES_DBG_CQ
, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1850 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1851 nescq
->hw_cq
.cq_number
, ret
, cqp_request
->major_code
,
1852 cqp_request
->minor_code
);
1854 nes_debug(NES_DBG_CQ
, "iWARP CQ%u destroy timeout expired\n",
1855 nescq
->hw_cq
.cq_number
);
1857 } else if (cqp_request
->major_code
) {
1858 nes_debug(NES_DBG_CQ
, "iWARP CQ%u destroy failed\n",
1859 nescq
->hw_cq
.cq_number
);
1864 nes_put_cqp_request(nesdev
, cqp_request
);
1866 if (nescq
->cq_mem_size
)
1867 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
,
1868 nescq
->hw_cq
.cq_vbase
, nescq
->hw_cq
.cq_pbase
);
1877 static u32
root_256(struct nes_device
*nesdev
,
1878 struct nes_root_vpbl
*root_vpbl
,
1879 struct nes_root_vpbl
*new_root
,
1885 if (pbl_count_4k
== 1) {
1886 new_root
->pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
,
1887 512, &new_root
->pbl_pbase
);
1889 if (new_root
->pbl_vbase
== NULL
)
1892 leaf_pbl
= (u64
)root_vpbl
->pbl_pbase
;
1893 for (i
= 0; i
< 16; i
++) {
1894 new_root
->pbl_vbase
[i
].pa_low
=
1895 cpu_to_le32((u32
)leaf_pbl
);
1896 new_root
->pbl_vbase
[i
].pa_high
=
1897 cpu_to_le32((u32
)((((u64
)leaf_pbl
) >> 32)));
1901 for (i
= 3; i
>= 0; i
--) {
1903 root_vpbl
->pbl_vbase
[j
] = root_vpbl
->pbl_vbase
[i
];
1904 leaf_pbl
= le32_to_cpu(root_vpbl
->pbl_vbase
[j
].pa_low
) +
1905 (((u64
)le32_to_cpu(root_vpbl
->pbl_vbase
[j
].pa_high
))
1907 for (k
= 1; k
< 16; k
++) {
1909 root_vpbl
->pbl_vbase
[j
+ k
].pa_low
=
1910 cpu_to_le32((u32
)leaf_pbl
);
1911 root_vpbl
->pbl_vbase
[j
+ k
].pa_high
=
1912 cpu_to_le32((u32
)((((u64
)leaf_pbl
) >> 32)));
1924 static int nes_reg_mr(struct nes_device
*nesdev
, struct nes_pd
*nespd
,
1925 u32 stag
, u64 region_length
, struct nes_root_vpbl
*root_vpbl
,
1926 dma_addr_t single_buffer
, u16 pbl_count_4k
,
1927 u16 residual_page_count_4k
, int acc
, u64
*iova_start
,
1928 u16
*actual_pbl_cnt
, u8
*used_4k_pbls
)
1930 struct nes_hw_cqp_wqe
*cqp_wqe
;
1931 struct nes_cqp_request
*cqp_request
;
1932 unsigned long flags
;
1934 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1936 u16 pbl_count_256
= 0;
1938 u8 use_256_pbls
= 0;
1940 u16 use_two_level
= (pbl_count_4k
> 1) ? 1 : 0;
1941 struct nes_root_vpbl new_root
= { 0, NULL
, NULL
};
1945 /* Register the region with the adapter */
1946 cqp_request
= nes_get_cqp_request(nesdev
);
1947 if (cqp_request
== NULL
) {
1948 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
1951 cqp_request
->waiting
= 1;
1952 cqp_wqe
= &cqp_request
->cqp_wqe
;
1955 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1957 pg_cnt
= ((pbl_count_4k
- 1) * 512) + residual_page_count_4k
;
1958 pbl_count_256
= (pg_cnt
+ 31) / 32;
1960 if (pbl_count_256
<= nesadapter
->free_256pbl
)
1962 else if (pbl_count_4k
<= nesadapter
->free_4kpbl
)
1964 } else if (pg_cnt
<= 2048) {
1965 if (((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) &&
1966 (nesadapter
->free_4kpbl
> (nesadapter
->max_4kpbl
>> 1))) {
1968 } else if ((pbl_count_256
+ 1) <= nesadapter
->free_256pbl
) {
1971 } else if ((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) {
1975 if ((pbl_count_4k
+ 1) <= nesadapter
->free_4kpbl
)
1980 pbl_count
= pbl_count_256
;
1981 nesadapter
->free_256pbl
-= pbl_count
+ use_two_level
;
1982 } else if (use_4k_pbls
) {
1983 pbl_count
= pbl_count_4k
;
1984 nesadapter
->free_4kpbl
-= pbl_count
+ use_two_level
;
1986 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1987 nes_debug(NES_DBG_MR
, "Out of Pbls\n");
1988 nes_free_cqp_request(nesdev
, cqp_request
);
1992 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1995 if (use_256_pbls
&& use_two_level
) {
1996 if (root_256(nesdev
, root_vpbl
, &new_root
, pbl_count_4k
) == 1) {
1997 if (new_root
.pbl_pbase
!= 0)
1998 root_vpbl
= &new_root
;
2000 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
2001 nesadapter
->free_256pbl
+= pbl_count_256
+ use_two_level
;
2004 if (pbl_count_4k
== 1)
2006 pbl_count
= pbl_count_4k
;
2008 if ((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) {
2009 nesadapter
->free_4kpbl
-= pbl_count
+ use_two_level
;
2012 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
2014 if (use_4k_pbls
== 0)
2019 opcode
= NES_CQP_REGISTER_STAG
| NES_CQP_STAG_RIGHTS_LOCAL_READ
|
2020 NES_CQP_STAG_VA_TO
| NES_CQP_STAG_MR
;
2021 if (acc
& IB_ACCESS_LOCAL_WRITE
)
2022 opcode
|= NES_CQP_STAG_RIGHTS_LOCAL_WRITE
;
2023 if (acc
& IB_ACCESS_REMOTE_WRITE
)
2024 opcode
|= NES_CQP_STAG_RIGHTS_REMOTE_WRITE
| NES_CQP_STAG_REM_ACC_EN
;
2025 if (acc
& IB_ACCESS_REMOTE_READ
)
2026 opcode
|= NES_CQP_STAG_RIGHTS_REMOTE_READ
| NES_CQP_STAG_REM_ACC_EN
;
2027 if (acc
& IB_ACCESS_MW_BIND
)
2028 opcode
|= NES_CQP_STAG_RIGHTS_WINDOW_BIND
| NES_CQP_STAG_REM_ACC_EN
;
2030 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2031 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
2032 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_VA_LOW_IDX
, *iova_start
);
2033 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_LEN_LOW_IDX
, region_length
);
2035 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] =
2036 cpu_to_le32((u32
)(region_length
>> 8) & 0xff000000);
2037 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] |=
2038 cpu_to_le32(nespd
->pd_id
& 0x00007fff);
2039 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, stag
);
2041 if (pbl_count
== 0) {
2042 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, single_buffer
);
2044 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, root_vpbl
->pbl_pbase
);
2045 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX
, pbl_count
);
2046 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_LEN_IDX
, (pg_cnt
* 8));
2049 cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE
);
2053 atomic_set(&cqp_request
->refcount
, 2);
2054 nes_post_cqp_request(nesdev
, cqp_request
);
2057 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
2059 nes_debug(NES_DBG_MR
, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2060 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2061 stag
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2062 major_code
= cqp_request
->major_code
;
2063 nes_put_cqp_request(nesdev
, cqp_request
);
2065 if ((!ret
|| major_code
) && pbl_count
!= 0) {
2066 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
2068 nesadapter
->free_256pbl
+= pbl_count
+ use_two_level
;
2069 else if (use_4k_pbls
)
2070 nesadapter
->free_4kpbl
+= pbl_count
+ use_two_level
;
2071 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
2073 if (new_root
.pbl_pbase
)
2074 pci_free_consistent(nesdev
->pcidev
, 512, new_root
.pbl_vbase
,
2075 new_root
.pbl_pbase
);
2079 else if (major_code
)
2082 *actual_pbl_cnt
= pbl_count
+ use_two_level
;
2083 *used_4k_pbls
= use_4k_pbls
;
2091 static struct ib_mr
*nes_reg_phys_mr(struct ib_pd
*ib_pd
,
2092 struct ib_phys_buf
*buffer_list
, int num_phys_buf
, int acc
,
2096 struct nes_pd
*nespd
= to_nespd(ib_pd
);
2097 struct nes_vnic
*nesvnic
= to_nesvnic(ib_pd
->device
);
2098 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2099 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2100 struct nes_mr
*nesmr
;
2102 struct nes_vpbl vpbl
;
2103 struct nes_root_vpbl root_vpbl
;
2108 u32 next_stag_index
= 0;
2110 u32 root_pbl_index
= 0;
2111 u32 cur_pbl_index
= 0;
2119 vpbl
.pbl_vbase
= NULL
;
2120 root_vpbl
.pbl_vbase
= NULL
;
2121 root_vpbl
.pbl_pbase
= 0;
2123 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
2124 stag_key
= (u8
)next_stag_index
;
2128 next_stag_index
>>= 8;
2129 next_stag_index
%= nesadapter
->max_mr
;
2130 if (num_phys_buf
> (1024*512)) {
2131 return ERR_PTR(-E2BIG
);
2134 if ((buffer_list
[0].addr
^ *iova_start
) & ~PAGE_MASK
)
2135 return ERR_PTR(-EINVAL
);
2137 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
, nesadapter
->max_mr
,
2138 &stag_index
, &next_stag_index
, NES_RESOURCE_PHYS_MR
);
2140 return ERR_PTR(err
);
2143 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2145 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2146 return ERR_PTR(-ENOMEM
);
2149 for (i
= 0; i
< num_phys_buf
; i
++) {
2151 if ((i
& 0x01FF) == 0) {
2152 if (root_pbl_index
== 1) {
2153 /* Allocate the root PBL */
2154 root_vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 8192,
2155 &root_vpbl
.pbl_pbase
);
2156 nes_debug(NES_DBG_MR
, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2157 root_vpbl
.pbl_vbase
, (unsigned int)root_vpbl
.pbl_pbase
);
2158 if (!root_vpbl
.pbl_vbase
) {
2159 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2161 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2163 return ERR_PTR(-ENOMEM
);
2165 root_vpbl
.leaf_vpbl
= kzalloc(sizeof(*root_vpbl
.leaf_vpbl
)*1024, GFP_KERNEL
);
2166 if (!root_vpbl
.leaf_vpbl
) {
2167 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2168 root_vpbl
.pbl_pbase
);
2169 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2171 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2173 return ERR_PTR(-ENOMEM
);
2175 root_vpbl
.pbl_vbase
[0].pa_low
= cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2176 root_vpbl
.pbl_vbase
[0].pa_high
=
2177 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
) >> 32)));
2178 root_vpbl
.leaf_vpbl
[0] = vpbl
;
2180 /* Allocate a 4K buffer for the PBL */
2181 vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 4096,
2183 nes_debug(NES_DBG_MR
, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2184 vpbl
.pbl_vbase
, (unsigned long)vpbl
.pbl_pbase
);
2185 if (!vpbl
.pbl_vbase
) {
2186 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2187 ibmr
= ERR_PTR(-ENOMEM
);
2191 /* Fill in the root table */
2192 if (1 <= root_pbl_index
) {
2193 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_low
=
2194 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2195 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_high
=
2196 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
) >> 32)));
2197 root_vpbl
.leaf_vpbl
[root_pbl_index
] = vpbl
;
2203 mask
= !buffer_list
[i
].size
;
2205 mask
|= buffer_list
[i
].addr
;
2206 if (i
!= num_phys_buf
- 1)
2207 mask
|= buffer_list
[i
].addr
+ buffer_list
[i
].size
;
2209 if (mask
& ~PAGE_MASK
) {
2210 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2211 nes_debug(NES_DBG_MR
, "Invalid buffer addr or size\n");
2212 ibmr
= ERR_PTR(-EINVAL
);
2217 region_length
+= buffer_list
[i
].size
;
2218 if ((i
!= 0) && (single_page
)) {
2219 if ((buffer_list
[i
-1].addr
+PAGE_SIZE
) != buffer_list
[i
].addr
)
2222 vpbl
.pbl_vbase
[cur_pbl_index
].pa_low
= cpu_to_le32((u32
)buffer_list
[i
].addr
& PAGE_MASK
);
2223 vpbl
.pbl_vbase
[cur_pbl_index
++].pa_high
=
2224 cpu_to_le32((u32
)((((u64
)buffer_list
[i
].addr
) >> 32)));
2227 stag
= stag_index
<< 8;
2229 stag
+= (u32
)stag_key
;
2231 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, VA = 0x%016lX,"
2232 " length = 0x%016lX, index = 0x%08X\n",
2233 stag
, (unsigned long)*iova_start
, (unsigned long)region_length
, stag_index
);
2235 /* Make the leaf PBL the root if only one PBL */
2236 if (root_pbl_index
== 1) {
2237 root_vpbl
.pbl_pbase
= vpbl
.pbl_pbase
;
2243 pbl_count
= root_pbl_index
;
2245 ret
= nes_reg_mr(nesdev
, nespd
, stag
, region_length
, &root_vpbl
,
2246 buffer_list
[0].addr
, pbl_count
, (u16
)cur_pbl_index
, acc
, iova_start
,
2247 &nesmr
->pbls_used
, &nesmr
->pbl_4k
);
2250 nesmr
->ibmr
.rkey
= stag
;
2251 nesmr
->ibmr
.lkey
= stag
;
2252 nesmr
->mode
= IWNES_MEMREG_TYPE_MEM
;
2253 ibmr
= &nesmr
->ibmr
;
2256 ibmr
= ERR_PTR(-ENOMEM
);
2260 /* free the resources */
2261 if (root_pbl_index
== 1) {
2262 /* single PBL case */
2263 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
, vpbl
.pbl_pbase
);
2265 for (i
=0; i
<root_pbl_index
; i
++) {
2266 pci_free_consistent(nesdev
->pcidev
, 4096, root_vpbl
.leaf_vpbl
[i
].pbl_vbase
,
2267 root_vpbl
.leaf_vpbl
[i
].pbl_pbase
);
2269 kfree(root_vpbl
.leaf_vpbl
);
2270 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2271 root_vpbl
.pbl_pbase
);
2281 static struct ib_mr
*nes_get_dma_mr(struct ib_pd
*pd
, int acc
)
2283 struct ib_phys_buf bl
;
2286 nes_debug(NES_DBG_MR
, "\n");
2288 bl
.size
= (u64
)0xffffffffffULL
;
2290 return nes_reg_phys_mr(pd
, &bl
, 1, acc
, &kva
);
2297 static struct ib_mr
*nes_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
2298 u64 virt
, int acc
, struct ib_udata
*udata
)
2303 dma_addr_t last_dma_addr
= 0;
2304 dma_addr_t first_dma_addr
= 0;
2305 struct nes_pd
*nespd
= to_nespd(pd
);
2306 struct nes_vnic
*nesvnic
= to_nesvnic(pd
->device
);
2307 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2308 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2309 struct ib_mr
*ibmr
= ERR_PTR(-EINVAL
);
2310 struct ib_umem_chunk
*chunk
;
2311 struct nes_ucontext
*nes_ucontext
;
2312 struct nes_pbl
*nespbl
;
2313 struct nes_mr
*nesmr
;
2314 struct ib_umem
*region
;
2315 struct nes_mem_reg_req req
;
2316 struct nes_vpbl vpbl
;
2317 struct nes_root_vpbl root_vpbl
;
2318 int nmap_index
, page_index
;
2320 int err
, pbl_depth
= 0;
2325 u32 next_stag_index
;
2327 u32 root_pbl_index
= 0;
2328 u32 cur_pbl_index
= 0;
2334 region
= ib_umem_get(pd
->uobject
->context
, start
, length
, acc
, 0);
2335 if (IS_ERR(region
)) {
2336 return (struct ib_mr
*)region
;
2339 nes_debug(NES_DBG_MR
, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2340 " offset = %u, page size = %u.\n",
2341 (unsigned long int)start
, (unsigned long int)virt
, (u32
)length
,
2342 region
->offset
, region
->page_size
);
2344 skip_pages
= ((u32
)region
->offset
) >> 12;
2346 if (ib_copy_from_udata(&req
, udata
, sizeof(req
))) {
2347 ib_umem_release(region
);
2348 return ERR_PTR(-EFAULT
);
2350 nes_debug(NES_DBG_MR
, "Memory Registration type = %08X.\n", req
.reg_type
);
2352 switch (req
.reg_type
) {
2353 case IWNES_MEMREG_TYPE_MEM
:
2356 vpbl
.pbl_vbase
= NULL
;
2357 root_vpbl
.pbl_vbase
= NULL
;
2358 root_vpbl
.pbl_pbase
= 0;
2360 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
2361 stag_key
= (u8
)next_stag_index
;
2363 driver_key
= next_stag_index
& 0x70000000;
2365 next_stag_index
>>= 8;
2366 next_stag_index
%= nesadapter
->max_mr
;
2368 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
,
2369 nesadapter
->max_mr
, &stag_index
, &next_stag_index
, NES_RESOURCE_USER_MR
);
2371 ib_umem_release(region
);
2372 return ERR_PTR(err
);
2375 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2377 ib_umem_release(region
);
2378 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2379 return ERR_PTR(-ENOMEM
);
2381 nesmr
->region
= region
;
2383 list_for_each_entry(chunk
, ®ion
->chunk_list
, list
) {
2384 nes_debug(NES_DBG_MR
, "Chunk: nents = %u, nmap = %u .\n",
2385 chunk
->nents
, chunk
->nmap
);
2386 for (nmap_index
= 0; nmap_index
< chunk
->nmap
; ++nmap_index
) {
2387 if (sg_dma_address(&chunk
->page_list
[nmap_index
]) & ~PAGE_MASK
) {
2388 ib_umem_release(region
);
2389 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2390 nes_debug(NES_DBG_MR
, "Unaligned Memory Buffer: 0x%x\n",
2391 (unsigned int) sg_dma_address(&chunk
->page_list
[nmap_index
]));
2392 ibmr
= ERR_PTR(-EINVAL
);
2394 goto reg_user_mr_err
;
2397 if (!sg_dma_len(&chunk
->page_list
[nmap_index
])) {
2398 ib_umem_release(region
);
2399 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2401 nes_debug(NES_DBG_MR
, "Invalid Buffer Size\n");
2402 ibmr
= ERR_PTR(-EINVAL
);
2404 goto reg_user_mr_err
;
2407 region_length
+= sg_dma_len(&chunk
->page_list
[nmap_index
]);
2408 chunk_pages
= sg_dma_len(&chunk
->page_list
[nmap_index
]) >> 12;
2409 region_length
-= skip_pages
<< 12;
2410 for (page_index
=skip_pages
; page_index
< chunk_pages
; page_index
++) {
2412 if ((page_count
!=0)&&(page_count
<<12)-(region
->offset
&(4096-1))>=region
->length
)
2414 if ((page_count
&0x01FF) == 0) {
2415 if (page_count
>= 1024 * 512) {
2416 ib_umem_release(region
);
2417 nes_free_resource(nesadapter
,
2418 nesadapter
->allocated_mrs
, stag_index
);
2420 ibmr
= ERR_PTR(-E2BIG
);
2421 goto reg_user_mr_err
;
2423 if (root_pbl_index
== 1) {
2424 root_vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
,
2425 8192, &root_vpbl
.pbl_pbase
);
2426 nes_debug(NES_DBG_MR
, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2427 root_vpbl
.pbl_vbase
, (unsigned int)root_vpbl
.pbl_pbase
);
2428 if (!root_vpbl
.pbl_vbase
) {
2429 ib_umem_release(region
);
2430 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2432 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2435 ibmr
= ERR_PTR(-ENOMEM
);
2436 goto reg_user_mr_err
;
2438 root_vpbl
.leaf_vpbl
= kzalloc(sizeof(*root_vpbl
.leaf_vpbl
)*1024,
2440 if (!root_vpbl
.leaf_vpbl
) {
2441 ib_umem_release(region
);
2442 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2443 root_vpbl
.pbl_pbase
);
2444 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2446 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2449 ibmr
= ERR_PTR(-ENOMEM
);
2450 goto reg_user_mr_err
;
2452 root_vpbl
.pbl_vbase
[0].pa_low
=
2453 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2454 root_vpbl
.pbl_vbase
[0].pa_high
=
2455 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
) >> 32)));
2456 root_vpbl
.leaf_vpbl
[0] = vpbl
;
2458 vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 4096,
2460 nes_debug(NES_DBG_MR
, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2461 vpbl
.pbl_vbase
, (unsigned int)vpbl
.pbl_pbase
);
2462 if (!vpbl
.pbl_vbase
) {
2463 ib_umem_release(region
);
2464 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2465 ibmr
= ERR_PTR(-ENOMEM
);
2467 goto reg_user_mr_err
;
2469 if (1 <= root_pbl_index
) {
2470 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_low
=
2471 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2472 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_high
=
2473 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
)>>32)));
2474 root_vpbl
.leaf_vpbl
[root_pbl_index
] = vpbl
;
2480 if (page_count
!= 0) {
2481 if ((last_dma_addr
+4096) !=
2482 (sg_dma_address(&chunk
->page_list
[nmap_index
])+
2485 last_dma_addr
= sg_dma_address(&chunk
->page_list
[nmap_index
])+
2488 first_dma_addr
= sg_dma_address(&chunk
->page_list
[nmap_index
])+
2490 last_dma_addr
= first_dma_addr
;
2494 vpbl
.pbl_vbase
[cur_pbl_index
].pa_low
=
2495 cpu_to_le32((u32
)(sg_dma_address(&chunk
->page_list
[nmap_index
])+
2496 (page_index
*4096)));
2497 vpbl
.pbl_vbase
[cur_pbl_index
].pa_high
=
2498 cpu_to_le32((u32
)((((u64
)(sg_dma_address(&chunk
->page_list
[nmap_index
])+
2499 (page_index
*4096))) >> 32)));
2506 nes_debug(NES_DBG_MR
, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2507 " stag_key=0x%08x\n",
2508 stag_index
, driver_key
, stag_key
);
2509 stag
= stag_index
<< 8;
2511 stag
+= (u32
)stag_key
;
2514 /* Make the leaf PBL the root if only one PBL */
2515 if (root_pbl_index
== 1) {
2516 root_vpbl
.pbl_pbase
= vpbl
.pbl_pbase
;
2522 pbl_count
= root_pbl_index
;
2525 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2526 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2527 stag
, (unsigned int)iova_start
,
2528 (unsigned int)region_length
, stag_index
,
2529 (unsigned long long)region
->length
, pbl_count
);
2530 ret
= nes_reg_mr(nesdev
, nespd
, stag
, region
->length
, &root_vpbl
,
2531 first_dma_addr
, pbl_count
, (u16
)cur_pbl_index
, acc
,
2532 &iova_start
, &nesmr
->pbls_used
, &nesmr
->pbl_4k
);
2534 nes_debug(NES_DBG_MR
, "ret=%d\n", ret
);
2537 nesmr
->ibmr
.rkey
= stag
;
2538 nesmr
->ibmr
.lkey
= stag
;
2539 nesmr
->mode
= IWNES_MEMREG_TYPE_MEM
;
2540 ibmr
= &nesmr
->ibmr
;
2542 ib_umem_release(region
);
2544 ibmr
= ERR_PTR(-ENOMEM
);
2548 /* free the resources */
2549 if (root_pbl_index
== 1) {
2550 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2553 for (page_index
=0; page_index
<root_pbl_index
; page_index
++) {
2554 pci_free_consistent(nesdev
->pcidev
, 4096,
2555 root_vpbl
.leaf_vpbl
[page_index
].pbl_vbase
,
2556 root_vpbl
.leaf_vpbl
[page_index
].pbl_pbase
);
2558 kfree(root_vpbl
.leaf_vpbl
);
2559 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2560 root_vpbl
.pbl_pbase
);
2563 nes_debug(NES_DBG_MR
, "Leaving, ibmr=%p", ibmr
);
2566 case IWNES_MEMREG_TYPE_QP
:
2567 case IWNES_MEMREG_TYPE_CQ
:
2568 if (!region
->length
) {
2569 nes_debug(NES_DBG_MR
, "Unable to register zero length region for CQ\n");
2570 ib_umem_release(region
);
2571 return ERR_PTR(-EINVAL
);
2573 nespbl
= kzalloc(sizeof(*nespbl
), GFP_KERNEL
);
2575 nes_debug(NES_DBG_MR
, "Unable to allocate PBL\n");
2576 ib_umem_release(region
);
2577 return ERR_PTR(-ENOMEM
);
2579 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2581 ib_umem_release(region
);
2583 nes_debug(NES_DBG_MR
, "Unable to allocate nesmr\n");
2584 return ERR_PTR(-ENOMEM
);
2586 nesmr
->region
= region
;
2587 nes_ucontext
= to_nesucontext(pd
->uobject
->context
);
2588 pbl_depth
= region
->length
>> 12;
2589 pbl_depth
+= (region
->length
& (4096-1)) ? 1 : 0;
2590 nespbl
->pbl_size
= pbl_depth
*sizeof(u64
);
2591 if (req
.reg_type
== IWNES_MEMREG_TYPE_QP
) {
2592 nes_debug(NES_DBG_MR
, "Attempting to allocate QP PBL memory");
2594 nes_debug(NES_DBG_MR
, "Attempting to allocate CP PBL memory");
2597 nes_debug(NES_DBG_MR
, " %u bytes, %u entries.\n",
2598 nespbl
->pbl_size
, pbl_depth
);
2599 pbl
= pci_alloc_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
2600 &nespbl
->pbl_pbase
);
2602 ib_umem_release(region
);
2605 nes_debug(NES_DBG_MR
, "Unable to allocate PBL memory\n");
2606 return ERR_PTR(-ENOMEM
);
2609 nespbl
->pbl_vbase
= (u64
*)pbl
;
2610 nespbl
->user_base
= start
;
2611 nes_debug(NES_DBG_MR
, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2612 " pbl_vbase=%p user_base=0x%lx\n",
2613 nespbl
->pbl_size
, (unsigned long) nespbl
->pbl_pbase
,
2614 (void *) nespbl
->pbl_vbase
, nespbl
->user_base
);
2616 list_for_each_entry(chunk
, ®ion
->chunk_list
, list
) {
2617 for (nmap_index
= 0; nmap_index
< chunk
->nmap
; ++nmap_index
) {
2618 chunk_pages
= sg_dma_len(&chunk
->page_list
[nmap_index
]) >> 12;
2619 chunk_pages
+= (sg_dma_len(&chunk
->page_list
[nmap_index
]) & (4096-1)) ? 1 : 0;
2620 nespbl
->page
= sg_page(&chunk
->page_list
[0]);
2621 for (page_index
=0; page_index
<chunk_pages
; page_index
++) {
2622 ((__le32
*)pbl
)[0] = cpu_to_le32((u32
)
2623 (sg_dma_address(&chunk
->page_list
[nmap_index
])+
2624 (page_index
*4096)));
2625 ((__le32
*)pbl
)[1] = cpu_to_le32(((u64
)
2626 (sg_dma_address(&chunk
->page_list
[nmap_index
])+
2627 (page_index
*4096)))>>32);
2628 nes_debug(NES_DBG_MR
, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl
,
2629 (unsigned long long)*pbl
,
2630 le32_to_cpu(((__le32
*)pbl
)[1]), le32_to_cpu(((__le32
*)pbl
)[0]));
2635 if (req
.reg_type
== IWNES_MEMREG_TYPE_QP
) {
2636 list_add_tail(&nespbl
->list
, &nes_ucontext
->qp_reg_mem_list
);
2638 list_add_tail(&nespbl
->list
, &nes_ucontext
->cq_reg_mem_list
);
2640 nesmr
->ibmr
.rkey
= -1;
2641 nesmr
->ibmr
.lkey
= -1;
2642 nesmr
->mode
= req
.reg_type
;
2643 return &nesmr
->ibmr
;
2646 ib_umem_release(region
);
2647 return ERR_PTR(-ENOSYS
);
2654 static int nes_dereg_mr(struct ib_mr
*ib_mr
)
2656 struct nes_mr
*nesmr
= to_nesmr(ib_mr
);
2657 struct nes_vnic
*nesvnic
= to_nesvnic(ib_mr
->device
);
2658 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2659 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2660 struct nes_hw_cqp_wqe
*cqp_wqe
;
2661 struct nes_cqp_request
*cqp_request
;
2662 unsigned long flags
;
2667 if (nesmr
->region
) {
2668 ib_umem_release(nesmr
->region
);
2670 if (nesmr
->mode
!= IWNES_MEMREG_TYPE_MEM
) {
2675 /* Deallocate the region with the adapter */
2677 cqp_request
= nes_get_cqp_request(nesdev
);
2678 if (cqp_request
== NULL
) {
2679 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
2682 cqp_request
->waiting
= 1;
2683 cqp_wqe
= &cqp_request
->cqp_wqe
;
2685 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2686 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
,
2687 NES_CQP_DEALLOCATE_STAG
| NES_CQP_STAG_VA_TO
|
2688 NES_CQP_STAG_DEALLOC_PBLS
| NES_CQP_STAG_MR
);
2689 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, ib_mr
->rkey
);
2691 atomic_set(&cqp_request
->refcount
, 2);
2692 nes_post_cqp_request(nesdev
, cqp_request
);
2695 nes_debug(NES_DBG_MR
, "Waiting for deallocate STag 0x%08X completed\n", ib_mr
->rkey
);
2696 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
2698 nes_debug(NES_DBG_MR
, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2699 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2700 ib_mr
->rkey
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2702 major_code
= cqp_request
->major_code
;
2703 minor_code
= cqp_request
->minor_code
;
2705 nes_put_cqp_request(nesdev
, cqp_request
);
2708 nes_debug(NES_DBG_MR
, "Timeout waiting to destroy STag,"
2709 " ib_mr=%p, rkey = 0x%08X\n",
2710 ib_mr
, ib_mr
->rkey
);
2712 } else if (major_code
) {
2713 nes_debug(NES_DBG_MR
, "Error (0x%04X:0x%04X) while attempting"
2714 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2715 major_code
, minor_code
, ib_mr
, ib_mr
->rkey
);
2719 if (nesmr
->pbls_used
!= 0) {
2720 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
2721 if (nesmr
->pbl_4k
) {
2722 nesadapter
->free_4kpbl
+= nesmr
->pbls_used
;
2723 if (nesadapter
->free_4kpbl
> nesadapter
->max_4kpbl
)
2724 printk(KERN_ERR PFX
"free 4KB PBLs(%u) has "
2725 "exceeded the max(%u)\n",
2726 nesadapter
->free_4kpbl
,
2727 nesadapter
->max_4kpbl
);
2729 nesadapter
->free_256pbl
+= nesmr
->pbls_used
;
2730 if (nesadapter
->free_256pbl
> nesadapter
->max_256pbl
)
2731 printk(KERN_ERR PFX
"free 256B PBLs(%u) has "
2732 "exceeded the max(%u)\n",
2733 nesadapter
->free_256pbl
,
2734 nesadapter
->max_256pbl
);
2736 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
2738 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2739 (ib_mr
->rkey
& 0x0fffff00) >> 8);
2750 static ssize_t
show_rev(struct device
*dev
, struct device_attribute
*attr
,
2753 struct nes_ib_device
*nesibdev
=
2754 container_of(dev
, struct nes_ib_device
, ibdev
.dev
);
2755 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
2757 nes_debug(NES_DBG_INIT
, "\n");
2758 return sprintf(buf
, "%x\n", nesvnic
->nesdev
->nesadapter
->hw_rev
);
2765 static ssize_t
show_fw_ver(struct device
*dev
, struct device_attribute
*attr
,
2768 struct nes_ib_device
*nesibdev
=
2769 container_of(dev
, struct nes_ib_device
, ibdev
.dev
);
2770 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
2772 nes_debug(NES_DBG_INIT
, "\n");
2773 return sprintf(buf
, "%u.%u\n",
2774 (nesvnic
->nesdev
->nesadapter
->firmware_version
>> 16),
2775 (nesvnic
->nesdev
->nesadapter
->firmware_version
& 0x000000ff));
2782 static ssize_t
show_hca(struct device
*dev
, struct device_attribute
*attr
,
2785 nes_debug(NES_DBG_INIT
, "\n");
2786 return sprintf(buf
, "NES020\n");
2793 static ssize_t
show_board(struct device
*dev
, struct device_attribute
*attr
,
2796 nes_debug(NES_DBG_INIT
, "\n");
2797 return sprintf(buf
, "%.*s\n", 32, "NES020 Board ID");
2801 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
2802 static DEVICE_ATTR(fw_ver
, S_IRUGO
, show_fw_ver
, NULL
);
2803 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca
, NULL
);
2804 static DEVICE_ATTR(board_id
, S_IRUGO
, show_board
, NULL
);
2806 static struct device_attribute
*nes_dev_attributes
[] = {
2817 static int nes_query_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2818 int attr_mask
, struct ib_qp_init_attr
*init_attr
)
2820 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
2822 nes_debug(NES_DBG_QP
, "\n");
2824 attr
->qp_access_flags
= 0;
2825 attr
->cap
.max_send_wr
= nesqp
->hwqp
.sq_size
;
2826 attr
->cap
.max_recv_wr
= nesqp
->hwqp
.rq_size
;
2827 attr
->cap
.max_recv_sge
= 1;
2828 if (nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
)
2829 attr
->cap
.max_inline_data
= 0;
2831 attr
->cap
.max_inline_data
= 64;
2833 init_attr
->event_handler
= nesqp
->ibqp
.event_handler
;
2834 init_attr
->qp_context
= nesqp
->ibqp
.qp_context
;
2835 init_attr
->send_cq
= nesqp
->ibqp
.send_cq
;
2836 init_attr
->recv_cq
= nesqp
->ibqp
.recv_cq
;
2837 init_attr
->srq
= nesqp
->ibqp
.srq
;
2838 init_attr
->cap
= attr
->cap
;
2847 int nes_hw_modify_qp(struct nes_device
*nesdev
, struct nes_qp
*nesqp
,
2848 u32 next_iwarp_state
, u32 termlen
, u32 wait_completion
)
2850 struct nes_hw_cqp_wqe
*cqp_wqe
;
2851 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2852 /* struct iw_cm_event cm_event; */
2853 struct nes_cqp_request
*cqp_request
;
2857 nes_debug(NES_DBG_MOD_QP
, "QP%u, refcount=%d\n",
2858 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
2860 cqp_request
= nes_get_cqp_request(nesdev
);
2861 if (cqp_request
== NULL
) {
2862 nes_debug(NES_DBG_MOD_QP
, "Failed to get a cqp_request.\n");
2865 if (wait_completion
) {
2866 cqp_request
->waiting
= 1;
2868 cqp_request
->waiting
= 0;
2870 cqp_wqe
= &cqp_request
->cqp_wqe
;
2872 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
,
2873 NES_CQP_MODIFY_QP
| NES_CQP_QP_TYPE_IWARP
| next_iwarp_state
);
2874 nes_debug(NES_DBG_MOD_QP
, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2875 next_iwarp_state
, le32_to_cpu(cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
]));
2876 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2877 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
, nesqp
->hwqp
.qp_id
);
2878 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_CONTEXT_LOW_IDX
, (u64
)nesqp
->nesqp_context_pbase
);
2880 /* If sending a terminate message, fill in the length (in words) */
2881 if (((next_iwarp_state
& NES_CQP_QP_IWARP_STATE_MASK
) == NES_CQP_QP_IWARP_STATE_TERMINATE
) &&
2882 !(next_iwarp_state
& NES_CQP_QP_TERM_DONT_SEND_TERM_MSG
)) {
2883 termlen
= ((termlen
+ 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT
;
2884 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_NEW_MSS_IDX
, termlen
);
2887 atomic_set(&cqp_request
->refcount
, 2);
2888 nes_post_cqp_request(nesdev
, cqp_request
);
2891 if (wait_completion
) {
2892 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2893 nesqp->hwqp.qp_id); */
2894 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
2896 nes_debug(NES_DBG_MOD_QP
, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2897 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2898 nesqp
->hwqp
.qp_id
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2899 major_code
= cqp_request
->major_code
;
2901 nes_debug(NES_DBG_MOD_QP
, "Modify iwarp QP%u failed"
2902 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2903 nesqp
->hwqp
.qp_id
, cqp_request
->major_code
,
2904 cqp_request
->minor_code
, next_iwarp_state
);
2907 nes_put_cqp_request(nesdev
, cqp_request
);
2911 else if (major_code
)
2924 int nes_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2925 int attr_mask
, struct ib_udata
*udata
)
2927 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
2928 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
2929 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2932 u32 next_iwarp_state
= 0;
2934 unsigned long qplockflags
;
2936 u16 original_last_aeq
;
2937 u8 issue_modify_qp
= 0;
2940 nes_debug(NES_DBG_MOD_QP
, "QP%u: QP State=%u, cur QP State=%u,"
2941 " iwarp_state=0x%X, refcount=%d\n",
2942 nesqp
->hwqp
.qp_id
, attr
->qp_state
, nesqp
->ibqp_state
,
2943 nesqp
->iwarp_state
, atomic_read(&nesqp
->refcount
));
2945 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
2947 nes_debug(NES_DBG_MOD_QP
, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2948 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2949 nesqp
->hwqp
.qp_id
, nesqp
->hw_iwarp_state
,
2950 nesqp
->hw_tcp_state
, attr
->qp_access_flags
, attr_mask
);
2952 if (attr_mask
& IB_QP_STATE
) {
2953 switch (attr
->qp_state
) {
2955 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = init\n",
2957 if (nesqp
->iwarp_state
> (u32
)NES_CQP_QP_IWARP_STATE_IDLE
) {
2958 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2961 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_IDLE
;
2962 issue_modify_qp
= 1;
2965 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = rtr\n",
2967 if (nesqp
->iwarp_state
>(u32
)NES_CQP_QP_IWARP_STATE_IDLE
) {
2968 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2971 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_IDLE
;
2972 issue_modify_qp
= 1;
2975 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = rts\n",
2977 if (nesqp
->iwarp_state
>(u32
)NES_CQP_QP_IWARP_STATE_RTS
) {
2978 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2981 if (nesqp
->cm_id
== NULL
) {
2982 nes_debug(NES_DBG_MOD_QP
, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2983 nesqp
->hwqp
.qp_id
);
2984 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2987 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_RTS
;
2988 if (nesqp
->iwarp_state
!= NES_CQP_QP_IWARP_STATE_RTS
)
2989 next_iwarp_state
|= NES_CQP_QP_CONTEXT_VALID
|
2990 NES_CQP_QP_ARP_VALID
| NES_CQP_QP_ORD_VALID
;
2991 issue_modify_qp
= 1;
2992 nesqp
->hw_tcp_state
= NES_AEQE_TCP_STATE_ESTABLISHED
;
2993 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_RTS
;
2994 nesqp
->hte_added
= 1;
2997 issue_modify_qp
= 1;
2998 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2999 nesqp
->hwqp
.qp_id
, nesqp
->hwqp
.sq_head
, nesqp
->hwqp
.sq_tail
);
3000 if (nesqp
->iwarp_state
== (u32
)NES_CQP_QP_IWARP_STATE_CLOSING
) {
3001 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3004 if (nesqp
->iwarp_state
> (u32
)NES_CQP_QP_IWARP_STATE_CLOSING
) {
3005 nes_debug(NES_DBG_MOD_QP
, "QP%u: State change to closing"
3006 " ignored due to current iWARP state\n",
3008 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3011 if (nesqp
->hw_iwarp_state
!= NES_AEQE_IWARP_STATE_RTS
) {
3012 nes_debug(NES_DBG_MOD_QP
, "QP%u: State change to closing"
3013 " already done based on hw state.\n",
3015 issue_modify_qp
= 0;
3017 switch (nesqp
->hw_iwarp_state
) {
3018 case NES_AEQE_IWARP_STATE_CLOSING
:
3019 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_CLOSING
;
3021 case NES_AEQE_IWARP_STATE_TERMINATE
:
3022 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_TERMINATE
;
3024 case NES_AEQE_IWARP_STATE_ERROR
:
3025 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_ERROR
;
3028 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_CLOSING
;
3029 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_CLOSING
;
3035 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = terminate\n",
3037 if (nesqp
->iwarp_state
>=(u32
)NES_CQP_QP_IWARP_STATE_TERMINATE
) {
3038 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3041 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3042 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_TERMINATE
;
3043 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_TERMINATE
;
3044 issue_modify_qp
= 1;
3048 if (nesqp
->iwarp_state
== (u32
)NES_CQP_QP_IWARP_STATE_ERROR
) {
3049 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3052 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = error\n",
3054 if (nesqp
->term_flags
)
3055 del_timer(&nesqp
->terminate_timer
);
3057 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_ERROR
;
3058 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3059 if (nesqp
->hte_added
) {
3060 nes_debug(NES_DBG_MOD_QP
, "set CQP_QP_DEL_HTE\n");
3061 next_iwarp_state
|= NES_CQP_QP_DEL_HTE
;
3062 nesqp
->hte_added
= 0;
3064 if ((nesqp
->hw_tcp_state
> NES_AEQE_TCP_STATE_CLOSED
) &&
3065 (nesdev
->iw_status
) &&
3066 (nesqp
->hw_tcp_state
!= NES_AEQE_TCP_STATE_TIME_WAIT
)) {
3067 next_iwarp_state
|= NES_CQP_QP_RESET
;
3069 nes_debug(NES_DBG_MOD_QP
, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3070 nesqp
->hwqp
.qp_id
, nesqp
->hw_tcp_state
);
3073 issue_modify_qp
= 1;
3074 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_ERROR
;
3077 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3082 nesqp
->ibqp_state
= attr
->qp_state
;
3083 nesqp
->iwarp_state
= next_iwarp_state
& NES_CQP_QP_IWARP_STATE_MASK
;
3084 nes_debug(NES_DBG_MOD_QP
, "Change nesqp->iwarp_state=%08x\n",
3085 nesqp
->iwarp_state
);
3088 if (attr_mask
& IB_QP_ACCESS_FLAGS
) {
3089 if (attr
->qp_access_flags
& IB_ACCESS_LOCAL_WRITE
) {
3090 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
|
3091 NES_QPCONTEXT_MISC_RDMA_READ_EN
);
3092 issue_modify_qp
= 1;
3094 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_WRITE
) {
3095 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
);
3096 issue_modify_qp
= 1;
3098 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_READ
) {
3099 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN
);
3100 issue_modify_qp
= 1;
3102 if (attr
->qp_access_flags
& IB_ACCESS_MW_BIND
) {
3103 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN
);
3104 issue_modify_qp
= 1;
3107 if (nesqp
->user_mode
) {
3108 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
|
3109 NES_QPCONTEXT_MISC_RDMA_READ_EN
);
3110 issue_modify_qp
= 1;
3114 original_last_aeq
= nesqp
->last_aeq
;
3115 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3117 nes_debug(NES_DBG_MOD_QP
, "issue_modify_qp=%u\n", issue_modify_qp
);
3122 if (issue_modify_qp
) {
3123 nes_debug(NES_DBG_MOD_QP
, "call nes_hw_modify_qp\n");
3124 ret
= nes_hw_modify_qp(nesdev
, nesqp
, next_iwarp_state
, 0, 1);
3126 nes_debug(NES_DBG_MOD_QP
, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3127 " failed for QP%u.\n",
3128 next_iwarp_state
, nesqp
->hwqp
.qp_id
);
3132 if ((issue_modify_qp
) && (nesqp
->ibqp_state
> IB_QPS_RTS
)) {
3133 nes_debug(NES_DBG_MOD_QP
, "QP%u Issued ModifyQP refcount (%d),"
3134 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3135 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3136 original_last_aeq
, nesqp
->last_aeq
);
3138 ((original_last_aeq
!= NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE
) &&
3141 if (nesqp
->cm_id
&& nesqp
->hw_tcp_state
!= 0) {
3142 nes_debug(NES_DBG_MOD_QP
, "QP%u Queuing fake disconnect for QP refcount (%d),"
3143 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3144 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3145 original_last_aeq
, nesqp
->last_aeq
);
3146 /* this one is for the cm_disconnect thread */
3147 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
3148 nesqp
->hw_tcp_state
= NES_AEQE_TCP_STATE_CLOSED
;
3149 nesqp
->last_aeq
= NES_AEQE_AEID_RESET_SENT
;
3150 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3151 nes_cm_disconn(nesqp
);
3153 nes_debug(NES_DBG_MOD_QP
, "QP%u No fake disconnect, QP refcount=%d\n",
3154 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
3157 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
3159 /* These two are for the timer thread */
3160 if (atomic_inc_return(&nesqp
->close_timer_started
) == 1) {
3161 nesqp
->cm_id
->add_ref(nesqp
->cm_id
);
3162 nes_debug(NES_DBG_MOD_QP
, "QP%u Not decrementing QP refcount (%d),"
3163 " need ae to finish up, original_last_aeq = 0x%04X."
3164 " last_aeq = 0x%04X, scheduling timer.\n",
3165 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3166 original_last_aeq
, nesqp
->last_aeq
);
3167 schedule_nes_timer(nesqp
->cm_node
, (struct sk_buff
*) nesqp
, NES_TIMER_TYPE_CLOSE
, 1, 0);
3169 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3171 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3172 nes_debug(NES_DBG_MOD_QP
, "QP%u Not decrementing QP refcount (%d),"
3173 " need ae to finish up, original_last_aeq = 0x%04X."
3174 " last_aeq = 0x%04X.\n",
3175 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3176 original_last_aeq
, nesqp
->last_aeq
);
3180 nes_debug(NES_DBG_MOD_QP
, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3181 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3182 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3183 original_last_aeq
, nesqp
->last_aeq
);
3186 nes_debug(NES_DBG_MOD_QP
, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3187 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3188 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3189 original_last_aeq
, nesqp
->last_aeq
);
3194 nes_debug(NES_DBG_MOD_QP
, "QP%u Leaving, refcount=%d\n",
3195 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
3202 * nes_muticast_attach
3204 static int nes_multicast_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
3206 nes_debug(NES_DBG_INIT
, "\n");
3212 * nes_multicast_detach
3214 static int nes_multicast_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
3216 nes_debug(NES_DBG_INIT
, "\n");
3224 static int nes_process_mad(struct ib_device
*ibdev
, int mad_flags
,
3225 u8 port_num
, struct ib_wc
*in_wc
, struct ib_grh
*in_grh
,
3226 struct ib_mad
*in_mad
, struct ib_mad
*out_mad
)
3228 nes_debug(NES_DBG_INIT
, "\n");
3233 fill_wqe_sg_send(struct nes_hw_qp_wqe
*wqe
, struct ib_send_wr
*ib_wr
, u32 uselkey
)
3236 int total_payload_length
= 0;
3237 for (sge_index
= 0; sge_index
< ib_wr
->num_sge
; sge_index
++) {
3238 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX
+(sge_index
*4),
3239 ib_wr
->sg_list
[sge_index
].addr
);
3240 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_LENGTH0_IDX
+ (sge_index
*4),
3241 ib_wr
->sg_list
[sge_index
].length
);
3243 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
+ (sge_index
*4),
3244 (ib_wr
->sg_list
[sge_index
].lkey
));
3246 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
+ (sge_index
*4), 0);
3248 total_payload_length
+= ib_wr
->sg_list
[sge_index
].length
;
3250 nes_debug(NES_DBG_IW_TX
, "UC UC UC, sending total_payload_length=%u \n",
3251 total_payload_length
);
3252 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3253 total_payload_length
);
3259 static int nes_post_send(struct ib_qp
*ibqp
, struct ib_send_wr
*ib_wr
,
3260 struct ib_send_wr
**bad_wr
)
3263 unsigned long flags
= 0;
3264 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
3265 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3266 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3267 struct nes_hw_qp_wqe
*wqe
;
3269 u32 qsize
= nesqp
->hwqp
.sq_size
;
3275 if (nesqp
->ibqp_state
> IB_QPS_RTS
) {
3280 spin_lock_irqsave(&nesqp
->lock
, flags
);
3282 head
= nesqp
->hwqp
.sq_head
;
3285 /* Check for QP error */
3286 if (nesqp
->term_flags
) {
3291 /* Check for SQ overflow */
3292 if (((head
+ (2 * qsize
) - nesqp
->hwqp
.sq_tail
) % qsize
) == (qsize
- 1)) {
3297 wqe
= &nesqp
->hwqp
.sq_vbase
[head
];
3298 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3299 nesqp->hwqp.qp_id, wqe, head); */
3300 nes_fill_init_qp_wqe(wqe
, nesqp
, head
);
3301 u64temp
= (u64
)(ib_wr
->wr_id
);
3302 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
,
3304 switch (ib_wr
->opcode
) {
3306 case IB_WR_SEND_WITH_INV
:
3307 if (IB_WR_SEND
== ib_wr
->opcode
) {
3308 if (ib_wr
->send_flags
& IB_SEND_SOLICITED
)
3309 wqe_misc
= NES_IWARP_SQ_OP_SENDSE
;
3311 wqe_misc
= NES_IWARP_SQ_OP_SEND
;
3313 if (ib_wr
->send_flags
& IB_SEND_SOLICITED
)
3314 wqe_misc
= NES_IWARP_SQ_OP_SENDSEINV
;
3316 wqe_misc
= NES_IWARP_SQ_OP_SENDINV
;
3318 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX
,
3319 ib_wr
->ex
.invalidate_rkey
);
3322 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3327 if (ib_wr
->send_flags
& IB_SEND_FENCE
)
3328 wqe_misc
|= NES_IWARP_SQ_WQE_LOCAL_FENCE
;
3330 if ((ib_wr
->send_flags
& IB_SEND_INLINE
) &&
3331 ((nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) == 0) &&
3332 (ib_wr
->sg_list
[0].length
<= 64)) {
3333 memcpy(&wqe
->wqe_words
[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX
],
3334 (void *)(unsigned long)ib_wr
->sg_list
[0].addr
, ib_wr
->sg_list
[0].length
);
3335 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3336 ib_wr
->sg_list
[0].length
);
3337 wqe_misc
|= NES_IWARP_SQ_WQE_IMM_DATA
;
3339 fill_wqe_sg_send(wqe
, ib_wr
, 1);
3343 case IB_WR_RDMA_WRITE
:
3344 wqe_misc
= NES_IWARP_SQ_OP_RDMAW
;
3345 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3346 nes_debug(NES_DBG_IW_TX
, "Exceeded max sge, ib_wr=%u, max=%u\n",
3347 ib_wr
->num_sge
, nesdev
->nesadapter
->max_sge
);
3352 if (ib_wr
->send_flags
& IB_SEND_FENCE
)
3353 wqe_misc
|= NES_IWARP_SQ_WQE_LOCAL_FENCE
;
3355 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_STAG_IDX
,
3356 ib_wr
->wr
.rdma
.rkey
);
3357 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX
,
3358 ib_wr
->wr
.rdma
.remote_addr
);
3360 if ((ib_wr
->send_flags
& IB_SEND_INLINE
) &&
3361 ((nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) == 0) &&
3362 (ib_wr
->sg_list
[0].length
<= 64)) {
3363 memcpy(&wqe
->wqe_words
[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX
],
3364 (void *)(unsigned long)ib_wr
->sg_list
[0].addr
, ib_wr
->sg_list
[0].length
);
3365 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3366 ib_wr
->sg_list
[0].length
);
3367 wqe_misc
|= NES_IWARP_SQ_WQE_IMM_DATA
;
3369 fill_wqe_sg_send(wqe
, ib_wr
, 1);
3372 wqe
->wqe_words
[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
] =
3373 wqe
->wqe_words
[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
];
3375 case IB_WR_RDMA_READ
:
3376 case IB_WR_RDMA_READ_WITH_INV
:
3377 /* iWARP only supports 1 sge for RDMA reads */
3378 if (ib_wr
->num_sge
> 1) {
3379 nes_debug(NES_DBG_IW_TX
, "Exceeded max sge, ib_wr=%u, max=1\n",
3384 if (ib_wr
->opcode
== IB_WR_RDMA_READ
) {
3385 wqe_misc
= NES_IWARP_SQ_OP_RDMAR
;
3387 wqe_misc
= NES_IWARP_SQ_OP_RDMAR_LOCINV
;
3388 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX
,
3389 ib_wr
->ex
.invalidate_rkey
);
3392 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX
,
3393 ib_wr
->wr
.rdma
.remote_addr
);
3394 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_STAG_IDX
,
3395 ib_wr
->wr
.rdma
.rkey
);
3396 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
,
3397 ib_wr
->sg_list
->length
);
3398 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX
,
3399 ib_wr
->sg_list
->addr
);
3400 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
,
3401 ib_wr
->sg_list
->lkey
);
3403 case IB_WR_LOCAL_INV
:
3404 wqe_misc
= NES_IWARP_SQ_OP_LOCINV
;
3405 set_wqe_32bit_value(wqe
->wqe_words
,
3406 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX
,
3407 ib_wr
->ex
.invalidate_rkey
);
3409 case IB_WR_FAST_REG_MR
:
3412 int flags
= ib_wr
->wr
.fast_reg
.access_flags
;
3413 struct nes_ib_fast_reg_page_list
*pnesfrpl
=
3414 container_of(ib_wr
->wr
.fast_reg
.page_list
,
3415 struct nes_ib_fast_reg_page_list
,
3417 u64
*src_page_list
= pnesfrpl
->ibfrpl
.page_list
;
3418 u64
*dst_page_list
= pnesfrpl
->nes_wqe_pbl
.kva
;
3420 if (ib_wr
->wr
.fast_reg
.page_list_len
>
3421 (NES_4K_PBL_CHUNK_SIZE
/ sizeof(u64
))) {
3422 nes_debug(NES_DBG_IW_TX
, "SQ_FMR: bad page_list_len\n");
3426 wqe_misc
= NES_IWARP_SQ_OP_FAST_REG
;
3427 set_wqe_64bit_value(wqe
->wqe_words
,
3428 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX
,
3429 ib_wr
->wr
.fast_reg
.iova_start
);
3430 set_wqe_32bit_value(wqe
->wqe_words
,
3431 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX
,
3432 ib_wr
->wr
.fast_reg
.length
);
3433 set_wqe_32bit_value(wqe
->wqe_words
,
3434 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX
, 0);
3435 set_wqe_32bit_value(wqe
->wqe_words
,
3436 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX
,
3437 ib_wr
->wr
.fast_reg
.rkey
);
3438 /* Set page size: */
3439 if (ib_wr
->wr
.fast_reg
.page_shift
== 12) {
3440 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K
;
3441 } else if (ib_wr
->wr
.fast_reg
.page_shift
== 21) {
3442 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M
;
3444 nes_debug(NES_DBG_IW_TX
, "Invalid page shift,"
3445 " ib_wr=%u, max=1\n", ib_wr
->num_sge
);
3449 /* Set access_flags */
3450 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ
;
3451 if (flags
& IB_ACCESS_LOCAL_WRITE
)
3452 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE
;
3454 if (flags
& IB_ACCESS_REMOTE_WRITE
)
3455 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE
;
3457 if (flags
& IB_ACCESS_REMOTE_READ
)
3458 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ
;
3460 if (flags
& IB_ACCESS_MW_BIND
)
3461 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND
;
3463 /* Fill in PBL info: */
3464 if (ib_wr
->wr
.fast_reg
.page_list_len
>
3465 pnesfrpl
->ibfrpl
.max_page_list_len
) {
3466 nes_debug(NES_DBG_IW_TX
, "Invalid page list length,"
3467 " ib_wr=%p, value=%u, max=%u\n",
3468 ib_wr
, ib_wr
->wr
.fast_reg
.page_list_len
,
3469 pnesfrpl
->ibfrpl
.max_page_list_len
);
3474 set_wqe_64bit_value(wqe
->wqe_words
,
3475 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX
,
3476 pnesfrpl
->nes_wqe_pbl
.paddr
);
3478 set_wqe_32bit_value(wqe
->wqe_words
,
3479 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX
,
3480 ib_wr
->wr
.fast_reg
.page_list_len
* 8);
3482 for (i
= 0; i
< ib_wr
->wr
.fast_reg
.page_list_len
; i
++)
3483 dst_page_list
[i
] = cpu_to_le64(src_page_list
[i
]);
3485 nes_debug(NES_DBG_IW_TX
, "SQ_FMR: iova_start: %llx, "
3486 "length: %d, rkey: %0x, pgl_paddr: %llx, "
3487 "page_list_len: %u, wqe_misc: %x\n",
3488 (unsigned long long) ib_wr
->wr
.fast_reg
.iova_start
,
3489 ib_wr
->wr
.fast_reg
.length
,
3490 ib_wr
->wr
.fast_reg
.rkey
,
3491 (unsigned long long) pnesfrpl
->nes_wqe_pbl
.paddr
,
3492 ib_wr
->wr
.fast_reg
.page_list_len
,
3505 if ((ib_wr
->send_flags
& IB_SEND_SIGNALED
) || nesqp
->sig_all
)
3506 wqe_misc
|= NES_IWARP_SQ_WQE_SIGNALED_COMPL
;
3508 wqe
->wqe_words
[NES_IWARP_SQ_WQE_MISC_IDX
] = cpu_to_le32(wqe_misc
);
3510 ib_wr
= ib_wr
->next
;
3518 nesqp
->hwqp
.sq_head
= head
;
3521 counter
= min(wqe_count
, ((u32
)255));
3522 wqe_count
-= counter
;
3523 nes_write32(nesdev
->regs
+ NES_WQE_ALLOC
,
3524 (counter
<< 24) | 0x00800000 | nesqp
->hwqp
.qp_id
);
3527 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
3539 static int nes_post_recv(struct ib_qp
*ibqp
, struct ib_recv_wr
*ib_wr
,
3540 struct ib_recv_wr
**bad_wr
)
3543 unsigned long flags
= 0;
3544 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
3545 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3546 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3547 struct nes_hw_qp_wqe
*wqe
;
3550 u32 qsize
= nesqp
->hwqp
.rq_size
;
3554 u32 total_payload_length
;
3556 if (nesqp
->ibqp_state
> IB_QPS_RTS
) {
3561 spin_lock_irqsave(&nesqp
->lock
, flags
);
3563 head
= nesqp
->hwqp
.rq_head
;
3566 /* Check for QP error */
3567 if (nesqp
->term_flags
) {
3572 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3576 /* Check for RQ overflow */
3577 if (((head
+ (2 * qsize
) - nesqp
->hwqp
.rq_tail
) % qsize
) == (qsize
- 1)) {
3582 nes_debug(NES_DBG_IW_RX
, "ibwr sge count = %u.\n", ib_wr
->num_sge
);
3583 wqe
= &nesqp
->hwqp
.rq_vbase
[head
];
3585 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3586 nesqp->hwqp.qp_id, wqe, head); */
3587 nes_fill_init_qp_wqe(wqe
, nesqp
, head
);
3588 u64temp
= (u64
)(ib_wr
->wr_id
);
3589 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
,
3591 total_payload_length
= 0;
3592 for (sge_index
=0; sge_index
< ib_wr
->num_sge
; sge_index
++) {
3593 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX
+(sge_index
*4),
3594 ib_wr
->sg_list
[sge_index
].addr
);
3595 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_LENGTH0_IDX
+(sge_index
*4),
3596 ib_wr
->sg_list
[sge_index
].length
);
3597 set_wqe_32bit_value(wqe
->wqe_words
,NES_IWARP_RQ_WQE_STAG0_IDX
+(sge_index
*4),
3598 ib_wr
->sg_list
[sge_index
].lkey
);
3600 total_payload_length
+= ib_wr
->sg_list
[sge_index
].length
;
3602 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX
,
3603 total_payload_length
);
3605 ib_wr
= ib_wr
->next
;
3612 nesqp
->hwqp
.rq_head
= head
;
3615 counter
= min(wqe_count
, ((u32
)255));
3616 wqe_count
-= counter
;
3617 nes_write32(nesdev
->regs
+NES_WQE_ALLOC
, (counter
<<24) | nesqp
->hwqp
.qp_id
);
3620 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
3632 static int nes_poll_cq(struct ib_cq
*ibcq
, int num_entries
, struct ib_wc
*entry
)
3636 unsigned long flags
= 0;
3637 struct nes_vnic
*nesvnic
= to_nesvnic(ibcq
->device
);
3638 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3639 struct nes_cq
*nescq
= to_nescq(ibcq
);
3640 struct nes_qp
*nesqp
;
3641 struct nes_hw_cqe cqe
;
3648 u32 move_cq_head
= 1;
3651 nes_debug(NES_DBG_CQ
, "\n");
3653 spin_lock_irqsave(&nescq
->lock
, flags
);
3655 head
= nescq
->hw_cq
.cq_head
;
3656 cq_size
= nescq
->hw_cq
.cq_size
;
3658 while (cqe_count
< num_entries
) {
3659 if ((le32_to_cpu(nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_OPCODE_IDX
]) &
3660 NES_CQE_VALID
) == 0)
3664 * Make sure we read CQ entry contents *after*
3665 * we've checked the valid bit.
3669 cqe
= nescq
->hw_cq
.cq_vbase
[head
];
3670 u32temp
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
3671 wqe_index
= u32temp
& (nesdev
->nesadapter
->max_qp_wr
- 1);
3672 u32temp
&= ~(NES_SW_CONTEXT_ALIGN
-1);
3673 /* parse CQE, get completion context from WQE (either rq or sq) */
3674 u64temp
= (((u64
)(le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
])))<<32) |
3678 nesqp
= (struct nes_qp
*)(unsigned long)u64temp
;
3679 memset(entry
, 0, sizeof *entry
);
3680 if (cqe
.cqe_words
[NES_CQE_ERROR_CODE_IDX
] == 0) {
3681 entry
->status
= IB_WC_SUCCESS
;
3683 err_code
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_ERROR_CODE_IDX
]);
3684 if (NES_IWARP_CQE_MAJOR_DRV
== (err_code
>> 16)) {
3685 entry
->status
= err_code
& 0x0000ffff;
3687 /* The rest of the cqe's will be marked as flushed */
3688 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_ERROR_CODE_IDX
] =
3689 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH
<< 16) |
3690 NES_IWARP_CQE_MINOR_FLUSH
);
3692 entry
->status
= IB_WC_WR_FLUSH_ERR
;
3695 entry
->qp
= &nesqp
->ibqp
;
3696 entry
->src_qp
= nesqp
->hwqp
.qp_id
;
3698 if (le32_to_cpu(cqe
.cqe_words
[NES_CQE_OPCODE_IDX
]) & NES_CQE_SQ
) {
3699 if (nesqp
->skip_lsmm
) {
3700 nesqp
->skip_lsmm
= 0;
3701 nesqp
->hwqp
.sq_tail
++;
3704 /* Working on a SQ Completion*/
3705 wrid
= (((u64
)(cpu_to_le32((u32
)nesqp
->hwqp
.sq_vbase
[wqe_index
].
3706 wqe_words
[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX
]))) << 32) |
3707 ((u64
)(cpu_to_le32((u32
)nesqp
->hwqp
.sq_vbase
[wqe_index
].
3708 wqe_words
[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
])));
3709 entry
->byte_len
= le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3710 wqe_words
[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
]);
3712 switch (le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3713 wqe_words
[NES_IWARP_SQ_WQE_MISC_IDX
]) & 0x3f) {
3714 case NES_IWARP_SQ_OP_RDMAW
:
3715 nes_debug(NES_DBG_CQ
, "Operation = RDMA WRITE.\n");
3716 entry
->opcode
= IB_WC_RDMA_WRITE
;
3718 case NES_IWARP_SQ_OP_RDMAR
:
3719 nes_debug(NES_DBG_CQ
, "Operation = RDMA READ.\n");
3720 entry
->opcode
= IB_WC_RDMA_READ
;
3721 entry
->byte_len
= le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3722 wqe_words
[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
]);
3724 case NES_IWARP_SQ_OP_SENDINV
:
3725 case NES_IWARP_SQ_OP_SENDSEINV
:
3726 case NES_IWARP_SQ_OP_SEND
:
3727 case NES_IWARP_SQ_OP_SENDSE
:
3728 nes_debug(NES_DBG_CQ
, "Operation = Send.\n");
3729 entry
->opcode
= IB_WC_SEND
;
3731 case NES_IWARP_SQ_OP_LOCINV
:
3732 entry
->opcode
= IB_WC_LOCAL_INV
;
3734 case NES_IWARP_SQ_OP_FAST_REG
:
3735 entry
->opcode
= IB_WC_FAST_REG_MR
;
3739 nesqp
->hwqp
.sq_tail
= (wqe_index
+1)&(nesqp
->hwqp
.sq_size
- 1);
3740 if ((entry
->status
!= IB_WC_SUCCESS
) && (nesqp
->hwqp
.sq_tail
!= nesqp
->hwqp
.sq_head
)) {
3742 wq_tail
= nesqp
->hwqp
.sq_tail
;
3745 /* Working on a RQ Completion*/
3746 entry
->byte_len
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_PAYLOAD_LENGTH_IDX
]);
3747 wrid
= ((u64
)(le32_to_cpu(nesqp
->hwqp
.rq_vbase
[wqe_index
].wqe_words
[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX
]))) |
3748 ((u64
)(le32_to_cpu(nesqp
->hwqp
.rq_vbase
[wqe_index
].wqe_words
[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX
]))<<32);
3749 entry
->opcode
= IB_WC_RECV
;
3751 nesqp
->hwqp
.rq_tail
= (wqe_index
+1)&(nesqp
->hwqp
.rq_size
- 1);
3752 if ((entry
->status
!= IB_WC_SUCCESS
) && (nesqp
->hwqp
.rq_tail
!= nesqp
->hwqp
.rq_head
)) {
3754 wq_tail
= nesqp
->hwqp
.rq_tail
;
3758 entry
->wr_id
= wrid
;
3764 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_OPCODE_IDX
] = 0;
3765 if (++head
>= cq_size
)
3767 nescq
->polled_completions
++;
3769 if ((nescq
->polled_completions
> (cq_size
/ 2)) ||
3770 (nescq
->polled_completions
== 255)) {
3771 nes_debug(NES_DBG_CQ
, "CQ%u Issuing CQE Allocate since more than half of cqes"
3772 " are pending %u of %u.\n",
3773 nescq
->hw_cq
.cq_number
, nescq
->polled_completions
, cq_size
);
3774 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
,
3775 nescq
->hw_cq
.cq_number
| (nescq
->polled_completions
<< 16));
3776 nescq
->polled_completions
= 0;
3779 /* Update the wqe index and set status to flush */
3780 wqe_index
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
3781 wqe_index
= (wqe_index
& (~(nesdev
->nesadapter
->max_qp_wr
- 1))) | wq_tail
;
3782 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
] =
3783 cpu_to_le32(wqe_index
);
3784 move_cq_head
= 1; /* ready for next pass */
3788 if (nescq
->polled_completions
) {
3789 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
,
3790 nescq
->hw_cq
.cq_number
| (nescq
->polled_completions
<< 16));
3791 nescq
->polled_completions
= 0;
3794 nescq
->hw_cq
.cq_head
= head
;
3795 nes_debug(NES_DBG_CQ
, "Reporting %u completions for CQ%u.\n",
3796 cqe_count
, nescq
->hw_cq
.cq_number
);
3798 spin_unlock_irqrestore(&nescq
->lock
, flags
);
3807 static int nes_req_notify_cq(struct ib_cq
*ibcq
, enum ib_cq_notify_flags notify_flags
)
3809 struct nes_vnic
*nesvnic
= to_nesvnic(ibcq
->device
);
3810 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3811 struct nes_cq
*nescq
= to_nescq(ibcq
);
3814 nes_debug(NES_DBG_CQ
, "Requesting notification for CQ%u.\n",
3815 nescq
->hw_cq
.cq_number
);
3817 cq_arm
= nescq
->hw_cq
.cq_number
;
3818 if ((notify_flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_NEXT_COMP
)
3819 cq_arm
|= NES_CQE_ALLOC_NOTIFY_NEXT
;
3820 else if ((notify_flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_SOLICITED
)
3821 cq_arm
|= NES_CQE_ALLOC_NOTIFY_SE
;
3825 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
, cq_arm
);
3826 nes_read32(nesdev
->regs
+NES_CQE_ALLOC
);
3833 * nes_init_ofa_device
3835 struct nes_ib_device
*nes_init_ofa_device(struct net_device
*netdev
)
3837 struct nes_ib_device
*nesibdev
;
3838 struct nes_vnic
*nesvnic
= netdev_priv(netdev
);
3839 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3841 nesibdev
= (struct nes_ib_device
*)ib_alloc_device(sizeof(struct nes_ib_device
));
3842 if (nesibdev
== NULL
) {
3845 strlcpy(nesibdev
->ibdev
.name
, "nes%d", IB_DEVICE_NAME_MAX
);
3846 nesibdev
->ibdev
.owner
= THIS_MODULE
;
3848 nesibdev
->ibdev
.node_type
= RDMA_NODE_RNIC
;
3849 memset(&nesibdev
->ibdev
.node_guid
, 0, sizeof(nesibdev
->ibdev
.node_guid
));
3850 memcpy(&nesibdev
->ibdev
.node_guid
, netdev
->dev_addr
, 6);
3852 nesibdev
->ibdev
.uverbs_cmd_mask
=
3853 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
3854 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
3855 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
3856 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
3857 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
3858 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
3859 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
3860 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
3861 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
3862 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
3863 (1ull << IB_USER_VERBS_CMD_CREATE_AH
) |
3864 (1ull << IB_USER_VERBS_CMD_DESTROY_AH
) |
3865 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ
) |
3866 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
3867 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
3868 (1ull << IB_USER_VERBS_CMD_POLL_CQ
) |
3869 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
3870 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
3871 (1ull << IB_USER_VERBS_CMD_BIND_MW
) |
3872 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
) |
3873 (1ull << IB_USER_VERBS_CMD_POST_RECV
) |
3874 (1ull << IB_USER_VERBS_CMD_POST_SEND
);
3876 nesibdev
->ibdev
.phys_port_cnt
= 1;
3877 nesibdev
->ibdev
.num_comp_vectors
= 1;
3878 nesibdev
->ibdev
.dma_device
= &nesdev
->pcidev
->dev
;
3879 nesibdev
->ibdev
.dev
.parent
= &nesdev
->pcidev
->dev
;
3880 nesibdev
->ibdev
.query_device
= nes_query_device
;
3881 nesibdev
->ibdev
.query_port
= nes_query_port
;
3882 nesibdev
->ibdev
.query_pkey
= nes_query_pkey
;
3883 nesibdev
->ibdev
.query_gid
= nes_query_gid
;
3884 nesibdev
->ibdev
.alloc_ucontext
= nes_alloc_ucontext
;
3885 nesibdev
->ibdev
.dealloc_ucontext
= nes_dealloc_ucontext
;
3886 nesibdev
->ibdev
.mmap
= nes_mmap
;
3887 nesibdev
->ibdev
.alloc_pd
= nes_alloc_pd
;
3888 nesibdev
->ibdev
.dealloc_pd
= nes_dealloc_pd
;
3889 nesibdev
->ibdev
.create_ah
= nes_create_ah
;
3890 nesibdev
->ibdev
.destroy_ah
= nes_destroy_ah
;
3891 nesibdev
->ibdev
.create_qp
= nes_create_qp
;
3892 nesibdev
->ibdev
.modify_qp
= nes_modify_qp
;
3893 nesibdev
->ibdev
.query_qp
= nes_query_qp
;
3894 nesibdev
->ibdev
.destroy_qp
= nes_destroy_qp
;
3895 nesibdev
->ibdev
.create_cq
= nes_create_cq
;
3896 nesibdev
->ibdev
.destroy_cq
= nes_destroy_cq
;
3897 nesibdev
->ibdev
.poll_cq
= nes_poll_cq
;
3898 nesibdev
->ibdev
.get_dma_mr
= nes_get_dma_mr
;
3899 nesibdev
->ibdev
.reg_phys_mr
= nes_reg_phys_mr
;
3900 nesibdev
->ibdev
.reg_user_mr
= nes_reg_user_mr
;
3901 nesibdev
->ibdev
.dereg_mr
= nes_dereg_mr
;
3902 nesibdev
->ibdev
.alloc_mw
= nes_alloc_mw
;
3903 nesibdev
->ibdev
.dealloc_mw
= nes_dealloc_mw
;
3904 nesibdev
->ibdev
.bind_mw
= nes_bind_mw
;
3906 nesibdev
->ibdev
.alloc_fast_reg_mr
= nes_alloc_fast_reg_mr
;
3907 nesibdev
->ibdev
.alloc_fast_reg_page_list
= nes_alloc_fast_reg_page_list
;
3908 nesibdev
->ibdev
.free_fast_reg_page_list
= nes_free_fast_reg_page_list
;
3910 nesibdev
->ibdev
.attach_mcast
= nes_multicast_attach
;
3911 nesibdev
->ibdev
.detach_mcast
= nes_multicast_detach
;
3912 nesibdev
->ibdev
.process_mad
= nes_process_mad
;
3914 nesibdev
->ibdev
.req_notify_cq
= nes_req_notify_cq
;
3915 nesibdev
->ibdev
.post_send
= nes_post_send
;
3916 nesibdev
->ibdev
.post_recv
= nes_post_recv
;
3918 nesibdev
->ibdev
.iwcm
= kzalloc(sizeof(*nesibdev
->ibdev
.iwcm
), GFP_KERNEL
);
3919 if (nesibdev
->ibdev
.iwcm
== NULL
) {
3920 ib_dealloc_device(&nesibdev
->ibdev
);
3923 nesibdev
->ibdev
.iwcm
->add_ref
= nes_add_ref
;
3924 nesibdev
->ibdev
.iwcm
->rem_ref
= nes_rem_ref
;
3925 nesibdev
->ibdev
.iwcm
->get_qp
= nes_get_qp
;
3926 nesibdev
->ibdev
.iwcm
->connect
= nes_connect
;
3927 nesibdev
->ibdev
.iwcm
->accept
= nes_accept
;
3928 nesibdev
->ibdev
.iwcm
->reject
= nes_reject
;
3929 nesibdev
->ibdev
.iwcm
->create_listen
= nes_create_listen
;
3930 nesibdev
->ibdev
.iwcm
->destroy_listen
= nes_destroy_listen
;
3937 * nes_handle_delayed_event
3939 static void nes_handle_delayed_event(unsigned long data
)
3941 struct nes_vnic
*nesvnic
= (void *) data
;
3943 if (nesvnic
->delayed_event
!= nesvnic
->last_dispatched_event
) {
3944 struct ib_event event
;
3946 event
.device
= &nesvnic
->nesibdev
->ibdev
;
3949 event
.event
= nesvnic
->delayed_event
;
3950 event
.element
.port_num
= nesvnic
->logical_port
+ 1;
3951 ib_dispatch_event(&event
);
3955 nesvnic
->event_timer
.function
= NULL
;
3959 void nes_port_ibevent(struct nes_vnic
*nesvnic
)
3961 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
3962 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3963 struct ib_event event
;
3964 event
.device
= &nesibdev
->ibdev
;
3965 event
.element
.port_num
= nesvnic
->logical_port
+ 1;
3966 event
.event
= nesdev
->iw_status
? IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
3968 if (!nesvnic
->event_timer
.function
) {
3969 ib_dispatch_event(&event
);
3970 nesvnic
->last_dispatched_event
= event
.event
;
3971 nesvnic
->event_timer
.function
= nes_handle_delayed_event
;
3972 nesvnic
->event_timer
.data
= (unsigned long) nesvnic
;
3973 nesvnic
->event_timer
.expires
= jiffies
+ NES_EVENT_DELAY
;
3974 add_timer(&nesvnic
->event_timer
);
3976 mod_timer(&nesvnic
->event_timer
, jiffies
+ NES_EVENT_DELAY
);
3978 nesvnic
->delayed_event
= event
.event
;
3983 * nes_destroy_ofa_device
3985 void nes_destroy_ofa_device(struct nes_ib_device
*nesibdev
)
3987 if (nesibdev
== NULL
)
3990 nes_unregister_ofa_device(nesibdev
);
3992 kfree(nesibdev
->ibdev
.iwcm
);
3993 ib_dealloc_device(&nesibdev
->ibdev
);
3998 * nes_register_ofa_device
4000 int nes_register_ofa_device(struct nes_ib_device
*nesibdev
)
4002 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
4003 struct nes_device
*nesdev
= nesvnic
->nesdev
;
4004 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
4007 ret
= ib_register_device(&nesvnic
->nesibdev
->ibdev
, NULL
);
4012 /* Get the resources allocated to this device */
4013 nesibdev
->max_cq
= (nesadapter
->max_cq
-NES_FIRST_QPN
) / nesadapter
->port_count
;
4014 nesibdev
->max_mr
= nesadapter
->max_mr
/ nesadapter
->port_count
;
4015 nesibdev
->max_qp
= (nesadapter
->max_qp
-NES_FIRST_QPN
) / nesadapter
->port_count
;
4016 nesibdev
->max_pd
= nesadapter
->max_pd
/ nesadapter
->port_count
;
4018 for (i
= 0; i
< ARRAY_SIZE(nes_dev_attributes
); ++i
) {
4019 ret
= device_create_file(&nesibdev
->ibdev
.dev
, nes_dev_attributes
[i
]);
4023 device_remove_file(&nesibdev
->ibdev
.dev
,
4024 nes_dev_attributes
[i
]);
4026 ib_unregister_device(&nesibdev
->ibdev
);
4031 nesvnic
->of_device_registered
= 1;
4038 * nes_unregister_ofa_device
4040 static void nes_unregister_ofa_device(struct nes_ib_device
*nesibdev
)
4042 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
4045 for (i
= 0; i
< ARRAY_SIZE(nes_dev_attributes
); ++i
) {
4046 device_remove_file(&nesibdev
->ibdev
.dev
, nes_dev_attributes
[i
]);
4049 if (nesvnic
->of_device_registered
) {
4050 ib_unregister_device(&nesibdev
->ibdev
);
4053 nesvnic
->of_device_registered
= 0;