Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / infiniband / hw / nes / nes_verbs.c
blob162475aeeedd7ffd30ca0478e9bcbe0c16204e54
1 /*
2 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/random.h>
37 #include <linux/highmem.h>
38 #include <linux/slab.h>
39 #include <asm/byteorder.h>
41 #include <rdma/ib_verbs.h>
42 #include <rdma/iw_cm.h>
43 #include <rdma/ib_user_verbs.h>
45 #include "nes.h"
47 #include <rdma/ib_umem.h>
49 atomic_t mod_qp_timouts;
50 atomic_t qps_created;
51 atomic_t sw_qps_destroyed;
53 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
54 static int nes_dereg_mr(struct ib_mr *ib_mr);
56 /**
57 * nes_alloc_mw
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;
67 struct nes_mr *nesmr;
68 struct ib_mw *ibmw;
69 struct nes_hw_cqp_wqe *cqp_wqe;
70 int ret;
71 u32 stag;
72 u32 stag_index = 0;
73 u32 next_stag_index = 0;
74 u32 driver_key = 0;
75 u8 stag_key = 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;
83 driver_key = 0;
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);
90 if (ret) {
91 return ERR_PTR(ret);
94 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
95 if (!nesmr) {
96 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
97 return ERR_PTR(-ENOMEM);
100 stag = stag_index << 8;
101 stag |= driver_key;
102 stag += (u32)stag_key;
104 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
105 stag, stag_index);
107 /* Register the region with the adapter */
108 cqp_request = nes_get_cqp_request(nesdev);
109 if (cqp_request == NULL) {
110 kfree(nesmr);
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);
130 /* Wait for CQP */
131 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
132 NES_EVENT_TIMEOUT);
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);
138 kfree(nesmr);
139 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
140 if (!ret) {
141 return ERR_PTR(-ETIME);
142 } else {
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;
150 ibmw = &nesmr->ibmw;
151 nesmr->pbl_4k = 0;
152 nesmr->pbls_used = 0;
154 return ibmw;
159 * nes_dealloc_mw
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;
169 int err = 0;
170 int ret;
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");
176 return -ENOMEM;
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);
187 /* Wait for CQP */
188 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
189 ibmw->rkey);
190 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
191 NES_EVENT_TIMEOUT);
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);
195 if (!ret)
196 err = -ETIME;
197 else if (cqp_request->major_code)
198 err = -EIO;
200 nes_put_cqp_request(nesdev, cqp_request);
202 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
203 (ibmw->rkey & 0x0fffff00) >> 8);
204 kfree(nesmr);
206 return err;
211 * nes_alloc_fast_mr
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;
218 unsigned long flags;
219 int ret;
220 struct nes_adapter *nesadapter = nesdev->nesadapter;
221 u32 opcode = 0;
222 u16 major_code;
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");
229 return -ENOMEM;
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);
241 } else {
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);
246 return -ENOMEM;
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);
275 barrier();
277 atomic_set(&cqp_request->refcount, 2);
278 nes_post_cqp_request(nesdev, cqp_request);
280 /* Wait for CQP */
281 ret = wait_event_timeout(cqp_request->waitq,
282 (0 != cqp_request->request_done),
283 NES_EVENT_TIMEOUT);
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);
298 if (!ret)
299 return -ETIME;
300 else if (major_code)
301 return -EIO;
302 return 0;
306 * nes_alloc_mr
308 static struct ib_mr *nes_alloc_mr(struct ib_pd *ibpd,
309 enum ib_mr_type mr_type,
310 u32 max_num_sg)
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;
317 u32 next_stag_index;
318 u8 stag_key = 0;
319 u32 driver_key = 0;
320 int err = 0;
321 u32 stag_index = 0;
322 struct nes_mr *nesmr;
323 u32 stag;
324 int ret;
325 struct ib_mr *ibmr;
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);
348 if (err)
349 return ERR_PTR(err);
351 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
352 if (!nesmr) {
353 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
354 return ERR_PTR(-ENOMEM);
357 stag = stag_index << 8;
358 stag |= driver_key;
359 stag += (u32)stag_key;
361 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
362 stag, stag_index);
364 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_num_sg);
366 if (ret == 0) {
367 nesmr->ibmr.rkey = stag;
368 nesmr->ibmr.lkey = stag;
369 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
370 ibmr = &nesmr->ibmr;
371 } else {
372 kfree(nesmr);
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),
379 &nesmr->paddr);
380 if (!nesmr->paddr)
381 goto err;
383 nesmr->max_pages = max_num_sg;
385 return ibmr;
387 err:
388 nes_dereg_mr(ibmr);
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))
398 return -ENOMEM;
400 nesmr->pages[nesmr->npages++] = cpu_to_le64(addr);
402 return 0;
405 static int nes_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
406 int sg_nents, unsigned int *sg_offset)
408 struct nes_mr *nesmr = to_nesmr(ibmr);
410 nesmr->npages = 0;
412 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, nes_set_page);
416 * nes_query_device
418 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
419 struct ib_udata *uhw)
421 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
422 struct nes_device *nesdev = nesvnic->nesdev;
423 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
425 if (uhw->inlen || uhw->outlen)
426 return -EINVAL;
428 memset(props, 0, sizeof(*props));
429 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
431 props->fw_ver = nesdev->nesadapter->firmware_version;
432 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
433 props->vendor_id = nesdev->nesadapter->vendor_id;
434 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
435 props->hw_ver = nesdev->nesadapter->hw_rev;
436 props->max_mr_size = 0x80000000;
437 props->max_qp = nesibdev->max_qp;
438 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
439 props->max_sge = nesdev->nesadapter->max_sge;
440 props->max_cq = nesibdev->max_cq;
441 props->max_cqe = nesdev->nesadapter->max_cqe;
442 props->max_mr = nesibdev->max_mr;
443 props->max_mw = nesibdev->max_mr;
444 props->max_pd = nesibdev->max_pd;
445 props->max_sge_rd = 1;
446 switch (nesdev->nesadapter->max_irrq_wr) {
447 case 0:
448 props->max_qp_rd_atom = 2;
449 break;
450 case 1:
451 props->max_qp_rd_atom = 8;
452 break;
453 case 2:
454 props->max_qp_rd_atom = 32;
455 break;
456 case 3:
457 props->max_qp_rd_atom = 64;
458 break;
459 default:
460 props->max_qp_rd_atom = 0;
462 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
463 props->atomic_cap = IB_ATOMIC_NONE;
464 props->max_map_per_fmr = 1;
466 return 0;
471 * nes_query_port
473 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
475 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
476 struct net_device *netdev = nesvnic->netdev;
478 /* props being zeroed by the caller, avoid zeroing it here */
480 props->max_mtu = IB_MTU_4096;
481 props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
483 props->lid = 1;
484 if (netif_queue_stopped(netdev))
485 props->state = IB_PORT_DOWN;
486 else if (nesvnic->linkup)
487 props->state = IB_PORT_ACTIVE;
488 else
489 props->state = IB_PORT_DOWN;
490 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
491 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
492 props->gid_tbl_len = 1;
493 props->pkey_tbl_len = 1;
494 props->active_width = IB_WIDTH_4X;
495 props->active_speed = IB_SPEED_SDR;
496 props->max_msg_sz = 0x80000000;
498 return 0;
502 * nes_query_pkey
504 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
506 *pkey = 0;
507 return 0;
512 * nes_query_gid
514 static int nes_query_gid(struct ib_device *ibdev, u8 port,
515 int index, union ib_gid *gid)
517 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
519 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
520 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
522 return 0;
527 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
528 * of all objects associated with a particular user-mode client.
530 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
531 struct ib_udata *udata)
533 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
534 struct nes_device *nesdev = nesvnic->nesdev;
535 struct nes_adapter *nesadapter = nesdev->nesadapter;
536 struct nes_alloc_ucontext_req req;
537 struct nes_alloc_ucontext_resp uresp;
538 struct nes_ucontext *nes_ucontext;
539 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
542 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
543 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
544 return ERR_PTR(-EINVAL);
547 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
548 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
549 req.userspace_ver, NES_ABI_USERSPACE_VER);
550 return ERR_PTR(-EINVAL);
554 memset(&uresp, 0, sizeof uresp);
556 uresp.max_qps = nesibdev->max_qp;
557 uresp.max_pds = nesibdev->max_pd;
558 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
559 uresp.virtwq = nesadapter->virtwq;
560 uresp.kernel_ver = NES_ABI_KERNEL_VER;
562 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
563 if (!nes_ucontext)
564 return ERR_PTR(-ENOMEM);
566 nes_ucontext->nesdev = nesdev;
567 nes_ucontext->mmap_wq_offset = uresp.max_pds;
568 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
569 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
570 PAGE_SIZE;
573 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
574 kfree(nes_ucontext);
575 return ERR_PTR(-EFAULT);
578 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
579 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
580 atomic_set(&nes_ucontext->usecnt, 1);
581 return &nes_ucontext->ibucontext;
586 * nes_dealloc_ucontext
588 static int nes_dealloc_ucontext(struct ib_ucontext *context)
590 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
591 /* struct nes_device *nesdev = nesvnic->nesdev; */
592 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
594 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
595 return 0;
596 kfree(nes_ucontext);
597 return 0;
602 * nes_mmap
604 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
606 unsigned long index;
607 struct nes_vnic *nesvnic = to_nesvnic(context->device);
608 struct nes_device *nesdev = nesvnic->nesdev;
609 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
610 struct nes_ucontext *nes_ucontext;
611 struct nes_qp *nesqp;
613 nes_ucontext = to_nesucontext(context);
616 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
617 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
618 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
619 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
620 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
621 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
622 return -EFAULT;
624 nesqp = nes_ucontext->mmap_nesqp[index];
625 if (nesqp == NULL) {
626 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
627 return -EFAULT;
629 if (remap_pfn_range(vma, vma->vm_start,
630 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
631 vma->vm_end - vma->vm_start,
632 vma->vm_page_prot)) {
633 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
634 return -EAGAIN;
636 vma->vm_private_data = nesqp;
637 return 0;
638 } else {
639 index = vma->vm_pgoff;
640 if (!test_bit(index, nes_ucontext->allocated_doorbells))
641 return -EFAULT;
643 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
644 if (io_remap_pfn_range(vma, vma->vm_start,
645 (nesdev->doorbell_start +
646 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
647 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
648 return -EAGAIN;
649 vma->vm_private_data = nes_ucontext;
650 return 0;
653 return -ENOSYS;
658 * nes_alloc_pd
660 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
661 struct ib_ucontext *context, struct ib_udata *udata)
663 struct nes_pd *nespd;
664 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
665 struct nes_device *nesdev = nesvnic->nesdev;
666 struct nes_adapter *nesadapter = nesdev->nesadapter;
667 struct nes_ucontext *nesucontext;
668 struct nes_alloc_pd_resp uresp;
669 u32 pd_num = 0;
670 int err;
672 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
673 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
674 netdev_refcnt_read(nesvnic->netdev));
676 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
677 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
678 if (err) {
679 return ERR_PTR(err);
682 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
683 if (!nespd) {
684 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
685 return ERR_PTR(-ENOMEM);
688 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
689 nespd, nesvnic->nesibdev->ibdev.name);
691 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
693 if (context) {
694 nesucontext = to_nesucontext(context);
695 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
696 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
697 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
698 nespd->mmap_db_index, nespd->pd_id);
699 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
700 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
701 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
702 kfree(nespd);
703 return ERR_PTR(-ENOMEM);
706 uresp.pd_id = nespd->pd_id;
707 uresp.mmap_db_index = nespd->mmap_db_index;
708 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
709 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
710 kfree(nespd);
711 return ERR_PTR(-EFAULT);
714 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
715 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
716 nesucontext->first_free_db = nespd->mmap_db_index + 1;
719 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
720 return &nespd->ibpd;
725 * nes_dealloc_pd
727 static int nes_dealloc_pd(struct ib_pd *ibpd)
729 struct nes_ucontext *nesucontext;
730 struct nes_pd *nespd = to_nespd(ibpd);
731 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
732 struct nes_device *nesdev = nesvnic->nesdev;
733 struct nes_adapter *nesadapter = nesdev->nesadapter;
735 if ((ibpd->uobject) && (ibpd->uobject->context)) {
736 nesucontext = to_nesucontext(ibpd->uobject->context);
737 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
738 nespd->mmap_db_index);
739 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
740 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
741 if (nesucontext->first_free_db > nespd->mmap_db_index) {
742 nesucontext->first_free_db = nespd->mmap_db_index;
746 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
747 nespd->pd_id, nespd);
748 nes_free_resource(nesadapter, nesadapter->allocated_pds,
749 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
750 kfree(nespd);
752 return 0;
757 * nes_create_ah
759 static struct ib_ah *nes_create_ah(struct ib_pd *pd,
760 struct rdma_ah_attr *ah_attr,
761 struct ib_udata *udata)
763 return ERR_PTR(-ENOSYS);
768 * nes_destroy_ah
770 static int nes_destroy_ah(struct ib_ah *ah)
772 return -ENOSYS;
777 * nes_get_encoded_size
779 static inline u8 nes_get_encoded_size(int *size)
781 u8 encoded_size = 0;
782 if (*size <= 32) {
783 *size = 32;
784 encoded_size = 1;
785 } else if (*size <= 128) {
786 *size = 128;
787 encoded_size = 2;
788 } else if (*size <= 512) {
789 *size = 512;
790 encoded_size = 3;
792 return (encoded_size);
798 * nes_setup_virt_qp
800 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
801 struct nes_vnic *nesvnic, int sq_size, int rq_size)
803 unsigned long flags;
804 void *mem;
805 __le64 *pbl = NULL;
806 __le64 *tpbl;
807 __le64 *pblbuffer;
808 struct nes_device *nesdev = nesvnic->nesdev;
809 struct nes_adapter *nesadapter = nesdev->nesadapter;
810 u32 pbl_entries;
811 u8 rq_pbl_entries;
812 u8 sq_pbl_entries;
814 pbl_entries = nespbl->pbl_size >> 3;
815 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
816 nespbl->pbl_size, pbl_entries,
817 (void *)nespbl->pbl_vbase,
818 (unsigned long) nespbl->pbl_pbase);
819 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
820 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
821 /* the first pbl to be fro the rq_vbase... */
822 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
823 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
824 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
825 if (!nespbl->page) {
826 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
827 kfree(nespbl);
828 return -ENOMEM;
831 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
832 nesqp->page = nespbl->page;
833 if (!nesqp->hwqp.sq_vbase) {
834 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
835 kfree(nespbl);
836 return -ENOMEM;
839 /* Now to get to sq.. we need to calculate how many */
840 /* PBL entries were used by the rq.. */
841 pbl += sq_pbl_entries;
842 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
843 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
844 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
846 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
847 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
848 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
849 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
850 if (!nesadapter->free_256pbl) {
851 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
852 nespbl->pbl_pbase);
853 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
854 kunmap(nesqp->page);
855 kfree(nespbl);
856 return -ENOMEM;
858 nesadapter->free_256pbl--;
859 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
861 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
862 pblbuffer = nesqp->pbl_vbase;
863 if (!nesqp->pbl_vbase) {
864 /* memory allocated during nes_reg_user_mr() */
865 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
866 nespbl->pbl_pbase);
867 kfree(nespbl);
868 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
869 nesadapter->free_256pbl++;
870 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
871 kunmap(nesqp->page);
872 return -ENOMEM;
874 memset(nesqp->pbl_vbase, 0, 256);
875 /* fill in the page address in the pbl buffer.. */
876 tpbl = pblbuffer + 16;
877 pbl = (__le64 *)nespbl->pbl_vbase;
878 while (sq_pbl_entries--)
879 *tpbl++ = *pbl++;
880 tpbl = pblbuffer;
881 while (rq_pbl_entries--)
882 *tpbl++ = *pbl++;
884 /* done with memory allocated during nes_reg_user_mr() */
885 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
886 nespbl->pbl_pbase);
887 kfree(nespbl);
889 nesqp->qp_mem_size =
890 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
891 /* Round up to a multiple of a page */
892 nesqp->qp_mem_size += PAGE_SIZE - 1;
893 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
895 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
896 &nesqp->hwqp.q2_pbase);
898 if (!mem) {
899 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
900 nesqp->pbl_vbase = NULL;
901 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
902 nesadapter->free_256pbl++;
903 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
904 kunmap(nesqp->page);
905 return -ENOMEM;
907 nesqp->sq_kmapped = 1;
908 nesqp->hwqp.q2_vbase = mem;
909 mem += 256;
910 memset(nesqp->hwqp.q2_vbase, 0, 256);
911 nesqp->nesqp_context = mem;
912 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
913 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
915 return 0;
920 * nes_setup_mmap_qp
922 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
923 int sq_size, int rq_size)
925 void *mem;
926 struct nes_device *nesdev = nesvnic->nesdev;
928 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
929 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
930 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
931 256; /* this is Q2 */
932 /* Round up to a multiple of a page */
933 nesqp->qp_mem_size += PAGE_SIZE - 1;
934 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
936 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
937 &nesqp->hwqp.sq_pbase);
938 if (!mem)
939 return -ENOMEM;
940 nes_debug(NES_DBG_QP, "PCI consistent memory for "
941 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
942 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
944 memset(mem, 0, nesqp->qp_mem_size);
946 nesqp->hwqp.sq_vbase = mem;
947 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
949 nesqp->hwqp.rq_vbase = mem;
950 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
951 sizeof(struct nes_hw_qp_wqe) * sq_size;
952 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
954 nesqp->hwqp.q2_vbase = mem;
955 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
956 sizeof(struct nes_hw_qp_wqe) * rq_size;
957 mem += 256;
958 memset(nesqp->hwqp.q2_vbase, 0, 256);
960 nesqp->nesqp_context = mem;
961 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
962 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
963 return 0;
968 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
970 static void nes_free_qp_mem(struct nes_device *nesdev,
971 struct nes_qp *nesqp, int virt_wqs)
973 unsigned long flags;
974 struct nes_adapter *nesadapter = nesdev->nesadapter;
975 if (!virt_wqs) {
976 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
977 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
978 }else {
979 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
980 nesadapter->free_256pbl++;
981 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
982 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
983 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
984 nesqp->pbl_vbase = NULL;
985 if (nesqp->sq_kmapped) {
986 nesqp->sq_kmapped = 0;
987 kunmap(nesqp->page);
994 * nes_create_qp
996 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
997 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
999 u64 u64temp= 0;
1000 u64 u64nesqp = 0;
1001 struct nes_pd *nespd = to_nespd(ibpd);
1002 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1003 struct nes_device *nesdev = nesvnic->nesdev;
1004 struct nes_adapter *nesadapter = nesdev->nesadapter;
1005 struct nes_qp *nesqp;
1006 struct nes_cq *nescq;
1007 struct nes_ucontext *nes_ucontext;
1008 struct nes_hw_cqp_wqe *cqp_wqe;
1009 struct nes_cqp_request *cqp_request;
1010 struct nes_create_qp_req req;
1011 struct nes_create_qp_resp uresp;
1012 struct nes_pbl *nespbl = NULL;
1013 u32 qp_num = 0;
1014 u32 opcode = 0;
1015 /* u32 counter = 0; */
1016 void *mem;
1017 unsigned long flags;
1018 int ret;
1019 int err;
1020 int virt_wqs = 0;
1021 int sq_size;
1022 int rq_size;
1023 u8 sq_encoded_size;
1024 u8 rq_encoded_size;
1025 /* int counter; */
1027 if (init_attr->create_flags)
1028 return ERR_PTR(-EINVAL);
1030 atomic_inc(&qps_created);
1031 switch (init_attr->qp_type) {
1032 case IB_QPT_RC:
1033 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1034 init_attr->cap.max_inline_data = 0;
1035 } else {
1036 init_attr->cap.max_inline_data = 64;
1038 sq_size = init_attr->cap.max_send_wr;
1039 rq_size = init_attr->cap.max_recv_wr;
1041 /* check if the encoded sizes are OK or not... */
1042 sq_encoded_size = nes_get_encoded_size(&sq_size);
1043 rq_encoded_size = nes_get_encoded_size(&rq_size);
1045 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1046 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1047 rq_size, sq_size);
1048 return ERR_PTR(-EINVAL);
1051 init_attr->cap.max_send_wr = sq_size -2;
1052 init_attr->cap.max_recv_wr = rq_size -1;
1053 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1055 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1056 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
1057 if (ret) {
1058 return ERR_PTR(ret);
1061 /* Need 512 (actually now 1024) byte alignment on this structure */
1062 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1063 if (!mem) {
1064 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1065 return ERR_PTR(-ENOMEM);
1067 u64nesqp = (unsigned long)mem;
1068 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1069 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1070 u64nesqp &= ~u64temp;
1071 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1072 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1073 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1074 nesqp->allocated_buffer = mem;
1076 if (udata) {
1077 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1078 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1079 kfree(nesqp->allocated_buffer);
1080 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1081 return ERR_PTR(-EFAULT);
1083 if (req.user_wqe_buffers) {
1084 virt_wqs = 1;
1086 if (req.user_qp_buffer)
1087 nesqp->nesuqp_addr = req.user_qp_buffer;
1088 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1089 nesqp->user_mode = 1;
1090 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1091 if (virt_wqs) {
1092 err = 1;
1093 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1094 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1095 list_del(&nespbl->list);
1096 err = 0;
1097 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1098 nespbl, nespbl->user_base);
1099 break;
1102 if (err) {
1103 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1104 (long long unsigned int)req.user_wqe_buffers);
1105 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1106 kfree(nesqp->allocated_buffer);
1107 return ERR_PTR(-EFAULT);
1111 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1112 nesqp->mmap_sq_db_index =
1113 find_next_zero_bit(nes_ucontext->allocated_wqs,
1114 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1115 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1116 nespd->mmap_db_index); */
1117 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1118 nes_debug(NES_DBG_QP,
1119 "db index > max user regions, failing create QP\n");
1120 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1121 if (virt_wqs) {
1122 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1123 nespbl->pbl_pbase);
1124 kfree(nespbl);
1126 kfree(nesqp->allocated_buffer);
1127 return ERR_PTR(-ENOMEM);
1129 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1130 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1131 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1132 } else {
1133 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1134 kfree(nesqp->allocated_buffer);
1135 return ERR_PTR(-EFAULT);
1138 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1139 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1140 if (err) {
1141 nes_debug(NES_DBG_QP,
1142 "error geting qp mem code = %d\n", err);
1143 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1144 kfree(nesqp->allocated_buffer);
1145 return ERR_PTR(-ENOMEM);
1148 nesqp->hwqp.sq_size = sq_size;
1149 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1150 nesqp->hwqp.sq_head = 1;
1151 nesqp->hwqp.rq_size = rq_size;
1152 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1153 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1154 (void *)nesqp->nesqp_context_pbase);
1156 nesqp->hwqp.qp_id = qp_num;
1157 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1158 nesqp->nespd = nespd;
1160 nescq = to_nescq(init_attr->send_cq);
1161 nesqp->nesscq = nescq;
1162 nescq = to_nescq(init_attr->recv_cq);
1163 nesqp->nesrcq = nescq;
1165 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1166 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1167 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1168 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1169 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1170 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1171 if (!udata) {
1172 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1173 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1175 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1176 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1177 u64temp = (u64)nesqp->hwqp.sq_pbase;
1178 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1179 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1182 if (!virt_wqs) {
1183 u64temp = (u64)nesqp->hwqp.sq_pbase;
1184 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1185 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1186 u64temp = (u64)nesqp->hwqp.rq_pbase;
1187 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1188 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1189 } else {
1190 u64temp = (u64)nesqp->pbl_pbase;
1191 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1192 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1195 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1196 nesvnic->next_qp_nic_index,
1197 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1198 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1199 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1200 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1201 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1202 nesvnic->next_qp_nic_index++;
1203 if ((nesvnic->next_qp_nic_index > 3) ||
1204 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1205 nesvnic->next_qp_nic_index = 0;
1207 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1209 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1210 u64temp = (u64)nesqp->hwqp.q2_pbase;
1211 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1212 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1213 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1214 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1215 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1216 NES_QPCONTEXT_ORDIRD_AAH |
1217 ((((u32)nesadapter->max_irrq_wr) <<
1218 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1219 if (disable_mpa_crc) {
1220 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1221 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1225 /* Create the QP */
1226 cqp_request = nes_get_cqp_request(nesdev);
1227 if (cqp_request == NULL) {
1228 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1229 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1230 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1231 kfree(nesqp->allocated_buffer);
1232 return ERR_PTR(-ENOMEM);
1234 cqp_request->waiting = 1;
1235 cqp_wqe = &cqp_request->cqp_wqe;
1237 if (!virt_wqs) {
1238 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1239 NES_CQP_QP_IWARP_STATE_IDLE;
1240 } else {
1241 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1242 NES_CQP_QP_IWARP_STATE_IDLE;
1244 opcode |= NES_CQP_QP_CQS_VALID;
1245 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1246 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1247 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1249 u64temp = (u64)nesqp->nesqp_context_pbase;
1250 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1252 atomic_set(&cqp_request->refcount, 2);
1253 nes_post_cqp_request(nesdev, cqp_request);
1255 /* Wait for CQP */
1256 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1257 nesqp->hwqp.qp_id);
1258 ret = wait_event_timeout(cqp_request->waitq,
1259 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1260 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1261 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1262 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1263 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1264 cqp_request->major_code, cqp_request->minor_code);
1265 if ((!ret) || (cqp_request->major_code)) {
1266 nes_put_cqp_request(nesdev, cqp_request);
1267 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1268 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1269 kfree(nesqp->allocated_buffer);
1270 if (!ret) {
1271 return ERR_PTR(-ETIME);
1272 } else {
1273 return ERR_PTR(-EIO);
1277 nes_put_cqp_request(nesdev, cqp_request);
1279 if (ibpd->uobject) {
1280 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1281 uresp.mmap_rq_db_index = 0;
1282 uresp.actual_sq_size = sq_size;
1283 uresp.actual_rq_size = rq_size;
1284 uresp.qp_id = nesqp->hwqp.qp_id;
1285 uresp.nes_drv_opt = nes_drv_opt;
1286 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1287 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1288 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1289 kfree(nesqp->allocated_buffer);
1290 return ERR_PTR(-EFAULT);
1294 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1295 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1296 spin_lock_init(&nesqp->lock);
1297 nes_add_ref(&nesqp->ibqp);
1298 break;
1299 default:
1300 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1301 return ERR_PTR(-EINVAL);
1303 init_completion(&nesqp->sq_drained);
1304 init_completion(&nesqp->rq_drained);
1306 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1307 timer_setup(&nesqp->terminate_timer, nes_terminate_timeout, 0);
1309 /* update the QP table */
1310 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1311 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1312 netdev_refcnt_read(nesvnic->netdev));
1314 return &nesqp->ibqp;
1318 * nes_clean_cq
1320 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1322 u32 cq_head;
1323 u32 lo;
1324 u32 hi;
1325 u64 u64temp;
1326 unsigned long flags = 0;
1328 spin_lock_irqsave(&nescq->lock, flags);
1330 cq_head = nescq->hw_cq.cq_head;
1331 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1332 rmb();
1333 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1334 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1335 u64temp = (((u64)hi) << 32) | ((u64)lo);
1336 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1337 if (u64temp == (u64)(unsigned long)nesqp) {
1338 /* Zero the context value so cqe will be ignored */
1339 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1340 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1343 if (++cq_head >= nescq->hw_cq.cq_size)
1344 cq_head = 0;
1347 spin_unlock_irqrestore(&nescq->lock, flags);
1352 * nes_destroy_qp
1354 static int nes_destroy_qp(struct ib_qp *ibqp)
1356 struct nes_qp *nesqp = to_nesqp(ibqp);
1357 struct nes_ucontext *nes_ucontext;
1358 struct ib_qp_attr attr;
1359 struct iw_cm_id *cm_id;
1360 struct iw_cm_event cm_event;
1361 int ret = 0;
1363 atomic_inc(&sw_qps_destroyed);
1364 nesqp->destroyed = 1;
1366 /* Blow away the connection if it exists. */
1367 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1368 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1369 attr.qp_state = IB_QPS_ERR;
1370 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1373 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1374 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1375 cm_id = nesqp->cm_id;
1376 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1377 cm_event.status = -ETIMEDOUT;
1378 cm_event.local_addr = cm_id->local_addr;
1379 cm_event.remote_addr = cm_id->remote_addr;
1380 cm_event.private_data = NULL;
1381 cm_event.private_data_len = 0;
1383 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1384 "QP%u. cm_id = %p, refcount = %u. \n",
1385 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1387 cm_id->rem_ref(cm_id);
1388 ret = cm_id->event_handler(cm_id, &cm_event);
1389 if (ret)
1390 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1393 if (nesqp->user_mode) {
1394 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1395 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1396 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1397 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1398 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1399 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1402 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1403 nesqp->sq_kmapped = 0;
1404 kunmap(nesqp->page);
1406 } else {
1407 /* Clean any pending completions from the cq(s) */
1408 if (nesqp->nesscq)
1409 nes_clean_cq(nesqp, nesqp->nesscq);
1411 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1412 nes_clean_cq(nesqp, nesqp->nesrcq);
1414 nes_rem_ref(&nesqp->ibqp);
1415 return 0;
1420 * nes_create_cq
1422 static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1423 const struct ib_cq_init_attr *attr,
1424 struct ib_ucontext *context,
1425 struct ib_udata *udata)
1427 int entries = attr->cqe;
1428 u64 u64temp;
1429 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1430 struct nes_device *nesdev = nesvnic->nesdev;
1431 struct nes_adapter *nesadapter = nesdev->nesadapter;
1432 struct nes_cq *nescq;
1433 struct nes_ucontext *nes_ucontext = NULL;
1434 struct nes_cqp_request *cqp_request;
1435 void *mem = NULL;
1436 struct nes_hw_cqp_wqe *cqp_wqe;
1437 struct nes_pbl *nespbl = NULL;
1438 struct nes_create_cq_req req;
1439 struct nes_create_cq_resp resp;
1440 u32 cq_num = 0;
1441 u32 opcode = 0;
1442 u32 pbl_entries = 1;
1443 int err;
1444 unsigned long flags;
1445 int ret;
1447 if (attr->flags)
1448 return ERR_PTR(-EINVAL);
1450 if (entries > nesadapter->max_cqe)
1451 return ERR_PTR(-EINVAL);
1453 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1454 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1455 if (err) {
1456 return ERR_PTR(err);
1459 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1460 if (!nescq) {
1461 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1462 return ERR_PTR(-ENOMEM);
1465 nescq->hw_cq.cq_size = max(entries + 1, 5);
1466 nescq->hw_cq.cq_number = cq_num;
1467 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1470 if (context) {
1471 nes_ucontext = to_nesucontext(context);
1472 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1473 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1474 kfree(nescq);
1475 return ERR_PTR(-EFAULT);
1477 nesvnic->mcrq_ucontext = nes_ucontext;
1478 nes_ucontext->mcrqf = req.mcrqf;
1479 if (nes_ucontext->mcrqf) {
1480 if (nes_ucontext->mcrqf & 0x80000000)
1481 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1482 else if (nes_ucontext->mcrqf & 0x40000000)
1483 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1484 else
1485 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1486 nescq->mcrqf = nes_ucontext->mcrqf;
1487 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1489 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1490 (unsigned long)req.user_cq_buffer, entries);
1491 err = 1;
1492 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1493 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1494 list_del(&nespbl->list);
1495 err = 0;
1496 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1497 nespbl);
1498 break;
1501 if (err) {
1502 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1503 kfree(nescq);
1504 return ERR_PTR(-EFAULT);
1507 pbl_entries = nespbl->pbl_size >> 3;
1508 nescq->cq_mem_size = 0;
1509 } else {
1510 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1511 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1512 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1514 /* allocate the physical buffer space */
1515 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1516 &nescq->hw_cq.cq_pbase);
1517 if (!mem) {
1518 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1519 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1520 kfree(nescq);
1521 return ERR_PTR(-ENOMEM);
1524 nescq->hw_cq.cq_vbase = mem;
1525 nescq->hw_cq.cq_head = 0;
1526 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1527 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1528 (u32)nescq->hw_cq.cq_pbase);
1531 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1532 spin_lock_init(&nescq->lock);
1534 /* send CreateCQ request to CQP */
1535 cqp_request = nes_get_cqp_request(nesdev);
1536 if (cqp_request == NULL) {
1537 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1538 if (!context)
1539 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1540 nescq->hw_cq.cq_pbase);
1541 else {
1542 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1543 nespbl->pbl_vbase, nespbl->pbl_pbase);
1544 kfree(nespbl);
1547 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1548 kfree(nescq);
1549 return ERR_PTR(-ENOMEM);
1551 cqp_request->waiting = 1;
1552 cqp_wqe = &cqp_request->cqp_wqe;
1554 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1555 NES_CQP_CQ_CHK_OVERFLOW |
1556 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1558 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1560 if (pbl_entries != 1) {
1561 if (pbl_entries > 32) {
1562 /* use 4k pbl */
1563 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1564 if (nesadapter->free_4kpbl == 0) {
1565 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1566 nes_free_cqp_request(nesdev, cqp_request);
1567 if (!context)
1568 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1569 nescq->hw_cq.cq_pbase);
1570 else {
1571 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1572 nespbl->pbl_vbase, nespbl->pbl_pbase);
1573 kfree(nespbl);
1575 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1576 kfree(nescq);
1577 return ERR_PTR(-ENOMEM);
1578 } else {
1579 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1580 nescq->virtual_cq = 2;
1581 nesadapter->free_4kpbl--;
1583 } else {
1584 /* use 256 byte pbl */
1585 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1586 if (nesadapter->free_256pbl == 0) {
1587 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1588 nes_free_cqp_request(nesdev, cqp_request);
1589 if (!context)
1590 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1591 nescq->hw_cq.cq_pbase);
1592 else {
1593 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1594 nespbl->pbl_vbase, nespbl->pbl_pbase);
1595 kfree(nespbl);
1597 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1598 kfree(nescq);
1599 return ERR_PTR(-ENOMEM);
1600 } else {
1601 opcode |= NES_CQP_CQ_VIRT;
1602 nescq->virtual_cq = 1;
1603 nesadapter->free_256pbl--;
1608 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1610 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1611 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1612 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1613 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1615 if (context) {
1616 if (pbl_entries != 1)
1617 u64temp = (u64)nespbl->pbl_pbase;
1618 else
1619 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1620 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1621 nes_ucontext->mmap_db_index[0]);
1622 } else {
1623 u64temp = (u64)nescq->hw_cq.cq_pbase;
1624 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1626 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1627 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1628 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1629 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1630 cpu_to_le32((u32)(u64temp >> 1));
1631 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1632 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1634 atomic_set(&cqp_request->refcount, 2);
1635 nes_post_cqp_request(nesdev, cqp_request);
1637 /* Wait for CQP */
1638 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1639 nescq->hw_cq.cq_number);
1640 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1641 NES_EVENT_TIMEOUT * 2);
1642 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1643 nescq->hw_cq.cq_number, ret);
1644 if ((!ret) || (cqp_request->major_code)) {
1645 nes_put_cqp_request(nesdev, cqp_request);
1646 if (!context)
1647 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1648 nescq->hw_cq.cq_pbase);
1649 else {
1650 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1651 nespbl->pbl_vbase, nespbl->pbl_pbase);
1652 kfree(nespbl);
1654 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1655 kfree(nescq);
1656 return ERR_PTR(-EIO);
1658 nes_put_cqp_request(nesdev, cqp_request);
1660 if (context) {
1661 /* free the nespbl */
1662 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1663 nespbl->pbl_pbase);
1664 kfree(nespbl);
1665 resp.cq_id = nescq->hw_cq.cq_number;
1666 resp.cq_size = nescq->hw_cq.cq_size;
1667 resp.mmap_db_index = 0;
1668 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
1669 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1670 kfree(nescq);
1671 return ERR_PTR(-EFAULT);
1675 return &nescq->ibcq;
1680 * nes_destroy_cq
1682 static int nes_destroy_cq(struct ib_cq *ib_cq)
1684 struct nes_cq *nescq;
1685 struct nes_device *nesdev;
1686 struct nes_vnic *nesvnic;
1687 struct nes_adapter *nesadapter;
1688 struct nes_hw_cqp_wqe *cqp_wqe;
1689 struct nes_cqp_request *cqp_request;
1690 unsigned long flags;
1691 u32 opcode = 0;
1692 int ret;
1694 if (ib_cq == NULL)
1695 return 0;
1697 nescq = to_nescq(ib_cq);
1698 nesvnic = to_nesvnic(ib_cq->device);
1699 nesdev = nesvnic->nesdev;
1700 nesadapter = nesdev->nesadapter;
1702 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1704 /* Send DestroyCQ request to CQP */
1705 cqp_request = nes_get_cqp_request(nesdev);
1706 if (cqp_request == NULL) {
1707 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1708 return -ENOMEM;
1710 cqp_request->waiting = 1;
1711 cqp_wqe = &cqp_request->cqp_wqe;
1712 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1713 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1714 if (nescq->virtual_cq == 1) {
1715 nesadapter->free_256pbl++;
1716 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1717 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1718 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1720 } else if (nescq->virtual_cq == 2) {
1721 nesadapter->free_4kpbl++;
1722 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1723 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1724 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1726 opcode |= NES_CQP_CQ_4KB_CHUNK;
1729 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1731 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1732 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1733 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1734 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1735 if (!nescq->mcrqf)
1736 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1738 atomic_set(&cqp_request->refcount, 2);
1739 nes_post_cqp_request(nesdev, cqp_request);
1741 /* Wait for CQP */
1742 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1743 nescq->hw_cq.cq_number);
1744 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1745 NES_EVENT_TIMEOUT);
1746 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1747 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1748 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1749 cqp_request->minor_code);
1750 if (!ret) {
1751 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1752 nescq->hw_cq.cq_number);
1753 ret = -ETIME;
1754 } else if (cqp_request->major_code) {
1755 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1756 nescq->hw_cq.cq_number);
1757 ret = -EIO;
1758 } else {
1759 ret = 0;
1761 nes_put_cqp_request(nesdev, cqp_request);
1763 if (nescq->cq_mem_size)
1764 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1765 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1766 kfree(nescq);
1768 return ret;
1772 * root_256
1774 static u32 root_256(struct nes_device *nesdev,
1775 struct nes_root_vpbl *root_vpbl,
1776 struct nes_root_vpbl *new_root,
1777 u16 pbl_count_4k)
1779 u64 leaf_pbl;
1780 int i, j, k;
1782 if (pbl_count_4k == 1) {
1783 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1784 512, &new_root->pbl_pbase);
1786 if (new_root->pbl_vbase == NULL)
1787 return 0;
1789 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1790 for (i = 0; i < 16; i++) {
1791 new_root->pbl_vbase[i].pa_low =
1792 cpu_to_le32((u32)leaf_pbl);
1793 new_root->pbl_vbase[i].pa_high =
1794 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1795 leaf_pbl += 256;
1797 } else {
1798 for (i = 3; i >= 0; i--) {
1799 j = i * 16;
1800 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1801 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1802 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1803 << 32);
1804 for (k = 1; k < 16; k++) {
1805 leaf_pbl += 256;
1806 root_vpbl->pbl_vbase[j + k].pa_low =
1807 cpu_to_le32((u32)leaf_pbl);
1808 root_vpbl->pbl_vbase[j + k].pa_high =
1809 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1814 return 1;
1819 * nes_reg_mr
1821 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1822 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1823 dma_addr_t single_buffer, u16 pbl_count_4k,
1824 u16 residual_page_count_4k, int acc, u64 *iova_start,
1825 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1827 struct nes_hw_cqp_wqe *cqp_wqe;
1828 struct nes_cqp_request *cqp_request;
1829 unsigned long flags;
1830 int ret;
1831 struct nes_adapter *nesadapter = nesdev->nesadapter;
1832 uint pg_cnt = 0;
1833 u16 pbl_count_256 = 0;
1834 u16 pbl_count = 0;
1835 u8 use_256_pbls = 0;
1836 u8 use_4k_pbls = 0;
1837 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1838 struct nes_root_vpbl new_root = { 0, NULL, NULL };
1839 u32 opcode = 0;
1840 u16 major_code;
1842 /* Register the region with the adapter */
1843 cqp_request = nes_get_cqp_request(nesdev);
1844 if (cqp_request == NULL) {
1845 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1846 return -ENOMEM;
1848 cqp_request->waiting = 1;
1849 cqp_wqe = &cqp_request->cqp_wqe;
1851 if (pbl_count_4k) {
1852 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1854 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1855 pbl_count_256 = (pg_cnt + 31) / 32;
1856 if (pg_cnt <= 32) {
1857 if (pbl_count_256 <= nesadapter->free_256pbl)
1858 use_256_pbls = 1;
1859 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1860 use_4k_pbls = 1;
1861 } else if (pg_cnt <= 2048) {
1862 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1863 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1864 use_4k_pbls = 1;
1865 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1866 use_256_pbls = 1;
1867 use_two_level = 1;
1868 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1869 use_4k_pbls = 1;
1871 } else {
1872 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1873 use_4k_pbls = 1;
1876 if (use_256_pbls) {
1877 pbl_count = pbl_count_256;
1878 nesadapter->free_256pbl -= pbl_count + use_two_level;
1879 } else if (use_4k_pbls) {
1880 pbl_count = pbl_count_4k;
1881 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1882 } else {
1883 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1884 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1885 nes_free_cqp_request(nesdev, cqp_request);
1886 return -ENOMEM;
1889 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1892 if (use_256_pbls && use_two_level) {
1893 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1894 if (new_root.pbl_pbase != 0)
1895 root_vpbl = &new_root;
1896 } else {
1897 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1898 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1899 use_256_pbls = 0;
1901 if (pbl_count_4k == 1)
1902 use_two_level = 0;
1903 pbl_count = pbl_count_4k;
1905 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1906 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1907 use_4k_pbls = 1;
1909 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1911 if (use_4k_pbls == 0)
1912 return -ENOMEM;
1916 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1917 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1918 if (acc & IB_ACCESS_LOCAL_WRITE)
1919 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1920 if (acc & IB_ACCESS_REMOTE_WRITE)
1921 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1922 if (acc & IB_ACCESS_REMOTE_READ)
1923 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1924 if (acc & IB_ACCESS_MW_BIND)
1925 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1927 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1928 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1929 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1930 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1932 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1933 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1934 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1935 cpu_to_le32(nespd->pd_id & 0x00007fff);
1936 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1938 if (pbl_count == 0) {
1939 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1940 } else {
1941 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1942 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1943 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
1945 if (use_4k_pbls)
1946 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1948 barrier();
1950 atomic_set(&cqp_request->refcount, 2);
1951 nes_post_cqp_request(nesdev, cqp_request);
1953 /* Wait for CQP */
1954 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1955 NES_EVENT_TIMEOUT);
1956 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1957 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1958 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1959 major_code = cqp_request->major_code;
1960 nes_put_cqp_request(nesdev, cqp_request);
1962 if ((!ret || major_code) && pbl_count != 0) {
1963 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1964 if (use_256_pbls)
1965 nesadapter->free_256pbl += pbl_count + use_two_level;
1966 else if (use_4k_pbls)
1967 nesadapter->free_4kpbl += pbl_count + use_two_level;
1968 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1970 if (new_root.pbl_pbase)
1971 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1972 new_root.pbl_pbase);
1974 if (!ret)
1975 return -ETIME;
1976 else if (major_code)
1977 return -EIO;
1979 *actual_pbl_cnt = pbl_count + use_two_level;
1980 *used_4k_pbls = use_4k_pbls;
1981 return 0;
1986 * nes_reg_phys_mr
1988 struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
1989 int acc, u64 *iova_start)
1991 u64 region_length;
1992 struct nes_pd *nespd = to_nespd(ib_pd);
1993 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
1994 struct nes_device *nesdev = nesvnic->nesdev;
1995 struct nes_adapter *nesadapter = nesdev->nesadapter;
1996 struct nes_mr *nesmr;
1997 struct ib_mr *ibmr;
1998 struct nes_vpbl vpbl;
1999 struct nes_root_vpbl root_vpbl;
2000 u32 stag;
2001 unsigned long mask;
2002 u32 stag_index = 0;
2003 u32 next_stag_index = 0;
2004 u32 driver_key = 0;
2005 int err = 0;
2006 int ret = 0;
2007 u16 pbl_count = 0;
2008 u8 single_page = 1;
2009 u8 stag_key = 0;
2011 region_length = 0;
2012 vpbl.pbl_vbase = NULL;
2013 root_vpbl.pbl_vbase = NULL;
2014 root_vpbl.pbl_pbase = 0;
2016 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2017 stag_key = (u8)next_stag_index;
2019 driver_key = 0;
2021 next_stag_index >>= 8;
2022 next_stag_index %= nesadapter->max_mr;
2024 if ((addr ^ *iova_start) & ~PAGE_MASK)
2025 return ERR_PTR(-EINVAL);
2027 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2028 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
2029 if (err) {
2030 return ERR_PTR(err);
2033 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2034 if (!nesmr) {
2035 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2036 return ERR_PTR(-ENOMEM);
2039 /* Allocate a 4K buffer for the PBL */
2040 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2041 &vpbl.pbl_pbase);
2042 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2043 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2044 if (!vpbl.pbl_vbase) {
2045 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2046 ibmr = ERR_PTR(-ENOMEM);
2047 kfree(nesmr);
2048 goto reg_phys_err;
2052 mask = !size;
2054 if (mask & ~PAGE_MASK) {
2055 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2056 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2057 ibmr = ERR_PTR(-EINVAL);
2058 kfree(nesmr);
2059 goto reg_phys_err;
2062 region_length += size;
2063 vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2064 vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2066 stag = stag_index << 8;
2067 stag |= driver_key;
2068 stag += (u32)stag_key;
2070 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2071 " length = 0x%016lX, index = 0x%08X\n",
2072 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2074 /* Make the leaf PBL the root if only one PBL */
2075 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2077 if (single_page) {
2078 pbl_count = 0;
2079 } else {
2080 pbl_count = 1;
2082 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2083 addr, pbl_count, 1, acc, iova_start,
2084 &nesmr->pbls_used, &nesmr->pbl_4k);
2086 if (ret == 0) {
2087 nesmr->ibmr.rkey = stag;
2088 nesmr->ibmr.lkey = stag;
2089 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2090 ibmr = &nesmr->ibmr;
2091 } else {
2092 kfree(nesmr);
2093 ibmr = ERR_PTR(-ENOMEM);
2096 reg_phys_err:
2097 /* single PBL case */
2098 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2099 return ibmr;
2104 * nes_get_dma_mr
2106 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2108 u64 kva = 0;
2110 nes_debug(NES_DBG_MR, "\n");
2112 return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
2116 * nes_reg_user_mr
2118 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2119 u64 virt, int acc, struct ib_udata *udata)
2121 u64 iova_start;
2122 __le64 *pbl;
2123 u64 region_length;
2124 dma_addr_t last_dma_addr = 0;
2125 dma_addr_t first_dma_addr = 0;
2126 struct nes_pd *nespd = to_nespd(pd);
2127 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2128 struct nes_device *nesdev = nesvnic->nesdev;
2129 struct nes_adapter *nesadapter = nesdev->nesadapter;
2130 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2131 struct scatterlist *sg;
2132 struct nes_ucontext *nes_ucontext;
2133 struct nes_pbl *nespbl;
2134 struct nes_mr *nesmr;
2135 struct ib_umem *region;
2136 struct nes_mem_reg_req req;
2137 struct nes_vpbl vpbl;
2138 struct nes_root_vpbl root_vpbl;
2139 int entry, page_index;
2140 int page_count = 0;
2141 int err, pbl_depth = 0;
2142 int chunk_pages;
2143 int ret;
2144 u32 stag;
2145 u32 stag_index = 0;
2146 u32 next_stag_index;
2147 u32 driver_key;
2148 u32 root_pbl_index = 0;
2149 u32 cur_pbl_index = 0;
2150 u32 skip_pages;
2151 u16 pbl_count;
2152 u8 single_page = 1;
2153 u8 stag_key;
2154 int first_page = 1;
2156 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2157 if (IS_ERR(region)) {
2158 return (struct ib_mr *)region;
2161 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2162 " offset = %u, page size = %lu.\n",
2163 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2164 ib_umem_offset(region), BIT(region->page_shift));
2166 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
2168 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2169 ib_umem_release(region);
2170 return ERR_PTR(-EFAULT);
2172 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2174 switch (req.reg_type) {
2175 case IWNES_MEMREG_TYPE_MEM:
2176 pbl_depth = 0;
2177 region_length = 0;
2178 vpbl.pbl_vbase = NULL;
2179 root_vpbl.pbl_vbase = NULL;
2180 root_vpbl.pbl_pbase = 0;
2182 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2183 stag_key = (u8)next_stag_index;
2185 driver_key = next_stag_index & 0x70000000;
2187 next_stag_index >>= 8;
2188 next_stag_index %= nesadapter->max_mr;
2190 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2191 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2192 if (err) {
2193 ib_umem_release(region);
2194 return ERR_PTR(err);
2197 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2198 if (!nesmr) {
2199 ib_umem_release(region);
2200 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2201 return ERR_PTR(-ENOMEM);
2203 nesmr->region = region;
2205 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2206 if (sg_dma_address(sg) & ~PAGE_MASK) {
2207 ib_umem_release(region);
2208 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2209 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2210 (unsigned int) sg_dma_address(sg));
2211 ibmr = ERR_PTR(-EINVAL);
2212 kfree(nesmr);
2213 goto reg_user_mr_err;
2216 if (!sg_dma_len(sg)) {
2217 ib_umem_release(region);
2218 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2219 stag_index);
2220 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2221 ibmr = ERR_PTR(-EINVAL);
2222 kfree(nesmr);
2223 goto reg_user_mr_err;
2226 region_length += sg_dma_len(sg);
2227 chunk_pages = sg_dma_len(sg) >> 12;
2228 region_length -= skip_pages << 12;
2229 for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2230 skip_pages = 0;
2231 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
2232 goto enough_pages;
2233 if ((page_count&0x01FF) == 0) {
2234 if (page_count >= 1024 * 512) {
2235 ib_umem_release(region);
2236 nes_free_resource(nesadapter,
2237 nesadapter->allocated_mrs, stag_index);
2238 kfree(nesmr);
2239 ibmr = ERR_PTR(-E2BIG);
2240 goto reg_user_mr_err;
2242 if (root_pbl_index == 1) {
2243 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2244 8192, &root_vpbl.pbl_pbase);
2245 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2246 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2247 if (!root_vpbl.pbl_vbase) {
2248 ib_umem_release(region);
2249 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2250 vpbl.pbl_pbase);
2251 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2252 stag_index);
2253 kfree(nesmr);
2254 ibmr = ERR_PTR(-ENOMEM);
2255 goto reg_user_mr_err;
2257 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2258 GFP_KERNEL);
2259 if (!root_vpbl.leaf_vpbl) {
2260 ib_umem_release(region);
2261 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2262 root_vpbl.pbl_pbase);
2263 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2264 vpbl.pbl_pbase);
2265 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2266 stag_index);
2267 kfree(nesmr);
2268 ibmr = ERR_PTR(-ENOMEM);
2269 goto reg_user_mr_err;
2271 root_vpbl.pbl_vbase[0].pa_low =
2272 cpu_to_le32((u32)vpbl.pbl_pbase);
2273 root_vpbl.pbl_vbase[0].pa_high =
2274 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2275 root_vpbl.leaf_vpbl[0] = vpbl;
2277 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2278 &vpbl.pbl_pbase);
2279 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2280 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2281 if (!vpbl.pbl_vbase) {
2282 ib_umem_release(region);
2283 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2284 ibmr = ERR_PTR(-ENOMEM);
2285 kfree(nesmr);
2286 goto reg_user_mr_err;
2288 if (1 <= root_pbl_index) {
2289 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2290 cpu_to_le32((u32)vpbl.pbl_pbase);
2291 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2292 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2293 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2295 root_pbl_index++;
2296 cur_pbl_index = 0;
2298 if (single_page) {
2299 if (page_count != 0) {
2300 if ((last_dma_addr+4096) !=
2301 (sg_dma_address(sg)+
2302 (page_index*4096)))
2303 single_page = 0;
2304 last_dma_addr = sg_dma_address(sg)+
2305 (page_index*4096);
2306 } else {
2307 first_dma_addr = sg_dma_address(sg)+
2308 (page_index*4096);
2309 last_dma_addr = first_dma_addr;
2313 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2314 cpu_to_le32((u32)(sg_dma_address(sg)+
2315 (page_index*4096)));
2316 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2317 cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2318 (page_index*4096))) >> 32)));
2319 cur_pbl_index++;
2320 page_count++;
2324 enough_pages:
2325 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2326 " stag_key=0x%08x\n",
2327 stag_index, driver_key, stag_key);
2328 stag = stag_index << 8;
2329 stag |= driver_key;
2330 stag += (u32)stag_key;
2332 iova_start = virt;
2333 /* Make the leaf PBL the root if only one PBL */
2334 if (root_pbl_index == 1) {
2335 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2338 if (single_page) {
2339 pbl_count = 0;
2340 } else {
2341 pbl_count = root_pbl_index;
2342 first_dma_addr = 0;
2344 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2345 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2346 stag, (unsigned int)iova_start,
2347 (unsigned int)region_length, stag_index,
2348 (unsigned long long)region->length, pbl_count);
2349 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2350 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2351 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2353 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2355 if (ret == 0) {
2356 nesmr->ibmr.rkey = stag;
2357 nesmr->ibmr.lkey = stag;
2358 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2359 ibmr = &nesmr->ibmr;
2360 } else {
2361 ib_umem_release(region);
2362 kfree(nesmr);
2363 ibmr = ERR_PTR(-ENOMEM);
2366 reg_user_mr_err:
2367 /* free the resources */
2368 if (root_pbl_index == 1) {
2369 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2370 vpbl.pbl_pbase);
2371 } else {
2372 for (page_index=0; page_index<root_pbl_index; page_index++) {
2373 pci_free_consistent(nesdev->pcidev, 4096,
2374 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2375 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2377 kfree(root_vpbl.leaf_vpbl);
2378 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2379 root_vpbl.pbl_pbase);
2382 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2384 return ibmr;
2385 case IWNES_MEMREG_TYPE_QP:
2386 case IWNES_MEMREG_TYPE_CQ:
2387 if (!region->length) {
2388 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2389 ib_umem_release(region);
2390 return ERR_PTR(-EINVAL);
2392 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2393 if (!nespbl) {
2394 ib_umem_release(region);
2395 return ERR_PTR(-ENOMEM);
2397 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2398 if (!nesmr) {
2399 ib_umem_release(region);
2400 kfree(nespbl);
2401 return ERR_PTR(-ENOMEM);
2403 nesmr->region = region;
2404 nes_ucontext = to_nesucontext(pd->uobject->context);
2405 pbl_depth = region->length >> 12;
2406 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2407 nespbl->pbl_size = pbl_depth*sizeof(u64);
2408 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2409 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2410 } else {
2411 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2414 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2415 nespbl->pbl_size, pbl_depth);
2416 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2417 &nespbl->pbl_pbase);
2418 if (!pbl) {
2419 ib_umem_release(region);
2420 kfree(nesmr);
2421 kfree(nespbl);
2422 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2423 return ERR_PTR(-ENOMEM);
2426 nespbl->pbl_vbase = (u64 *)pbl;
2427 nespbl->user_base = start;
2428 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2429 " pbl_vbase=%p user_base=0x%lx\n",
2430 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2431 (void *) nespbl->pbl_vbase, nespbl->user_base);
2433 for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2434 chunk_pages = sg_dma_len(sg) >> 12;
2435 chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2436 if (first_page) {
2437 nespbl->page = sg_page(sg);
2438 first_page = 0;
2441 for (page_index = 0; page_index < chunk_pages; page_index++) {
2442 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2443 (sg_dma_address(sg)+
2444 (page_index*4096)));
2445 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2446 (sg_dma_address(sg)+
2447 (page_index*4096)))>>32);
2448 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2449 (unsigned long long)*pbl,
2450 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2451 pbl++;
2455 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2456 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2457 } else {
2458 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2460 nesmr->ibmr.rkey = -1;
2461 nesmr->ibmr.lkey = -1;
2462 nesmr->mode = req.reg_type;
2463 return &nesmr->ibmr;
2466 ib_umem_release(region);
2467 return ERR_PTR(-ENOSYS);
2472 * nes_dereg_mr
2474 static int nes_dereg_mr(struct ib_mr *ib_mr)
2476 struct nes_mr *nesmr = to_nesmr(ib_mr);
2477 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2478 struct nes_device *nesdev = nesvnic->nesdev;
2479 struct nes_adapter *nesadapter = nesdev->nesadapter;
2480 struct nes_hw_cqp_wqe *cqp_wqe;
2481 struct nes_cqp_request *cqp_request;
2482 unsigned long flags;
2483 int ret;
2484 u16 major_code;
2485 u16 minor_code;
2488 if (nesmr->pages)
2489 pci_free_consistent(nesdev->pcidev,
2490 nesmr->max_pages * sizeof(u64),
2491 nesmr->pages,
2492 nesmr->paddr);
2494 if (nesmr->region) {
2495 ib_umem_release(nesmr->region);
2497 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2498 kfree(nesmr);
2499 return 0;
2502 /* Deallocate the region with the adapter */
2504 cqp_request = nes_get_cqp_request(nesdev);
2505 if (cqp_request == NULL) {
2506 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2507 return -ENOMEM;
2509 cqp_request->waiting = 1;
2510 cqp_wqe = &cqp_request->cqp_wqe;
2512 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2513 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2514 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2515 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2516 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2518 atomic_set(&cqp_request->refcount, 2);
2519 nes_post_cqp_request(nesdev, cqp_request);
2521 /* Wait for CQP */
2522 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2523 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2524 NES_EVENT_TIMEOUT);
2525 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2526 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2527 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2529 major_code = cqp_request->major_code;
2530 minor_code = cqp_request->minor_code;
2532 nes_put_cqp_request(nesdev, cqp_request);
2534 if (!ret) {
2535 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2536 " ib_mr=%p, rkey = 0x%08X\n",
2537 ib_mr, ib_mr->rkey);
2538 return -ETIME;
2539 } else if (major_code) {
2540 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2541 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2542 major_code, minor_code, ib_mr, ib_mr->rkey);
2543 return -EIO;
2546 if (nesmr->pbls_used != 0) {
2547 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2548 if (nesmr->pbl_4k) {
2549 nesadapter->free_4kpbl += nesmr->pbls_used;
2550 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2551 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2552 "exceeded the max(%u)\n",
2553 nesadapter->free_4kpbl,
2554 nesadapter->max_4kpbl);
2555 } else {
2556 nesadapter->free_256pbl += nesmr->pbls_used;
2557 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2558 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2559 "exceeded the max(%u)\n",
2560 nesadapter->free_256pbl,
2561 nesadapter->max_256pbl);
2563 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2565 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2566 (ib_mr->rkey & 0x0fffff00) >> 8);
2568 kfree(nesmr);
2570 return 0;
2575 * show_rev
2577 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2578 char *buf)
2580 struct nes_ib_device *nesibdev =
2581 container_of(dev, struct nes_ib_device, ibdev.dev);
2582 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2584 nes_debug(NES_DBG_INIT, "\n");
2585 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2590 * show_hca
2592 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2593 char *buf)
2595 nes_debug(NES_DBG_INIT, "\n");
2596 return sprintf(buf, "NES020\n");
2601 * show_board
2603 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2604 char *buf)
2606 nes_debug(NES_DBG_INIT, "\n");
2607 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2611 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2612 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2613 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2615 static struct device_attribute *nes_dev_attributes[] = {
2616 &dev_attr_hw_rev,
2617 &dev_attr_hca_type,
2618 &dev_attr_board_id
2623 * nes_query_qp
2625 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2626 int attr_mask, struct ib_qp_init_attr *init_attr)
2628 struct nes_qp *nesqp = to_nesqp(ibqp);
2630 nes_debug(NES_DBG_QP, "\n");
2632 attr->qp_access_flags = 0;
2633 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2634 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2635 attr->cap.max_recv_sge = 1;
2636 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2637 attr->cap.max_inline_data = 0;
2638 else
2639 attr->cap.max_inline_data = 64;
2641 init_attr->event_handler = nesqp->ibqp.event_handler;
2642 init_attr->qp_context = nesqp->ibqp.qp_context;
2643 init_attr->send_cq = nesqp->ibqp.send_cq;
2644 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2645 init_attr->srq = nesqp->ibqp.srq;
2646 init_attr->cap = attr->cap;
2648 return 0;
2653 * nes_hw_modify_qp
2655 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2656 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2658 struct nes_hw_cqp_wqe *cqp_wqe;
2659 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2660 /* struct iw_cm_event cm_event; */
2661 struct nes_cqp_request *cqp_request;
2662 int ret;
2663 u16 major_code;
2665 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2666 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2668 cqp_request = nes_get_cqp_request(nesdev);
2669 if (cqp_request == NULL) {
2670 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2671 return -ENOMEM;
2673 if (wait_completion) {
2674 cqp_request->waiting = 1;
2675 } else {
2676 cqp_request->waiting = 0;
2678 cqp_wqe = &cqp_request->cqp_wqe;
2680 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2681 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2682 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2683 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2684 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2685 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2686 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2688 /* If sending a terminate message, fill in the length (in words) */
2689 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2690 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2691 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2692 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2695 atomic_set(&cqp_request->refcount, 2);
2696 nes_post_cqp_request(nesdev, cqp_request);
2698 /* Wait for CQP */
2699 if (wait_completion) {
2700 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2701 nesqp->hwqp.qp_id); */
2702 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2703 NES_EVENT_TIMEOUT);
2704 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2705 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2706 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2707 major_code = cqp_request->major_code;
2708 if (major_code) {
2709 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2710 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2711 nesqp->hwqp.qp_id, cqp_request->major_code,
2712 cqp_request->minor_code, next_iwarp_state);
2715 nes_put_cqp_request(nesdev, cqp_request);
2717 if (!ret)
2718 return -ETIME;
2719 else if (major_code)
2720 return -EIO;
2721 else
2722 return 0;
2723 } else {
2724 return 0;
2730 * nes_modify_qp
2732 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2733 int attr_mask, struct ib_udata *udata)
2735 struct nes_qp *nesqp = to_nesqp(ibqp);
2736 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2737 struct nes_device *nesdev = nesvnic->nesdev;
2738 /* u32 cqp_head; */
2739 /* u32 counter; */
2740 u32 next_iwarp_state = 0;
2741 int err;
2742 unsigned long qplockflags;
2743 int ret;
2744 u16 original_last_aeq;
2745 u8 issue_modify_qp = 0;
2746 u8 dont_wait = 0;
2748 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2749 " iwarp_state=0x%X, refcount=%d\n",
2750 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2751 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2753 spin_lock_irqsave(&nesqp->lock, qplockflags);
2755 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2756 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2757 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2758 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2760 if (attr_mask & IB_QP_STATE) {
2761 switch (attr->qp_state) {
2762 case IB_QPS_INIT:
2763 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2764 nesqp->hwqp.qp_id);
2765 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2766 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2767 return -EINVAL;
2769 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2770 issue_modify_qp = 1;
2771 break;
2772 case IB_QPS_RTR:
2773 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2774 nesqp->hwqp.qp_id);
2775 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2776 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2777 return -EINVAL;
2779 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2780 issue_modify_qp = 1;
2781 break;
2782 case IB_QPS_RTS:
2783 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2784 nesqp->hwqp.qp_id);
2785 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2786 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2787 return -EINVAL;
2789 if (nesqp->cm_id == NULL) {
2790 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2791 nesqp->hwqp.qp_id );
2792 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2793 return -EINVAL;
2795 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2796 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2797 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2798 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2799 issue_modify_qp = 1;
2800 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2801 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2802 nesqp->hte_added = 1;
2803 break;
2804 case IB_QPS_SQD:
2805 issue_modify_qp = 1;
2806 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2807 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2808 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2809 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2810 return 0;
2811 } else {
2812 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2813 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2814 " ignored due to current iWARP state\n",
2815 nesqp->hwqp.qp_id);
2816 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2817 return -EINVAL;
2819 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2820 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2821 " already done based on hw state.\n",
2822 nesqp->hwqp.qp_id);
2823 issue_modify_qp = 0;
2825 switch (nesqp->hw_iwarp_state) {
2826 case NES_AEQE_IWARP_STATE_CLOSING:
2827 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2828 break;
2829 case NES_AEQE_IWARP_STATE_TERMINATE:
2830 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2831 break;
2832 case NES_AEQE_IWARP_STATE_ERROR:
2833 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2834 break;
2835 default:
2836 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2837 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2838 break;
2841 break;
2842 case IB_QPS_SQE:
2843 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2844 nesqp->hwqp.qp_id);
2845 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2846 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2847 return -EINVAL;
2849 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2850 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2851 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2852 issue_modify_qp = 1;
2853 break;
2854 case IB_QPS_ERR:
2855 case IB_QPS_RESET:
2856 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2857 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2858 return -EINVAL;
2860 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2861 nesqp->hwqp.qp_id);
2862 if (nesqp->term_flags)
2863 del_timer(&nesqp->terminate_timer);
2865 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2866 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2867 if (nesqp->hte_added) {
2868 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2869 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2870 nesqp->hte_added = 0;
2872 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2873 (nesdev->iw_status) &&
2874 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2875 next_iwarp_state |= NES_CQP_QP_RESET;
2876 } else {
2877 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2878 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2879 dont_wait = 1;
2881 issue_modify_qp = 1;
2882 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2883 break;
2884 default:
2885 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2886 return -EINVAL;
2887 break;
2890 nesqp->ibqp_state = attr->qp_state;
2891 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2892 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2893 nesqp->iwarp_state);
2896 if (attr_mask & IB_QP_ACCESS_FLAGS) {
2897 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2898 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2899 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2900 issue_modify_qp = 1;
2902 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2903 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2904 issue_modify_qp = 1;
2906 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2907 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2908 issue_modify_qp = 1;
2910 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2911 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2912 issue_modify_qp = 1;
2915 if (nesqp->user_mode) {
2916 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2917 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2918 issue_modify_qp = 1;
2922 original_last_aeq = nesqp->last_aeq;
2923 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2925 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2927 ret = 0;
2930 if (issue_modify_qp) {
2931 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
2932 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
2933 if (ret)
2934 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2935 " failed for QP%u.\n",
2936 next_iwarp_state, nesqp->hwqp.qp_id);
2940 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2941 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2942 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2943 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2944 original_last_aeq, nesqp->last_aeq);
2945 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
2946 if (dont_wait) {
2947 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2948 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2949 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2950 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2951 original_last_aeq, nesqp->last_aeq);
2952 /* this one is for the cm_disconnect thread */
2953 spin_lock_irqsave(&nesqp->lock, qplockflags);
2954 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2955 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2956 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2957 nes_cm_disconn(nesqp);
2958 } else {
2959 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2960 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2962 } else {
2963 spin_lock_irqsave(&nesqp->lock, qplockflags);
2964 if (nesqp->cm_id) {
2965 /* These two are for the timer thread */
2966 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
2967 nesqp->cm_id->add_ref(nesqp->cm_id);
2968 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2969 " need ae to finish up, original_last_aeq = 0x%04X."
2970 " last_aeq = 0x%04X, scheduling timer.\n",
2971 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2972 original_last_aeq, nesqp->last_aeq);
2973 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2975 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2976 } else {
2977 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2978 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2979 " need ae to finish up, original_last_aeq = 0x%04X."
2980 " last_aeq = 0x%04X.\n",
2981 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2982 original_last_aeq, nesqp->last_aeq);
2985 } else {
2986 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2987 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2988 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2989 original_last_aeq, nesqp->last_aeq);
2991 } else {
2992 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2993 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2994 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2995 original_last_aeq, nesqp->last_aeq);
2998 err = 0;
3000 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3001 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3003 return err;
3008 * nes_muticast_attach
3010 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3012 nes_debug(NES_DBG_INIT, "\n");
3013 return -ENOSYS;
3018 * nes_multicast_detach
3020 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3022 nes_debug(NES_DBG_INIT, "\n");
3023 return -ENOSYS;
3028 * nes_process_mad
3030 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3031 u8 port_num, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
3032 const struct ib_mad_hdr *in, size_t in_mad_size,
3033 struct ib_mad_hdr *out, size_t *out_mad_size,
3034 u16 *out_mad_pkey_index)
3036 nes_debug(NES_DBG_INIT, "\n");
3037 return -ENOSYS;
3040 static inline void
3041 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3043 int sge_index;
3044 int total_payload_length = 0;
3045 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3046 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3047 ib_wr->sg_list[sge_index].addr);
3048 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3049 ib_wr->sg_list[sge_index].length);
3050 if (uselkey)
3051 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3052 (ib_wr->sg_list[sge_index].lkey));
3053 else
3054 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3056 total_payload_length += ib_wr->sg_list[sge_index].length;
3058 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3059 total_payload_length);
3060 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3061 total_payload_length);
3065 * nes_post_send
3067 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3068 struct ib_send_wr **bad_wr)
3070 u64 u64temp;
3071 unsigned long flags = 0;
3072 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3073 struct nes_device *nesdev = nesvnic->nesdev;
3074 struct nes_qp *nesqp = to_nesqp(ibqp);
3075 struct nes_hw_qp_wqe *wqe;
3076 int err = 0;
3077 u32 qsize = nesqp->hwqp.sq_size;
3078 u32 head;
3079 u32 wqe_misc = 0;
3080 u32 wqe_count = 0;
3081 u32 counter;
3083 if (nesqp->ibqp_state > IB_QPS_RTS) {
3084 err = -EINVAL;
3085 goto out;
3088 spin_lock_irqsave(&nesqp->lock, flags);
3090 head = nesqp->hwqp.sq_head;
3092 while (ib_wr) {
3093 /* Check for QP error */
3094 if (nesqp->term_flags) {
3095 err = -EINVAL;
3096 break;
3099 /* Check for SQ overflow */
3100 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3101 err = -ENOMEM;
3102 break;
3105 wqe = &nesqp->hwqp.sq_vbase[head];
3106 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3107 nesqp->hwqp.qp_id, wqe, head); */
3108 nes_fill_init_qp_wqe(wqe, nesqp, head);
3109 u64temp = (u64)(ib_wr->wr_id);
3110 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3111 u64temp);
3112 switch (ib_wr->opcode) {
3113 case IB_WR_SEND:
3114 case IB_WR_SEND_WITH_INV:
3115 if (IB_WR_SEND == ib_wr->opcode) {
3116 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3117 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3118 else
3119 wqe_misc = NES_IWARP_SQ_OP_SEND;
3120 } else {
3121 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3122 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3123 else
3124 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
3126 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3127 ib_wr->ex.invalidate_rkey);
3130 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3131 err = -EINVAL;
3132 break;
3135 if (ib_wr->send_flags & IB_SEND_FENCE)
3136 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3138 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3139 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3140 (ib_wr->sg_list[0].length <= 64)) {
3141 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3142 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3143 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3144 ib_wr->sg_list[0].length);
3145 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3146 } else {
3147 fill_wqe_sg_send(wqe, ib_wr, 1);
3150 break;
3151 case IB_WR_RDMA_WRITE:
3152 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3153 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3154 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3155 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3156 err = -EINVAL;
3157 break;
3160 if (ib_wr->send_flags & IB_SEND_FENCE)
3161 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3163 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3164 rdma_wr(ib_wr)->rkey);
3165 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3166 rdma_wr(ib_wr)->remote_addr);
3168 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3169 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3170 (ib_wr->sg_list[0].length <= 64)) {
3171 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3172 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3173 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3174 ib_wr->sg_list[0].length);
3175 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3176 } else {
3177 fill_wqe_sg_send(wqe, ib_wr, 1);
3180 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3181 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3182 break;
3183 case IB_WR_RDMA_READ:
3184 case IB_WR_RDMA_READ_WITH_INV:
3185 /* iWARP only supports 1 sge for RDMA reads */
3186 if (ib_wr->num_sge > 1) {
3187 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3188 ib_wr->num_sge);
3189 err = -EINVAL;
3190 break;
3192 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3193 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3194 } else {
3195 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3196 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3197 ib_wr->ex.invalidate_rkey);
3200 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3201 rdma_wr(ib_wr)->remote_addr);
3202 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3203 rdma_wr(ib_wr)->rkey);
3204 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3205 ib_wr->sg_list->length);
3206 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3207 ib_wr->sg_list->addr);
3208 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3209 ib_wr->sg_list->lkey);
3210 break;
3211 case IB_WR_LOCAL_INV:
3212 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3213 set_wqe_32bit_value(wqe->wqe_words,
3214 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3215 ib_wr->ex.invalidate_rkey);
3216 break;
3217 case IB_WR_REG_MR:
3219 struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3220 int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3221 int flags = reg_wr(ib_wr)->access;
3223 if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3224 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3225 err = -EINVAL;
3226 break;
3228 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3229 set_wqe_64bit_value(wqe->wqe_words,
3230 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3231 mr->ibmr.iova);
3232 set_wqe_32bit_value(wqe->wqe_words,
3233 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3234 lower_32_bits(mr->ibmr.length));
3235 set_wqe_32bit_value(wqe->wqe_words,
3236 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3237 set_wqe_32bit_value(wqe->wqe_words,
3238 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3239 reg_wr(ib_wr)->key);
3241 if (page_shift == 12) {
3242 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3243 } else if (page_shift == 21) {
3244 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3245 } else {
3246 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3247 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3248 err = -EINVAL;
3249 break;
3252 /* Set access_flags */
3253 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3254 if (flags & IB_ACCESS_LOCAL_WRITE)
3255 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3257 if (flags & IB_ACCESS_REMOTE_WRITE)
3258 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3260 if (flags & IB_ACCESS_REMOTE_READ)
3261 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3263 if (flags & IB_ACCESS_MW_BIND)
3264 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3266 /* Fill in PBL info: */
3267 set_wqe_64bit_value(wqe->wqe_words,
3268 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3269 mr->paddr);
3271 set_wqe_32bit_value(wqe->wqe_words,
3272 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3273 mr->npages * 8);
3275 nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3276 "length: %lld, rkey: %0x, pgl_paddr: %llx, "
3277 "page_list_len: %u, wqe_misc: %x\n",
3278 (unsigned long long) mr->ibmr.iova,
3279 mr->ibmr.length,
3280 reg_wr(ib_wr)->key,
3281 (unsigned long long) mr->paddr,
3282 mr->npages,
3283 wqe_misc);
3284 break;
3286 default:
3287 /* error */
3288 err = -EINVAL;
3289 break;
3292 if (err)
3293 break;
3295 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3296 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3298 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3300 ib_wr = ib_wr->next;
3301 head++;
3302 wqe_count++;
3303 if (head >= qsize)
3304 head = 0;
3308 nesqp->hwqp.sq_head = head;
3309 barrier();
3310 while (wqe_count) {
3311 counter = min(wqe_count, ((u32)255));
3312 wqe_count -= counter;
3313 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3314 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3317 spin_unlock_irqrestore(&nesqp->lock, flags);
3319 out:
3320 if (err)
3321 *bad_wr = ib_wr;
3322 return err;
3327 * nes_post_recv
3329 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3330 struct ib_recv_wr **bad_wr)
3332 u64 u64temp;
3333 unsigned long flags = 0;
3334 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3335 struct nes_device *nesdev = nesvnic->nesdev;
3336 struct nes_qp *nesqp = to_nesqp(ibqp);
3337 struct nes_hw_qp_wqe *wqe;
3338 int err = 0;
3339 int sge_index;
3340 u32 qsize = nesqp->hwqp.rq_size;
3341 u32 head;
3342 u32 wqe_count = 0;
3343 u32 counter;
3344 u32 total_payload_length;
3346 if (nesqp->ibqp_state > IB_QPS_RTS) {
3347 err = -EINVAL;
3348 goto out;
3351 spin_lock_irqsave(&nesqp->lock, flags);
3353 head = nesqp->hwqp.rq_head;
3355 while (ib_wr) {
3356 /* Check for QP error */
3357 if (nesqp->term_flags) {
3358 err = -EINVAL;
3359 break;
3362 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3363 err = -EINVAL;
3364 break;
3366 /* Check for RQ overflow */
3367 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3368 err = -ENOMEM;
3369 break;
3372 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3373 wqe = &nesqp->hwqp.rq_vbase[head];
3375 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3376 nesqp->hwqp.qp_id, wqe, head); */
3377 nes_fill_init_qp_wqe(wqe, nesqp, head);
3378 u64temp = (u64)(ib_wr->wr_id);
3379 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3380 u64temp);
3381 total_payload_length = 0;
3382 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3383 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3384 ib_wr->sg_list[sge_index].addr);
3385 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3386 ib_wr->sg_list[sge_index].length);
3387 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3388 ib_wr->sg_list[sge_index].lkey);
3390 total_payload_length += ib_wr->sg_list[sge_index].length;
3392 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3393 total_payload_length);
3395 ib_wr = ib_wr->next;
3396 head++;
3397 wqe_count++;
3398 if (head >= qsize)
3399 head = 0;
3402 nesqp->hwqp.rq_head = head;
3403 barrier();
3404 while (wqe_count) {
3405 counter = min(wqe_count, ((u32)255));
3406 wqe_count -= counter;
3407 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3410 spin_unlock_irqrestore(&nesqp->lock, flags);
3412 out:
3413 if (err)
3414 *bad_wr = ib_wr;
3415 return err;
3419 * nes_drain_sq - drain sq
3420 * @ibqp: pointer to ibqp
3422 static void nes_drain_sq(struct ib_qp *ibqp)
3424 struct nes_qp *nesqp = to_nesqp(ibqp);
3426 if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3427 wait_for_completion(&nesqp->sq_drained);
3431 * nes_drain_rq - drain rq
3432 * @ibqp: pointer to ibqp
3434 static void nes_drain_rq(struct ib_qp *ibqp)
3436 struct nes_qp *nesqp = to_nesqp(ibqp);
3438 if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3439 wait_for_completion(&nesqp->rq_drained);
3443 * nes_poll_cq
3445 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3447 u64 u64temp;
3448 u64 wrid;
3449 unsigned long flags = 0;
3450 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3451 struct nes_device *nesdev = nesvnic->nesdev;
3452 struct nes_cq *nescq = to_nescq(ibcq);
3453 struct nes_qp *nesqp;
3454 struct nes_hw_cqe cqe;
3455 u32 head;
3456 u32 wq_tail = 0;
3457 u32 cq_size;
3458 u32 cqe_count = 0;
3459 u32 wqe_index;
3460 u32 u32temp;
3461 u32 move_cq_head = 1;
3462 u32 err_code;
3464 nes_debug(NES_DBG_CQ, "\n");
3466 spin_lock_irqsave(&nescq->lock, flags);
3468 head = nescq->hw_cq.cq_head;
3469 cq_size = nescq->hw_cq.cq_size;
3471 while (cqe_count < num_entries) {
3472 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3473 NES_CQE_VALID) == 0)
3474 break;
3477 * Make sure we read CQ entry contents *after*
3478 * we've checked the valid bit.
3480 rmb();
3482 cqe = nescq->hw_cq.cq_vbase[head];
3483 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3484 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3485 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3486 /* parse CQE, get completion context from WQE (either rq or sq) */
3487 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3488 ((u64)u32temp);
3490 if (u64temp) {
3491 nesqp = (struct nes_qp *)(unsigned long)u64temp;
3492 memset(entry, 0, sizeof *entry);
3493 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3494 entry->status = IB_WC_SUCCESS;
3495 } else {
3496 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3497 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3498 entry->status = err_code & 0x0000ffff;
3500 /* The rest of the cqe's will be marked as flushed */
3501 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3502 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3503 NES_IWARP_CQE_MINOR_FLUSH);
3504 } else
3505 entry->status = IB_WC_WR_FLUSH_ERR;
3508 entry->qp = &nesqp->ibqp;
3509 entry->src_qp = nesqp->hwqp.qp_id;
3511 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3512 if (nesqp->skip_lsmm) {
3513 nesqp->skip_lsmm = 0;
3514 nesqp->hwqp.sq_tail++;
3517 /* Working on a SQ Completion*/
3518 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3519 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3520 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3521 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3522 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3523 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3525 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3526 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3527 case NES_IWARP_SQ_OP_RDMAW:
3528 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3529 entry->opcode = IB_WC_RDMA_WRITE;
3530 break;
3531 case NES_IWARP_SQ_OP_RDMAR:
3532 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3533 entry->opcode = IB_WC_RDMA_READ;
3534 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3535 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3536 break;
3537 case NES_IWARP_SQ_OP_SENDINV:
3538 case NES_IWARP_SQ_OP_SENDSEINV:
3539 case NES_IWARP_SQ_OP_SEND:
3540 case NES_IWARP_SQ_OP_SENDSE:
3541 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3542 entry->opcode = IB_WC_SEND;
3543 break;
3544 case NES_IWARP_SQ_OP_LOCINV:
3545 entry->opcode = IB_WC_LOCAL_INV;
3546 break;
3547 case NES_IWARP_SQ_OP_FAST_REG:
3548 entry->opcode = IB_WC_REG_MR;
3549 break;
3552 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3553 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3554 move_cq_head = 0;
3555 wq_tail = nesqp->hwqp.sq_tail;
3557 } else {
3558 /* Working on a RQ Completion*/
3559 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3560 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3561 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3562 entry->opcode = IB_WC_RECV;
3564 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3565 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3566 move_cq_head = 0;
3567 wq_tail = nesqp->hwqp.rq_tail;
3571 if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3572 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3573 complete(&nesqp->sq_drained);
3574 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3575 complete(&nesqp->rq_drained);
3578 entry->wr_id = wrid;
3579 entry++;
3580 cqe_count++;
3583 if (move_cq_head) {
3584 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3585 if (++head >= cq_size)
3586 head = 0;
3587 nescq->polled_completions++;
3589 if ((nescq->polled_completions > (cq_size / 2)) ||
3590 (nescq->polled_completions == 255)) {
3591 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3592 " are pending %u of %u.\n",
3593 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3594 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3595 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3596 nescq->polled_completions = 0;
3598 } else {
3599 /* Update the wqe index and set status to flush */
3600 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3601 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3602 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3603 cpu_to_le32(wqe_index);
3604 move_cq_head = 1; /* ready for next pass */
3608 if (nescq->polled_completions) {
3609 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3610 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3611 nescq->polled_completions = 0;
3614 nescq->hw_cq.cq_head = head;
3615 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3616 cqe_count, nescq->hw_cq.cq_number);
3618 spin_unlock_irqrestore(&nescq->lock, flags);
3620 return cqe_count;
3625 * nes_req_notify_cq
3627 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3629 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3630 struct nes_device *nesdev = nesvnic->nesdev;
3631 struct nes_cq *nescq = to_nescq(ibcq);
3632 u32 cq_arm;
3634 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3635 nescq->hw_cq.cq_number);
3637 cq_arm = nescq->hw_cq.cq_number;
3638 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3639 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3640 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3641 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3642 else
3643 return -EINVAL;
3645 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3646 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3648 return 0;
3651 static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3652 struct ib_port_immutable *immutable)
3654 struct ib_port_attr attr;
3655 int err;
3657 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3659 err = nes_query_port(ibdev, port_num, &attr);
3660 if (err)
3661 return err;
3663 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3664 immutable->gid_tbl_len = attr.gid_tbl_len;
3666 return 0;
3669 static void get_dev_fw_str(struct ib_device *dev, char *str)
3671 struct nes_ib_device *nesibdev =
3672 container_of(dev, struct nes_ib_device, ibdev);
3673 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3675 nes_debug(NES_DBG_INIT, "\n");
3676 snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u",
3677 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3678 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3682 * nes_init_ofa_device
3684 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3686 struct nes_ib_device *nesibdev;
3687 struct nes_vnic *nesvnic = netdev_priv(netdev);
3688 struct nes_device *nesdev = nesvnic->nesdev;
3690 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3691 if (nesibdev == NULL) {
3692 return NULL;
3694 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3695 nesibdev->ibdev.owner = THIS_MODULE;
3697 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3698 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3699 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3701 nesibdev->ibdev.uverbs_cmd_mask =
3702 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3703 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3704 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3705 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3706 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3707 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3708 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3709 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3710 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3711 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3712 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3713 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3714 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3715 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3716 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3717 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3718 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3719 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3720 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3721 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3722 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3723 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3725 nesibdev->ibdev.phys_port_cnt = 1;
3726 nesibdev->ibdev.num_comp_vectors = 1;
3727 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3728 nesibdev->ibdev.query_device = nes_query_device;
3729 nesibdev->ibdev.query_port = nes_query_port;
3730 nesibdev->ibdev.query_pkey = nes_query_pkey;
3731 nesibdev->ibdev.query_gid = nes_query_gid;
3732 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3733 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3734 nesibdev->ibdev.mmap = nes_mmap;
3735 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3736 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3737 nesibdev->ibdev.create_ah = nes_create_ah;
3738 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3739 nesibdev->ibdev.create_qp = nes_create_qp;
3740 nesibdev->ibdev.modify_qp = nes_modify_qp;
3741 nesibdev->ibdev.query_qp = nes_query_qp;
3742 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3743 nesibdev->ibdev.create_cq = nes_create_cq;
3744 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3745 nesibdev->ibdev.poll_cq = nes_poll_cq;
3746 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3747 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3748 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3749 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3750 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3752 nesibdev->ibdev.alloc_mr = nes_alloc_mr;
3753 nesibdev->ibdev.map_mr_sg = nes_map_mr_sg;
3755 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3756 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3757 nesibdev->ibdev.process_mad = nes_process_mad;
3759 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3760 nesibdev->ibdev.post_send = nes_post_send;
3761 nesibdev->ibdev.post_recv = nes_post_recv;
3762 nesibdev->ibdev.drain_sq = nes_drain_sq;
3763 nesibdev->ibdev.drain_rq = nes_drain_rq;
3765 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3766 if (nesibdev->ibdev.iwcm == NULL) {
3767 ib_dealloc_device(&nesibdev->ibdev);
3768 return NULL;
3770 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3771 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3772 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3773 nesibdev->ibdev.iwcm->connect = nes_connect;
3774 nesibdev->ibdev.iwcm->accept = nes_accept;
3775 nesibdev->ibdev.iwcm->reject = nes_reject;
3776 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3777 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3778 nesibdev->ibdev.get_port_immutable = nes_port_immutable;
3779 nesibdev->ibdev.get_dev_fw_str = get_dev_fw_str;
3780 memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3781 sizeof(nesibdev->ibdev.iwcm->ifname));
3783 return nesibdev;
3788 * nes_handle_delayed_event
3790 static void nes_handle_delayed_event(struct timer_list *t)
3792 struct nes_vnic *nesvnic = from_timer(nesvnic, t, event_timer);
3794 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3795 struct ib_event event;
3797 event.device = &nesvnic->nesibdev->ibdev;
3798 if (!event.device)
3799 goto stop_timer;
3800 event.event = nesvnic->delayed_event;
3801 event.element.port_num = nesvnic->logical_port + 1;
3802 ib_dispatch_event(&event);
3805 stop_timer:
3806 nesvnic->event_timer.function = NULL;
3810 void nes_port_ibevent(struct nes_vnic *nesvnic)
3812 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3813 struct nes_device *nesdev = nesvnic->nesdev;
3814 struct ib_event event;
3815 event.device = &nesibdev->ibdev;
3816 event.element.port_num = nesvnic->logical_port + 1;
3817 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3819 if (!nesvnic->event_timer.function) {
3820 ib_dispatch_event(&event);
3821 nesvnic->last_dispatched_event = event.event;
3822 nesvnic->event_timer.function = nes_handle_delayed_event;
3823 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3824 add_timer(&nesvnic->event_timer);
3825 } else {
3826 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3828 nesvnic->delayed_event = event.event;
3833 * nes_destroy_ofa_device
3835 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3837 if (nesibdev == NULL)
3838 return;
3840 nes_unregister_ofa_device(nesibdev);
3842 kfree(nesibdev->ibdev.iwcm);
3843 ib_dealloc_device(&nesibdev->ibdev);
3848 * nes_register_ofa_device
3850 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3852 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3853 struct nes_device *nesdev = nesvnic->nesdev;
3854 struct nes_adapter *nesadapter = nesdev->nesadapter;
3855 int i, ret;
3857 ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
3858 if (ret) {
3859 return ret;
3862 /* Get the resources allocated to this device */
3863 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3864 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3865 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3866 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3868 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3869 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3870 if (ret) {
3871 while (i > 0) {
3872 i--;
3873 device_remove_file(&nesibdev->ibdev.dev,
3874 nes_dev_attributes[i]);
3876 ib_unregister_device(&nesibdev->ibdev);
3877 return ret;
3881 nesvnic->of_device_registered = 1;
3883 return 0;
3888 * nes_unregister_ofa_device
3890 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3892 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3893 int i;
3895 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3896 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3899 if (nesvnic->of_device_registered) {
3900 ib_unregister_device(&nesibdev->ibdev);
3903 nesvnic->of_device_registered = 0;