1/*
2 * This file is part of the Chelsio T6 Ethernet driver for Linux.
3 *
4 * Copyright (c) 2017-2018 Chelsio Communications, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#include "cxgb4.h"
36#include "t4_msg.h"
37#include "srq.h"
38
39struct srq_data *t4_init_srq(int srq_size)
40{
41	struct srq_data *s;
42
43	s = kvzalloc(sizeof(*s), GFP_KERNEL);
44	if (!s)
45		return NULL;
46
47	s->srq_size = srq_size;
48	init_completion(&s->comp);
49	mutex_init(&s->lock);
50
51	return s;
52}
53
54/* cxgb4_get_srq_entry: read the SRQ table entry
55 * @dev: Pointer to the net_device
56 * @idx: Index to the srq
57 * @entryp: pointer to the srq entry
58 *
59 * Sends CPL_SRQ_TABLE_REQ message for the given index.
60 * Contents will be returned in CPL_SRQ_TABLE_RPL message.
61 *
62 * Returns zero if the read is successful, else a error
63 * number will be returned. Caller should not use the srq
64 * entry if the return value is non-zero.
65 *
66 *
67 */
68int cxgb4_get_srq_entry(struct net_device *dev,
69			int srq_idx, struct srq_entry *entryp)
70{
71	struct cpl_srq_table_req *req;
72	struct adapter *adap;
73	struct sk_buff *skb;
74	struct srq_data *s;
75	int rc = -ENODEV;
76
77	adap = netdev2adap(dev);
78	s = adap->srq;
79
80	if (!(adap->flags & CXGB4_FULL_INIT_DONE) || !s)
81		goto out;
82
83	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
84	if (!skb)
85		return -ENOMEM;
86	req = (struct cpl_srq_table_req *)
87		__skb_put_zero(skb, sizeof(*req));
88	INIT_TP_WR(req, 0);
89	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SRQ_TABLE_REQ,
90					      TID_TID_V(srq_idx) |
91				TID_QID_V(adap->sge.fw_evtq.abs_id)));
92	req->idx = srq_idx;
93
94	mutex_lock(&s->lock);
95
96	s->entryp = entryp;
97	t4_mgmt_tx(adap, skb);
98
99	rc = wait_for_completion_timeout(&s->comp, SRQ_WAIT_TO);
100	if (rc)
101		rc = 0;
102	else /* !rc means we timed out */
103		rc = -ETIMEDOUT;
104
105	WARN_ON_ONCE(entryp->idx != srq_idx);
106	mutex_unlock(&s->lock);
107out:
108	return rc;
109}
110EXPORT_SYMBOL(cxgb4_get_srq_entry);
111
112void do_srq_table_rpl(struct adapter *adap,
113		      const struct cpl_srq_table_rpl *rpl)
114{
115	unsigned int idx = TID_TID_G(GET_TID(rpl));
116	struct srq_data *s = adap->srq;
117	struct srq_entry *e;
118
119	if (unlikely(rpl->status != CPL_CONTAINS_READ_RPL)) {
120		dev_err(adap->pdev_dev,
121			"Unexpected SRQ_TABLE_RPL status %u for entry %u\n",
122				rpl->status, idx);
123		goto out;
124	}
125
126	/* Store the read entry */
127	e = s->entryp;
128	e->valid = 1;
129	e->idx = idx;
130	e->pdid = SRQT_PDID_G(be64_to_cpu(rpl->rsvd_pdid));
131	e->qlen = SRQT_QLEN_G(be32_to_cpu(rpl->qlen_qbase));
132	e->qbase = SRQT_QBASE_G(be32_to_cpu(rpl->qlen_qbase));
133	e->cur_msn = be16_to_cpu(rpl->cur_msn);
134	e->max_msn = be16_to_cpu(rpl->max_msn);
135out:
136	complete(&s->comp);
137}
138