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