blob: 7507968deb2ca3857628f2394c39f7792f460e17 [file] [log] [blame]
Parav Panditfe2caef2012-03-21 04:09:06 +05301/*******************************************************************
2 * This file is part of the Emulex RoCE Device Driver for *
3 * RoCE (RDMA over Converged Ethernet) CNA Adapters. *
4 * Copyright (C) 2008-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
19 *
20 * Contact Information:
21 * linux-drivers@emulex.com
22 *
23 * Emulex
24 * 3333 Susan Street
25 * Costa Mesa, CA 92626
26 *******************************************************************/
27
28#include <linux/sched.h>
29#include <linux/interrupt.h>
30#include <linux/log2.h>
31#include <linux/dma-mapping.h>
32
33#include <rdma/ib_verbs.h>
34#include <rdma/ib_user_verbs.h>
35#include <rdma/ib_addr.h>
36
37#include "ocrdma.h"
38#include "ocrdma_hw.h"
39#include "ocrdma_verbs.h"
40#include "ocrdma_ah.h"
41
42enum mbx_status {
43 OCRDMA_MBX_STATUS_FAILED = 1,
44 OCRDMA_MBX_STATUS_ILLEGAL_FIELD = 3,
45 OCRDMA_MBX_STATUS_OOR = 100,
46 OCRDMA_MBX_STATUS_INVALID_PD = 101,
47 OCRDMA_MBX_STATUS_PD_INUSE = 102,
48 OCRDMA_MBX_STATUS_INVALID_CQ = 103,
49 OCRDMA_MBX_STATUS_INVALID_QP = 104,
50 OCRDMA_MBX_STATUS_INVALID_LKEY = 105,
51 OCRDMA_MBX_STATUS_ORD_EXCEEDS = 106,
52 OCRDMA_MBX_STATUS_IRD_EXCEEDS = 107,
53 OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS = 108,
54 OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS = 109,
55 OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS = 110,
56 OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS = 111,
57 OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS = 112,
58 OCRDMA_MBX_STATUS_INVALID_STATE_CHANGE = 113,
59 OCRDMA_MBX_STATUS_MW_BOUND = 114,
60 OCRDMA_MBX_STATUS_INVALID_VA = 115,
61 OCRDMA_MBX_STATUS_INVALID_LENGTH = 116,
62 OCRDMA_MBX_STATUS_INVALID_FBO = 117,
63 OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS = 118,
64 OCRDMA_MBX_STATUS_INVALID_PBE_SIZE = 119,
65 OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY = 120,
66 OCRDMA_MBX_STATUS_INVALID_PBL_SHIFT = 121,
67 OCRDMA_MBX_STATUS_INVALID_SRQ_ID = 129,
68 OCRDMA_MBX_STATUS_SRQ_ERROR = 133,
69 OCRDMA_MBX_STATUS_RQE_EXCEEDS = 134,
70 OCRDMA_MBX_STATUS_MTU_EXCEEDS = 135,
71 OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS = 136,
72 OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS = 137,
73 OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS = 138,
74 OCRDMA_MBX_STATUS_QP_BOUND = 130,
75 OCRDMA_MBX_STATUS_INVALID_CHANGE = 139,
76 OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP = 140,
77 OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER = 141,
78 OCRDMA_MBX_STATUS_MW_STILL_BOUND = 142,
79 OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID = 143,
80 OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS = 144
81};
82
83enum additional_status {
84 OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES = 22
85};
86
87enum cqe_status {
88 OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES = 1,
89 OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER = 2,
90 OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES = 3,
91 OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING = 4,
92 OCRDMA_MBX_CQE_STATUS_DMA_FAILED = 5
93};
94
95static inline void *ocrdma_get_eqe(struct ocrdma_eq *eq)
96{
97 return (u8 *)eq->q.va + (eq->q.tail * sizeof(struct ocrdma_eqe));
98}
99
100static inline void ocrdma_eq_inc_tail(struct ocrdma_eq *eq)
101{
102 eq->q.tail = (eq->q.tail + 1) & (OCRDMA_EQ_LEN - 1);
103}
104
105static inline void *ocrdma_get_mcqe(struct ocrdma_dev *dev)
106{
107 struct ocrdma_mcqe *cqe = (struct ocrdma_mcqe *)
108 ((u8 *) dev->mq.cq.va +
109 (dev->mq.cq.tail * sizeof(struct ocrdma_mcqe)));
110
111 if (!(le32_to_cpu(cqe->valid_ae_cmpl_cons) & OCRDMA_MCQE_VALID_MASK))
112 return NULL;
113 return cqe;
114}
115
116static inline void ocrdma_mcq_inc_tail(struct ocrdma_dev *dev)
117{
118 dev->mq.cq.tail = (dev->mq.cq.tail + 1) & (OCRDMA_MQ_CQ_LEN - 1);
119}
120
121static inline struct ocrdma_mqe *ocrdma_get_mqe(struct ocrdma_dev *dev)
122{
123 return (struct ocrdma_mqe *)((u8 *) dev->mq.sq.va +
124 (dev->mq.sq.head *
125 sizeof(struct ocrdma_mqe)));
126}
127
128static inline void ocrdma_mq_inc_head(struct ocrdma_dev *dev)
129{
130 dev->mq.sq.head = (dev->mq.sq.head + 1) & (OCRDMA_MQ_LEN - 1);
131 atomic_inc(&dev->mq.sq.used);
132}
133
134static inline void *ocrdma_get_mqe_rsp(struct ocrdma_dev *dev)
135{
136 return (void *)((u8 *) dev->mq.sq.va +
137 (dev->mqe_ctx.tag * sizeof(struct ocrdma_mqe)));
138}
139
140enum ib_qp_state get_ibqp_state(enum ocrdma_qp_state qps)
141{
142 switch (qps) {
143 case OCRDMA_QPS_RST:
144 return IB_QPS_RESET;
145 case OCRDMA_QPS_INIT:
146 return IB_QPS_INIT;
147 case OCRDMA_QPS_RTR:
148 return IB_QPS_RTR;
149 case OCRDMA_QPS_RTS:
150 return IB_QPS_RTS;
151 case OCRDMA_QPS_SQD:
152 case OCRDMA_QPS_SQ_DRAINING:
153 return IB_QPS_SQD;
154 case OCRDMA_QPS_SQE:
155 return IB_QPS_SQE;
156 case OCRDMA_QPS_ERR:
157 return IB_QPS_ERR;
158 };
159 return IB_QPS_ERR;
160}
161
Roland Dreierabe3afa2012-04-16 11:36:29 -0700162static enum ocrdma_qp_state get_ocrdma_qp_state(enum ib_qp_state qps)
Parav Panditfe2caef2012-03-21 04:09:06 +0530163{
164 switch (qps) {
165 case IB_QPS_RESET:
166 return OCRDMA_QPS_RST;
167 case IB_QPS_INIT:
168 return OCRDMA_QPS_INIT;
169 case IB_QPS_RTR:
170 return OCRDMA_QPS_RTR;
171 case IB_QPS_RTS:
172 return OCRDMA_QPS_RTS;
173 case IB_QPS_SQD:
174 return OCRDMA_QPS_SQD;
175 case IB_QPS_SQE:
176 return OCRDMA_QPS_SQE;
177 case IB_QPS_ERR:
178 return OCRDMA_QPS_ERR;
179 };
180 return OCRDMA_QPS_ERR;
181}
182
183static int ocrdma_get_mbx_errno(u32 status)
184{
185 int err_num = -EFAULT;
186 u8 mbox_status = (status & OCRDMA_MBX_RSP_STATUS_MASK) >>
187 OCRDMA_MBX_RSP_STATUS_SHIFT;
188 u8 add_status = (status & OCRDMA_MBX_RSP_ASTATUS_MASK) >>
189 OCRDMA_MBX_RSP_ASTATUS_SHIFT;
190
191 switch (mbox_status) {
192 case OCRDMA_MBX_STATUS_OOR:
193 case OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS:
194 err_num = -EAGAIN;
195 break;
196
197 case OCRDMA_MBX_STATUS_INVALID_PD:
198 case OCRDMA_MBX_STATUS_INVALID_CQ:
199 case OCRDMA_MBX_STATUS_INVALID_SRQ_ID:
200 case OCRDMA_MBX_STATUS_INVALID_QP:
201 case OCRDMA_MBX_STATUS_INVALID_CHANGE:
202 case OCRDMA_MBX_STATUS_MTU_EXCEEDS:
203 case OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER:
204 case OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID:
205 case OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS:
206 case OCRDMA_MBX_STATUS_ILLEGAL_FIELD:
207 case OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY:
208 case OCRDMA_MBX_STATUS_INVALID_LKEY:
209 case OCRDMA_MBX_STATUS_INVALID_VA:
210 case OCRDMA_MBX_STATUS_INVALID_LENGTH:
211 case OCRDMA_MBX_STATUS_INVALID_FBO:
212 case OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS:
213 case OCRDMA_MBX_STATUS_INVALID_PBE_SIZE:
214 case OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP:
215 case OCRDMA_MBX_STATUS_SRQ_ERROR:
216 case OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS:
217 err_num = -EINVAL;
218 break;
219
220 case OCRDMA_MBX_STATUS_PD_INUSE:
221 case OCRDMA_MBX_STATUS_QP_BOUND:
222 case OCRDMA_MBX_STATUS_MW_STILL_BOUND:
223 case OCRDMA_MBX_STATUS_MW_BOUND:
224 err_num = -EBUSY;
225 break;
226
227 case OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS:
228 case OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS:
229 case OCRDMA_MBX_STATUS_RQE_EXCEEDS:
230 case OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS:
231 case OCRDMA_MBX_STATUS_ORD_EXCEEDS:
232 case OCRDMA_MBX_STATUS_IRD_EXCEEDS:
233 case OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS:
234 case OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS:
235 case OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS:
236 err_num = -ENOBUFS;
237 break;
238
239 case OCRDMA_MBX_STATUS_FAILED:
240 switch (add_status) {
241 case OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES:
242 err_num = -EAGAIN;
243 break;
244 }
245 default:
246 err_num = -EFAULT;
247 }
248 return err_num;
249}
250
251static int ocrdma_get_mbx_cqe_errno(u16 cqe_status)
252{
253 int err_num = -EINVAL;
254
255 switch (cqe_status) {
256 case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES:
257 err_num = -EPERM;
258 break;
259 case OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER:
260 err_num = -EINVAL;
261 break;
262 case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES:
263 case OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING:
264 err_num = -EAGAIN;
265 break;
266 case OCRDMA_MBX_CQE_STATUS_DMA_FAILED:
267 err_num = -EIO;
268 break;
269 }
270 return err_num;
271}
272
273void ocrdma_ring_cq_db(struct ocrdma_dev *dev, u16 cq_id, bool armed,
274 bool solicited, u16 cqe_popped)
275{
276 u32 val = cq_id & OCRDMA_DB_CQ_RING_ID_MASK;
277
278 val |= ((cq_id & OCRDMA_DB_CQ_RING_ID_EXT_MASK) <<
279 OCRDMA_DB_CQ_RING_ID_EXT_MASK_SHIFT);
280
281 if (armed)
282 val |= (1 << OCRDMA_DB_CQ_REARM_SHIFT);
283 if (solicited)
284 val |= (1 << OCRDMA_DB_CQ_SOLICIT_SHIFT);
285 val |= (cqe_popped << OCRDMA_DB_CQ_NUM_POPPED_SHIFT);
286 iowrite32(val, dev->nic_info.db + OCRDMA_DB_CQ_OFFSET);
287}
288
289static void ocrdma_ring_mq_db(struct ocrdma_dev *dev)
290{
291 u32 val = 0;
292
293 val |= dev->mq.sq.id & OCRDMA_MQ_ID_MASK;
294 val |= 1 << OCRDMA_MQ_NUM_MQE_SHIFT;
295 iowrite32(val, dev->nic_info.db + OCRDMA_DB_MQ_OFFSET);
296}
297
298static void ocrdma_ring_eq_db(struct ocrdma_dev *dev, u16 eq_id,
299 bool arm, bool clear_int, u16 num_eqe)
300{
301 u32 val = 0;
302
303 val |= eq_id & OCRDMA_EQ_ID_MASK;
304 val |= ((eq_id & OCRDMA_EQ_ID_EXT_MASK) << OCRDMA_EQ_ID_EXT_MASK_SHIFT);
305 if (arm)
306 val |= (1 << OCRDMA_REARM_SHIFT);
307 if (clear_int)
308 val |= (1 << OCRDMA_EQ_CLR_SHIFT);
309 val |= (1 << OCRDMA_EQ_TYPE_SHIFT);
310 val |= (num_eqe << OCRDMA_NUM_EQE_SHIFT);
311 iowrite32(val, dev->nic_info.db + OCRDMA_DB_EQ_OFFSET);
312}
313
314static void ocrdma_init_mch(struct ocrdma_mbx_hdr *cmd_hdr,
315 u8 opcode, u8 subsys, u32 cmd_len)
316{
317 cmd_hdr->subsys_op = (opcode | (subsys << OCRDMA_MCH_SUBSYS_SHIFT));
318 cmd_hdr->timeout = 20; /* seconds */
319 cmd_hdr->cmd_len = cmd_len - sizeof(struct ocrdma_mbx_hdr);
320}
321
322static void *ocrdma_init_emb_mqe(u8 opcode, u32 cmd_len)
323{
324 struct ocrdma_mqe *mqe;
325
326 mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
327 if (!mqe)
328 return NULL;
329 mqe->hdr.spcl_sge_cnt_emb |=
330 (OCRDMA_MQE_EMBEDDED << OCRDMA_MQE_HDR_EMB_SHIFT) &
331 OCRDMA_MQE_HDR_EMB_MASK;
332 mqe->hdr.pyld_len = cmd_len - sizeof(struct ocrdma_mqe_hdr);
333
334 ocrdma_init_mch(&mqe->u.emb_req.mch, opcode, OCRDMA_SUBSYS_ROCE,
335 mqe->hdr.pyld_len);
336 return mqe;
337}
338
339static void ocrdma_free_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q)
340{
341 dma_free_coherent(&dev->nic_info.pdev->dev, q->size, q->va, q->dma);
342}
343
344static int ocrdma_alloc_q(struct ocrdma_dev *dev,
345 struct ocrdma_queue_info *q, u16 len, u16 entry_size)
346{
347 memset(q, 0, sizeof(*q));
348 q->len = len;
349 q->entry_size = entry_size;
350 q->size = len * entry_size;
351 q->va = dma_alloc_coherent(&dev->nic_info.pdev->dev, q->size,
352 &q->dma, GFP_KERNEL);
353 if (!q->va)
354 return -ENOMEM;
355 memset(q->va, 0, q->size);
356 return 0;
357}
358
359static void ocrdma_build_q_pages(struct ocrdma_pa *q_pa, int cnt,
360 dma_addr_t host_pa, int hw_page_size)
361{
362 int i;
363
364 for (i = 0; i < cnt; i++) {
365 q_pa[i].lo = (u32) (host_pa & 0xffffffff);
366 q_pa[i].hi = (u32) upper_32_bits(host_pa);
367 host_pa += hw_page_size;
368 }
369}
370
371static void ocrdma_assign_eq_vect_gen2(struct ocrdma_dev *dev,
372 struct ocrdma_eq *eq)
373{
374 /* assign vector and update vector id for next EQ */
375 eq->vector = dev->nic_info.msix.start_vector;
376 dev->nic_info.msix.start_vector += 1;
377}
378
379static void ocrdma_free_eq_vect_gen2(struct ocrdma_dev *dev)
380{
381 /* this assumes that EQs are freed in exactly reverse order
382 * as its allocation.
383 */
384 dev->nic_info.msix.start_vector -= 1;
385}
386
Roland Dreierabe3afa2012-04-16 11:36:29 -0700387static int ocrdma_mbx_delete_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q,
388 int queue_type)
Parav Panditfe2caef2012-03-21 04:09:06 +0530389{
390 u8 opcode = 0;
391 int status;
392 struct ocrdma_delete_q_req *cmd = dev->mbx_cmd;
393
394 switch (queue_type) {
395 case QTYPE_MCCQ:
396 opcode = OCRDMA_CMD_DELETE_MQ;
397 break;
398 case QTYPE_CQ:
399 opcode = OCRDMA_CMD_DELETE_CQ;
400 break;
401 case QTYPE_EQ:
402 opcode = OCRDMA_CMD_DELETE_EQ;
403 break;
404 default:
405 BUG();
406 }
407 memset(cmd, 0, sizeof(*cmd));
408 ocrdma_init_mch(&cmd->req, opcode, OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
409 cmd->id = q->id;
410
411 status = be_roce_mcc_cmd(dev->nic_info.netdev,
412 cmd, sizeof(*cmd), NULL, NULL);
413 if (!status)
414 q->created = false;
415 return status;
416}
417
418static int ocrdma_mbx_create_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
419{
420 int status;
421 struct ocrdma_create_eq_req *cmd = dev->mbx_cmd;
422 struct ocrdma_create_eq_rsp *rsp = dev->mbx_cmd;
423
424 memset(cmd, 0, sizeof(*cmd));
425 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_EQ, OCRDMA_SUBSYS_COMMON,
426 sizeof(*cmd));
427 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY)
428 cmd->req.rsvd_version = 0;
429 else
430 cmd->req.rsvd_version = 2;
431
432 cmd->num_pages = 4;
433 cmd->valid = OCRDMA_CREATE_EQ_VALID;
434 cmd->cnt = 4 << OCRDMA_CREATE_EQ_CNT_SHIFT;
435
436 ocrdma_build_q_pages(&cmd->pa[0], cmd->num_pages, eq->q.dma,
437 PAGE_SIZE_4K);
438 status = be_roce_mcc_cmd(dev->nic_info.netdev, cmd, sizeof(*cmd), NULL,
439 NULL);
440 if (!status) {
441 eq->q.id = rsp->vector_eqid & 0xffff;
442 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY)
443 ocrdma_assign_eq_vect_gen2(dev, eq);
444 else {
445 eq->vector = (rsp->vector_eqid >> 16) & 0xffff;
446 dev->nic_info.msix.start_vector += 1;
447 }
448 eq->q.created = true;
449 }
450 return status;
451}
452
453static int ocrdma_create_eq(struct ocrdma_dev *dev,
454 struct ocrdma_eq *eq, u16 q_len)
455{
456 int status;
457
458 status = ocrdma_alloc_q(dev, &eq->q, OCRDMA_EQ_LEN,
459 sizeof(struct ocrdma_eqe));
460 if (status)
461 return status;
462
463 status = ocrdma_mbx_create_eq(dev, eq);
464 if (status)
465 goto mbx_err;
466 eq->dev = dev;
467 ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
468
469 return 0;
470mbx_err:
471 ocrdma_free_q(dev, &eq->q);
472 return status;
473}
474
475static int ocrdma_get_irq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
476{
477 int irq;
478
479 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
480 irq = dev->nic_info.pdev->irq;
481 else
482 irq = dev->nic_info.msix.vector_list[eq->vector];
483 return irq;
484}
485
486static void _ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
487{
488 if (eq->q.created) {
489 ocrdma_mbx_delete_q(dev, &eq->q, QTYPE_EQ);
490 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY)
491 ocrdma_free_eq_vect_gen2(dev);
492 ocrdma_free_q(dev, &eq->q);
493 }
494}
495
496static void ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
497{
498 int irq;
499
500 /* disarm EQ so that interrupts are not generated
501 * during freeing and EQ delete is in progress.
502 */
503 ocrdma_ring_eq_db(dev, eq->q.id, false, false, 0);
504
505 irq = ocrdma_get_irq(dev, eq);
506 free_irq(irq, eq);
507 _ocrdma_destroy_eq(dev, eq);
508}
509
510static void ocrdma_destroy_qp_eqs(struct ocrdma_dev *dev)
511{
512 int i;
513
514 /* deallocate the data path eqs */
515 for (i = 0; i < dev->eq_cnt; i++)
516 ocrdma_destroy_eq(dev, &dev->qp_eq_tbl[i]);
517}
518
Roland Dreierabe3afa2012-04-16 11:36:29 -0700519static int ocrdma_mbx_mq_cq_create(struct ocrdma_dev *dev,
520 struct ocrdma_queue_info *cq,
521 struct ocrdma_queue_info *eq)
Parav Panditfe2caef2012-03-21 04:09:06 +0530522{
523 struct ocrdma_create_cq_cmd *cmd = dev->mbx_cmd;
524 struct ocrdma_create_cq_cmd_rsp *rsp = dev->mbx_cmd;
525 int status;
526
527 memset(cmd, 0, sizeof(*cmd));
528 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_CQ,
529 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
530
531 cmd->pgsz_pgcnt = PAGES_4K_SPANNED(cq->va, cq->size);
532 cmd->ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
533 cmd->eqn = (eq->id << OCRDMA_CREATE_CQ_EQID_SHIFT);
534
535 ocrdma_build_q_pages(&cmd->pa[0], cmd->pgsz_pgcnt,
536 cq->dma, PAGE_SIZE_4K);
537 status = be_roce_mcc_cmd(dev->nic_info.netdev,
538 cmd, sizeof(*cmd), NULL, NULL);
539 if (!status) {
540 cq->id = (rsp->cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
541 cq->created = true;
542 }
543 return status;
544}
545
546static u32 ocrdma_encoded_q_len(int q_len)
547{
548 u32 len_encoded = fls(q_len); /* log2(len) + 1 */
549
550 if (len_encoded == 16)
551 len_encoded = 0;
552 return len_encoded;
553}
554
555static int ocrdma_mbx_create_mq(struct ocrdma_dev *dev,
556 struct ocrdma_queue_info *mq,
557 struct ocrdma_queue_info *cq)
558{
559 int num_pages, status;
560 struct ocrdma_create_mq_req *cmd = dev->mbx_cmd;
561 struct ocrdma_create_mq_rsp *rsp = dev->mbx_cmd;
562 struct ocrdma_pa *pa;
563
564 memset(cmd, 0, sizeof(*cmd));
565 num_pages = PAGES_4K_SPANNED(mq->va, mq->size);
566
567 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
568 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_MQ,
569 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
570 cmd->v0.pages = num_pages;
571 cmd->v0.async_cqid_valid = OCRDMA_CREATE_MQ_ASYNC_CQ_VALID;
572 cmd->v0.async_cqid_valid = (cq->id << 1);
573 cmd->v0.cqid_ringsize |= (ocrdma_encoded_q_len(mq->len) <<
574 OCRDMA_CREATE_MQ_RING_SIZE_SHIFT);
575 cmd->v0.cqid_ringsize |=
576 (cq->id << OCRDMA_CREATE_MQ_V0_CQ_ID_SHIFT);
577 cmd->v0.valid = OCRDMA_CREATE_MQ_VALID;
578 pa = &cmd->v0.pa[0];
579 } else {
580 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_MQ_EXT,
581 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
582 cmd->req.rsvd_version = 1;
583 cmd->v1.cqid_pages = num_pages;
584 cmd->v1.cqid_pages |= (cq->id << OCRDMA_CREATE_MQ_CQ_ID_SHIFT);
585 cmd->v1.async_cqid_valid = OCRDMA_CREATE_MQ_ASYNC_CQ_VALID;
586 cmd->v1.async_event_bitmap = Bit(20);
587 cmd->v1.async_cqid_ringsize = cq->id;
588 cmd->v1.async_cqid_ringsize |= (ocrdma_encoded_q_len(mq->len) <<
589 OCRDMA_CREATE_MQ_RING_SIZE_SHIFT);
590 cmd->v1.valid = OCRDMA_CREATE_MQ_VALID;
591 pa = &cmd->v1.pa[0];
592 }
593 ocrdma_build_q_pages(pa, num_pages, mq->dma, PAGE_SIZE_4K);
594 status = be_roce_mcc_cmd(dev->nic_info.netdev,
595 cmd, sizeof(*cmd), NULL, NULL);
596 if (!status) {
597 mq->id = rsp->id;
598 mq->created = true;
599 }
600 return status;
601}
602
603static int ocrdma_create_mq(struct ocrdma_dev *dev)
604{
605 int status;
606
607 /* Alloc completion queue for Mailbox queue */
608 status = ocrdma_alloc_q(dev, &dev->mq.cq, OCRDMA_MQ_CQ_LEN,
609 sizeof(struct ocrdma_mcqe));
610 if (status)
611 goto alloc_err;
612
613 status = ocrdma_mbx_mq_cq_create(dev, &dev->mq.cq, &dev->meq.q);
614 if (status)
615 goto mbx_cq_free;
616
617 memset(&dev->mqe_ctx, 0, sizeof(dev->mqe_ctx));
618 init_waitqueue_head(&dev->mqe_ctx.cmd_wait);
619 mutex_init(&dev->mqe_ctx.lock);
620
621 /* Alloc Mailbox queue */
622 status = ocrdma_alloc_q(dev, &dev->mq.sq, OCRDMA_MQ_LEN,
623 sizeof(struct ocrdma_mqe));
624 if (status)
625 goto mbx_cq_destroy;
626 status = ocrdma_mbx_create_mq(dev, &dev->mq.sq, &dev->mq.cq);
627 if (status)
628 goto mbx_q_free;
629 ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, 0);
630 return 0;
631
632mbx_q_free:
633 ocrdma_free_q(dev, &dev->mq.sq);
634mbx_cq_destroy:
635 ocrdma_mbx_delete_q(dev, &dev->mq.cq, QTYPE_CQ);
636mbx_cq_free:
637 ocrdma_free_q(dev, &dev->mq.cq);
638alloc_err:
639 return status;
640}
641
642static void ocrdma_destroy_mq(struct ocrdma_dev *dev)
643{
644 struct ocrdma_queue_info *mbxq, *cq;
645
646 /* mqe_ctx lock synchronizes with any other pending cmds. */
647 mutex_lock(&dev->mqe_ctx.lock);
648 mbxq = &dev->mq.sq;
649 if (mbxq->created) {
650 ocrdma_mbx_delete_q(dev, mbxq, QTYPE_MCCQ);
651 ocrdma_free_q(dev, mbxq);
652 }
653 mutex_unlock(&dev->mqe_ctx.lock);
654
655 cq = &dev->mq.cq;
656 if (cq->created) {
657 ocrdma_mbx_delete_q(dev, cq, QTYPE_CQ);
658 ocrdma_free_q(dev, cq);
659 }
660}
661
662static void ocrdma_process_qpcat_error(struct ocrdma_dev *dev,
663 struct ocrdma_qp *qp)
664{
665 enum ib_qp_state new_ib_qps = IB_QPS_ERR;
666 enum ib_qp_state old_ib_qps;
667
668 if (qp == NULL)
669 BUG();
670 ocrdma_qp_state_machine(qp, new_ib_qps, &old_ib_qps);
671}
672
673static void ocrdma_dispatch_ibevent(struct ocrdma_dev *dev,
674 struct ocrdma_ae_mcqe *cqe)
675{
676 struct ocrdma_qp *qp = NULL;
677 struct ocrdma_cq *cq = NULL;
Roland Dreiere9db2952012-04-16 12:13:24 -0700678 struct ib_event ib_evt;
Parav Panditfe2caef2012-03-21 04:09:06 +0530679 int cq_event = 0;
680 int qp_event = 1;
681 int srq_event = 0;
682 int dev_event = 0;
683 int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
684 OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
685
686 if (cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPVALID)
687 qp = dev->qp_tbl[cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPID_MASK];
688 if (cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQVALID)
689 cq = dev->cq_tbl[cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQID_MASK];
690
Roland Dreiere9db2952012-04-16 12:13:24 -0700691 ib_evt.device = &dev->ibdev;
692
Parav Panditfe2caef2012-03-21 04:09:06 +0530693 switch (type) {
694 case OCRDMA_CQ_ERROR:
695 ib_evt.element.cq = &cq->ibcq;
696 ib_evt.event = IB_EVENT_CQ_ERR;
697 cq_event = 1;
698 qp_event = 0;
699 break;
700 case OCRDMA_CQ_OVERRUN_ERROR:
701 ib_evt.element.cq = &cq->ibcq;
702 ib_evt.event = IB_EVENT_CQ_ERR;
703 break;
704 case OCRDMA_CQ_QPCAT_ERROR:
705 ib_evt.element.qp = &qp->ibqp;
706 ib_evt.event = IB_EVENT_QP_FATAL;
707 ocrdma_process_qpcat_error(dev, qp);
708 break;
709 case OCRDMA_QP_ACCESS_ERROR:
710 ib_evt.element.qp = &qp->ibqp;
711 ib_evt.event = IB_EVENT_QP_ACCESS_ERR;
712 break;
713 case OCRDMA_QP_COMM_EST_EVENT:
714 ib_evt.element.qp = &qp->ibqp;
715 ib_evt.event = IB_EVENT_COMM_EST;
716 break;
717 case OCRDMA_SQ_DRAINED_EVENT:
718 ib_evt.element.qp = &qp->ibqp;
719 ib_evt.event = IB_EVENT_SQ_DRAINED;
720 break;
721 case OCRDMA_DEVICE_FATAL_EVENT:
722 ib_evt.element.port_num = 1;
723 ib_evt.event = IB_EVENT_DEVICE_FATAL;
724 qp_event = 0;
725 dev_event = 1;
726 break;
727 case OCRDMA_SRQCAT_ERROR:
728 ib_evt.element.srq = &qp->srq->ibsrq;
729 ib_evt.event = IB_EVENT_SRQ_ERR;
730 srq_event = 1;
731 qp_event = 0;
732 break;
733 case OCRDMA_SRQ_LIMIT_EVENT:
734 ib_evt.element.srq = &qp->srq->ibsrq;
Parav Pandit804eaf22012-05-23 21:11:17 +0530735 ib_evt.event = IB_EVENT_SRQ_LIMIT_REACHED;
Parav Panditfe2caef2012-03-21 04:09:06 +0530736 srq_event = 1;
737 qp_event = 0;
738 break;
739 case OCRDMA_QP_LAST_WQE_EVENT:
740 ib_evt.element.qp = &qp->ibqp;
741 ib_evt.event = IB_EVENT_QP_LAST_WQE_REACHED;
742 break;
743 default:
744 cq_event = 0;
745 qp_event = 0;
746 srq_event = 0;
747 dev_event = 0;
748 ocrdma_err("%s() unknown type=0x%x\n", __func__, type);
749 break;
750 }
751
752 if (qp_event) {
753 if (qp->ibqp.event_handler)
754 qp->ibqp.event_handler(&ib_evt, qp->ibqp.qp_context);
755 } else if (cq_event) {
756 if (cq->ibcq.event_handler)
757 cq->ibcq.event_handler(&ib_evt, cq->ibcq.cq_context);
758 } else if (srq_event) {
759 if (qp->srq->ibsrq.event_handler)
760 qp->srq->ibsrq.event_handler(&ib_evt,
761 qp->srq->ibsrq.
762 srq_context);
763 } else if (dev_event)
764 ib_dispatch_event(&ib_evt);
765
766}
767
768static void ocrdma_process_acqe(struct ocrdma_dev *dev, void *ae_cqe)
769{
770 /* async CQE processing */
771 struct ocrdma_ae_mcqe *cqe = ae_cqe;
772 u32 evt_code = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_CODE_MASK) >>
773 OCRDMA_AE_MCQE_EVENT_CODE_SHIFT;
774
775 if (evt_code == OCRDMA_ASYNC_EVE_CODE)
776 ocrdma_dispatch_ibevent(dev, cqe);
777 else
778 ocrdma_err("%s(%d) invalid evt code=0x%x\n",
779 __func__, dev->id, evt_code);
780}
781
782static void ocrdma_process_mcqe(struct ocrdma_dev *dev, struct ocrdma_mcqe *cqe)
783{
784 if (dev->mqe_ctx.tag == cqe->tag_lo && dev->mqe_ctx.cmd_done == false) {
785 dev->mqe_ctx.cqe_status = (cqe->status &
786 OCRDMA_MCQE_STATUS_MASK) >> OCRDMA_MCQE_STATUS_SHIFT;
787 dev->mqe_ctx.ext_status =
788 (cqe->status & OCRDMA_MCQE_ESTATUS_MASK)
789 >> OCRDMA_MCQE_ESTATUS_SHIFT;
790 dev->mqe_ctx.cmd_done = true;
791 wake_up(&dev->mqe_ctx.cmd_wait);
792 } else
793 ocrdma_err("%s() cqe for invalid tag0x%x.expected=0x%x\n",
794 __func__, cqe->tag_lo, dev->mqe_ctx.tag);
795}
796
797static int ocrdma_mq_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
798{
799 u16 cqe_popped = 0;
800 struct ocrdma_mcqe *cqe;
801
802 while (1) {
803 cqe = ocrdma_get_mcqe(dev);
804 if (cqe == NULL)
805 break;
806 ocrdma_le32_to_cpu(cqe, sizeof(*cqe));
807 cqe_popped += 1;
808 if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_AE_MASK)
809 ocrdma_process_acqe(dev, cqe);
810 else if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_CMPL_MASK)
811 ocrdma_process_mcqe(dev, cqe);
812 else
813 ocrdma_err("%s() cqe->compl is not set.\n", __func__);
814 memset(cqe, 0, sizeof(struct ocrdma_mcqe));
815 ocrdma_mcq_inc_tail(dev);
816 }
817 ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, cqe_popped);
818 return 0;
819}
820
821static void ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
822 struct ocrdma_cq *cq)
823{
824 unsigned long flags;
825 struct ocrdma_qp *qp;
826 bool buddy_cq_found = false;
827 /* Go through list of QPs in error state which are using this CQ
828 * and invoke its callback handler to trigger CQE processing for
829 * error/flushed CQE. It is rare to find more than few entries in
830 * this list as most consumers stops after getting error CQE.
831 * List is traversed only once when a matching buddy cq found for a QP.
832 */
833 spin_lock_irqsave(&dev->flush_q_lock, flags);
834 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
835 if (qp->srq)
836 continue;
837 /* if wq and rq share the same cq, than comp_handler
838 * is already invoked.
839 */
840 if (qp->sq_cq == qp->rq_cq)
841 continue;
842 /* if completion came on sq, rq's cq is buddy cq.
843 * if completion came on rq, sq's cq is buddy cq.
844 */
845 if (qp->sq_cq == cq)
846 cq = qp->rq_cq;
847 else
848 cq = qp->sq_cq;
849 buddy_cq_found = true;
850 break;
851 }
852 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
853 if (buddy_cq_found == false)
854 return;
855 if (cq->ibcq.comp_handler) {
856 spin_lock_irqsave(&cq->comp_handler_lock, flags);
857 (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
858 spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
859 }
860}
861
862static void ocrdma_qp_cq_handler(struct ocrdma_dev *dev, u16 cq_idx)
863{
864 unsigned long flags;
865 struct ocrdma_cq *cq;
866
867 if (cq_idx >= OCRDMA_MAX_CQ)
868 BUG();
869
870 cq = dev->cq_tbl[cq_idx];
871 if (cq == NULL) {
872 ocrdma_err("%s%d invalid id=0x%x\n", __func__, dev->id, cq_idx);
873 return;
874 }
875 spin_lock_irqsave(&cq->cq_lock, flags);
876 cq->armed = false;
877 cq->solicited = false;
878 spin_unlock_irqrestore(&cq->cq_lock, flags);
879
880 ocrdma_ring_cq_db(dev, cq->id, false, false, 0);
881
882 if (cq->ibcq.comp_handler) {
883 spin_lock_irqsave(&cq->comp_handler_lock, flags);
884 (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
885 spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
886 }
887 ocrdma_qp_buddy_cq_handler(dev, cq);
888}
889
890static void ocrdma_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
891{
892 /* process the MQ-CQE. */
893 if (cq_id == dev->mq.cq.id)
894 ocrdma_mq_cq_handler(dev, cq_id);
895 else
896 ocrdma_qp_cq_handler(dev, cq_id);
897}
898
899static irqreturn_t ocrdma_irq_handler(int irq, void *handle)
900{
901 struct ocrdma_eq *eq = handle;
902 struct ocrdma_dev *dev = eq->dev;
903 struct ocrdma_eqe eqe;
904 struct ocrdma_eqe *ptr;
905 u16 eqe_popped = 0;
906 u16 cq_id;
907 while (1) {
908 ptr = ocrdma_get_eqe(eq);
909 eqe = *ptr;
910 ocrdma_le32_to_cpu(&eqe, sizeof(eqe));
911 if ((eqe.id_valid & OCRDMA_EQE_VALID_MASK) == 0)
912 break;
913 eqe_popped += 1;
914 ptr->id_valid = 0;
915 /* check whether its CQE or not. */
916 if ((eqe.id_valid & OCRDMA_EQE_FOR_CQE_MASK) == 0) {
917 cq_id = eqe.id_valid >> OCRDMA_EQE_RESOURCE_ID_SHIFT;
918 ocrdma_cq_handler(dev, cq_id);
919 }
920 ocrdma_eq_inc_tail(eq);
921 }
922 ocrdma_ring_eq_db(dev, eq->q.id, true, true, eqe_popped);
923 /* Ring EQ doorbell with num_popped to 0 to enable interrupts again. */
924 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
925 ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
926 return IRQ_HANDLED;
927}
928
929static void ocrdma_post_mqe(struct ocrdma_dev *dev, struct ocrdma_mqe *cmd)
930{
931 struct ocrdma_mqe *mqe;
932
933 dev->mqe_ctx.tag = dev->mq.sq.head;
934 dev->mqe_ctx.cmd_done = false;
935 mqe = ocrdma_get_mqe(dev);
936 cmd->hdr.tag_lo = dev->mq.sq.head;
937 ocrdma_copy_cpu_to_le32(mqe, cmd, sizeof(*mqe));
938 /* make sure descriptor is written before ringing doorbell */
939 wmb();
940 ocrdma_mq_inc_head(dev);
941 ocrdma_ring_mq_db(dev);
942}
943
944static int ocrdma_wait_mqe_cmpl(struct ocrdma_dev *dev)
945{
946 long status;
947 /* 30 sec timeout */
948 status = wait_event_timeout(dev->mqe_ctx.cmd_wait,
949 (dev->mqe_ctx.cmd_done != false),
950 msecs_to_jiffies(30000));
951 if (status)
952 return 0;
953 else
954 return -1;
955}
956
957/* issue a mailbox command on the MQ */
958static int ocrdma_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe)
959{
960 int status = 0;
961 u16 cqe_status, ext_status;
962 struct ocrdma_mqe *rsp;
963
964 mutex_lock(&dev->mqe_ctx.lock);
965 ocrdma_post_mqe(dev, mqe);
966 status = ocrdma_wait_mqe_cmpl(dev);
967 if (status)
968 goto mbx_err;
969 cqe_status = dev->mqe_ctx.cqe_status;
970 ext_status = dev->mqe_ctx.ext_status;
971 rsp = ocrdma_get_mqe_rsp(dev);
972 ocrdma_copy_le32_to_cpu(mqe, rsp, (sizeof(*mqe)));
973 if (cqe_status || ext_status) {
974 ocrdma_err
975 ("%s() opcode=0x%x, cqe_status=0x%x, ext_status=0x%x\n",
976 __func__,
977 (rsp->u.rsp.subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
978 OCRDMA_MBX_RSP_OPCODE_SHIFT, cqe_status, ext_status);
979 status = ocrdma_get_mbx_cqe_errno(cqe_status);
980 goto mbx_err;
981 }
982 if (mqe->u.rsp.status & OCRDMA_MBX_RSP_STATUS_MASK)
983 status = ocrdma_get_mbx_errno(mqe->u.rsp.status);
984mbx_err:
985 mutex_unlock(&dev->mqe_ctx.lock);
986 return status;
987}
988
989static void ocrdma_get_attr(struct ocrdma_dev *dev,
990 struct ocrdma_dev_attr *attr,
991 struct ocrdma_mbx_query_config *rsp)
992{
Parav Panditfe2caef2012-03-21 04:09:06 +0530993 attr->max_pd =
994 (rsp->max_pd_ca_ack_delay & OCRDMA_MBX_QUERY_CFG_MAX_PD_MASK) >>
995 OCRDMA_MBX_QUERY_CFG_MAX_PD_SHIFT;
996 attr->max_qp =
997 (rsp->qp_srq_cq_ird_ord & OCRDMA_MBX_QUERY_CFG_MAX_QP_MASK) >>
998 OCRDMA_MBX_QUERY_CFG_MAX_QP_SHIFT;
999 attr->max_send_sge = ((rsp->max_write_send_sge &
1000 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
1001 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT);
1002 attr->max_recv_sge = (rsp->max_write_send_sge &
1003 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
1004 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT;
1005 attr->max_ord_per_qp = (rsp->max_ird_ord_per_qp &
1006 OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_MASK) >>
1007 OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_SHIFT;
1008 attr->max_ird_per_qp = (rsp->max_ird_ord_per_qp &
1009 OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_MASK) >>
1010 OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_SHIFT;
1011 attr->cq_overflow_detect = (rsp->qp_srq_cq_ird_ord &
1012 OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_MASK) >>
1013 OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_SHIFT;
1014 attr->srq_supported = (rsp->qp_srq_cq_ird_ord &
1015 OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_MASK) >>
1016 OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_SHIFT;
1017 attr->local_ca_ack_delay = (rsp->max_pd_ca_ack_delay &
1018 OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_MASK) >>
1019 OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_SHIFT;
1020 attr->max_mr = rsp->max_mr;
1021 attr->max_mr_size = ~0ull;
1022 attr->max_fmr = 0;
1023 attr->max_pages_per_frmr = rsp->max_pages_per_frmr;
1024 attr->max_num_mr_pbl = rsp->max_num_mr_pbl;
1025 attr->max_cqe = rsp->max_cq_cqes_per_cq &
1026 OCRDMA_MBX_QUERY_CFG_MAX_CQES_PER_CQ_MASK;
1027 attr->wqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1028 OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_MASK) >>
1029 OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_OFFSET) *
1030 OCRDMA_WQE_STRIDE;
1031 attr->rqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1032 OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_MASK) >>
1033 OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_OFFSET) *
1034 OCRDMA_WQE_STRIDE;
1035 attr->max_inline_data =
1036 attr->wqe_size - (sizeof(struct ocrdma_hdr_wqe) +
1037 sizeof(struct ocrdma_sge));
Parav Panditfe2caef2012-03-21 04:09:06 +05301038 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301039 attr->ird = 1;
1040 attr->ird_page_size = OCRDMA_MIN_Q_PAGE_SIZE;
1041 attr->num_ird_pages = MAX_OCRDMA_IRD_PAGES;
Mahesh Vardhamanaiah07bb5422012-06-08 21:25:52 +05301042 }
1043 dev->attr.max_wqe = rsp->max_wqes_rqes_per_q >>
1044 OCRDMA_MBX_QUERY_CFG_MAX_WQES_PER_WQ_OFFSET;
1045 dev->attr.max_rqe = rsp->max_wqes_rqes_per_q &
1046 OCRDMA_MBX_QUERY_CFG_MAX_RQES_PER_RQ_MASK;
Parav Panditfe2caef2012-03-21 04:09:06 +05301047}
1048
1049static int ocrdma_check_fw_config(struct ocrdma_dev *dev,
1050 struct ocrdma_fw_conf_rsp *conf)
1051{
1052 u32 fn_mode;
1053
1054 fn_mode = conf->fn_mode & OCRDMA_FN_MODE_RDMA;
1055 if (fn_mode != OCRDMA_FN_MODE_RDMA)
1056 return -EINVAL;
1057 dev->base_eqid = conf->base_eqid;
1058 dev->max_eq = conf->max_eq;
1059 dev->attr.max_cq = OCRDMA_MAX_CQ - 1;
1060 return 0;
1061}
1062
1063/* can be issued only during init time. */
1064static int ocrdma_mbx_query_fw_ver(struct ocrdma_dev *dev)
1065{
1066 int status = -ENOMEM;
1067 struct ocrdma_mqe *cmd;
1068 struct ocrdma_fw_ver_rsp *rsp;
1069
1070 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_VER, sizeof(*cmd));
1071 if (!cmd)
1072 return -ENOMEM;
1073 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1074 OCRDMA_CMD_GET_FW_VER,
1075 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1076
1077 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1078 if (status)
1079 goto mbx_err;
1080 rsp = (struct ocrdma_fw_ver_rsp *)cmd;
1081 memset(&dev->attr.fw_ver[0], 0, sizeof(dev->attr.fw_ver));
1082 memcpy(&dev->attr.fw_ver[0], &rsp->running_ver[0],
1083 sizeof(rsp->running_ver));
1084 ocrdma_le32_to_cpu(dev->attr.fw_ver, sizeof(rsp->running_ver));
1085mbx_err:
1086 kfree(cmd);
1087 return status;
1088}
1089
1090/* can be issued only during init time. */
1091static int ocrdma_mbx_query_fw_config(struct ocrdma_dev *dev)
1092{
1093 int status = -ENOMEM;
1094 struct ocrdma_mqe *cmd;
1095 struct ocrdma_fw_conf_rsp *rsp;
1096
1097 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_CONFIG, sizeof(*cmd));
1098 if (!cmd)
1099 return -ENOMEM;
1100 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1101 OCRDMA_CMD_GET_FW_CONFIG,
1102 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1103 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1104 if (status)
1105 goto mbx_err;
1106 rsp = (struct ocrdma_fw_conf_rsp *)cmd;
1107 status = ocrdma_check_fw_config(dev, rsp);
1108mbx_err:
1109 kfree(cmd);
1110 return status;
1111}
1112
1113static int ocrdma_mbx_query_dev(struct ocrdma_dev *dev)
1114{
1115 int status = -ENOMEM;
1116 struct ocrdma_mbx_query_config *rsp;
1117 struct ocrdma_mqe *cmd;
1118
1119 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_CONFIG, sizeof(*cmd));
1120 if (!cmd)
1121 return status;
1122 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1123 if (status)
1124 goto mbx_err;
1125 rsp = (struct ocrdma_mbx_query_config *)cmd;
1126 ocrdma_get_attr(dev, &dev->attr, rsp);
1127mbx_err:
1128 kfree(cmd);
1129 return status;
1130}
1131
1132int ocrdma_mbx_alloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1133{
1134 int status = -ENOMEM;
1135 struct ocrdma_alloc_pd *cmd;
1136 struct ocrdma_alloc_pd_rsp *rsp;
1137
1138 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD, sizeof(*cmd));
1139 if (!cmd)
1140 return status;
1141 if (pd->dpp_enabled)
1142 cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
1143 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1144 if (status)
1145 goto mbx_err;
1146 rsp = (struct ocrdma_alloc_pd_rsp *)cmd;
1147 pd->id = rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_PDID_MASK;
1148 if (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) {
1149 pd->dpp_enabled = true;
1150 pd->dpp_page = rsp->dpp_page_pdid >>
1151 OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
1152 } else {
1153 pd->dpp_enabled = false;
1154 pd->num_dpp_qp = 0;
1155 }
1156mbx_err:
1157 kfree(cmd);
1158 return status;
1159}
1160
1161int ocrdma_mbx_dealloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1162{
1163 int status = -ENOMEM;
1164 struct ocrdma_dealloc_pd *cmd;
1165
1166 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD, sizeof(*cmd));
1167 if (!cmd)
1168 return status;
1169 cmd->id = pd->id;
1170 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1171 kfree(cmd);
1172 return status;
1173}
1174
1175static int ocrdma_build_q_conf(u32 *num_entries, int entry_size,
1176 int *num_pages, int *page_size)
1177{
1178 int i;
1179 int mem_size;
1180
1181 *num_entries = roundup_pow_of_two(*num_entries);
1182 mem_size = *num_entries * entry_size;
1183 /* find the possible lowest possible multiplier */
1184 for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1185 if (mem_size <= (OCRDMA_Q_PAGE_BASE_SIZE << i))
1186 break;
1187 }
1188 if (i >= OCRDMA_MAX_Q_PAGE_SIZE_CNT)
1189 return -EINVAL;
1190 mem_size = roundup(mem_size,
1191 ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES));
1192 *num_pages =
1193 mem_size / ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1194 *page_size = ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1195 *num_entries = mem_size / entry_size;
1196 return 0;
1197}
1198
1199static int ocrdma_mbx_create_ah_tbl(struct ocrdma_dev *dev)
1200{
1201 int i ;
1202 int status = 0;
1203 int max_ah;
1204 struct ocrdma_create_ah_tbl *cmd;
1205 struct ocrdma_create_ah_tbl_rsp *rsp;
1206 struct pci_dev *pdev = dev->nic_info.pdev;
1207 dma_addr_t pa;
1208 struct ocrdma_pbe *pbes;
1209
1210 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_AH_TBL, sizeof(*cmd));
1211 if (!cmd)
1212 return status;
1213
1214 max_ah = OCRDMA_MAX_AH;
1215 dev->av_tbl.size = sizeof(struct ocrdma_av) * max_ah;
1216
1217 /* number of PBEs in PBL */
1218 cmd->ah_conf = (OCRDMA_AH_TBL_PAGES <<
1219 OCRDMA_CREATE_AH_NUM_PAGES_SHIFT) &
1220 OCRDMA_CREATE_AH_NUM_PAGES_MASK;
1221
1222 /* page size */
1223 for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1224 if (PAGE_SIZE == (OCRDMA_MIN_Q_PAGE_SIZE << i))
1225 break;
1226 }
1227 cmd->ah_conf |= (i << OCRDMA_CREATE_AH_PAGE_SIZE_SHIFT) &
1228 OCRDMA_CREATE_AH_PAGE_SIZE_MASK;
1229
1230 /* ah_entry size */
1231 cmd->ah_conf |= (sizeof(struct ocrdma_av) <<
1232 OCRDMA_CREATE_AH_ENTRY_SIZE_SHIFT) &
1233 OCRDMA_CREATE_AH_ENTRY_SIZE_MASK;
1234
1235 dev->av_tbl.pbl.va = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
1236 &dev->av_tbl.pbl.pa,
1237 GFP_KERNEL);
1238 if (dev->av_tbl.pbl.va == NULL)
1239 goto mem_err;
1240
1241 dev->av_tbl.va = dma_alloc_coherent(&pdev->dev, dev->av_tbl.size,
1242 &pa, GFP_KERNEL);
1243 if (dev->av_tbl.va == NULL)
1244 goto mem_err_ah;
1245 dev->av_tbl.pa = pa;
1246 dev->av_tbl.num_ah = max_ah;
1247 memset(dev->av_tbl.va, 0, dev->av_tbl.size);
1248
1249 pbes = (struct ocrdma_pbe *)dev->av_tbl.pbl.va;
1250 for (i = 0; i < dev->av_tbl.size / OCRDMA_MIN_Q_PAGE_SIZE; i++) {
1251 pbes[i].pa_lo = (u32) (pa & 0xffffffff);
1252 pbes[i].pa_hi = (u32) upper_32_bits(pa);
1253 pa += PAGE_SIZE;
1254 }
1255 cmd->tbl_addr[0].lo = (u32)(dev->av_tbl.pbl.pa & 0xFFFFFFFF);
1256 cmd->tbl_addr[0].hi = (u32)upper_32_bits(dev->av_tbl.pbl.pa);
1257 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1258 if (status)
1259 goto mbx_err;
1260 rsp = (struct ocrdma_create_ah_tbl_rsp *)cmd;
1261 dev->av_tbl.ahid = rsp->ahid & 0xFFFF;
1262 kfree(cmd);
1263 return 0;
1264
1265mbx_err:
1266 dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1267 dev->av_tbl.pa);
1268 dev->av_tbl.va = NULL;
1269mem_err_ah:
1270 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1271 dev->av_tbl.pbl.pa);
1272 dev->av_tbl.pbl.va = NULL;
1273 dev->av_tbl.size = 0;
1274mem_err:
1275 kfree(cmd);
1276 return status;
1277}
1278
1279static void ocrdma_mbx_delete_ah_tbl(struct ocrdma_dev *dev)
1280{
1281 struct ocrdma_delete_ah_tbl *cmd;
1282 struct pci_dev *pdev = dev->nic_info.pdev;
1283
1284 if (dev->av_tbl.va == NULL)
1285 return;
1286
1287 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_AH_TBL, sizeof(*cmd));
1288 if (!cmd)
1289 return;
1290 cmd->ahid = dev->av_tbl.ahid;
1291
1292 ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1293 dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1294 dev->av_tbl.pa);
1295 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1296 dev->av_tbl.pbl.pa);
1297 kfree(cmd);
1298}
1299
1300/* Multiple CQs uses the EQ. This routine returns least used
1301 * EQ to associate with CQ. This will distributes the interrupt
1302 * processing and CPU load to associated EQ, vector and so to that CPU.
1303 */
1304static u16 ocrdma_bind_eq(struct ocrdma_dev *dev)
1305{
1306 int i, selected_eq = 0, cq_cnt = 0;
1307 u16 eq_id;
1308
1309 mutex_lock(&dev->dev_lock);
1310 cq_cnt = dev->qp_eq_tbl[0].cq_cnt;
1311 eq_id = dev->qp_eq_tbl[0].q.id;
1312 /* find the EQ which is has the least number of
1313 * CQs associated with it.
1314 */
1315 for (i = 0; i < dev->eq_cnt; i++) {
1316 if (dev->qp_eq_tbl[i].cq_cnt < cq_cnt) {
1317 cq_cnt = dev->qp_eq_tbl[i].cq_cnt;
1318 eq_id = dev->qp_eq_tbl[i].q.id;
1319 selected_eq = i;
1320 }
1321 }
1322 dev->qp_eq_tbl[selected_eq].cq_cnt += 1;
1323 mutex_unlock(&dev->dev_lock);
1324 return eq_id;
1325}
1326
1327static void ocrdma_unbind_eq(struct ocrdma_dev *dev, u16 eq_id)
1328{
1329 int i;
1330
1331 mutex_lock(&dev->dev_lock);
1332 for (i = 0; i < dev->eq_cnt; i++) {
1333 if (dev->qp_eq_tbl[i].q.id != eq_id)
1334 continue;
1335 dev->qp_eq_tbl[i].cq_cnt -= 1;
1336 break;
1337 }
1338 mutex_unlock(&dev->dev_lock);
1339}
1340
1341int ocrdma_mbx_create_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
1342 int entries, int dpp_cq)
1343{
1344 int status = -ENOMEM; int max_hw_cqe;
1345 struct pci_dev *pdev = dev->nic_info.pdev;
1346 struct ocrdma_create_cq *cmd;
1347 struct ocrdma_create_cq_rsp *rsp;
1348 u32 hw_pages, cqe_size, page_size, cqe_count;
1349
1350 if (dpp_cq)
1351 return -EINVAL;
1352 if (entries > dev->attr.max_cqe) {
1353 ocrdma_err("%s(%d) max_cqe=0x%x, requester_cqe=0x%x\n",
1354 __func__, dev->id, dev->attr.max_cqe, entries);
1355 return -EINVAL;
1356 }
1357 if (dpp_cq && (dev->nic_info.dev_family != OCRDMA_GEN2_FAMILY))
1358 return -EINVAL;
1359
1360 if (dpp_cq) {
1361 cq->max_hw_cqe = 1;
1362 max_hw_cqe = 1;
1363 cqe_size = OCRDMA_DPP_CQE_SIZE;
1364 hw_pages = 1;
1365 } else {
1366 cq->max_hw_cqe = dev->attr.max_cqe;
1367 max_hw_cqe = dev->attr.max_cqe;
1368 cqe_size = sizeof(struct ocrdma_cqe);
1369 hw_pages = OCRDMA_CREATE_CQ_MAX_PAGES;
1370 }
1371
1372 cq->len = roundup(max_hw_cqe * cqe_size, OCRDMA_MIN_Q_PAGE_SIZE);
1373
1374 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_CQ, sizeof(*cmd));
1375 if (!cmd)
1376 return -ENOMEM;
1377 ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_CREATE_CQ,
1378 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1379 cq->va = dma_alloc_coherent(&pdev->dev, cq->len, &cq->pa, GFP_KERNEL);
1380 if (!cq->va) {
1381 status = -ENOMEM;
1382 goto mem_err;
1383 }
1384 memset(cq->va, 0, cq->len);
1385 page_size = cq->len / hw_pages;
1386 cmd->cmd.pgsz_pgcnt = (page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
1387 OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
1388 cmd->cmd.pgsz_pgcnt |= hw_pages;
1389 cmd->cmd.ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
1390
1391 if (dev->eq_cnt < 0)
1392 goto eq_err;
1393 cq->eqn = ocrdma_bind_eq(dev);
1394 cmd->cmd.req.rsvd_version = OCRDMA_CREATE_CQ_VER2;
1395 cqe_count = cq->len / cqe_size;
1396 if (cqe_count > 1024)
1397 /* Set cnt to 3 to indicate more than 1024 cq entries */
1398 cmd->cmd.ev_cnt_flags |= (0x3 << OCRDMA_CREATE_CQ_CNT_SHIFT);
1399 else {
1400 u8 count = 0;
1401 switch (cqe_count) {
1402 case 256:
1403 count = 0;
1404 break;
1405 case 512:
1406 count = 1;
1407 break;
1408 case 1024:
1409 count = 2;
1410 break;
1411 default:
1412 goto mbx_err;
1413 }
1414 cmd->cmd.ev_cnt_flags |= (count << OCRDMA_CREATE_CQ_CNT_SHIFT);
1415 }
1416 /* shared eq between all the consumer cqs. */
1417 cmd->cmd.eqn = cq->eqn;
1418 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
1419 if (dpp_cq)
1420 cmd->cmd.pgsz_pgcnt |= OCRDMA_CREATE_CQ_DPP <<
1421 OCRDMA_CREATE_CQ_TYPE_SHIFT;
1422 cq->phase_change = false;
1423 cmd->cmd.cqe_count = (cq->len / cqe_size);
1424 } else {
1425 cmd->cmd.cqe_count = (cq->len / cqe_size) - 1;
1426 cmd->cmd.ev_cnt_flags |= OCRDMA_CREATE_CQ_FLAGS_AUTO_VALID;
1427 cq->phase_change = true;
1428 }
1429
1430 ocrdma_build_q_pages(&cmd->cmd.pa[0], hw_pages, cq->pa, page_size);
1431 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1432 if (status)
1433 goto mbx_err;
1434
1435 rsp = (struct ocrdma_create_cq_rsp *)cmd;
1436 cq->id = (u16) (rsp->rsp.cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
1437 kfree(cmd);
1438 return 0;
1439mbx_err:
1440 ocrdma_unbind_eq(dev, cq->eqn);
1441eq_err:
1442 dma_free_coherent(&pdev->dev, cq->len, cq->va, cq->pa);
1443mem_err:
1444 kfree(cmd);
1445 return status;
1446}
1447
1448int ocrdma_mbx_destroy_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq)
1449{
1450 int status = -ENOMEM;
1451 struct ocrdma_destroy_cq *cmd;
1452
1453 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_CQ, sizeof(*cmd));
1454 if (!cmd)
1455 return status;
1456 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_DELETE_CQ,
1457 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1458
1459 cmd->bypass_flush_qid |=
1460 (cq->id << OCRDMA_DESTROY_CQ_QID_SHIFT) &
1461 OCRDMA_DESTROY_CQ_QID_MASK;
1462
1463 ocrdma_unbind_eq(dev, cq->eqn);
1464 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1465 if (status)
1466 goto mbx_err;
1467 dma_free_coherent(&dev->nic_info.pdev->dev, cq->len, cq->va, cq->pa);
1468mbx_err:
1469 kfree(cmd);
1470 return status;
1471}
1472
1473int ocrdma_mbx_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1474 u32 pdid, int addr_check)
1475{
1476 int status = -ENOMEM;
1477 struct ocrdma_alloc_lkey *cmd;
1478 struct ocrdma_alloc_lkey_rsp *rsp;
1479
1480 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_LKEY, sizeof(*cmd));
1481 if (!cmd)
1482 return status;
1483 cmd->pdid = pdid;
1484 cmd->pbl_sz_flags |= addr_check;
1485 cmd->pbl_sz_flags |= (hwmr->fr_mr << OCRDMA_ALLOC_LKEY_FMR_SHIFT);
1486 cmd->pbl_sz_flags |=
1487 (hwmr->remote_wr << OCRDMA_ALLOC_LKEY_REMOTE_WR_SHIFT);
1488 cmd->pbl_sz_flags |=
1489 (hwmr->remote_rd << OCRDMA_ALLOC_LKEY_REMOTE_RD_SHIFT);
1490 cmd->pbl_sz_flags |=
1491 (hwmr->local_wr << OCRDMA_ALLOC_LKEY_LOCAL_WR_SHIFT);
1492 cmd->pbl_sz_flags |=
1493 (hwmr->remote_atomic << OCRDMA_ALLOC_LKEY_REMOTE_ATOMIC_SHIFT);
1494 cmd->pbl_sz_flags |=
1495 (hwmr->num_pbls << OCRDMA_ALLOC_LKEY_PBL_SIZE_SHIFT);
1496
1497 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1498 if (status)
1499 goto mbx_err;
1500 rsp = (struct ocrdma_alloc_lkey_rsp *)cmd;
1501 hwmr->lkey = rsp->lrkey;
1502mbx_err:
1503 kfree(cmd);
1504 return status;
1505}
1506
1507int ocrdma_mbx_dealloc_lkey(struct ocrdma_dev *dev, int fr_mr, u32 lkey)
1508{
1509 int status = -ENOMEM;
1510 struct ocrdma_dealloc_lkey *cmd;
1511
1512 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_LKEY, sizeof(*cmd));
1513 if (!cmd)
1514 return -ENOMEM;
1515 cmd->lkey = lkey;
1516 cmd->rsvd_frmr = fr_mr ? 1 : 0;
1517 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1518 if (status)
1519 goto mbx_err;
1520mbx_err:
1521 kfree(cmd);
1522 return status;
1523}
1524
1525static int ocrdma_mbx_reg_mr(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1526 u32 pdid, u32 pbl_cnt, u32 pbe_size, u32 last)
1527{
1528 int status = -ENOMEM;
1529 int i;
1530 struct ocrdma_reg_nsmr *cmd;
1531 struct ocrdma_reg_nsmr_rsp *rsp;
1532
1533 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR, sizeof(*cmd));
1534 if (!cmd)
1535 return -ENOMEM;
1536 cmd->num_pbl_pdid =
1537 pdid | (hwmr->num_pbls << OCRDMA_REG_NSMR_NUM_PBL_SHIFT);
1538
1539 cmd->flags_hpage_pbe_sz |= (hwmr->remote_wr <<
1540 OCRDMA_REG_NSMR_REMOTE_WR_SHIFT);
1541 cmd->flags_hpage_pbe_sz |= (hwmr->remote_rd <<
1542 OCRDMA_REG_NSMR_REMOTE_RD_SHIFT);
1543 cmd->flags_hpage_pbe_sz |= (hwmr->local_wr <<
1544 OCRDMA_REG_NSMR_LOCAL_WR_SHIFT);
1545 cmd->flags_hpage_pbe_sz |= (hwmr->remote_atomic <<
1546 OCRDMA_REG_NSMR_REMOTE_ATOMIC_SHIFT);
1547 cmd->flags_hpage_pbe_sz |= (hwmr->mw_bind <<
1548 OCRDMA_REG_NSMR_BIND_MEMWIN_SHIFT);
1549 cmd->flags_hpage_pbe_sz |= (last << OCRDMA_REG_NSMR_LAST_SHIFT);
1550
1551 cmd->flags_hpage_pbe_sz |= (hwmr->pbe_size / OCRDMA_MIN_HPAGE_SIZE);
1552 cmd->flags_hpage_pbe_sz |= (hwmr->pbl_size / OCRDMA_MIN_HPAGE_SIZE) <<
1553 OCRDMA_REG_NSMR_HPAGE_SIZE_SHIFT;
1554 cmd->totlen_low = hwmr->len;
1555 cmd->totlen_high = upper_32_bits(hwmr->len);
1556 cmd->fbo_low = (u32) (hwmr->fbo & 0xffffffff);
1557 cmd->fbo_high = (u32) upper_32_bits(hwmr->fbo);
1558 cmd->va_loaddr = (u32) hwmr->va;
1559 cmd->va_hiaddr = (u32) upper_32_bits(hwmr->va);
1560
1561 for (i = 0; i < pbl_cnt; i++) {
1562 cmd->pbl[i].lo = (u32) (hwmr->pbl_table[i].pa & 0xffffffff);
1563 cmd->pbl[i].hi = upper_32_bits(hwmr->pbl_table[i].pa);
1564 }
1565 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1566 if (status)
1567 goto mbx_err;
1568 rsp = (struct ocrdma_reg_nsmr_rsp *)cmd;
1569 hwmr->lkey = rsp->lrkey;
1570mbx_err:
1571 kfree(cmd);
1572 return status;
1573}
1574
1575static int ocrdma_mbx_reg_mr_cont(struct ocrdma_dev *dev,
1576 struct ocrdma_hw_mr *hwmr, u32 pbl_cnt,
1577 u32 pbl_offset, u32 last)
1578{
1579 int status = -ENOMEM;
1580 int i;
1581 struct ocrdma_reg_nsmr_cont *cmd;
1582
1583 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR_CONT, sizeof(*cmd));
1584 if (!cmd)
1585 return -ENOMEM;
1586 cmd->lrkey = hwmr->lkey;
1587 cmd->num_pbl_offset = (pbl_cnt << OCRDMA_REG_NSMR_CONT_NUM_PBL_SHIFT) |
1588 (pbl_offset & OCRDMA_REG_NSMR_CONT_PBL_SHIFT_MASK);
1589 cmd->last = last << OCRDMA_REG_NSMR_CONT_LAST_SHIFT;
1590
1591 for (i = 0; i < pbl_cnt; i++) {
1592 cmd->pbl[i].lo =
1593 (u32) (hwmr->pbl_table[i + pbl_offset].pa & 0xffffffff);
1594 cmd->pbl[i].hi =
1595 upper_32_bits(hwmr->pbl_table[i + pbl_offset].pa);
1596 }
1597 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1598 if (status)
1599 goto mbx_err;
1600mbx_err:
1601 kfree(cmd);
1602 return status;
1603}
1604
1605int ocrdma_reg_mr(struct ocrdma_dev *dev,
1606 struct ocrdma_hw_mr *hwmr, u32 pdid, int acc)
1607{
1608 int status;
1609 u32 last = 0;
1610 u32 cur_pbl_cnt, pbl_offset;
1611 u32 pending_pbl_cnt = hwmr->num_pbls;
1612
1613 pbl_offset = 0;
1614 cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
1615 if (cur_pbl_cnt == pending_pbl_cnt)
1616 last = 1;
1617
1618 status = ocrdma_mbx_reg_mr(dev, hwmr, pdid,
1619 cur_pbl_cnt, hwmr->pbe_size, last);
1620 if (status) {
1621 ocrdma_err("%s() status=%d\n", __func__, status);
1622 return status;
1623 }
1624 /* if there is no more pbls to register then exit. */
1625 if (last)
1626 return 0;
1627
1628 while (!last) {
1629 pbl_offset += cur_pbl_cnt;
1630 pending_pbl_cnt -= cur_pbl_cnt;
1631 cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
1632 /* if we reach the end of the pbls, then need to set the last
1633 * bit, indicating no more pbls to register for this memory key.
1634 */
1635 if (cur_pbl_cnt == pending_pbl_cnt)
1636 last = 1;
1637
1638 status = ocrdma_mbx_reg_mr_cont(dev, hwmr, cur_pbl_cnt,
1639 pbl_offset, last);
1640 if (status)
1641 break;
1642 }
1643 if (status)
1644 ocrdma_err("%s() err. status=%d\n", __func__, status);
1645
1646 return status;
1647}
1648
1649bool ocrdma_is_qp_in_sq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
1650{
1651 struct ocrdma_qp *tmp;
1652 bool found = false;
1653 list_for_each_entry(tmp, &cq->sq_head, sq_entry) {
1654 if (qp == tmp) {
1655 found = true;
1656 break;
1657 }
1658 }
1659 return found;
1660}
1661
1662bool ocrdma_is_qp_in_rq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
1663{
1664 struct ocrdma_qp *tmp;
1665 bool found = false;
1666 list_for_each_entry(tmp, &cq->rq_head, rq_entry) {
1667 if (qp == tmp) {
1668 found = true;
1669 break;
1670 }
1671 }
1672 return found;
1673}
1674
1675void ocrdma_flush_qp(struct ocrdma_qp *qp)
1676{
1677 bool found;
1678 unsigned long flags;
1679
1680 spin_lock_irqsave(&qp->dev->flush_q_lock, flags);
1681 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1682 if (!found)
1683 list_add_tail(&qp->sq_entry, &qp->sq_cq->sq_head);
1684 if (!qp->srq) {
1685 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1686 if (!found)
1687 list_add_tail(&qp->rq_entry, &qp->rq_cq->rq_head);
1688 }
1689 spin_unlock_irqrestore(&qp->dev->flush_q_lock, flags);
1690}
1691
1692int ocrdma_qp_state_machine(struct ocrdma_qp *qp, enum ib_qp_state new_ib_state,
1693 enum ib_qp_state *old_ib_state)
1694{
1695 unsigned long flags;
1696 int status = 0;
1697 enum ocrdma_qp_state new_state;
1698 new_state = get_ocrdma_qp_state(new_ib_state);
1699
1700 /* sync with wqe and rqe posting */
1701 spin_lock_irqsave(&qp->q_lock, flags);
1702
1703 if (old_ib_state)
1704 *old_ib_state = get_ibqp_state(qp->state);
1705 if (new_state == qp->state) {
1706 spin_unlock_irqrestore(&qp->q_lock, flags);
1707 return 1;
1708 }
1709
1710 switch (qp->state) {
1711 case OCRDMA_QPS_RST:
1712 switch (new_state) {
1713 case OCRDMA_QPS_RST:
1714 case OCRDMA_QPS_INIT:
1715 break;
1716 default:
1717 status = -EINVAL;
1718 break;
1719 };
1720 break;
1721 case OCRDMA_QPS_INIT:
1722 /* qps: INIT->XXX */
1723 switch (new_state) {
1724 case OCRDMA_QPS_INIT:
1725 case OCRDMA_QPS_RTR:
1726 break;
1727 case OCRDMA_QPS_ERR:
1728 ocrdma_flush_qp(qp);
1729 break;
1730 default:
1731 status = -EINVAL;
1732 break;
1733 };
1734 break;
1735 case OCRDMA_QPS_RTR:
1736 /* qps: RTS->XXX */
1737 switch (new_state) {
1738 case OCRDMA_QPS_RTS:
1739 break;
1740 case OCRDMA_QPS_ERR:
1741 ocrdma_flush_qp(qp);
1742 break;
1743 default:
1744 status = -EINVAL;
1745 break;
1746 };
1747 break;
1748 case OCRDMA_QPS_RTS:
1749 /* qps: RTS->XXX */
1750 switch (new_state) {
1751 case OCRDMA_QPS_SQD:
1752 case OCRDMA_QPS_SQE:
1753 break;
1754 case OCRDMA_QPS_ERR:
1755 ocrdma_flush_qp(qp);
1756 break;
1757 default:
1758 status = -EINVAL;
1759 break;
1760 };
1761 break;
1762 case OCRDMA_QPS_SQD:
1763 /* qps: SQD->XXX */
1764 switch (new_state) {
1765 case OCRDMA_QPS_RTS:
1766 case OCRDMA_QPS_SQE:
1767 case OCRDMA_QPS_ERR:
1768 break;
1769 default:
1770 status = -EINVAL;
1771 break;
1772 };
1773 break;
1774 case OCRDMA_QPS_SQE:
1775 switch (new_state) {
1776 case OCRDMA_QPS_RTS:
1777 case OCRDMA_QPS_ERR:
1778 break;
1779 default:
1780 status = -EINVAL;
1781 break;
1782 };
1783 break;
1784 case OCRDMA_QPS_ERR:
1785 /* qps: ERR->XXX */
1786 switch (new_state) {
1787 case OCRDMA_QPS_RST:
1788 break;
1789 default:
1790 status = -EINVAL;
1791 break;
1792 };
1793 break;
1794 default:
1795 status = -EINVAL;
1796 break;
1797 };
1798 if (!status)
1799 qp->state = new_state;
1800
1801 spin_unlock_irqrestore(&qp->q_lock, flags);
1802 return status;
1803}
1804
1805static u32 ocrdma_set_create_qp_mbx_access_flags(struct ocrdma_qp *qp)
1806{
1807 u32 flags = 0;
1808 if (qp->cap_flags & OCRDMA_QP_INB_RD)
1809 flags |= OCRDMA_CREATE_QP_REQ_INB_RDEN_MASK;
1810 if (qp->cap_flags & OCRDMA_QP_INB_WR)
1811 flags |= OCRDMA_CREATE_QP_REQ_INB_WREN_MASK;
1812 if (qp->cap_flags & OCRDMA_QP_MW_BIND)
1813 flags |= OCRDMA_CREATE_QP_REQ_BIND_MEMWIN_MASK;
1814 if (qp->cap_flags & OCRDMA_QP_LKEY0)
1815 flags |= OCRDMA_CREATE_QP_REQ_ZERO_LKEYEN_MASK;
1816 if (qp->cap_flags & OCRDMA_QP_FAST_REG)
1817 flags |= OCRDMA_CREATE_QP_REQ_FMR_EN_MASK;
1818 return flags;
1819}
1820
1821static int ocrdma_set_create_qp_sq_cmd(struct ocrdma_create_qp_req *cmd,
1822 struct ib_qp_init_attr *attrs,
1823 struct ocrdma_qp *qp)
1824{
1825 int status;
1826 u32 len, hw_pages, hw_page_size;
1827 dma_addr_t pa;
1828 struct ocrdma_dev *dev = qp->dev;
1829 struct pci_dev *pdev = dev->nic_info.pdev;
1830 u32 max_wqe_allocated;
1831 u32 max_sges = attrs->cap.max_send_sge;
1832
1833 max_wqe_allocated = attrs->cap.max_send_wr;
1834 /* need to allocate one extra to for GEN1 family */
1835 if (dev->nic_info.dev_family != OCRDMA_GEN2_FAMILY)
1836 max_wqe_allocated += 1;
1837
1838 status = ocrdma_build_q_conf(&max_wqe_allocated,
1839 dev->attr.wqe_size, &hw_pages, &hw_page_size);
1840 if (status) {
1841 ocrdma_err("%s() req. max_send_wr=0x%x\n", __func__,
1842 max_wqe_allocated);
1843 return -EINVAL;
1844 }
1845 qp->sq.max_cnt = max_wqe_allocated;
1846 len = (hw_pages * hw_page_size);
1847
1848 qp->sq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
1849 if (!qp->sq.va)
1850 return -EINVAL;
1851 memset(qp->sq.va, 0, len);
1852 qp->sq.len = len;
1853 qp->sq.pa = pa;
1854 qp->sq.entry_size = dev->attr.wqe_size;
1855 ocrdma_build_q_pages(&cmd->wq_addr[0], hw_pages, pa, hw_page_size);
1856
1857 cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
1858 << OCRDMA_CREATE_QP_REQ_SQ_PAGE_SIZE_SHIFT);
1859 cmd->num_wq_rq_pages |= (hw_pages <<
1860 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_SHIFT) &
1861 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_MASK;
1862 cmd->max_sge_send_write |= (max_sges <<
1863 OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_SHIFT) &
1864 OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_MASK;
1865 cmd->max_sge_send_write |= (max_sges <<
1866 OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_SHIFT) &
1867 OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_MASK;
1868 cmd->max_wqe_rqe |= (ilog2(qp->sq.max_cnt) <<
1869 OCRDMA_CREATE_QP_REQ_MAX_WQE_SHIFT) &
1870 OCRDMA_CREATE_QP_REQ_MAX_WQE_MASK;
1871 cmd->wqe_rqe_size |= (dev->attr.wqe_size <<
1872 OCRDMA_CREATE_QP_REQ_WQE_SIZE_SHIFT) &
1873 OCRDMA_CREATE_QP_REQ_WQE_SIZE_MASK;
1874 return 0;
1875}
1876
1877static int ocrdma_set_create_qp_rq_cmd(struct ocrdma_create_qp_req *cmd,
1878 struct ib_qp_init_attr *attrs,
1879 struct ocrdma_qp *qp)
1880{
1881 int status;
1882 u32 len, hw_pages, hw_page_size;
1883 dma_addr_t pa = 0;
1884 struct ocrdma_dev *dev = qp->dev;
1885 struct pci_dev *pdev = dev->nic_info.pdev;
1886 u32 max_rqe_allocated = attrs->cap.max_recv_wr + 1;
1887
1888 status = ocrdma_build_q_conf(&max_rqe_allocated, dev->attr.rqe_size,
1889 &hw_pages, &hw_page_size);
1890 if (status) {
1891 ocrdma_err("%s() req. max_recv_wr=0x%x\n", __func__,
1892 attrs->cap.max_recv_wr + 1);
1893 return status;
1894 }
1895 qp->rq.max_cnt = max_rqe_allocated;
1896 len = (hw_pages * hw_page_size);
1897
1898 qp->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
1899 if (!qp->rq.va)
1900 return status;
1901 memset(qp->rq.va, 0, len);
1902 qp->rq.pa = pa;
1903 qp->rq.len = len;
1904 qp->rq.entry_size = dev->attr.rqe_size;
1905
1906 ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
1907 cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
1908 OCRDMA_CREATE_QP_REQ_RQ_PAGE_SIZE_SHIFT);
1909 cmd->num_wq_rq_pages |=
1910 (hw_pages << OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_SHIFT) &
1911 OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_MASK;
1912 cmd->max_sge_recv_flags |= (attrs->cap.max_recv_sge <<
1913 OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_SHIFT) &
1914 OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_MASK;
1915 cmd->max_wqe_rqe |= (ilog2(qp->rq.max_cnt) <<
1916 OCRDMA_CREATE_QP_REQ_MAX_RQE_SHIFT) &
1917 OCRDMA_CREATE_QP_REQ_MAX_RQE_MASK;
1918 cmd->wqe_rqe_size |= (dev->attr.rqe_size <<
1919 OCRDMA_CREATE_QP_REQ_RQE_SIZE_SHIFT) &
1920 OCRDMA_CREATE_QP_REQ_RQE_SIZE_MASK;
1921 return 0;
1922}
1923
1924static void ocrdma_set_create_qp_dpp_cmd(struct ocrdma_create_qp_req *cmd,
1925 struct ocrdma_pd *pd,
1926 struct ocrdma_qp *qp,
1927 u8 enable_dpp_cq, u16 dpp_cq_id)
1928{
1929 pd->num_dpp_qp--;
1930 qp->dpp_enabled = true;
1931 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
1932 if (!enable_dpp_cq)
1933 return;
1934 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
1935 cmd->dpp_credits_cqid = dpp_cq_id;
1936 cmd->dpp_credits_cqid |= OCRDMA_CREATE_QP_REQ_DPP_CREDIT_LIMIT <<
1937 OCRDMA_CREATE_QP_REQ_DPP_CREDIT_SHIFT;
1938}
1939
1940static int ocrdma_set_create_qp_ird_cmd(struct ocrdma_create_qp_req *cmd,
1941 struct ocrdma_qp *qp)
1942{
1943 struct ocrdma_dev *dev = qp->dev;
1944 struct pci_dev *pdev = dev->nic_info.pdev;
1945 dma_addr_t pa = 0;
1946 int ird_page_size = dev->attr.ird_page_size;
1947 int ird_q_len = dev->attr.num_ird_pages * ird_page_size;
1948
1949 if (dev->attr.ird == 0)
1950 return 0;
1951
1952 qp->ird_q_va = dma_alloc_coherent(&pdev->dev, ird_q_len,
1953 &pa, GFP_KERNEL);
1954 if (!qp->ird_q_va)
1955 return -ENOMEM;
1956 memset(qp->ird_q_va, 0, ird_q_len);
1957 ocrdma_build_q_pages(&cmd->ird_addr[0], dev->attr.num_ird_pages,
1958 pa, ird_page_size);
1959 return 0;
1960}
1961
1962static void ocrdma_get_create_qp_rsp(struct ocrdma_create_qp_rsp *rsp,
1963 struct ocrdma_qp *qp,
1964 struct ib_qp_init_attr *attrs,
1965 u16 *dpp_offset, u16 *dpp_credit_lmt)
1966{
1967 u32 max_wqe_allocated, max_rqe_allocated;
1968 qp->id = rsp->qp_id & OCRDMA_CREATE_QP_RSP_QP_ID_MASK;
1969 qp->rq.dbid = rsp->sq_rq_id & OCRDMA_CREATE_QP_RSP_RQ_ID_MASK;
1970 qp->sq.dbid = rsp->sq_rq_id >> OCRDMA_CREATE_QP_RSP_SQ_ID_SHIFT;
1971 qp->max_ird = rsp->max_ord_ird & OCRDMA_CREATE_QP_RSP_MAX_IRD_MASK;
1972 qp->max_ord = (rsp->max_ord_ird >> OCRDMA_CREATE_QP_RSP_MAX_ORD_SHIFT);
1973 qp->dpp_enabled = false;
1974 if (rsp->dpp_response & OCRDMA_CREATE_QP_RSP_DPP_ENABLED_MASK) {
1975 qp->dpp_enabled = true;
1976 *dpp_credit_lmt = (rsp->dpp_response &
1977 OCRDMA_CREATE_QP_RSP_DPP_CREDITS_MASK) >>
1978 OCRDMA_CREATE_QP_RSP_DPP_CREDITS_SHIFT;
1979 *dpp_offset = (rsp->dpp_response &
1980 OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_MASK) >>
1981 OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_SHIFT;
1982 }
1983 max_wqe_allocated =
1984 rsp->max_wqe_rqe >> OCRDMA_CREATE_QP_RSP_MAX_WQE_SHIFT;
1985 max_wqe_allocated = 1 << max_wqe_allocated;
1986 max_rqe_allocated = 1 << ((u16)rsp->max_wqe_rqe);
1987
Parav Panditfe2caef2012-03-21 04:09:06 +05301988 qp->sq.max_cnt = max_wqe_allocated;
1989 qp->sq.max_wqe_idx = max_wqe_allocated - 1;
1990
1991 if (!attrs->srq) {
1992 qp->rq.max_cnt = max_rqe_allocated;
1993 qp->rq.max_wqe_idx = max_rqe_allocated - 1;
Parav Panditfe2caef2012-03-21 04:09:06 +05301994 }
1995}
1996
1997int ocrdma_mbx_create_qp(struct ocrdma_qp *qp, struct ib_qp_init_attr *attrs,
1998 u8 enable_dpp_cq, u16 dpp_cq_id, u16 *dpp_offset,
1999 u16 *dpp_credit_lmt)
2000{
2001 int status = -ENOMEM;
2002 u32 flags = 0;
2003 struct ocrdma_dev *dev = qp->dev;
2004 struct ocrdma_pd *pd = qp->pd;
2005 struct pci_dev *pdev = dev->nic_info.pdev;
2006 struct ocrdma_cq *cq;
2007 struct ocrdma_create_qp_req *cmd;
2008 struct ocrdma_create_qp_rsp *rsp;
2009 int qptype;
2010
2011 switch (attrs->qp_type) {
2012 case IB_QPT_GSI:
2013 qptype = OCRDMA_QPT_GSI;
2014 break;
2015 case IB_QPT_RC:
2016 qptype = OCRDMA_QPT_RC;
2017 break;
2018 case IB_QPT_UD:
2019 qptype = OCRDMA_QPT_UD;
2020 break;
2021 default:
2022 return -EINVAL;
2023 };
2024
2025 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_QP, sizeof(*cmd));
2026 if (!cmd)
2027 return status;
2028 cmd->type_pgsz_pdn |= (qptype << OCRDMA_CREATE_QP_REQ_QPT_SHIFT) &
2029 OCRDMA_CREATE_QP_REQ_QPT_MASK;
2030 status = ocrdma_set_create_qp_sq_cmd(cmd, attrs, qp);
2031 if (status)
2032 goto sq_err;
2033
2034 if (attrs->srq) {
2035 struct ocrdma_srq *srq = get_ocrdma_srq(attrs->srq);
2036 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_USE_SRQ_MASK;
2037 cmd->rq_addr[0].lo = srq->id;
2038 qp->srq = srq;
2039 } else {
2040 status = ocrdma_set_create_qp_rq_cmd(cmd, attrs, qp);
2041 if (status)
2042 goto rq_err;
2043 }
2044
2045 status = ocrdma_set_create_qp_ird_cmd(cmd, qp);
2046 if (status)
2047 goto mbx_err;
2048
2049 cmd->type_pgsz_pdn |= (pd->id << OCRDMA_CREATE_QP_REQ_PD_ID_SHIFT) &
2050 OCRDMA_CREATE_QP_REQ_PD_ID_MASK;
2051
2052 flags = ocrdma_set_create_qp_mbx_access_flags(qp);
2053
2054 cmd->max_sge_recv_flags |= flags;
2055 cmd->max_ord_ird |= (dev->attr.max_ord_per_qp <<
2056 OCRDMA_CREATE_QP_REQ_MAX_ORD_SHIFT) &
2057 OCRDMA_CREATE_QP_REQ_MAX_ORD_MASK;
2058 cmd->max_ord_ird |= (dev->attr.max_ird_per_qp <<
2059 OCRDMA_CREATE_QP_REQ_MAX_IRD_SHIFT) &
2060 OCRDMA_CREATE_QP_REQ_MAX_IRD_MASK;
2061 cq = get_ocrdma_cq(attrs->send_cq);
2062 cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_WQ_CQID_SHIFT) &
2063 OCRDMA_CREATE_QP_REQ_WQ_CQID_MASK;
2064 qp->sq_cq = cq;
2065 cq = get_ocrdma_cq(attrs->recv_cq);
2066 cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_RQ_CQID_SHIFT) &
2067 OCRDMA_CREATE_QP_REQ_RQ_CQID_MASK;
2068 qp->rq_cq = cq;
2069
2070 if (pd->dpp_enabled && attrs->cap.max_inline_data && pd->num_dpp_qp &&
2071 (attrs->cap.max_inline_data <= dev->attr.max_inline_data))
2072 ocrdma_set_create_qp_dpp_cmd(cmd, pd, qp, enable_dpp_cq,
2073 dpp_cq_id);
2074
2075 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2076 if (status)
2077 goto mbx_err;
2078 rsp = (struct ocrdma_create_qp_rsp *)cmd;
2079 ocrdma_get_create_qp_rsp(rsp, qp, attrs, dpp_offset, dpp_credit_lmt);
2080 qp->state = OCRDMA_QPS_RST;
2081 kfree(cmd);
2082 return 0;
2083mbx_err:
2084 if (qp->rq.va)
2085 dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2086rq_err:
2087 ocrdma_err("%s(%d) rq_err\n", __func__, dev->id);
2088 dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2089sq_err:
2090 ocrdma_err("%s(%d) sq_err\n", __func__, dev->id);
2091 kfree(cmd);
2092 return status;
2093}
2094
2095int ocrdma_mbx_query_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2096 struct ocrdma_qp_params *param)
2097{
2098 int status = -ENOMEM;
2099 struct ocrdma_query_qp *cmd;
2100 struct ocrdma_query_qp_rsp *rsp;
2101
2102 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_QP, sizeof(*cmd));
2103 if (!cmd)
2104 return status;
2105 cmd->qp_id = qp->id;
2106 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2107 if (status)
2108 goto mbx_err;
2109 rsp = (struct ocrdma_query_qp_rsp *)cmd;
2110 memcpy(param, &rsp->params, sizeof(struct ocrdma_qp_params));
2111mbx_err:
2112 kfree(cmd);
2113 return status;
2114}
2115
2116int ocrdma_resolve_dgid(struct ocrdma_dev *dev, union ib_gid *dgid,
2117 u8 *mac_addr)
2118{
2119 struct in6_addr in6;
2120
2121 memcpy(&in6, dgid, sizeof in6);
2122 if (rdma_is_multicast_addr(&in6))
2123 rdma_get_mcast_mac(&in6, mac_addr);
2124 else if (rdma_link_local_addr(&in6))
2125 rdma_get_ll_mac(&in6, mac_addr);
2126 else {
2127 ocrdma_err("%s() fail to resolve mac_addr.\n", __func__);
2128 return -EINVAL;
2129 }
2130 return 0;
2131}
2132
2133static void ocrdma_set_av_params(struct ocrdma_qp *qp,
2134 struct ocrdma_modify_qp *cmd,
2135 struct ib_qp_attr *attrs)
2136{
2137 struct ib_ah_attr *ah_attr = &attrs->ah_attr;
2138 union ib_gid sgid;
2139 u32 vlan_id;
2140 u8 mac_addr[6];
2141 if ((ah_attr->ah_flags & IB_AH_GRH) == 0)
2142 return;
2143 cmd->params.tclass_sq_psn |=
2144 (ah_attr->grh.traffic_class << OCRDMA_QP_PARAMS_TCLASS_SHIFT);
2145 cmd->params.rnt_rc_sl_fl |=
2146 (ah_attr->grh.flow_label & OCRDMA_QP_PARAMS_FLOW_LABEL_MASK);
2147 cmd->params.hop_lmt_rq_psn |=
2148 (ah_attr->grh.hop_limit << OCRDMA_QP_PARAMS_HOP_LMT_SHIFT);
2149 cmd->flags |= OCRDMA_QP_PARA_FLOW_LBL_VALID;
2150 memcpy(&cmd->params.dgid[0], &ah_attr->grh.dgid.raw[0],
2151 sizeof(cmd->params.dgid));
2152 ocrdma_query_gid(&qp->dev->ibdev, 1,
2153 ah_attr->grh.sgid_index, &sgid);
2154 qp->sgid_idx = ah_attr->grh.sgid_index;
2155 memcpy(&cmd->params.sgid[0], &sgid.raw[0], sizeof(cmd->params.sgid));
2156 ocrdma_resolve_dgid(qp->dev, &ah_attr->grh.dgid, &mac_addr[0]);
2157 cmd->params.dmac_b0_to_b3 = mac_addr[0] | (mac_addr[1] << 8) |
2158 (mac_addr[2] << 16) | (mac_addr[3] << 24);
2159 /* convert them to LE format. */
2160 ocrdma_cpu_to_le32(&cmd->params.dgid[0], sizeof(cmd->params.dgid));
2161 ocrdma_cpu_to_le32(&cmd->params.sgid[0], sizeof(cmd->params.sgid));
2162 cmd->params.vlan_dmac_b4_to_b5 = mac_addr[4] | (mac_addr[5] << 8);
2163 vlan_id = rdma_get_vlan_id(&sgid);
2164 if (vlan_id && (vlan_id < 0x1000)) {
2165 cmd->params.vlan_dmac_b4_to_b5 |=
2166 vlan_id << OCRDMA_QP_PARAMS_VLAN_SHIFT;
2167 cmd->flags |= OCRDMA_QP_PARA_VLAN_EN_VALID;
2168 }
2169}
2170
2171static int ocrdma_set_qp_params(struct ocrdma_qp *qp,
2172 struct ocrdma_modify_qp *cmd,
2173 struct ib_qp_attr *attrs, int attr_mask,
2174 enum ib_qp_state old_qps)
2175{
2176 int status = 0;
2177 struct net_device *netdev = qp->dev->nic_info.netdev;
2178 int eth_mtu = iboe_get_mtu(netdev->mtu);
2179
2180 if (attr_mask & IB_QP_PKEY_INDEX) {
2181 cmd->params.path_mtu_pkey_indx |= (attrs->pkey_index &
2182 OCRDMA_QP_PARAMS_PKEY_INDEX_MASK);
2183 cmd->flags |= OCRDMA_QP_PARA_PKEY_VALID;
2184 }
2185 if (attr_mask & IB_QP_QKEY) {
2186 qp->qkey = attrs->qkey;
2187 cmd->params.qkey = attrs->qkey;
2188 cmd->flags |= OCRDMA_QP_PARA_QKEY_VALID;
2189 }
2190 if (attr_mask & IB_QP_AV)
2191 ocrdma_set_av_params(qp, cmd, attrs);
2192 else if (qp->qp_type == IB_QPT_GSI || qp->qp_type == IB_QPT_UD) {
2193 /* set the default mac address for UD, GSI QPs */
2194 cmd->params.dmac_b0_to_b3 = qp->dev->nic_info.mac_addr[0] |
2195 (qp->dev->nic_info.mac_addr[1] << 8) |
2196 (qp->dev->nic_info.mac_addr[2] << 16) |
2197 (qp->dev->nic_info.mac_addr[3] << 24);
2198 cmd->params.vlan_dmac_b4_to_b5 = qp->dev->nic_info.mac_addr[4] |
2199 (qp->dev->nic_info.mac_addr[5] << 8);
2200 }
2201 if ((attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) &&
2202 attrs->en_sqd_async_notify) {
2203 cmd->params.max_sge_recv_flags |=
2204 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC;
2205 cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2206 }
2207 if (attr_mask & IB_QP_DEST_QPN) {
2208 cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->dest_qp_num &
2209 OCRDMA_QP_PARAMS_DEST_QPN_MASK);
2210 cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2211 }
2212 if (attr_mask & IB_QP_PATH_MTU) {
2213 if (ib_mtu_enum_to_int(eth_mtu) <
2214 ib_mtu_enum_to_int(attrs->path_mtu)) {
2215 status = -EINVAL;
2216 goto pmtu_err;
2217 }
2218 cmd->params.path_mtu_pkey_indx |=
2219 (ib_mtu_enum_to_int(attrs->path_mtu) <<
2220 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT) &
2221 OCRDMA_QP_PARAMS_PATH_MTU_MASK;
2222 cmd->flags |= OCRDMA_QP_PARA_PMTU_VALID;
2223 }
2224 if (attr_mask & IB_QP_TIMEOUT) {
2225 cmd->params.ack_to_rnr_rtc_dest_qpn |= attrs->timeout <<
2226 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
2227 cmd->flags |= OCRDMA_QP_PARA_ACK_TO_VALID;
2228 }
2229 if (attr_mask & IB_QP_RETRY_CNT) {
2230 cmd->params.rnt_rc_sl_fl |= (attrs->retry_cnt <<
2231 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT) &
2232 OCRDMA_QP_PARAMS_RETRY_CNT_MASK;
2233 cmd->flags |= OCRDMA_QP_PARA_RETRY_CNT_VALID;
2234 }
2235 if (attr_mask & IB_QP_MIN_RNR_TIMER) {
2236 cmd->params.rnt_rc_sl_fl |= (attrs->min_rnr_timer <<
2237 OCRDMA_QP_PARAMS_RNR_NAK_TIMER_SHIFT) &
2238 OCRDMA_QP_PARAMS_RNR_NAK_TIMER_MASK;
2239 cmd->flags |= OCRDMA_QP_PARA_RNT_VALID;
2240 }
2241 if (attr_mask & IB_QP_RNR_RETRY) {
2242 cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->rnr_retry <<
2243 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT)
2244 & OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK;
2245 cmd->flags |= OCRDMA_QP_PARA_RRC_VALID;
2246 }
2247 if (attr_mask & IB_QP_SQ_PSN) {
2248 cmd->params.tclass_sq_psn |= (attrs->sq_psn & 0x00ffffff);
2249 cmd->flags |= OCRDMA_QP_PARA_SQPSN_VALID;
2250 }
2251 if (attr_mask & IB_QP_RQ_PSN) {
2252 cmd->params.hop_lmt_rq_psn |= (attrs->rq_psn & 0x00ffffff);
2253 cmd->flags |= OCRDMA_QP_PARA_RQPSN_VALID;
2254 }
2255 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2256 if (attrs->max_rd_atomic > qp->dev->attr.max_ord_per_qp) {
2257 status = -EINVAL;
2258 goto pmtu_err;
2259 }
2260 qp->max_ord = attrs->max_rd_atomic;
2261 cmd->flags |= OCRDMA_QP_PARA_MAX_ORD_VALID;
2262 }
2263 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2264 if (attrs->max_dest_rd_atomic > qp->dev->attr.max_ird_per_qp) {
2265 status = -EINVAL;
2266 goto pmtu_err;
2267 }
2268 qp->max_ird = attrs->max_dest_rd_atomic;
2269 cmd->flags |= OCRDMA_QP_PARA_MAX_IRD_VALID;
2270 }
2271 cmd->params.max_ord_ird = (qp->max_ord <<
2272 OCRDMA_QP_PARAMS_MAX_ORD_SHIFT) |
2273 (qp->max_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK);
2274pmtu_err:
2275 return status;
2276}
2277
2278int ocrdma_mbx_modify_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2279 struct ib_qp_attr *attrs, int attr_mask,
2280 enum ib_qp_state old_qps)
2281{
2282 int status = -ENOMEM;
2283 struct ocrdma_modify_qp *cmd;
Parav Panditfe2caef2012-03-21 04:09:06 +05302284
2285 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_QP, sizeof(*cmd));
2286 if (!cmd)
2287 return status;
2288
2289 cmd->params.id = qp->id;
2290 cmd->flags = 0;
2291 if (attr_mask & IB_QP_STATE) {
2292 cmd->params.max_sge_recv_flags |=
2293 (get_ocrdma_qp_state(attrs->qp_state) <<
2294 OCRDMA_QP_PARAMS_STATE_SHIFT) &
2295 OCRDMA_QP_PARAMS_STATE_MASK;
2296 cmd->flags |= OCRDMA_QP_PARA_QPS_VALID;
2297 } else
2298 cmd->params.max_sge_recv_flags |=
2299 (qp->state << OCRDMA_QP_PARAMS_STATE_SHIFT) &
2300 OCRDMA_QP_PARAMS_STATE_MASK;
2301 status = ocrdma_set_qp_params(qp, cmd, attrs, attr_mask, old_qps);
2302 if (status)
2303 goto mbx_err;
2304 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2305 if (status)
2306 goto mbx_err;
Roland Dreierc592c422012-04-17 01:18:28 -07002307
Parav Panditfe2caef2012-03-21 04:09:06 +05302308mbx_err:
2309 kfree(cmd);
2310 return status;
2311}
2312
2313int ocrdma_mbx_destroy_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
2314{
2315 int status = -ENOMEM;
2316 struct ocrdma_destroy_qp *cmd;
Parav Panditfe2caef2012-03-21 04:09:06 +05302317 struct pci_dev *pdev = dev->nic_info.pdev;
2318
2319 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_QP, sizeof(*cmd));
2320 if (!cmd)
2321 return status;
2322 cmd->qp_id = qp->id;
2323 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2324 if (status)
2325 goto mbx_err;
Roland Dreierc592c422012-04-17 01:18:28 -07002326
Parav Panditfe2caef2012-03-21 04:09:06 +05302327mbx_err:
2328 kfree(cmd);
2329 if (qp->sq.va)
2330 dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2331 if (!qp->srq && qp->rq.va)
2332 dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2333 if (qp->dpp_enabled)
2334 qp->pd->num_dpp_qp++;
2335 return status;
2336}
2337
2338int ocrdma_mbx_create_srq(struct ocrdma_srq *srq,
2339 struct ib_srq_init_attr *srq_attr,
2340 struct ocrdma_pd *pd)
2341{
2342 int status = -ENOMEM;
2343 int hw_pages, hw_page_size;
2344 int len;
2345 struct ocrdma_create_srq_rsp *rsp;
2346 struct ocrdma_create_srq *cmd;
2347 dma_addr_t pa;
2348 struct ocrdma_dev *dev = srq->dev;
2349 struct pci_dev *pdev = dev->nic_info.pdev;
2350 u32 max_rqe_allocated;
2351
2352 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2353 if (!cmd)
2354 return status;
2355
2356 cmd->pgsz_pdid = pd->id & OCRDMA_CREATE_SRQ_PD_ID_MASK;
2357 max_rqe_allocated = srq_attr->attr.max_wr + 1;
2358 status = ocrdma_build_q_conf(&max_rqe_allocated,
2359 dev->attr.rqe_size,
2360 &hw_pages, &hw_page_size);
2361 if (status) {
2362 ocrdma_err("%s() req. max_wr=0x%x\n", __func__,
2363 srq_attr->attr.max_wr);
2364 status = -EINVAL;
2365 goto ret;
2366 }
2367 len = hw_pages * hw_page_size;
2368 srq->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2369 if (!srq->rq.va) {
2370 status = -ENOMEM;
2371 goto ret;
2372 }
2373 ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
2374
2375 srq->rq.entry_size = dev->attr.rqe_size;
2376 srq->rq.pa = pa;
2377 srq->rq.len = len;
2378 srq->rq.max_cnt = max_rqe_allocated;
2379
2380 cmd->max_sge_rqe = ilog2(max_rqe_allocated);
2381 cmd->max_sge_rqe |= srq_attr->attr.max_sge <<
2382 OCRDMA_CREATE_SRQ_MAX_SGE_RECV_SHIFT;
2383
2384 cmd->pgsz_pdid |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
2385 << OCRDMA_CREATE_SRQ_PG_SZ_SHIFT);
2386 cmd->pages_rqe_sz |= (dev->attr.rqe_size
2387 << OCRDMA_CREATE_SRQ_RQE_SIZE_SHIFT)
2388 & OCRDMA_CREATE_SRQ_RQE_SIZE_MASK;
2389 cmd->pages_rqe_sz |= hw_pages << OCRDMA_CREATE_SRQ_NUM_RQ_PAGES_SHIFT;
2390
2391 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2392 if (status)
2393 goto mbx_err;
2394 rsp = (struct ocrdma_create_srq_rsp *)cmd;
2395 srq->id = rsp->id;
2396 srq->rq.dbid = rsp->id;
2397 max_rqe_allocated = ((rsp->max_sge_rqe_allocated &
2398 OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_MASK) >>
2399 OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_SHIFT);
2400 max_rqe_allocated = (1 << max_rqe_allocated);
2401 srq->rq.max_cnt = max_rqe_allocated;
2402 srq->rq.max_wqe_idx = max_rqe_allocated - 1;
2403 srq->rq.max_sges = (rsp->max_sge_rqe_allocated &
2404 OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_MASK) >>
2405 OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_SHIFT;
2406 goto ret;
2407mbx_err:
2408 dma_free_coherent(&pdev->dev, srq->rq.len, srq->rq.va, pa);
2409ret:
2410 kfree(cmd);
2411 return status;
2412}
2413
2414int ocrdma_mbx_modify_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2415{
2416 int status = -ENOMEM;
2417 struct ocrdma_modify_srq *cmd;
2418 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2419 if (!cmd)
2420 return status;
2421 cmd->id = srq->id;
2422 cmd->limit_max_rqe |= srq_attr->srq_limit <<
2423 OCRDMA_MODIFY_SRQ_LIMIT_SHIFT;
2424 status = ocrdma_mbx_cmd(srq->dev, (struct ocrdma_mqe *)cmd);
2425 kfree(cmd);
2426 return status;
2427}
2428
2429int ocrdma_mbx_query_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2430{
2431 int status = -ENOMEM;
2432 struct ocrdma_query_srq *cmd;
2433 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2434 if (!cmd)
2435 return status;
2436 cmd->id = srq->rq.dbid;
2437 status = ocrdma_mbx_cmd(srq->dev, (struct ocrdma_mqe *)cmd);
2438 if (status == 0) {
2439 struct ocrdma_query_srq_rsp *rsp =
2440 (struct ocrdma_query_srq_rsp *)cmd;
2441 srq_attr->max_sge =
2442 rsp->srq_lmt_max_sge &
2443 OCRDMA_QUERY_SRQ_RSP_MAX_SGE_RECV_MASK;
2444 srq_attr->max_wr =
2445 rsp->max_rqe_pdid >> OCRDMA_QUERY_SRQ_RSP_MAX_RQE_SHIFT;
2446 srq_attr->srq_limit = rsp->srq_lmt_max_sge >>
2447 OCRDMA_QUERY_SRQ_RSP_SRQ_LIMIT_SHIFT;
2448 }
2449 kfree(cmd);
2450 return status;
2451}
2452
2453int ocrdma_mbx_destroy_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq)
2454{
2455 int status = -ENOMEM;
2456 struct ocrdma_destroy_srq *cmd;
2457 struct pci_dev *pdev = dev->nic_info.pdev;
2458 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_SRQ, sizeof(*cmd));
2459 if (!cmd)
2460 return status;
2461 cmd->id = srq->id;
2462 status = ocrdma_mbx_cmd(srq->dev, (struct ocrdma_mqe *)cmd);
2463 if (srq->rq.va)
2464 dma_free_coherent(&pdev->dev, srq->rq.len,
2465 srq->rq.va, srq->rq.pa);
2466 kfree(cmd);
2467 return status;
2468}
2469
2470int ocrdma_alloc_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
2471{
2472 int i;
2473 int status = -EINVAL;
2474 struct ocrdma_av *av;
2475 unsigned long flags;
2476
2477 av = dev->av_tbl.va;
2478 spin_lock_irqsave(&dev->av_tbl.lock, flags);
2479 for (i = 0; i < dev->av_tbl.num_ah; i++) {
2480 if (av->valid == 0) {
2481 av->valid = OCRDMA_AV_VALID;
2482 ah->av = av;
2483 ah->id = i;
2484 status = 0;
2485 break;
2486 }
2487 av++;
2488 }
2489 if (i == dev->av_tbl.num_ah)
2490 status = -EAGAIN;
2491 spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
2492 return status;
2493}
2494
2495int ocrdma_free_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
2496{
2497 unsigned long flags;
2498 spin_lock_irqsave(&dev->av_tbl.lock, flags);
2499 ah->av->valid = 0;
2500 spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
2501 return 0;
2502}
2503
2504static int ocrdma_create_mq_eq(struct ocrdma_dev *dev)
2505{
2506 int status;
2507 int irq;
2508 unsigned long flags = 0;
2509 int num_eq = 0;
2510
2511 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
2512 flags = IRQF_SHARED;
2513 else {
2514 num_eq = dev->nic_info.msix.num_vectors -
2515 dev->nic_info.msix.start_vector;
2516 /* minimum two vectors/eq are required for rdma to work.
2517 * one for control path and one for data path.
2518 */
2519 if (num_eq < 2)
2520 return -EBUSY;
2521 }
2522
2523 status = ocrdma_create_eq(dev, &dev->meq, OCRDMA_EQ_LEN);
2524 if (status)
2525 return status;
2526 sprintf(dev->meq.irq_name, "ocrdma_mq%d", dev->id);
2527 irq = ocrdma_get_irq(dev, &dev->meq);
2528 status = request_irq(irq, ocrdma_irq_handler, flags, dev->meq.irq_name,
2529 &dev->meq);
2530 if (status)
2531 _ocrdma_destroy_eq(dev, &dev->meq);
2532 return status;
2533}
2534
2535static int ocrdma_create_qp_eqs(struct ocrdma_dev *dev)
2536{
Roland Dreierda496432012-04-16 11:32:17 -07002537 int num_eq, i, status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302538 int irq;
2539 unsigned long flags = 0;
2540
2541 num_eq = dev->nic_info.msix.num_vectors -
2542 dev->nic_info.msix.start_vector;
2543 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX) {
2544 num_eq = 1;
2545 flags = IRQF_SHARED;
2546 } else
2547 num_eq = min_t(u32, num_eq, num_online_cpus());
2548 dev->qp_eq_tbl = kzalloc(sizeof(struct ocrdma_eq) * num_eq, GFP_KERNEL);
2549 if (!dev->qp_eq_tbl)
2550 return -ENOMEM;
2551
2552 for (i = 0; i < num_eq; i++) {
2553 status = ocrdma_create_eq(dev, &dev->qp_eq_tbl[i],
2554 OCRDMA_EQ_LEN);
2555 if (status) {
2556 status = -EINVAL;
2557 break;
2558 }
2559 sprintf(dev->qp_eq_tbl[i].irq_name, "ocrdma_qp%d-%d",
2560 dev->id, i);
2561 irq = ocrdma_get_irq(dev, &dev->qp_eq_tbl[i]);
2562 status = request_irq(irq, ocrdma_irq_handler, flags,
2563 dev->qp_eq_tbl[i].irq_name,
2564 &dev->qp_eq_tbl[i]);
2565 if (status) {
2566 _ocrdma_destroy_eq(dev, &dev->qp_eq_tbl[i]);
2567 status = -EINVAL;
2568 break;
2569 }
2570 dev->eq_cnt += 1;
2571 }
2572 /* one eq is sufficient for data path to work */
2573 if (dev->eq_cnt >= 1)
2574 return 0;
2575 if (status)
2576 ocrdma_destroy_qp_eqs(dev);
2577 return status;
2578}
2579
2580int ocrdma_init_hw(struct ocrdma_dev *dev)
2581{
2582 int status;
2583 /* set up control path eq */
2584 status = ocrdma_create_mq_eq(dev);
2585 if (status)
2586 return status;
2587 /* set up data path eq */
2588 status = ocrdma_create_qp_eqs(dev);
2589 if (status)
2590 goto qpeq_err;
2591 status = ocrdma_create_mq(dev);
2592 if (status)
2593 goto mq_err;
2594 status = ocrdma_mbx_query_fw_config(dev);
2595 if (status)
2596 goto conf_err;
2597 status = ocrdma_mbx_query_dev(dev);
2598 if (status)
2599 goto conf_err;
2600 status = ocrdma_mbx_query_fw_ver(dev);
2601 if (status)
2602 goto conf_err;
2603 status = ocrdma_mbx_create_ah_tbl(dev);
2604 if (status)
2605 goto conf_err;
2606 return 0;
2607
2608conf_err:
2609 ocrdma_destroy_mq(dev);
2610mq_err:
2611 ocrdma_destroy_qp_eqs(dev);
2612qpeq_err:
2613 ocrdma_destroy_eq(dev, &dev->meq);
2614 ocrdma_err("%s() status=%d\n", __func__, status);
2615 return status;
2616}
2617
2618void ocrdma_cleanup_hw(struct ocrdma_dev *dev)
2619{
2620 ocrdma_mbx_delete_ah_tbl(dev);
2621
2622 /* cleanup the data path eqs */
2623 ocrdma_destroy_qp_eqs(dev);
2624
2625 /* cleanup the control path */
2626 ocrdma_destroy_mq(dev);
2627 ocrdma_destroy_eq(dev, &dev->meq);
2628}