1 /*
2  * Copyright (c) 2016 Hisilicon Limited.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/dmapool.h>
34 #include "hns_roce_common.h"
35 #include "hns_roce_device.h"
36 #include "hns_roce_cmd.h"
37 
38 #define CMD_POLL_TOKEN 0xffff
39 #define CMD_MAX_NUM 32
40 
41 static int hns_roce_cmd_mbox_post_hw(struct hns_roce_dev *hr_dev, u64 in_param,
42 				     u64 out_param, u32 in_modifier,
43 				     u8 op_modifier, u16 op, u16 token,
44 				     int event)
45 {
46 	return hr_dev->hw->post_mbox(hr_dev, in_param, out_param, in_modifier,
47 				     op_modifier, op, token, event);
48 }
49 
50 /* this should be called with "poll_sem" */
51 static int __hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
52 				    u64 out_param, unsigned long in_modifier,
53 				    u8 op_modifier, u16 op,
54 				    unsigned int timeout)
55 {
56 	int ret;
57 
58 	ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
59 					in_modifier, op_modifier, op,
60 					CMD_POLL_TOKEN, 0);
61 	if (ret) {
62 		dev_err_ratelimited(hr_dev->dev,
63 				    "failed to post mailbox 0x%x in poll mode, ret = %d.\n",
64 				    op, ret);
65 		return ret;
66 	}
67 
68 	return hr_dev->hw->poll_mbox_done(hr_dev, timeout);
69 }
70 
71 static int hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
72 				  u64 out_param, unsigned long in_modifier,
73 				  u8 op_modifier, u16 op, unsigned int timeout)
74 {
75 	int ret;
76 
77 	down(&hr_dev->cmd.poll_sem);
78 	ret = __hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param, in_modifier,
79 				       op_modifier, op, timeout);
80 	up(&hr_dev->cmd.poll_sem);
81 
82 	return ret;
83 }
84 
85 void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
86 			u64 out_param)
87 {
88 	struct hns_roce_cmd_context *context =
89 		&hr_dev->cmd.context[token % hr_dev->cmd.max_cmds];
90 
91 	if (unlikely(token != context->token)) {
92 		dev_err_ratelimited(hr_dev->dev,
93 				    "[cmd] invalid ae token 0x%x, context token is 0x%x.\n",
94 				    token, context->token);
95 		return;
96 	}
97 
98 	context->result = (status == HNS_ROCE_CMD_SUCCESS) ? 0 : (-EIO);
99 	context->out_param = out_param;
100 	complete(&context->done);
101 }
102 
103 static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
104 				    u64 out_param, unsigned long in_modifier,
105 				    u8 op_modifier, u16 op,
106 				    unsigned int timeout)
107 {
108 	struct hns_roce_cmdq *cmd = &hr_dev->cmd;
109 	struct hns_roce_cmd_context *context;
110 	struct device *dev = hr_dev->dev;
111 	int ret;
112 
113 	spin_lock(&cmd->context_lock);
114 
115 	do {
116 		context = &cmd->context[cmd->free_head];
117 		cmd->free_head = context->next;
118 	} while (context->busy);
119 
120 	context->busy = 1;
121 	context->token += cmd->max_cmds;
122 
123 	spin_unlock(&cmd->context_lock);
124 
125 	reinit_completion(&context->done);
126 
127 	ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
128 					in_modifier, op_modifier, op,
129 					context->token, 1);
130 	if (ret) {
131 		dev_err_ratelimited(dev,
132 				    "failed to post mailbox 0x%x in event mode, ret = %d.\n",
133 				    op, ret);
134 		goto out;
135 	}
136 
137 	if (!wait_for_completion_timeout(&context->done,
138 					 msecs_to_jiffies(timeout))) {
139 		dev_err_ratelimited(dev, "[cmd] token 0x%x mailbox 0x%x timeout.\n",
140 				    context->token, op);
141 		ret = -EBUSY;
142 		goto out;
143 	}
144 
145 	ret = context->result;
146 	if (ret)
147 		dev_err_ratelimited(dev, "[cmd] token 0x%x mailbox 0x%x error %d.\n",
148 				    context->token, op, ret);
149 
150 out:
151 	context->busy = 0;
152 	return ret;
153 }
154 
155 static int hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
156 				  u64 out_param, unsigned long in_modifier,
157 				  u8 op_modifier, u16 op, unsigned int timeout)
158 {
159 	int ret;
160 
161 	down(&hr_dev->cmd.event_sem);
162 	ret = __hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param, in_modifier,
163 				       op_modifier, op, timeout);
164 	up(&hr_dev->cmd.event_sem);
165 
166 	return ret;
167 }
168 
169 int hns_roce_cmd_mbox(struct hns_roce_dev *hr_dev, u64 in_param, u64 out_param,
170 		      unsigned long in_modifier, u8 op_modifier, u16 op,
171 		      unsigned int timeout)
172 {
173 	bool is_busy;
174 
175 	if (hr_dev->hw->chk_mbox_avail)
176 		if (!hr_dev->hw->chk_mbox_avail(hr_dev, &is_busy))
177 			return is_busy ? -EBUSY : 0;
178 
179 	if (hr_dev->cmd.use_events)
180 		return hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
181 					      in_modifier, op_modifier, op,
182 					      timeout);
183 	else
184 		return hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param,
185 					      in_modifier, op_modifier, op,
186 					      timeout);
187 }
188 
189 int hns_roce_cmd_init(struct hns_roce_dev *hr_dev)
190 {
191 	sema_init(&hr_dev->cmd.poll_sem, 1);
192 	hr_dev->cmd.use_events = 0;
193 	hr_dev->cmd.max_cmds = CMD_MAX_NUM;
194 	hr_dev->cmd.pool = dma_pool_create("hns_roce_cmd", hr_dev->dev,
195 					   HNS_ROCE_MAILBOX_SIZE,
196 					   HNS_ROCE_MAILBOX_SIZE, 0);
197 	if (!hr_dev->cmd.pool)
198 		return -ENOMEM;
199 
200 	return 0;
201 }
202 
203 void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev)
204 {
205 	dma_pool_destroy(hr_dev->cmd.pool);
206 }
207 
208 int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev)
209 {
210 	struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
211 	int i;
212 
213 	hr_cmd->context =
214 		kcalloc(hr_cmd->max_cmds, sizeof(*hr_cmd->context), GFP_KERNEL);
215 	if (!hr_cmd->context) {
216 		hr_dev->cmd_mod = 0;
217 		return -ENOMEM;
218 	}
219 
220 	for (i = 0; i < hr_cmd->max_cmds; ++i) {
221 		hr_cmd->context[i].token = i;
222 		hr_cmd->context[i].next = i + 1;
223 		init_completion(&hr_cmd->context[i].done);
224 	}
225 	hr_cmd->context[hr_cmd->max_cmds - 1].next = 0;
226 	hr_cmd->free_head = 0;
227 
228 	sema_init(&hr_cmd->event_sem, hr_cmd->max_cmds);
229 	spin_lock_init(&hr_cmd->context_lock);
230 
231 	hr_cmd->use_events = 1;
232 
233 	return 0;
234 }
235 
236 void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev)
237 {
238 	struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
239 
240 	kfree(hr_cmd->context);
241 	hr_cmd->use_events = 0;
242 }
243 
244 struct hns_roce_cmd_mailbox *
245 hns_roce_alloc_cmd_mailbox(struct hns_roce_dev *hr_dev)
246 {
247 	struct hns_roce_cmd_mailbox *mailbox;
248 
249 	mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
250 	if (!mailbox)
251 		return ERR_PTR(-ENOMEM);
252 
253 	mailbox->buf =
254 		dma_pool_alloc(hr_dev->cmd.pool, GFP_KERNEL, &mailbox->dma);
255 	if (!mailbox->buf) {
256 		kfree(mailbox);
257 		return ERR_PTR(-ENOMEM);
258 	}
259 
260 	return mailbox;
261 }
262 
263 void hns_roce_free_cmd_mailbox(struct hns_roce_dev *hr_dev,
264 			       struct hns_roce_cmd_mailbox *mailbox)
265 {
266 	if (!mailbox)
267 		return;
268 
269 	dma_pool_free(hr_dev->cmd.pool, mailbox->buf, mailbox->dma);
270 	kfree(mailbox);
271 }
272