blob: 3f027552a1a13df1309b811dd8a8b78d0a2c5837 [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{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +053097 return eq->q.va + (eq->q.tail * sizeof(struct ocrdma_eqe));
Parav Panditfe2caef2012-03-21 04:09:06 +053098}
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 *)
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530108 (dev->mq.cq.va + (dev->mq.cq.tail * sizeof(struct ocrdma_mcqe)));
Parav Panditfe2caef2012-03-21 04:09:06 +0530109
110 if (!(le32_to_cpu(cqe->valid_ae_cmpl_cons) & OCRDMA_MCQE_VALID_MASK))
111 return NULL;
112 return cqe;
113}
114
115static inline void ocrdma_mcq_inc_tail(struct ocrdma_dev *dev)
116{
117 dev->mq.cq.tail = (dev->mq.cq.tail + 1) & (OCRDMA_MQ_CQ_LEN - 1);
118}
119
120static inline struct ocrdma_mqe *ocrdma_get_mqe(struct ocrdma_dev *dev)
121{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530122 return dev->mq.sq.va + (dev->mq.sq.head * sizeof(struct ocrdma_mqe));
Parav Panditfe2caef2012-03-21 04:09:06 +0530123}
124
125static inline void ocrdma_mq_inc_head(struct ocrdma_dev *dev)
126{
127 dev->mq.sq.head = (dev->mq.sq.head + 1) & (OCRDMA_MQ_LEN - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +0530128}
129
130static inline void *ocrdma_get_mqe_rsp(struct ocrdma_dev *dev)
131{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530132 return dev->mq.sq.va + (dev->mqe_ctx.tag * sizeof(struct ocrdma_mqe));
Parav Panditfe2caef2012-03-21 04:09:06 +0530133}
134
135enum ib_qp_state get_ibqp_state(enum ocrdma_qp_state qps)
136{
137 switch (qps) {
138 case OCRDMA_QPS_RST:
139 return IB_QPS_RESET;
140 case OCRDMA_QPS_INIT:
141 return IB_QPS_INIT;
142 case OCRDMA_QPS_RTR:
143 return IB_QPS_RTR;
144 case OCRDMA_QPS_RTS:
145 return IB_QPS_RTS;
146 case OCRDMA_QPS_SQD:
147 case OCRDMA_QPS_SQ_DRAINING:
148 return IB_QPS_SQD;
149 case OCRDMA_QPS_SQE:
150 return IB_QPS_SQE;
151 case OCRDMA_QPS_ERR:
152 return IB_QPS_ERR;
153 };
154 return IB_QPS_ERR;
155}
156
Roland Dreierabe3afa2012-04-16 11:36:29 -0700157static enum ocrdma_qp_state get_ocrdma_qp_state(enum ib_qp_state qps)
Parav Panditfe2caef2012-03-21 04:09:06 +0530158{
159 switch (qps) {
160 case IB_QPS_RESET:
161 return OCRDMA_QPS_RST;
162 case IB_QPS_INIT:
163 return OCRDMA_QPS_INIT;
164 case IB_QPS_RTR:
165 return OCRDMA_QPS_RTR;
166 case IB_QPS_RTS:
167 return OCRDMA_QPS_RTS;
168 case IB_QPS_SQD:
169 return OCRDMA_QPS_SQD;
170 case IB_QPS_SQE:
171 return OCRDMA_QPS_SQE;
172 case IB_QPS_ERR:
173 return OCRDMA_QPS_ERR;
174 };
175 return OCRDMA_QPS_ERR;
176}
177
178static int ocrdma_get_mbx_errno(u32 status)
179{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530180 int err_num;
Parav Panditfe2caef2012-03-21 04:09:06 +0530181 u8 mbox_status = (status & OCRDMA_MBX_RSP_STATUS_MASK) >>
182 OCRDMA_MBX_RSP_STATUS_SHIFT;
183 u8 add_status = (status & OCRDMA_MBX_RSP_ASTATUS_MASK) >>
184 OCRDMA_MBX_RSP_ASTATUS_SHIFT;
185
186 switch (mbox_status) {
187 case OCRDMA_MBX_STATUS_OOR:
188 case OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS:
189 err_num = -EAGAIN;
190 break;
191
192 case OCRDMA_MBX_STATUS_INVALID_PD:
193 case OCRDMA_MBX_STATUS_INVALID_CQ:
194 case OCRDMA_MBX_STATUS_INVALID_SRQ_ID:
195 case OCRDMA_MBX_STATUS_INVALID_QP:
196 case OCRDMA_MBX_STATUS_INVALID_CHANGE:
197 case OCRDMA_MBX_STATUS_MTU_EXCEEDS:
198 case OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER:
199 case OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID:
200 case OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS:
201 case OCRDMA_MBX_STATUS_ILLEGAL_FIELD:
202 case OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY:
203 case OCRDMA_MBX_STATUS_INVALID_LKEY:
204 case OCRDMA_MBX_STATUS_INVALID_VA:
205 case OCRDMA_MBX_STATUS_INVALID_LENGTH:
206 case OCRDMA_MBX_STATUS_INVALID_FBO:
207 case OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS:
208 case OCRDMA_MBX_STATUS_INVALID_PBE_SIZE:
209 case OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP:
210 case OCRDMA_MBX_STATUS_SRQ_ERROR:
211 case OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS:
212 err_num = -EINVAL;
213 break;
214
215 case OCRDMA_MBX_STATUS_PD_INUSE:
216 case OCRDMA_MBX_STATUS_QP_BOUND:
217 case OCRDMA_MBX_STATUS_MW_STILL_BOUND:
218 case OCRDMA_MBX_STATUS_MW_BOUND:
219 err_num = -EBUSY;
220 break;
221
222 case OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS:
223 case OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS:
224 case OCRDMA_MBX_STATUS_RQE_EXCEEDS:
225 case OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS:
226 case OCRDMA_MBX_STATUS_ORD_EXCEEDS:
227 case OCRDMA_MBX_STATUS_IRD_EXCEEDS:
228 case OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS:
229 case OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS:
230 case OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS:
231 err_num = -ENOBUFS;
232 break;
233
234 case OCRDMA_MBX_STATUS_FAILED:
235 switch (add_status) {
236 case OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES:
237 err_num = -EAGAIN;
238 break;
239 }
240 default:
241 err_num = -EFAULT;
242 }
243 return err_num;
244}
245
246static int ocrdma_get_mbx_cqe_errno(u16 cqe_status)
247{
248 int err_num = -EINVAL;
249
250 switch (cqe_status) {
251 case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES:
252 err_num = -EPERM;
253 break;
254 case OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER:
255 err_num = -EINVAL;
256 break;
257 case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES:
258 case OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING:
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +0530259 err_num = -EINVAL;
Parav Panditfe2caef2012-03-21 04:09:06 +0530260 break;
261 case OCRDMA_MBX_CQE_STATUS_DMA_FAILED:
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530262 default:
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +0530263 err_num = -EINVAL;
Parav Panditfe2caef2012-03-21 04:09:06 +0530264 break;
265 }
266 return err_num;
267}
268
269void ocrdma_ring_cq_db(struct ocrdma_dev *dev, u16 cq_id, bool armed,
270 bool solicited, u16 cqe_popped)
271{
272 u32 val = cq_id & OCRDMA_DB_CQ_RING_ID_MASK;
273
274 val |= ((cq_id & OCRDMA_DB_CQ_RING_ID_EXT_MASK) <<
275 OCRDMA_DB_CQ_RING_ID_EXT_MASK_SHIFT);
276
277 if (armed)
278 val |= (1 << OCRDMA_DB_CQ_REARM_SHIFT);
279 if (solicited)
280 val |= (1 << OCRDMA_DB_CQ_SOLICIT_SHIFT);
281 val |= (cqe_popped << OCRDMA_DB_CQ_NUM_POPPED_SHIFT);
282 iowrite32(val, dev->nic_info.db + OCRDMA_DB_CQ_OFFSET);
283}
284
285static void ocrdma_ring_mq_db(struct ocrdma_dev *dev)
286{
287 u32 val = 0;
288
289 val |= dev->mq.sq.id & OCRDMA_MQ_ID_MASK;
290 val |= 1 << OCRDMA_MQ_NUM_MQE_SHIFT;
291 iowrite32(val, dev->nic_info.db + OCRDMA_DB_MQ_OFFSET);
292}
293
294static void ocrdma_ring_eq_db(struct ocrdma_dev *dev, u16 eq_id,
295 bool arm, bool clear_int, u16 num_eqe)
296{
297 u32 val = 0;
298
299 val |= eq_id & OCRDMA_EQ_ID_MASK;
300 val |= ((eq_id & OCRDMA_EQ_ID_EXT_MASK) << OCRDMA_EQ_ID_EXT_MASK_SHIFT);
301 if (arm)
302 val |= (1 << OCRDMA_REARM_SHIFT);
303 if (clear_int)
304 val |= (1 << OCRDMA_EQ_CLR_SHIFT);
305 val |= (1 << OCRDMA_EQ_TYPE_SHIFT);
306 val |= (num_eqe << OCRDMA_NUM_EQE_SHIFT);
307 iowrite32(val, dev->nic_info.db + OCRDMA_DB_EQ_OFFSET);
308}
309
310static void ocrdma_init_mch(struct ocrdma_mbx_hdr *cmd_hdr,
311 u8 opcode, u8 subsys, u32 cmd_len)
312{
313 cmd_hdr->subsys_op = (opcode | (subsys << OCRDMA_MCH_SUBSYS_SHIFT));
314 cmd_hdr->timeout = 20; /* seconds */
315 cmd_hdr->cmd_len = cmd_len - sizeof(struct ocrdma_mbx_hdr);
316}
317
318static void *ocrdma_init_emb_mqe(u8 opcode, u32 cmd_len)
319{
320 struct ocrdma_mqe *mqe;
321
322 mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
323 if (!mqe)
324 return NULL;
325 mqe->hdr.spcl_sge_cnt_emb |=
326 (OCRDMA_MQE_EMBEDDED << OCRDMA_MQE_HDR_EMB_SHIFT) &
327 OCRDMA_MQE_HDR_EMB_MASK;
328 mqe->hdr.pyld_len = cmd_len - sizeof(struct ocrdma_mqe_hdr);
329
330 ocrdma_init_mch(&mqe->u.emb_req.mch, opcode, OCRDMA_SUBSYS_ROCE,
331 mqe->hdr.pyld_len);
332 return mqe;
333}
334
335static void ocrdma_free_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q)
336{
337 dma_free_coherent(&dev->nic_info.pdev->dev, q->size, q->va, q->dma);
338}
339
340static int ocrdma_alloc_q(struct ocrdma_dev *dev,
341 struct ocrdma_queue_info *q, u16 len, u16 entry_size)
342{
343 memset(q, 0, sizeof(*q));
344 q->len = len;
345 q->entry_size = entry_size;
346 q->size = len * entry_size;
347 q->va = dma_alloc_coherent(&dev->nic_info.pdev->dev, q->size,
348 &q->dma, GFP_KERNEL);
349 if (!q->va)
350 return -ENOMEM;
351 memset(q->va, 0, q->size);
352 return 0;
353}
354
355static void ocrdma_build_q_pages(struct ocrdma_pa *q_pa, int cnt,
356 dma_addr_t host_pa, int hw_page_size)
357{
358 int i;
359
360 for (i = 0; i < cnt; i++) {
361 q_pa[i].lo = (u32) (host_pa & 0xffffffff);
362 q_pa[i].hi = (u32) upper_32_bits(host_pa);
363 host_pa += hw_page_size;
364 }
365}
366
Roland Dreierabe3afa2012-04-16 11:36:29 -0700367static int ocrdma_mbx_delete_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q,
368 int queue_type)
Parav Panditfe2caef2012-03-21 04:09:06 +0530369{
370 u8 opcode = 0;
371 int status;
372 struct ocrdma_delete_q_req *cmd = dev->mbx_cmd;
373
374 switch (queue_type) {
375 case QTYPE_MCCQ:
376 opcode = OCRDMA_CMD_DELETE_MQ;
377 break;
378 case QTYPE_CQ:
379 opcode = OCRDMA_CMD_DELETE_CQ;
380 break;
381 case QTYPE_EQ:
382 opcode = OCRDMA_CMD_DELETE_EQ;
383 break;
384 default:
385 BUG();
386 }
387 memset(cmd, 0, sizeof(*cmd));
388 ocrdma_init_mch(&cmd->req, opcode, OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
389 cmd->id = q->id;
390
391 status = be_roce_mcc_cmd(dev->nic_info.netdev,
392 cmd, sizeof(*cmd), NULL, NULL);
393 if (!status)
394 q->created = false;
395 return status;
396}
397
398static int ocrdma_mbx_create_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
399{
400 int status;
401 struct ocrdma_create_eq_req *cmd = dev->mbx_cmd;
402 struct ocrdma_create_eq_rsp *rsp = dev->mbx_cmd;
403
404 memset(cmd, 0, sizeof(*cmd));
405 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_EQ, OCRDMA_SUBSYS_COMMON,
406 sizeof(*cmd));
Parav Panditfe2caef2012-03-21 04:09:06 +0530407
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +0530408 cmd->req.rsvd_version = 2;
Parav Panditfe2caef2012-03-21 04:09:06 +0530409 cmd->num_pages = 4;
410 cmd->valid = OCRDMA_CREATE_EQ_VALID;
411 cmd->cnt = 4 << OCRDMA_CREATE_EQ_CNT_SHIFT;
412
413 ocrdma_build_q_pages(&cmd->pa[0], cmd->num_pages, eq->q.dma,
414 PAGE_SIZE_4K);
415 status = be_roce_mcc_cmd(dev->nic_info.netdev, cmd, sizeof(*cmd), NULL,
416 NULL);
417 if (!status) {
418 eq->q.id = rsp->vector_eqid & 0xffff;
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +0530419 eq->vector = (rsp->vector_eqid >> 16) & 0xffff;
Parav Panditfe2caef2012-03-21 04:09:06 +0530420 eq->q.created = true;
421 }
422 return status;
423}
424
425static int ocrdma_create_eq(struct ocrdma_dev *dev,
426 struct ocrdma_eq *eq, u16 q_len)
427{
428 int status;
429
430 status = ocrdma_alloc_q(dev, &eq->q, OCRDMA_EQ_LEN,
431 sizeof(struct ocrdma_eqe));
432 if (status)
433 return status;
434
435 status = ocrdma_mbx_create_eq(dev, eq);
436 if (status)
437 goto mbx_err;
438 eq->dev = dev;
439 ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
440
441 return 0;
442mbx_err:
443 ocrdma_free_q(dev, &eq->q);
444 return status;
445}
446
447static int ocrdma_get_irq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
448{
449 int irq;
450
451 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
452 irq = dev->nic_info.pdev->irq;
453 else
454 irq = dev->nic_info.msix.vector_list[eq->vector];
455 return irq;
456}
457
458static void _ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
459{
460 if (eq->q.created) {
461 ocrdma_mbx_delete_q(dev, &eq->q, QTYPE_EQ);
Parav Panditfe2caef2012-03-21 04:09:06 +0530462 ocrdma_free_q(dev, &eq->q);
463 }
464}
465
466static void ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
467{
468 int irq;
469
470 /* disarm EQ so that interrupts are not generated
471 * during freeing and EQ delete is in progress.
472 */
473 ocrdma_ring_eq_db(dev, eq->q.id, false, false, 0);
474
475 irq = ocrdma_get_irq(dev, eq);
476 free_irq(irq, eq);
477 _ocrdma_destroy_eq(dev, eq);
478}
479
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +0530480static void ocrdma_destroy_eqs(struct ocrdma_dev *dev)
Parav Panditfe2caef2012-03-21 04:09:06 +0530481{
482 int i;
483
Parav Panditfe2caef2012-03-21 04:09:06 +0530484 for (i = 0; i < dev->eq_cnt; i++)
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +0530485 ocrdma_destroy_eq(dev, &dev->eq_tbl[i]);
Parav Panditfe2caef2012-03-21 04:09:06 +0530486}
487
Roland Dreierabe3afa2012-04-16 11:36:29 -0700488static int ocrdma_mbx_mq_cq_create(struct ocrdma_dev *dev,
489 struct ocrdma_queue_info *cq,
490 struct ocrdma_queue_info *eq)
Parav Panditfe2caef2012-03-21 04:09:06 +0530491{
492 struct ocrdma_create_cq_cmd *cmd = dev->mbx_cmd;
493 struct ocrdma_create_cq_cmd_rsp *rsp = dev->mbx_cmd;
494 int status;
495
496 memset(cmd, 0, sizeof(*cmd));
497 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_CQ,
498 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
499
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530500 cmd->req.rsvd_version = OCRDMA_CREATE_CQ_VER2;
501 cmd->pgsz_pgcnt = (cq->size / OCRDMA_MIN_Q_PAGE_SIZE) <<
502 OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
503 cmd->pgsz_pgcnt |= PAGES_4K_SPANNED(cq->va, cq->size);
Parav Panditfe2caef2012-03-21 04:09:06 +0530504
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530505 cmd->ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
506 cmd->eqn = eq->id;
507 cmd->cqe_count = cq->size / sizeof(struct ocrdma_mcqe);
508
509 ocrdma_build_q_pages(&cmd->pa[0], cq->size / OCRDMA_MIN_Q_PAGE_SIZE,
Parav Panditfe2caef2012-03-21 04:09:06 +0530510 cq->dma, PAGE_SIZE_4K);
511 status = be_roce_mcc_cmd(dev->nic_info.netdev,
512 cmd, sizeof(*cmd), NULL, NULL);
513 if (!status) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530514 cq->id = (u16) (rsp->cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +0530515 cq->created = true;
516 }
517 return status;
518}
519
520static u32 ocrdma_encoded_q_len(int q_len)
521{
522 u32 len_encoded = fls(q_len); /* log2(len) + 1 */
523
524 if (len_encoded == 16)
525 len_encoded = 0;
526 return len_encoded;
527}
528
529static int ocrdma_mbx_create_mq(struct ocrdma_dev *dev,
530 struct ocrdma_queue_info *mq,
531 struct ocrdma_queue_info *cq)
532{
533 int num_pages, status;
534 struct ocrdma_create_mq_req *cmd = dev->mbx_cmd;
535 struct ocrdma_create_mq_rsp *rsp = dev->mbx_cmd;
536 struct ocrdma_pa *pa;
537
538 memset(cmd, 0, sizeof(*cmd));
539 num_pages = PAGES_4K_SPANNED(mq->va, mq->size);
540
Naresh Gottumukkalab1d58b92013-06-10 04:42:38 +0000541 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_MQ_EXT,
542 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
543 cmd->req.rsvd_version = 1;
544 cmd->cqid_pages = num_pages;
545 cmd->cqid_pages |= (cq->id << OCRDMA_CREATE_MQ_CQ_ID_SHIFT);
546 cmd->async_cqid_valid = OCRDMA_CREATE_MQ_ASYNC_CQ_VALID;
547 cmd->async_event_bitmap = Bit(20);
548 cmd->async_cqid_ringsize = cq->id;
549 cmd->async_cqid_ringsize |= (ocrdma_encoded_q_len(mq->len) <<
550 OCRDMA_CREATE_MQ_RING_SIZE_SHIFT);
551 cmd->valid = OCRDMA_CREATE_MQ_VALID;
552 pa = &cmd->pa[0];
553
Parav Panditfe2caef2012-03-21 04:09:06 +0530554 ocrdma_build_q_pages(pa, num_pages, mq->dma, PAGE_SIZE_4K);
555 status = be_roce_mcc_cmd(dev->nic_info.netdev,
556 cmd, sizeof(*cmd), NULL, NULL);
557 if (!status) {
558 mq->id = rsp->id;
559 mq->created = true;
560 }
561 return status;
562}
563
564static int ocrdma_create_mq(struct ocrdma_dev *dev)
565{
566 int status;
567
568 /* Alloc completion queue for Mailbox queue */
569 status = ocrdma_alloc_q(dev, &dev->mq.cq, OCRDMA_MQ_CQ_LEN,
570 sizeof(struct ocrdma_mcqe));
571 if (status)
572 goto alloc_err;
573
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +0530574 status = ocrdma_mbx_mq_cq_create(dev, &dev->mq.cq, &dev->eq_tbl[0].q);
Parav Panditfe2caef2012-03-21 04:09:06 +0530575 if (status)
576 goto mbx_cq_free;
577
578 memset(&dev->mqe_ctx, 0, sizeof(dev->mqe_ctx));
579 init_waitqueue_head(&dev->mqe_ctx.cmd_wait);
580 mutex_init(&dev->mqe_ctx.lock);
581
582 /* Alloc Mailbox queue */
583 status = ocrdma_alloc_q(dev, &dev->mq.sq, OCRDMA_MQ_LEN,
584 sizeof(struct ocrdma_mqe));
585 if (status)
586 goto mbx_cq_destroy;
587 status = ocrdma_mbx_create_mq(dev, &dev->mq.sq, &dev->mq.cq);
588 if (status)
589 goto mbx_q_free;
590 ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, 0);
591 return 0;
592
593mbx_q_free:
594 ocrdma_free_q(dev, &dev->mq.sq);
595mbx_cq_destroy:
596 ocrdma_mbx_delete_q(dev, &dev->mq.cq, QTYPE_CQ);
597mbx_cq_free:
598 ocrdma_free_q(dev, &dev->mq.cq);
599alloc_err:
600 return status;
601}
602
603static void ocrdma_destroy_mq(struct ocrdma_dev *dev)
604{
605 struct ocrdma_queue_info *mbxq, *cq;
606
607 /* mqe_ctx lock synchronizes with any other pending cmds. */
608 mutex_lock(&dev->mqe_ctx.lock);
609 mbxq = &dev->mq.sq;
610 if (mbxq->created) {
611 ocrdma_mbx_delete_q(dev, mbxq, QTYPE_MCCQ);
612 ocrdma_free_q(dev, mbxq);
613 }
614 mutex_unlock(&dev->mqe_ctx.lock);
615
616 cq = &dev->mq.cq;
617 if (cq->created) {
618 ocrdma_mbx_delete_q(dev, cq, QTYPE_CQ);
619 ocrdma_free_q(dev, cq);
620 }
621}
622
623static void ocrdma_process_qpcat_error(struct ocrdma_dev *dev,
624 struct ocrdma_qp *qp)
625{
626 enum ib_qp_state new_ib_qps = IB_QPS_ERR;
627 enum ib_qp_state old_ib_qps;
628
629 if (qp == NULL)
630 BUG();
Naresh Gottumukkala057729c2013-08-07 12:52:35 +0530631 ocrdma_qp_state_change(qp, new_ib_qps, &old_ib_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +0530632}
633
634static void ocrdma_dispatch_ibevent(struct ocrdma_dev *dev,
635 struct ocrdma_ae_mcqe *cqe)
636{
637 struct ocrdma_qp *qp = NULL;
638 struct ocrdma_cq *cq = NULL;
Roland Dreiere9db2952012-04-16 12:13:24 -0700639 struct ib_event ib_evt;
Parav Panditfe2caef2012-03-21 04:09:06 +0530640 int cq_event = 0;
641 int qp_event = 1;
642 int srq_event = 0;
643 int dev_event = 0;
644 int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
645 OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
646
647 if (cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPVALID)
648 qp = dev->qp_tbl[cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPID_MASK];
649 if (cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQVALID)
650 cq = dev->cq_tbl[cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQID_MASK];
651
Roland Dreiere9db2952012-04-16 12:13:24 -0700652 ib_evt.device = &dev->ibdev;
653
Parav Panditfe2caef2012-03-21 04:09:06 +0530654 switch (type) {
655 case OCRDMA_CQ_ERROR:
656 ib_evt.element.cq = &cq->ibcq;
657 ib_evt.event = IB_EVENT_CQ_ERR;
658 cq_event = 1;
659 qp_event = 0;
660 break;
661 case OCRDMA_CQ_OVERRUN_ERROR:
662 ib_evt.element.cq = &cq->ibcq;
663 ib_evt.event = IB_EVENT_CQ_ERR;
664 break;
665 case OCRDMA_CQ_QPCAT_ERROR:
666 ib_evt.element.qp = &qp->ibqp;
667 ib_evt.event = IB_EVENT_QP_FATAL;
668 ocrdma_process_qpcat_error(dev, qp);
669 break;
670 case OCRDMA_QP_ACCESS_ERROR:
671 ib_evt.element.qp = &qp->ibqp;
672 ib_evt.event = IB_EVENT_QP_ACCESS_ERR;
673 break;
674 case OCRDMA_QP_COMM_EST_EVENT:
675 ib_evt.element.qp = &qp->ibqp;
676 ib_evt.event = IB_EVENT_COMM_EST;
677 break;
678 case OCRDMA_SQ_DRAINED_EVENT:
679 ib_evt.element.qp = &qp->ibqp;
680 ib_evt.event = IB_EVENT_SQ_DRAINED;
681 break;
682 case OCRDMA_DEVICE_FATAL_EVENT:
683 ib_evt.element.port_num = 1;
684 ib_evt.event = IB_EVENT_DEVICE_FATAL;
685 qp_event = 0;
686 dev_event = 1;
687 break;
688 case OCRDMA_SRQCAT_ERROR:
689 ib_evt.element.srq = &qp->srq->ibsrq;
690 ib_evt.event = IB_EVENT_SRQ_ERR;
691 srq_event = 1;
692 qp_event = 0;
693 break;
694 case OCRDMA_SRQ_LIMIT_EVENT:
695 ib_evt.element.srq = &qp->srq->ibsrq;
Parav Pandit804eaf22012-05-23 21:11:17 +0530696 ib_evt.event = IB_EVENT_SRQ_LIMIT_REACHED;
Parav Panditfe2caef2012-03-21 04:09:06 +0530697 srq_event = 1;
698 qp_event = 0;
699 break;
700 case OCRDMA_QP_LAST_WQE_EVENT:
701 ib_evt.element.qp = &qp->ibqp;
702 ib_evt.event = IB_EVENT_QP_LAST_WQE_REACHED;
703 break;
704 default:
705 cq_event = 0;
706 qp_event = 0;
707 srq_event = 0;
708 dev_event = 0;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000709 pr_err("%s() unknown type=0x%x\n", __func__, type);
Parav Panditfe2caef2012-03-21 04:09:06 +0530710 break;
711 }
712
713 if (qp_event) {
714 if (qp->ibqp.event_handler)
715 qp->ibqp.event_handler(&ib_evt, qp->ibqp.qp_context);
716 } else if (cq_event) {
717 if (cq->ibcq.event_handler)
718 cq->ibcq.event_handler(&ib_evt, cq->ibcq.cq_context);
719 } else if (srq_event) {
720 if (qp->srq->ibsrq.event_handler)
721 qp->srq->ibsrq.event_handler(&ib_evt,
722 qp->srq->ibsrq.
723 srq_context);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530724 } else if (dev_event) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530725 ib_dispatch_event(&ib_evt);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530726 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530727
728}
729
730static void ocrdma_process_acqe(struct ocrdma_dev *dev, void *ae_cqe)
731{
732 /* async CQE processing */
733 struct ocrdma_ae_mcqe *cqe = ae_cqe;
734 u32 evt_code = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_CODE_MASK) >>
735 OCRDMA_AE_MCQE_EVENT_CODE_SHIFT;
736
737 if (evt_code == OCRDMA_ASYNC_EVE_CODE)
738 ocrdma_dispatch_ibevent(dev, cqe);
739 else
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000740 pr_err("%s(%d) invalid evt code=0x%x\n", __func__,
741 dev->id, evt_code);
Parav Panditfe2caef2012-03-21 04:09:06 +0530742}
743
744static void ocrdma_process_mcqe(struct ocrdma_dev *dev, struct ocrdma_mcqe *cqe)
745{
746 if (dev->mqe_ctx.tag == cqe->tag_lo && dev->mqe_ctx.cmd_done == false) {
747 dev->mqe_ctx.cqe_status = (cqe->status &
748 OCRDMA_MCQE_STATUS_MASK) >> OCRDMA_MCQE_STATUS_SHIFT;
749 dev->mqe_ctx.ext_status =
750 (cqe->status & OCRDMA_MCQE_ESTATUS_MASK)
751 >> OCRDMA_MCQE_ESTATUS_SHIFT;
752 dev->mqe_ctx.cmd_done = true;
753 wake_up(&dev->mqe_ctx.cmd_wait);
754 } else
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000755 pr_err("%s() cqe for invalid tag0x%x.expected=0x%x\n",
756 __func__, cqe->tag_lo, dev->mqe_ctx.tag);
Parav Panditfe2caef2012-03-21 04:09:06 +0530757}
758
759static int ocrdma_mq_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
760{
761 u16 cqe_popped = 0;
762 struct ocrdma_mcqe *cqe;
763
764 while (1) {
765 cqe = ocrdma_get_mcqe(dev);
766 if (cqe == NULL)
767 break;
768 ocrdma_le32_to_cpu(cqe, sizeof(*cqe));
769 cqe_popped += 1;
770 if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_AE_MASK)
771 ocrdma_process_acqe(dev, cqe);
772 else if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_CMPL_MASK)
773 ocrdma_process_mcqe(dev, cqe);
774 else
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000775 pr_err("%s() cqe->compl is not set.\n", __func__);
Parav Panditfe2caef2012-03-21 04:09:06 +0530776 memset(cqe, 0, sizeof(struct ocrdma_mcqe));
777 ocrdma_mcq_inc_tail(dev);
778 }
779 ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, cqe_popped);
780 return 0;
781}
782
783static void ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
784 struct ocrdma_cq *cq)
785{
786 unsigned long flags;
787 struct ocrdma_qp *qp;
788 bool buddy_cq_found = false;
789 /* Go through list of QPs in error state which are using this CQ
790 * and invoke its callback handler to trigger CQE processing for
791 * error/flushed CQE. It is rare to find more than few entries in
792 * this list as most consumers stops after getting error CQE.
793 * List is traversed only once when a matching buddy cq found for a QP.
794 */
795 spin_lock_irqsave(&dev->flush_q_lock, flags);
796 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
797 if (qp->srq)
798 continue;
799 /* if wq and rq share the same cq, than comp_handler
800 * is already invoked.
801 */
802 if (qp->sq_cq == qp->rq_cq)
803 continue;
804 /* if completion came on sq, rq's cq is buddy cq.
805 * if completion came on rq, sq's cq is buddy cq.
806 */
807 if (qp->sq_cq == cq)
808 cq = qp->rq_cq;
809 else
810 cq = qp->sq_cq;
811 buddy_cq_found = true;
812 break;
813 }
814 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
815 if (buddy_cq_found == false)
816 return;
817 if (cq->ibcq.comp_handler) {
818 spin_lock_irqsave(&cq->comp_handler_lock, flags);
819 (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
820 spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
821 }
822}
823
824static void ocrdma_qp_cq_handler(struct ocrdma_dev *dev, u16 cq_idx)
825{
826 unsigned long flags;
827 struct ocrdma_cq *cq;
828
829 if (cq_idx >= OCRDMA_MAX_CQ)
830 BUG();
831
832 cq = dev->cq_tbl[cq_idx];
833 if (cq == NULL) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000834 pr_err("%s%d invalid id=0x%x\n", __func__, dev->id, cq_idx);
Parav Panditfe2caef2012-03-21 04:09:06 +0530835 return;
836 }
837 spin_lock_irqsave(&cq->cq_lock, flags);
838 cq->armed = false;
839 cq->solicited = false;
840 spin_unlock_irqrestore(&cq->cq_lock, flags);
841
842 ocrdma_ring_cq_db(dev, cq->id, false, false, 0);
843
844 if (cq->ibcq.comp_handler) {
845 spin_lock_irqsave(&cq->comp_handler_lock, flags);
846 (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
847 spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
848 }
849 ocrdma_qp_buddy_cq_handler(dev, cq);
850}
851
852static void ocrdma_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
853{
854 /* process the MQ-CQE. */
855 if (cq_id == dev->mq.cq.id)
856 ocrdma_mq_cq_handler(dev, cq_id);
857 else
858 ocrdma_qp_cq_handler(dev, cq_id);
859}
860
861static irqreturn_t ocrdma_irq_handler(int irq, void *handle)
862{
863 struct ocrdma_eq *eq = handle;
864 struct ocrdma_dev *dev = eq->dev;
865 struct ocrdma_eqe eqe;
866 struct ocrdma_eqe *ptr;
867 u16 eqe_popped = 0;
868 u16 cq_id;
869 while (1) {
870 ptr = ocrdma_get_eqe(eq);
871 eqe = *ptr;
872 ocrdma_le32_to_cpu(&eqe, sizeof(eqe));
873 if ((eqe.id_valid & OCRDMA_EQE_VALID_MASK) == 0)
874 break;
875 eqe_popped += 1;
876 ptr->id_valid = 0;
877 /* check whether its CQE or not. */
878 if ((eqe.id_valid & OCRDMA_EQE_FOR_CQE_MASK) == 0) {
879 cq_id = eqe.id_valid >> OCRDMA_EQE_RESOURCE_ID_SHIFT;
880 ocrdma_cq_handler(dev, cq_id);
881 }
882 ocrdma_eq_inc_tail(eq);
883 }
884 ocrdma_ring_eq_db(dev, eq->q.id, true, true, eqe_popped);
885 /* Ring EQ doorbell with num_popped to 0 to enable interrupts again. */
886 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
887 ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
888 return IRQ_HANDLED;
889}
890
891static void ocrdma_post_mqe(struct ocrdma_dev *dev, struct ocrdma_mqe *cmd)
892{
893 struct ocrdma_mqe *mqe;
894
895 dev->mqe_ctx.tag = dev->mq.sq.head;
896 dev->mqe_ctx.cmd_done = false;
897 mqe = ocrdma_get_mqe(dev);
898 cmd->hdr.tag_lo = dev->mq.sq.head;
899 ocrdma_copy_cpu_to_le32(mqe, cmd, sizeof(*mqe));
900 /* make sure descriptor is written before ringing doorbell */
901 wmb();
902 ocrdma_mq_inc_head(dev);
903 ocrdma_ring_mq_db(dev);
904}
905
906static int ocrdma_wait_mqe_cmpl(struct ocrdma_dev *dev)
907{
908 long status;
909 /* 30 sec timeout */
910 status = wait_event_timeout(dev->mqe_ctx.cmd_wait,
911 (dev->mqe_ctx.cmd_done != false),
912 msecs_to_jiffies(30000));
913 if (status)
914 return 0;
915 else
916 return -1;
917}
918
919/* issue a mailbox command on the MQ */
920static int ocrdma_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe)
921{
922 int status = 0;
923 u16 cqe_status, ext_status;
924 struct ocrdma_mqe *rsp;
925
926 mutex_lock(&dev->mqe_ctx.lock);
927 ocrdma_post_mqe(dev, mqe);
928 status = ocrdma_wait_mqe_cmpl(dev);
929 if (status)
930 goto mbx_err;
931 cqe_status = dev->mqe_ctx.cqe_status;
932 ext_status = dev->mqe_ctx.ext_status;
933 rsp = ocrdma_get_mqe_rsp(dev);
934 ocrdma_copy_le32_to_cpu(mqe, rsp, (sizeof(*mqe)));
935 if (cqe_status || ext_status) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530936 pr_err("%s() opcode=0x%x, cqe_status=0x%x, ext_status=0x%x\n",
937 __func__,
Parav Panditfe2caef2012-03-21 04:09:06 +0530938 (rsp->u.rsp.subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
939 OCRDMA_MBX_RSP_OPCODE_SHIFT, cqe_status, ext_status);
940 status = ocrdma_get_mbx_cqe_errno(cqe_status);
941 goto mbx_err;
942 }
943 if (mqe->u.rsp.status & OCRDMA_MBX_RSP_STATUS_MASK)
944 status = ocrdma_get_mbx_errno(mqe->u.rsp.status);
945mbx_err:
946 mutex_unlock(&dev->mqe_ctx.lock);
947 return status;
948}
949
950static void ocrdma_get_attr(struct ocrdma_dev *dev,
951 struct ocrdma_dev_attr *attr,
952 struct ocrdma_mbx_query_config *rsp)
953{
Parav Panditfe2caef2012-03-21 04:09:06 +0530954 attr->max_pd =
955 (rsp->max_pd_ca_ack_delay & OCRDMA_MBX_QUERY_CFG_MAX_PD_MASK) >>
956 OCRDMA_MBX_QUERY_CFG_MAX_PD_SHIFT;
957 attr->max_qp =
958 (rsp->qp_srq_cq_ird_ord & OCRDMA_MBX_QUERY_CFG_MAX_QP_MASK) >>
959 OCRDMA_MBX_QUERY_CFG_MAX_QP_SHIFT;
960 attr->max_send_sge = ((rsp->max_write_send_sge &
961 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
962 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT);
963 attr->max_recv_sge = (rsp->max_write_send_sge &
964 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
965 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT;
Mahesh Vardhamanaiah634c5792012-06-08 21:26:11 +0530966 attr->max_srq_sge = (rsp->max_srq_rqe_sge &
967 OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_MASK) >>
968 OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_OFFSET;
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530969 attr->max_rdma_sge = (rsp->max_write_send_sge &
970 OCRDMA_MBX_QUERY_CFG_MAX_WRITE_SGE_MASK) >>
971 OCRDMA_MBX_QUERY_CFG_MAX_WRITE_SGE_SHIFT;
Parav Panditfe2caef2012-03-21 04:09:06 +0530972 attr->max_ord_per_qp = (rsp->max_ird_ord_per_qp &
973 OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_MASK) >>
974 OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_SHIFT;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530975 attr->max_srq =
976 (rsp->max_srq_rpir_qps & OCRDMA_MBX_QUERY_CFG_MAX_SRQ_MASK) >>
977 OCRDMA_MBX_QUERY_CFG_MAX_SRQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +0530978 attr->max_ird_per_qp = (rsp->max_ird_ord_per_qp &
979 OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_MASK) >>
980 OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_SHIFT;
981 attr->cq_overflow_detect = (rsp->qp_srq_cq_ird_ord &
982 OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_MASK) >>
983 OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_SHIFT;
984 attr->srq_supported = (rsp->qp_srq_cq_ird_ord &
985 OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_MASK) >>
986 OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_SHIFT;
987 attr->local_ca_ack_delay = (rsp->max_pd_ca_ack_delay &
988 OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_MASK) >>
989 OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_SHIFT;
990 attr->max_mr = rsp->max_mr;
991 attr->max_mr_size = ~0ull;
992 attr->max_fmr = 0;
993 attr->max_pages_per_frmr = rsp->max_pages_per_frmr;
994 attr->max_num_mr_pbl = rsp->max_num_mr_pbl;
995 attr->max_cqe = rsp->max_cq_cqes_per_cq &
996 OCRDMA_MBX_QUERY_CFG_MAX_CQES_PER_CQ_MASK;
Naresh Gottumukkalac43e9ab2013-08-26 15:27:46 +0530997 attr->max_cq = (rsp->max_cq_cqes_per_cq &
998 OCRDMA_MBX_QUERY_CFG_MAX_CQ_MASK) >>
999 OCRDMA_MBX_QUERY_CFG_MAX_CQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301000 attr->wqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1001 OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_MASK) >>
1002 OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_OFFSET) *
1003 OCRDMA_WQE_STRIDE;
1004 attr->rqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1005 OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_MASK) >>
1006 OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_OFFSET) *
1007 OCRDMA_WQE_STRIDE;
1008 attr->max_inline_data =
1009 attr->wqe_size - (sizeof(struct ocrdma_hdr_wqe) +
1010 sizeof(struct ocrdma_sge));
Parav Panditfe2caef2012-03-21 04:09:06 +05301011 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301012 attr->ird = 1;
1013 attr->ird_page_size = OCRDMA_MIN_Q_PAGE_SIZE;
1014 attr->num_ird_pages = MAX_OCRDMA_IRD_PAGES;
Mahesh Vardhamanaiah07bb5422012-06-08 21:25:52 +05301015 }
1016 dev->attr.max_wqe = rsp->max_wqes_rqes_per_q >>
1017 OCRDMA_MBX_QUERY_CFG_MAX_WQES_PER_WQ_OFFSET;
1018 dev->attr.max_rqe = rsp->max_wqes_rqes_per_q &
1019 OCRDMA_MBX_QUERY_CFG_MAX_RQES_PER_RQ_MASK;
Parav Panditfe2caef2012-03-21 04:09:06 +05301020}
1021
1022static int ocrdma_check_fw_config(struct ocrdma_dev *dev,
1023 struct ocrdma_fw_conf_rsp *conf)
1024{
1025 u32 fn_mode;
1026
1027 fn_mode = conf->fn_mode & OCRDMA_FN_MODE_RDMA;
1028 if (fn_mode != OCRDMA_FN_MODE_RDMA)
1029 return -EINVAL;
1030 dev->base_eqid = conf->base_eqid;
1031 dev->max_eq = conf->max_eq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301032 return 0;
1033}
1034
1035/* can be issued only during init time. */
1036static int ocrdma_mbx_query_fw_ver(struct ocrdma_dev *dev)
1037{
1038 int status = -ENOMEM;
1039 struct ocrdma_mqe *cmd;
1040 struct ocrdma_fw_ver_rsp *rsp;
1041
1042 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_VER, sizeof(*cmd));
1043 if (!cmd)
1044 return -ENOMEM;
1045 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1046 OCRDMA_CMD_GET_FW_VER,
1047 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1048
1049 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1050 if (status)
1051 goto mbx_err;
1052 rsp = (struct ocrdma_fw_ver_rsp *)cmd;
1053 memset(&dev->attr.fw_ver[0], 0, sizeof(dev->attr.fw_ver));
1054 memcpy(&dev->attr.fw_ver[0], &rsp->running_ver[0],
1055 sizeof(rsp->running_ver));
1056 ocrdma_le32_to_cpu(dev->attr.fw_ver, sizeof(rsp->running_ver));
1057mbx_err:
1058 kfree(cmd);
1059 return status;
1060}
1061
1062/* can be issued only during init time. */
1063static int ocrdma_mbx_query_fw_config(struct ocrdma_dev *dev)
1064{
1065 int status = -ENOMEM;
1066 struct ocrdma_mqe *cmd;
1067 struct ocrdma_fw_conf_rsp *rsp;
1068
1069 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_CONFIG, sizeof(*cmd));
1070 if (!cmd)
1071 return -ENOMEM;
1072 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1073 OCRDMA_CMD_GET_FW_CONFIG,
1074 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1075 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1076 if (status)
1077 goto mbx_err;
1078 rsp = (struct ocrdma_fw_conf_rsp *)cmd;
1079 status = ocrdma_check_fw_config(dev, rsp);
1080mbx_err:
1081 kfree(cmd);
1082 return status;
1083}
1084
1085static int ocrdma_mbx_query_dev(struct ocrdma_dev *dev)
1086{
1087 int status = -ENOMEM;
1088 struct ocrdma_mbx_query_config *rsp;
1089 struct ocrdma_mqe *cmd;
1090
1091 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_CONFIG, sizeof(*cmd));
1092 if (!cmd)
1093 return status;
1094 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1095 if (status)
1096 goto mbx_err;
1097 rsp = (struct ocrdma_mbx_query_config *)cmd;
1098 ocrdma_get_attr(dev, &dev->attr, rsp);
1099mbx_err:
1100 kfree(cmd);
1101 return status;
1102}
1103
1104int ocrdma_mbx_alloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1105{
1106 int status = -ENOMEM;
1107 struct ocrdma_alloc_pd *cmd;
1108 struct ocrdma_alloc_pd_rsp *rsp;
1109
1110 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD, sizeof(*cmd));
1111 if (!cmd)
1112 return status;
1113 if (pd->dpp_enabled)
1114 cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
1115 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1116 if (status)
1117 goto mbx_err;
1118 rsp = (struct ocrdma_alloc_pd_rsp *)cmd;
1119 pd->id = rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_PDID_MASK;
1120 if (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) {
1121 pd->dpp_enabled = true;
1122 pd->dpp_page = rsp->dpp_page_pdid >>
1123 OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
1124 } else {
1125 pd->dpp_enabled = false;
1126 pd->num_dpp_qp = 0;
1127 }
1128mbx_err:
1129 kfree(cmd);
1130 return status;
1131}
1132
1133int ocrdma_mbx_dealloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1134{
1135 int status = -ENOMEM;
1136 struct ocrdma_dealloc_pd *cmd;
1137
1138 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD, sizeof(*cmd));
1139 if (!cmd)
1140 return status;
1141 cmd->id = pd->id;
1142 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1143 kfree(cmd);
1144 return status;
1145}
1146
1147static int ocrdma_build_q_conf(u32 *num_entries, int entry_size,
1148 int *num_pages, int *page_size)
1149{
1150 int i;
1151 int mem_size;
1152
1153 *num_entries = roundup_pow_of_two(*num_entries);
1154 mem_size = *num_entries * entry_size;
1155 /* find the possible lowest possible multiplier */
1156 for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1157 if (mem_size <= (OCRDMA_Q_PAGE_BASE_SIZE << i))
1158 break;
1159 }
1160 if (i >= OCRDMA_MAX_Q_PAGE_SIZE_CNT)
1161 return -EINVAL;
1162 mem_size = roundup(mem_size,
1163 ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES));
1164 *num_pages =
1165 mem_size / ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1166 *page_size = ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1167 *num_entries = mem_size / entry_size;
1168 return 0;
1169}
1170
1171static int ocrdma_mbx_create_ah_tbl(struct ocrdma_dev *dev)
1172{
1173 int i ;
1174 int status = 0;
1175 int max_ah;
1176 struct ocrdma_create_ah_tbl *cmd;
1177 struct ocrdma_create_ah_tbl_rsp *rsp;
1178 struct pci_dev *pdev = dev->nic_info.pdev;
1179 dma_addr_t pa;
1180 struct ocrdma_pbe *pbes;
1181
1182 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_AH_TBL, sizeof(*cmd));
1183 if (!cmd)
1184 return status;
1185
1186 max_ah = OCRDMA_MAX_AH;
1187 dev->av_tbl.size = sizeof(struct ocrdma_av) * max_ah;
1188
1189 /* number of PBEs in PBL */
1190 cmd->ah_conf = (OCRDMA_AH_TBL_PAGES <<
1191 OCRDMA_CREATE_AH_NUM_PAGES_SHIFT) &
1192 OCRDMA_CREATE_AH_NUM_PAGES_MASK;
1193
1194 /* page size */
1195 for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1196 if (PAGE_SIZE == (OCRDMA_MIN_Q_PAGE_SIZE << i))
1197 break;
1198 }
1199 cmd->ah_conf |= (i << OCRDMA_CREATE_AH_PAGE_SIZE_SHIFT) &
1200 OCRDMA_CREATE_AH_PAGE_SIZE_MASK;
1201
1202 /* ah_entry size */
1203 cmd->ah_conf |= (sizeof(struct ocrdma_av) <<
1204 OCRDMA_CREATE_AH_ENTRY_SIZE_SHIFT) &
1205 OCRDMA_CREATE_AH_ENTRY_SIZE_MASK;
1206
1207 dev->av_tbl.pbl.va = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
1208 &dev->av_tbl.pbl.pa,
1209 GFP_KERNEL);
1210 if (dev->av_tbl.pbl.va == NULL)
1211 goto mem_err;
1212
1213 dev->av_tbl.va = dma_alloc_coherent(&pdev->dev, dev->av_tbl.size,
1214 &pa, GFP_KERNEL);
1215 if (dev->av_tbl.va == NULL)
1216 goto mem_err_ah;
1217 dev->av_tbl.pa = pa;
1218 dev->av_tbl.num_ah = max_ah;
1219 memset(dev->av_tbl.va, 0, dev->av_tbl.size);
1220
1221 pbes = (struct ocrdma_pbe *)dev->av_tbl.pbl.va;
1222 for (i = 0; i < dev->av_tbl.size / OCRDMA_MIN_Q_PAGE_SIZE; i++) {
1223 pbes[i].pa_lo = (u32) (pa & 0xffffffff);
1224 pbes[i].pa_hi = (u32) upper_32_bits(pa);
1225 pa += PAGE_SIZE;
1226 }
1227 cmd->tbl_addr[0].lo = (u32)(dev->av_tbl.pbl.pa & 0xFFFFFFFF);
1228 cmd->tbl_addr[0].hi = (u32)upper_32_bits(dev->av_tbl.pbl.pa);
1229 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1230 if (status)
1231 goto mbx_err;
1232 rsp = (struct ocrdma_create_ah_tbl_rsp *)cmd;
1233 dev->av_tbl.ahid = rsp->ahid & 0xFFFF;
1234 kfree(cmd);
1235 return 0;
1236
1237mbx_err:
1238 dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1239 dev->av_tbl.pa);
1240 dev->av_tbl.va = NULL;
1241mem_err_ah:
1242 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1243 dev->av_tbl.pbl.pa);
1244 dev->av_tbl.pbl.va = NULL;
1245 dev->av_tbl.size = 0;
1246mem_err:
1247 kfree(cmd);
1248 return status;
1249}
1250
1251static void ocrdma_mbx_delete_ah_tbl(struct ocrdma_dev *dev)
1252{
1253 struct ocrdma_delete_ah_tbl *cmd;
1254 struct pci_dev *pdev = dev->nic_info.pdev;
1255
1256 if (dev->av_tbl.va == NULL)
1257 return;
1258
1259 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_AH_TBL, sizeof(*cmd));
1260 if (!cmd)
1261 return;
1262 cmd->ahid = dev->av_tbl.ahid;
1263
1264 ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1265 dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1266 dev->av_tbl.pa);
1267 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1268 dev->av_tbl.pbl.pa);
1269 kfree(cmd);
1270}
1271
1272/* Multiple CQs uses the EQ. This routine returns least used
1273 * EQ to associate with CQ. This will distributes the interrupt
1274 * processing and CPU load to associated EQ, vector and so to that CPU.
1275 */
1276static u16 ocrdma_bind_eq(struct ocrdma_dev *dev)
1277{
1278 int i, selected_eq = 0, cq_cnt = 0;
1279 u16 eq_id;
1280
1281 mutex_lock(&dev->dev_lock);
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05301282 cq_cnt = dev->eq_tbl[0].cq_cnt;
1283 eq_id = dev->eq_tbl[0].q.id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301284 /* find the EQ which is has the least number of
1285 * CQs associated with it.
1286 */
1287 for (i = 0; i < dev->eq_cnt; i++) {
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05301288 if (dev->eq_tbl[i].cq_cnt < cq_cnt) {
1289 cq_cnt = dev->eq_tbl[i].cq_cnt;
1290 eq_id = dev->eq_tbl[i].q.id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301291 selected_eq = i;
1292 }
1293 }
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05301294 dev->eq_tbl[selected_eq].cq_cnt += 1;
Parav Panditfe2caef2012-03-21 04:09:06 +05301295 mutex_unlock(&dev->dev_lock);
1296 return eq_id;
1297}
1298
1299static void ocrdma_unbind_eq(struct ocrdma_dev *dev, u16 eq_id)
1300{
1301 int i;
1302
1303 mutex_lock(&dev->dev_lock);
1304 for (i = 0; i < dev->eq_cnt; i++) {
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05301305 if (dev->eq_tbl[i].q.id != eq_id)
Parav Panditfe2caef2012-03-21 04:09:06 +05301306 continue;
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05301307 dev->eq_tbl[i].cq_cnt -= 1;
Parav Panditfe2caef2012-03-21 04:09:06 +05301308 break;
1309 }
1310 mutex_unlock(&dev->dev_lock);
1311}
1312
1313int ocrdma_mbx_create_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301314 int entries, int dpp_cq, u16 pd_id)
Parav Panditfe2caef2012-03-21 04:09:06 +05301315{
1316 int status = -ENOMEM; int max_hw_cqe;
1317 struct pci_dev *pdev = dev->nic_info.pdev;
1318 struct ocrdma_create_cq *cmd;
1319 struct ocrdma_create_cq_rsp *rsp;
1320 u32 hw_pages, cqe_size, page_size, cqe_count;
1321
Parav Panditfe2caef2012-03-21 04:09:06 +05301322 if (entries > dev->attr.max_cqe) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001323 pr_err("%s(%d) max_cqe=0x%x, requester_cqe=0x%x\n",
1324 __func__, dev->id, dev->attr.max_cqe, entries);
Parav Panditfe2caef2012-03-21 04:09:06 +05301325 return -EINVAL;
1326 }
1327 if (dpp_cq && (dev->nic_info.dev_family != OCRDMA_GEN2_FAMILY))
1328 return -EINVAL;
1329
1330 if (dpp_cq) {
1331 cq->max_hw_cqe = 1;
1332 max_hw_cqe = 1;
1333 cqe_size = OCRDMA_DPP_CQE_SIZE;
1334 hw_pages = 1;
1335 } else {
1336 cq->max_hw_cqe = dev->attr.max_cqe;
1337 max_hw_cqe = dev->attr.max_cqe;
1338 cqe_size = sizeof(struct ocrdma_cqe);
1339 hw_pages = OCRDMA_CREATE_CQ_MAX_PAGES;
1340 }
1341
1342 cq->len = roundup(max_hw_cqe * cqe_size, OCRDMA_MIN_Q_PAGE_SIZE);
1343
1344 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_CQ, sizeof(*cmd));
1345 if (!cmd)
1346 return -ENOMEM;
1347 ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_CREATE_CQ,
1348 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1349 cq->va = dma_alloc_coherent(&pdev->dev, cq->len, &cq->pa, GFP_KERNEL);
1350 if (!cq->va) {
1351 status = -ENOMEM;
1352 goto mem_err;
1353 }
1354 memset(cq->va, 0, cq->len);
1355 page_size = cq->len / hw_pages;
1356 cmd->cmd.pgsz_pgcnt = (page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
1357 OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
1358 cmd->cmd.pgsz_pgcnt |= hw_pages;
1359 cmd->cmd.ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
1360
Parav Panditfe2caef2012-03-21 04:09:06 +05301361 cq->eqn = ocrdma_bind_eq(dev);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301362 cmd->cmd.req.rsvd_version = OCRDMA_CREATE_CQ_VER3;
Parav Panditfe2caef2012-03-21 04:09:06 +05301363 cqe_count = cq->len / cqe_size;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301364 if (cqe_count > 1024) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301365 /* Set cnt to 3 to indicate more than 1024 cq entries */
1366 cmd->cmd.ev_cnt_flags |= (0x3 << OCRDMA_CREATE_CQ_CNT_SHIFT);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301367 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301368 u8 count = 0;
1369 switch (cqe_count) {
1370 case 256:
1371 count = 0;
1372 break;
1373 case 512:
1374 count = 1;
1375 break;
1376 case 1024:
1377 count = 2;
1378 break;
1379 default:
1380 goto mbx_err;
1381 }
1382 cmd->cmd.ev_cnt_flags |= (count << OCRDMA_CREATE_CQ_CNT_SHIFT);
1383 }
1384 /* shared eq between all the consumer cqs. */
1385 cmd->cmd.eqn = cq->eqn;
1386 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
1387 if (dpp_cq)
1388 cmd->cmd.pgsz_pgcnt |= OCRDMA_CREATE_CQ_DPP <<
1389 OCRDMA_CREATE_CQ_TYPE_SHIFT;
1390 cq->phase_change = false;
1391 cmd->cmd.cqe_count = (cq->len / cqe_size);
1392 } else {
1393 cmd->cmd.cqe_count = (cq->len / cqe_size) - 1;
1394 cmd->cmd.ev_cnt_flags |= OCRDMA_CREATE_CQ_FLAGS_AUTO_VALID;
1395 cq->phase_change = true;
1396 }
1397
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301398 cmd->cmd.pd_id = pd_id; /* valid only for v3 */
Parav Panditfe2caef2012-03-21 04:09:06 +05301399 ocrdma_build_q_pages(&cmd->cmd.pa[0], hw_pages, cq->pa, page_size);
1400 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1401 if (status)
1402 goto mbx_err;
1403
1404 rsp = (struct ocrdma_create_cq_rsp *)cmd;
1405 cq->id = (u16) (rsp->rsp.cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
1406 kfree(cmd);
1407 return 0;
1408mbx_err:
1409 ocrdma_unbind_eq(dev, cq->eqn);
Parav Panditfe2caef2012-03-21 04:09:06 +05301410 dma_free_coherent(&pdev->dev, cq->len, cq->va, cq->pa);
1411mem_err:
1412 kfree(cmd);
1413 return status;
1414}
1415
1416int ocrdma_mbx_destroy_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq)
1417{
1418 int status = -ENOMEM;
1419 struct ocrdma_destroy_cq *cmd;
1420
1421 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_CQ, sizeof(*cmd));
1422 if (!cmd)
1423 return status;
1424 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_DELETE_CQ,
1425 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1426
1427 cmd->bypass_flush_qid |=
1428 (cq->id << OCRDMA_DESTROY_CQ_QID_SHIFT) &
1429 OCRDMA_DESTROY_CQ_QID_MASK;
1430
1431 ocrdma_unbind_eq(dev, cq->eqn);
1432 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1433 if (status)
1434 goto mbx_err;
1435 dma_free_coherent(&dev->nic_info.pdev->dev, cq->len, cq->va, cq->pa);
1436mbx_err:
1437 kfree(cmd);
1438 return status;
1439}
1440
1441int ocrdma_mbx_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1442 u32 pdid, int addr_check)
1443{
1444 int status = -ENOMEM;
1445 struct ocrdma_alloc_lkey *cmd;
1446 struct ocrdma_alloc_lkey_rsp *rsp;
1447
1448 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_LKEY, sizeof(*cmd));
1449 if (!cmd)
1450 return status;
1451 cmd->pdid = pdid;
1452 cmd->pbl_sz_flags |= addr_check;
1453 cmd->pbl_sz_flags |= (hwmr->fr_mr << OCRDMA_ALLOC_LKEY_FMR_SHIFT);
1454 cmd->pbl_sz_flags |=
1455 (hwmr->remote_wr << OCRDMA_ALLOC_LKEY_REMOTE_WR_SHIFT);
1456 cmd->pbl_sz_flags |=
1457 (hwmr->remote_rd << OCRDMA_ALLOC_LKEY_REMOTE_RD_SHIFT);
1458 cmd->pbl_sz_flags |=
1459 (hwmr->local_wr << OCRDMA_ALLOC_LKEY_LOCAL_WR_SHIFT);
1460 cmd->pbl_sz_flags |=
1461 (hwmr->remote_atomic << OCRDMA_ALLOC_LKEY_REMOTE_ATOMIC_SHIFT);
1462 cmd->pbl_sz_flags |=
1463 (hwmr->num_pbls << OCRDMA_ALLOC_LKEY_PBL_SIZE_SHIFT);
1464
1465 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1466 if (status)
1467 goto mbx_err;
1468 rsp = (struct ocrdma_alloc_lkey_rsp *)cmd;
1469 hwmr->lkey = rsp->lrkey;
1470mbx_err:
1471 kfree(cmd);
1472 return status;
1473}
1474
1475int ocrdma_mbx_dealloc_lkey(struct ocrdma_dev *dev, int fr_mr, u32 lkey)
1476{
1477 int status = -ENOMEM;
1478 struct ocrdma_dealloc_lkey *cmd;
1479
1480 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_LKEY, sizeof(*cmd));
1481 if (!cmd)
1482 return -ENOMEM;
1483 cmd->lkey = lkey;
1484 cmd->rsvd_frmr = fr_mr ? 1 : 0;
1485 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1486 if (status)
1487 goto mbx_err;
1488mbx_err:
1489 kfree(cmd);
1490 return status;
1491}
1492
1493static int ocrdma_mbx_reg_mr(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1494 u32 pdid, u32 pbl_cnt, u32 pbe_size, u32 last)
1495{
1496 int status = -ENOMEM;
1497 int i;
1498 struct ocrdma_reg_nsmr *cmd;
1499 struct ocrdma_reg_nsmr_rsp *rsp;
1500
1501 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR, sizeof(*cmd));
1502 if (!cmd)
1503 return -ENOMEM;
1504 cmd->num_pbl_pdid =
1505 pdid | (hwmr->num_pbls << OCRDMA_REG_NSMR_NUM_PBL_SHIFT);
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05301506 cmd->fr_mr = hwmr->fr_mr;
Parav Panditfe2caef2012-03-21 04:09:06 +05301507
1508 cmd->flags_hpage_pbe_sz |= (hwmr->remote_wr <<
1509 OCRDMA_REG_NSMR_REMOTE_WR_SHIFT);
1510 cmd->flags_hpage_pbe_sz |= (hwmr->remote_rd <<
1511 OCRDMA_REG_NSMR_REMOTE_RD_SHIFT);
1512 cmd->flags_hpage_pbe_sz |= (hwmr->local_wr <<
1513 OCRDMA_REG_NSMR_LOCAL_WR_SHIFT);
1514 cmd->flags_hpage_pbe_sz |= (hwmr->remote_atomic <<
1515 OCRDMA_REG_NSMR_REMOTE_ATOMIC_SHIFT);
1516 cmd->flags_hpage_pbe_sz |= (hwmr->mw_bind <<
1517 OCRDMA_REG_NSMR_BIND_MEMWIN_SHIFT);
1518 cmd->flags_hpage_pbe_sz |= (last << OCRDMA_REG_NSMR_LAST_SHIFT);
1519
1520 cmd->flags_hpage_pbe_sz |= (hwmr->pbe_size / OCRDMA_MIN_HPAGE_SIZE);
1521 cmd->flags_hpage_pbe_sz |= (hwmr->pbl_size / OCRDMA_MIN_HPAGE_SIZE) <<
1522 OCRDMA_REG_NSMR_HPAGE_SIZE_SHIFT;
1523 cmd->totlen_low = hwmr->len;
1524 cmd->totlen_high = upper_32_bits(hwmr->len);
1525 cmd->fbo_low = (u32) (hwmr->fbo & 0xffffffff);
1526 cmd->fbo_high = (u32) upper_32_bits(hwmr->fbo);
1527 cmd->va_loaddr = (u32) hwmr->va;
1528 cmd->va_hiaddr = (u32) upper_32_bits(hwmr->va);
1529
1530 for (i = 0; i < pbl_cnt; i++) {
1531 cmd->pbl[i].lo = (u32) (hwmr->pbl_table[i].pa & 0xffffffff);
1532 cmd->pbl[i].hi = upper_32_bits(hwmr->pbl_table[i].pa);
1533 }
1534 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1535 if (status)
1536 goto mbx_err;
1537 rsp = (struct ocrdma_reg_nsmr_rsp *)cmd;
1538 hwmr->lkey = rsp->lrkey;
1539mbx_err:
1540 kfree(cmd);
1541 return status;
1542}
1543
1544static int ocrdma_mbx_reg_mr_cont(struct ocrdma_dev *dev,
1545 struct ocrdma_hw_mr *hwmr, u32 pbl_cnt,
1546 u32 pbl_offset, u32 last)
1547{
1548 int status = -ENOMEM;
1549 int i;
1550 struct ocrdma_reg_nsmr_cont *cmd;
1551
1552 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR_CONT, sizeof(*cmd));
1553 if (!cmd)
1554 return -ENOMEM;
1555 cmd->lrkey = hwmr->lkey;
1556 cmd->num_pbl_offset = (pbl_cnt << OCRDMA_REG_NSMR_CONT_NUM_PBL_SHIFT) |
1557 (pbl_offset & OCRDMA_REG_NSMR_CONT_PBL_SHIFT_MASK);
1558 cmd->last = last << OCRDMA_REG_NSMR_CONT_LAST_SHIFT;
1559
1560 for (i = 0; i < pbl_cnt; i++) {
1561 cmd->pbl[i].lo =
1562 (u32) (hwmr->pbl_table[i + pbl_offset].pa & 0xffffffff);
1563 cmd->pbl[i].hi =
1564 upper_32_bits(hwmr->pbl_table[i + pbl_offset].pa);
1565 }
1566 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1567 if (status)
1568 goto mbx_err;
1569mbx_err:
1570 kfree(cmd);
1571 return status;
1572}
1573
1574int ocrdma_reg_mr(struct ocrdma_dev *dev,
1575 struct ocrdma_hw_mr *hwmr, u32 pdid, int acc)
1576{
1577 int status;
1578 u32 last = 0;
1579 u32 cur_pbl_cnt, pbl_offset;
1580 u32 pending_pbl_cnt = hwmr->num_pbls;
1581
1582 pbl_offset = 0;
1583 cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
1584 if (cur_pbl_cnt == pending_pbl_cnt)
1585 last = 1;
1586
1587 status = ocrdma_mbx_reg_mr(dev, hwmr, pdid,
1588 cur_pbl_cnt, hwmr->pbe_size, last);
1589 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001590 pr_err("%s() status=%d\n", __func__, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301591 return status;
1592 }
1593 /* if there is no more pbls to register then exit. */
1594 if (last)
1595 return 0;
1596
1597 while (!last) {
1598 pbl_offset += cur_pbl_cnt;
1599 pending_pbl_cnt -= cur_pbl_cnt;
1600 cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
1601 /* if we reach the end of the pbls, then need to set the last
1602 * bit, indicating no more pbls to register for this memory key.
1603 */
1604 if (cur_pbl_cnt == pending_pbl_cnt)
1605 last = 1;
1606
1607 status = ocrdma_mbx_reg_mr_cont(dev, hwmr, cur_pbl_cnt,
1608 pbl_offset, last);
1609 if (status)
1610 break;
1611 }
1612 if (status)
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001613 pr_err("%s() err. status=%d\n", __func__, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301614
1615 return status;
1616}
1617
1618bool ocrdma_is_qp_in_sq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
1619{
1620 struct ocrdma_qp *tmp;
1621 bool found = false;
1622 list_for_each_entry(tmp, &cq->sq_head, sq_entry) {
1623 if (qp == tmp) {
1624 found = true;
1625 break;
1626 }
1627 }
1628 return found;
1629}
1630
1631bool ocrdma_is_qp_in_rq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
1632{
1633 struct ocrdma_qp *tmp;
1634 bool found = false;
1635 list_for_each_entry(tmp, &cq->rq_head, rq_entry) {
1636 if (qp == tmp) {
1637 found = true;
1638 break;
1639 }
1640 }
1641 return found;
1642}
1643
1644void ocrdma_flush_qp(struct ocrdma_qp *qp)
1645{
1646 bool found;
1647 unsigned long flags;
1648
1649 spin_lock_irqsave(&qp->dev->flush_q_lock, flags);
1650 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1651 if (!found)
1652 list_add_tail(&qp->sq_entry, &qp->sq_cq->sq_head);
1653 if (!qp->srq) {
1654 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1655 if (!found)
1656 list_add_tail(&qp->rq_entry, &qp->rq_cq->rq_head);
1657 }
1658 spin_unlock_irqrestore(&qp->dev->flush_q_lock, flags);
1659}
1660
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301661static void ocrdma_init_hwq_ptr(struct ocrdma_qp *qp)
1662{
1663 qp->sq.head = 0;
1664 qp->sq.tail = 0;
1665 qp->rq.head = 0;
1666 qp->rq.tail = 0;
1667}
1668
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301669int ocrdma_qp_state_change(struct ocrdma_qp *qp, enum ib_qp_state new_ib_state,
1670 enum ib_qp_state *old_ib_state)
Parav Panditfe2caef2012-03-21 04:09:06 +05301671{
1672 unsigned long flags;
1673 int status = 0;
1674 enum ocrdma_qp_state new_state;
1675 new_state = get_ocrdma_qp_state(new_ib_state);
1676
1677 /* sync with wqe and rqe posting */
1678 spin_lock_irqsave(&qp->q_lock, flags);
1679
1680 if (old_ib_state)
1681 *old_ib_state = get_ibqp_state(qp->state);
1682 if (new_state == qp->state) {
1683 spin_unlock_irqrestore(&qp->q_lock, flags);
1684 return 1;
1685 }
1686
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301687
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301688 if (new_state == OCRDMA_QPS_INIT) {
1689 ocrdma_init_hwq_ptr(qp);
1690 ocrdma_del_flush_qp(qp);
1691 } else if (new_state == OCRDMA_QPS_ERR) {
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301692 ocrdma_flush_qp(qp);
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301693 }
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301694
1695 qp->state = new_state;
Parav Panditfe2caef2012-03-21 04:09:06 +05301696
1697 spin_unlock_irqrestore(&qp->q_lock, flags);
1698 return status;
1699}
1700
1701static u32 ocrdma_set_create_qp_mbx_access_flags(struct ocrdma_qp *qp)
1702{
1703 u32 flags = 0;
1704 if (qp->cap_flags & OCRDMA_QP_INB_RD)
1705 flags |= OCRDMA_CREATE_QP_REQ_INB_RDEN_MASK;
1706 if (qp->cap_flags & OCRDMA_QP_INB_WR)
1707 flags |= OCRDMA_CREATE_QP_REQ_INB_WREN_MASK;
1708 if (qp->cap_flags & OCRDMA_QP_MW_BIND)
1709 flags |= OCRDMA_CREATE_QP_REQ_BIND_MEMWIN_MASK;
1710 if (qp->cap_flags & OCRDMA_QP_LKEY0)
1711 flags |= OCRDMA_CREATE_QP_REQ_ZERO_LKEYEN_MASK;
1712 if (qp->cap_flags & OCRDMA_QP_FAST_REG)
1713 flags |= OCRDMA_CREATE_QP_REQ_FMR_EN_MASK;
1714 return flags;
1715}
1716
1717static int ocrdma_set_create_qp_sq_cmd(struct ocrdma_create_qp_req *cmd,
1718 struct ib_qp_init_attr *attrs,
1719 struct ocrdma_qp *qp)
1720{
1721 int status;
1722 u32 len, hw_pages, hw_page_size;
1723 dma_addr_t pa;
1724 struct ocrdma_dev *dev = qp->dev;
1725 struct pci_dev *pdev = dev->nic_info.pdev;
1726 u32 max_wqe_allocated;
1727 u32 max_sges = attrs->cap.max_send_sge;
1728
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301729 /* QP1 may exceed 127 */
1730 max_wqe_allocated = min_t(int, attrs->cap.max_send_wr + 1,
1731 dev->attr.max_wqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301732
1733 status = ocrdma_build_q_conf(&max_wqe_allocated,
1734 dev->attr.wqe_size, &hw_pages, &hw_page_size);
1735 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001736 pr_err("%s() req. max_send_wr=0x%x\n", __func__,
1737 max_wqe_allocated);
Parav Panditfe2caef2012-03-21 04:09:06 +05301738 return -EINVAL;
1739 }
1740 qp->sq.max_cnt = max_wqe_allocated;
1741 len = (hw_pages * hw_page_size);
1742
1743 qp->sq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
1744 if (!qp->sq.va)
1745 return -EINVAL;
1746 memset(qp->sq.va, 0, len);
1747 qp->sq.len = len;
1748 qp->sq.pa = pa;
1749 qp->sq.entry_size = dev->attr.wqe_size;
1750 ocrdma_build_q_pages(&cmd->wq_addr[0], hw_pages, pa, hw_page_size);
1751
1752 cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
1753 << OCRDMA_CREATE_QP_REQ_SQ_PAGE_SIZE_SHIFT);
1754 cmd->num_wq_rq_pages |= (hw_pages <<
1755 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_SHIFT) &
1756 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_MASK;
1757 cmd->max_sge_send_write |= (max_sges <<
1758 OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_SHIFT) &
1759 OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_MASK;
1760 cmd->max_sge_send_write |= (max_sges <<
1761 OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_SHIFT) &
1762 OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_MASK;
1763 cmd->max_wqe_rqe |= (ilog2(qp->sq.max_cnt) <<
1764 OCRDMA_CREATE_QP_REQ_MAX_WQE_SHIFT) &
1765 OCRDMA_CREATE_QP_REQ_MAX_WQE_MASK;
1766 cmd->wqe_rqe_size |= (dev->attr.wqe_size <<
1767 OCRDMA_CREATE_QP_REQ_WQE_SIZE_SHIFT) &
1768 OCRDMA_CREATE_QP_REQ_WQE_SIZE_MASK;
1769 return 0;
1770}
1771
1772static int ocrdma_set_create_qp_rq_cmd(struct ocrdma_create_qp_req *cmd,
1773 struct ib_qp_init_attr *attrs,
1774 struct ocrdma_qp *qp)
1775{
1776 int status;
1777 u32 len, hw_pages, hw_page_size;
1778 dma_addr_t pa = 0;
1779 struct ocrdma_dev *dev = qp->dev;
1780 struct pci_dev *pdev = dev->nic_info.pdev;
1781 u32 max_rqe_allocated = attrs->cap.max_recv_wr + 1;
1782
1783 status = ocrdma_build_q_conf(&max_rqe_allocated, dev->attr.rqe_size,
1784 &hw_pages, &hw_page_size);
1785 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001786 pr_err("%s() req. max_recv_wr=0x%x\n", __func__,
1787 attrs->cap.max_recv_wr + 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301788 return status;
1789 }
1790 qp->rq.max_cnt = max_rqe_allocated;
1791 len = (hw_pages * hw_page_size);
1792
1793 qp->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
1794 if (!qp->rq.va)
Wei Yongjunc94e15c2013-06-23 09:07:19 +08001795 return -ENOMEM;
Parav Panditfe2caef2012-03-21 04:09:06 +05301796 memset(qp->rq.va, 0, len);
1797 qp->rq.pa = pa;
1798 qp->rq.len = len;
1799 qp->rq.entry_size = dev->attr.rqe_size;
1800
1801 ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
1802 cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
1803 OCRDMA_CREATE_QP_REQ_RQ_PAGE_SIZE_SHIFT);
1804 cmd->num_wq_rq_pages |=
1805 (hw_pages << OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_SHIFT) &
1806 OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_MASK;
1807 cmd->max_sge_recv_flags |= (attrs->cap.max_recv_sge <<
1808 OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_SHIFT) &
1809 OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_MASK;
1810 cmd->max_wqe_rqe |= (ilog2(qp->rq.max_cnt) <<
1811 OCRDMA_CREATE_QP_REQ_MAX_RQE_SHIFT) &
1812 OCRDMA_CREATE_QP_REQ_MAX_RQE_MASK;
1813 cmd->wqe_rqe_size |= (dev->attr.rqe_size <<
1814 OCRDMA_CREATE_QP_REQ_RQE_SIZE_SHIFT) &
1815 OCRDMA_CREATE_QP_REQ_RQE_SIZE_MASK;
1816 return 0;
1817}
1818
1819static void ocrdma_set_create_qp_dpp_cmd(struct ocrdma_create_qp_req *cmd,
1820 struct ocrdma_pd *pd,
1821 struct ocrdma_qp *qp,
1822 u8 enable_dpp_cq, u16 dpp_cq_id)
1823{
1824 pd->num_dpp_qp--;
1825 qp->dpp_enabled = true;
1826 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
1827 if (!enable_dpp_cq)
1828 return;
1829 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
1830 cmd->dpp_credits_cqid = dpp_cq_id;
1831 cmd->dpp_credits_cqid |= OCRDMA_CREATE_QP_REQ_DPP_CREDIT_LIMIT <<
1832 OCRDMA_CREATE_QP_REQ_DPP_CREDIT_SHIFT;
1833}
1834
1835static int ocrdma_set_create_qp_ird_cmd(struct ocrdma_create_qp_req *cmd,
1836 struct ocrdma_qp *qp)
1837{
1838 struct ocrdma_dev *dev = qp->dev;
1839 struct pci_dev *pdev = dev->nic_info.pdev;
1840 dma_addr_t pa = 0;
1841 int ird_page_size = dev->attr.ird_page_size;
1842 int ird_q_len = dev->attr.num_ird_pages * ird_page_size;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301843 struct ocrdma_hdr_wqe *rqe;
1844 int i = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301845
1846 if (dev->attr.ird == 0)
1847 return 0;
1848
1849 qp->ird_q_va = dma_alloc_coherent(&pdev->dev, ird_q_len,
1850 &pa, GFP_KERNEL);
1851 if (!qp->ird_q_va)
1852 return -ENOMEM;
1853 memset(qp->ird_q_va, 0, ird_q_len);
1854 ocrdma_build_q_pages(&cmd->ird_addr[0], dev->attr.num_ird_pages,
1855 pa, ird_page_size);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301856 for (; i < ird_q_len / dev->attr.rqe_size; i++) {
1857 rqe = (struct ocrdma_hdr_wqe *)(qp->ird_q_va +
1858 (i * dev->attr.rqe_size));
1859 rqe->cw = 0;
1860 rqe->cw |= 2;
1861 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1862 rqe->cw |= (8 << OCRDMA_WQE_SIZE_SHIFT);
1863 rqe->cw |= (8 << OCRDMA_WQE_NXT_WQE_SIZE_SHIFT);
1864 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301865 return 0;
1866}
1867
1868static void ocrdma_get_create_qp_rsp(struct ocrdma_create_qp_rsp *rsp,
1869 struct ocrdma_qp *qp,
1870 struct ib_qp_init_attr *attrs,
1871 u16 *dpp_offset, u16 *dpp_credit_lmt)
1872{
1873 u32 max_wqe_allocated, max_rqe_allocated;
1874 qp->id = rsp->qp_id & OCRDMA_CREATE_QP_RSP_QP_ID_MASK;
1875 qp->rq.dbid = rsp->sq_rq_id & OCRDMA_CREATE_QP_RSP_RQ_ID_MASK;
1876 qp->sq.dbid = rsp->sq_rq_id >> OCRDMA_CREATE_QP_RSP_SQ_ID_SHIFT;
1877 qp->max_ird = rsp->max_ord_ird & OCRDMA_CREATE_QP_RSP_MAX_IRD_MASK;
1878 qp->max_ord = (rsp->max_ord_ird >> OCRDMA_CREATE_QP_RSP_MAX_ORD_SHIFT);
1879 qp->dpp_enabled = false;
1880 if (rsp->dpp_response & OCRDMA_CREATE_QP_RSP_DPP_ENABLED_MASK) {
1881 qp->dpp_enabled = true;
1882 *dpp_credit_lmt = (rsp->dpp_response &
1883 OCRDMA_CREATE_QP_RSP_DPP_CREDITS_MASK) >>
1884 OCRDMA_CREATE_QP_RSP_DPP_CREDITS_SHIFT;
1885 *dpp_offset = (rsp->dpp_response &
1886 OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_MASK) >>
1887 OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_SHIFT;
1888 }
1889 max_wqe_allocated =
1890 rsp->max_wqe_rqe >> OCRDMA_CREATE_QP_RSP_MAX_WQE_SHIFT;
1891 max_wqe_allocated = 1 << max_wqe_allocated;
1892 max_rqe_allocated = 1 << ((u16)rsp->max_wqe_rqe);
1893
Parav Panditfe2caef2012-03-21 04:09:06 +05301894 qp->sq.max_cnt = max_wqe_allocated;
1895 qp->sq.max_wqe_idx = max_wqe_allocated - 1;
1896
1897 if (!attrs->srq) {
1898 qp->rq.max_cnt = max_rqe_allocated;
1899 qp->rq.max_wqe_idx = max_rqe_allocated - 1;
Parav Panditfe2caef2012-03-21 04:09:06 +05301900 }
1901}
1902
1903int ocrdma_mbx_create_qp(struct ocrdma_qp *qp, struct ib_qp_init_attr *attrs,
1904 u8 enable_dpp_cq, u16 dpp_cq_id, u16 *dpp_offset,
1905 u16 *dpp_credit_lmt)
1906{
1907 int status = -ENOMEM;
1908 u32 flags = 0;
1909 struct ocrdma_dev *dev = qp->dev;
1910 struct ocrdma_pd *pd = qp->pd;
1911 struct pci_dev *pdev = dev->nic_info.pdev;
1912 struct ocrdma_cq *cq;
1913 struct ocrdma_create_qp_req *cmd;
1914 struct ocrdma_create_qp_rsp *rsp;
1915 int qptype;
1916
1917 switch (attrs->qp_type) {
1918 case IB_QPT_GSI:
1919 qptype = OCRDMA_QPT_GSI;
1920 break;
1921 case IB_QPT_RC:
1922 qptype = OCRDMA_QPT_RC;
1923 break;
1924 case IB_QPT_UD:
1925 qptype = OCRDMA_QPT_UD;
1926 break;
1927 default:
1928 return -EINVAL;
1929 };
1930
1931 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_QP, sizeof(*cmd));
1932 if (!cmd)
1933 return status;
1934 cmd->type_pgsz_pdn |= (qptype << OCRDMA_CREATE_QP_REQ_QPT_SHIFT) &
1935 OCRDMA_CREATE_QP_REQ_QPT_MASK;
1936 status = ocrdma_set_create_qp_sq_cmd(cmd, attrs, qp);
1937 if (status)
1938 goto sq_err;
1939
1940 if (attrs->srq) {
1941 struct ocrdma_srq *srq = get_ocrdma_srq(attrs->srq);
1942 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_USE_SRQ_MASK;
1943 cmd->rq_addr[0].lo = srq->id;
1944 qp->srq = srq;
1945 } else {
1946 status = ocrdma_set_create_qp_rq_cmd(cmd, attrs, qp);
1947 if (status)
1948 goto rq_err;
1949 }
1950
1951 status = ocrdma_set_create_qp_ird_cmd(cmd, qp);
1952 if (status)
1953 goto mbx_err;
1954
1955 cmd->type_pgsz_pdn |= (pd->id << OCRDMA_CREATE_QP_REQ_PD_ID_SHIFT) &
1956 OCRDMA_CREATE_QP_REQ_PD_ID_MASK;
1957
1958 flags = ocrdma_set_create_qp_mbx_access_flags(qp);
1959
1960 cmd->max_sge_recv_flags |= flags;
1961 cmd->max_ord_ird |= (dev->attr.max_ord_per_qp <<
1962 OCRDMA_CREATE_QP_REQ_MAX_ORD_SHIFT) &
1963 OCRDMA_CREATE_QP_REQ_MAX_ORD_MASK;
1964 cmd->max_ord_ird |= (dev->attr.max_ird_per_qp <<
1965 OCRDMA_CREATE_QP_REQ_MAX_IRD_SHIFT) &
1966 OCRDMA_CREATE_QP_REQ_MAX_IRD_MASK;
1967 cq = get_ocrdma_cq(attrs->send_cq);
1968 cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_WQ_CQID_SHIFT) &
1969 OCRDMA_CREATE_QP_REQ_WQ_CQID_MASK;
1970 qp->sq_cq = cq;
1971 cq = get_ocrdma_cq(attrs->recv_cq);
1972 cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_RQ_CQID_SHIFT) &
1973 OCRDMA_CREATE_QP_REQ_RQ_CQID_MASK;
1974 qp->rq_cq = cq;
1975
1976 if (pd->dpp_enabled && attrs->cap.max_inline_data && pd->num_dpp_qp &&
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301977 (attrs->cap.max_inline_data <= dev->attr.max_inline_data)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301978 ocrdma_set_create_qp_dpp_cmd(cmd, pd, qp, enable_dpp_cq,
1979 dpp_cq_id);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301980 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301981
1982 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1983 if (status)
1984 goto mbx_err;
1985 rsp = (struct ocrdma_create_qp_rsp *)cmd;
1986 ocrdma_get_create_qp_rsp(rsp, qp, attrs, dpp_offset, dpp_credit_lmt);
1987 qp->state = OCRDMA_QPS_RST;
1988 kfree(cmd);
1989 return 0;
1990mbx_err:
1991 if (qp->rq.va)
1992 dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
1993rq_err:
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001994 pr_err("%s(%d) rq_err\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301995 dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
1996sq_err:
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001997 pr_err("%s(%d) sq_err\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301998 kfree(cmd);
1999 return status;
2000}
2001
2002int ocrdma_mbx_query_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2003 struct ocrdma_qp_params *param)
2004{
2005 int status = -ENOMEM;
2006 struct ocrdma_query_qp *cmd;
2007 struct ocrdma_query_qp_rsp *rsp;
2008
2009 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_QP, sizeof(*cmd));
2010 if (!cmd)
2011 return status;
2012 cmd->qp_id = qp->id;
2013 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2014 if (status)
2015 goto mbx_err;
2016 rsp = (struct ocrdma_query_qp_rsp *)cmd;
2017 memcpy(param, &rsp->params, sizeof(struct ocrdma_qp_params));
2018mbx_err:
2019 kfree(cmd);
2020 return status;
2021}
2022
2023int ocrdma_resolve_dgid(struct ocrdma_dev *dev, union ib_gid *dgid,
2024 u8 *mac_addr)
2025{
2026 struct in6_addr in6;
2027
2028 memcpy(&in6, dgid, sizeof in6);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302029 if (rdma_is_multicast_addr(&in6)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302030 rdma_get_mcast_mac(&in6, mac_addr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302031 } else if (rdma_link_local_addr(&in6)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302032 rdma_get_ll_mac(&in6, mac_addr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302033 } else {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002034 pr_err("%s() fail to resolve mac_addr.\n", __func__);
Parav Panditfe2caef2012-03-21 04:09:06 +05302035 return -EINVAL;
2036 }
2037 return 0;
2038}
2039
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302040static int ocrdma_set_av_params(struct ocrdma_qp *qp,
Parav Panditfe2caef2012-03-21 04:09:06 +05302041 struct ocrdma_modify_qp *cmd,
2042 struct ib_qp_attr *attrs)
2043{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302044 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +05302045 struct ib_ah_attr *ah_attr = &attrs->ah_attr;
Naresh Gottumukkala9c587262013-08-07 12:52:34 +05302046 union ib_gid sgid, zgid;
Parav Panditfe2caef2012-03-21 04:09:06 +05302047 u32 vlan_id;
2048 u8 mac_addr[6];
Naresh Gottumukkala9c587262013-08-07 12:52:34 +05302049
Parav Panditfe2caef2012-03-21 04:09:06 +05302050 if ((ah_attr->ah_flags & IB_AH_GRH) == 0)
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302051 return -EINVAL;
Parav Panditfe2caef2012-03-21 04:09:06 +05302052 cmd->params.tclass_sq_psn |=
2053 (ah_attr->grh.traffic_class << OCRDMA_QP_PARAMS_TCLASS_SHIFT);
2054 cmd->params.rnt_rc_sl_fl |=
2055 (ah_attr->grh.flow_label & OCRDMA_QP_PARAMS_FLOW_LABEL_MASK);
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302056 cmd->params.rnt_rc_sl_fl |= (ah_attr->sl << OCRDMA_QP_PARAMS_SL_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302057 cmd->params.hop_lmt_rq_psn |=
2058 (ah_attr->grh.hop_limit << OCRDMA_QP_PARAMS_HOP_LMT_SHIFT);
2059 cmd->flags |= OCRDMA_QP_PARA_FLOW_LBL_VALID;
2060 memcpy(&cmd->params.dgid[0], &ah_attr->grh.dgid.raw[0],
2061 sizeof(cmd->params.dgid));
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302062 status = ocrdma_query_gid(&qp->dev->ibdev, 1,
Parav Panditfe2caef2012-03-21 04:09:06 +05302063 ah_attr->grh.sgid_index, &sgid);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302064 if (status)
2065 return status;
Naresh Gottumukkala9c587262013-08-07 12:52:34 +05302066
2067 memset(&zgid, 0, sizeof(zgid));
2068 if (!memcmp(&sgid, &zgid, sizeof(zgid)))
2069 return -EINVAL;
2070
Parav Panditfe2caef2012-03-21 04:09:06 +05302071 qp->sgid_idx = ah_attr->grh.sgid_index;
2072 memcpy(&cmd->params.sgid[0], &sgid.raw[0], sizeof(cmd->params.sgid));
2073 ocrdma_resolve_dgid(qp->dev, &ah_attr->grh.dgid, &mac_addr[0]);
2074 cmd->params.dmac_b0_to_b3 = mac_addr[0] | (mac_addr[1] << 8) |
2075 (mac_addr[2] << 16) | (mac_addr[3] << 24);
2076 /* convert them to LE format. */
2077 ocrdma_cpu_to_le32(&cmd->params.dgid[0], sizeof(cmd->params.dgid));
2078 ocrdma_cpu_to_le32(&cmd->params.sgid[0], sizeof(cmd->params.sgid));
2079 cmd->params.vlan_dmac_b4_to_b5 = mac_addr[4] | (mac_addr[5] << 8);
2080 vlan_id = rdma_get_vlan_id(&sgid);
2081 if (vlan_id && (vlan_id < 0x1000)) {
2082 cmd->params.vlan_dmac_b4_to_b5 |=
2083 vlan_id << OCRDMA_QP_PARAMS_VLAN_SHIFT;
2084 cmd->flags |= OCRDMA_QP_PARA_VLAN_EN_VALID;
2085 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302086 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302087}
2088
2089static int ocrdma_set_qp_params(struct ocrdma_qp *qp,
2090 struct ocrdma_modify_qp *cmd,
2091 struct ib_qp_attr *attrs, int attr_mask,
2092 enum ib_qp_state old_qps)
2093{
2094 int status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302095
2096 if (attr_mask & IB_QP_PKEY_INDEX) {
2097 cmd->params.path_mtu_pkey_indx |= (attrs->pkey_index &
2098 OCRDMA_QP_PARAMS_PKEY_INDEX_MASK);
2099 cmd->flags |= OCRDMA_QP_PARA_PKEY_VALID;
2100 }
2101 if (attr_mask & IB_QP_QKEY) {
2102 qp->qkey = attrs->qkey;
2103 cmd->params.qkey = attrs->qkey;
2104 cmd->flags |= OCRDMA_QP_PARA_QKEY_VALID;
2105 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302106 if (attr_mask & IB_QP_AV) {
2107 status = ocrdma_set_av_params(qp, cmd, attrs);
2108 if (status)
2109 return status;
2110 } else if (qp->qp_type == IB_QPT_GSI || qp->qp_type == IB_QPT_UD) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302111 /* set the default mac address for UD, GSI QPs */
2112 cmd->params.dmac_b0_to_b3 = qp->dev->nic_info.mac_addr[0] |
2113 (qp->dev->nic_info.mac_addr[1] << 8) |
2114 (qp->dev->nic_info.mac_addr[2] << 16) |
2115 (qp->dev->nic_info.mac_addr[3] << 24);
2116 cmd->params.vlan_dmac_b4_to_b5 = qp->dev->nic_info.mac_addr[4] |
2117 (qp->dev->nic_info.mac_addr[5] << 8);
2118 }
2119 if ((attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) &&
2120 attrs->en_sqd_async_notify) {
2121 cmd->params.max_sge_recv_flags |=
2122 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC;
2123 cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2124 }
2125 if (attr_mask & IB_QP_DEST_QPN) {
2126 cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->dest_qp_num &
2127 OCRDMA_QP_PARAMS_DEST_QPN_MASK);
2128 cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2129 }
2130 if (attr_mask & IB_QP_PATH_MTU) {
Naresh Gottumukkalad3cb6c02013-08-26 15:27:40 +05302131 if (attrs->path_mtu < IB_MTU_256 ||
2132 attrs->path_mtu > IB_MTU_4096) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302133 status = -EINVAL;
2134 goto pmtu_err;
2135 }
2136 cmd->params.path_mtu_pkey_indx |=
2137 (ib_mtu_enum_to_int(attrs->path_mtu) <<
2138 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT) &
2139 OCRDMA_QP_PARAMS_PATH_MTU_MASK;
2140 cmd->flags |= OCRDMA_QP_PARA_PMTU_VALID;
2141 }
2142 if (attr_mask & IB_QP_TIMEOUT) {
2143 cmd->params.ack_to_rnr_rtc_dest_qpn |= attrs->timeout <<
2144 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
2145 cmd->flags |= OCRDMA_QP_PARA_ACK_TO_VALID;
2146 }
2147 if (attr_mask & IB_QP_RETRY_CNT) {
2148 cmd->params.rnt_rc_sl_fl |= (attrs->retry_cnt <<
2149 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT) &
2150 OCRDMA_QP_PARAMS_RETRY_CNT_MASK;
2151 cmd->flags |= OCRDMA_QP_PARA_RETRY_CNT_VALID;
2152 }
2153 if (attr_mask & IB_QP_MIN_RNR_TIMER) {
2154 cmd->params.rnt_rc_sl_fl |= (attrs->min_rnr_timer <<
2155 OCRDMA_QP_PARAMS_RNR_NAK_TIMER_SHIFT) &
2156 OCRDMA_QP_PARAMS_RNR_NAK_TIMER_MASK;
2157 cmd->flags |= OCRDMA_QP_PARA_RNT_VALID;
2158 }
2159 if (attr_mask & IB_QP_RNR_RETRY) {
2160 cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->rnr_retry <<
2161 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT)
2162 & OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK;
2163 cmd->flags |= OCRDMA_QP_PARA_RRC_VALID;
2164 }
2165 if (attr_mask & IB_QP_SQ_PSN) {
2166 cmd->params.tclass_sq_psn |= (attrs->sq_psn & 0x00ffffff);
2167 cmd->flags |= OCRDMA_QP_PARA_SQPSN_VALID;
2168 }
2169 if (attr_mask & IB_QP_RQ_PSN) {
2170 cmd->params.hop_lmt_rq_psn |= (attrs->rq_psn & 0x00ffffff);
2171 cmd->flags |= OCRDMA_QP_PARA_RQPSN_VALID;
2172 }
2173 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2174 if (attrs->max_rd_atomic > qp->dev->attr.max_ord_per_qp) {
2175 status = -EINVAL;
2176 goto pmtu_err;
2177 }
2178 qp->max_ord = attrs->max_rd_atomic;
2179 cmd->flags |= OCRDMA_QP_PARA_MAX_ORD_VALID;
2180 }
2181 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2182 if (attrs->max_dest_rd_atomic > qp->dev->attr.max_ird_per_qp) {
2183 status = -EINVAL;
2184 goto pmtu_err;
2185 }
2186 qp->max_ird = attrs->max_dest_rd_atomic;
2187 cmd->flags |= OCRDMA_QP_PARA_MAX_IRD_VALID;
2188 }
2189 cmd->params.max_ord_ird = (qp->max_ord <<
2190 OCRDMA_QP_PARAMS_MAX_ORD_SHIFT) |
2191 (qp->max_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK);
2192pmtu_err:
2193 return status;
2194}
2195
2196int ocrdma_mbx_modify_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2197 struct ib_qp_attr *attrs, int attr_mask,
2198 enum ib_qp_state old_qps)
2199{
2200 int status = -ENOMEM;
2201 struct ocrdma_modify_qp *cmd;
Parav Panditfe2caef2012-03-21 04:09:06 +05302202
2203 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_QP, sizeof(*cmd));
2204 if (!cmd)
2205 return status;
2206
2207 cmd->params.id = qp->id;
2208 cmd->flags = 0;
2209 if (attr_mask & IB_QP_STATE) {
2210 cmd->params.max_sge_recv_flags |=
2211 (get_ocrdma_qp_state(attrs->qp_state) <<
2212 OCRDMA_QP_PARAMS_STATE_SHIFT) &
2213 OCRDMA_QP_PARAMS_STATE_MASK;
2214 cmd->flags |= OCRDMA_QP_PARA_QPS_VALID;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302215 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302216 cmd->params.max_sge_recv_flags |=
2217 (qp->state << OCRDMA_QP_PARAMS_STATE_SHIFT) &
2218 OCRDMA_QP_PARAMS_STATE_MASK;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302219 }
2220
Parav Panditfe2caef2012-03-21 04:09:06 +05302221 status = ocrdma_set_qp_params(qp, cmd, attrs, attr_mask, old_qps);
2222 if (status)
2223 goto mbx_err;
2224 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2225 if (status)
2226 goto mbx_err;
Roland Dreierc592c422012-04-17 01:18:28 -07002227
Parav Panditfe2caef2012-03-21 04:09:06 +05302228mbx_err:
2229 kfree(cmd);
2230 return status;
2231}
2232
2233int ocrdma_mbx_destroy_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
2234{
2235 int status = -ENOMEM;
2236 struct ocrdma_destroy_qp *cmd;
Parav Panditfe2caef2012-03-21 04:09:06 +05302237 struct pci_dev *pdev = dev->nic_info.pdev;
2238
2239 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_QP, sizeof(*cmd));
2240 if (!cmd)
2241 return status;
2242 cmd->qp_id = qp->id;
2243 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2244 if (status)
2245 goto mbx_err;
Roland Dreierc592c422012-04-17 01:18:28 -07002246
Parav Panditfe2caef2012-03-21 04:09:06 +05302247mbx_err:
2248 kfree(cmd);
2249 if (qp->sq.va)
2250 dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2251 if (!qp->srq && qp->rq.va)
2252 dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2253 if (qp->dpp_enabled)
2254 qp->pd->num_dpp_qp++;
2255 return status;
2256}
2257
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302258int ocrdma_mbx_create_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
Parav Panditfe2caef2012-03-21 04:09:06 +05302259 struct ib_srq_init_attr *srq_attr,
2260 struct ocrdma_pd *pd)
2261{
2262 int status = -ENOMEM;
2263 int hw_pages, hw_page_size;
2264 int len;
2265 struct ocrdma_create_srq_rsp *rsp;
2266 struct ocrdma_create_srq *cmd;
2267 dma_addr_t pa;
Parav Panditfe2caef2012-03-21 04:09:06 +05302268 struct pci_dev *pdev = dev->nic_info.pdev;
2269 u32 max_rqe_allocated;
2270
2271 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2272 if (!cmd)
2273 return status;
2274
2275 cmd->pgsz_pdid = pd->id & OCRDMA_CREATE_SRQ_PD_ID_MASK;
2276 max_rqe_allocated = srq_attr->attr.max_wr + 1;
2277 status = ocrdma_build_q_conf(&max_rqe_allocated,
2278 dev->attr.rqe_size,
2279 &hw_pages, &hw_page_size);
2280 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002281 pr_err("%s() req. max_wr=0x%x\n", __func__,
2282 srq_attr->attr.max_wr);
Parav Panditfe2caef2012-03-21 04:09:06 +05302283 status = -EINVAL;
2284 goto ret;
2285 }
2286 len = hw_pages * hw_page_size;
2287 srq->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2288 if (!srq->rq.va) {
2289 status = -ENOMEM;
2290 goto ret;
2291 }
2292 ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
2293
2294 srq->rq.entry_size = dev->attr.rqe_size;
2295 srq->rq.pa = pa;
2296 srq->rq.len = len;
2297 srq->rq.max_cnt = max_rqe_allocated;
2298
2299 cmd->max_sge_rqe = ilog2(max_rqe_allocated);
2300 cmd->max_sge_rqe |= srq_attr->attr.max_sge <<
2301 OCRDMA_CREATE_SRQ_MAX_SGE_RECV_SHIFT;
2302
2303 cmd->pgsz_pdid |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
2304 << OCRDMA_CREATE_SRQ_PG_SZ_SHIFT);
2305 cmd->pages_rqe_sz |= (dev->attr.rqe_size
2306 << OCRDMA_CREATE_SRQ_RQE_SIZE_SHIFT)
2307 & OCRDMA_CREATE_SRQ_RQE_SIZE_MASK;
2308 cmd->pages_rqe_sz |= hw_pages << OCRDMA_CREATE_SRQ_NUM_RQ_PAGES_SHIFT;
2309
2310 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2311 if (status)
2312 goto mbx_err;
2313 rsp = (struct ocrdma_create_srq_rsp *)cmd;
2314 srq->id = rsp->id;
2315 srq->rq.dbid = rsp->id;
2316 max_rqe_allocated = ((rsp->max_sge_rqe_allocated &
2317 OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_MASK) >>
2318 OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_SHIFT);
2319 max_rqe_allocated = (1 << max_rqe_allocated);
2320 srq->rq.max_cnt = max_rqe_allocated;
2321 srq->rq.max_wqe_idx = max_rqe_allocated - 1;
2322 srq->rq.max_sges = (rsp->max_sge_rqe_allocated &
2323 OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_MASK) >>
2324 OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_SHIFT;
2325 goto ret;
2326mbx_err:
2327 dma_free_coherent(&pdev->dev, srq->rq.len, srq->rq.va, pa);
2328ret:
2329 kfree(cmd);
2330 return status;
2331}
2332
2333int ocrdma_mbx_modify_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2334{
2335 int status = -ENOMEM;
2336 struct ocrdma_modify_srq *cmd;
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05302337 struct ocrdma_pd *pd = srq->pd;
2338 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302339
Parav Panditfe2caef2012-03-21 04:09:06 +05302340 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2341 if (!cmd)
2342 return status;
2343 cmd->id = srq->id;
2344 cmd->limit_max_rqe |= srq_attr->srq_limit <<
2345 OCRDMA_MODIFY_SRQ_LIMIT_SHIFT;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302346 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
Parav Panditfe2caef2012-03-21 04:09:06 +05302347 kfree(cmd);
2348 return status;
2349}
2350
2351int ocrdma_mbx_query_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2352{
2353 int status = -ENOMEM;
2354 struct ocrdma_query_srq *cmd;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302355 struct ocrdma_dev *dev = get_ocrdma_dev(srq->ibsrq.device);
2356
Parav Panditfe2caef2012-03-21 04:09:06 +05302357 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2358 if (!cmd)
2359 return status;
2360 cmd->id = srq->rq.dbid;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302361 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
Parav Panditfe2caef2012-03-21 04:09:06 +05302362 if (status == 0) {
2363 struct ocrdma_query_srq_rsp *rsp =
2364 (struct ocrdma_query_srq_rsp *)cmd;
2365 srq_attr->max_sge =
2366 rsp->srq_lmt_max_sge &
2367 OCRDMA_QUERY_SRQ_RSP_MAX_SGE_RECV_MASK;
2368 srq_attr->max_wr =
2369 rsp->max_rqe_pdid >> OCRDMA_QUERY_SRQ_RSP_MAX_RQE_SHIFT;
2370 srq_attr->srq_limit = rsp->srq_lmt_max_sge >>
2371 OCRDMA_QUERY_SRQ_RSP_SRQ_LIMIT_SHIFT;
2372 }
2373 kfree(cmd);
2374 return status;
2375}
2376
2377int ocrdma_mbx_destroy_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq)
2378{
2379 int status = -ENOMEM;
2380 struct ocrdma_destroy_srq *cmd;
2381 struct pci_dev *pdev = dev->nic_info.pdev;
2382 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_SRQ, sizeof(*cmd));
2383 if (!cmd)
2384 return status;
2385 cmd->id = srq->id;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302386 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
Parav Panditfe2caef2012-03-21 04:09:06 +05302387 if (srq->rq.va)
2388 dma_free_coherent(&pdev->dev, srq->rq.len,
2389 srq->rq.va, srq->rq.pa);
2390 kfree(cmd);
2391 return status;
2392}
2393
2394int ocrdma_alloc_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
2395{
2396 int i;
2397 int status = -EINVAL;
2398 struct ocrdma_av *av;
2399 unsigned long flags;
2400
2401 av = dev->av_tbl.va;
2402 spin_lock_irqsave(&dev->av_tbl.lock, flags);
2403 for (i = 0; i < dev->av_tbl.num_ah; i++) {
2404 if (av->valid == 0) {
2405 av->valid = OCRDMA_AV_VALID;
2406 ah->av = av;
2407 ah->id = i;
2408 status = 0;
2409 break;
2410 }
2411 av++;
2412 }
2413 if (i == dev->av_tbl.num_ah)
2414 status = -EAGAIN;
2415 spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
2416 return status;
2417}
2418
2419int ocrdma_free_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
2420{
2421 unsigned long flags;
2422 spin_lock_irqsave(&dev->av_tbl.lock, flags);
2423 ah->av->valid = 0;
2424 spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
2425 return 0;
2426}
2427
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302428static int ocrdma_create_eqs(struct ocrdma_dev *dev)
Parav Panditfe2caef2012-03-21 04:09:06 +05302429{
Roland Dreierda496432012-04-16 11:32:17 -07002430 int num_eq, i, status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302431 int irq;
2432 unsigned long flags = 0;
2433
2434 num_eq = dev->nic_info.msix.num_vectors -
2435 dev->nic_info.msix.start_vector;
2436 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX) {
2437 num_eq = 1;
2438 flags = IRQF_SHARED;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302439 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302440 num_eq = min_t(u32, num_eq, num_online_cpus());
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302441 }
2442
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302443 if (!num_eq)
2444 return -EINVAL;
2445
2446 dev->eq_tbl = kzalloc(sizeof(struct ocrdma_eq) * num_eq, GFP_KERNEL);
2447 if (!dev->eq_tbl)
Parav Panditfe2caef2012-03-21 04:09:06 +05302448 return -ENOMEM;
2449
2450 for (i = 0; i < num_eq; i++) {
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302451 status = ocrdma_create_eq(dev, &dev->eq_tbl[i],
Parav Panditfe2caef2012-03-21 04:09:06 +05302452 OCRDMA_EQ_LEN);
2453 if (status) {
2454 status = -EINVAL;
2455 break;
2456 }
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302457 sprintf(dev->eq_tbl[i].irq_name, "ocrdma%d-%d",
Parav Panditfe2caef2012-03-21 04:09:06 +05302458 dev->id, i);
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302459 irq = ocrdma_get_irq(dev, &dev->eq_tbl[i]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302460 status = request_irq(irq, ocrdma_irq_handler, flags,
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302461 dev->eq_tbl[i].irq_name,
2462 &dev->eq_tbl[i]);
2463 if (status)
2464 goto done;
Parav Panditfe2caef2012-03-21 04:09:06 +05302465 dev->eq_cnt += 1;
2466 }
2467 /* one eq is sufficient for data path to work */
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302468 return 0;
2469done:
2470 ocrdma_destroy_eqs(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +05302471 return status;
2472}
2473
2474int ocrdma_init_hw(struct ocrdma_dev *dev)
2475{
2476 int status;
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302477
2478 /* create the eqs */
2479 status = ocrdma_create_eqs(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +05302480 if (status)
2481 goto qpeq_err;
2482 status = ocrdma_create_mq(dev);
2483 if (status)
2484 goto mq_err;
2485 status = ocrdma_mbx_query_fw_config(dev);
2486 if (status)
2487 goto conf_err;
2488 status = ocrdma_mbx_query_dev(dev);
2489 if (status)
2490 goto conf_err;
2491 status = ocrdma_mbx_query_fw_ver(dev);
2492 if (status)
2493 goto conf_err;
2494 status = ocrdma_mbx_create_ah_tbl(dev);
2495 if (status)
2496 goto conf_err;
2497 return 0;
2498
2499conf_err:
2500 ocrdma_destroy_mq(dev);
2501mq_err:
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302502 ocrdma_destroy_eqs(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +05302503qpeq_err:
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002504 pr_err("%s() status=%d\n", __func__, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05302505 return status;
2506}
2507
2508void ocrdma_cleanup_hw(struct ocrdma_dev *dev)
2509{
2510 ocrdma_mbx_delete_ah_tbl(dev);
2511
Naresh Gottumukkalac88bd032013-08-26 15:27:41 +05302512 /* cleanup the eqs */
2513 ocrdma_destroy_eqs(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +05302514
2515 /* cleanup the control path */
2516 ocrdma_destroy_mq(dev);
Parav Panditfe2caef2012-03-21 04:09:06 +05302517}