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 <linux/platform_device.h>
35 #include "hns_roce_common.h"
36 #include "hns_roce_device.h"
37 #include "hns_roce_cmd.h"
38
39 #define CMD_POLL_TOKEN 0xffff
40 #define CMD_MAX_NUM 32
41 #define CMD_TOKEN_MASK 0x1f
42
hns_roce_cmd_mbox_post_hw(struct hns_roce_dev * hr_dev,u64 in_param,u64 out_param,u32 in_modifier,u8 op_modifier,u16 op,u16 token,int event)43 static int hns_roce_cmd_mbox_post_hw(struct hns_roce_dev *hr_dev, u64 in_param,
44 u64 out_param, u32 in_modifier,
45 u8 op_modifier, u16 op, u16 token,
46 int event)
47 {
48 struct hns_roce_cmdq *cmd = &hr_dev->cmd;
49 int ret;
50
51 mutex_lock(&cmd->hcr_mutex);
52 ret = hr_dev->hw->post_mbox(hr_dev, in_param, out_param, in_modifier,
53 op_modifier, op, token, event);
54 mutex_unlock(&cmd->hcr_mutex);
55
56 return ret;
57 }
58
59 /* this should be called with "poll_sem" */
__hns_roce_cmd_mbox_poll(struct hns_roce_dev * hr_dev,u64 in_param,u64 out_param,unsigned long in_modifier,u8 op_modifier,u16 op,unsigned long timeout)60 static int __hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
61 u64 out_param, unsigned long in_modifier,
62 u8 op_modifier, u16 op,
63 unsigned long timeout)
64 {
65 struct device *dev = hr_dev->dev;
66 int ret;
67
68 ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
69 in_modifier, op_modifier, op,
70 CMD_POLL_TOKEN, 0);
71 if (ret) {
72 dev_err(dev, "[cmd_poll]hns_roce_cmd_mbox_post_hw failed\n");
73 return ret;
74 }
75
76 return hr_dev->hw->chk_mbox(hr_dev, timeout);
77 }
78
hns_roce_cmd_mbox_poll(struct hns_roce_dev * hr_dev,u64 in_param,u64 out_param,unsigned long in_modifier,u8 op_modifier,u16 op,unsigned long timeout)79 static int hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
80 u64 out_param, unsigned long in_modifier,
81 u8 op_modifier, u16 op, unsigned long timeout)
82 {
83 int ret;
84
85 down(&hr_dev->cmd.poll_sem);
86 ret = __hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param, in_modifier,
87 op_modifier, op, timeout);
88 up(&hr_dev->cmd.poll_sem);
89
90 return ret;
91 }
92
hns_roce_cmd_event(struct hns_roce_dev * hr_dev,u16 token,u8 status,u64 out_param)93 void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
94 u64 out_param)
95 {
96 struct hns_roce_cmd_context
97 *context = &hr_dev->cmd.context[token & hr_dev->cmd.token_mask];
98
99 if (token != context->token)
100 return;
101
102 context->result = (status == HNS_ROCE_CMD_SUCCESS) ? 0 : (-EIO);
103 context->out_param = out_param;
104 complete(&context->done);
105 }
106 EXPORT_SYMBOL_GPL(hns_roce_cmd_event);
107
108 /* this should be called with "use_events" */
__hns_roce_cmd_mbox_wait(struct hns_roce_dev * hr_dev,u64 in_param,u64 out_param,unsigned long in_modifier,u8 op_modifier,u16 op,unsigned long timeout)109 static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
110 u64 out_param, unsigned long in_modifier,
111 u8 op_modifier, u16 op,
112 unsigned long timeout)
113 {
114 struct hns_roce_cmdq *cmd = &hr_dev->cmd;
115 struct hns_roce_cmd_context *context;
116 struct device *dev = hr_dev->dev;
117 int ret;
118
119 spin_lock(&cmd->context_lock);
120 WARN_ON(cmd->free_head < 0);
121 context = &cmd->context[cmd->free_head];
122 context->token += cmd->token_mask + 1;
123 cmd->free_head = context->next;
124 spin_unlock(&cmd->context_lock);
125
126 init_completion(&context->done);
127
128 ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
129 in_modifier, op_modifier, op,
130 context->token, 1);
131 if (ret)
132 goto out;
133
134 /*
135 * It is timeout when wait_for_completion_timeout return 0
136 * The return value is the time limit set in advance
137 * how many seconds showing
138 */
139 if (!wait_for_completion_timeout(&context->done,
140 msecs_to_jiffies(timeout))) {
141 dev_err(dev, "[cmd]wait_for_completion_timeout timeout\n");
142 ret = -EBUSY;
143 goto out;
144 }
145
146 ret = context->result;
147 if (ret) {
148 dev_err(dev, "[cmd]event mod cmd process error!err=%d\n", ret);
149 goto out;
150 }
151
152 out:
153 spin_lock(&cmd->context_lock);
154 context->next = cmd->free_head;
155 cmd->free_head = context - cmd->context;
156 spin_unlock(&cmd->context_lock);
157
158 return ret;
159 }
160
hns_roce_cmd_mbox_wait(struct hns_roce_dev * hr_dev,u64 in_param,u64 out_param,unsigned long in_modifier,u8 op_modifier,u16 op,unsigned long timeout)161 static int hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
162 u64 out_param, unsigned long in_modifier,
163 u8 op_modifier, u16 op, unsigned long timeout)
164 {
165 int ret = 0;
166
167 down(&hr_dev->cmd.event_sem);
168 ret = __hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
169 in_modifier, op_modifier, op, timeout);
170 up(&hr_dev->cmd.event_sem);
171
172 return ret;
173 }
174
hns_roce_cmd_mbox(struct hns_roce_dev * hr_dev,u64 in_param,u64 out_param,unsigned long in_modifier,u8 op_modifier,u16 op,unsigned long timeout)175 int hns_roce_cmd_mbox(struct hns_roce_dev *hr_dev, u64 in_param, u64 out_param,
176 unsigned long in_modifier, u8 op_modifier, u16 op,
177 unsigned long timeout)
178 {
179 if (hr_dev->is_reset)
180 return 0;
181
182 if (hr_dev->cmd.use_events)
183 return hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
184 in_modifier, op_modifier, op,
185 timeout);
186 else
187 return hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param,
188 in_modifier, op_modifier, op,
189 timeout);
190 }
191 EXPORT_SYMBOL_GPL(hns_roce_cmd_mbox);
192
hns_roce_cmd_init(struct hns_roce_dev * hr_dev)193 int hns_roce_cmd_init(struct hns_roce_dev *hr_dev)
194 {
195 struct device *dev = hr_dev->dev;
196
197 mutex_init(&hr_dev->cmd.hcr_mutex);
198 sema_init(&hr_dev->cmd.poll_sem, 1);
199 hr_dev->cmd.use_events = 0;
200 hr_dev->cmd.toggle = 1;
201 hr_dev->cmd.max_cmds = CMD_MAX_NUM;
202 hr_dev->cmd.pool = dma_pool_create("hns_roce_cmd", dev,
203 HNS_ROCE_MAILBOX_SIZE,
204 HNS_ROCE_MAILBOX_SIZE, 0);
205 if (!hr_dev->cmd.pool)
206 return -ENOMEM;
207
208 return 0;
209 }
210
hns_roce_cmd_cleanup(struct hns_roce_dev * hr_dev)211 void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev)
212 {
213 dma_pool_destroy(hr_dev->cmd.pool);
214 }
215
hns_roce_cmd_use_events(struct hns_roce_dev * hr_dev)216 int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev)
217 {
218 struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
219 int i;
220
221 hr_cmd->context = kmalloc_array(hr_cmd->max_cmds,
222 sizeof(*hr_cmd->context),
223 GFP_KERNEL);
224 if (!hr_cmd->context)
225 return -ENOMEM;
226
227 for (i = 0; i < hr_cmd->max_cmds; ++i) {
228 hr_cmd->context[i].token = i;
229 hr_cmd->context[i].next = i + 1;
230 }
231
232 hr_cmd->context[hr_cmd->max_cmds - 1].next = -1;
233 hr_cmd->free_head = 0;
234
235 sema_init(&hr_cmd->event_sem, hr_cmd->max_cmds);
236 spin_lock_init(&hr_cmd->context_lock);
237
238 hr_cmd->token_mask = CMD_TOKEN_MASK;
239 hr_cmd->use_events = 1;
240
241 down(&hr_cmd->poll_sem);
242
243 return 0;
244 }
245
hns_roce_cmd_use_polling(struct hns_roce_dev * hr_dev)246 void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev)
247 {
248 struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
249 int i;
250
251 hr_cmd->use_events = 0;
252
253 for (i = 0; i < hr_cmd->max_cmds; ++i)
254 down(&hr_cmd->event_sem);
255
256 kfree(hr_cmd->context);
257 up(&hr_cmd->poll_sem);
258 }
259
260 struct hns_roce_cmd_mailbox
hns_roce_alloc_cmd_mailbox(struct hns_roce_dev * hr_dev)261 *hns_roce_alloc_cmd_mailbox(struct hns_roce_dev *hr_dev)
262 {
263 struct hns_roce_cmd_mailbox *mailbox;
264
265 mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
266 if (!mailbox)
267 return ERR_PTR(-ENOMEM);
268
269 mailbox->buf = dma_pool_alloc(hr_dev->cmd.pool, GFP_KERNEL,
270 &mailbox->dma);
271 if (!mailbox->buf) {
272 kfree(mailbox);
273 return ERR_PTR(-ENOMEM);
274 }
275
276 return mailbox;
277 }
278 EXPORT_SYMBOL_GPL(hns_roce_alloc_cmd_mailbox);
279
hns_roce_free_cmd_mailbox(struct hns_roce_dev * hr_dev,struct hns_roce_cmd_mailbox * mailbox)280 void hns_roce_free_cmd_mailbox(struct hns_roce_dev *hr_dev,
281 struct hns_roce_cmd_mailbox *mailbox)
282 {
283 if (!mailbox)
284 return;
285
286 dma_pool_free(hr_dev->cmd.pool, mailbox->buf, mailbox->dma);
287 kfree(mailbox);
288 }
289 EXPORT_SYMBOL_GPL(hns_roce_free_cmd_mailbox);
290