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
,
406 struct scatterlist
*sg
,
409 struct nes_mr
*nesmr
= to_nesmr(ibmr
);
413 return ib_sg_to_pages(ibmr
, sg
, sg_nents
, nes_set_page
);
419 static int nes_query_device(struct ib_device
*ibdev
, struct ib_device_attr
*props
,
420 struct ib_udata
*uhw
)
422 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
423 struct nes_device
*nesdev
= nesvnic
->nesdev
;
424 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
426 if (uhw
->inlen
|| uhw
->outlen
)
429 memset(props
, 0, sizeof(*props
));
430 memcpy(&props
->sys_image_guid
, nesvnic
->netdev
->dev_addr
, 6);
432 props
->fw_ver
= nesdev
->nesadapter
->firmware_version
;
433 props
->device_cap_flags
= nesdev
->nesadapter
->device_cap_flags
;
434 props
->vendor_id
= nesdev
->nesadapter
->vendor_id
;
435 props
->vendor_part_id
= nesdev
->nesadapter
->vendor_part_id
;
436 props
->hw_ver
= nesdev
->nesadapter
->hw_rev
;
437 props
->max_mr_size
= 0x80000000;
438 props
->max_qp
= nesibdev
->max_qp
;
439 props
->max_qp_wr
= nesdev
->nesadapter
->max_qp_wr
- 2;
440 props
->max_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 memset(props
, 0, sizeof(*props
));
481 props
->max_mtu
= IB_MTU_4096
;
483 if (netdev
->mtu
>= 4096)
484 props
->active_mtu
= IB_MTU_4096
;
485 else if (netdev
->mtu
>= 2048)
486 props
->active_mtu
= IB_MTU_2048
;
487 else if (netdev
->mtu
>= 1024)
488 props
->active_mtu
= IB_MTU_1024
;
489 else if (netdev
->mtu
>= 512)
490 props
->active_mtu
= IB_MTU_512
;
492 props
->active_mtu
= IB_MTU_256
;
498 if (netif_queue_stopped(netdev
))
499 props
->state
= IB_PORT_DOWN
;
500 else if (nesvnic
->linkup
)
501 props
->state
= IB_PORT_ACTIVE
;
503 props
->state
= IB_PORT_DOWN
;
504 props
->phys_state
= 0;
505 props
->port_cap_flags
= IB_PORT_CM_SUP
| IB_PORT_REINIT_SUP
|
506 IB_PORT_VENDOR_CLASS_SUP
| IB_PORT_BOOT_MGMT_SUP
;
507 props
->gid_tbl_len
= 1;
508 props
->pkey_tbl_len
= 1;
509 props
->qkey_viol_cntr
= 0;
510 props
->active_width
= IB_WIDTH_4X
;
511 props
->active_speed
= IB_SPEED_SDR
;
512 props
->max_msg_sz
= 0x80000000;
520 static int nes_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
, u16
*pkey
)
530 static int nes_query_gid(struct ib_device
*ibdev
, u8 port
,
531 int index
, union ib_gid
*gid
)
533 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
535 memset(&(gid
->raw
[0]), 0, sizeof(gid
->raw
));
536 memcpy(&(gid
->raw
[0]), nesvnic
->netdev
->dev_addr
, 6);
543 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
544 * of all objects associated with a particular user-mode client.
546 static struct ib_ucontext
*nes_alloc_ucontext(struct ib_device
*ibdev
,
547 struct ib_udata
*udata
)
549 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
550 struct nes_device
*nesdev
= nesvnic
->nesdev
;
551 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
552 struct nes_alloc_ucontext_req req
;
553 struct nes_alloc_ucontext_resp uresp
;
554 struct nes_ucontext
*nes_ucontext
;
555 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
558 if (ib_copy_from_udata(&req
, udata
, sizeof(struct nes_alloc_ucontext_req
))) {
559 printk(KERN_ERR PFX
"Invalid structure size on allocate user context.\n");
560 return ERR_PTR(-EINVAL
);
563 if (req
.userspace_ver
!= NES_ABI_USERSPACE_VER
) {
564 printk(KERN_ERR PFX
"Invalid userspace driver version detected. Detected version %d, should be %d\n",
565 req
.userspace_ver
, NES_ABI_USERSPACE_VER
);
566 return ERR_PTR(-EINVAL
);
570 memset(&uresp
, 0, sizeof uresp
);
572 uresp
.max_qps
= nesibdev
->max_qp
;
573 uresp
.max_pds
= nesibdev
->max_pd
;
574 uresp
.wq_size
= nesdev
->nesadapter
->max_qp_wr
* 2;
575 uresp
.virtwq
= nesadapter
->virtwq
;
576 uresp
.kernel_ver
= NES_ABI_KERNEL_VER
;
578 nes_ucontext
= kzalloc(sizeof *nes_ucontext
, GFP_KERNEL
);
580 return ERR_PTR(-ENOMEM
);
582 nes_ucontext
->nesdev
= nesdev
;
583 nes_ucontext
->mmap_wq_offset
= uresp
.max_pds
;
584 nes_ucontext
->mmap_cq_offset
= nes_ucontext
->mmap_wq_offset
+
585 ((sizeof(struct nes_hw_qp_wqe
) * uresp
.max_qps
* 2) + PAGE_SIZE
-1) /
589 if (ib_copy_to_udata(udata
, &uresp
, sizeof uresp
)) {
591 return ERR_PTR(-EFAULT
);
594 INIT_LIST_HEAD(&nes_ucontext
->cq_reg_mem_list
);
595 INIT_LIST_HEAD(&nes_ucontext
->qp_reg_mem_list
);
596 atomic_set(&nes_ucontext
->usecnt
, 1);
597 return &nes_ucontext
->ibucontext
;
602 * nes_dealloc_ucontext
604 static int nes_dealloc_ucontext(struct ib_ucontext
*context
)
606 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
607 /* struct nes_device *nesdev = nesvnic->nesdev; */
608 struct nes_ucontext
*nes_ucontext
= to_nesucontext(context
);
610 if (!atomic_dec_and_test(&nes_ucontext
->usecnt
))
620 static int nes_mmap(struct ib_ucontext
*context
, struct vm_area_struct
*vma
)
623 struct nes_vnic
*nesvnic
= to_nesvnic(context
->device
);
624 struct nes_device
*nesdev
= nesvnic
->nesdev
;
625 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
626 struct nes_ucontext
*nes_ucontext
;
627 struct nes_qp
*nesqp
;
629 nes_ucontext
= to_nesucontext(context
);
632 if (vma
->vm_pgoff
>= nes_ucontext
->mmap_wq_offset
) {
633 index
= (vma
->vm_pgoff
- nes_ucontext
->mmap_wq_offset
) * PAGE_SIZE
;
634 index
/= ((sizeof(struct nes_hw_qp_wqe
) * nesdev
->nesadapter
->max_qp_wr
* 2) +
635 PAGE_SIZE
-1) & (~(PAGE_SIZE
-1));
636 if (!test_bit(index
, nes_ucontext
->allocated_wqs
)) {
637 nes_debug(NES_DBG_MMAP
, "wq %lu not allocated\n", index
);
640 nesqp
= nes_ucontext
->mmap_nesqp
[index
];
642 nes_debug(NES_DBG_MMAP
, "wq %lu has a NULL QP base.\n", index
);
645 if (remap_pfn_range(vma
, vma
->vm_start
,
646 virt_to_phys(nesqp
->hwqp
.sq_vbase
) >> PAGE_SHIFT
,
647 vma
->vm_end
- vma
->vm_start
,
648 vma
->vm_page_prot
)) {
649 nes_debug(NES_DBG_MMAP
, "remap_pfn_range failed.\n");
652 vma
->vm_private_data
= nesqp
;
655 index
= vma
->vm_pgoff
;
656 if (!test_bit(index
, nes_ucontext
->allocated_doorbells
))
659 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
660 if (io_remap_pfn_range(vma
, vma
->vm_start
,
661 (nesdev
->doorbell_start
+
662 ((nes_ucontext
->mmap_db_index
[index
] - nesdev
->base_doorbell_index
) * 4096))
663 >> PAGE_SHIFT
, PAGE_SIZE
, vma
->vm_page_prot
))
665 vma
->vm_private_data
= nes_ucontext
;
676 static struct ib_pd
*nes_alloc_pd(struct ib_device
*ibdev
,
677 struct ib_ucontext
*context
, struct ib_udata
*udata
)
679 struct nes_pd
*nespd
;
680 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
681 struct nes_device
*nesdev
= nesvnic
->nesdev
;
682 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
683 struct nes_ucontext
*nesucontext
;
684 struct nes_alloc_pd_resp uresp
;
688 nes_debug(NES_DBG_PD
, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
689 nesvnic
, nesdev
->netdev
[0], nesdev
->netdev
[0]->name
, ibdev
, context
,
690 netdev_refcnt_read(nesvnic
->netdev
));
692 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_pds
,
693 nesadapter
->max_pd
, &pd_num
, &nesadapter
->next_pd
, NES_RESOURCE_PD
);
698 nespd
= kzalloc(sizeof (struct nes_pd
), GFP_KERNEL
);
700 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
, pd_num
);
701 return ERR_PTR(-ENOMEM
);
704 nes_debug(NES_DBG_PD
, "Allocating PD (%p) for ib device %s\n",
705 nespd
, nesvnic
->nesibdev
->ibdev
.name
);
707 nespd
->pd_id
= (pd_num
<< (PAGE_SHIFT
-12)) + nesadapter
->base_pd
;
710 nesucontext
= to_nesucontext(context
);
711 nespd
->mmap_db_index
= find_next_zero_bit(nesucontext
->allocated_doorbells
,
712 NES_MAX_USER_DB_REGIONS
, nesucontext
->first_free_db
);
713 nes_debug(NES_DBG_PD
, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
714 nespd
->mmap_db_index
, nespd
->pd_id
);
715 if (nespd
->mmap_db_index
>= NES_MAX_USER_DB_REGIONS
) {
716 nes_debug(NES_DBG_PD
, "mmap_db_index > MAX\n");
717 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
, pd_num
);
719 return ERR_PTR(-ENOMEM
);
722 uresp
.pd_id
= nespd
->pd_id
;
723 uresp
.mmap_db_index
= nespd
->mmap_db_index
;
724 if (ib_copy_to_udata(udata
, &uresp
, sizeof (struct nes_alloc_pd_resp
))) {
725 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
, pd_num
);
727 return ERR_PTR(-EFAULT
);
730 set_bit(nespd
->mmap_db_index
, nesucontext
->allocated_doorbells
);
731 nesucontext
->mmap_db_index
[nespd
->mmap_db_index
] = nespd
->pd_id
;
732 nesucontext
->first_free_db
= nespd
->mmap_db_index
+ 1;
735 nes_debug(NES_DBG_PD
, "PD%u structure located @%p.\n", nespd
->pd_id
, nespd
);
743 static int nes_dealloc_pd(struct ib_pd
*ibpd
)
745 struct nes_ucontext
*nesucontext
;
746 struct nes_pd
*nespd
= to_nespd(ibpd
);
747 struct nes_vnic
*nesvnic
= to_nesvnic(ibpd
->device
);
748 struct nes_device
*nesdev
= nesvnic
->nesdev
;
749 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
751 if ((ibpd
->uobject
) && (ibpd
->uobject
->context
)) {
752 nesucontext
= to_nesucontext(ibpd
->uobject
->context
);
753 nes_debug(NES_DBG_PD
, "Clearing bit %u from allocated doorbells\n",
754 nespd
->mmap_db_index
);
755 clear_bit(nespd
->mmap_db_index
, nesucontext
->allocated_doorbells
);
756 nesucontext
->mmap_db_index
[nespd
->mmap_db_index
] = 0;
757 if (nesucontext
->first_free_db
> nespd
->mmap_db_index
) {
758 nesucontext
->first_free_db
= nespd
->mmap_db_index
;
762 nes_debug(NES_DBG_PD
, "Deallocating PD%u structure located @%p.\n",
763 nespd
->pd_id
, nespd
);
764 nes_free_resource(nesadapter
, nesadapter
->allocated_pds
,
765 (nespd
->pd_id
-nesadapter
->base_pd
)>>(PAGE_SHIFT
-12));
775 static struct ib_ah
*nes_create_ah(struct ib_pd
*pd
, struct ib_ah_attr
*ah_attr
)
777 return ERR_PTR(-ENOSYS
);
784 static int nes_destroy_ah(struct ib_ah
*ah
)
791 * nes_get_encoded_size
793 static inline u8
nes_get_encoded_size(int *size
)
799 } else if (*size
<= 128) {
802 } else if (*size
<= 512) {
806 return (encoded_size
);
814 static int nes_setup_virt_qp(struct nes_qp
*nesqp
, struct nes_pbl
*nespbl
,
815 struct nes_vnic
*nesvnic
, int sq_size
, int rq_size
)
822 struct nes_device
*nesdev
= nesvnic
->nesdev
;
823 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
828 pbl_entries
= nespbl
->pbl_size
>> 3;
829 nes_debug(NES_DBG_QP
, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
830 nespbl
->pbl_size
, pbl_entries
,
831 (void *)nespbl
->pbl_vbase
,
832 (unsigned long) nespbl
->pbl_pbase
);
833 pbl
= (__le64
*) nespbl
->pbl_vbase
; /* points to first pbl entry */
834 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
835 /* the first pbl to be fro the rq_vbase... */
836 rq_pbl_entries
= (rq_size
* sizeof(struct nes_hw_qp_wqe
)) >> 12;
837 sq_pbl_entries
= (sq_size
* sizeof(struct nes_hw_qp_wqe
)) >> 12;
838 nesqp
->hwqp
.sq_pbase
= (le32_to_cpu(((__le32
*)pbl
)[0])) | ((u64
)((le32_to_cpu(((__le32
*)pbl
)[1]))) << 32);
840 nes_debug(NES_DBG_QP
, "QP nespbl->page is NULL \n");
845 nesqp
->hwqp
.sq_vbase
= kmap(nespbl
->page
);
846 nesqp
->page
= nespbl
->page
;
847 if (!nesqp
->hwqp
.sq_vbase
) {
848 nes_debug(NES_DBG_QP
, "QP sq_vbase kmap failed\n");
853 /* Now to get to sq.. we need to calculate how many */
854 /* PBL entries were used by the rq.. */
855 pbl
+= sq_pbl_entries
;
856 nesqp
->hwqp
.rq_pbase
= (le32_to_cpu(((__le32
*)pbl
)[0])) | ((u64
)((le32_to_cpu(((__le32
*)pbl
)[1]))) << 32);
857 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
858 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
860 nes_debug(NES_DBG_QP
, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
861 nesqp
->hwqp
.sq_vbase
, (unsigned long) nesqp
->hwqp
.sq_pbase
,
862 nesqp
->hwqp
.rq_vbase
, (unsigned long) nesqp
->hwqp
.rq_pbase
);
863 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
864 if (!nesadapter
->free_256pbl
) {
865 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
867 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
872 nesadapter
->free_256pbl
--;
873 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
875 nesqp
->pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 256, &nesqp
->pbl_pbase
);
876 pblbuffer
= nesqp
->pbl_vbase
;
877 if (!nesqp
->pbl_vbase
) {
878 /* memory allocated during nes_reg_user_mr() */
879 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
882 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
883 nesadapter
->free_256pbl
++;
884 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
888 memset(nesqp
->pbl_vbase
, 0, 256);
889 /* fill in the page address in the pbl buffer.. */
890 tpbl
= pblbuffer
+ 16;
891 pbl
= (__le64
*)nespbl
->pbl_vbase
;
892 while (sq_pbl_entries
--)
895 while (rq_pbl_entries
--)
898 /* done with memory allocated during nes_reg_user_mr() */
899 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
904 max((u32
)sizeof(struct nes_qp_context
), ((u32
)256)) + 256; /* this is Q2 */
905 /* Round up to a multiple of a page */
906 nesqp
->qp_mem_size
+= PAGE_SIZE
- 1;
907 nesqp
->qp_mem_size
&= ~(PAGE_SIZE
- 1);
909 mem
= pci_alloc_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
910 &nesqp
->hwqp
.q2_pbase
);
913 pci_free_consistent(nesdev
->pcidev
, 256, nesqp
->pbl_vbase
, nesqp
->pbl_pbase
);
914 nesqp
->pbl_vbase
= NULL
;
915 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
916 nesadapter
->free_256pbl
++;
917 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
921 nesqp
->sq_kmapped
= 1;
922 nesqp
->hwqp
.q2_vbase
= mem
;
924 memset(nesqp
->hwqp
.q2_vbase
, 0, 256);
925 nesqp
->nesqp_context
= mem
;
926 memset(nesqp
->nesqp_context
, 0, sizeof(*nesqp
->nesqp_context
));
927 nesqp
->nesqp_context_pbase
= nesqp
->hwqp
.q2_pbase
+ 256;
936 static int nes_setup_mmap_qp(struct nes_qp
*nesqp
, struct nes_vnic
*nesvnic
,
937 int sq_size
, int rq_size
)
940 struct nes_device
*nesdev
= nesvnic
->nesdev
;
942 nesqp
->qp_mem_size
= (sizeof(struct nes_hw_qp_wqe
) * sq_size
) +
943 (sizeof(struct nes_hw_qp_wqe
) * rq_size
) +
944 max((u32
)sizeof(struct nes_qp_context
), ((u32
)256)) +
945 256; /* this is Q2 */
946 /* Round up to a multiple of a page */
947 nesqp
->qp_mem_size
+= PAGE_SIZE
- 1;
948 nesqp
->qp_mem_size
&= ~(PAGE_SIZE
- 1);
950 mem
= pci_alloc_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
951 &nesqp
->hwqp
.sq_pbase
);
954 nes_debug(NES_DBG_QP
, "PCI consistent memory for "
955 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
956 mem
, (unsigned long)nesqp
->hwqp
.sq_pbase
, nesqp
->qp_mem_size
);
958 memset(mem
, 0, nesqp
->qp_mem_size
);
960 nesqp
->hwqp
.sq_vbase
= mem
;
961 mem
+= sizeof(struct nes_hw_qp_wqe
) * sq_size
;
963 nesqp
->hwqp
.rq_vbase
= mem
;
964 nesqp
->hwqp
.rq_pbase
= nesqp
->hwqp
.sq_pbase
+
965 sizeof(struct nes_hw_qp_wqe
) * sq_size
;
966 mem
+= sizeof(struct nes_hw_qp_wqe
) * rq_size
;
968 nesqp
->hwqp
.q2_vbase
= mem
;
969 nesqp
->hwqp
.q2_pbase
= nesqp
->hwqp
.rq_pbase
+
970 sizeof(struct nes_hw_qp_wqe
) * rq_size
;
972 memset(nesqp
->hwqp
.q2_vbase
, 0, 256);
974 nesqp
->nesqp_context
= mem
;
975 nesqp
->nesqp_context_pbase
= nesqp
->hwqp
.q2_pbase
+ 256;
976 memset(nesqp
->nesqp_context
, 0, sizeof(*nesqp
->nesqp_context
));
982 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
984 static inline void nes_free_qp_mem(struct nes_device
*nesdev
,
985 struct nes_qp
*nesqp
, int virt_wqs
)
988 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
990 pci_free_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
,
991 nesqp
->hwqp
.sq_vbase
, nesqp
->hwqp
.sq_pbase
);
993 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
994 nesadapter
->free_256pbl
++;
995 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
996 pci_free_consistent(nesdev
->pcidev
, nesqp
->qp_mem_size
, nesqp
->hwqp
.q2_vbase
, nesqp
->hwqp
.q2_pbase
);
997 pci_free_consistent(nesdev
->pcidev
, 256, nesqp
->pbl_vbase
, nesqp
->pbl_pbase
);
998 nesqp
->pbl_vbase
= NULL
;
999 if (nesqp
->sq_kmapped
) {
1000 nesqp
->sq_kmapped
= 0;
1001 kunmap(nesqp
->page
);
1010 static struct ib_qp
*nes_create_qp(struct ib_pd
*ibpd
,
1011 struct ib_qp_init_attr
*init_attr
, struct ib_udata
*udata
)
1015 struct nes_pd
*nespd
= to_nespd(ibpd
);
1016 struct nes_vnic
*nesvnic
= to_nesvnic(ibpd
->device
);
1017 struct nes_device
*nesdev
= nesvnic
->nesdev
;
1018 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1019 struct nes_qp
*nesqp
;
1020 struct nes_cq
*nescq
;
1021 struct nes_ucontext
*nes_ucontext
;
1022 struct nes_hw_cqp_wqe
*cqp_wqe
;
1023 struct nes_cqp_request
*cqp_request
;
1024 struct nes_create_qp_req req
;
1025 struct nes_create_qp_resp uresp
;
1026 struct nes_pbl
*nespbl
= NULL
;
1029 /* u32 counter = 0; */
1031 unsigned long flags
;
1041 if (init_attr
->create_flags
)
1042 return ERR_PTR(-EINVAL
);
1044 atomic_inc(&qps_created
);
1045 switch (init_attr
->qp_type
) {
1047 if (nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) {
1048 init_attr
->cap
.max_inline_data
= 0;
1050 init_attr
->cap
.max_inline_data
= 64;
1052 sq_size
= init_attr
->cap
.max_send_wr
;
1053 rq_size
= init_attr
->cap
.max_recv_wr
;
1055 /* check if the encoded sizes are OK or not... */
1056 sq_encoded_size
= nes_get_encoded_size(&sq_size
);
1057 rq_encoded_size
= nes_get_encoded_size(&rq_size
);
1059 if ((!sq_encoded_size
) || (!rq_encoded_size
)) {
1060 nes_debug(NES_DBG_QP
, "ERROR bad rq (%u) or sq (%u) size\n",
1062 return ERR_PTR(-EINVAL
);
1065 init_attr
->cap
.max_send_wr
= sq_size
-2;
1066 init_attr
->cap
.max_recv_wr
= rq_size
-1;
1067 nes_debug(NES_DBG_QP
, "RQ size=%u, SQ Size=%u\n", rq_size
, sq_size
);
1069 ret
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_qps
,
1070 nesadapter
->max_qp
, &qp_num
, &nesadapter
->next_qp
, NES_RESOURCE_QP
);
1072 return ERR_PTR(ret
);
1075 /* Need 512 (actually now 1024) byte alignment on this structure */
1076 mem
= kzalloc(sizeof(*nesqp
)+NES_SW_CONTEXT_ALIGN
-1, GFP_KERNEL
);
1078 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1079 nes_debug(NES_DBG_QP
, "Unable to allocate QP\n");
1080 return ERR_PTR(-ENOMEM
);
1082 u64nesqp
= (unsigned long)mem
;
1083 u64nesqp
+= ((u64
)NES_SW_CONTEXT_ALIGN
) - 1;
1084 u64temp
= ((u64
)NES_SW_CONTEXT_ALIGN
) - 1;
1085 u64nesqp
&= ~u64temp
;
1086 nesqp
= (struct nes_qp
*)(unsigned long)u64nesqp
;
1087 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1088 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1089 nesqp
->allocated_buffer
= mem
;
1092 if (ib_copy_from_udata(&req
, udata
, sizeof(struct nes_create_qp_req
))) {
1093 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1094 kfree(nesqp
->allocated_buffer
);
1095 nes_debug(NES_DBG_QP
, "ib_copy_from_udata() Failed \n");
1096 return ERR_PTR(-EFAULT
);
1098 if (req
.user_wqe_buffers
) {
1101 if (req
.user_qp_buffer
)
1102 nesqp
->nesuqp_addr
= req
.user_qp_buffer
;
1103 if ((ibpd
->uobject
) && (ibpd
->uobject
->context
)) {
1104 nesqp
->user_mode
= 1;
1105 nes_ucontext
= to_nesucontext(ibpd
->uobject
->context
);
1108 list_for_each_entry(nespbl
, &nes_ucontext
->qp_reg_mem_list
, list
) {
1109 if (nespbl
->user_base
== (unsigned long )req
.user_wqe_buffers
) {
1110 list_del(&nespbl
->list
);
1112 nes_debug(NES_DBG_QP
, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1113 nespbl
, nespbl
->user_base
);
1118 nes_debug(NES_DBG_QP
, "Didn't Find PBL for virtual QP. address = %llx.\n",
1119 (long long unsigned int)req
.user_wqe_buffers
);
1120 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1121 kfree(nesqp
->allocated_buffer
);
1122 return ERR_PTR(-EFAULT
);
1126 nes_ucontext
= to_nesucontext(ibpd
->uobject
->context
);
1127 nesqp
->mmap_sq_db_index
=
1128 find_next_zero_bit(nes_ucontext
->allocated_wqs
,
1129 NES_MAX_USER_WQ_REGIONS
, nes_ucontext
->first_free_wq
);
1130 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1131 nespd->mmap_db_index); */
1132 if (nesqp
->mmap_sq_db_index
>= NES_MAX_USER_WQ_REGIONS
) {
1133 nes_debug(NES_DBG_QP
,
1134 "db index > max user regions, failing create QP\n");
1135 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1137 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
1141 kfree(nesqp
->allocated_buffer
);
1142 return ERR_PTR(-ENOMEM
);
1144 set_bit(nesqp
->mmap_sq_db_index
, nes_ucontext
->allocated_wqs
);
1145 nes_ucontext
->mmap_nesqp
[nesqp
->mmap_sq_db_index
] = nesqp
;
1146 nes_ucontext
->first_free_wq
= nesqp
->mmap_sq_db_index
+ 1;
1148 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1149 kfree(nesqp
->allocated_buffer
);
1150 return ERR_PTR(-EFAULT
);
1153 err
= (!virt_wqs
) ? nes_setup_mmap_qp(nesqp
, nesvnic
, sq_size
, rq_size
) :
1154 nes_setup_virt_qp(nesqp
, nespbl
, nesvnic
, sq_size
, rq_size
);
1156 nes_debug(NES_DBG_QP
,
1157 "error geting qp mem code = %d\n", err
);
1158 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1159 kfree(nesqp
->allocated_buffer
);
1160 return ERR_PTR(-ENOMEM
);
1163 nesqp
->hwqp
.sq_size
= sq_size
;
1164 nesqp
->hwqp
.sq_encoded_size
= sq_encoded_size
;
1165 nesqp
->hwqp
.sq_head
= 1;
1166 nesqp
->hwqp
.rq_size
= rq_size
;
1167 nesqp
->hwqp
.rq_encoded_size
= rq_encoded_size
;
1168 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1169 (void *)nesqp->nesqp_context_pbase);
1171 nesqp
->hwqp
.qp_id
= qp_num
;
1172 nesqp
->ibqp
.qp_num
= nesqp
->hwqp
.qp_id
;
1173 nesqp
->nespd
= nespd
;
1175 nescq
= to_nescq(init_attr
->send_cq
);
1176 nesqp
->nesscq
= nescq
;
1177 nescq
= to_nescq(init_attr
->recv_cq
);
1178 nesqp
->nesrcq
= nescq
;
1180 nesqp
->nesqp_context
->misc
|= cpu_to_le32((u32
)PCI_FUNC(nesdev
->pcidev
->devfn
) <<
1181 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT
);
1182 nesqp
->nesqp_context
->misc
|= cpu_to_le32((u32
)nesqp
->hwqp
.rq_encoded_size
<<
1183 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT
);
1184 nesqp
->nesqp_context
->misc
|= cpu_to_le32((u32
)nesqp
->hwqp
.sq_encoded_size
<<
1185 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT
);
1187 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN
);
1188 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN
);
1190 nesqp
->nesqp_context
->cqs
= cpu_to_le32(nesqp
->nesscq
->hw_cq
.cq_number
+
1191 ((u32
)nesqp
->nesrcq
->hw_cq
.cq_number
<< 16));
1192 u64temp
= (u64
)nesqp
->hwqp
.sq_pbase
;
1193 nesqp
->nesqp_context
->sq_addr_low
= cpu_to_le32((u32
)u64temp
);
1194 nesqp
->nesqp_context
->sq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1198 u64temp
= (u64
)nesqp
->hwqp
.sq_pbase
;
1199 nesqp
->nesqp_context
->sq_addr_low
= cpu_to_le32((u32
)u64temp
);
1200 nesqp
->nesqp_context
->sq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1201 u64temp
= (u64
)nesqp
->hwqp
.rq_pbase
;
1202 nesqp
->nesqp_context
->rq_addr_low
= cpu_to_le32((u32
)u64temp
);
1203 nesqp
->nesqp_context
->rq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1205 u64temp
= (u64
)nesqp
->pbl_pbase
;
1206 nesqp
->nesqp_context
->rq_addr_low
= cpu_to_le32((u32
)u64temp
);
1207 nesqp
->nesqp_context
->rq_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1210 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1211 nesvnic->next_qp_nic_index,
1212 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1213 spin_lock_irqsave(&nesdev
->cqp
.lock
, flags
);
1214 nesqp
->nesqp_context
->misc2
|= cpu_to_le32(
1215 (u32
)nesvnic
->qp_nic_index
[nesvnic
->next_qp_nic_index
] <<
1216 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT
);
1217 nesvnic
->next_qp_nic_index
++;
1218 if ((nesvnic
->next_qp_nic_index
> 3) ||
1219 (nesvnic
->qp_nic_index
[nesvnic
->next_qp_nic_index
] == 0xf)) {
1220 nesvnic
->next_qp_nic_index
= 0;
1222 spin_unlock_irqrestore(&nesdev
->cqp
.lock
, flags
);
1224 nesqp
->nesqp_context
->pd_index_wscale
|= cpu_to_le32((u32
)nesqp
->nespd
->pd_id
<< 16);
1225 u64temp
= (u64
)nesqp
->hwqp
.q2_pbase
;
1226 nesqp
->nesqp_context
->q2_addr_low
= cpu_to_le32((u32
)u64temp
);
1227 nesqp
->nesqp_context
->q2_addr_high
= cpu_to_le32((u32
)(u64temp
>> 32));
1228 nesqp
->nesqp_context
->aeq_token_low
= cpu_to_le32((u32
)((unsigned long)(nesqp
)));
1229 nesqp
->nesqp_context
->aeq_token_high
= cpu_to_le32((u32
)(upper_32_bits((unsigned long)(nesqp
))));
1230 nesqp
->nesqp_context
->ird_ord_sizes
= cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM
|
1231 NES_QPCONTEXT_ORDIRD_AAH
|
1232 ((((u32
)nesadapter
->max_irrq_wr
) <<
1233 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT
) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK
));
1234 if (disable_mpa_crc
) {
1235 nes_debug(NES_DBG_QP
, "Disabling MPA crc checking due to module option.\n");
1236 nesqp
->nesqp_context
->ird_ord_sizes
|= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC
);
1241 cqp_request
= nes_get_cqp_request(nesdev
);
1242 if (cqp_request
== NULL
) {
1243 nes_debug(NES_DBG_QP
, "Failed to get a cqp_request\n");
1244 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1245 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1246 kfree(nesqp
->allocated_buffer
);
1247 return ERR_PTR(-ENOMEM
);
1249 cqp_request
->waiting
= 1;
1250 cqp_wqe
= &cqp_request
->cqp_wqe
;
1253 opcode
= NES_CQP_CREATE_QP
| NES_CQP_QP_TYPE_IWARP
|
1254 NES_CQP_QP_IWARP_STATE_IDLE
;
1256 opcode
= NES_CQP_CREATE_QP
| NES_CQP_QP_TYPE_IWARP
| NES_CQP_QP_VIRT_WQS
|
1257 NES_CQP_QP_IWARP_STATE_IDLE
;
1259 opcode
|= NES_CQP_QP_CQS_VALID
;
1260 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1261 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1262 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
, nesqp
->hwqp
.qp_id
);
1264 u64temp
= (u64
)nesqp
->nesqp_context_pbase
;
1265 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_CONTEXT_LOW_IDX
, u64temp
);
1267 atomic_set(&cqp_request
->refcount
, 2);
1268 nes_post_cqp_request(nesdev
, cqp_request
);
1271 nes_debug(NES_DBG_QP
, "Waiting for create iWARP QP%u to complete.\n",
1273 ret
= wait_event_timeout(cqp_request
->waitq
,
1274 (cqp_request
->request_done
!= 0), NES_EVENT_TIMEOUT
);
1275 nes_debug(NES_DBG_QP
, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1276 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1277 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1278 nesqp
->hwqp
.qp_id
, ret
, nesdev
->cqp
.sq_head
, nesdev
->cqp
.sq_tail
,
1279 cqp_request
->major_code
, cqp_request
->minor_code
);
1280 if ((!ret
) || (cqp_request
->major_code
)) {
1281 nes_put_cqp_request(nesdev
, cqp_request
);
1282 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1283 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1284 kfree(nesqp
->allocated_buffer
);
1286 return ERR_PTR(-ETIME
);
1288 return ERR_PTR(-EIO
);
1292 nes_put_cqp_request(nesdev
, cqp_request
);
1294 if (ibpd
->uobject
) {
1295 uresp
.mmap_sq_db_index
= nesqp
->mmap_sq_db_index
;
1296 uresp
.mmap_rq_db_index
= 0;
1297 uresp
.actual_sq_size
= sq_size
;
1298 uresp
.actual_rq_size
= rq_size
;
1299 uresp
.qp_id
= nesqp
->hwqp
.qp_id
;
1300 uresp
.nes_drv_opt
= nes_drv_opt
;
1301 if (ib_copy_to_udata(udata
, &uresp
, sizeof uresp
)) {
1302 nes_free_resource(nesadapter
, nesadapter
->allocated_qps
, qp_num
);
1303 nes_free_qp_mem(nesdev
, nesqp
,virt_wqs
);
1304 kfree(nesqp
->allocated_buffer
);
1305 return ERR_PTR(-EFAULT
);
1309 nes_debug(NES_DBG_QP
, "QP%u structure located @%p.Size = %u.\n",
1310 nesqp
->hwqp
.qp_id
, nesqp
, (u32
)sizeof(*nesqp
));
1311 spin_lock_init(&nesqp
->lock
);
1312 nes_add_ref(&nesqp
->ibqp
);
1315 nes_debug(NES_DBG_QP
, "Invalid QP type: %d\n", init_attr
->qp_type
);
1316 return ERR_PTR(-EINVAL
);
1319 nesqp
->sig_all
= (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
);
1320 init_timer(&nesqp
->terminate_timer
);
1321 nesqp
->terminate_timer
.function
= nes_terminate_timeout
;
1322 nesqp
->terminate_timer
.data
= (unsigned long)nesqp
;
1324 /* update the QP table */
1325 nesdev
->nesadapter
->qp_table
[nesqp
->hwqp
.qp_id
-NES_FIRST_QPN
] = nesqp
;
1326 nes_debug(NES_DBG_QP
, "netdev refcnt=%u\n",
1327 netdev_refcnt_read(nesvnic
->netdev
));
1329 return &nesqp
->ibqp
;
1335 static void nes_clean_cq(struct nes_qp
*nesqp
, struct nes_cq
*nescq
)
1341 unsigned long flags
= 0;
1343 spin_lock_irqsave(&nescq
->lock
, flags
);
1345 cq_head
= nescq
->hw_cq
.cq_head
;
1346 while (le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_OPCODE_IDX
]) & NES_CQE_VALID
) {
1348 lo
= le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
1349 hi
= le32_to_cpu(nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
]);
1350 u64temp
= (((u64
)hi
) << 32) | ((u64
)lo
);
1351 u64temp
&= ~(NES_SW_CONTEXT_ALIGN
-1);
1352 if (u64temp
== (u64
)(unsigned long)nesqp
) {
1353 /* Zero the context value so cqe will be ignored */
1354 nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
] = 0;
1355 nescq
->hw_cq
.cq_vbase
[cq_head
].cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
] = 0;
1358 if (++cq_head
>= nescq
->hw_cq
.cq_size
)
1362 spin_unlock_irqrestore(&nescq
->lock
, flags
);
1369 static int nes_destroy_qp(struct ib_qp
*ibqp
)
1371 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
1372 struct nes_ucontext
*nes_ucontext
;
1373 struct ib_qp_attr attr
;
1374 struct iw_cm_id
*cm_id
;
1375 struct iw_cm_event cm_event
;
1378 atomic_inc(&sw_qps_destroyed
);
1379 nesqp
->destroyed
= 1;
1381 /* Blow away the connection if it exists. */
1382 if (nesqp
->ibqp_state
>= IB_QPS_INIT
&& nesqp
->ibqp_state
<= IB_QPS_RTS
) {
1383 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1384 attr
.qp_state
= IB_QPS_ERR
;
1385 nes_modify_qp(&nesqp
->ibqp
, &attr
, IB_QP_STATE
, NULL
);
1388 if (((nesqp
->ibqp_state
== IB_QPS_INIT
) ||
1389 (nesqp
->ibqp_state
== IB_QPS_RTR
)) && (nesqp
->cm_id
)) {
1390 cm_id
= nesqp
->cm_id
;
1391 cm_event
.event
= IW_CM_EVENT_CONNECT_REPLY
;
1392 cm_event
.status
= -ETIMEDOUT
;
1393 cm_event
.local_addr
= cm_id
->local_addr
;
1394 cm_event
.remote_addr
= cm_id
->remote_addr
;
1395 cm_event
.private_data
= NULL
;
1396 cm_event
.private_data_len
= 0;
1398 nes_debug(NES_DBG_QP
, "Generating a CM Timeout Event for "
1399 "QP%u. cm_id = %p, refcount = %u. \n",
1400 nesqp
->hwqp
.qp_id
, cm_id
, atomic_read(&nesqp
->refcount
));
1402 cm_id
->rem_ref(cm_id
);
1403 ret
= cm_id
->event_handler(cm_id
, &cm_event
);
1405 nes_debug(NES_DBG_QP
, "OFA CM event_handler returned, ret=%d\n", ret
);
1408 if (nesqp
->user_mode
) {
1409 if ((ibqp
->uobject
)&&(ibqp
->uobject
->context
)) {
1410 nes_ucontext
= to_nesucontext(ibqp
->uobject
->context
);
1411 clear_bit(nesqp
->mmap_sq_db_index
, nes_ucontext
->allocated_wqs
);
1412 nes_ucontext
->mmap_nesqp
[nesqp
->mmap_sq_db_index
] = NULL
;
1413 if (nes_ucontext
->first_free_wq
> nesqp
->mmap_sq_db_index
) {
1414 nes_ucontext
->first_free_wq
= nesqp
->mmap_sq_db_index
;
1417 if (nesqp
->pbl_pbase
&& nesqp
->sq_kmapped
) {
1418 nesqp
->sq_kmapped
= 0;
1419 kunmap(nesqp
->page
);
1422 /* Clean any pending completions from the cq(s) */
1424 nes_clean_cq(nesqp
, nesqp
->nesscq
);
1426 if ((nesqp
->nesrcq
) && (nesqp
->nesrcq
!= nesqp
->nesscq
))
1427 nes_clean_cq(nesqp
, nesqp
->nesrcq
);
1429 nes_rem_ref(&nesqp
->ibqp
);
1437 static struct ib_cq
*nes_create_cq(struct ib_device
*ibdev
,
1438 const struct ib_cq_init_attr
*attr
,
1439 struct ib_ucontext
*context
,
1440 struct ib_udata
*udata
)
1442 int entries
= attr
->cqe
;
1444 struct nes_vnic
*nesvnic
= to_nesvnic(ibdev
);
1445 struct nes_device
*nesdev
= nesvnic
->nesdev
;
1446 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1447 struct nes_cq
*nescq
;
1448 struct nes_ucontext
*nes_ucontext
= NULL
;
1449 struct nes_cqp_request
*cqp_request
;
1451 struct nes_hw_cqp_wqe
*cqp_wqe
;
1452 struct nes_pbl
*nespbl
= NULL
;
1453 struct nes_create_cq_req req
;
1454 struct nes_create_cq_resp resp
;
1457 u32 pbl_entries
= 1;
1459 unsigned long flags
;
1463 return ERR_PTR(-EINVAL
);
1465 if (entries
> nesadapter
->max_cqe
)
1466 return ERR_PTR(-EINVAL
);
1468 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_cqs
,
1469 nesadapter
->max_cq
, &cq_num
, &nesadapter
->next_cq
, NES_RESOURCE_CQ
);
1471 return ERR_PTR(err
);
1474 nescq
= kzalloc(sizeof(struct nes_cq
), GFP_KERNEL
);
1476 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1477 nes_debug(NES_DBG_CQ
, "Unable to allocate nes_cq struct\n");
1478 return ERR_PTR(-ENOMEM
);
1481 nescq
->hw_cq
.cq_size
= max(entries
+ 1, 5);
1482 nescq
->hw_cq
.cq_number
= cq_num
;
1483 nescq
->ibcq
.cqe
= nescq
->hw_cq
.cq_size
- 1;
1487 nes_ucontext
= to_nesucontext(context
);
1488 if (ib_copy_from_udata(&req
, udata
, sizeof (struct nes_create_cq_req
))) {
1489 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1491 return ERR_PTR(-EFAULT
);
1493 nesvnic
->mcrq_ucontext
= nes_ucontext
;
1494 nes_ucontext
->mcrqf
= req
.mcrqf
;
1495 if (nes_ucontext
->mcrqf
) {
1496 if (nes_ucontext
->mcrqf
& 0x80000000)
1497 nescq
->hw_cq
.cq_number
= nesvnic
->nic
.qp_id
+ 28 + 2 * ((nes_ucontext
->mcrqf
& 0xf) - 1);
1498 else if (nes_ucontext
->mcrqf
& 0x40000000)
1499 nescq
->hw_cq
.cq_number
= nes_ucontext
->mcrqf
& 0xffff;
1501 nescq
->hw_cq
.cq_number
= nesvnic
->mcrq_qp_id
+ nes_ucontext
->mcrqf
-1;
1502 nescq
->mcrqf
= nes_ucontext
->mcrqf
;
1503 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1505 nes_debug(NES_DBG_CQ
, "CQ Virtual Address = %08lX, size = %u.\n",
1506 (unsigned long)req
.user_cq_buffer
, entries
);
1508 list_for_each_entry(nespbl
, &nes_ucontext
->cq_reg_mem_list
, list
) {
1509 if (nespbl
->user_base
== (unsigned long )req
.user_cq_buffer
) {
1510 list_del(&nespbl
->list
);
1512 nes_debug(NES_DBG_CQ
, "Found PBL for virtual CQ. nespbl=%p.\n",
1518 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1520 return ERR_PTR(-EFAULT
);
1523 pbl_entries
= nespbl
->pbl_size
>> 3;
1524 nescq
->cq_mem_size
= 0;
1526 nescq
->cq_mem_size
= nescq
->hw_cq
.cq_size
* sizeof(struct nes_hw_cqe
);
1527 nes_debug(NES_DBG_CQ
, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1528 entries
, nescq
->cq_mem_size
, nescq
->hw_cq
.cq_number
);
1530 /* allocate the physical buffer space */
1531 mem
= pci_zalloc_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
,
1532 &nescq
->hw_cq
.cq_pbase
);
1534 printk(KERN_ERR PFX
"Unable to allocate pci memory for cq\n");
1535 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1537 return ERR_PTR(-ENOMEM
);
1540 nescq
->hw_cq
.cq_vbase
= mem
;
1541 nescq
->hw_cq
.cq_head
= 0;
1542 nes_debug(NES_DBG_CQ
, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1543 nescq
->hw_cq
.cq_number
, nescq
->hw_cq
.cq_vbase
,
1544 (u32
)nescq
->hw_cq
.cq_pbase
);
1547 nescq
->hw_cq
.ce_handler
= nes_iwarp_ce_handler
;
1548 spin_lock_init(&nescq
->lock
);
1550 /* send CreateCQ request to CQP */
1551 cqp_request
= nes_get_cqp_request(nesdev
);
1552 if (cqp_request
== NULL
) {
1553 nes_debug(NES_DBG_CQ
, "Failed to get a cqp_request.\n");
1555 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1556 nescq
->hw_cq
.cq_pbase
);
1558 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1559 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1563 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1565 return ERR_PTR(-ENOMEM
);
1567 cqp_request
->waiting
= 1;
1568 cqp_wqe
= &cqp_request
->cqp_wqe
;
1570 opcode
= NES_CQP_CREATE_CQ
| NES_CQP_CQ_CEQ_VALID
|
1571 NES_CQP_CQ_CHK_OVERFLOW
|
1572 NES_CQP_CQ_CEQE_MASK
| ((u32
)nescq
->hw_cq
.cq_size
<< 16);
1574 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1576 if (pbl_entries
!= 1) {
1577 if (pbl_entries
> 32) {
1579 nes_debug(NES_DBG_CQ
, "pbl_entries=%u, use a 4k PBL\n", pbl_entries
);
1580 if (nesadapter
->free_4kpbl
== 0) {
1581 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1582 nes_free_cqp_request(nesdev
, cqp_request
);
1584 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1585 nescq
->hw_cq
.cq_pbase
);
1587 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1588 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1591 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1593 return ERR_PTR(-ENOMEM
);
1595 opcode
|= (NES_CQP_CQ_VIRT
| NES_CQP_CQ_4KB_CHUNK
);
1596 nescq
->virtual_cq
= 2;
1597 nesadapter
->free_4kpbl
--;
1600 /* use 256 byte pbl */
1601 nes_debug(NES_DBG_CQ
, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries
);
1602 if (nesadapter
->free_256pbl
== 0) {
1603 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1604 nes_free_cqp_request(nesdev
, cqp_request
);
1606 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1607 nescq
->hw_cq
.cq_pbase
);
1609 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1610 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1613 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1615 return ERR_PTR(-ENOMEM
);
1617 opcode
|= NES_CQP_CQ_VIRT
;
1618 nescq
->virtual_cq
= 1;
1619 nesadapter
->free_256pbl
--;
1624 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1626 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1627 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1628 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
,
1629 (nescq
->hw_cq
.cq_number
| ((u32
)nesdev
->ceq_index
<< 16)));
1632 if (pbl_entries
!= 1)
1633 u64temp
= (u64
)nespbl
->pbl_pbase
;
1635 u64temp
= le64_to_cpu(nespbl
->pbl_vbase
[0]);
1636 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX
,
1637 nes_ucontext
->mmap_db_index
[0]);
1639 u64temp
= (u64
)nescq
->hw_cq
.cq_pbase
;
1640 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX
] = 0;
1642 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_CQ_WQE_PBL_LOW_IDX
, u64temp
);
1643 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX
] = 0;
1644 u64temp
= (u64
)(unsigned long)&nescq
->hw_cq
;
1645 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX
] =
1646 cpu_to_le32((u32
)(u64temp
>> 1));
1647 cqp_wqe
->wqe_words
[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX
] =
1648 cpu_to_le32(((u32
)((u64temp
) >> 33)) & 0x7FFFFFFF);
1650 atomic_set(&cqp_request
->refcount
, 2);
1651 nes_post_cqp_request(nesdev
, cqp_request
);
1654 nes_debug(NES_DBG_CQ
, "Waiting for create iWARP CQ%u to complete.\n",
1655 nescq
->hw_cq
.cq_number
);
1656 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1657 NES_EVENT_TIMEOUT
* 2);
1658 nes_debug(NES_DBG_CQ
, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1659 nescq
->hw_cq
.cq_number
, ret
);
1660 if ((!ret
) || (cqp_request
->major_code
)) {
1661 nes_put_cqp_request(nesdev
, cqp_request
);
1663 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
, mem
,
1664 nescq
->hw_cq
.cq_pbase
);
1666 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
1667 nespbl
->pbl_vbase
, nespbl
->pbl_pbase
);
1670 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1672 return ERR_PTR(-EIO
);
1674 nes_put_cqp_request(nesdev
, cqp_request
);
1677 /* free the nespbl */
1678 pci_free_consistent(nesdev
->pcidev
, nespbl
->pbl_size
, nespbl
->pbl_vbase
,
1681 resp
.cq_id
= nescq
->hw_cq
.cq_number
;
1682 resp
.cq_size
= nescq
->hw_cq
.cq_size
;
1683 resp
.mmap_db_index
= 0;
1684 if (ib_copy_to_udata(udata
, &resp
, sizeof resp
- sizeof resp
.reserved
)) {
1685 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, cq_num
);
1687 return ERR_PTR(-EFAULT
);
1691 return &nescq
->ibcq
;
1698 static int nes_destroy_cq(struct ib_cq
*ib_cq
)
1700 struct nes_cq
*nescq
;
1701 struct nes_device
*nesdev
;
1702 struct nes_vnic
*nesvnic
;
1703 struct nes_adapter
*nesadapter
;
1704 struct nes_hw_cqp_wqe
*cqp_wqe
;
1705 struct nes_cqp_request
*cqp_request
;
1706 unsigned long flags
;
1713 nescq
= to_nescq(ib_cq
);
1714 nesvnic
= to_nesvnic(ib_cq
->device
);
1715 nesdev
= nesvnic
->nesdev
;
1716 nesadapter
= nesdev
->nesadapter
;
1718 nes_debug(NES_DBG_CQ
, "Destroy CQ%u\n", nescq
->hw_cq
.cq_number
);
1720 /* Send DestroyCQ request to CQP */
1721 cqp_request
= nes_get_cqp_request(nesdev
);
1722 if (cqp_request
== NULL
) {
1723 nes_debug(NES_DBG_CQ
, "Failed to get a cqp_request.\n");
1726 cqp_request
->waiting
= 1;
1727 cqp_wqe
= &cqp_request
->cqp_wqe
;
1728 opcode
= NES_CQP_DESTROY_CQ
| (nescq
->hw_cq
.cq_size
<< 16);
1729 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1730 if (nescq
->virtual_cq
== 1) {
1731 nesadapter
->free_256pbl
++;
1732 if (nesadapter
->free_256pbl
> nesadapter
->max_256pbl
) {
1733 printk(KERN_ERR PFX
"%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1734 __func__
, nesadapter
->free_256pbl
, nesadapter
->max_256pbl
);
1736 } else if (nescq
->virtual_cq
== 2) {
1737 nesadapter
->free_4kpbl
++;
1738 if (nesadapter
->free_4kpbl
> nesadapter
->max_4kpbl
) {
1739 printk(KERN_ERR PFX
"%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1740 __func__
, nesadapter
->free_4kpbl
, nesadapter
->max_4kpbl
);
1742 opcode
|= NES_CQP_CQ_4KB_CHUNK
;
1745 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1747 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1748 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1749 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
,
1750 (nescq
->hw_cq
.cq_number
| ((u32
)PCI_FUNC(nesdev
->pcidev
->devfn
) << 16)));
1752 nes_free_resource(nesadapter
, nesadapter
->allocated_cqs
, nescq
->hw_cq
.cq_number
);
1754 atomic_set(&cqp_request
->refcount
, 2);
1755 nes_post_cqp_request(nesdev
, cqp_request
);
1758 nes_debug(NES_DBG_CQ
, "Waiting for destroy iWARP CQ%u to complete.\n",
1759 nescq
->hw_cq
.cq_number
);
1760 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1762 nes_debug(NES_DBG_CQ
, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1763 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1764 nescq
->hw_cq
.cq_number
, ret
, cqp_request
->major_code
,
1765 cqp_request
->minor_code
);
1767 nes_debug(NES_DBG_CQ
, "iWARP CQ%u destroy timeout expired\n",
1768 nescq
->hw_cq
.cq_number
);
1770 } else if (cqp_request
->major_code
) {
1771 nes_debug(NES_DBG_CQ
, "iWARP CQ%u destroy failed\n",
1772 nescq
->hw_cq
.cq_number
);
1777 nes_put_cqp_request(nesdev
, cqp_request
);
1779 if (nescq
->cq_mem_size
)
1780 pci_free_consistent(nesdev
->pcidev
, nescq
->cq_mem_size
,
1781 nescq
->hw_cq
.cq_vbase
, nescq
->hw_cq
.cq_pbase
);
1790 static u32
root_256(struct nes_device
*nesdev
,
1791 struct nes_root_vpbl
*root_vpbl
,
1792 struct nes_root_vpbl
*new_root
,
1798 if (pbl_count_4k
== 1) {
1799 new_root
->pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
,
1800 512, &new_root
->pbl_pbase
);
1802 if (new_root
->pbl_vbase
== NULL
)
1805 leaf_pbl
= (u64
)root_vpbl
->pbl_pbase
;
1806 for (i
= 0; i
< 16; i
++) {
1807 new_root
->pbl_vbase
[i
].pa_low
=
1808 cpu_to_le32((u32
)leaf_pbl
);
1809 new_root
->pbl_vbase
[i
].pa_high
=
1810 cpu_to_le32((u32
)((((u64
)leaf_pbl
) >> 32)));
1814 for (i
= 3; i
>= 0; i
--) {
1816 root_vpbl
->pbl_vbase
[j
] = root_vpbl
->pbl_vbase
[i
];
1817 leaf_pbl
= le32_to_cpu(root_vpbl
->pbl_vbase
[j
].pa_low
) +
1818 (((u64
)le32_to_cpu(root_vpbl
->pbl_vbase
[j
].pa_high
))
1820 for (k
= 1; k
< 16; k
++) {
1822 root_vpbl
->pbl_vbase
[j
+ k
].pa_low
=
1823 cpu_to_le32((u32
)leaf_pbl
);
1824 root_vpbl
->pbl_vbase
[j
+ k
].pa_high
=
1825 cpu_to_le32((u32
)((((u64
)leaf_pbl
) >> 32)));
1837 static int nes_reg_mr(struct nes_device
*nesdev
, struct nes_pd
*nespd
,
1838 u32 stag
, u64 region_length
, struct nes_root_vpbl
*root_vpbl
,
1839 dma_addr_t single_buffer
, u16 pbl_count_4k
,
1840 u16 residual_page_count_4k
, int acc
, u64
*iova_start
,
1841 u16
*actual_pbl_cnt
, u8
*used_4k_pbls
)
1843 struct nes_hw_cqp_wqe
*cqp_wqe
;
1844 struct nes_cqp_request
*cqp_request
;
1845 unsigned long flags
;
1847 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
1849 u16 pbl_count_256
= 0;
1851 u8 use_256_pbls
= 0;
1853 u16 use_two_level
= (pbl_count_4k
> 1) ? 1 : 0;
1854 struct nes_root_vpbl new_root
= { 0, NULL
, NULL
};
1858 /* Register the region with the adapter */
1859 cqp_request
= nes_get_cqp_request(nesdev
);
1860 if (cqp_request
== NULL
) {
1861 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
1864 cqp_request
->waiting
= 1;
1865 cqp_wqe
= &cqp_request
->cqp_wqe
;
1868 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1870 pg_cnt
= ((pbl_count_4k
- 1) * 512) + residual_page_count_4k
;
1871 pbl_count_256
= (pg_cnt
+ 31) / 32;
1873 if (pbl_count_256
<= nesadapter
->free_256pbl
)
1875 else if (pbl_count_4k
<= nesadapter
->free_4kpbl
)
1877 } else if (pg_cnt
<= 2048) {
1878 if (((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) &&
1879 (nesadapter
->free_4kpbl
> (nesadapter
->max_4kpbl
>> 1))) {
1881 } else if ((pbl_count_256
+ 1) <= nesadapter
->free_256pbl
) {
1884 } else if ((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) {
1888 if ((pbl_count_4k
+ 1) <= nesadapter
->free_4kpbl
)
1893 pbl_count
= pbl_count_256
;
1894 nesadapter
->free_256pbl
-= pbl_count
+ use_two_level
;
1895 } else if (use_4k_pbls
) {
1896 pbl_count
= pbl_count_4k
;
1897 nesadapter
->free_4kpbl
-= pbl_count
+ use_two_level
;
1899 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1900 nes_debug(NES_DBG_MR
, "Out of Pbls\n");
1901 nes_free_cqp_request(nesdev
, cqp_request
);
1905 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1908 if (use_256_pbls
&& use_two_level
) {
1909 if (root_256(nesdev
, root_vpbl
, &new_root
, pbl_count_4k
) == 1) {
1910 if (new_root
.pbl_pbase
!= 0)
1911 root_vpbl
= &new_root
;
1913 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1914 nesadapter
->free_256pbl
+= pbl_count_256
+ use_two_level
;
1917 if (pbl_count_4k
== 1)
1919 pbl_count
= pbl_count_4k
;
1921 if ((pbl_count_4k
+ use_two_level
) <= nesadapter
->free_4kpbl
) {
1922 nesadapter
->free_4kpbl
-= pbl_count
+ use_two_level
;
1925 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1927 if (use_4k_pbls
== 0)
1932 opcode
= NES_CQP_REGISTER_STAG
| NES_CQP_STAG_RIGHTS_LOCAL_READ
|
1933 NES_CQP_STAG_VA_TO
| NES_CQP_STAG_MR
;
1934 if (acc
& IB_ACCESS_LOCAL_WRITE
)
1935 opcode
|= NES_CQP_STAG_RIGHTS_LOCAL_WRITE
;
1936 if (acc
& IB_ACCESS_REMOTE_WRITE
)
1937 opcode
|= NES_CQP_STAG_RIGHTS_REMOTE_WRITE
| NES_CQP_STAG_REM_ACC_EN
;
1938 if (acc
& IB_ACCESS_REMOTE_READ
)
1939 opcode
|= NES_CQP_STAG_RIGHTS_REMOTE_READ
| NES_CQP_STAG_REM_ACC_EN
;
1940 if (acc
& IB_ACCESS_MW_BIND
)
1941 opcode
|= NES_CQP_STAG_RIGHTS_WINDOW_BIND
| NES_CQP_STAG_REM_ACC_EN
;
1943 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
1944 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
, opcode
);
1945 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_VA_LOW_IDX
, *iova_start
);
1946 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_LEN_LOW_IDX
, region_length
);
1948 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] =
1949 cpu_to_le32((u32
)(region_length
>> 8) & 0xff000000);
1950 cqp_wqe
->wqe_words
[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX
] |=
1951 cpu_to_le32(nespd
->pd_id
& 0x00007fff);
1952 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, stag
);
1954 if (pbl_count
== 0) {
1955 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, single_buffer
);
1957 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PA_LOW_IDX
, root_vpbl
->pbl_pbase
);
1958 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX
, pbl_count
);
1959 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_PBL_LEN_IDX
, (pg_cnt
* 8));
1962 cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE
);
1966 atomic_set(&cqp_request
->refcount
, 2);
1967 nes_post_cqp_request(nesdev
, cqp_request
);
1970 ret
= wait_event_timeout(cqp_request
->waitq
, (0 != cqp_request
->request_done
),
1972 nes_debug(NES_DBG_MR
, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1973 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1974 stag
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
1975 major_code
= cqp_request
->major_code
;
1976 nes_put_cqp_request(nesdev
, cqp_request
);
1978 if ((!ret
|| major_code
) && pbl_count
!= 0) {
1979 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
1981 nesadapter
->free_256pbl
+= pbl_count
+ use_two_level
;
1982 else if (use_4k_pbls
)
1983 nesadapter
->free_4kpbl
+= pbl_count
+ use_two_level
;
1984 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
1986 if (new_root
.pbl_pbase
)
1987 pci_free_consistent(nesdev
->pcidev
, 512, new_root
.pbl_vbase
,
1988 new_root
.pbl_pbase
);
1992 else if (major_code
)
1995 *actual_pbl_cnt
= pbl_count
+ use_two_level
;
1996 *used_4k_pbls
= use_4k_pbls
;
2004 struct ib_mr
*nes_reg_phys_mr(struct ib_pd
*ib_pd
, u64 addr
, u64 size
,
2005 int acc
, u64
*iova_start
)
2008 struct nes_pd
*nespd
= to_nespd(ib_pd
);
2009 struct nes_vnic
*nesvnic
= to_nesvnic(ib_pd
->device
);
2010 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2011 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2012 struct nes_mr
*nesmr
;
2014 struct nes_vpbl vpbl
;
2015 struct nes_root_vpbl root_vpbl
;
2019 u32 next_stag_index
= 0;
2028 vpbl
.pbl_vbase
= NULL
;
2029 root_vpbl
.pbl_vbase
= NULL
;
2030 root_vpbl
.pbl_pbase
= 0;
2032 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
2033 stag_key
= (u8
)next_stag_index
;
2037 next_stag_index
>>= 8;
2038 next_stag_index
%= nesadapter
->max_mr
;
2040 if ((addr
^ *iova_start
) & ~PAGE_MASK
)
2041 return ERR_PTR(-EINVAL
);
2043 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
, nesadapter
->max_mr
,
2044 &stag_index
, &next_stag_index
, NES_RESOURCE_PHYS_MR
);
2046 return ERR_PTR(err
);
2049 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2051 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2052 return ERR_PTR(-ENOMEM
);
2055 /* Allocate a 4K buffer for the PBL */
2056 vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 4096,
2058 nes_debug(NES_DBG_MR
, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2059 vpbl
.pbl_vbase
, (unsigned long)vpbl
.pbl_pbase
);
2060 if (!vpbl
.pbl_vbase
) {
2061 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2062 ibmr
= ERR_PTR(-ENOMEM
);
2070 if (mask
& ~PAGE_MASK
) {
2071 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2072 nes_debug(NES_DBG_MR
, "Invalid buffer addr or size\n");
2073 ibmr
= ERR_PTR(-EINVAL
);
2078 region_length
+= size
;
2079 vpbl
.pbl_vbase
[0].pa_low
= cpu_to_le32((u32
)addr
& PAGE_MASK
);
2080 vpbl
.pbl_vbase
[0].pa_high
= cpu_to_le32((u32
)((((u64
)addr
) >> 32)));
2082 stag
= stag_index
<< 8;
2084 stag
+= (u32
)stag_key
;
2086 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, VA = 0x%016lX,"
2087 " length = 0x%016lX, index = 0x%08X\n",
2088 stag
, (unsigned long)*iova_start
, (unsigned long)region_length
, stag_index
);
2090 /* Make the leaf PBL the root if only one PBL */
2091 root_vpbl
.pbl_pbase
= vpbl
.pbl_pbase
;
2098 ret
= nes_reg_mr(nesdev
, nespd
, stag
, region_length
, &root_vpbl
,
2099 addr
, pbl_count
, 1, acc
, iova_start
,
2100 &nesmr
->pbls_used
, &nesmr
->pbl_4k
);
2103 nesmr
->ibmr
.rkey
= stag
;
2104 nesmr
->ibmr
.lkey
= stag
;
2105 nesmr
->mode
= IWNES_MEMREG_TYPE_MEM
;
2106 ibmr
= &nesmr
->ibmr
;
2109 ibmr
= ERR_PTR(-ENOMEM
);
2113 /* single PBL case */
2114 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
, vpbl
.pbl_pbase
);
2122 static struct ib_mr
*nes_get_dma_mr(struct ib_pd
*pd
, int acc
)
2126 nes_debug(NES_DBG_MR
, "\n");
2128 return nes_reg_phys_mr(pd
, 0, 0xffffffffffULL
, acc
, &kva
);
2134 static struct ib_mr
*nes_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
2135 u64 virt
, int acc
, struct ib_udata
*udata
)
2140 dma_addr_t last_dma_addr
= 0;
2141 dma_addr_t first_dma_addr
= 0;
2142 struct nes_pd
*nespd
= to_nespd(pd
);
2143 struct nes_vnic
*nesvnic
= to_nesvnic(pd
->device
);
2144 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2145 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2146 struct ib_mr
*ibmr
= ERR_PTR(-EINVAL
);
2147 struct scatterlist
*sg
;
2148 struct nes_ucontext
*nes_ucontext
;
2149 struct nes_pbl
*nespbl
;
2150 struct nes_mr
*nesmr
;
2151 struct ib_umem
*region
;
2152 struct nes_mem_reg_req req
;
2153 struct nes_vpbl vpbl
;
2154 struct nes_root_vpbl root_vpbl
;
2155 int entry
, page_index
;
2157 int err
, pbl_depth
= 0;
2162 u32 next_stag_index
;
2164 u32 root_pbl_index
= 0;
2165 u32 cur_pbl_index
= 0;
2172 region
= ib_umem_get(pd
->uobject
->context
, start
, length
, acc
, 0);
2173 if (IS_ERR(region
)) {
2174 return (struct ib_mr
*)region
;
2177 nes_debug(NES_DBG_MR
, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2178 " offset = %u, page size = %u.\n",
2179 (unsigned long int)start
, (unsigned long int)virt
, (u32
)length
,
2180 ib_umem_offset(region
), region
->page_size
);
2182 skip_pages
= ((u32
)ib_umem_offset(region
)) >> 12;
2184 if (ib_copy_from_udata(&req
, udata
, sizeof(req
))) {
2185 ib_umem_release(region
);
2186 return ERR_PTR(-EFAULT
);
2188 nes_debug(NES_DBG_MR
, "Memory Registration type = %08X.\n", req
.reg_type
);
2190 switch (req
.reg_type
) {
2191 case IWNES_MEMREG_TYPE_MEM
:
2194 vpbl
.pbl_vbase
= NULL
;
2195 root_vpbl
.pbl_vbase
= NULL
;
2196 root_vpbl
.pbl_pbase
= 0;
2198 get_random_bytes(&next_stag_index
, sizeof(next_stag_index
));
2199 stag_key
= (u8
)next_stag_index
;
2201 driver_key
= next_stag_index
& 0x70000000;
2203 next_stag_index
>>= 8;
2204 next_stag_index
%= nesadapter
->max_mr
;
2206 err
= nes_alloc_resource(nesadapter
, nesadapter
->allocated_mrs
,
2207 nesadapter
->max_mr
, &stag_index
, &next_stag_index
, NES_RESOURCE_USER_MR
);
2209 ib_umem_release(region
);
2210 return ERR_PTR(err
);
2213 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2215 ib_umem_release(region
);
2216 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2217 return ERR_PTR(-ENOMEM
);
2219 nesmr
->region
= region
;
2221 for_each_sg(region
->sg_head
.sgl
, sg
, region
->nmap
, entry
) {
2222 if (sg_dma_address(sg
) & ~PAGE_MASK
) {
2223 ib_umem_release(region
);
2224 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2225 nes_debug(NES_DBG_MR
, "Unaligned Memory Buffer: 0x%x\n",
2226 (unsigned int) sg_dma_address(sg
));
2227 ibmr
= ERR_PTR(-EINVAL
);
2229 goto reg_user_mr_err
;
2232 if (!sg_dma_len(sg
)) {
2233 ib_umem_release(region
);
2234 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2236 nes_debug(NES_DBG_MR
, "Invalid Buffer Size\n");
2237 ibmr
= ERR_PTR(-EINVAL
);
2239 goto reg_user_mr_err
;
2242 region_length
+= sg_dma_len(sg
);
2243 chunk_pages
= sg_dma_len(sg
) >> 12;
2244 region_length
-= skip_pages
<< 12;
2245 for (page_index
= skip_pages
; page_index
< chunk_pages
; page_index
++) {
2247 if ((page_count
!= 0) && (page_count
<< 12) - (ib_umem_offset(region
) & (4096 - 1)) >= region
->length
)
2249 if ((page_count
&0x01FF) == 0) {
2250 if (page_count
>= 1024 * 512) {
2251 ib_umem_release(region
);
2252 nes_free_resource(nesadapter
,
2253 nesadapter
->allocated_mrs
, stag_index
);
2255 ibmr
= ERR_PTR(-E2BIG
);
2256 goto reg_user_mr_err
;
2258 if (root_pbl_index
== 1) {
2259 root_vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
,
2260 8192, &root_vpbl
.pbl_pbase
);
2261 nes_debug(NES_DBG_MR
, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2262 root_vpbl
.pbl_vbase
, (unsigned int)root_vpbl
.pbl_pbase
);
2263 if (!root_vpbl
.pbl_vbase
) {
2264 ib_umem_release(region
);
2265 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2267 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2270 ibmr
= ERR_PTR(-ENOMEM
);
2271 goto reg_user_mr_err
;
2273 root_vpbl
.leaf_vpbl
= kzalloc(sizeof(*root_vpbl
.leaf_vpbl
)*1024,
2275 if (!root_vpbl
.leaf_vpbl
) {
2276 ib_umem_release(region
);
2277 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2278 root_vpbl
.pbl_pbase
);
2279 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2281 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2284 ibmr
= ERR_PTR(-ENOMEM
);
2285 goto reg_user_mr_err
;
2287 root_vpbl
.pbl_vbase
[0].pa_low
=
2288 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2289 root_vpbl
.pbl_vbase
[0].pa_high
=
2290 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
) >> 32)));
2291 root_vpbl
.leaf_vpbl
[0] = vpbl
;
2293 vpbl
.pbl_vbase
= pci_alloc_consistent(nesdev
->pcidev
, 4096,
2295 nes_debug(NES_DBG_MR
, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2296 vpbl
.pbl_vbase
, (unsigned int)vpbl
.pbl_pbase
);
2297 if (!vpbl
.pbl_vbase
) {
2298 ib_umem_release(region
);
2299 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
, stag_index
);
2300 ibmr
= ERR_PTR(-ENOMEM
);
2302 goto reg_user_mr_err
;
2304 if (1 <= root_pbl_index
) {
2305 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_low
=
2306 cpu_to_le32((u32
)vpbl
.pbl_pbase
);
2307 root_vpbl
.pbl_vbase
[root_pbl_index
].pa_high
=
2308 cpu_to_le32((u32
)((((u64
)vpbl
.pbl_pbase
)>>32)));
2309 root_vpbl
.leaf_vpbl
[root_pbl_index
] = vpbl
;
2315 if (page_count
!= 0) {
2316 if ((last_dma_addr
+4096) !=
2317 (sg_dma_address(sg
)+
2320 last_dma_addr
= sg_dma_address(sg
)+
2323 first_dma_addr
= sg_dma_address(sg
)+
2325 last_dma_addr
= first_dma_addr
;
2329 vpbl
.pbl_vbase
[cur_pbl_index
].pa_low
=
2330 cpu_to_le32((u32
)(sg_dma_address(sg
)+
2331 (page_index
*4096)));
2332 vpbl
.pbl_vbase
[cur_pbl_index
].pa_high
=
2333 cpu_to_le32((u32
)((((u64
)(sg_dma_address(sg
)+
2334 (page_index
*4096))) >> 32)));
2341 nes_debug(NES_DBG_MR
, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2342 " stag_key=0x%08x\n",
2343 stag_index
, driver_key
, stag_key
);
2344 stag
= stag_index
<< 8;
2346 stag
+= (u32
)stag_key
;
2349 /* Make the leaf PBL the root if only one PBL */
2350 if (root_pbl_index
== 1) {
2351 root_vpbl
.pbl_pbase
= vpbl
.pbl_pbase
;
2357 pbl_count
= root_pbl_index
;
2360 nes_debug(NES_DBG_MR
, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2361 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2362 stag
, (unsigned int)iova_start
,
2363 (unsigned int)region_length
, stag_index
,
2364 (unsigned long long)region
->length
, pbl_count
);
2365 ret
= nes_reg_mr(nesdev
, nespd
, stag
, region
->length
, &root_vpbl
,
2366 first_dma_addr
, pbl_count
, (u16
)cur_pbl_index
, acc
,
2367 &iova_start
, &nesmr
->pbls_used
, &nesmr
->pbl_4k
);
2369 nes_debug(NES_DBG_MR
, "ret=%d\n", ret
);
2372 nesmr
->ibmr
.rkey
= stag
;
2373 nesmr
->ibmr
.lkey
= stag
;
2374 nesmr
->mode
= IWNES_MEMREG_TYPE_MEM
;
2375 ibmr
= &nesmr
->ibmr
;
2377 ib_umem_release(region
);
2379 ibmr
= ERR_PTR(-ENOMEM
);
2383 /* free the resources */
2384 if (root_pbl_index
== 1) {
2385 pci_free_consistent(nesdev
->pcidev
, 4096, vpbl
.pbl_vbase
,
2388 for (page_index
=0; page_index
<root_pbl_index
; page_index
++) {
2389 pci_free_consistent(nesdev
->pcidev
, 4096,
2390 root_vpbl
.leaf_vpbl
[page_index
].pbl_vbase
,
2391 root_vpbl
.leaf_vpbl
[page_index
].pbl_pbase
);
2393 kfree(root_vpbl
.leaf_vpbl
);
2394 pci_free_consistent(nesdev
->pcidev
, 8192, root_vpbl
.pbl_vbase
,
2395 root_vpbl
.pbl_pbase
);
2398 nes_debug(NES_DBG_MR
, "Leaving, ibmr=%p", ibmr
);
2401 case IWNES_MEMREG_TYPE_QP
:
2402 case IWNES_MEMREG_TYPE_CQ
:
2403 if (!region
->length
) {
2404 nes_debug(NES_DBG_MR
, "Unable to register zero length region for CQ\n");
2405 ib_umem_release(region
);
2406 return ERR_PTR(-EINVAL
);
2408 nespbl
= kzalloc(sizeof(*nespbl
), GFP_KERNEL
);
2410 nes_debug(NES_DBG_MR
, "Unable to allocate PBL\n");
2411 ib_umem_release(region
);
2412 return ERR_PTR(-ENOMEM
);
2414 nesmr
= kzalloc(sizeof(*nesmr
), GFP_KERNEL
);
2416 ib_umem_release(region
);
2418 nes_debug(NES_DBG_MR
, "Unable to allocate nesmr\n");
2419 return ERR_PTR(-ENOMEM
);
2421 nesmr
->region
= region
;
2422 nes_ucontext
= to_nesucontext(pd
->uobject
->context
);
2423 pbl_depth
= region
->length
>> 12;
2424 pbl_depth
+= (region
->length
& (4096-1)) ? 1 : 0;
2425 nespbl
->pbl_size
= pbl_depth
*sizeof(u64
);
2426 if (req
.reg_type
== IWNES_MEMREG_TYPE_QP
) {
2427 nes_debug(NES_DBG_MR
, "Attempting to allocate QP PBL memory");
2429 nes_debug(NES_DBG_MR
, "Attempting to allocate CP PBL memory");
2432 nes_debug(NES_DBG_MR
, " %u bytes, %u entries.\n",
2433 nespbl
->pbl_size
, pbl_depth
);
2434 pbl
= pci_alloc_consistent(nesdev
->pcidev
, nespbl
->pbl_size
,
2435 &nespbl
->pbl_pbase
);
2437 ib_umem_release(region
);
2440 nes_debug(NES_DBG_MR
, "Unable to allocate PBL memory\n");
2441 return ERR_PTR(-ENOMEM
);
2444 nespbl
->pbl_vbase
= (u64
*)pbl
;
2445 nespbl
->user_base
= start
;
2446 nes_debug(NES_DBG_MR
, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2447 " pbl_vbase=%p user_base=0x%lx\n",
2448 nespbl
->pbl_size
, (unsigned long) nespbl
->pbl_pbase
,
2449 (void *) nespbl
->pbl_vbase
, nespbl
->user_base
);
2451 for_each_sg(region
->sg_head
.sgl
, sg
, region
->nmap
, entry
) {
2452 chunk_pages
= sg_dma_len(sg
) >> 12;
2453 chunk_pages
+= (sg_dma_len(sg
) & (4096-1)) ? 1 : 0;
2455 nespbl
->page
= sg_page(sg
);
2459 for (page_index
= 0; page_index
< chunk_pages
; page_index
++) {
2460 ((__le32
*)pbl
)[0] = cpu_to_le32((u32
)
2461 (sg_dma_address(sg
)+
2462 (page_index
*4096)));
2463 ((__le32
*)pbl
)[1] = cpu_to_le32(((u64
)
2464 (sg_dma_address(sg
)+
2465 (page_index
*4096)))>>32);
2466 nes_debug(NES_DBG_MR
, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl
,
2467 (unsigned long long)*pbl
,
2468 le32_to_cpu(((__le32
*)pbl
)[1]), le32_to_cpu(((__le32
*)pbl
)[0]));
2473 if (req
.reg_type
== IWNES_MEMREG_TYPE_QP
) {
2474 list_add_tail(&nespbl
->list
, &nes_ucontext
->qp_reg_mem_list
);
2476 list_add_tail(&nespbl
->list
, &nes_ucontext
->cq_reg_mem_list
);
2478 nesmr
->ibmr
.rkey
= -1;
2479 nesmr
->ibmr
.lkey
= -1;
2480 nesmr
->mode
= req
.reg_type
;
2481 return &nesmr
->ibmr
;
2484 ib_umem_release(region
);
2485 return ERR_PTR(-ENOSYS
);
2492 static int nes_dereg_mr(struct ib_mr
*ib_mr
)
2494 struct nes_mr
*nesmr
= to_nesmr(ib_mr
);
2495 struct nes_vnic
*nesvnic
= to_nesvnic(ib_mr
->device
);
2496 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2497 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
2498 struct nes_hw_cqp_wqe
*cqp_wqe
;
2499 struct nes_cqp_request
*cqp_request
;
2500 unsigned long flags
;
2507 pci_free_consistent(nesdev
->pcidev
,
2508 nesmr
->max_pages
* sizeof(u64
),
2512 if (nesmr
->region
) {
2513 ib_umem_release(nesmr
->region
);
2515 if (nesmr
->mode
!= IWNES_MEMREG_TYPE_MEM
) {
2520 /* Deallocate the region with the adapter */
2522 cqp_request
= nes_get_cqp_request(nesdev
);
2523 if (cqp_request
== NULL
) {
2524 nes_debug(NES_DBG_MR
, "Failed to get a cqp_request.\n");
2527 cqp_request
->waiting
= 1;
2528 cqp_wqe
= &cqp_request
->cqp_wqe
;
2530 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2531 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
,
2532 NES_CQP_DEALLOCATE_STAG
| NES_CQP_STAG_VA_TO
|
2533 NES_CQP_STAG_DEALLOC_PBLS
| NES_CQP_STAG_MR
);
2534 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_STAG_WQE_STAG_IDX
, ib_mr
->rkey
);
2536 atomic_set(&cqp_request
->refcount
, 2);
2537 nes_post_cqp_request(nesdev
, cqp_request
);
2540 nes_debug(NES_DBG_MR
, "Waiting for deallocate STag 0x%08X completed\n", ib_mr
->rkey
);
2541 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
2543 nes_debug(NES_DBG_MR
, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2544 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2545 ib_mr
->rkey
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2547 major_code
= cqp_request
->major_code
;
2548 minor_code
= cqp_request
->minor_code
;
2550 nes_put_cqp_request(nesdev
, cqp_request
);
2553 nes_debug(NES_DBG_MR
, "Timeout waiting to destroy STag,"
2554 " ib_mr=%p, rkey = 0x%08X\n",
2555 ib_mr
, ib_mr
->rkey
);
2557 } else if (major_code
) {
2558 nes_debug(NES_DBG_MR
, "Error (0x%04X:0x%04X) while attempting"
2559 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2560 major_code
, minor_code
, ib_mr
, ib_mr
->rkey
);
2564 if (nesmr
->pbls_used
!= 0) {
2565 spin_lock_irqsave(&nesadapter
->pbl_lock
, flags
);
2566 if (nesmr
->pbl_4k
) {
2567 nesadapter
->free_4kpbl
+= nesmr
->pbls_used
;
2568 if (nesadapter
->free_4kpbl
> nesadapter
->max_4kpbl
)
2569 printk(KERN_ERR PFX
"free 4KB PBLs(%u) has "
2570 "exceeded the max(%u)\n",
2571 nesadapter
->free_4kpbl
,
2572 nesadapter
->max_4kpbl
);
2574 nesadapter
->free_256pbl
+= nesmr
->pbls_used
;
2575 if (nesadapter
->free_256pbl
> nesadapter
->max_256pbl
)
2576 printk(KERN_ERR PFX
"free 256B PBLs(%u) has "
2577 "exceeded the max(%u)\n",
2578 nesadapter
->free_256pbl
,
2579 nesadapter
->max_256pbl
);
2581 spin_unlock_irqrestore(&nesadapter
->pbl_lock
, flags
);
2583 nes_free_resource(nesadapter
, nesadapter
->allocated_mrs
,
2584 (ib_mr
->rkey
& 0x0fffff00) >> 8);
2595 static ssize_t
show_rev(struct device
*dev
, struct device_attribute
*attr
,
2598 struct nes_ib_device
*nesibdev
=
2599 container_of(dev
, struct nes_ib_device
, ibdev
.dev
);
2600 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
2602 nes_debug(NES_DBG_INIT
, "\n");
2603 return sprintf(buf
, "%x\n", nesvnic
->nesdev
->nesadapter
->hw_rev
);
2610 static ssize_t
show_fw_ver(struct device
*dev
, struct device_attribute
*attr
,
2613 struct nes_ib_device
*nesibdev
=
2614 container_of(dev
, struct nes_ib_device
, ibdev
.dev
);
2615 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
2617 nes_debug(NES_DBG_INIT
, "\n");
2618 return sprintf(buf
, "%u.%u\n",
2619 (nesvnic
->nesdev
->nesadapter
->firmware_version
>> 16),
2620 (nesvnic
->nesdev
->nesadapter
->firmware_version
& 0x000000ff));
2627 static ssize_t
show_hca(struct device
*dev
, struct device_attribute
*attr
,
2630 nes_debug(NES_DBG_INIT
, "\n");
2631 return sprintf(buf
, "NES020\n");
2638 static ssize_t
show_board(struct device
*dev
, struct device_attribute
*attr
,
2641 nes_debug(NES_DBG_INIT
, "\n");
2642 return sprintf(buf
, "%.*s\n", 32, "NES020 Board ID");
2646 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
2647 static DEVICE_ATTR(fw_ver
, S_IRUGO
, show_fw_ver
, NULL
);
2648 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca
, NULL
);
2649 static DEVICE_ATTR(board_id
, S_IRUGO
, show_board
, NULL
);
2651 static struct device_attribute
*nes_dev_attributes
[] = {
2662 static int nes_query_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2663 int attr_mask
, struct ib_qp_init_attr
*init_attr
)
2665 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
2667 nes_debug(NES_DBG_QP
, "\n");
2669 attr
->qp_access_flags
= 0;
2670 attr
->cap
.max_send_wr
= nesqp
->hwqp
.sq_size
;
2671 attr
->cap
.max_recv_wr
= nesqp
->hwqp
.rq_size
;
2672 attr
->cap
.max_recv_sge
= 1;
2673 if (nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
)
2674 attr
->cap
.max_inline_data
= 0;
2676 attr
->cap
.max_inline_data
= 64;
2678 init_attr
->event_handler
= nesqp
->ibqp
.event_handler
;
2679 init_attr
->qp_context
= nesqp
->ibqp
.qp_context
;
2680 init_attr
->send_cq
= nesqp
->ibqp
.send_cq
;
2681 init_attr
->recv_cq
= nesqp
->ibqp
.recv_cq
;
2682 init_attr
->srq
= nesqp
->ibqp
.srq
;
2683 init_attr
->cap
= attr
->cap
;
2692 int nes_hw_modify_qp(struct nes_device
*nesdev
, struct nes_qp
*nesqp
,
2693 u32 next_iwarp_state
, u32 termlen
, u32 wait_completion
)
2695 struct nes_hw_cqp_wqe
*cqp_wqe
;
2696 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2697 /* struct iw_cm_event cm_event; */
2698 struct nes_cqp_request
*cqp_request
;
2702 nes_debug(NES_DBG_MOD_QP
, "QP%u, refcount=%d\n",
2703 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
2705 cqp_request
= nes_get_cqp_request(nesdev
);
2706 if (cqp_request
== NULL
) {
2707 nes_debug(NES_DBG_MOD_QP
, "Failed to get a cqp_request.\n");
2710 if (wait_completion
) {
2711 cqp_request
->waiting
= 1;
2713 cqp_request
->waiting
= 0;
2715 cqp_wqe
= &cqp_request
->cqp_wqe
;
2717 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_OPCODE_IDX
,
2718 NES_CQP_MODIFY_QP
| NES_CQP_QP_TYPE_IWARP
| next_iwarp_state
);
2719 nes_debug(NES_DBG_MOD_QP
, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2720 next_iwarp_state
, le32_to_cpu(cqp_wqe
->wqe_words
[NES_CQP_WQE_OPCODE_IDX
]));
2721 nes_fill_init_cqp_wqe(cqp_wqe
, nesdev
);
2722 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_WQE_ID_IDX
, nesqp
->hwqp
.qp_id
);
2723 set_wqe_64bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_CONTEXT_LOW_IDX
, (u64
)nesqp
->nesqp_context_pbase
);
2725 /* If sending a terminate message, fill in the length (in words) */
2726 if (((next_iwarp_state
& NES_CQP_QP_IWARP_STATE_MASK
) == NES_CQP_QP_IWARP_STATE_TERMINATE
) &&
2727 !(next_iwarp_state
& NES_CQP_QP_TERM_DONT_SEND_TERM_MSG
)) {
2728 termlen
= ((termlen
+ 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT
;
2729 set_wqe_32bit_value(cqp_wqe
->wqe_words
, NES_CQP_QP_WQE_NEW_MSS_IDX
, termlen
);
2732 atomic_set(&cqp_request
->refcount
, 2);
2733 nes_post_cqp_request(nesdev
, cqp_request
);
2736 if (wait_completion
) {
2737 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2738 nesqp->hwqp.qp_id); */
2739 ret
= wait_event_timeout(cqp_request
->waitq
, (cqp_request
->request_done
!= 0),
2741 nes_debug(NES_DBG_MOD_QP
, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2742 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2743 nesqp
->hwqp
.qp_id
, ret
, cqp_request
->major_code
, cqp_request
->minor_code
);
2744 major_code
= cqp_request
->major_code
;
2746 nes_debug(NES_DBG_MOD_QP
, "Modify iwarp QP%u failed"
2747 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2748 nesqp
->hwqp
.qp_id
, cqp_request
->major_code
,
2749 cqp_request
->minor_code
, next_iwarp_state
);
2752 nes_put_cqp_request(nesdev
, cqp_request
);
2756 else if (major_code
)
2769 int nes_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2770 int attr_mask
, struct ib_udata
*udata
)
2772 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
2773 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
2774 struct nes_device
*nesdev
= nesvnic
->nesdev
;
2777 u32 next_iwarp_state
= 0;
2779 unsigned long qplockflags
;
2781 u16 original_last_aeq
;
2782 u8 issue_modify_qp
= 0;
2785 nes_debug(NES_DBG_MOD_QP
, "QP%u: QP State=%u, cur QP State=%u,"
2786 " iwarp_state=0x%X, refcount=%d\n",
2787 nesqp
->hwqp
.qp_id
, attr
->qp_state
, nesqp
->ibqp_state
,
2788 nesqp
->iwarp_state
, atomic_read(&nesqp
->refcount
));
2790 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
2792 nes_debug(NES_DBG_MOD_QP
, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2793 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2794 nesqp
->hwqp
.qp_id
, nesqp
->hw_iwarp_state
,
2795 nesqp
->hw_tcp_state
, attr
->qp_access_flags
, attr_mask
);
2797 if (attr_mask
& IB_QP_STATE
) {
2798 switch (attr
->qp_state
) {
2800 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = init\n",
2802 if (nesqp
->iwarp_state
> (u32
)NES_CQP_QP_IWARP_STATE_IDLE
) {
2803 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2806 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_IDLE
;
2807 issue_modify_qp
= 1;
2810 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = rtr\n",
2812 if (nesqp
->iwarp_state
>(u32
)NES_CQP_QP_IWARP_STATE_IDLE
) {
2813 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2816 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_IDLE
;
2817 issue_modify_qp
= 1;
2820 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = rts\n",
2822 if (nesqp
->iwarp_state
>(u32
)NES_CQP_QP_IWARP_STATE_RTS
) {
2823 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2826 if (nesqp
->cm_id
== NULL
) {
2827 nes_debug(NES_DBG_MOD_QP
, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2828 nesqp
->hwqp
.qp_id
);
2829 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2832 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_RTS
;
2833 if (nesqp
->iwarp_state
!= NES_CQP_QP_IWARP_STATE_RTS
)
2834 next_iwarp_state
|= NES_CQP_QP_CONTEXT_VALID
|
2835 NES_CQP_QP_ARP_VALID
| NES_CQP_QP_ORD_VALID
;
2836 issue_modify_qp
= 1;
2837 nesqp
->hw_tcp_state
= NES_AEQE_TCP_STATE_ESTABLISHED
;
2838 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_RTS
;
2839 nesqp
->hte_added
= 1;
2842 issue_modify_qp
= 1;
2843 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2844 nesqp
->hwqp
.qp_id
, nesqp
->hwqp
.sq_head
, nesqp
->hwqp
.sq_tail
);
2845 if (nesqp
->iwarp_state
== (u32
)NES_CQP_QP_IWARP_STATE_CLOSING
) {
2846 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2849 if (nesqp
->iwarp_state
> (u32
)NES_CQP_QP_IWARP_STATE_CLOSING
) {
2850 nes_debug(NES_DBG_MOD_QP
, "QP%u: State change to closing"
2851 " ignored due to current iWARP state\n",
2853 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2856 if (nesqp
->hw_iwarp_state
!= NES_AEQE_IWARP_STATE_RTS
) {
2857 nes_debug(NES_DBG_MOD_QP
, "QP%u: State change to closing"
2858 " already done based on hw state.\n",
2860 issue_modify_qp
= 0;
2862 switch (nesqp
->hw_iwarp_state
) {
2863 case NES_AEQE_IWARP_STATE_CLOSING
:
2864 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_CLOSING
;
2866 case NES_AEQE_IWARP_STATE_TERMINATE
:
2867 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_TERMINATE
;
2869 case NES_AEQE_IWARP_STATE_ERROR
:
2870 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_ERROR
;
2873 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_CLOSING
;
2874 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_CLOSING
;
2880 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = terminate\n",
2882 if (nesqp
->iwarp_state
>=(u32
)NES_CQP_QP_IWARP_STATE_TERMINATE
) {
2883 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2886 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2887 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_TERMINATE
;
2888 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_TERMINATE
;
2889 issue_modify_qp
= 1;
2893 if (nesqp
->iwarp_state
== (u32
)NES_CQP_QP_IWARP_STATE_ERROR
) {
2894 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2897 nes_debug(NES_DBG_MOD_QP
, "QP%u: new state = error\n",
2899 if (nesqp
->term_flags
)
2900 del_timer(&nesqp
->terminate_timer
);
2902 next_iwarp_state
= NES_CQP_QP_IWARP_STATE_ERROR
;
2903 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2904 if (nesqp
->hte_added
) {
2905 nes_debug(NES_DBG_MOD_QP
, "set CQP_QP_DEL_HTE\n");
2906 next_iwarp_state
|= NES_CQP_QP_DEL_HTE
;
2907 nesqp
->hte_added
= 0;
2909 if ((nesqp
->hw_tcp_state
> NES_AEQE_TCP_STATE_CLOSED
) &&
2910 (nesdev
->iw_status
) &&
2911 (nesqp
->hw_tcp_state
!= NES_AEQE_TCP_STATE_TIME_WAIT
)) {
2912 next_iwarp_state
|= NES_CQP_QP_RESET
;
2914 nes_debug(NES_DBG_MOD_QP
, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2915 nesqp
->hwqp
.qp_id
, nesqp
->hw_tcp_state
);
2918 issue_modify_qp
= 1;
2919 nesqp
->hw_iwarp_state
= NES_AEQE_IWARP_STATE_ERROR
;
2922 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2927 nesqp
->ibqp_state
= attr
->qp_state
;
2928 nesqp
->iwarp_state
= next_iwarp_state
& NES_CQP_QP_IWARP_STATE_MASK
;
2929 nes_debug(NES_DBG_MOD_QP
, "Change nesqp->iwarp_state=%08x\n",
2930 nesqp
->iwarp_state
);
2933 if (attr_mask
& IB_QP_ACCESS_FLAGS
) {
2934 if (attr
->qp_access_flags
& IB_ACCESS_LOCAL_WRITE
) {
2935 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
|
2936 NES_QPCONTEXT_MISC_RDMA_READ_EN
);
2937 issue_modify_qp
= 1;
2939 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_WRITE
) {
2940 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
);
2941 issue_modify_qp
= 1;
2943 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_READ
) {
2944 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN
);
2945 issue_modify_qp
= 1;
2947 if (attr
->qp_access_flags
& IB_ACCESS_MW_BIND
) {
2948 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN
);
2949 issue_modify_qp
= 1;
2952 if (nesqp
->user_mode
) {
2953 nesqp
->nesqp_context
->misc
|= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN
|
2954 NES_QPCONTEXT_MISC_RDMA_READ_EN
);
2955 issue_modify_qp
= 1;
2959 original_last_aeq
= nesqp
->last_aeq
;
2960 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2962 nes_debug(NES_DBG_MOD_QP
, "issue_modify_qp=%u\n", issue_modify_qp
);
2967 if (issue_modify_qp
) {
2968 nes_debug(NES_DBG_MOD_QP
, "call nes_hw_modify_qp\n");
2969 ret
= nes_hw_modify_qp(nesdev
, nesqp
, next_iwarp_state
, 0, 1);
2971 nes_debug(NES_DBG_MOD_QP
, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2972 " failed for QP%u.\n",
2973 next_iwarp_state
, nesqp
->hwqp
.qp_id
);
2977 if ((issue_modify_qp
) && (nesqp
->ibqp_state
> IB_QPS_RTS
)) {
2978 nes_debug(NES_DBG_MOD_QP
, "QP%u Issued ModifyQP refcount (%d),"
2979 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2980 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2981 original_last_aeq
, nesqp
->last_aeq
);
2982 if (!ret
|| original_last_aeq
!= NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE
) {
2984 if (nesqp
->cm_id
&& nesqp
->hw_tcp_state
!= 0) {
2985 nes_debug(NES_DBG_MOD_QP
, "QP%u Queuing fake disconnect for QP refcount (%d),"
2986 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2987 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
2988 original_last_aeq
, nesqp
->last_aeq
);
2989 /* this one is for the cm_disconnect thread */
2990 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
2991 nesqp
->hw_tcp_state
= NES_AEQE_TCP_STATE_CLOSED
;
2992 nesqp
->last_aeq
= NES_AEQE_AEID_RESET_SENT
;
2993 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
2994 nes_cm_disconn(nesqp
);
2996 nes_debug(NES_DBG_MOD_QP
, "QP%u No fake disconnect, QP refcount=%d\n",
2997 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
3000 spin_lock_irqsave(&nesqp
->lock
, qplockflags
);
3002 /* These two are for the timer thread */
3003 if (atomic_inc_return(&nesqp
->close_timer_started
) == 1) {
3004 nesqp
->cm_id
->add_ref(nesqp
->cm_id
);
3005 nes_debug(NES_DBG_MOD_QP
, "QP%u Not decrementing QP refcount (%d),"
3006 " need ae to finish up, original_last_aeq = 0x%04X."
3007 " last_aeq = 0x%04X, scheduling timer.\n",
3008 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3009 original_last_aeq
, nesqp
->last_aeq
);
3010 schedule_nes_timer(nesqp
->cm_node
, (struct sk_buff
*) nesqp
, NES_TIMER_TYPE_CLOSE
, 1, 0);
3012 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3014 spin_unlock_irqrestore(&nesqp
->lock
, qplockflags
);
3015 nes_debug(NES_DBG_MOD_QP
, "QP%u Not decrementing QP refcount (%d),"
3016 " need ae to finish up, original_last_aeq = 0x%04X."
3017 " last_aeq = 0x%04X.\n",
3018 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3019 original_last_aeq
, nesqp
->last_aeq
);
3023 nes_debug(NES_DBG_MOD_QP
, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3024 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3025 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3026 original_last_aeq
, nesqp
->last_aeq
);
3029 nes_debug(NES_DBG_MOD_QP
, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3030 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3031 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
),
3032 original_last_aeq
, nesqp
->last_aeq
);
3037 nes_debug(NES_DBG_MOD_QP
, "QP%u Leaving, refcount=%d\n",
3038 nesqp
->hwqp
.qp_id
, atomic_read(&nesqp
->refcount
));
3045 * nes_muticast_attach
3047 static int nes_multicast_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
3049 nes_debug(NES_DBG_INIT
, "\n");
3055 * nes_multicast_detach
3057 static int nes_multicast_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
3059 nes_debug(NES_DBG_INIT
, "\n");
3067 static int nes_process_mad(struct ib_device
*ibdev
, int mad_flags
,
3068 u8 port_num
, const struct ib_wc
*in_wc
, const struct ib_grh
*in_grh
,
3069 const struct ib_mad_hdr
*in
, size_t in_mad_size
,
3070 struct ib_mad_hdr
*out
, size_t *out_mad_size
,
3071 u16
*out_mad_pkey_index
)
3073 nes_debug(NES_DBG_INIT
, "\n");
3078 fill_wqe_sg_send(struct nes_hw_qp_wqe
*wqe
, struct ib_send_wr
*ib_wr
, u32 uselkey
)
3081 int total_payload_length
= 0;
3082 for (sge_index
= 0; sge_index
< ib_wr
->num_sge
; sge_index
++) {
3083 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX
+(sge_index
*4),
3084 ib_wr
->sg_list
[sge_index
].addr
);
3085 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_LENGTH0_IDX
+ (sge_index
*4),
3086 ib_wr
->sg_list
[sge_index
].length
);
3088 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
+ (sge_index
*4),
3089 (ib_wr
->sg_list
[sge_index
].lkey
));
3091 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
+ (sge_index
*4), 0);
3093 total_payload_length
+= ib_wr
->sg_list
[sge_index
].length
;
3095 nes_debug(NES_DBG_IW_TX
, "UC UC UC, sending total_payload_length=%u \n",
3096 total_payload_length
);
3097 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3098 total_payload_length
);
3104 static int nes_post_send(struct ib_qp
*ibqp
, struct ib_send_wr
*ib_wr
,
3105 struct ib_send_wr
**bad_wr
)
3108 unsigned long flags
= 0;
3109 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
3110 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3111 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3112 struct nes_hw_qp_wqe
*wqe
;
3114 u32 qsize
= nesqp
->hwqp
.sq_size
;
3120 if (nesqp
->ibqp_state
> IB_QPS_RTS
) {
3125 spin_lock_irqsave(&nesqp
->lock
, flags
);
3127 head
= nesqp
->hwqp
.sq_head
;
3130 /* Check for QP error */
3131 if (nesqp
->term_flags
) {
3136 /* Check for SQ overflow */
3137 if (((head
+ (2 * qsize
) - nesqp
->hwqp
.sq_tail
) % qsize
) == (qsize
- 1)) {
3142 wqe
= &nesqp
->hwqp
.sq_vbase
[head
];
3143 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3144 nesqp->hwqp.qp_id, wqe, head); */
3145 nes_fill_init_qp_wqe(wqe
, nesqp
, head
);
3146 u64temp
= (u64
)(ib_wr
->wr_id
);
3147 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
,
3149 switch (ib_wr
->opcode
) {
3151 case IB_WR_SEND_WITH_INV
:
3152 if (IB_WR_SEND
== ib_wr
->opcode
) {
3153 if (ib_wr
->send_flags
& IB_SEND_SOLICITED
)
3154 wqe_misc
= NES_IWARP_SQ_OP_SENDSE
;
3156 wqe_misc
= NES_IWARP_SQ_OP_SEND
;
3158 if (ib_wr
->send_flags
& IB_SEND_SOLICITED
)
3159 wqe_misc
= NES_IWARP_SQ_OP_SENDSEINV
;
3161 wqe_misc
= NES_IWARP_SQ_OP_SENDINV
;
3163 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX
,
3164 ib_wr
->ex
.invalidate_rkey
);
3167 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3172 if (ib_wr
->send_flags
& IB_SEND_FENCE
)
3173 wqe_misc
|= NES_IWARP_SQ_WQE_LOCAL_FENCE
;
3175 if ((ib_wr
->send_flags
& IB_SEND_INLINE
) &&
3176 ((nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) == 0) &&
3177 (ib_wr
->sg_list
[0].length
<= 64)) {
3178 memcpy(&wqe
->wqe_words
[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX
],
3179 (void *)(unsigned long)ib_wr
->sg_list
[0].addr
, ib_wr
->sg_list
[0].length
);
3180 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3181 ib_wr
->sg_list
[0].length
);
3182 wqe_misc
|= NES_IWARP_SQ_WQE_IMM_DATA
;
3184 fill_wqe_sg_send(wqe
, ib_wr
, 1);
3188 case IB_WR_RDMA_WRITE
:
3189 wqe_misc
= NES_IWARP_SQ_OP_RDMAW
;
3190 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3191 nes_debug(NES_DBG_IW_TX
, "Exceeded max sge, ib_wr=%u, max=%u\n",
3192 ib_wr
->num_sge
, nesdev
->nesadapter
->max_sge
);
3197 if (ib_wr
->send_flags
& IB_SEND_FENCE
)
3198 wqe_misc
|= NES_IWARP_SQ_WQE_LOCAL_FENCE
;
3200 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_STAG_IDX
,
3201 rdma_wr(ib_wr
)->rkey
);
3202 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX
,
3203 rdma_wr(ib_wr
)->remote_addr
);
3205 if ((ib_wr
->send_flags
& IB_SEND_INLINE
) &&
3206 ((nes_drv_opt
& NES_DRV_OPT_NO_INLINE_DATA
) == 0) &&
3207 (ib_wr
->sg_list
[0].length
<= 64)) {
3208 memcpy(&wqe
->wqe_words
[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX
],
3209 (void *)(unsigned long)ib_wr
->sg_list
[0].addr
, ib_wr
->sg_list
[0].length
);
3210 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
,
3211 ib_wr
->sg_list
[0].length
);
3212 wqe_misc
|= NES_IWARP_SQ_WQE_IMM_DATA
;
3214 fill_wqe_sg_send(wqe
, ib_wr
, 1);
3217 wqe
->wqe_words
[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
] =
3218 wqe
->wqe_words
[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
];
3220 case IB_WR_RDMA_READ
:
3221 case IB_WR_RDMA_READ_WITH_INV
:
3222 /* iWARP only supports 1 sge for RDMA reads */
3223 if (ib_wr
->num_sge
> 1) {
3224 nes_debug(NES_DBG_IW_TX
, "Exceeded max sge, ib_wr=%u, max=1\n",
3229 if (ib_wr
->opcode
== IB_WR_RDMA_READ
) {
3230 wqe_misc
= NES_IWARP_SQ_OP_RDMAR
;
3232 wqe_misc
= NES_IWARP_SQ_OP_RDMAR_LOCINV
;
3233 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX
,
3234 ib_wr
->ex
.invalidate_rkey
);
3237 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX
,
3238 rdma_wr(ib_wr
)->remote_addr
);
3239 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_STAG_IDX
,
3240 rdma_wr(ib_wr
)->rkey
);
3241 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
,
3242 ib_wr
->sg_list
->length
);
3243 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX
,
3244 ib_wr
->sg_list
->addr
);
3245 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_STAG0_IDX
,
3246 ib_wr
->sg_list
->lkey
);
3248 case IB_WR_LOCAL_INV
:
3249 wqe_misc
= NES_IWARP_SQ_OP_LOCINV
;
3250 set_wqe_32bit_value(wqe
->wqe_words
,
3251 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX
,
3252 ib_wr
->ex
.invalidate_rkey
);
3256 struct nes_mr
*mr
= to_nesmr(reg_wr(ib_wr
)->mr
);
3257 int page_shift
= ilog2(reg_wr(ib_wr
)->mr
->page_size
);
3258 int flags
= reg_wr(ib_wr
)->access
;
3260 if (mr
->npages
> (NES_4K_PBL_CHUNK_SIZE
/ sizeof(u64
))) {
3261 nes_debug(NES_DBG_IW_TX
, "SQ_FMR: bad page_list_len\n");
3265 wqe_misc
= NES_IWARP_SQ_OP_FAST_REG
;
3266 set_wqe_64bit_value(wqe
->wqe_words
,
3267 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX
,
3269 set_wqe_32bit_value(wqe
->wqe_words
,
3270 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX
,
3272 set_wqe_32bit_value(wqe
->wqe_words
,
3273 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX
, 0);
3274 set_wqe_32bit_value(wqe
->wqe_words
,
3275 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX
,
3276 reg_wr(ib_wr
)->key
);
3278 if (page_shift
== 12) {
3279 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K
;
3280 } else if (page_shift
== 21) {
3281 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M
;
3283 nes_debug(NES_DBG_IW_TX
, "Invalid page shift,"
3284 " ib_wr=%u, max=1\n", ib_wr
->num_sge
);
3289 /* Set access_flags */
3290 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ
;
3291 if (flags
& IB_ACCESS_LOCAL_WRITE
)
3292 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE
;
3294 if (flags
& IB_ACCESS_REMOTE_WRITE
)
3295 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE
;
3297 if (flags
& IB_ACCESS_REMOTE_READ
)
3298 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ
;
3300 if (flags
& IB_ACCESS_MW_BIND
)
3301 wqe_misc
|= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND
;
3303 /* Fill in PBL info: */
3304 set_wqe_64bit_value(wqe
->wqe_words
,
3305 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX
,
3308 set_wqe_32bit_value(wqe
->wqe_words
,
3309 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX
,
3312 nes_debug(NES_DBG_IW_TX
, "SQ_REG_MR: iova_start: %llx, "
3313 "length: %d, rkey: %0x, pgl_paddr: %llx, "
3314 "page_list_len: %u, wqe_misc: %x\n",
3315 (unsigned long long) mr
->ibmr
.iova
,
3318 (unsigned long long) mr
->paddr
,
3332 if ((ib_wr
->send_flags
& IB_SEND_SIGNALED
) || nesqp
->sig_all
)
3333 wqe_misc
|= NES_IWARP_SQ_WQE_SIGNALED_COMPL
;
3335 wqe
->wqe_words
[NES_IWARP_SQ_WQE_MISC_IDX
] = cpu_to_le32(wqe_misc
);
3337 ib_wr
= ib_wr
->next
;
3345 nesqp
->hwqp
.sq_head
= head
;
3348 counter
= min(wqe_count
, ((u32
)255));
3349 wqe_count
-= counter
;
3350 nes_write32(nesdev
->regs
+ NES_WQE_ALLOC
,
3351 (counter
<< 24) | 0x00800000 | nesqp
->hwqp
.qp_id
);
3354 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
3366 static int nes_post_recv(struct ib_qp
*ibqp
, struct ib_recv_wr
*ib_wr
,
3367 struct ib_recv_wr
**bad_wr
)
3370 unsigned long flags
= 0;
3371 struct nes_vnic
*nesvnic
= to_nesvnic(ibqp
->device
);
3372 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3373 struct nes_qp
*nesqp
= to_nesqp(ibqp
);
3374 struct nes_hw_qp_wqe
*wqe
;
3377 u32 qsize
= nesqp
->hwqp
.rq_size
;
3381 u32 total_payload_length
;
3383 if (nesqp
->ibqp_state
> IB_QPS_RTS
) {
3388 spin_lock_irqsave(&nesqp
->lock
, flags
);
3390 head
= nesqp
->hwqp
.rq_head
;
3393 /* Check for QP error */
3394 if (nesqp
->term_flags
) {
3399 if (ib_wr
->num_sge
> nesdev
->nesadapter
->max_sge
) {
3403 /* Check for RQ overflow */
3404 if (((head
+ (2 * qsize
) - nesqp
->hwqp
.rq_tail
) % qsize
) == (qsize
- 1)) {
3409 nes_debug(NES_DBG_IW_RX
, "ibwr sge count = %u.\n", ib_wr
->num_sge
);
3410 wqe
= &nesqp
->hwqp
.rq_vbase
[head
];
3412 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3413 nesqp->hwqp.qp_id, wqe, head); */
3414 nes_fill_init_qp_wqe(wqe
, nesqp
, head
);
3415 u64temp
= (u64
)(ib_wr
->wr_id
);
3416 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
,
3418 total_payload_length
= 0;
3419 for (sge_index
=0; sge_index
< ib_wr
->num_sge
; sge_index
++) {
3420 set_wqe_64bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX
+(sge_index
*4),
3421 ib_wr
->sg_list
[sge_index
].addr
);
3422 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_LENGTH0_IDX
+(sge_index
*4),
3423 ib_wr
->sg_list
[sge_index
].length
);
3424 set_wqe_32bit_value(wqe
->wqe_words
,NES_IWARP_RQ_WQE_STAG0_IDX
+(sge_index
*4),
3425 ib_wr
->sg_list
[sge_index
].lkey
);
3427 total_payload_length
+= ib_wr
->sg_list
[sge_index
].length
;
3429 set_wqe_32bit_value(wqe
->wqe_words
, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX
,
3430 total_payload_length
);
3432 ib_wr
= ib_wr
->next
;
3439 nesqp
->hwqp
.rq_head
= head
;
3442 counter
= min(wqe_count
, ((u32
)255));
3443 wqe_count
-= counter
;
3444 nes_write32(nesdev
->regs
+NES_WQE_ALLOC
, (counter
<<24) | nesqp
->hwqp
.qp_id
);
3447 spin_unlock_irqrestore(&nesqp
->lock
, flags
);
3459 static int nes_poll_cq(struct ib_cq
*ibcq
, int num_entries
, struct ib_wc
*entry
)
3463 unsigned long flags
= 0;
3464 struct nes_vnic
*nesvnic
= to_nesvnic(ibcq
->device
);
3465 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3466 struct nes_cq
*nescq
= to_nescq(ibcq
);
3467 struct nes_qp
*nesqp
;
3468 struct nes_hw_cqe cqe
;
3475 u32 move_cq_head
= 1;
3478 nes_debug(NES_DBG_CQ
, "\n");
3480 spin_lock_irqsave(&nescq
->lock
, flags
);
3482 head
= nescq
->hw_cq
.cq_head
;
3483 cq_size
= nescq
->hw_cq
.cq_size
;
3485 while (cqe_count
< num_entries
) {
3486 if ((le32_to_cpu(nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_OPCODE_IDX
]) &
3487 NES_CQE_VALID
) == 0)
3491 * Make sure we read CQ entry contents *after*
3492 * we've checked the valid bit.
3496 cqe
= nescq
->hw_cq
.cq_vbase
[head
];
3497 u32temp
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
3498 wqe_index
= u32temp
& (nesdev
->nesadapter
->max_qp_wr
- 1);
3499 u32temp
&= ~(NES_SW_CONTEXT_ALIGN
-1);
3500 /* parse CQE, get completion context from WQE (either rq or sq) */
3501 u64temp
= (((u64
)(le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_HIGH_IDX
])))<<32) |
3505 nesqp
= (struct nes_qp
*)(unsigned long)u64temp
;
3506 memset(entry
, 0, sizeof *entry
);
3507 if (cqe
.cqe_words
[NES_CQE_ERROR_CODE_IDX
] == 0) {
3508 entry
->status
= IB_WC_SUCCESS
;
3510 err_code
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_ERROR_CODE_IDX
]);
3511 if (NES_IWARP_CQE_MAJOR_DRV
== (err_code
>> 16)) {
3512 entry
->status
= err_code
& 0x0000ffff;
3514 /* The rest of the cqe's will be marked as flushed */
3515 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_ERROR_CODE_IDX
] =
3516 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH
<< 16) |
3517 NES_IWARP_CQE_MINOR_FLUSH
);
3519 entry
->status
= IB_WC_WR_FLUSH_ERR
;
3522 entry
->qp
= &nesqp
->ibqp
;
3523 entry
->src_qp
= nesqp
->hwqp
.qp_id
;
3525 if (le32_to_cpu(cqe
.cqe_words
[NES_CQE_OPCODE_IDX
]) & NES_CQE_SQ
) {
3526 if (nesqp
->skip_lsmm
) {
3527 nesqp
->skip_lsmm
= 0;
3528 nesqp
->hwqp
.sq_tail
++;
3531 /* Working on a SQ Completion*/
3532 wrid
= (((u64
)(cpu_to_le32((u32
)nesqp
->hwqp
.sq_vbase
[wqe_index
].
3533 wqe_words
[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX
]))) << 32) |
3534 ((u64
)(cpu_to_le32((u32
)nesqp
->hwqp
.sq_vbase
[wqe_index
].
3535 wqe_words
[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX
])));
3536 entry
->byte_len
= le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3537 wqe_words
[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX
]);
3539 switch (le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3540 wqe_words
[NES_IWARP_SQ_WQE_MISC_IDX
]) & 0x3f) {
3541 case NES_IWARP_SQ_OP_RDMAW
:
3542 nes_debug(NES_DBG_CQ
, "Operation = RDMA WRITE.\n");
3543 entry
->opcode
= IB_WC_RDMA_WRITE
;
3545 case NES_IWARP_SQ_OP_RDMAR
:
3546 nes_debug(NES_DBG_CQ
, "Operation = RDMA READ.\n");
3547 entry
->opcode
= IB_WC_RDMA_READ
;
3548 entry
->byte_len
= le32_to_cpu(nesqp
->hwqp
.sq_vbase
[wqe_index
].
3549 wqe_words
[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX
]);
3551 case NES_IWARP_SQ_OP_SENDINV
:
3552 case NES_IWARP_SQ_OP_SENDSEINV
:
3553 case NES_IWARP_SQ_OP_SEND
:
3554 case NES_IWARP_SQ_OP_SENDSE
:
3555 nes_debug(NES_DBG_CQ
, "Operation = Send.\n");
3556 entry
->opcode
= IB_WC_SEND
;
3558 case NES_IWARP_SQ_OP_LOCINV
:
3559 entry
->opcode
= IB_WC_LOCAL_INV
;
3561 case NES_IWARP_SQ_OP_FAST_REG
:
3562 entry
->opcode
= IB_WC_REG_MR
;
3566 nesqp
->hwqp
.sq_tail
= (wqe_index
+1)&(nesqp
->hwqp
.sq_size
- 1);
3567 if ((entry
->status
!= IB_WC_SUCCESS
) && (nesqp
->hwqp
.sq_tail
!= nesqp
->hwqp
.sq_head
)) {
3569 wq_tail
= nesqp
->hwqp
.sq_tail
;
3572 /* Working on a RQ Completion*/
3573 entry
->byte_len
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_PAYLOAD_LENGTH_IDX
]);
3574 wrid
= ((u64
)(le32_to_cpu(nesqp
->hwqp
.rq_vbase
[wqe_index
].wqe_words
[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX
]))) |
3575 ((u64
)(le32_to_cpu(nesqp
->hwqp
.rq_vbase
[wqe_index
].wqe_words
[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX
]))<<32);
3576 entry
->opcode
= IB_WC_RECV
;
3578 nesqp
->hwqp
.rq_tail
= (wqe_index
+1)&(nesqp
->hwqp
.rq_size
- 1);
3579 if ((entry
->status
!= IB_WC_SUCCESS
) && (nesqp
->hwqp
.rq_tail
!= nesqp
->hwqp
.rq_head
)) {
3581 wq_tail
= nesqp
->hwqp
.rq_tail
;
3585 entry
->wr_id
= wrid
;
3591 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_OPCODE_IDX
] = 0;
3592 if (++head
>= cq_size
)
3594 nescq
->polled_completions
++;
3596 if ((nescq
->polled_completions
> (cq_size
/ 2)) ||
3597 (nescq
->polled_completions
== 255)) {
3598 nes_debug(NES_DBG_CQ
, "CQ%u Issuing CQE Allocate since more than half of cqes"
3599 " are pending %u of %u.\n",
3600 nescq
->hw_cq
.cq_number
, nescq
->polled_completions
, cq_size
);
3601 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
,
3602 nescq
->hw_cq
.cq_number
| (nescq
->polled_completions
<< 16));
3603 nescq
->polled_completions
= 0;
3606 /* Update the wqe index and set status to flush */
3607 wqe_index
= le32_to_cpu(cqe
.cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
]);
3608 wqe_index
= (wqe_index
& (~(nesdev
->nesadapter
->max_qp_wr
- 1))) | wq_tail
;
3609 nescq
->hw_cq
.cq_vbase
[head
].cqe_words
[NES_CQE_COMP_COMP_CTX_LOW_IDX
] =
3610 cpu_to_le32(wqe_index
);
3611 move_cq_head
= 1; /* ready for next pass */
3615 if (nescq
->polled_completions
) {
3616 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
,
3617 nescq
->hw_cq
.cq_number
| (nescq
->polled_completions
<< 16));
3618 nescq
->polled_completions
= 0;
3621 nescq
->hw_cq
.cq_head
= head
;
3622 nes_debug(NES_DBG_CQ
, "Reporting %u completions for CQ%u.\n",
3623 cqe_count
, nescq
->hw_cq
.cq_number
);
3625 spin_unlock_irqrestore(&nescq
->lock
, flags
);
3634 static int nes_req_notify_cq(struct ib_cq
*ibcq
, enum ib_cq_notify_flags notify_flags
)
3636 struct nes_vnic
*nesvnic
= to_nesvnic(ibcq
->device
);
3637 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3638 struct nes_cq
*nescq
= to_nescq(ibcq
);
3641 nes_debug(NES_DBG_CQ
, "Requesting notification for CQ%u.\n",
3642 nescq
->hw_cq
.cq_number
);
3644 cq_arm
= nescq
->hw_cq
.cq_number
;
3645 if ((notify_flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_NEXT_COMP
)
3646 cq_arm
|= NES_CQE_ALLOC_NOTIFY_NEXT
;
3647 else if ((notify_flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_SOLICITED
)
3648 cq_arm
|= NES_CQE_ALLOC_NOTIFY_SE
;
3652 nes_write32(nesdev
->regs
+NES_CQE_ALLOC
, cq_arm
);
3653 nes_read32(nesdev
->regs
+NES_CQE_ALLOC
);
3658 static int nes_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
3659 struct ib_port_immutable
*immutable
)
3661 struct ib_port_attr attr
;
3664 err
= nes_query_port(ibdev
, port_num
, &attr
);
3668 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
3669 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
3670 immutable
->core_cap_flags
= RDMA_CORE_PORT_IWARP
;
3676 * nes_init_ofa_device
3678 struct nes_ib_device
*nes_init_ofa_device(struct net_device
*netdev
)
3680 struct nes_ib_device
*nesibdev
;
3681 struct nes_vnic
*nesvnic
= netdev_priv(netdev
);
3682 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3684 nesibdev
= (struct nes_ib_device
*)ib_alloc_device(sizeof(struct nes_ib_device
));
3685 if (nesibdev
== NULL
) {
3688 strlcpy(nesibdev
->ibdev
.name
, "nes%d", IB_DEVICE_NAME_MAX
);
3689 nesibdev
->ibdev
.owner
= THIS_MODULE
;
3691 nesibdev
->ibdev
.node_type
= RDMA_NODE_RNIC
;
3692 memset(&nesibdev
->ibdev
.node_guid
, 0, sizeof(nesibdev
->ibdev
.node_guid
));
3693 memcpy(&nesibdev
->ibdev
.node_guid
, netdev
->dev_addr
, 6);
3695 nesibdev
->ibdev
.uverbs_cmd_mask
=
3696 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
3697 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
3698 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
3699 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
3700 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
3701 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
3702 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
3703 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
3704 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
3705 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
3706 (1ull << IB_USER_VERBS_CMD_CREATE_AH
) |
3707 (1ull << IB_USER_VERBS_CMD_DESTROY_AH
) |
3708 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ
) |
3709 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
3710 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
3711 (1ull << IB_USER_VERBS_CMD_POLL_CQ
) |
3712 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
3713 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
3714 (1ull << IB_USER_VERBS_CMD_BIND_MW
) |
3715 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
) |
3716 (1ull << IB_USER_VERBS_CMD_POST_RECV
) |
3717 (1ull << IB_USER_VERBS_CMD_POST_SEND
);
3719 nesibdev
->ibdev
.phys_port_cnt
= 1;
3720 nesibdev
->ibdev
.num_comp_vectors
= 1;
3721 nesibdev
->ibdev
.dma_device
= &nesdev
->pcidev
->dev
;
3722 nesibdev
->ibdev
.dev
.parent
= &nesdev
->pcidev
->dev
;
3723 nesibdev
->ibdev
.query_device
= nes_query_device
;
3724 nesibdev
->ibdev
.query_port
= nes_query_port
;
3725 nesibdev
->ibdev
.query_pkey
= nes_query_pkey
;
3726 nesibdev
->ibdev
.query_gid
= nes_query_gid
;
3727 nesibdev
->ibdev
.alloc_ucontext
= nes_alloc_ucontext
;
3728 nesibdev
->ibdev
.dealloc_ucontext
= nes_dealloc_ucontext
;
3729 nesibdev
->ibdev
.mmap
= nes_mmap
;
3730 nesibdev
->ibdev
.alloc_pd
= nes_alloc_pd
;
3731 nesibdev
->ibdev
.dealloc_pd
= nes_dealloc_pd
;
3732 nesibdev
->ibdev
.create_ah
= nes_create_ah
;
3733 nesibdev
->ibdev
.destroy_ah
= nes_destroy_ah
;
3734 nesibdev
->ibdev
.create_qp
= nes_create_qp
;
3735 nesibdev
->ibdev
.modify_qp
= nes_modify_qp
;
3736 nesibdev
->ibdev
.query_qp
= nes_query_qp
;
3737 nesibdev
->ibdev
.destroy_qp
= nes_destroy_qp
;
3738 nesibdev
->ibdev
.create_cq
= nes_create_cq
;
3739 nesibdev
->ibdev
.destroy_cq
= nes_destroy_cq
;
3740 nesibdev
->ibdev
.poll_cq
= nes_poll_cq
;
3741 nesibdev
->ibdev
.get_dma_mr
= nes_get_dma_mr
;
3742 nesibdev
->ibdev
.reg_user_mr
= nes_reg_user_mr
;
3743 nesibdev
->ibdev
.dereg_mr
= nes_dereg_mr
;
3744 nesibdev
->ibdev
.alloc_mw
= nes_alloc_mw
;
3745 nesibdev
->ibdev
.dealloc_mw
= nes_dealloc_mw
;
3747 nesibdev
->ibdev
.alloc_mr
= nes_alloc_mr
;
3748 nesibdev
->ibdev
.map_mr_sg
= nes_map_mr_sg
;
3750 nesibdev
->ibdev
.attach_mcast
= nes_multicast_attach
;
3751 nesibdev
->ibdev
.detach_mcast
= nes_multicast_detach
;
3752 nesibdev
->ibdev
.process_mad
= nes_process_mad
;
3754 nesibdev
->ibdev
.req_notify_cq
= nes_req_notify_cq
;
3755 nesibdev
->ibdev
.post_send
= nes_post_send
;
3756 nesibdev
->ibdev
.post_recv
= nes_post_recv
;
3758 nesibdev
->ibdev
.iwcm
= kzalloc(sizeof(*nesibdev
->ibdev
.iwcm
), GFP_KERNEL
);
3759 if (nesibdev
->ibdev
.iwcm
== NULL
) {
3760 ib_dealloc_device(&nesibdev
->ibdev
);
3763 nesibdev
->ibdev
.iwcm
->add_ref
= nes_add_ref
;
3764 nesibdev
->ibdev
.iwcm
->rem_ref
= nes_rem_ref
;
3765 nesibdev
->ibdev
.iwcm
->get_qp
= nes_get_qp
;
3766 nesibdev
->ibdev
.iwcm
->connect
= nes_connect
;
3767 nesibdev
->ibdev
.iwcm
->accept
= nes_accept
;
3768 nesibdev
->ibdev
.iwcm
->reject
= nes_reject
;
3769 nesibdev
->ibdev
.iwcm
->create_listen
= nes_create_listen
;
3770 nesibdev
->ibdev
.iwcm
->destroy_listen
= nes_destroy_listen
;
3771 nesibdev
->ibdev
.get_port_immutable
= nes_port_immutable
;
3772 memcpy(nesibdev
->ibdev
.iwcm
->ifname
, netdev
->name
,
3773 sizeof(nesibdev
->ibdev
.iwcm
->ifname
));
3780 * nes_handle_delayed_event
3782 static void nes_handle_delayed_event(unsigned long data
)
3784 struct nes_vnic
*nesvnic
= (void *) data
;
3786 if (nesvnic
->delayed_event
!= nesvnic
->last_dispatched_event
) {
3787 struct ib_event event
;
3789 event
.device
= &nesvnic
->nesibdev
->ibdev
;
3792 event
.event
= nesvnic
->delayed_event
;
3793 event
.element
.port_num
= nesvnic
->logical_port
+ 1;
3794 ib_dispatch_event(&event
);
3798 nesvnic
->event_timer
.function
= NULL
;
3802 void nes_port_ibevent(struct nes_vnic
*nesvnic
)
3804 struct nes_ib_device
*nesibdev
= nesvnic
->nesibdev
;
3805 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3806 struct ib_event event
;
3807 event
.device
= &nesibdev
->ibdev
;
3808 event
.element
.port_num
= nesvnic
->logical_port
+ 1;
3809 event
.event
= nesdev
->iw_status
? IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
3811 if (!nesvnic
->event_timer
.function
) {
3812 ib_dispatch_event(&event
);
3813 nesvnic
->last_dispatched_event
= event
.event
;
3814 nesvnic
->event_timer
.function
= nes_handle_delayed_event
;
3815 nesvnic
->event_timer
.data
= (unsigned long) nesvnic
;
3816 nesvnic
->event_timer
.expires
= jiffies
+ NES_EVENT_DELAY
;
3817 add_timer(&nesvnic
->event_timer
);
3819 mod_timer(&nesvnic
->event_timer
, jiffies
+ NES_EVENT_DELAY
);
3821 nesvnic
->delayed_event
= event
.event
;
3826 * nes_destroy_ofa_device
3828 void nes_destroy_ofa_device(struct nes_ib_device
*nesibdev
)
3830 if (nesibdev
== NULL
)
3833 nes_unregister_ofa_device(nesibdev
);
3835 kfree(nesibdev
->ibdev
.iwcm
);
3836 ib_dealloc_device(&nesibdev
->ibdev
);
3841 * nes_register_ofa_device
3843 int nes_register_ofa_device(struct nes_ib_device
*nesibdev
)
3845 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
3846 struct nes_device
*nesdev
= nesvnic
->nesdev
;
3847 struct nes_adapter
*nesadapter
= nesdev
->nesadapter
;
3850 ret
= ib_register_device(&nesvnic
->nesibdev
->ibdev
, NULL
);
3855 /* Get the resources allocated to this device */
3856 nesibdev
->max_cq
= (nesadapter
->max_cq
-NES_FIRST_QPN
) / nesadapter
->port_count
;
3857 nesibdev
->max_mr
= nesadapter
->max_mr
/ nesadapter
->port_count
;
3858 nesibdev
->max_qp
= (nesadapter
->max_qp
-NES_FIRST_QPN
) / nesadapter
->port_count
;
3859 nesibdev
->max_pd
= nesadapter
->max_pd
/ nesadapter
->port_count
;
3861 for (i
= 0; i
< ARRAY_SIZE(nes_dev_attributes
); ++i
) {
3862 ret
= device_create_file(&nesibdev
->ibdev
.dev
, nes_dev_attributes
[i
]);
3866 device_remove_file(&nesibdev
->ibdev
.dev
,
3867 nes_dev_attributes
[i
]);
3869 ib_unregister_device(&nesibdev
->ibdev
);
3874 nesvnic
->of_device_registered
= 1;
3881 * nes_unregister_ofa_device
3883 static void nes_unregister_ofa_device(struct nes_ib_device
*nesibdev
)
3885 struct nes_vnic
*nesvnic
= nesibdev
->nesvnic
;
3888 for (i
= 0; i
< ARRAY_SIZE(nes_dev_attributes
); ++i
) {
3889 device_remove_file(&nesibdev
->ibdev
.dev
, nes_dev_attributes
[i
]);
3892 if (nesvnic
->of_device_registered
) {
3893 ib_unregister_device(&nesibdev
->ibdev
);
3896 nesvnic
->of_device_registered
= 0;