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
);
54 static int nes_dereg_mr(struct ib_mr
*ib_mr
);
59 static struct ib_mw
*nes_alloc_mw(struct ib_pd
*ibpd
, enum ib_mw_type type
,
60 struct ib_udata
*udata
)
62 struct nes_pd
*nespd
= to_nespd(ibpd
);
63 struct nes_vnic
*nesvnic
= to_nesvnic(ibpd
->device
);
64 struct nes_device
*nesdev
= nesvnic
->nesdev
;
65 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
66 struct nes_cqp_request
*cqp_request
;
69 struct nes_hw_cqp_wqe
*cqp_wqe
;
73 u32 next_stag_index
= 0;
77 if (type
!= IB_MW_TYPE_1
)
78 return ERR_PTR(-EINVAL
);
80 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
81 stag_key
= (u8
)next_stag_index
;
85 next_stag_index
>>= 8;
86 next_stag_index
%= nesadapter
->max_mr
;
88 ret
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
,
89 nesadapter
->max_mr
, &stag_index
, &next_stag_index
, NES_RESOURCE_MW
);
94 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
96 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
97 return ERR_PTR(-ENOMEM
);
100 stag
= stag_index
<< 8;
102 stag
+= (u32
)stag_key
;
104 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, index = 0x%08X\n",
107 /* Register the region with the adapter */
108 cqp_request
= nes_get_cqp_request(nesdev
);
109 if (cqp_request
== NULL
) {
111 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
112 return ERR_PTR(-ENOMEM
);
115 cqp_request
->waiting
= 1;
116 cqp_wqe
= &cqp_request
->cqp_wqe
;
118 cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
] =
119 cpu_to_le32( NES_CQP_ALLOCATE_STAG
| NES_CQP_STAG_RIGHTS_REMOTE_READ
|
120 NES_CQP_STAG_RIGHTS_REMOTE_WRITE
| NES_CQP_STAG_VA_TO
|
121 NES_CQP_STAG_REM_ACC_EN
);
123 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
124 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
, (nespd
->pd_id
& 0x00007fff));
125 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, stag
);
127 atomic_set(&cqp_request
->refcount
, 2);
128 nes_post_cqp_request(nesdev
, cqp_request
);
131 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
133 nes_debug(NES_DBG_MR
, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
134 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
135 stag
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
136 if ((!ret
) || (cqp_request
->major_code
)) {
137 nes_put_cqp_request(nesdev
, cqp_request
);
139 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
141 return ERR_PTR(-ETIME
);
143 return ERR_PTR(-ENOMEM
);
146 nes_put_cqp_request(nesdev
, cqp_request
);
148 nesmr
->ibmw
.rkey
= stag
;
149 nesmr
->mode
= IWNES_MEMREG_TYPE_MW
;
152 nesmr
->pbls_used
= 0;
161 static int nes_dealloc_mw(struct ib_mw
*ibmw
)
163 struct nes_mr
*nesmr
= to_nesmw(ibmw
);
164 struct nes_vnic
*nesvnic
= to_nesvnic(ibmw
->device
);
165 struct nes_device
*nesdev
= nesvnic
->nesdev
;
166 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
167 struct nes_hw_cqp_wqe
*cqp_wqe
;
168 struct nes_cqp_request
*cqp_request
;
172 /* Deallocate the window with the adapter */
173 cqp_request
= nes_get_cqp_request(nesdev
);
174 if (cqp_request
== NULL
) {
175 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
178 cqp_request
->waiting
= 1;
179 cqp_wqe
= &cqp_request
->cqp_wqe
;
180 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
181 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, NES_CQP_DEALLOCATE_STAG
);
182 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, ibmw
->rkey
);
184 atomic_set(&cqp_request
->refcount
, 2);
185 nes_post_cqp_request(nesdev
, cqp_request
);
188 nes_debug(NES_DBG_MR
, "Waiting for deallocate STag 0x%08X to complete.\n",
190 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
192 nes_debug(NES_DBG_MR
, "Deallocate STag completed, wait_event_timeout ret = %u,"
193 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
194 ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
197 else if (cqp_request
->major_code
)
200 nes_put_cqp_request(nesdev
, cqp_request
);
202 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
203 (ibmw
->rkey
& 0x0fffff00) >> 8);
213 static int alloc_fast_reg_mr(struct nes_device
*nesdev
, struct nes_pd
*nespd
,
214 u32 stag
, u32 page_count
)
216 struct nes_hw_cqp_wqe
*cqp_wqe
;
217 struct nes_cqp_request
*cqp_request
;
220 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
223 u64 region_length
= page_count
* PAGE_SIZE
;
226 cqp_request
= nes_get_cqp_request(nesdev
);
227 if (cqp_request
== NULL
) {
228 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
231 nes_debug(NES_DBG_MR
, "alloc_fast_reg_mr: page_count = %d, "
232 "region_length = %llu\n",
233 page_count
, region_length
);
234 cqp_request
->waiting
= 1;
235 cqp_wqe
= &cqp_request
->cqp_wqe
;
237 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
238 if (nesadapter
->free_4kpbl
> 0) {
239 nesadapter
->free_4kpbl
--;
240 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
242 /* No 4kpbl's available: */
243 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
244 nes_debug(NES_DBG_MR
, "Out of Pbls\n");
245 nes_free_cqp_request(nesdev
, cqp_request
);
249 opcode
= NES_CQP_ALLOCATE_STAG
| NES_CQP_STAG_MR
|
250 NES_CQP_STAG_PBL_BLK_SIZE
| NES_CQP_STAG_VA_TO
|
251 NES_CQP_STAG_REM_ACC_EN
;
253 * The current OFED API does not support the zero based TO option.
254 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
255 * the API does not support that ability to have the MR set for local
256 * access only when created and not allow the SQ op to override. Given
257 * this the remote enable must be set here.
260 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
261 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
262 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX
, 1);
264 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] =
265 cpu_to_le32((u32
)(region_length
>> 8) & 0xff000000);
266 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] |=
267 cpu_to_le32(nespd
->pd_id
& 0x00007fff);
269 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, stag
);
270 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_VA_LOW_IDX
, 0);
271 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_LEN_LOW_IDX
, 0);
272 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, 0);
273 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_LEN_IDX
, (page_count
* 8));
274 cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE
);
277 atomic_set(&cqp_request
->refcount
, 2);
278 nes_post_cqp_request(nesdev
, cqp_request
);
281 ret
= wait_event_timeout(cqp_request
->waitq
,
282 (0 != cqp_request
->request_done
),
285 nes_debug(NES_DBG_MR
, "Allocate STag 0x%08X completed, "
286 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
287 "0x%04X:0x%04X.\n", stag
, ret
, cqp_request
->major_code
,
288 cqp_request
->minor_code
);
289 major_code
= cqp_request
->major_code
;
290 nes_put_cqp_request(nesdev
, cqp_request
);
292 if (!ret
|| major_code
) {
293 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
294 nesadapter
->free_4kpbl
++;
295 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
308 static struct ib_mr
*nes_alloc_mr(struct ib_pd
*ibpd
,
309 enum ib_mr_type mr_type
,
312 struct nes_pd
*nespd
= to_nespd(ibpd
);
313 struct nes_vnic
*nesvnic
= to_nesvnic(ibpd
->device
);
314 struct nes_device
*nesdev
= nesvnic
->nesdev
;
315 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
322 struct nes_mr
*nesmr
;
327 if (mr_type
!= IB_MR_TYPE_MEM_REG
)
328 return ERR_PTR(-EINVAL
);
330 if (max_num_sg
> (NES_4K_PBL_CHUNK_SIZE
/ sizeof(u64
)))
331 return ERR_PTR(-E2BIG
);
334 * Note: Set to always use a fixed length single page entry PBL. This is to allow
335 * for the fast_reg_mr operation to always know the size of the PBL.
337 if (max_num_sg
> (NES_4K_PBL_CHUNK_SIZE
/ sizeof(u64
)))
338 return ERR_PTR(-E2BIG
);
340 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
341 stag_key
= (u8
)next_stag_index
;
342 next_stag_index
>>= 8;
343 next_stag_index
%= nesadapter
->max_mr
;
345 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
,
346 nesadapter
->max_mr
, &stag_index
,
347 &next_stag_index
, NES_RESOURCE_FAST_MR
);
351 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
353 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
354 return ERR_PTR(-ENOMEM
);
357 stag
= stag_index
<< 8;
359 stag
+= (u32
)stag_key
;
361 nes_debug(NES_DBG_MR
, "Allocating STag 0x%08X index = 0x%08X\n",
364 ret
= alloc_fast_reg_mr(nesdev
, nespd
, stag
, max_num_sg
);
367 nesmr
->ibmr
.rkey
= stag
;
368 nesmr
->ibmr
.lkey
= stag
;
369 nesmr
->mode
= IWNES_MEMREG_TYPE_FMEM
;
373 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
374 return ERR_PTR(-ENOMEM
);
377 nesmr
->pages
= pci_alloc_consistent(nesdev
->pcidev
,
378 max_num_sg
* sizeof(u64
),
383 nesmr
->max_pages
= max_num_sg
;
390 return ERR_PTR(-ENOMEM
);
393 static int nes_set_page(struct ib_mr
*ibmr
, u64 addr
)
395 struct nes_mr
*nesmr
= to_nesmr(ibmr
);
397 if (unlikely(nesmr
->npages
== nesmr
->max_pages
))
400 nesmr
->pages
[nesmr
->npages
++] = cpu_to_le64(addr
);
405 static int nes_map_mr_sg(struct ib_mr
*ibmr
, struct scatterlist
*sg
,
406 int sg_nents
, unsigned int *sg_offset
)
408 struct nes_mr
*nesmr
= to_nesmr(ibmr
);
412 return ib_sg_to_pages(ibmr
, sg
, sg_nents
, sg_offset
, nes_set_page
);
418 static int nes_query_device(struct ib_device
*ibdev
, struct ib_device_attr
*props
,
419 struct ib_udata
*uhw
)
421 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
422 struct nes_device
*nesdev
= nesvnic
->nesdev
;
423 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
425 if (uhw
->inlen
|| uhw
->outlen
)
428 memset(props
, 0, sizeof(*props
));
429 memcpy(&props
->sys_image_guid
, nesvnic
->netdev
->dev_addr
, 6);
431 props
->fw_ver
= nesdev
->nesadapter
->firmware_version
;
432 props
->device_cap_flags
= nesdev
->nesadapter
->device_cap_flags
;
433 props
->vendor_id
= nesdev
->nesadapter
->vendor_id
;
434 props
->vendor_part_id
= nesdev
->nesadapter
->vendor_part_id
;
435 props
->hw_ver
= nesdev
->nesadapter
->hw_rev
;
436 props
->max_mr_size
= 0x80000000;
437 props
->max_qp
= nesibdev
->max_qp
;
438 props
->max_qp_wr
= nesdev
->nesadapter
->max_qp_wr
- 2;
439 props
->max_send_sge
= nesdev
->nesadapter
->max_sge
;
440 props
->max_recv_sge
= nesdev
->nesadapter
->max_sge
;
441 props
->max_cq
= nesibdev
->max_cq
;
442 props
->max_cqe
= nesdev
->nesadapter
->max_cqe
;
443 props
->max_mr
= nesibdev
->max_mr
;
444 props
->max_mw
= nesibdev
->max_mr
;
445 props
->max_pd
= nesibdev
->max_pd
;
446 props
->max_sge_rd
= 1;
447 switch (nesdev
->nesadapter
->max_irrq_wr
) {
449 props
->max_qp_rd_atom
= 2;
452 props
->max_qp_rd_atom
= 8;
455 props
->max_qp_rd_atom
= 32;
458 props
->max_qp_rd_atom
= 64;
461 props
->max_qp_rd_atom
= 0;
463 props
->max_qp_init_rd_atom
= props
->max_qp_rd_atom
;
464 props
->atomic_cap
= IB_ATOMIC_NONE
;
465 props
->max_map_per_fmr
= 1;
474 static int nes_query_port(struct ib_device
*ibdev
, u8 port
, struct ib_port_attr
*props
)
476 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
477 struct net_device
*netdev
= nesvnic
->netdev
;
479 /* props being zeroed by the caller, avoid zeroing it here */
481 props
->max_mtu
= IB_MTU_4096
;
482 props
->active_mtu
= ib_mtu_int_to_enum(netdev
->mtu
);
485 if (netif_queue_stopped(netdev
))
486 props
->state
= IB_PORT_DOWN
;
487 else if (nesvnic
->linkup
)
488 props
->state
= IB_PORT_ACTIVE
;
490 props
->state
= IB_PORT_DOWN
;
491 props
->port_cap_flags
= IB_PORT_CM_SUP
| IB_PORT_REINIT_SUP
|
492 IB_PORT_VENDOR_CLASS_SUP
| IB_PORT_BOOT_MGMT_SUP
;
493 props
->gid_tbl_len
= 1;
494 props
->pkey_tbl_len
= 1;
495 props
->active_width
= IB_WIDTH_4X
;
496 props
->active_speed
= IB_SPEED_SDR
;
497 props
->max_msg_sz
= 0x80000000;
505 static int nes_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
, u16
*pkey
)
515 static int nes_query_gid(struct ib_device
*ibdev
, u8 port
,
516 int index
, union ib_gid
*gid
)
518 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
520 memset(&(gid
->raw
[0]), 0, sizeof(gid
->raw
));
521 memcpy(&(gid
->raw
[0]), nesvnic
->netdev
->dev_addr
, 6);
528 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
529 * of all objects associated with a particular user-mode client.
531 static struct ib_ucontext
*nes_alloc_ucontext(struct ib_device
*ibdev
,
532 struct ib_udata
*udata
)
534 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
535 struct nes_device
*nesdev
= nesvnic
->nesdev
;
536 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
537 struct nes_alloc_ucontext_req req
;
538 struct nes_alloc_ucontext_resp uresp
;
539 struct nes_ucontext
*nes_ucontext
;
540 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
543 if (ib_copy_from_udata(&req
, udata
, sizeof(struct nes_alloc_ucontext_req
))) {
544 printk(KERN_ERR PFX
"Invalid structure size on allocate user context.\n");
545 return ERR_PTR(-EINVAL
);
548 if (req
.userspace_ver
!= NES_ABI_USERSPACE_VER
) {
549 printk(KERN_ERR PFX
"Invalid userspace driver version detected. Detected version %d, should be %d\n",
550 req
.userspace_ver
, NES_ABI_USERSPACE_VER
);
551 return ERR_PTR(-EINVAL
);
555 memset(&uresp
, 0, sizeof uresp
);
557 uresp
.max_qps
= nesibdev
->max_qp
;
558 uresp
.max_pds
= nesibdev
->max_pd
;
559 uresp
.wq_size
= nesdev
->nesadapter
->max_qp_wr
* 2;
560 uresp
.virtwq
= nesadapter
->virtwq
;
561 uresp
.kernel_ver
= NES_ABI_KERNEL_VER
;
563 nes_ucontext
= kzalloc(sizeof *nes_ucontext
, GFP_KERNEL
);
565 return ERR_PTR(-ENOMEM
);
567 nes_ucontext
->nesdev
= nesdev
;
568 nes_ucontext
->mmap_wq_offset
= uresp
.max_pds
;
569 nes_ucontext
->mmap_cq_offset
= nes_ucontext
->mmap_wq_offset
+
570 ((sizeof(struct nes_hw_qp_wqe
) * uresp
.max_qps
* 2) + PAGE_SIZE
-1) /
574 if (ib_copy_to_udata(udata
, &uresp
, sizeof uresp
)) {
576 return ERR_PTR(-EFAULT
);
579 INIT_LIST_HEAD(&nes_ucontext
->cq_reg_mem_list
);
580 INIT_LIST_HEAD(&nes_ucontext
->qp_reg_mem_list
);
581 atomic_set(&nes_ucontext
->usecnt
, 1);
582 return &nes_ucontext
->ibucontext
;
587 * nes_dealloc_ucontext
589 static int nes_dealloc_ucontext(struct ib_ucontext
*context
)
591 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
592 /* struct nes_device *nesdev = nesvnic->nesdev; */
593 struct nes_ucontext
*nes_ucontext
= to_nesucontext(context
);
595 if (!atomic_dec_and_test(&nes_ucontext
->usecnt
))
605 static int nes_mmap(struct ib_ucontext
*context
, struct vm_area_struct
*vma
)
608 struct nes_vnic
*nesvnic
= to_nesvnic(context
->device
);
609 struct nes_device
*nesdev
= nesvnic
->nesdev
;
610 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
611 struct nes_ucontext
*nes_ucontext
;
612 struct nes_qp
*nesqp
;
614 nes_ucontext
= to_nesucontext(context
);
617 if (vma
->vm_pgoff
>= nes_ucontext
->mmap_wq_offset
) {
618 index
= (vma
->vm_pgoff
- nes_ucontext
->mmap_wq_offset
) * PAGE_SIZE
;
619 index
/= ((sizeof(struct nes_hw_qp_wqe
) * nesdev
->nesadapter
->max_qp_wr
* 2) +
620 PAGE_SIZE
-1) & (~(PAGE_SIZE
-1));
621 if (!test_bit(index
, nes_ucontext
->allocated_wqs
)) {
622 nes_debug(NES_DBG_MMAP
, "wq %lu not allocated\n", index
);
625 nesqp
= nes_ucontext
->mmap_nesqp
[index
];
627 nes_debug(NES_DBG_MMAP
, "wq %lu has a NULL QP base.\n", index
);
630 if (remap_pfn_range(vma
, vma
->vm_start
,
631 virt_to_phys(nesqp
->hwqp
.sq_vbase
) >> PAGE_SHIFT
,
632 vma
->vm_end
- vma
->vm_start
,
633 vma
->vm_page_prot
)) {
634 nes_debug(NES_DBG_MMAP
, "remap_pfn_range failed.\n");
637 vma
->vm_private_data
= nesqp
;
640 index
= vma
->vm_pgoff
;
641 if (!test_bit(index
, nes_ucontext
->allocated_doorbells
))
644 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
645 if (io_remap_pfn_range(vma
, vma
->vm_start
,
646 (nesdev
->doorbell_start
+
647 ((nes_ucontext
->mmap_db_index
[index
] - nesdev
->base_doorbell_index
) * 4096))
648 >> PAGE_SHIFT
, PAGE_SIZE
, vma
->vm_page_prot
))
650 vma
->vm_private_data
= nes_ucontext
;
661 static struct ib_pd
*nes_alloc_pd(struct ib_device
*ibdev
,
662 struct ib_ucontext
*context
, struct ib_udata
*udata
)
664 struct nes_pd
*nespd
;
665 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
666 struct nes_device
*nesdev
= nesvnic
->nesdev
;
667 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
668 struct nes_ucontext
*nesucontext
;
669 struct nes_alloc_pd_resp uresp
;
673 nes_debug(NES_DBG_PD
, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
674 nesvnic
, nesdev
->netdev
[0], nesdev
->netdev
[0]->name
, ibdev
, context
,
675 netdev_refcnt_read(nesvnic
->netdev
));
677 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_pds
,
678 nesadapter
->max_pd
, &pd_num
, &nesadapter
->next_pd
, NES_RESOURCE_PD
);
683 nespd
= kzalloc(sizeof (struct nes_pd
), GFP_KERNEL
);
685 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
, pd_num
);
686 return ERR_PTR(-ENOMEM
);
689 nes_debug(NES_DBG_PD
, "Allocating PD (%p) for ib device %s\n",
690 nespd
, dev_name(&nesvnic
->nesibdev
->ibdev
.dev
));
692 nespd
->pd_id
= (pd_num
<< (PAGE_SHIFT
-12)) + nesadapter
->base_pd
;
695 nesucontext
= to_nesucontext(context
);
696 nespd
->mmap_db_index
= find_next_zero_bit(nesucontext
->allocated_doorbells
,
697 NES_MAX_USER_DB_REGIONS
, nesucontext
->first_free_db
);
698 nes_debug(NES_DBG_PD
, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
699 nespd
->mmap_db_index
, nespd
->pd_id
);
700 if (nespd
->mmap_db_index
>= NES_MAX_USER_DB_REGIONS
) {
701 nes_debug(NES_DBG_PD
, "mmap_db_index > MAX\n");
702 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
, pd_num
);
704 return ERR_PTR(-ENOMEM
);
707 uresp
.pd_id
= nespd
->pd_id
;
708 uresp
.mmap_db_index
= nespd
->mmap_db_index
;
709 if (ib_copy_to_udata(udata
, &uresp
, sizeof (struct nes_alloc_pd_resp
))) {
710 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
, pd_num
);
712 return ERR_PTR(-EFAULT
);
715 set_bit(nespd
->mmap_db_index
, nesucontext
->allocated_doorbells
);
716 nesucontext
->mmap_db_index
[nespd
->mmap_db_index
] = nespd
->pd_id
;
717 nesucontext
->first_free_db
= nespd
->mmap_db_index
+ 1;
720 nes_debug(NES_DBG_PD
, "PD%u structure located @%p.\n", nespd
->pd_id
, nespd
);
728 static int nes_dealloc_pd(struct ib_pd
*ibpd
)
730 struct nes_ucontext
*nesucontext
;
731 struct nes_pd
*nespd
= to_nespd(ibpd
);
732 struct nes_vnic
*nesvnic
= to_nesvnic(ibpd
->device
);
733 struct nes_device
*nesdev
= nesvnic
->nesdev
;
734 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
736 if ((ibpd
->uobject
) && (ibpd
->uobject
->context
)) {
737 nesucontext
= to_nesucontext(ibpd
->uobject
->context
);
738 nes_debug(NES_DBG_PD
, "Clearing bit %u from allocated doorbells\n",
739 nespd
->mmap_db_index
);
740 clear_bit(nespd
->mmap_db_index
, nesucontext
->allocated_doorbells
);
741 nesucontext
->mmap_db_index
[nespd
->mmap_db_index
] = 0;
742 if (nesucontext
->first_free_db
> nespd
->mmap_db_index
) {
743 nesucontext
->first_free_db
= nespd
->mmap_db_index
;
747 nes_debug(NES_DBG_PD
, "Deallocating PD%u structure located @%p.\n",
748 nespd
->pd_id
, nespd
);
749 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
,
750 (nespd
->pd_id
-nesadapter
->base_pd
)>>(PAGE_SHIFT
-12));
758 * nes_get_encoded_size
760 static inline u8
nes_get_encoded_size(int *size
)
766 } else if (*size
<= 128) {
769 } else if (*size
<= 512) {
773 return (encoded_size
);
781 static int nes_setup_virt_qp(struct nes_qp
*nesqp
, struct nes_pbl
*nespbl
,
782 struct nes_vnic
*nesvnic
, int sq_size
, int rq_size
)
789 struct nes_device
*nesdev
= nesvnic
->nesdev
;
790 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
795 pbl_entries
= nespbl
->pbl_size
>> 3;
796 nes_debug(NES_DBG_QP
, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
797 nespbl
->pbl_size
, pbl_entries
,
798 (void *)nespbl
->pbl_vbase
,
799 (unsigned long) nespbl
->pbl_pbase
);
800 pbl
= (__le64
*) nespbl
->pbl_vbase
; /* points to first pbl entry */
801 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
802 /* the first pbl to be fro the rq_vbase... */
803 rq_pbl_entries
= (rq_size
* sizeof(struct nes_hw_qp_wqe
)) >> 12;
804 sq_pbl_entries
= (sq_size
* sizeof(struct nes_hw_qp_wqe
)) >> 12;
805 nesqp
->hwqp
.sq_pbase
= (le32_to_cpu(((__le32
*)pbl
)[0])) | ((u64
)((le32_to_cpu(((__le32
*)pbl
)[1]))) << 32);
807 nes_debug(NES_DBG_QP
, "QP nespbl->page is NULL \n");
812 nesqp
->hwqp
.sq_vbase
= kmap(nespbl
->page
);
813 nesqp
->page
= nespbl
->page
;
814 if (!nesqp
->hwqp
.sq_vbase
) {
815 nes_debug(NES_DBG_QP
, "QP sq_vbase kmap failed\n");
820 /* Now to get to sq.. we need to calculate how many */
821 /* PBL entries were used by the rq.. */
822 pbl
+= sq_pbl_entries
;
823 nesqp
->hwqp
.rq_pbase
= (le32_to_cpu(((__le32
*)pbl
)[0])) | ((u64
)((le32_to_cpu(((__le32
*)pbl
)[1]))) << 32);
824 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
825 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
827 nes_debug(NES_DBG_QP
, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
828 nesqp
->hwqp
.sq_vbase
, (unsigned long) nesqp
->hwqp
.sq_pbase
,
829 nesqp
->hwqp
.rq_vbase
, (unsigned long) nesqp
->hwqp
.rq_pbase
);
830 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
831 if (!nesadapter
->free_256pbl
) {
832 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
834 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
839 nesadapter
->free_256pbl
--;
840 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
842 nesqp
->pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 256, &nesqp
->pbl_pbase
);
843 pblbuffer
= nesqp
->pbl_vbase
;
844 if (!nesqp
->pbl_vbase
) {
845 /* memory allocated during nes_reg_user_mr() */
846 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
849 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
850 nesadapter
->free_256pbl
++;
851 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
855 memset(nesqp
->pbl_vbase
, 0, 256);
856 /* fill in the page address in the pbl buffer.. */
857 tpbl
= pblbuffer
+ 16;
858 pbl
= (__le64
*)nespbl
->pbl_vbase
;
859 while (sq_pbl_entries
--)
862 while (rq_pbl_entries
--)
865 /* done with memory allocated during nes_reg_user_mr() */
866 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
871 max((u32
)sizeof(struct nes_qp_context
), ((u32
)256)) + 256; /* this is Q2 */
872 /* Round up to a multiple of a page */
873 nesqp
->qp_mem_size
+= PAGE_SIZE
- 1;
874 nesqp
->qp_mem_size
&= ~(PAGE_SIZE
- 1);
876 mem
= pci_alloc_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
877 &nesqp
->hwqp
.q2_pbase
);
880 pci_free_consistent(nesdev
->pcidev
, 256, nesqp
->pbl_vbase
, nesqp
->pbl_pbase
);
881 nesqp
->pbl_vbase
= NULL
;
882 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
883 nesadapter
->free_256pbl
++;
884 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
888 nesqp
->sq_kmapped
= 1;
889 nesqp
->hwqp
.q2_vbase
= mem
;
891 memset(nesqp
->hwqp
.q2_vbase
, 0, 256);
892 nesqp
->nesqp_context
= mem
;
893 memset(nesqp
->nesqp_context
, 0, sizeof(*nesqp
->nesqp_context
));
894 nesqp
->nesqp_context_pbase
= nesqp
->hwqp
.q2_pbase
+ 256;
903 static int nes_setup_mmap_qp(struct nes_qp
*nesqp
, struct nes_vnic
*nesvnic
,
904 int sq_size
, int rq_size
)
907 struct nes_device
*nesdev
= nesvnic
->nesdev
;
909 nesqp
->qp_mem_size
= (sizeof(struct nes_hw_qp_wqe
) * sq_size
) +
910 (sizeof(struct nes_hw_qp_wqe
) * rq_size
) +
911 max((u32
)sizeof(struct nes_qp_context
), ((u32
)256)) +
912 256; /* this is Q2 */
913 /* Round up to a multiple of a page */
914 nesqp
->qp_mem_size
+= PAGE_SIZE
- 1;
915 nesqp
->qp_mem_size
&= ~(PAGE_SIZE
- 1);
917 mem
= pci_alloc_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
918 &nesqp
->hwqp
.sq_pbase
);
921 nes_debug(NES_DBG_QP
, "PCI consistent memory for "
922 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
923 mem
, (unsigned long)nesqp
->hwqp
.sq_pbase
, nesqp
->qp_mem_size
);
925 memset(mem
, 0, nesqp
->qp_mem_size
);
927 nesqp
->hwqp
.sq_vbase
= mem
;
928 mem
+= sizeof(struct nes_hw_qp_wqe
) * sq_size
;
930 nesqp
->hwqp
.rq_vbase
= mem
;
931 nesqp
->hwqp
.rq_pbase
= nesqp
->hwqp
.sq_pbase
+
932 sizeof(struct nes_hw_qp_wqe
) * sq_size
;
933 mem
+= sizeof(struct nes_hw_qp_wqe
) * rq_size
;
935 nesqp
->hwqp
.q2_vbase
= mem
;
936 nesqp
->hwqp
.q2_pbase
= nesqp
->hwqp
.rq_pbase
+
937 sizeof(struct nes_hw_qp_wqe
) * rq_size
;
939 memset(nesqp
->hwqp
.q2_vbase
, 0, 256);
941 nesqp
->nesqp_context
= mem
;
942 nesqp
->nesqp_context_pbase
= nesqp
->hwqp
.q2_pbase
+ 256;
943 memset(nesqp
->nesqp_context
, 0, sizeof(*nesqp
->nesqp_context
));
949 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
951 static void nes_free_qp_mem(struct nes_device
*nesdev
,
952 struct nes_qp
*nesqp
, int virt_wqs
)
955 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
957 pci_free_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
958 nesqp
->hwqp
.sq_vbase
, nesqp
->hwqp
.sq_pbase
);
960 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
961 nesadapter
->free_256pbl
++;
962 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
963 pci_free_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
, nesqp
->hwqp
.q2_vbase
, nesqp
->hwqp
.q2_pbase
);
964 pci_free_consistent(nesdev
->pcidev
, 256, nesqp
->pbl_vbase
, nesqp
->pbl_pbase
);
965 nesqp
->pbl_vbase
= NULL
;
966 if (nesqp
->sq_kmapped
) {
967 nesqp
->sq_kmapped
= 0;
977 static struct ib_qp
*nes_create_qp(struct ib_pd
*ibpd
,
978 struct ib_qp_init_attr
*init_attr
, struct ib_udata
*udata
)
982 struct nes_pd
*nespd
= to_nespd(ibpd
);
983 struct nes_vnic
*nesvnic
= to_nesvnic(ibpd
->device
);
984 struct nes_device
*nesdev
= nesvnic
->nesdev
;
985 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
986 struct nes_qp
*nesqp
;
987 struct nes_cq
*nescq
;
988 struct nes_ucontext
*nes_ucontext
;
989 struct nes_hw_cqp_wqe
*cqp_wqe
;
990 struct nes_cqp_request
*cqp_request
;
991 struct nes_create_qp_req req
;
992 struct nes_create_qp_resp uresp
;
993 struct nes_pbl
*nespbl
= NULL
;
996 /* u32 counter = 0; */
1008 if (init_attr
->create_flags
)
1009 return ERR_PTR(-EINVAL
);
1011 atomic_inc(&qps_created
);
1012 switch (init_attr
->qp_type
) {
1014 if (nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) {
1015 init_attr
->cap
.max_inline_data
= 0;
1017 init_attr
->cap
.max_inline_data
= 64;
1019 sq_size
= init_attr
->cap
.max_send_wr
;
1020 rq_size
= init_attr
->cap
.max_recv_wr
;
1022 /* check if the encoded sizes are OK or not... */
1023 sq_encoded_size
= nes_get_encoded_size(&sq_size
);
1024 rq_encoded_size
= nes_get_encoded_size(&rq_size
);
1026 if ((!sq_encoded_size
) || (!rq_encoded_size
)) {
1027 nes_debug(NES_DBG_QP
, "ERROR bad rq (%u) or sq (%u) size\n",
1029 return ERR_PTR(-EINVAL
);
1032 init_attr
->cap
.max_send_wr
= sq_size
-2;
1033 init_attr
->cap
.max_recv_wr
= rq_size
-1;
1034 nes_debug(NES_DBG_QP
, "RQ size=%u, SQ Size=%u\n", rq_size
, sq_size
);
1036 ret
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_qps
,
1037 nesadapter
->max_qp
, &qp_num
, &nesadapter
->next_qp
, NES_RESOURCE_QP
);
1039 return ERR_PTR(ret
);
1042 /* Need 512 (actually now 1024) byte alignment on this structure */
1043 mem
= kzalloc(sizeof(*nesqp
)+NES_SW_CONTEXT_ALIGN
-1, GFP_KERNEL
);
1045 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1046 return ERR_PTR(-ENOMEM
);
1048 u64nesqp
= (unsigned long)mem
;
1049 u64nesqp
+= ((u64
)NES_SW_CONTEXT_ALIGN
) - 1;
1050 u64temp
= ((u64
)NES_SW_CONTEXT_ALIGN
) - 1;
1051 u64nesqp
&= ~u64temp
;
1052 nesqp
= (struct nes_qp
*)(unsigned long)u64nesqp
;
1053 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1054 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1055 nesqp
->allocated_buffer
= mem
;
1058 if (ib_copy_from_udata(&req
, udata
, sizeof(struct nes_create_qp_req
))) {
1059 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1060 kfree(nesqp
->allocated_buffer
);
1061 nes_debug(NES_DBG_QP
, "ib_copy_from_udata() Failed \n");
1062 return ERR_PTR(-EFAULT
);
1064 if (req
.user_wqe_buffers
) {
1067 if (req
.user_qp_buffer
)
1068 nesqp
->nesuqp_addr
= req
.user_qp_buffer
;
1069 if (udata
&& (ibpd
->uobject
->context
)) {
1070 nesqp
->user_mode
= 1;
1071 nes_ucontext
= to_nesucontext(ibpd
->uobject
->context
);
1074 list_for_each_entry(nespbl
, &nes_ucontext
->qp_reg_mem_list
, list
) {
1075 if (nespbl
->user_base
== (unsigned long )req
.user_wqe_buffers
) {
1076 list_del(&nespbl
->list
);
1078 nes_debug(NES_DBG_QP
, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1079 nespbl
, nespbl
->user_base
);
1084 nes_debug(NES_DBG_QP
, "Didn't Find PBL for virtual QP. address = %llx.\n",
1085 (long long unsigned int)req
.user_wqe_buffers
);
1086 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1087 kfree(nesqp
->allocated_buffer
);
1088 return ERR_PTR(-EFAULT
);
1092 nes_ucontext
= to_nesucontext(ibpd
->uobject
->context
);
1093 nesqp
->mmap_sq_db_index
=
1094 find_next_zero_bit(nes_ucontext
->allocated_wqs
,
1095 NES_MAX_USER_WQ_REGIONS
, nes_ucontext
->first_free_wq
);
1096 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1097 nespd->mmap_db_index); */
1098 if (nesqp
->mmap_sq_db_index
>= NES_MAX_USER_WQ_REGIONS
) {
1099 nes_debug(NES_DBG_QP
,
1100 "db index > max user regions, failing create QP\n");
1101 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1103 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
1107 kfree(nesqp
->allocated_buffer
);
1108 return ERR_PTR(-ENOMEM
);
1110 set_bit(nesqp
->mmap_sq_db_index
, nes_ucontext
->allocated_wqs
);
1111 nes_ucontext
->mmap_nesqp
[nesqp
->mmap_sq_db_index
] = nesqp
;
1112 nes_ucontext
->first_free_wq
= nesqp
->mmap_sq_db_index
+ 1;
1114 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1115 kfree(nesqp
->allocated_buffer
);
1116 return ERR_PTR(-EFAULT
);
1119 err
= (!virt_wqs
) ? nes_setup_mmap_qp(nesqp
, nesvnic
, sq_size
, rq_size
) :
1120 nes_setup_virt_qp(nesqp
, nespbl
, nesvnic
, sq_size
, rq_size
);
1122 nes_debug(NES_DBG_QP
,
1123 "error geting qp mem code = %d\n", err
);
1124 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1125 kfree(nesqp
->allocated_buffer
);
1126 return ERR_PTR(-ENOMEM
);
1129 nesqp
->hwqp
.sq_size
= sq_size
;
1130 nesqp
->hwqp
.sq_encoded_size
= sq_encoded_size
;
1131 nesqp
->hwqp
.sq_head
= 1;
1132 nesqp
->hwqp
.rq_size
= rq_size
;
1133 nesqp
->hwqp
.rq_encoded_size
= rq_encoded_size
;
1134 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1135 (void *)nesqp->nesqp_context_pbase);
1137 nesqp
->hwqp
.qp_id
= qp_num
;
1138 nesqp
->ibqp
.qp_num
= nesqp
->hwqp
.qp_id
;
1139 nesqp
->nespd
= nespd
;
1141 nescq
= to_nescq(init_attr
->send_cq
);
1142 nesqp
->nesscq
= nescq
;
1143 nescq
= to_nescq(init_attr
->recv_cq
);
1144 nesqp
->nesrcq
= nescq
;
1146 nesqp
->nesqp_context
->misc
|= cpu_to_le32((u32
)PCI_FUNC(nesdev
->pcidev
->devfn
) <<
1147 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT
);
1148 nesqp
->nesqp_context
->misc
|= cpu_to_le32((u32
)nesqp
->hwqp
.rq_encoded_size
<<
1149 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT
);
1150 nesqp
->nesqp_context
->misc
|= cpu_to_le32((u32
)nesqp
->hwqp
.sq_encoded_size
<<
1151 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT
);
1153 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN
);
1154 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN
);
1156 nesqp
->nesqp_context
->cqs
= cpu_to_le32(nesqp
->nesscq
->hw_cq
.cq_number
+
1157 ((u32
)nesqp
->nesrcq
->hw_cq
.cq_number
<< 16));
1158 u64temp
= (u64
)nesqp
->hwqp
.sq_pbase
;
1159 nesqp
->nesqp_context
->sq_addr_low
= cpu_to_le32((u32
)u64temp
);
1160 nesqp
->nesqp_context
->sq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1164 u64temp
= (u64
)nesqp
->hwqp
.sq_pbase
;
1165 nesqp
->nesqp_context
->sq_addr_low
= cpu_to_le32((u32
)u64temp
);
1166 nesqp
->nesqp_context
->sq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1167 u64temp
= (u64
)nesqp
->hwqp
.rq_pbase
;
1168 nesqp
->nesqp_context
->rq_addr_low
= cpu_to_le32((u32
)u64temp
);
1169 nesqp
->nesqp_context
->rq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1171 u64temp
= (u64
)nesqp
->pbl_pbase
;
1172 nesqp
->nesqp_context
->rq_addr_low
= cpu_to_le32((u32
)u64temp
);
1173 nesqp
->nesqp_context
->rq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1176 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1177 nesvnic->next_qp_nic_index,
1178 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1179 spin_lock_irqsave(&nesdev
->cqp
.lock
, flags
);
1180 nesqp
->nesqp_context
->misc2
|= cpu_to_le32(
1181 (u32
)nesvnic
->qp_nic_index
[nesvnic
->next_qp_nic_index
] <<
1182 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT
);
1183 nesvnic
->next_qp_nic_index
++;
1184 if ((nesvnic
->next_qp_nic_index
> 3) ||
1185 (nesvnic
->qp_nic_index
[nesvnic
->next_qp_nic_index
] == 0xf)) {
1186 nesvnic
->next_qp_nic_index
= 0;
1188 spin_unlock_irqrestore(&nesdev
->cqp
.lock
, flags
);
1190 nesqp
->nesqp_context
->pd_index_wscale
|= cpu_to_le32((u32
)nesqp
->nespd
->pd_id
<< 16);
1191 u64temp
= (u64
)nesqp
->hwqp
.q2_pbase
;
1192 nesqp
->nesqp_context
->q2_addr_low
= cpu_to_le32((u32
)u64temp
);
1193 nesqp
->nesqp_context
->q2_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1194 nesqp
->nesqp_context
->aeq_token_low
= cpu_to_le32((u32
)((unsigned long)(nesqp
)));
1195 nesqp
->nesqp_context
->aeq_token_high
= cpu_to_le32((u32
)(upper_32_bits((unsigned long)(nesqp
))));
1196 nesqp
->nesqp_context
->ird_ord_sizes
= cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM
|
1197 NES_QPCONTEXT_ORDIRD_AAH
|
1198 ((((u32
)nesadapter
->max_irrq_wr
) <<
1199 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT
) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK
));
1200 if (disable_mpa_crc
) {
1201 nes_debug(NES_DBG_QP
, "Disabling MPA crc checking due to module option.\n");
1202 nesqp
->nesqp_context
->ird_ord_sizes
|= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC
);
1207 cqp_request
= nes_get_cqp_request(nesdev
);
1208 if (cqp_request
== NULL
) {
1209 nes_debug(NES_DBG_QP
, "Failed to get a cqp_request\n");
1210 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1211 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1212 kfree(nesqp
->allocated_buffer
);
1213 return ERR_PTR(-ENOMEM
);
1215 cqp_request
->waiting
= 1;
1216 cqp_wqe
= &cqp_request
->cqp_wqe
;
1219 opcode
= NES_CQP_CREATE_QP
| NES_CQP_QP_TYPE_IWARP
|
1220 NES_CQP_QP_IWARP_STATE_IDLE
;
1222 opcode
= NES_CQP_CREATE_QP
| NES_CQP_QP_TYPE_IWARP
| NES_CQP_QP_VIRT_WQS
|
1223 NES_CQP_QP_IWARP_STATE_IDLE
;
1225 opcode
|= NES_CQP_QP_CQS_VALID
;
1226 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1227 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1228 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
, nesqp
->hwqp
.qp_id
);
1230 u64temp
= (u64
)nesqp
->nesqp_context_pbase
;
1231 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_CONTEXT_LOW_IDX
, u64temp
);
1233 atomic_set(&cqp_request
->refcount
, 2);
1234 nes_post_cqp_request(nesdev
, cqp_request
);
1237 nes_debug(NES_DBG_QP
, "Waiting for create iWARP QP%u to complete.\n",
1239 ret
= wait_event_timeout(cqp_request
->waitq
,
1240 (cqp_request
->request_done
!= 0), NES_EVENT_TIMEOUT
);
1241 nes_debug(NES_DBG_QP
, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1242 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1243 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1244 nesqp
->hwqp
.qp_id
, ret
, nesdev
->cqp
.sq_head
, nesdev
->cqp
.sq_tail
,
1245 cqp_request
->major_code
, cqp_request
->minor_code
);
1246 if ((!ret
) || (cqp_request
->major_code
)) {
1247 nes_put_cqp_request(nesdev
, cqp_request
);
1248 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1249 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1250 kfree(nesqp
->allocated_buffer
);
1252 return ERR_PTR(-ETIME
);
1254 return ERR_PTR(-EIO
);
1258 nes_put_cqp_request(nesdev
, cqp_request
);
1261 uresp
.mmap_sq_db_index
= nesqp
->mmap_sq_db_index
;
1262 uresp
.mmap_rq_db_index
= 0;
1263 uresp
.actual_sq_size
= sq_size
;
1264 uresp
.actual_rq_size
= rq_size
;
1265 uresp
.qp_id
= nesqp
->hwqp
.qp_id
;
1266 uresp
.nes_drv_opt
= nes_drv_opt
;
1267 if (ib_copy_to_udata(udata
, &uresp
, sizeof uresp
)) {
1268 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1269 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1270 kfree(nesqp
->allocated_buffer
);
1271 return ERR_PTR(-EFAULT
);
1275 nes_debug(NES_DBG_QP
, "QP%u structure located @%p.Size = %u.\n",
1276 nesqp
->hwqp
.qp_id
, nesqp
, (u32
)sizeof(*nesqp
));
1277 spin_lock_init(&nesqp
->lock
);
1278 nes_add_ref(&nesqp
->ibqp
);
1281 nes_debug(NES_DBG_QP
, "Invalid QP type: %d\n", init_attr
->qp_type
);
1282 return ERR_PTR(-EINVAL
);
1284 init_completion(&nesqp
->sq_drained
);
1285 init_completion(&nesqp
->rq_drained
);
1287 nesqp
->sig_all
= (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
);
1288 timer_setup(&nesqp
->terminate_timer
, nes_terminate_timeout
, 0);
1290 /* update the QP table */
1291 nesdev
->nesadapter
->qp_table
[nesqp
->hwqp
.qp_id
-NES_FIRST_QPN
] = nesqp
;
1292 nes_debug(NES_DBG_QP
, "netdev refcnt=%u\n",
1293 netdev_refcnt_read(nesvnic
->netdev
));
1295 return &nesqp
->ibqp
;
1301 static void nes_clean_cq(struct nes_qp
*nesqp
, struct nes_cq
*nescq
)
1307 unsigned long flags
= 0;
1309 spin_lock_irqsave(&nescq
->lock
, flags
);
1311 cq_head
= nescq
->hw_cq
.cq_head
;
1312 while (le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_OPCODE_IDX
]) & NES_CQE_VALID
) {
1314 lo
= le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
1315 hi
= le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
]);
1316 u64temp
= (((u64
)hi
) << 32) | ((u64
)lo
);
1317 u64temp
&= ~(NES_SW_CONTEXT_ALIGN
-1);
1318 if (u64temp
== (u64
)(unsigned long)nesqp
) {
1319 /* Zero the context value so cqe will be ignored */
1320 nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
] = 0;
1321 nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
] = 0;
1324 if (++cq_head
>= nescq
->hw_cq
.cq_size
)
1328 spin_unlock_irqrestore(&nescq
->lock
, flags
);
1335 static int nes_destroy_qp(struct ib_qp
*ibqp
)
1337 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
1338 struct nes_ucontext
*nes_ucontext
;
1339 struct ib_qp_attr attr
;
1340 struct iw_cm_id
*cm_id
;
1341 struct iw_cm_event cm_event
;
1344 atomic_inc(&sw_qps_destroyed
);
1345 nesqp
->destroyed
= 1;
1347 /* Blow away the connection if it exists. */
1348 if (nesqp
->ibqp_state
>= IB_QPS_INIT
&& nesqp
->ibqp_state
<= IB_QPS_RTS
) {
1349 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1350 attr
.qp_state
= IB_QPS_ERR
;
1351 nes_modify_qp(&nesqp
->ibqp
, &attr
, IB_QP_STATE
, NULL
);
1354 if (((nesqp
->ibqp_state
== IB_QPS_INIT
) ||
1355 (nesqp
->ibqp_state
== IB_QPS_RTR
)) && (nesqp
->cm_id
)) {
1356 cm_id
= nesqp
->cm_id
;
1357 cm_event
.event
= IW_CM_EVENT_CONNECT_REPLY
;
1358 cm_event
.status
= -ETIMEDOUT
;
1359 cm_event
.local_addr
= cm_id
->local_addr
;
1360 cm_event
.remote_addr
= cm_id
->remote_addr
;
1361 cm_event
.private_data
= NULL
;
1362 cm_event
.private_data_len
= 0;
1364 nes_debug(NES_DBG_QP
, "Generating a CM Timeout Event for "
1365 "QP%u. cm_id = %p, refcount = %u. \n",
1366 nesqp
->hwqp
.qp_id
, cm_id
, atomic_read(&nesqp
->refcount
));
1368 cm_id
->rem_ref(cm_id
);
1369 ret
= cm_id
->event_handler(cm_id
, &cm_event
);
1371 nes_debug(NES_DBG_QP
, "OFA CM event_handler returned, ret=%d\n", ret
);
1374 if (nesqp
->user_mode
) {
1375 if ((ibqp
->uobject
)&&(ibqp
->uobject
->context
)) {
1376 nes_ucontext
= to_nesucontext(ibqp
->uobject
->context
);
1377 clear_bit(nesqp
->mmap_sq_db_index
, nes_ucontext
->allocated_wqs
);
1378 nes_ucontext
->mmap_nesqp
[nesqp
->mmap_sq_db_index
] = NULL
;
1379 if (nes_ucontext
->first_free_wq
> nesqp
->mmap_sq_db_index
) {
1380 nes_ucontext
->first_free_wq
= nesqp
->mmap_sq_db_index
;
1383 if (nesqp
->pbl_pbase
&& nesqp
->sq_kmapped
) {
1384 nesqp
->sq_kmapped
= 0;
1385 kunmap(nesqp
->page
);
1388 /* Clean any pending completions from the cq(s) */
1390 nes_clean_cq(nesqp
, nesqp
->nesscq
);
1392 if ((nesqp
->nesrcq
) && (nesqp
->nesrcq
!= nesqp
->nesscq
))
1393 nes_clean_cq(nesqp
, nesqp
->nesrcq
);
1395 nes_rem_ref(&nesqp
->ibqp
);
1403 static struct ib_cq
*nes_create_cq(struct ib_device
*ibdev
,
1404 const struct ib_cq_init_attr
*attr
,
1405 struct ib_ucontext
*context
,
1406 struct ib_udata
*udata
)
1408 int entries
= attr
->cqe
;
1410 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
1411 struct nes_device
*nesdev
= nesvnic
->nesdev
;
1412 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1413 struct nes_cq
*nescq
;
1414 struct nes_ucontext
*nes_ucontext
= NULL
;
1415 struct nes_cqp_request
*cqp_request
;
1417 struct nes_hw_cqp_wqe
*cqp_wqe
;
1418 struct nes_pbl
*nespbl
= NULL
;
1419 struct nes_create_cq_req req
;
1420 struct nes_create_cq_resp resp
;
1423 u32 pbl_entries
= 1;
1425 unsigned long flags
;
1429 return ERR_PTR(-EINVAL
);
1431 if (entries
> nesadapter
->max_cqe
)
1432 return ERR_PTR(-EINVAL
);
1434 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_cqs
,
1435 nesadapter
->max_cq
, &cq_num
, &nesadapter
->next_cq
, NES_RESOURCE_CQ
);
1437 return ERR_PTR(err
);
1440 nescq
= kzalloc(sizeof(struct nes_cq
), GFP_KERNEL
);
1442 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1443 return ERR_PTR(-ENOMEM
);
1446 nescq
->hw_cq
.cq_size
= max(entries
+ 1, 5);
1447 nescq
->hw_cq
.cq_number
= cq_num
;
1448 nescq
->ibcq
.cqe
= nescq
->hw_cq
.cq_size
- 1;
1452 nes_ucontext
= to_nesucontext(context
);
1453 if (ib_copy_from_udata(&req
, udata
, sizeof (struct nes_create_cq_req
))) {
1454 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1456 return ERR_PTR(-EFAULT
);
1458 nesvnic
->mcrq_ucontext
= nes_ucontext
;
1459 nes_ucontext
->mcrqf
= req
.mcrqf
;
1460 if (nes_ucontext
->mcrqf
) {
1461 if (nes_ucontext
->mcrqf
& 0x80000000)
1462 nescq
->hw_cq
.cq_number
= nesvnic
->nic
.qp_id
+ 28 + 2 * ((nes_ucontext
->mcrqf
& 0xf) - 1);
1463 else if (nes_ucontext
->mcrqf
& 0x40000000)
1464 nescq
->hw_cq
.cq_number
= nes_ucontext
->mcrqf
& 0xffff;
1466 nescq
->hw_cq
.cq_number
= nesvnic
->mcrq_qp_id
+ nes_ucontext
->mcrqf
-1;
1467 nescq
->mcrqf
= nes_ucontext
->mcrqf
;
1468 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1470 nes_debug(NES_DBG_CQ
, "CQ Virtual Address = %08lX, size = %u.\n",
1471 (unsigned long)req
.user_cq_buffer
, entries
);
1473 list_for_each_entry(nespbl
, &nes_ucontext
->cq_reg_mem_list
, list
) {
1474 if (nespbl
->user_base
== (unsigned long )req
.user_cq_buffer
) {
1475 list_del(&nespbl
->list
);
1477 nes_debug(NES_DBG_CQ
, "Found PBL for virtual CQ. nespbl=%p.\n",
1483 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1485 return ERR_PTR(-EFAULT
);
1488 pbl_entries
= nespbl
->pbl_size
>> 3;
1489 nescq
->cq_mem_size
= 0;
1491 nescq
->cq_mem_size
= nescq
->hw_cq
.cq_size
* sizeof(struct nes_hw_cqe
);
1492 nes_debug(NES_DBG_CQ
, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1493 entries
, nescq
->cq_mem_size
, nescq
->hw_cq
.cq_number
);
1495 /* allocate the physical buffer space */
1496 mem
= pci_zalloc_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
,
1497 &nescq
->hw_cq
.cq_pbase
);
1499 printk(KERN_ERR PFX
"Unable to allocate pci memory for cq\n");
1500 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1502 return ERR_PTR(-ENOMEM
);
1505 nescq
->hw_cq
.cq_vbase
= mem
;
1506 nescq
->hw_cq
.cq_head
= 0;
1507 nes_debug(NES_DBG_CQ
, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1508 nescq
->hw_cq
.cq_number
, nescq
->hw_cq
.cq_vbase
,
1509 (u32
)nescq
->hw_cq
.cq_pbase
);
1512 nescq
->hw_cq
.ce_handler
= nes_iwarp_ce_handler
;
1513 spin_lock_init(&nescq
->lock
);
1515 /* send CreateCQ request to CQP */
1516 cqp_request
= nes_get_cqp_request(nesdev
);
1517 if (cqp_request
== NULL
) {
1518 nes_debug(NES_DBG_CQ
, "Failed to get a cqp_request.\n");
1520 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1521 nescq
->hw_cq
.cq_pbase
);
1523 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1524 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1528 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1530 return ERR_PTR(-ENOMEM
);
1532 cqp_request
->waiting
= 1;
1533 cqp_wqe
= &cqp_request
->cqp_wqe
;
1535 opcode
= NES_CQP_CREATE_CQ
| NES_CQP_CQ_CEQ_VALID
|
1536 NES_CQP_CQ_CHK_OVERFLOW
|
1537 NES_CQP_CQ_CEQE_MASK
| ((u32
)nescq
->hw_cq
.cq_size
<< 16);
1539 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1541 if (pbl_entries
!= 1) {
1542 if (pbl_entries
> 32) {
1544 nes_debug(NES_DBG_CQ
, "pbl_entries=%u, use a 4k PBL\n", pbl_entries
);
1545 if (nesadapter
->free_4kpbl
== 0) {
1546 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1547 nes_free_cqp_request(nesdev
, cqp_request
);
1549 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1550 nescq
->hw_cq
.cq_pbase
);
1552 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1553 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1556 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1558 return ERR_PTR(-ENOMEM
);
1560 opcode
|= (NES_CQP_CQ_VIRT
| NES_CQP_CQ_4KB_CHUNK
);
1561 nescq
->virtual_cq
= 2;
1562 nesadapter
->free_4kpbl
--;
1565 /* use 256 byte pbl */
1566 nes_debug(NES_DBG_CQ
, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries
);
1567 if (nesadapter
->free_256pbl
== 0) {
1568 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1569 nes_free_cqp_request(nesdev
, cqp_request
);
1571 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1572 nescq
->hw_cq
.cq_pbase
);
1574 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1575 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1578 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1580 return ERR_PTR(-ENOMEM
);
1582 opcode
|= NES_CQP_CQ_VIRT
;
1583 nescq
->virtual_cq
= 1;
1584 nesadapter
->free_256pbl
--;
1589 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1591 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1592 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1593 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
,
1594 (nescq
->hw_cq
.cq_number
| ((u32
)nesdev
->ceq_index
<< 16)));
1597 if (pbl_entries
!= 1)
1598 u64temp
= (u64
)nespbl
->pbl_pbase
;
1600 u64temp
= le64_to_cpu(nespbl
->pbl_vbase
[0]);
1601 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX
,
1602 nes_ucontext
->mmap_db_index
[0]);
1604 u64temp
= (u64
)nescq
->hw_cq
.cq_pbase
;
1605 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX
] = 0;
1607 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_CQ_WQE_PBL_LOW_IDX
, u64temp
);
1608 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX
] = 0;
1609 u64temp
= (u64
)(unsigned long)&nescq
->hw_cq
;
1610 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX
] =
1611 cpu_to_le32((u32
)(u64temp
>> 1));
1612 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX
] =
1613 cpu_to_le32(((u32
)((u64temp
) >> 33)) & 0x7FFFFFFF);
1615 atomic_set(&cqp_request
->refcount
, 2);
1616 nes_post_cqp_request(nesdev
, cqp_request
);
1619 nes_debug(NES_DBG_CQ
, "Waiting for create iWARP CQ%u to complete.\n",
1620 nescq
->hw_cq
.cq_number
);
1621 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1622 NES_EVENT_TIMEOUT
* 2);
1623 nes_debug(NES_DBG_CQ
, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1624 nescq
->hw_cq
.cq_number
, ret
);
1625 if ((!ret
) || (cqp_request
->major_code
)) {
1626 nes_put_cqp_request(nesdev
, cqp_request
);
1628 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1629 nescq
->hw_cq
.cq_pbase
);
1631 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1632 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1635 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1637 return ERR_PTR(-EIO
);
1639 nes_put_cqp_request(nesdev
, cqp_request
);
1642 /* free the nespbl */
1643 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
1646 resp
.cq_id
= nescq
->hw_cq
.cq_number
;
1647 resp
.cq_size
= nescq
->hw_cq
.cq_size
;
1648 resp
.mmap_db_index
= 0;
1649 if (ib_copy_to_udata(udata
, &resp
, sizeof resp
- sizeof resp
.reserved
)) {
1650 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1652 return ERR_PTR(-EFAULT
);
1656 return &nescq
->ibcq
;
1663 static int nes_destroy_cq(struct ib_cq
*ib_cq
)
1665 struct nes_cq
*nescq
;
1666 struct nes_device
*nesdev
;
1667 struct nes_vnic
*nesvnic
;
1668 struct nes_adapter
*nesadapter
;
1669 struct nes_hw_cqp_wqe
*cqp_wqe
;
1670 struct nes_cqp_request
*cqp_request
;
1671 unsigned long flags
;
1678 nescq
= to_nescq(ib_cq
);
1679 nesvnic
= to_nesvnic(ib_cq
->device
);
1680 nesdev
= nesvnic
->nesdev
;
1681 nesadapter
= nesdev
->nesadapter
;
1683 nes_debug(NES_DBG_CQ
, "Destroy CQ%u\n", nescq
->hw_cq
.cq_number
);
1685 /* Send DestroyCQ request to CQP */
1686 cqp_request
= nes_get_cqp_request(nesdev
);
1687 if (cqp_request
== NULL
) {
1688 nes_debug(NES_DBG_CQ
, "Failed to get a cqp_request.\n");
1691 cqp_request
->waiting
= 1;
1692 cqp_wqe
= &cqp_request
->cqp_wqe
;
1693 opcode
= NES_CQP_DESTROY_CQ
| (nescq
->hw_cq
.cq_size
<< 16);
1694 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1695 if (nescq
->virtual_cq
== 1) {
1696 nesadapter
->free_256pbl
++;
1697 if (nesadapter
->free_256pbl
> nesadapter
->max_256pbl
) {
1698 printk(KERN_ERR PFX
"%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1699 __func__
, nesadapter
->free_256pbl
, nesadapter
->max_256pbl
);
1701 } else if (nescq
->virtual_cq
== 2) {
1702 nesadapter
->free_4kpbl
++;
1703 if (nesadapter
->free_4kpbl
> nesadapter
->max_4kpbl
) {
1704 printk(KERN_ERR PFX
"%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1705 __func__
, nesadapter
->free_4kpbl
, nesadapter
->max_4kpbl
);
1707 opcode
|= NES_CQP_CQ_4KB_CHUNK
;
1710 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1712 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1713 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1714 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
,
1715 (nescq
->hw_cq
.cq_number
| ((u32
)PCI_FUNC(nesdev
->pcidev
->devfn
) << 16)));
1717 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, nescq
->hw_cq
.cq_number
);
1719 atomic_set(&cqp_request
->refcount
, 2);
1720 nes_post_cqp_request(nesdev
, cqp_request
);
1723 nes_debug(NES_DBG_CQ
, "Waiting for destroy iWARP CQ%u to complete.\n",
1724 nescq
->hw_cq
.cq_number
);
1725 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1727 nes_debug(NES_DBG_CQ
, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1728 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1729 nescq
->hw_cq
.cq_number
, ret
, cqp_request
->major_code
,
1730 cqp_request
->minor_code
);
1732 nes_debug(NES_DBG_CQ
, "iWARP CQ%u destroy timeout expired\n",
1733 nescq
->hw_cq
.cq_number
);
1735 } else if (cqp_request
->major_code
) {
1736 nes_debug(NES_DBG_CQ
, "iWARP CQ%u destroy failed\n",
1737 nescq
->hw_cq
.cq_number
);
1742 nes_put_cqp_request(nesdev
, cqp_request
);
1744 if (nescq
->cq_mem_size
)
1745 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
,
1746 nescq
->hw_cq
.cq_vbase
, nescq
->hw_cq
.cq_pbase
);
1755 static u32
root_256(struct nes_device
*nesdev
,
1756 struct nes_root_vpbl
*root_vpbl
,
1757 struct nes_root_vpbl
*new_root
,
1763 if (pbl_count_4k
== 1) {
1764 new_root
->pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
,
1765 512, &new_root
->pbl_pbase
);
1767 if (new_root
->pbl_vbase
== NULL
)
1770 leaf_pbl
= (u64
)root_vpbl
->pbl_pbase
;
1771 for (i
= 0; i
< 16; i
++) {
1772 new_root
->pbl_vbase
[i
].pa_low
=
1773 cpu_to_le32((u32
)leaf_pbl
);
1774 new_root
->pbl_vbase
[i
].pa_high
=
1775 cpu_to_le32((u32
)((((u64
)leaf_pbl
) >> 32)));
1779 for (i
= 3; i
>= 0; i
--) {
1781 root_vpbl
->pbl_vbase
[j
] = root_vpbl
->pbl_vbase
[i
];
1782 leaf_pbl
= le32_to_cpu(root_vpbl
->pbl_vbase
[j
].pa_low
) +
1783 (((u64
)le32_to_cpu(root_vpbl
->pbl_vbase
[j
].pa_high
))
1785 for (k
= 1; k
< 16; k
++) {
1787 root_vpbl
->pbl_vbase
[j
+ k
].pa_low
=
1788 cpu_to_le32((u32
)leaf_pbl
);
1789 root_vpbl
->pbl_vbase
[j
+ k
].pa_high
=
1790 cpu_to_le32((u32
)((((u64
)leaf_pbl
) >> 32)));
1802 static int nes_reg_mr(struct nes_device
*nesdev
, struct nes_pd
*nespd
,
1803 u32 stag
, u64 region_length
, struct nes_root_vpbl
*root_vpbl
,
1804 dma_addr_t single_buffer
, u16 pbl_count_4k
,
1805 u16 residual_page_count_4k
, int acc
, u64
*iova_start
,
1806 u16
*actual_pbl_cnt
, u8
*used_4k_pbls
)
1808 struct nes_hw_cqp_wqe
*cqp_wqe
;
1809 struct nes_cqp_request
*cqp_request
;
1810 unsigned long flags
;
1812 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1814 u16 pbl_count_256
= 0;
1816 u8 use_256_pbls
= 0;
1818 u16 use_two_level
= (pbl_count_4k
> 1) ? 1 : 0;
1819 struct nes_root_vpbl new_root
= { 0, NULL
, NULL
};
1823 /* Register the region with the adapter */
1824 cqp_request
= nes_get_cqp_request(nesdev
);
1825 if (cqp_request
== NULL
) {
1826 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
1829 cqp_request
->waiting
= 1;
1830 cqp_wqe
= &cqp_request
->cqp_wqe
;
1833 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1835 pg_cnt
= ((pbl_count_4k
- 1) * 512) + residual_page_count_4k
;
1836 pbl_count_256
= (pg_cnt
+ 31) / 32;
1838 if (pbl_count_256
<= nesadapter
->free_256pbl
)
1840 else if (pbl_count_4k
<= nesadapter
->free_4kpbl
)
1842 } else if (pg_cnt
<= 2048) {
1843 if (((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) &&
1844 (nesadapter
->free_4kpbl
> (nesadapter
->max_4kpbl
>> 1))) {
1846 } else if ((pbl_count_256
+ 1) <= nesadapter
->free_256pbl
) {
1849 } else if ((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) {
1853 if ((pbl_count_4k
+ 1) <= nesadapter
->free_4kpbl
)
1858 pbl_count
= pbl_count_256
;
1859 nesadapter
->free_256pbl
-= pbl_count
+ use_two_level
;
1860 } else if (use_4k_pbls
) {
1861 pbl_count
= pbl_count_4k
;
1862 nesadapter
->free_4kpbl
-= pbl_count
+ use_two_level
;
1864 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1865 nes_debug(NES_DBG_MR
, "Out of Pbls\n");
1866 nes_free_cqp_request(nesdev
, cqp_request
);
1870 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1873 if (use_256_pbls
&& use_two_level
) {
1874 if (root_256(nesdev
, root_vpbl
, &new_root
, pbl_count_4k
) == 1) {
1875 if (new_root
.pbl_pbase
!= 0)
1876 root_vpbl
= &new_root
;
1878 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1879 nesadapter
->free_256pbl
+= pbl_count_256
+ use_two_level
;
1882 if (pbl_count_4k
== 1)
1884 pbl_count
= pbl_count_4k
;
1886 if ((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) {
1887 nesadapter
->free_4kpbl
-= pbl_count
+ use_two_level
;
1890 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1892 if (use_4k_pbls
== 0)
1897 opcode
= NES_CQP_REGISTER_STAG
| NES_CQP_STAG_RIGHTS_LOCAL_READ
|
1898 NES_CQP_STAG_VA_TO
| NES_CQP_STAG_MR
;
1899 if (acc
& IB_ACCESS_LOCAL_WRITE
)
1900 opcode
|= NES_CQP_STAG_RIGHTS_LOCAL_WRITE
;
1901 if (acc
& IB_ACCESS_REMOTE_WRITE
)
1902 opcode
|= NES_CQP_STAG_RIGHTS_REMOTE_WRITE
| NES_CQP_STAG_REM_ACC_EN
;
1903 if (acc
& IB_ACCESS_REMOTE_READ
)
1904 opcode
|= NES_CQP_STAG_RIGHTS_REMOTE_READ
| NES_CQP_STAG_REM_ACC_EN
;
1905 if (acc
& IB_ACCESS_MW_BIND
)
1906 opcode
|= NES_CQP_STAG_RIGHTS_WINDOW_BIND
| NES_CQP_STAG_REM_ACC_EN
;
1908 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1909 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1910 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_VA_LOW_IDX
, *iova_start
);
1911 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_LEN_LOW_IDX
, region_length
);
1913 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] =
1914 cpu_to_le32((u32
)(region_length
>> 8) & 0xff000000);
1915 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] |=
1916 cpu_to_le32(nespd
->pd_id
& 0x00007fff);
1917 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, stag
);
1919 if (pbl_count
== 0) {
1920 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, single_buffer
);
1922 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, root_vpbl
->pbl_pbase
);
1923 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX
, pbl_count
);
1924 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_LEN_IDX
, (pg_cnt
* 8));
1927 cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE
);
1931 atomic_set(&cqp_request
->refcount
, 2);
1932 nes_post_cqp_request(nesdev
, cqp_request
);
1935 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1937 nes_debug(NES_DBG_MR
, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1938 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1939 stag
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
1940 major_code
= cqp_request
->major_code
;
1941 nes_put_cqp_request(nesdev
, cqp_request
);
1943 if ((!ret
|| major_code
) && pbl_count
!= 0) {
1944 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1946 nesadapter
->free_256pbl
+= pbl_count
+ use_two_level
;
1947 else if (use_4k_pbls
)
1948 nesadapter
->free_4kpbl
+= pbl_count
+ use_two_level
;
1949 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1951 if (new_root
.pbl_pbase
)
1952 pci_free_consistent(nesdev
->pcidev
, 512, new_root
.pbl_vbase
,
1953 new_root
.pbl_pbase
);
1957 else if (major_code
)
1960 *actual_pbl_cnt
= pbl_count
+ use_two_level
;
1961 *used_4k_pbls
= use_4k_pbls
;
1969 struct ib_mr
*nes_reg_phys_mr(struct ib_pd
*ib_pd
, u64 addr
, u64 size
,
1970 int acc
, u64
*iova_start
)
1973 struct nes_pd
*nespd
= to_nespd(ib_pd
);
1974 struct nes_vnic
*nesvnic
= to_nesvnic(ib_pd
->device
);
1975 struct nes_device
*nesdev
= nesvnic
->nesdev
;
1976 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1977 struct nes_mr
*nesmr
;
1979 struct nes_vpbl vpbl
;
1980 struct nes_root_vpbl root_vpbl
;
1984 u32 next_stag_index
= 0;
1993 vpbl
.pbl_vbase
= NULL
;
1994 root_vpbl
.pbl_vbase
= NULL
;
1995 root_vpbl
.pbl_pbase
= 0;
1997 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
1998 stag_key
= (u8
)next_stag_index
;
2002 next_stag_index
>>= 8;
2003 next_stag_index
%= nesadapter
->max_mr
;
2005 if ((addr
^ *iova_start
) & ~PAGE_MASK
)
2006 return ERR_PTR(-EINVAL
);
2008 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
, nesadapter
->max_mr
,
2009 &stag_index
, &next_stag_index
, NES_RESOURCE_PHYS_MR
);
2011 return ERR_PTR(err
);
2014 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2016 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2017 return ERR_PTR(-ENOMEM
);
2020 /* Allocate a 4K buffer for the PBL */
2021 vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 4096,
2023 nes_debug(NES_DBG_MR
, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2024 vpbl
.pbl_vbase
, (unsigned long)vpbl
.pbl_pbase
);
2025 if (!vpbl
.pbl_vbase
) {
2026 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2027 ibmr
= ERR_PTR(-ENOMEM
);
2035 if (mask
& ~PAGE_MASK
) {
2036 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2037 nes_debug(NES_DBG_MR
, "Invalid buffer addr or size\n");
2038 ibmr
= ERR_PTR(-EINVAL
);
2043 region_length
+= size
;
2044 vpbl
.pbl_vbase
[0].pa_low
= cpu_to_le32((u32
)addr
& PAGE_MASK
);
2045 vpbl
.pbl_vbase
[0].pa_high
= cpu_to_le32((u32
)((((u64
)addr
) >> 32)));
2047 stag
= stag_index
<< 8;
2049 stag
+= (u32
)stag_key
;
2051 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, VA = 0x%016lX,"
2052 " length = 0x%016lX, index = 0x%08X\n",
2053 stag
, (unsigned long)*iova_start
, (unsigned long)region_length
, stag_index
);
2055 /* Make the leaf PBL the root if only one PBL */
2056 root_vpbl
.pbl_pbase
= vpbl
.pbl_pbase
;
2063 ret
= nes_reg_mr(nesdev
, nespd
, stag
, region_length
, &root_vpbl
,
2064 addr
, pbl_count
, 1, acc
, iova_start
,
2065 &nesmr
->pbls_used
, &nesmr
->pbl_4k
);
2068 nesmr
->ibmr
.rkey
= stag
;
2069 nesmr
->ibmr
.lkey
= stag
;
2070 nesmr
->mode
= IWNES_MEMREG_TYPE_MEM
;
2071 ibmr
= &nesmr
->ibmr
;
2074 ibmr
= ERR_PTR(-ENOMEM
);
2078 /* single PBL case */
2079 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
, vpbl
.pbl_pbase
);
2087 static struct ib_mr
*nes_get_dma_mr(struct ib_pd
*pd
, int acc
)
2091 nes_debug(NES_DBG_MR
, "\n");
2093 return nes_reg_phys_mr(pd
, 0, 0xffffffffffULL
, acc
, &kva
);
2099 static struct ib_mr
*nes_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
2100 u64 virt
, int acc
, struct ib_udata
*udata
)
2105 dma_addr_t last_dma_addr
= 0;
2106 dma_addr_t first_dma_addr
= 0;
2107 struct nes_pd
*nespd
= to_nespd(pd
);
2108 struct nes_vnic
*nesvnic
= to_nesvnic(pd
->device
);
2109 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2110 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2111 struct ib_mr
*ibmr
= ERR_PTR(-EINVAL
);
2112 struct scatterlist
*sg
;
2113 struct nes_ucontext
*nes_ucontext
;
2114 struct nes_pbl
*nespbl
;
2115 struct nes_mr
*nesmr
;
2116 struct ib_umem
*region
;
2117 struct nes_mem_reg_req req
;
2118 struct nes_vpbl vpbl
;
2119 struct nes_root_vpbl root_vpbl
;
2120 int entry
, page_index
;
2122 int err
, pbl_depth
= 0;
2127 u32 next_stag_index
;
2129 u32 root_pbl_index
= 0;
2130 u32 cur_pbl_index
= 0;
2137 region
= ib_umem_get(pd
->uobject
->context
, start
, length
, acc
, 0);
2138 if (IS_ERR(region
)) {
2139 return (struct ib_mr
*)region
;
2142 nes_debug(NES_DBG_MR
, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2143 " offset = %u, page size = %lu.\n",
2144 (unsigned long int)start
, (unsigned long int)virt
, (u32
)length
,
2145 ib_umem_offset(region
), BIT(region
->page_shift
));
2147 skip_pages
= ((u32
)ib_umem_offset(region
)) >> 12;
2149 if (ib_copy_from_udata(&req
, udata
, sizeof(req
))) {
2150 ib_umem_release(region
);
2151 return ERR_PTR(-EFAULT
);
2153 nes_debug(NES_DBG_MR
, "Memory Registration type = %08X.\n", req
.reg_type
);
2155 switch (req
.reg_type
) {
2156 case IWNES_MEMREG_TYPE_MEM
:
2159 vpbl
.pbl_vbase
= NULL
;
2160 root_vpbl
.pbl_vbase
= NULL
;
2161 root_vpbl
.pbl_pbase
= 0;
2163 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
2164 stag_key
= (u8
)next_stag_index
;
2166 driver_key
= next_stag_index
& 0x70000000;
2168 next_stag_index
>>= 8;
2169 next_stag_index
%= nesadapter
->max_mr
;
2171 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
,
2172 nesadapter
->max_mr
, &stag_index
, &next_stag_index
, NES_RESOURCE_USER_MR
);
2174 ib_umem_release(region
);
2175 return ERR_PTR(err
);
2178 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2180 ib_umem_release(region
);
2181 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2182 return ERR_PTR(-ENOMEM
);
2184 nesmr
->region
= region
;
2186 for_each_sg(region
->sg_head
.sgl
, sg
, region
->nmap
, entry
) {
2187 if (sg_dma_address(sg
) & ~PAGE_MASK
) {
2188 ib_umem_release(region
);
2189 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2190 nes_debug(NES_DBG_MR
, "Unaligned Memory Buffer: 0x%x\n",
2191 (unsigned int) sg_dma_address(sg
));
2192 ibmr
= ERR_PTR(-EINVAL
);
2194 goto reg_user_mr_err
;
2197 if (!sg_dma_len(sg
)) {
2198 ib_umem_release(region
);
2199 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2201 nes_debug(NES_DBG_MR
, "Invalid Buffer Size\n");
2202 ibmr
= ERR_PTR(-EINVAL
);
2204 goto reg_user_mr_err
;
2207 region_length
+= sg_dma_len(sg
);
2208 chunk_pages
= sg_dma_len(sg
) >> 12;
2209 region_length
-= skip_pages
<< 12;
2210 for (page_index
= skip_pages
; page_index
< chunk_pages
; page_index
++) {
2212 if ((page_count
!= 0) && (page_count
<< 12) - (ib_umem_offset(region
) & (4096 - 1)) >= region
->length
)
2214 if ((page_count
&0x01FF) == 0) {
2215 if (page_count
>= 1024 * 512) {
2216 ib_umem_release(region
);
2217 nes_free_resource(nesadapter
,
2218 nesadapter
->allocated_mrs
, stag_index
);
2220 ibmr
= ERR_PTR(-E2BIG
);
2221 goto reg_user_mr_err
;
2223 if (root_pbl_index
== 1) {
2224 root_vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
,
2225 8192, &root_vpbl
.pbl_pbase
);
2226 nes_debug(NES_DBG_MR
, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2227 root_vpbl
.pbl_vbase
, (unsigned int)root_vpbl
.pbl_pbase
);
2228 if (!root_vpbl
.pbl_vbase
) {
2229 ib_umem_release(region
);
2230 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2232 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2235 ibmr
= ERR_PTR(-ENOMEM
);
2236 goto reg_user_mr_err
;
2238 root_vpbl
.leaf_vpbl
= kcalloc(1024,
2239 sizeof(*root_vpbl
.leaf_vpbl
),
2241 if (!root_vpbl
.leaf_vpbl
) {
2242 ib_umem_release(region
);
2243 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2244 root_vpbl
.pbl_pbase
);
2245 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2247 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2250 ibmr
= ERR_PTR(-ENOMEM
);
2251 goto reg_user_mr_err
;
2253 root_vpbl
.pbl_vbase
[0].pa_low
=
2254 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2255 root_vpbl
.pbl_vbase
[0].pa_high
=
2256 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
) >> 32)));
2257 root_vpbl
.leaf_vpbl
[0] = vpbl
;
2259 vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 4096,
2261 nes_debug(NES_DBG_MR
, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2262 vpbl
.pbl_vbase
, (unsigned int)vpbl
.pbl_pbase
);
2263 if (!vpbl
.pbl_vbase
) {
2264 ib_umem_release(region
);
2265 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2266 ibmr
= ERR_PTR(-ENOMEM
);
2268 goto reg_user_mr_err
;
2270 if (1 <= root_pbl_index
) {
2271 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_low
=
2272 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2273 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_high
=
2274 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
)>>32)));
2275 root_vpbl
.leaf_vpbl
[root_pbl_index
] = vpbl
;
2281 if (page_count
!= 0) {
2282 if ((last_dma_addr
+4096) !=
2283 (sg_dma_address(sg
)+
2286 last_dma_addr
= sg_dma_address(sg
)+
2289 first_dma_addr
= sg_dma_address(sg
)+
2291 last_dma_addr
= first_dma_addr
;
2295 vpbl
.pbl_vbase
[cur_pbl_index
].pa_low
=
2296 cpu_to_le32((u32
)(sg_dma_address(sg
)+
2297 (page_index
*4096)));
2298 vpbl
.pbl_vbase
[cur_pbl_index
].pa_high
=
2299 cpu_to_le32((u32
)((((u64
)(sg_dma_address(sg
)+
2300 (page_index
*4096))) >> 32)));
2307 nes_debug(NES_DBG_MR
, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2308 " stag_key=0x%08x\n",
2309 stag_index
, driver_key
, stag_key
);
2310 stag
= stag_index
<< 8;
2312 stag
+= (u32
)stag_key
;
2315 /* Make the leaf PBL the root if only one PBL */
2316 if (root_pbl_index
== 1) {
2317 root_vpbl
.pbl_pbase
= vpbl
.pbl_pbase
;
2323 pbl_count
= root_pbl_index
;
2326 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2327 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2328 stag
, (unsigned int)iova_start
,
2329 (unsigned int)region_length
, stag_index
,
2330 (unsigned long long)region
->length
, pbl_count
);
2331 ret
= nes_reg_mr(nesdev
, nespd
, stag
, region
->length
, &root_vpbl
,
2332 first_dma_addr
, pbl_count
, (u16
)cur_pbl_index
, acc
,
2333 &iova_start
, &nesmr
->pbls_used
, &nesmr
->pbl_4k
);
2335 nes_debug(NES_DBG_MR
, "ret=%d\n", ret
);
2338 nesmr
->ibmr
.rkey
= stag
;
2339 nesmr
->ibmr
.lkey
= stag
;
2340 nesmr
->mode
= IWNES_MEMREG_TYPE_MEM
;
2341 ibmr
= &nesmr
->ibmr
;
2343 ib_umem_release(region
);
2345 ibmr
= ERR_PTR(-ENOMEM
);
2349 /* free the resources */
2350 if (root_pbl_index
== 1) {
2351 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2354 for (page_index
=0; page_index
<root_pbl_index
; page_index
++) {
2355 pci_free_consistent(nesdev
->pcidev
, 4096,
2356 root_vpbl
.leaf_vpbl
[page_index
].pbl_vbase
,
2357 root_vpbl
.leaf_vpbl
[page_index
].pbl_pbase
);
2359 kfree(root_vpbl
.leaf_vpbl
);
2360 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2361 root_vpbl
.pbl_pbase
);
2364 nes_debug(NES_DBG_MR
, "Leaving, ibmr=%p", ibmr
);
2367 case IWNES_MEMREG_TYPE_QP
:
2368 case IWNES_MEMREG_TYPE_CQ
:
2369 if (!region
->length
) {
2370 nes_debug(NES_DBG_MR
, "Unable to register zero length region for CQ\n");
2371 ib_umem_release(region
);
2372 return ERR_PTR(-EINVAL
);
2374 nespbl
= kzalloc(sizeof(*nespbl
), GFP_KERNEL
);
2376 ib_umem_release(region
);
2377 return ERR_PTR(-ENOMEM
);
2379 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2381 ib_umem_release(region
);
2383 return ERR_PTR(-ENOMEM
);
2385 nesmr
->region
= region
;
2386 nes_ucontext
= to_nesucontext(pd
->uobject
->context
);
2387 pbl_depth
= region
->length
>> 12;
2388 pbl_depth
+= (region
->length
& (4096-1)) ? 1 : 0;
2389 nespbl
->pbl_size
= pbl_depth
*sizeof(u64
);
2390 if (req
.reg_type
== IWNES_MEMREG_TYPE_QP
) {
2391 nes_debug(NES_DBG_MR
, "Attempting to allocate QP PBL memory");
2393 nes_debug(NES_DBG_MR
, "Attempting to allocate CP PBL memory");
2396 nes_debug(NES_DBG_MR
, " %u bytes, %u entries.\n",
2397 nespbl
->pbl_size
, pbl_depth
);
2398 pbl
= pci_alloc_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
2399 &nespbl
->pbl_pbase
);
2401 ib_umem_release(region
);
2404 nes_debug(NES_DBG_MR
, "Unable to allocate PBL memory\n");
2405 return ERR_PTR(-ENOMEM
);
2408 nespbl
->pbl_vbase
= (u64
*)pbl
;
2409 nespbl
->user_base
= start
;
2410 nes_debug(NES_DBG_MR
, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2411 " pbl_vbase=%p user_base=0x%lx\n",
2412 nespbl
->pbl_size
, (unsigned long) nespbl
->pbl_pbase
,
2413 (void *) nespbl
->pbl_vbase
, nespbl
->user_base
);
2415 for_each_sg(region
->sg_head
.sgl
, sg
, region
->nmap
, entry
) {
2416 chunk_pages
= sg_dma_len(sg
) >> 12;
2417 chunk_pages
+= (sg_dma_len(sg
) & (4096-1)) ? 1 : 0;
2419 nespbl
->page
= sg_page(sg
);
2423 for (page_index
= 0; page_index
< chunk_pages
; page_index
++) {
2424 ((__le32
*)pbl
)[0] = cpu_to_le32((u32
)
2425 (sg_dma_address(sg
)+
2426 (page_index
*4096)));
2427 ((__le32
*)pbl
)[1] = cpu_to_le32(((u64
)
2428 (sg_dma_address(sg
)+
2429 (page_index
*4096)))>>32);
2430 nes_debug(NES_DBG_MR
, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl
,
2431 (unsigned long long)*pbl
,
2432 le32_to_cpu(((__le32
*)pbl
)[1]), le32_to_cpu(((__le32
*)pbl
)[0]));
2437 if (req
.reg_type
== IWNES_MEMREG_TYPE_QP
) {
2438 list_add_tail(&nespbl
->list
, &nes_ucontext
->qp_reg_mem_list
);
2440 list_add_tail(&nespbl
->list
, &nes_ucontext
->cq_reg_mem_list
);
2442 nesmr
->ibmr
.rkey
= -1;
2443 nesmr
->ibmr
.lkey
= -1;
2444 nesmr
->mode
= req
.reg_type
;
2445 return &nesmr
->ibmr
;
2448 ib_umem_release(region
);
2449 return ERR_PTR(-ENOSYS
);
2456 static int nes_dereg_mr(struct ib_mr
*ib_mr
)
2458 struct nes_mr
*nesmr
= to_nesmr(ib_mr
);
2459 struct nes_vnic
*nesvnic
= to_nesvnic(ib_mr
->device
);
2460 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2461 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2462 struct nes_hw_cqp_wqe
*cqp_wqe
;
2463 struct nes_cqp_request
*cqp_request
;
2464 unsigned long flags
;
2471 pci_free_consistent(nesdev
->pcidev
,
2472 nesmr
->max_pages
* sizeof(u64
),
2476 if (nesmr
->region
) {
2477 ib_umem_release(nesmr
->region
);
2479 if (nesmr
->mode
!= IWNES_MEMREG_TYPE_MEM
) {
2484 /* Deallocate the region with the adapter */
2486 cqp_request
= nes_get_cqp_request(nesdev
);
2487 if (cqp_request
== NULL
) {
2488 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
2491 cqp_request
->waiting
= 1;
2492 cqp_wqe
= &cqp_request
->cqp_wqe
;
2494 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2495 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
,
2496 NES_CQP_DEALLOCATE_STAG
| NES_CQP_STAG_VA_TO
|
2497 NES_CQP_STAG_DEALLOC_PBLS
| NES_CQP_STAG_MR
);
2498 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, ib_mr
->rkey
);
2500 atomic_set(&cqp_request
->refcount
, 2);
2501 nes_post_cqp_request(nesdev
, cqp_request
);
2504 nes_debug(NES_DBG_MR
, "Waiting for deallocate STag 0x%08X completed\n", ib_mr
->rkey
);
2505 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
2507 nes_debug(NES_DBG_MR
, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2508 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2509 ib_mr
->rkey
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2511 major_code
= cqp_request
->major_code
;
2512 minor_code
= cqp_request
->minor_code
;
2514 nes_put_cqp_request(nesdev
, cqp_request
);
2517 nes_debug(NES_DBG_MR
, "Timeout waiting to destroy STag,"
2518 " ib_mr=%p, rkey = 0x%08X\n",
2519 ib_mr
, ib_mr
->rkey
);
2521 } else if (major_code
) {
2522 nes_debug(NES_DBG_MR
, "Error (0x%04X:0x%04X) while attempting"
2523 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2524 major_code
, minor_code
, ib_mr
, ib_mr
->rkey
);
2528 if (nesmr
->pbls_used
!= 0) {
2529 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
2530 if (nesmr
->pbl_4k
) {
2531 nesadapter
->free_4kpbl
+= nesmr
->pbls_used
;
2532 if (nesadapter
->free_4kpbl
> nesadapter
->max_4kpbl
)
2533 printk(KERN_ERR PFX
"free 4KB PBLs(%u) has "
2534 "exceeded the max(%u)\n",
2535 nesadapter
->free_4kpbl
,
2536 nesadapter
->max_4kpbl
);
2538 nesadapter
->free_256pbl
+= nesmr
->pbls_used
;
2539 if (nesadapter
->free_256pbl
> nesadapter
->max_256pbl
)
2540 printk(KERN_ERR PFX
"free 256B PBLs(%u) has "
2541 "exceeded the max(%u)\n",
2542 nesadapter
->free_256pbl
,
2543 nesadapter
->max_256pbl
);
2545 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
2547 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2548 (ib_mr
->rkey
& 0x0fffff00) >> 8);
2559 static ssize_t
hw_rev_show(struct device
*dev
,
2560 struct device_attribute
*attr
, char *buf
)
2562 struct nes_ib_device
*nesibdev
=
2563 container_of(dev
, struct nes_ib_device
, ibdev
.dev
);
2564 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
2566 nes_debug(NES_DBG_INIT
, "\n");
2567 return sprintf(buf
, "%x\n", nesvnic
->nesdev
->nesadapter
->hw_rev
);
2569 static DEVICE_ATTR_RO(hw_rev
);
2574 static ssize_t
hca_type_show(struct device
*dev
,
2575 struct device_attribute
*attr
, char *buf
)
2577 nes_debug(NES_DBG_INIT
, "\n");
2578 return sprintf(buf
, "NES020\n");
2580 static DEVICE_ATTR_RO(hca_type
);
2585 static ssize_t
board_id_show(struct device
*dev
,
2586 struct device_attribute
*attr
, char *buf
)
2588 nes_debug(NES_DBG_INIT
, "\n");
2589 return sprintf(buf
, "%.*s\n", 32, "NES020 Board ID");
2591 static DEVICE_ATTR_RO(board_id
);
2593 static struct attribute
*nes_dev_attributes
[] = {
2594 &dev_attr_hw_rev
.attr
,
2595 &dev_attr_hca_type
.attr
,
2596 &dev_attr_board_id
.attr
,
2600 static const struct attribute_group nes_attr_group
= {
2601 .attrs
= nes_dev_attributes
,
2607 static int nes_query_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2608 int attr_mask
, struct ib_qp_init_attr
*init_attr
)
2610 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
2612 nes_debug(NES_DBG_QP
, "\n");
2614 attr
->qp_access_flags
= 0;
2615 attr
->cap
.max_send_wr
= nesqp
->hwqp
.sq_size
;
2616 attr
->cap
.max_recv_wr
= nesqp
->hwqp
.rq_size
;
2617 attr
->cap
.max_recv_sge
= 1;
2618 if (nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
)
2619 attr
->cap
.max_inline_data
= 0;
2621 attr
->cap
.max_inline_data
= 64;
2623 init_attr
->event_handler
= nesqp
->ibqp
.event_handler
;
2624 init_attr
->qp_context
= nesqp
->ibqp
.qp_context
;
2625 init_attr
->send_cq
= nesqp
->ibqp
.send_cq
;
2626 init_attr
->recv_cq
= nesqp
->ibqp
.recv_cq
;
2627 init_attr
->srq
= nesqp
->ibqp
.srq
;
2628 init_attr
->cap
= attr
->cap
;
2637 int nes_hw_modify_qp(struct nes_device
*nesdev
, struct nes_qp
*nesqp
,
2638 u32 next_iwarp_state
, u32 termlen
, u32 wait_completion
)
2640 struct nes_hw_cqp_wqe
*cqp_wqe
;
2641 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2642 /* struct iw_cm_event cm_event; */
2643 struct nes_cqp_request
*cqp_request
;
2647 nes_debug(NES_DBG_MOD_QP
, "QP%u, refcount=%d\n",
2648 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
2650 cqp_request
= nes_get_cqp_request(nesdev
);
2651 if (cqp_request
== NULL
) {
2652 nes_debug(NES_DBG_MOD_QP
, "Failed to get a cqp_request.\n");
2655 if (wait_completion
) {
2656 cqp_request
->waiting
= 1;
2658 cqp_request
->waiting
= 0;
2660 cqp_wqe
= &cqp_request
->cqp_wqe
;
2662 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
,
2663 NES_CQP_MODIFY_QP
| NES_CQP_QP_TYPE_IWARP
| next_iwarp_state
);
2664 nes_debug(NES_DBG_MOD_QP
, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2665 next_iwarp_state
, le32_to_cpu(cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
]));
2666 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2667 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
, nesqp
->hwqp
.qp_id
);
2668 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_CONTEXT_LOW_IDX
, (u64
)nesqp
->nesqp_context_pbase
);
2670 /* If sending a terminate message, fill in the length (in words) */
2671 if (((next_iwarp_state
& NES_CQP_QP_IWARP_STATE_MASK
) == NES_CQP_QP_IWARP_STATE_TERMINATE
) &&
2672 !(next_iwarp_state
& NES_CQP_QP_TERM_DONT_SEND_TERM_MSG
)) {
2673 termlen
= ((termlen
+ 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT
;
2674 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_NEW_MSS_IDX
, termlen
);
2677 atomic_set(&cqp_request
->refcount
, 2);
2678 nes_post_cqp_request(nesdev
, cqp_request
);
2681 if (wait_completion
) {
2682 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2683 nesqp->hwqp.qp_id); */
2684 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
2686 nes_debug(NES_DBG_MOD_QP
, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2687 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2688 nesqp
->hwqp
.qp_id
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2689 major_code
= cqp_request
->major_code
;
2691 nes_debug(NES_DBG_MOD_QP
, "Modify iwarp QP%u failed"
2692 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2693 nesqp
->hwqp
.qp_id
, cqp_request
->major_code
,
2694 cqp_request
->minor_code
, next_iwarp_state
);
2697 nes_put_cqp_request(nesdev
, cqp_request
);
2701 else if (major_code
)
2714 int nes_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2715 int attr_mask
, struct ib_udata
*udata
)
2717 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
2718 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
2719 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2722 u32 next_iwarp_state
= 0;
2724 unsigned long qplockflags
;
2726 u16 original_last_aeq
;
2727 u8 issue_modify_qp
= 0;
2730 nes_debug(NES_DBG_MOD_QP
, "QP%u: QP State=%u, cur QP State=%u,"
2731 " iwarp_state=0x%X, refcount=%d\n",
2732 nesqp
->hwqp
.qp_id
, attr
->qp_state
, nesqp
->ibqp_state
,
2733 nesqp
->iwarp_state
, atomic_read(&nesqp
->refcount
));
2735 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
2737 nes_debug(NES_DBG_MOD_QP
, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2738 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2739 nesqp
->hwqp
.qp_id
, nesqp
->hw_iwarp_state
,
2740 nesqp
->hw_tcp_state
, attr
->qp_access_flags
, attr_mask
);
2742 if (attr_mask
& IB_QP_STATE
) {
2743 switch (attr
->qp_state
) {
2745 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = init\n",
2747 if (nesqp
->iwarp_state
> (u32
)NES_CQP_QP_IWARP_STATE_IDLE
) {
2748 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2751 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_IDLE
;
2752 issue_modify_qp
= 1;
2755 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = rtr\n",
2757 if (nesqp
->iwarp_state
>(u32
)NES_CQP_QP_IWARP_STATE_IDLE
) {
2758 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2761 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_IDLE
;
2762 issue_modify_qp
= 1;
2765 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = rts\n",
2767 if (nesqp
->iwarp_state
>(u32
)NES_CQP_QP_IWARP_STATE_RTS
) {
2768 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2771 if (nesqp
->cm_id
== NULL
) {
2772 nes_debug(NES_DBG_MOD_QP
, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2773 nesqp
->hwqp
.qp_id
);
2774 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2777 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_RTS
;
2778 if (nesqp
->iwarp_state
!= NES_CQP_QP_IWARP_STATE_RTS
)
2779 next_iwarp_state
|= NES_CQP_QP_CONTEXT_VALID
|
2780 NES_CQP_QP_ARP_VALID
| NES_CQP_QP_ORD_VALID
;
2781 issue_modify_qp
= 1;
2782 nesqp
->hw_tcp_state
= NES_AEQE_TCP_STATE_ESTABLISHED
;
2783 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_RTS
;
2784 nesqp
->hte_added
= 1;
2787 issue_modify_qp
= 1;
2788 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2789 nesqp
->hwqp
.qp_id
, nesqp
->hwqp
.sq_head
, nesqp
->hwqp
.sq_tail
);
2790 if (nesqp
->iwarp_state
== (u32
)NES_CQP_QP_IWARP_STATE_CLOSING
) {
2791 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2794 if (nesqp
->iwarp_state
> (u32
)NES_CQP_QP_IWARP_STATE_CLOSING
) {
2795 nes_debug(NES_DBG_MOD_QP
, "QP%u: State change to closing"
2796 " ignored due to current iWARP state\n",
2798 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2801 if (nesqp
->hw_iwarp_state
!= NES_AEQE_IWARP_STATE_RTS
) {
2802 nes_debug(NES_DBG_MOD_QP
, "QP%u: State change to closing"
2803 " already done based on hw state.\n",
2805 issue_modify_qp
= 0;
2807 switch (nesqp
->hw_iwarp_state
) {
2808 case NES_AEQE_IWARP_STATE_CLOSING
:
2809 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_CLOSING
;
2811 case NES_AEQE_IWARP_STATE_TERMINATE
:
2812 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_TERMINATE
;
2814 case NES_AEQE_IWARP_STATE_ERROR
:
2815 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_ERROR
;
2818 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_CLOSING
;
2819 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_CLOSING
;
2825 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = terminate\n",
2827 if (nesqp
->iwarp_state
>=(u32
)NES_CQP_QP_IWARP_STATE_TERMINATE
) {
2828 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2831 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2832 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_TERMINATE
;
2833 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_TERMINATE
;
2834 issue_modify_qp
= 1;
2838 if (nesqp
->iwarp_state
== (u32
)NES_CQP_QP_IWARP_STATE_ERROR
) {
2839 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2842 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = error\n",
2844 if (nesqp
->term_flags
)
2845 del_timer(&nesqp
->terminate_timer
);
2847 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_ERROR
;
2848 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2849 if (nesqp
->hte_added
) {
2850 nes_debug(NES_DBG_MOD_QP
, "set CQP_QP_DEL_HTE\n");
2851 next_iwarp_state
|= NES_CQP_QP_DEL_HTE
;
2852 nesqp
->hte_added
= 0;
2854 if ((nesqp
->hw_tcp_state
> NES_AEQE_TCP_STATE_CLOSED
) &&
2855 (nesdev
->iw_status
) &&
2856 (nesqp
->hw_tcp_state
!= NES_AEQE_TCP_STATE_TIME_WAIT
)) {
2857 next_iwarp_state
|= NES_CQP_QP_RESET
;
2859 nes_debug(NES_DBG_MOD_QP
, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2860 nesqp
->hwqp
.qp_id
, nesqp
->hw_tcp_state
);
2863 issue_modify_qp
= 1;
2864 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_ERROR
;
2867 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2872 nesqp
->ibqp_state
= attr
->qp_state
;
2873 nesqp
->iwarp_state
= next_iwarp_state
& NES_CQP_QP_IWARP_STATE_MASK
;
2874 nes_debug(NES_DBG_MOD_QP
, "Change nesqp->iwarp_state=%08x\n",
2875 nesqp
->iwarp_state
);
2878 if (attr_mask
& IB_QP_ACCESS_FLAGS
) {
2879 if (attr
->qp_access_flags
& IB_ACCESS_LOCAL_WRITE
) {
2880 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
|
2881 NES_QPCONTEXT_MISC_RDMA_READ_EN
);
2882 issue_modify_qp
= 1;
2884 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_WRITE
) {
2885 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
);
2886 issue_modify_qp
= 1;
2888 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_READ
) {
2889 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN
);
2890 issue_modify_qp
= 1;
2892 if (attr
->qp_access_flags
& IB_ACCESS_MW_BIND
) {
2893 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN
);
2894 issue_modify_qp
= 1;
2897 if (nesqp
->user_mode
) {
2898 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
|
2899 NES_QPCONTEXT_MISC_RDMA_READ_EN
);
2900 issue_modify_qp
= 1;
2904 original_last_aeq
= nesqp
->last_aeq
;
2905 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2907 nes_debug(NES_DBG_MOD_QP
, "issue_modify_qp=%u\n", issue_modify_qp
);
2912 if (issue_modify_qp
) {
2913 nes_debug(NES_DBG_MOD_QP
, "call nes_hw_modify_qp\n");
2914 ret
= nes_hw_modify_qp(nesdev
, nesqp
, next_iwarp_state
, 0, 1);
2916 nes_debug(NES_DBG_MOD_QP
, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2917 " failed for QP%u.\n",
2918 next_iwarp_state
, nesqp
->hwqp
.qp_id
);
2922 if ((issue_modify_qp
) && (nesqp
->ibqp_state
> IB_QPS_RTS
)) {
2923 nes_debug(NES_DBG_MOD_QP
, "QP%u Issued ModifyQP refcount (%d),"
2924 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2925 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2926 original_last_aeq
, nesqp
->last_aeq
);
2927 if (!ret
|| original_last_aeq
!= NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE
) {
2929 if (nesqp
->cm_id
&& nesqp
->hw_tcp_state
!= 0) {
2930 nes_debug(NES_DBG_MOD_QP
, "QP%u Queuing fake disconnect for QP refcount (%d),"
2931 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2932 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2933 original_last_aeq
, nesqp
->last_aeq
);
2934 /* this one is for the cm_disconnect thread */
2935 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
2936 nesqp
->hw_tcp_state
= NES_AEQE_TCP_STATE_CLOSED
;
2937 nesqp
->last_aeq
= NES_AEQE_AEID_RESET_SENT
;
2938 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2939 nes_cm_disconn(nesqp
);
2941 nes_debug(NES_DBG_MOD_QP
, "QP%u No fake disconnect, QP refcount=%d\n",
2942 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
2945 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
2947 /* These two are for the timer thread */
2948 if (atomic_inc_return(&nesqp
->close_timer_started
) == 1) {
2949 nesqp
->cm_id
->add_ref(nesqp
->cm_id
);
2950 nes_debug(NES_DBG_MOD_QP
, "QP%u Not decrementing QP refcount (%d),"
2951 " need ae to finish up, original_last_aeq = 0x%04X."
2952 " last_aeq = 0x%04X, scheduling timer.\n",
2953 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2954 original_last_aeq
, nesqp
->last_aeq
);
2955 schedule_nes_timer(nesqp
->cm_node
, (struct sk_buff
*) nesqp
, NES_TIMER_TYPE_CLOSE
, 1, 0);
2957 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2959 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2960 nes_debug(NES_DBG_MOD_QP
, "QP%u Not decrementing QP refcount (%d),"
2961 " need ae to finish up, original_last_aeq = 0x%04X."
2962 " last_aeq = 0x%04X.\n",
2963 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2964 original_last_aeq
, nesqp
->last_aeq
);
2968 nes_debug(NES_DBG_MOD_QP
, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2969 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2970 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2971 original_last_aeq
, nesqp
->last_aeq
);
2974 nes_debug(NES_DBG_MOD_QP
, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2975 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2976 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2977 original_last_aeq
, nesqp
->last_aeq
);
2982 nes_debug(NES_DBG_MOD_QP
, "QP%u Leaving, refcount=%d\n",
2983 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
2989 fill_wqe_sg_send(struct nes_hw_qp_wqe
*wqe
, const struct ib_send_wr
*ib_wr
,
2993 int total_payload_length
= 0;
2994 for (sge_index
= 0; sge_index
< ib_wr
->num_sge
; sge_index
++) {
2995 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX
+(sge_index
*4),
2996 ib_wr
->sg_list
[sge_index
].addr
);
2997 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_LENGTH0_IDX
+ (sge_index
*4),
2998 ib_wr
->sg_list
[sge_index
].length
);
3000 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
+ (sge_index
*4),
3001 (ib_wr
->sg_list
[sge_index
].lkey
));
3003 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
+ (sge_index
*4), 0);
3005 total_payload_length
+= ib_wr
->sg_list
[sge_index
].length
;
3007 nes_debug(NES_DBG_IW_TX
, "UC UC UC, sending total_payload_length=%u \n",
3008 total_payload_length
);
3009 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3010 total_payload_length
);
3016 static int nes_post_send(struct ib_qp
*ibqp
, const struct ib_send_wr
*ib_wr
,
3017 const struct ib_send_wr
**bad_wr
)
3020 unsigned long flags
= 0;
3021 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
3022 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3023 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3024 struct nes_hw_qp_wqe
*wqe
;
3026 u32 qsize
= nesqp
->hwqp
.sq_size
;
3032 if (nesqp
->ibqp_state
> IB_QPS_RTS
) {
3037 spin_lock_irqsave(&nesqp
->lock
, flags
);
3039 head
= nesqp
->hwqp
.sq_head
;
3042 /* Check for QP error */
3043 if (nesqp
->term_flags
) {
3048 /* Check for SQ overflow */
3049 if (((head
+ (2 * qsize
) - nesqp
->hwqp
.sq_tail
) % qsize
) == (qsize
- 1)) {
3054 wqe
= &nesqp
->hwqp
.sq_vbase
[head
];
3055 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3056 nesqp->hwqp.qp_id, wqe, head); */
3057 nes_fill_init_qp_wqe(wqe
, nesqp
, head
);
3058 u64temp
= (u64
)(ib_wr
->wr_id
);
3059 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
,
3061 switch (ib_wr
->opcode
) {
3063 case IB_WR_SEND_WITH_INV
:
3064 if (IB_WR_SEND
== ib_wr
->opcode
) {
3065 if (ib_wr
->send_flags
& IB_SEND_SOLICITED
)
3066 wqe_misc
= NES_IWARP_SQ_OP_SENDSE
;
3068 wqe_misc
= NES_IWARP_SQ_OP_SEND
;
3070 if (ib_wr
->send_flags
& IB_SEND_SOLICITED
)
3071 wqe_misc
= NES_IWARP_SQ_OP_SENDSEINV
;
3073 wqe_misc
= NES_IWARP_SQ_OP_SENDINV
;
3075 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX
,
3076 ib_wr
->ex
.invalidate_rkey
);
3079 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3084 if (ib_wr
->send_flags
& IB_SEND_FENCE
)
3085 wqe_misc
|= NES_IWARP_SQ_WQE_LOCAL_FENCE
;
3087 if ((ib_wr
->send_flags
& IB_SEND_INLINE
) &&
3088 ((nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) == 0) &&
3089 (ib_wr
->sg_list
[0].length
<= 64)) {
3090 memcpy(&wqe
->wqe_words
[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX
],
3091 (void *)(unsigned long)ib_wr
->sg_list
[0].addr
, ib_wr
->sg_list
[0].length
);
3092 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3093 ib_wr
->sg_list
[0].length
);
3094 wqe_misc
|= NES_IWARP_SQ_WQE_IMM_DATA
;
3096 fill_wqe_sg_send(wqe
, ib_wr
, 1);
3100 case IB_WR_RDMA_WRITE
:
3101 wqe_misc
= NES_IWARP_SQ_OP_RDMAW
;
3102 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3103 nes_debug(NES_DBG_IW_TX
, "Exceeded max sge, ib_wr=%u, max=%u\n",
3104 ib_wr
->num_sge
, nesdev
->nesadapter
->max_sge
);
3109 if (ib_wr
->send_flags
& IB_SEND_FENCE
)
3110 wqe_misc
|= NES_IWARP_SQ_WQE_LOCAL_FENCE
;
3112 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_STAG_IDX
,
3113 rdma_wr(ib_wr
)->rkey
);
3114 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX
,
3115 rdma_wr(ib_wr
)->remote_addr
);
3117 if ((ib_wr
->send_flags
& IB_SEND_INLINE
) &&
3118 ((nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) == 0) &&
3119 (ib_wr
->sg_list
[0].length
<= 64)) {
3120 memcpy(&wqe
->wqe_words
[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX
],
3121 (void *)(unsigned long)ib_wr
->sg_list
[0].addr
, ib_wr
->sg_list
[0].length
);
3122 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3123 ib_wr
->sg_list
[0].length
);
3124 wqe_misc
|= NES_IWARP_SQ_WQE_IMM_DATA
;
3126 fill_wqe_sg_send(wqe
, ib_wr
, 1);
3129 wqe
->wqe_words
[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
] =
3130 wqe
->wqe_words
[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
];
3132 case IB_WR_RDMA_READ
:
3133 case IB_WR_RDMA_READ_WITH_INV
:
3134 /* iWARP only supports 1 sge for RDMA reads */
3135 if (ib_wr
->num_sge
> 1) {
3136 nes_debug(NES_DBG_IW_TX
, "Exceeded max sge, ib_wr=%u, max=1\n",
3141 if (ib_wr
->opcode
== IB_WR_RDMA_READ
) {
3142 wqe_misc
= NES_IWARP_SQ_OP_RDMAR
;
3144 wqe_misc
= NES_IWARP_SQ_OP_RDMAR_LOCINV
;
3145 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX
,
3146 ib_wr
->ex
.invalidate_rkey
);
3149 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX
,
3150 rdma_wr(ib_wr
)->remote_addr
);
3151 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_STAG_IDX
,
3152 rdma_wr(ib_wr
)->rkey
);
3153 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
,
3154 ib_wr
->sg_list
->length
);
3155 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX
,
3156 ib_wr
->sg_list
->addr
);
3157 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
,
3158 ib_wr
->sg_list
->lkey
);
3160 case IB_WR_LOCAL_INV
:
3161 wqe_misc
= NES_IWARP_SQ_OP_LOCINV
;
3162 set_wqe_32bit_value(wqe
->wqe_words
,
3163 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX
,
3164 ib_wr
->ex
.invalidate_rkey
);
3168 struct nes_mr
*mr
= to_nesmr(reg_wr(ib_wr
)->mr
);
3169 int page_shift
= ilog2(reg_wr(ib_wr
)->mr
->page_size
);
3170 int flags
= reg_wr(ib_wr
)->access
;
3172 if (mr
->npages
> (NES_4K_PBL_CHUNK_SIZE
/ sizeof(u64
))) {
3173 nes_debug(NES_DBG_IW_TX
, "SQ_FMR: bad page_list_len\n");
3177 wqe_misc
= NES_IWARP_SQ_OP_FAST_REG
;
3178 set_wqe_64bit_value(wqe
->wqe_words
,
3179 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX
,
3181 set_wqe_32bit_value(wqe
->wqe_words
,
3182 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX
,
3183 lower_32_bits(mr
->ibmr
.length
));
3184 set_wqe_32bit_value(wqe
->wqe_words
,
3185 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX
, 0);
3186 set_wqe_32bit_value(wqe
->wqe_words
,
3187 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX
,
3188 reg_wr(ib_wr
)->key
);
3190 if (page_shift
== 12) {
3191 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K
;
3192 } else if (page_shift
== 21) {
3193 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M
;
3195 nes_debug(NES_DBG_IW_TX
, "Invalid page shift,"
3196 " ib_wr=%u, max=1\n", ib_wr
->num_sge
);
3201 /* Set access_flags */
3202 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ
;
3203 if (flags
& IB_ACCESS_LOCAL_WRITE
)
3204 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE
;
3206 if (flags
& IB_ACCESS_REMOTE_WRITE
)
3207 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE
;
3209 if (flags
& IB_ACCESS_REMOTE_READ
)
3210 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ
;
3212 if (flags
& IB_ACCESS_MW_BIND
)
3213 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND
;
3215 /* Fill in PBL info: */
3216 set_wqe_64bit_value(wqe
->wqe_words
,
3217 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX
,
3220 set_wqe_32bit_value(wqe
->wqe_words
,
3221 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX
,
3224 nes_debug(NES_DBG_IW_TX
, "SQ_REG_MR: iova_start: %llx, "
3225 "length: %lld, rkey: %0x, pgl_paddr: %llx, "
3226 "page_list_len: %u, wqe_misc: %x\n",
3227 (unsigned long long) mr
->ibmr
.iova
,
3230 (unsigned long long) mr
->paddr
,
3244 if ((ib_wr
->send_flags
& IB_SEND_SIGNALED
) || nesqp
->sig_all
)
3245 wqe_misc
|= NES_IWARP_SQ_WQE_SIGNALED_COMPL
;
3247 wqe
->wqe_words
[NES_IWARP_SQ_WQE_MISC_IDX
] = cpu_to_le32(wqe_misc
);
3249 ib_wr
= ib_wr
->next
;
3257 nesqp
->hwqp
.sq_head
= head
;
3260 counter
= min(wqe_count
, ((u32
)255));
3261 wqe_count
-= counter
;
3262 nes_write32(nesdev
->regs
+ NES_WQE_ALLOC
,
3263 (counter
<< 24) | 0x00800000 | nesqp
->hwqp
.qp_id
);
3266 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
3278 static int nes_post_recv(struct ib_qp
*ibqp
, const struct ib_recv_wr
*ib_wr
,
3279 const struct ib_recv_wr
**bad_wr
)
3282 unsigned long flags
= 0;
3283 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
3284 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3285 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3286 struct nes_hw_qp_wqe
*wqe
;
3289 u32 qsize
= nesqp
->hwqp
.rq_size
;
3293 u32 total_payload_length
;
3295 if (nesqp
->ibqp_state
> IB_QPS_RTS
) {
3300 spin_lock_irqsave(&nesqp
->lock
, flags
);
3302 head
= nesqp
->hwqp
.rq_head
;
3305 /* Check for QP error */
3306 if (nesqp
->term_flags
) {
3311 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3315 /* Check for RQ overflow */
3316 if (((head
+ (2 * qsize
) - nesqp
->hwqp
.rq_tail
) % qsize
) == (qsize
- 1)) {
3321 nes_debug(NES_DBG_IW_RX
, "ibwr sge count = %u.\n", ib_wr
->num_sge
);
3322 wqe
= &nesqp
->hwqp
.rq_vbase
[head
];
3324 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3325 nesqp->hwqp.qp_id, wqe, head); */
3326 nes_fill_init_qp_wqe(wqe
, nesqp
, head
);
3327 u64temp
= (u64
)(ib_wr
->wr_id
);
3328 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
,
3330 total_payload_length
= 0;
3331 for (sge_index
=0; sge_index
< ib_wr
->num_sge
; sge_index
++) {
3332 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX
+(sge_index
*4),
3333 ib_wr
->sg_list
[sge_index
].addr
);
3334 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_LENGTH0_IDX
+(sge_index
*4),
3335 ib_wr
->sg_list
[sge_index
].length
);
3336 set_wqe_32bit_value(wqe
->wqe_words
,NES_IWARP_RQ_WQE_STAG0_IDX
+(sge_index
*4),
3337 ib_wr
->sg_list
[sge_index
].lkey
);
3339 total_payload_length
+= ib_wr
->sg_list
[sge_index
].length
;
3341 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX
,
3342 total_payload_length
);
3344 ib_wr
= ib_wr
->next
;
3351 nesqp
->hwqp
.rq_head
= head
;
3354 counter
= min(wqe_count
, ((u32
)255));
3355 wqe_count
-= counter
;
3356 nes_write32(nesdev
->regs
+NES_WQE_ALLOC
, (counter
<<24) | nesqp
->hwqp
.qp_id
);
3359 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
3368 * nes_drain_sq - drain sq
3369 * @ibqp: pointer to ibqp
3371 static void nes_drain_sq(struct ib_qp
*ibqp
)
3373 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3375 if (nesqp
->hwqp
.sq_tail
!= nesqp
->hwqp
.sq_head
)
3376 wait_for_completion(&nesqp
->sq_drained
);
3380 * nes_drain_rq - drain rq
3381 * @ibqp: pointer to ibqp
3383 static void nes_drain_rq(struct ib_qp
*ibqp
)
3385 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3387 if (nesqp
->hwqp
.rq_tail
!= nesqp
->hwqp
.rq_head
)
3388 wait_for_completion(&nesqp
->rq_drained
);
3394 static int nes_poll_cq(struct ib_cq
*ibcq
, int num_entries
, struct ib_wc
*entry
)
3398 unsigned long flags
= 0;
3399 struct nes_vnic
*nesvnic
= to_nesvnic(ibcq
->device
);
3400 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3401 struct nes_cq
*nescq
= to_nescq(ibcq
);
3402 struct nes_qp
*nesqp
;
3403 struct nes_hw_cqe cqe
;
3410 u32 move_cq_head
= 1;
3413 nes_debug(NES_DBG_CQ
, "\n");
3415 spin_lock_irqsave(&nescq
->lock
, flags
);
3417 head
= nescq
->hw_cq
.cq_head
;
3418 cq_size
= nescq
->hw_cq
.cq_size
;
3420 while (cqe_count
< num_entries
) {
3421 if ((le32_to_cpu(nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_OPCODE_IDX
]) &
3422 NES_CQE_VALID
) == 0)
3426 * Make sure we read CQ entry contents *after*
3427 * we've checked the valid bit.
3431 cqe
= nescq
->hw_cq
.cq_vbase
[head
];
3432 u32temp
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
3433 wqe_index
= u32temp
& (nesdev
->nesadapter
->max_qp_wr
- 1);
3434 u32temp
&= ~(NES_SW_CONTEXT_ALIGN
-1);
3435 /* parse CQE, get completion context from WQE (either rq or sq) */
3436 u64temp
= (((u64
)(le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
])))<<32) |
3440 nesqp
= (struct nes_qp
*)(unsigned long)u64temp
;
3441 memset(entry
, 0, sizeof *entry
);
3442 if (cqe
.cqe_words
[NES_CQE_ERROR_CODE_IDX
] == 0) {
3443 entry
->status
= IB_WC_SUCCESS
;
3445 err_code
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_ERROR_CODE_IDX
]);
3446 if (NES_IWARP_CQE_MAJOR_DRV
== (err_code
>> 16)) {
3447 entry
->status
= err_code
& 0x0000ffff;
3449 /* The rest of the cqe's will be marked as flushed */
3450 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_ERROR_CODE_IDX
] =
3451 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH
<< 16) |
3452 NES_IWARP_CQE_MINOR_FLUSH
);
3454 entry
->status
= IB_WC_WR_FLUSH_ERR
;
3457 entry
->qp
= &nesqp
->ibqp
;
3458 entry
->src_qp
= nesqp
->hwqp
.qp_id
;
3460 if (le32_to_cpu(cqe
.cqe_words
[NES_CQE_OPCODE_IDX
]) & NES_CQE_SQ
) {
3461 if (nesqp
->skip_lsmm
) {
3462 nesqp
->skip_lsmm
= 0;
3463 nesqp
->hwqp
.sq_tail
++;
3466 /* Working on a SQ Completion*/
3467 wrid
= (((u64
)(cpu_to_le32((u32
)nesqp
->hwqp
.sq_vbase
[wqe_index
].
3468 wqe_words
[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX
]))) << 32) |
3469 ((u64
)(cpu_to_le32((u32
)nesqp
->hwqp
.sq_vbase
[wqe_index
].
3470 wqe_words
[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
])));
3471 entry
->byte_len
= le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3472 wqe_words
[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
]);
3474 switch (le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3475 wqe_words
[NES_IWARP_SQ_WQE_MISC_IDX
]) & 0x3f) {
3476 case NES_IWARP_SQ_OP_RDMAW
:
3477 nes_debug(NES_DBG_CQ
, "Operation = RDMA WRITE.\n");
3478 entry
->opcode
= IB_WC_RDMA_WRITE
;
3480 case NES_IWARP_SQ_OP_RDMAR
:
3481 nes_debug(NES_DBG_CQ
, "Operation = RDMA READ.\n");
3482 entry
->opcode
= IB_WC_RDMA_READ
;
3483 entry
->byte_len
= le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3484 wqe_words
[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
]);
3486 case NES_IWARP_SQ_OP_SENDINV
:
3487 case NES_IWARP_SQ_OP_SENDSEINV
:
3488 case NES_IWARP_SQ_OP_SEND
:
3489 case NES_IWARP_SQ_OP_SENDSE
:
3490 nes_debug(NES_DBG_CQ
, "Operation = Send.\n");
3491 entry
->opcode
= IB_WC_SEND
;
3493 case NES_IWARP_SQ_OP_LOCINV
:
3494 entry
->opcode
= IB_WC_LOCAL_INV
;
3496 case NES_IWARP_SQ_OP_FAST_REG
:
3497 entry
->opcode
= IB_WC_REG_MR
;
3501 nesqp
->hwqp
.sq_tail
= (wqe_index
+1)&(nesqp
->hwqp
.sq_size
- 1);
3502 if ((entry
->status
!= IB_WC_SUCCESS
) && (nesqp
->hwqp
.sq_tail
!= nesqp
->hwqp
.sq_head
)) {
3504 wq_tail
= nesqp
->hwqp
.sq_tail
;
3507 /* Working on a RQ Completion*/
3508 entry
->byte_len
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_PAYLOAD_LENGTH_IDX
]);
3509 wrid
= ((u64
)(le32_to_cpu(nesqp
->hwqp
.rq_vbase
[wqe_index
].wqe_words
[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX
]))) |
3510 ((u64
)(le32_to_cpu(nesqp
->hwqp
.rq_vbase
[wqe_index
].wqe_words
[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX
]))<<32);
3511 entry
->opcode
= IB_WC_RECV
;
3513 nesqp
->hwqp
.rq_tail
= (wqe_index
+1)&(nesqp
->hwqp
.rq_size
- 1);
3514 if ((entry
->status
!= IB_WC_SUCCESS
) && (nesqp
->hwqp
.rq_tail
!= nesqp
->hwqp
.rq_head
)) {
3516 wq_tail
= nesqp
->hwqp
.rq_tail
;
3520 if (nesqp
->iwarp_state
> NES_CQP_QP_IWARP_STATE_RTS
) {
3521 if (nesqp
->hwqp
.sq_tail
== nesqp
->hwqp
.sq_head
)
3522 complete(&nesqp
->sq_drained
);
3523 if (nesqp
->hwqp
.rq_tail
== nesqp
->hwqp
.rq_head
)
3524 complete(&nesqp
->rq_drained
);
3527 entry
->wr_id
= wrid
;
3533 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_OPCODE_IDX
] = 0;
3534 if (++head
>= cq_size
)
3536 nescq
->polled_completions
++;
3538 if ((nescq
->polled_completions
> (cq_size
/ 2)) ||
3539 (nescq
->polled_completions
== 255)) {
3540 nes_debug(NES_DBG_CQ
, "CQ%u Issuing CQE Allocate since more than half of cqes"
3541 " are pending %u of %u.\n",
3542 nescq
->hw_cq
.cq_number
, nescq
->polled_completions
, cq_size
);
3543 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
,
3544 nescq
->hw_cq
.cq_number
| (nescq
->polled_completions
<< 16));
3545 nescq
->polled_completions
= 0;
3548 /* Update the wqe index and set status to flush */
3549 wqe_index
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
3550 wqe_index
= (wqe_index
& (~(nesdev
->nesadapter
->max_qp_wr
- 1))) | wq_tail
;
3551 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
] =
3552 cpu_to_le32(wqe_index
);
3553 move_cq_head
= 1; /* ready for next pass */
3557 if (nescq
->polled_completions
) {
3558 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
,
3559 nescq
->hw_cq
.cq_number
| (nescq
->polled_completions
<< 16));
3560 nescq
->polled_completions
= 0;
3563 nescq
->hw_cq
.cq_head
= head
;
3564 nes_debug(NES_DBG_CQ
, "Reporting %u completions for CQ%u.\n",
3565 cqe_count
, nescq
->hw_cq
.cq_number
);
3567 spin_unlock_irqrestore(&nescq
->lock
, flags
);
3576 static int nes_req_notify_cq(struct ib_cq
*ibcq
, enum ib_cq_notify_flags notify_flags
)
3578 struct nes_vnic
*nesvnic
= to_nesvnic(ibcq
->device
);
3579 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3580 struct nes_cq
*nescq
= to_nescq(ibcq
);
3583 nes_debug(NES_DBG_CQ
, "Requesting notification for CQ%u.\n",
3584 nescq
->hw_cq
.cq_number
);
3586 cq_arm
= nescq
->hw_cq
.cq_number
;
3587 if ((notify_flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_NEXT_COMP
)
3588 cq_arm
|= NES_CQE_ALLOC_NOTIFY_NEXT
;
3589 else if ((notify_flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_SOLICITED
)
3590 cq_arm
|= NES_CQE_ALLOC_NOTIFY_SE
;
3594 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
, cq_arm
);
3595 nes_read32(nesdev
->regs
+NES_CQE_ALLOC
);
3600 static int nes_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
3601 struct ib_port_immutable
*immutable
)
3603 struct ib_port_attr attr
;
3606 immutable
->core_cap_flags
= RDMA_CORE_PORT_IWARP
;
3608 err
= nes_query_port(ibdev
, port_num
, &attr
);
3612 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
3613 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
3618 static void get_dev_fw_str(struct ib_device
*dev
, char *str
)
3620 struct nes_ib_device
*nesibdev
=
3621 container_of(dev
, struct nes_ib_device
, ibdev
);
3622 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
3624 nes_debug(NES_DBG_INIT
, "\n");
3625 snprintf(str
, IB_FW_VERSION_NAME_MAX
, "%u.%u",
3626 (nesvnic
->nesdev
->nesadapter
->firmware_version
>> 16),
3627 (nesvnic
->nesdev
->nesadapter
->firmware_version
& 0x000000ff));
3630 static const struct ib_device_ops nes_dev_ops
= {
3631 .alloc_mr
= nes_alloc_mr
,
3632 .alloc_mw
= nes_alloc_mw
,
3633 .alloc_pd
= nes_alloc_pd
,
3634 .alloc_ucontext
= nes_alloc_ucontext
,
3635 .create_cq
= nes_create_cq
,
3636 .create_qp
= nes_create_qp
,
3637 .dealloc_mw
= nes_dealloc_mw
,
3638 .dealloc_pd
= nes_dealloc_pd
,
3639 .dealloc_ucontext
= nes_dealloc_ucontext
,
3640 .dereg_mr
= nes_dereg_mr
,
3641 .destroy_cq
= nes_destroy_cq
,
3642 .destroy_qp
= nes_destroy_qp
,
3643 .drain_rq
= nes_drain_rq
,
3644 .drain_sq
= nes_drain_sq
,
3645 .get_dev_fw_str
= get_dev_fw_str
,
3646 .get_dma_mr
= nes_get_dma_mr
,
3647 .get_port_immutable
= nes_port_immutable
,
3648 .map_mr_sg
= nes_map_mr_sg
,
3650 .modify_qp
= nes_modify_qp
,
3651 .poll_cq
= nes_poll_cq
,
3652 .post_recv
= nes_post_recv
,
3653 .post_send
= nes_post_send
,
3654 .query_device
= nes_query_device
,
3655 .query_gid
= nes_query_gid
,
3656 .query_pkey
= nes_query_pkey
,
3657 .query_port
= nes_query_port
,
3658 .query_qp
= nes_query_qp
,
3659 .reg_user_mr
= nes_reg_user_mr
,
3660 .req_notify_cq
= nes_req_notify_cq
,
3664 * nes_init_ofa_device
3666 struct nes_ib_device
*nes_init_ofa_device(struct net_device
*netdev
)
3668 struct nes_ib_device
*nesibdev
;
3669 struct nes_vnic
*nesvnic
= netdev_priv(netdev
);
3670 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3672 nesibdev
= (struct nes_ib_device
*)ib_alloc_device(sizeof(struct nes_ib_device
));
3673 if (nesibdev
== NULL
) {
3676 nesibdev
->ibdev
.owner
= THIS_MODULE
;
3678 nesibdev
->ibdev
.node_type
= RDMA_NODE_RNIC
;
3679 memset(&nesibdev
->ibdev
.node_guid
, 0, sizeof(nesibdev
->ibdev
.node_guid
));
3680 memcpy(&nesibdev
->ibdev
.node_guid
, netdev
->dev_addr
, 6);
3682 nesibdev
->ibdev
.uverbs_cmd_mask
=
3683 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
3684 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
3685 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
3686 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
3687 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
3688 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
3689 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
3690 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
3691 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
3692 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
3693 (1ull << IB_USER_VERBS_CMD_CREATE_AH
) |
3694 (1ull << IB_USER_VERBS_CMD_DESTROY_AH
) |
3695 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ
) |
3696 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
3697 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
3698 (1ull << IB_USER_VERBS_CMD_POLL_CQ
) |
3699 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
3700 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
3701 (1ull << IB_USER_VERBS_CMD_BIND_MW
) |
3702 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
) |
3703 (1ull << IB_USER_VERBS_CMD_POST_RECV
) |
3704 (1ull << IB_USER_VERBS_CMD_POST_SEND
);
3706 nesibdev
->ibdev
.phys_port_cnt
= 1;
3707 nesibdev
->ibdev
.num_comp_vectors
= 1;
3708 nesibdev
->ibdev
.dev
.parent
= &nesdev
->pcidev
->dev
;
3710 nesibdev
->ibdev
.iwcm
= kzalloc(sizeof(*nesibdev
->ibdev
.iwcm
), GFP_KERNEL
);
3711 if (nesibdev
->ibdev
.iwcm
== NULL
) {
3712 ib_dealloc_device(&nesibdev
->ibdev
);
3715 nesibdev
->ibdev
.iwcm
->add_ref
= nes_add_ref
;
3716 nesibdev
->ibdev
.iwcm
->rem_ref
= nes_rem_ref
;
3717 nesibdev
->ibdev
.iwcm
->get_qp
= nes_get_qp
;
3718 nesibdev
->ibdev
.iwcm
->connect
= nes_connect
;
3719 nesibdev
->ibdev
.iwcm
->accept
= nes_accept
;
3720 nesibdev
->ibdev
.iwcm
->reject
= nes_reject
;
3721 nesibdev
->ibdev
.iwcm
->create_listen
= nes_create_listen
;
3722 nesibdev
->ibdev
.iwcm
->destroy_listen
= nes_destroy_listen
;
3724 ib_set_device_ops(&nesibdev
->ibdev
, &nes_dev_ops
);
3725 memcpy(nesibdev
->ibdev
.iwcm
->ifname
, netdev
->name
,
3726 sizeof(nesibdev
->ibdev
.iwcm
->ifname
));
3733 * nes_handle_delayed_event
3735 static void nes_handle_delayed_event(struct timer_list
*t
)
3737 struct nes_vnic
*nesvnic
= from_timer(nesvnic
, t
, event_timer
);
3739 if (nesvnic
->delayed_event
!= nesvnic
->last_dispatched_event
) {
3740 struct ib_event event
;
3742 event
.device
= &nesvnic
->nesibdev
->ibdev
;
3745 event
.event
= nesvnic
->delayed_event
;
3746 event
.element
.port_num
= nesvnic
->logical_port
+ 1;
3747 ib_dispatch_event(&event
);
3751 nesvnic
->event_timer
.function
= NULL
;
3755 void nes_port_ibevent(struct nes_vnic
*nesvnic
)
3757 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
3758 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3759 struct ib_event event
;
3760 event
.device
= &nesibdev
->ibdev
;
3761 event
.element
.port_num
= nesvnic
->logical_port
+ 1;
3762 event
.event
= nesdev
->iw_status
? IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
3764 if (!nesvnic
->event_timer
.function
) {
3765 ib_dispatch_event(&event
);
3766 nesvnic
->last_dispatched_event
= event
.event
;
3767 nesvnic
->event_timer
.function
= nes_handle_delayed_event
;
3768 nesvnic
->event_timer
.expires
= jiffies
+ NES_EVENT_DELAY
;
3769 add_timer(&nesvnic
->event_timer
);
3771 mod_timer(&nesvnic
->event_timer
, jiffies
+ NES_EVENT_DELAY
);
3773 nesvnic
->delayed_event
= event
.event
;
3778 * nes_destroy_ofa_device
3780 void nes_destroy_ofa_device(struct nes_ib_device
*nesibdev
)
3782 if (nesibdev
== NULL
)
3785 nes_unregister_ofa_device(nesibdev
);
3787 kfree(nesibdev
->ibdev
.iwcm
);
3788 ib_dealloc_device(&nesibdev
->ibdev
);
3793 * nes_register_ofa_device
3795 int nes_register_ofa_device(struct nes_ib_device
*nesibdev
)
3797 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
3798 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3799 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
3802 rdma_set_device_sysfs_group(&nesvnic
->nesibdev
->ibdev
, &nes_attr_group
);
3803 nesvnic
->nesibdev
->ibdev
.driver_id
= RDMA_DRIVER_NES
;
3804 ret
= ib_register_device(&nesvnic
->nesibdev
->ibdev
, "nes%d", NULL
);
3809 /* Get the resources allocated to this device */
3810 nesibdev
->max_cq
= (nesadapter
->max_cq
-NES_FIRST_QPN
) / nesadapter
->port_count
;
3811 nesibdev
->max_mr
= nesadapter
->max_mr
/ nesadapter
->port_count
;
3812 nesibdev
->max_qp
= (nesadapter
->max_qp
-NES_FIRST_QPN
) / nesadapter
->port_count
;
3813 nesibdev
->max_pd
= nesadapter
->max_pd
/ nesadapter
->port_count
;
3815 nesvnic
->of_device_registered
= 1;
3822 * nes_unregister_ofa_device
3824 static void nes_unregister_ofa_device(struct nes_ib_device
*nesibdev
)
3826 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
3828 if (nesvnic
->of_device_registered
)
3829 ib_unregister_device(&nesibdev
->ibdev
);
3831 nesvnic
->of_device_registered
= 0;