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