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