]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/ofed/drivers/net/mlx4/cmd.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / ofed / drivers / net / mlx4 / cmd.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2005, 2006, 2007 Cisco Systems, 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 <linux/sched.h>
36 #include <linux/pci.h>
37 #include <linux/errno.h>
38
39 #include <linux/mlx4/cmd.h>
40
41 #include <asm/io.h>
42
43 #include "mlx4.h"
44
45 #define CMD_POLL_TOKEN 0xffff
46
47 enum {
48         /* command completed successfully: */
49         CMD_STAT_OK             = 0x00,
50         /* Internal error (such as a bus error) occurred while processing command: */
51         CMD_STAT_INTERNAL_ERR   = 0x01,
52         /* Operation/command not supported or opcode modifier not supported: */
53         CMD_STAT_BAD_OP         = 0x02,
54         /* Parameter not supported or parameter out of range: */
55         CMD_STAT_BAD_PARAM      = 0x03,
56         /* System not enabled or bad system state: */
57         CMD_STAT_BAD_SYS_STATE  = 0x04,
58         /* Attempt to access reserved or unallocaterd resource: */
59         CMD_STAT_BAD_RESOURCE   = 0x05,
60         /* Requested resource is currently executing a command, or is otherwise busy: */
61         CMD_STAT_RESOURCE_BUSY  = 0x06,
62         /* Required capability exceeds device limits: */
63         CMD_STAT_EXCEED_LIM     = 0x08,
64         /* Resource is not in the appropriate state or ownership: */
65         CMD_STAT_BAD_RES_STATE  = 0x09,
66         /* Index out of range: */
67         CMD_STAT_BAD_INDEX      = 0x0a,
68         /* FW image corrupted: */
69         CMD_STAT_BAD_NVMEM      = 0x0b,
70         /* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
71         CMD_STAT_ICM_ERROR      = 0x0c,
72         /* Attempt to modify a QP/EE which is not in the presumed state: */
73         CMD_STAT_BAD_QP_STATE   = 0x10,
74         /* Bad segment parameters (Address/Size): */
75         CMD_STAT_BAD_SEG_PARAM  = 0x20,
76         /* Memory Region has Memory Windows bound to: */
77         CMD_STAT_REG_BOUND      = 0x21,
78         /* HCA local attached memory not present: */
79         CMD_STAT_LAM_NOT_PRE    = 0x22,
80         /* Bad management packet (silently discarded): */
81         CMD_STAT_BAD_PKT        = 0x30,
82         /* More outstanding CQEs in CQ than new CQ size: */
83         CMD_STAT_BAD_SIZE       = 0x40,
84         /* Multi Function device support required: */
85         CMD_STAT_MULTI_FUNC_REQ = 0x50,
86 };
87
88 enum {
89         HCR_IN_PARAM_OFFSET     = 0x00,
90         HCR_IN_MODIFIER_OFFSET  = 0x08,
91         HCR_OUT_PARAM_OFFSET    = 0x0c,
92         HCR_TOKEN_OFFSET        = 0x14,
93         HCR_STATUS_OFFSET       = 0x18,
94
95         HCR_OPMOD_SHIFT         = 12,
96         HCR_T_BIT               = 21,
97         HCR_E_BIT               = 22,
98         HCR_GO_BIT              = 23
99 };
100
101 enum {
102         GO_BIT_TIMEOUT_MSECS    = 10000
103 };
104
105 struct mlx4_cmd_context {
106         struct completion       done;
107         int                     result;
108         int                     next;
109         u64                     out_param;
110         u16                     token;
111         u8                      fw_status;
112 };
113
114 static int mlx4_status_to_errno(u8 status)
115 {
116         static const int trans_table[] = {
117                 [CMD_STAT_INTERNAL_ERR]   = -EIO,
118                 [CMD_STAT_BAD_OP]         = -EPERM,
119                 [CMD_STAT_BAD_PARAM]      = -EINVAL,
120                 [CMD_STAT_BAD_SYS_STATE]  = -ENXIO,
121                 [CMD_STAT_BAD_RESOURCE]   = -EBADF,
122                 [CMD_STAT_RESOURCE_BUSY]  = -EBUSY,
123                 [CMD_STAT_EXCEED_LIM]     = -ENOMEM,
124                 [CMD_STAT_BAD_RES_STATE]  = -EBADF,
125                 [CMD_STAT_BAD_INDEX]      = -EBADF,
126                 [CMD_STAT_BAD_NVMEM]      = -EFAULT,
127                 [CMD_STAT_ICM_ERROR]      = -ENFILE,
128                 [CMD_STAT_BAD_QP_STATE]   = -EINVAL,
129                 [CMD_STAT_BAD_SEG_PARAM]  = -EFAULT,
130                 [CMD_STAT_REG_BOUND]      = -EBUSY,
131                 [CMD_STAT_LAM_NOT_PRE]    = -EAGAIN,
132                 [CMD_STAT_BAD_PKT]        = -EINVAL,
133                 [CMD_STAT_BAD_SIZE]       = -ENOMEM,
134                 [CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
135         };
136
137         if (status >= ARRAY_SIZE(trans_table) ||
138             (status != CMD_STAT_OK && trans_table[status] == 0))
139                 return -EIO;
140
141         return trans_table[status];
142 }
143
144 static int cmd_pending(struct mlx4_dev *dev)
145 {
146         u32 status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
147
148         return (status & swab32(1 << HCR_GO_BIT)) ||
149                 (mlx4_priv(dev)->cmd.toggle ==
150                  !!(status & swab32(1 << HCR_T_BIT)));
151 }
152
153 static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
154                          u32 in_modifier, u8 op_modifier, u16 op, u16 token,
155                          int event)
156 {
157         struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
158         u32 __iomem *hcr = cmd->hcr;
159         int ret = -EAGAIN;
160         unsigned long end;
161
162         mutex_lock(&cmd->hcr_mutex);
163
164         end = jiffies;
165         if (event)
166                 end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
167
168         while (cmd_pending(dev)) {
169                 if (time_after_eq(jiffies, end))
170                         goto out;
171                 cond_resched();
172         }
173
174         /*
175          * We use writel (instead of something like memcpy_toio)
176          * because writes of less than 32 bits to the HCR don't work
177          * (and some architectures such as ia64 implement memcpy_toio
178          * in terms of writeb).
179          */
180         __raw_writel((__force u32) cpu_to_be32(in_param >> 32),           hcr + 0);
181         __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful),  hcr + 1);
182         __raw_writel((__force u32) cpu_to_be32(in_modifier),              hcr + 2);
183         __raw_writel((__force u32) cpu_to_be32(out_param >> 32),          hcr + 3);
184         __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
185         __raw_writel((__force u32) cpu_to_be32(token << 16),              hcr + 5);
186
187         /* __raw_writel may not order writes. */
188         wmb();
189
190         __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT)                |
191                                                (cmd->toggle << HCR_T_BIT)       |
192                                                (event ? (1 << HCR_E_BIT) : 0)   |
193                                                (op_modifier << HCR_OPMOD_SHIFT) |
194                                                op),                       hcr + 6);
195
196         /*
197          * Make sure that our HCR writes don't get mixed in with
198          * writes from another CPU starting a FW command.
199          */
200         mmiowb();
201
202         cmd->toggle = cmd->toggle ^ 1;
203
204         ret = 0;
205
206 out:
207         mutex_unlock(&cmd->hcr_mutex);
208         return ret;
209 }
210
211 static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
212                          int out_is_imm, u32 in_modifier, u8 op_modifier,
213                          u16 op, unsigned long timeout)
214 {
215         struct mlx4_priv *priv = mlx4_priv(dev);
216         void __iomem *hcr = priv->cmd.hcr;
217         int err = 0;
218         unsigned long end;
219         u32 stat;
220
221         down(&priv->cmd.poll_sem);
222
223         err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
224                             in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
225         if (err)
226                 goto out;
227
228         end = msecs_to_jiffies(timeout) + jiffies;
229         while (cmd_pending(dev) && time_before(jiffies, end))
230                 cond_resched();
231
232         if (cmd_pending(dev)) {
233                 err = -ETIMEDOUT;
234                 goto out;
235         }
236
237         if (out_is_imm)
238                 *out_param =
239                         (u64) be32_to_cpu((__force __be32)
240                                           __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
241                         (u64) be32_to_cpu((__force __be32)
242                                           __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
243         stat = be32_to_cpu((__force __be32) __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
244         err = mlx4_status_to_errno(stat);
245         if (err) {
246                 if (op != MLX4_CMD_SET_NODE || stat != CMD_STAT_BAD_OP)
247                         mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
248                                  op, stat);
249         }
250
251 out:
252         up(&priv->cmd.poll_sem);
253         return err;
254 }
255
256 void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
257 {
258         struct mlx4_priv *priv = mlx4_priv(dev);
259         struct mlx4_cmd_context *context =
260                 &priv->cmd.context[token & priv->cmd.token_mask];
261
262         /* previously timed out command completing at long last */
263         if (token != context->token)
264                 return;
265
266         context->fw_status = status;
267         context->result    = mlx4_status_to_errno(status);
268         context->out_param = out_param;
269
270         complete(&context->done);
271 }
272
273 static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
274                          int out_is_imm, u32 in_modifier, u8 op_modifier,
275                          u16 op, unsigned long timeout)
276 {
277         struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
278         struct mlx4_cmd_context *context;
279         int err = 0;
280
281         down(&cmd->event_sem);
282
283         spin_lock(&cmd->context_lock);
284         BUG_ON(cmd->free_head < 0);
285         context = &cmd->context[cmd->free_head];
286         context->token += cmd->token_mask + 1;
287         cmd->free_head = context->next;
288         spin_unlock(&cmd->context_lock);
289
290         init_completion(&context->done);
291
292         mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
293                       in_modifier, op_modifier, op, context->token, 1);
294
295         if (!wait_for_completion_timeout(&context->done, msecs_to_jiffies(timeout))) {
296                 err = -EBUSY;
297                 goto out;
298         }
299
300         err = context->result;
301         if (err) {
302                 if (op != MLX4_CMD_SET_NODE || context->fw_status != CMD_STAT_BAD_OP)
303                         mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
304                                  op, context->fw_status);
305                 goto out;
306         }
307
308         if (out_is_imm)
309                 *out_param = context->out_param;
310
311 out:
312         spin_lock(&cmd->context_lock);
313         context->next = cmd->free_head;
314         cmd->free_head = context - cmd->context;
315         spin_unlock(&cmd->context_lock);
316
317         up(&cmd->event_sem);
318         return err;
319 }
320
321 int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
322                int out_is_imm, u32 in_modifier, u8 op_modifier,
323                u16 op, unsigned long timeout)
324 {
325         if (mlx4_priv(dev)->cmd.use_events && !cold)
326                 return mlx4_cmd_wait(dev, in_param, out_param, out_is_imm,
327                                      in_modifier, op_modifier, op, timeout);
328         else
329                 return mlx4_cmd_poll(dev, in_param, out_param, out_is_imm,
330                                      in_modifier, op_modifier, op, timeout);
331 }
332 EXPORT_SYMBOL_GPL(__mlx4_cmd);
333
334 int mlx4_cmd_init(struct mlx4_dev *dev)
335 {
336         struct mlx4_priv *priv = mlx4_priv(dev);
337
338         mutex_init(&priv->cmd.hcr_mutex);
339         sema_init(&priv->cmd.poll_sem, 1);
340         priv->cmd.use_events = 0;
341         priv->cmd.toggle     = 1;
342
343         priv->cmd.hcr = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_HCR_BASE,
344                                 MLX4_HCR_SIZE);
345         if (!priv->cmd.hcr) {
346                 mlx4_err(dev, "Couldn't map command register.");
347                 return -ENOMEM;
348         }
349
350         priv->cmd.pool = pci_pool_create("mlx4_cmd", dev->pdev,
351                                          MLX4_MAILBOX_SIZE,
352                                          MLX4_MAILBOX_SIZE, 0);
353         if (!priv->cmd.pool) {
354                 iounmap(priv->cmd.hcr);
355                 return -ENOMEM;
356         }
357
358         return 0;
359 }
360
361 void mlx4_cmd_cleanup(struct mlx4_dev *dev)
362 {
363         struct mlx4_priv *priv = mlx4_priv(dev);
364
365         pci_pool_destroy(priv->cmd.pool);
366         iounmap(priv->cmd.hcr);
367 }
368
369 /*
370  * Switch to using events to issue FW commands (can only be called
371  * after event queue for command events has been initialized).
372  */
373 int mlx4_cmd_use_events(struct mlx4_dev *dev)
374 {
375         struct mlx4_priv *priv = mlx4_priv(dev);
376         int i;
377
378         priv->cmd.context = kmalloc(priv->cmd.max_cmds *
379                                    sizeof (struct mlx4_cmd_context),
380                                    GFP_KERNEL);
381         if (!priv->cmd.context)
382                 return -ENOMEM;
383
384         for (i = 0; i < priv->cmd.max_cmds; ++i) {
385                 priv->cmd.context[i].token = i;
386                 priv->cmd.context[i].next  = i + 1;
387         }
388
389         priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
390         priv->cmd.free_head = 0;
391
392         sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
393         spin_lock_init(&priv->cmd.context_lock);
394
395         for (priv->cmd.token_mask = 1;
396              priv->cmd.token_mask < priv->cmd.max_cmds;
397              priv->cmd.token_mask <<= 1)
398                 ; /* nothing */
399         --priv->cmd.token_mask;
400
401         priv->cmd.use_events = 1;
402
403         down(&priv->cmd.poll_sem);
404
405         return 0;
406 }
407
408 /*
409  * Switch back to polling (used when shutting down the device)
410  */
411 void mlx4_cmd_use_polling(struct mlx4_dev *dev)
412 {
413         struct mlx4_priv *priv = mlx4_priv(dev);
414         int i;
415
416         priv->cmd.use_events = 0;
417
418         for (i = 0; i < priv->cmd.max_cmds; ++i)
419                 down(&priv->cmd.event_sem);
420
421         kfree(priv->cmd.context);
422
423         up(&priv->cmd.poll_sem);
424 }
425
426 struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
427 {
428         struct mlx4_cmd_mailbox *mailbox;
429
430         mailbox = kmalloc(sizeof *mailbox, GFP_KERNEL);
431         if (!mailbox)
432                 return ERR_PTR(-ENOMEM);
433
434         mailbox->buf = pci_pool_alloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
435                                       &mailbox->dma);
436         if (!mailbox->buf) {
437                 kfree(mailbox);
438                 return ERR_PTR(-ENOMEM);
439         }
440
441         return mailbox;
442 }
443 EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
444
445 void mlx4_free_cmd_mailbox(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox)
446 {
447         if (!mailbox)
448                 return;
449
450         pci_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
451         kfree(mailbox);
452 }
453 EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);