blob: d1e7ecb5f2331bdf8ef4a0df9f69ed9fecd14243 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
Roland Dreier4885bf62006-01-30 14:31:33 -08004 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 * $Id: mthca_cmd.c 1349 2004-12-16 21:09:43Z roland $
35 */
36
37#include <linux/sched.h>
38#include <linux/pci.h>
39#include <linux/errno.h>
40#include <asm/io.h>
Roland Dreiera4d61e82005-08-25 13:40:04 -070041#include <rdma/ib_mad.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "mthca_dev.h"
44#include "mthca_config_reg.h"
45#include "mthca_cmd.h"
46#include "mthca_memfree.h"
47
48#define CMD_POLL_TOKEN 0xffff
49
50enum {
51 HCR_IN_PARAM_OFFSET = 0x00,
52 HCR_IN_MODIFIER_OFFSET = 0x08,
53 HCR_OUT_PARAM_OFFSET = 0x0c,
54 HCR_TOKEN_OFFSET = 0x14,
55 HCR_STATUS_OFFSET = 0x18,
56
57 HCR_OPMOD_SHIFT = 12,
58 HCA_E_BIT = 22,
59 HCR_GO_BIT = 23
60};
61
62enum {
63 /* initialization and general commands */
64 CMD_SYS_EN = 0x1,
65 CMD_SYS_DIS = 0x2,
66 CMD_MAP_FA = 0xfff,
67 CMD_UNMAP_FA = 0xffe,
68 CMD_RUN_FW = 0xff6,
69 CMD_MOD_STAT_CFG = 0x34,
70 CMD_QUERY_DEV_LIM = 0x3,
71 CMD_QUERY_FW = 0x4,
72 CMD_ENABLE_LAM = 0xff8,
73 CMD_DISABLE_LAM = 0xff7,
74 CMD_QUERY_DDR = 0x5,
75 CMD_QUERY_ADAPTER = 0x6,
76 CMD_INIT_HCA = 0x7,
77 CMD_CLOSE_HCA = 0x8,
78 CMD_INIT_IB = 0x9,
79 CMD_CLOSE_IB = 0xa,
80 CMD_QUERY_HCA = 0xb,
81 CMD_SET_IB = 0xc,
82 CMD_ACCESS_DDR = 0x2e,
83 CMD_MAP_ICM = 0xffa,
84 CMD_UNMAP_ICM = 0xff9,
85 CMD_MAP_ICM_AUX = 0xffc,
86 CMD_UNMAP_ICM_AUX = 0xffb,
87 CMD_SET_ICM_SIZE = 0xffd,
88
89 /* TPT commands */
90 CMD_SW2HW_MPT = 0xd,
91 CMD_QUERY_MPT = 0xe,
92 CMD_HW2SW_MPT = 0xf,
93 CMD_READ_MTT = 0x10,
94 CMD_WRITE_MTT = 0x11,
95 CMD_SYNC_TPT = 0x2f,
96
97 /* EQ commands */
98 CMD_MAP_EQ = 0x12,
99 CMD_SW2HW_EQ = 0x13,
100 CMD_HW2SW_EQ = 0x14,
101 CMD_QUERY_EQ = 0x15,
102
103 /* CQ commands */
104 CMD_SW2HW_CQ = 0x16,
105 CMD_HW2SW_CQ = 0x17,
106 CMD_QUERY_CQ = 0x18,
107 CMD_RESIZE_CQ = 0x2c,
108
109 /* SRQ commands */
110 CMD_SW2HW_SRQ = 0x35,
111 CMD_HW2SW_SRQ = 0x36,
112 CMD_QUERY_SRQ = 0x37,
Roland Dreierec34a922005-08-19 10:59:31 -0700113 CMD_ARM_SRQ = 0x40,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* QP/EE commands */
116 CMD_RST2INIT_QPEE = 0x19,
117 CMD_INIT2RTR_QPEE = 0x1a,
118 CMD_RTR2RTS_QPEE = 0x1b,
119 CMD_RTS2RTS_QPEE = 0x1c,
120 CMD_SQERR2RTS_QPEE = 0x1d,
121 CMD_2ERR_QPEE = 0x1e,
122 CMD_RTS2SQD_QPEE = 0x1f,
123 CMD_SQD2SQD_QPEE = 0x38,
124 CMD_SQD2RTS_QPEE = 0x20,
125 CMD_ERR2RST_QPEE = 0x21,
126 CMD_QUERY_QPEE = 0x22,
127 CMD_INIT2INIT_QPEE = 0x2d,
128 CMD_SUSPEND_QPEE = 0x32,
129 CMD_UNSUSPEND_QPEE = 0x33,
130 /* special QPs and management commands */
131 CMD_CONF_SPECIAL_QP = 0x23,
132 CMD_MAD_IFC = 0x24,
133
134 /* multicast commands */
135 CMD_READ_MGM = 0x25,
136 CMD_WRITE_MGM = 0x26,
137 CMD_MGID_HASH = 0x27,
138
139 /* miscellaneous commands */
140 CMD_DIAG_RPRT = 0x30,
141 CMD_NOP = 0x31,
142
143 /* debug commands */
144 CMD_QUERY_DEBUG_MSG = 0x2a,
145 CMD_SET_DEBUG_MSG = 0x2b,
146};
147
148/*
149 * According to Mellanox code, FW may be starved and never complete
150 * commands. So we can't use strict timeouts described in PRM -- we
151 * just arbitrarily select 60 seconds for now.
152 */
153#if 0
154/*
155 * Round up and add 1 to make sure we get the full wait time (since we
156 * will be starting in the middle of a jiffy)
157 */
158enum {
159 CMD_TIME_CLASS_A = (HZ + 999) / 1000 + 1,
160 CMD_TIME_CLASS_B = (HZ + 99) / 100 + 1,
161 CMD_TIME_CLASS_C = (HZ + 9) / 10 + 1
162};
163#else
164enum {
165 CMD_TIME_CLASS_A = 60 * HZ,
166 CMD_TIME_CLASS_B = 60 * HZ,
167 CMD_TIME_CLASS_C = 60 * HZ
168};
169#endif
170
171enum {
172 GO_BIT_TIMEOUT = HZ * 10
173};
174
175struct mthca_cmd_context {
176 struct completion done;
177 struct timer_list timer;
178 int result;
179 int next;
180 u64 out_param;
181 u16 token;
182 u8 status;
183};
184
Eli Cohen14abdff2006-02-26 14:36:06 -0800185static int fw_cmd_doorbell = 1;
186module_param(fw_cmd_doorbell, int, 0644);
187MODULE_PARM_DESC(fw_cmd_doorbell, "post FW commands through doorbell page if nonzero "
188 "(and supported by FW)");
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static inline int go_bit(struct mthca_dev *dev)
191{
192 return readl(dev->hcr + HCR_STATUS_OFFSET) &
193 swab32(1 << HCR_GO_BIT);
194}
195
Eli Cohen14abdff2006-02-26 14:36:06 -0800196static void mthca_cmd_post_dbell(struct mthca_dev *dev,
197 u64 in_param,
198 u64 out_param,
199 u32 in_modifier,
200 u8 op_modifier,
201 u16 op,
202 u16 token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Eli Cohen14abdff2006-02-26 14:36:06 -0800204 void __iomem *ptr = dev->cmd.dbell_map;
205 u16 *offs = dev->cmd.dbell_offsets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Eli Cohen14abdff2006-02-26 14:36:06 -0800207 __raw_writel((__force u32) cpu_to_be32(in_param >> 32), ptr + offs[0]);
208 wmb();
209 __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), ptr + offs[1]);
210 wmb();
211 __raw_writel((__force u32) cpu_to_be32(in_modifier), ptr + offs[2]);
212 wmb();
213 __raw_writel((__force u32) cpu_to_be32(out_param >> 32), ptr + offs[3]);
214 wmb();
215 __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), ptr + offs[4]);
216 wmb();
217 __raw_writel((__force u32) cpu_to_be32(token << 16), ptr + offs[5]);
218 wmb();
219 __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
220 (1 << HCA_E_BIT) |
221 (op_modifier << HCR_OPMOD_SHIFT) |
222 op), ptr + offs[6]);
223 wmb();
224 __raw_writel((__force u32) 0, ptr + offs[7]);
225 wmb();
226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eli Cohen14abdff2006-02-26 14:36:06 -0800228static int mthca_cmd_post_hcr(struct mthca_dev *dev,
229 u64 in_param,
230 u64 out_param,
231 u32 in_modifier,
232 u8 op_modifier,
233 u16 op,
234 u16 token,
235 int event)
236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (event) {
238 unsigned long end = jiffies + GO_BIT_TIMEOUT;
239
240 while (go_bit(dev) && time_before(jiffies, end)) {
241 set_current_state(TASK_RUNNING);
242 schedule();
243 }
244 }
245
Eli Cohen14abdff2006-02-26 14:36:06 -0800246 if (go_bit(dev))
247 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /*
250 * We use writel (instead of something like memcpy_toio)
251 * because writes of less than 32 bits to the HCR don't work
252 * (and some architectures such as ia64 implement memcpy_toio
253 * in terms of writeb).
254 */
Sean Hefty97f52eb2005-08-13 21:05:57 -0700255 __raw_writel((__force u32) cpu_to_be32(in_param >> 32), dev->hcr + 0 * 4);
256 __raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful), dev->hcr + 1 * 4);
257 __raw_writel((__force u32) cpu_to_be32(in_modifier), dev->hcr + 2 * 4);
258 __raw_writel((__force u32) cpu_to_be32(out_param >> 32), dev->hcr + 3 * 4);
259 __raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), dev->hcr + 4 * 4);
260 __raw_writel((__force u32) cpu_to_be32(token << 16), dev->hcr + 5 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* __raw_writel may not order writes. */
263 wmb();
264
Sean Hefty97f52eb2005-08-13 21:05:57 -0700265 __raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT) |
266 (event ? (1 << HCA_E_BIT) : 0) |
267 (op_modifier << HCR_OPMOD_SHIFT) |
268 op), dev->hcr + 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Eli Cohen14abdff2006-02-26 14:36:06 -0800270 return 0;
271}
272
273static int mthca_cmd_post(struct mthca_dev *dev,
274 u64 in_param,
275 u64 out_param,
276 u32 in_modifier,
277 u8 op_modifier,
278 u16 op,
279 u16 token,
280 int event)
281{
282 int err = 0;
283
284 mutex_lock(&dev->cmd.hcr_mutex);
285
286 if (event && dev->cmd.flags & MTHCA_CMD_POST_DOORBELLS && fw_cmd_doorbell)
287 mthca_cmd_post_dbell(dev, in_param, out_param, in_modifier,
288 op_modifier, op, token);
289 else
290 err = mthca_cmd_post_hcr(dev, in_param, out_param, in_modifier,
291 op_modifier, op, token, event);
292
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800293 mutex_unlock(&dev->cmd.hcr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return err;
295}
296
297static int mthca_cmd_poll(struct mthca_dev *dev,
298 u64 in_param,
299 u64 *out_param,
300 int out_is_imm,
301 u32 in_modifier,
302 u8 op_modifier,
303 u16 op,
304 unsigned long timeout,
305 u8 *status)
306{
307 int err = 0;
308 unsigned long end;
309
Michael S. Tsirkine3aa31c2006-01-30 16:22:29 -0800310 down(&dev->cmd.poll_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 err = mthca_cmd_post(dev, in_param,
313 out_param ? *out_param : 0,
314 in_modifier, op_modifier,
315 op, CMD_POLL_TOKEN, 0);
316 if (err)
317 goto out;
318
319 end = timeout + jiffies;
320 while (go_bit(dev) && time_before(jiffies, end)) {
321 set_current_state(TASK_RUNNING);
322 schedule();
323 }
324
325 if (go_bit(dev)) {
326 err = -EBUSY;
327 goto out;
328 }
329
Sean Hefty97f52eb2005-08-13 21:05:57 -0700330 if (out_is_imm)
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800331 *out_param =
Sean Hefty97f52eb2005-08-13 21:05:57 -0700332 (u64) be32_to_cpu((__force __be32)
333 __raw_readl(dev->hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
334 (u64) be32_to_cpu((__force __be32)
335 __raw_readl(dev->hcr + HCR_OUT_PARAM_OFFSET + 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Sean Hefty97f52eb2005-08-13 21:05:57 -0700337 *status = be32_to_cpu((__force __be32) __raw_readl(dev->hcr + HCR_STATUS_OFFSET)) >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339out:
340 up(&dev->cmd.poll_sem);
341 return err;
342}
343
344void mthca_cmd_event(struct mthca_dev *dev,
345 u16 token,
346 u8 status,
347 u64 out_param)
348{
349 struct mthca_cmd_context *context =
350 &dev->cmd.context[token & dev->cmd.token_mask];
351
352 /* previously timed out command completing at long last */
353 if (token != context->token)
354 return;
355
356 context->result = 0;
357 context->status = status;
358 context->out_param = out_param;
359
360 context->token += dev->cmd.token_mask + 1;
361
362 complete(&context->done);
363}
364
365static void event_timeout(unsigned long context_ptr)
366{
367 struct mthca_cmd_context *context =
368 (struct mthca_cmd_context *) context_ptr;
369
370 context->result = -EBUSY;
371 complete(&context->done);
372}
373
374static int mthca_cmd_wait(struct mthca_dev *dev,
375 u64 in_param,
376 u64 *out_param,
377 int out_is_imm,
378 u32 in_modifier,
379 u8 op_modifier,
380 u16 op,
381 unsigned long timeout,
382 u8 *status)
383{
384 int err = 0;
385 struct mthca_cmd_context *context;
386
Michael S. Tsirkine3aa31c2006-01-30 16:22:29 -0800387 down(&dev->cmd.event_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 spin_lock(&dev->cmd.context_lock);
390 BUG_ON(dev->cmd.free_head < 0);
391 context = &dev->cmd.context[dev->cmd.free_head];
392 dev->cmd.free_head = context->next;
393 spin_unlock(&dev->cmd.context_lock);
394
395 init_completion(&context->done);
396
397 err = mthca_cmd_post(dev, in_param,
398 out_param ? *out_param : 0,
399 in_modifier, op_modifier,
400 op, context->token, 1);
401 if (err)
402 goto out;
403
404 context->timer.expires = jiffies + timeout;
405 add_timer(&context->timer);
406
407 wait_for_completion(&context->done);
408 del_timer_sync(&context->timer);
409
410 err = context->result;
411 if (err)
412 goto out;
413
414 *status = context->status;
415 if (*status)
416 mthca_dbg(dev, "Command %02x completed with status %02x\n",
417 op, *status);
418
419 if (out_is_imm)
420 *out_param = context->out_param;
421
422out:
423 spin_lock(&dev->cmd.context_lock);
424 context->next = dev->cmd.free_head;
425 dev->cmd.free_head = context - dev->cmd.context;
426 spin_unlock(&dev->cmd.context_lock);
427
428 up(&dev->cmd.event_sem);
429 return err;
430}
431
432/* Invoke a command with an output mailbox */
433static int mthca_cmd_box(struct mthca_dev *dev,
434 u64 in_param,
435 u64 out_param,
436 u32 in_modifier,
437 u8 op_modifier,
438 u16 op,
439 unsigned long timeout,
440 u8 *status)
441{
Eli Cohen14abdff2006-02-26 14:36:06 -0800442 if (dev->cmd.flags & MTHCA_CMD_USE_EVENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return mthca_cmd_wait(dev, in_param, &out_param, 0,
444 in_modifier, op_modifier, op,
445 timeout, status);
446 else
447 return mthca_cmd_poll(dev, in_param, &out_param, 0,
448 in_modifier, op_modifier, op,
449 timeout, status);
450}
451
452/* Invoke a command with no output parameter */
453static int mthca_cmd(struct mthca_dev *dev,
454 u64 in_param,
455 u32 in_modifier,
456 u8 op_modifier,
457 u16 op,
458 unsigned long timeout,
459 u8 *status)
460{
461 return mthca_cmd_box(dev, in_param, 0, in_modifier,
462 op_modifier, op, timeout, status);
463}
464
465/*
466 * Invoke a command with an immediate output parameter (and copy the
467 * output into the caller's out_param pointer after the command
468 * executes).
469 */
470static int mthca_cmd_imm(struct mthca_dev *dev,
471 u64 in_param,
472 u64 *out_param,
473 u32 in_modifier,
474 u8 op_modifier,
475 u16 op,
476 unsigned long timeout,
477 u8 *status)
478{
Eli Cohen14abdff2006-02-26 14:36:06 -0800479 if (dev->cmd.flags & MTHCA_CMD_USE_EVENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return mthca_cmd_wait(dev, in_param, out_param, 1,
481 in_modifier, op_modifier, op,
482 timeout, status);
483 else
484 return mthca_cmd_poll(dev, in_param, out_param, 1,
485 in_modifier, op_modifier, op,
486 timeout, status);
487}
488
Roland Dreier80fd8232005-06-27 14:36:45 -0700489int mthca_cmd_init(struct mthca_dev *dev)
490{
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800491 mutex_init(&dev->cmd.hcr_mutex);
Roland Dreier80fd8232005-06-27 14:36:45 -0700492 sema_init(&dev->cmd.poll_sem, 1);
Eli Cohen14abdff2006-02-26 14:36:06 -0800493 dev->cmd.flags = 0;
Roland Dreier80fd8232005-06-27 14:36:45 -0700494
495 dev->hcr = ioremap(pci_resource_start(dev->pdev, 0) + MTHCA_HCR_BASE,
496 MTHCA_HCR_SIZE);
497 if (!dev->hcr) {
498 mthca_err(dev, "Couldn't map command register.");
499 return -ENOMEM;
500 }
501
Roland Dreiered878452005-06-27 14:36:45 -0700502 dev->cmd.pool = pci_pool_create("mthca_cmd", dev->pdev,
503 MTHCA_MAILBOX_SIZE,
504 MTHCA_MAILBOX_SIZE, 0);
505 if (!dev->cmd.pool) {
506 iounmap(dev->hcr);
507 return -ENOMEM;
508 }
509
Roland Dreier80fd8232005-06-27 14:36:45 -0700510 return 0;
511}
512
513void mthca_cmd_cleanup(struct mthca_dev *dev)
514{
Roland Dreiered878452005-06-27 14:36:45 -0700515 pci_pool_destroy(dev->cmd.pool);
Roland Dreier80fd8232005-06-27 14:36:45 -0700516 iounmap(dev->hcr);
Eli Cohen14abdff2006-02-26 14:36:06 -0800517 if (dev->cmd.flags & MTHCA_CMD_POST_DOORBELLS)
518 iounmap(dev->cmd.dbell_map);
Roland Dreier80fd8232005-06-27 14:36:45 -0700519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
522 * Switch to using events to issue FW commands (should be called after
523 * event queue to command events has been initialized).
524 */
525int mthca_cmd_use_events(struct mthca_dev *dev)
526{
527 int i;
528
529 dev->cmd.context = kmalloc(dev->cmd.max_cmds *
530 sizeof (struct mthca_cmd_context),
531 GFP_KERNEL);
532 if (!dev->cmd.context)
533 return -ENOMEM;
534
535 for (i = 0; i < dev->cmd.max_cmds; ++i) {
536 dev->cmd.context[i].token = i;
537 dev->cmd.context[i].next = i + 1;
538 init_timer(&dev->cmd.context[i].timer);
539 dev->cmd.context[i].timer.data =
540 (unsigned long) &dev->cmd.context[i];
541 dev->cmd.context[i].timer.function = event_timeout;
542 }
543
544 dev->cmd.context[dev->cmd.max_cmds - 1].next = -1;
545 dev->cmd.free_head = 0;
546
547 sema_init(&dev->cmd.event_sem, dev->cmd.max_cmds);
548 spin_lock_init(&dev->cmd.context_lock);
549
550 for (dev->cmd.token_mask = 1;
551 dev->cmd.token_mask < dev->cmd.max_cmds;
552 dev->cmd.token_mask <<= 1)
553 ; /* nothing */
554 --dev->cmd.token_mask;
555
Eli Cohen14abdff2006-02-26 14:36:06 -0800556 dev->cmd.flags |= MTHCA_CMD_USE_EVENTS;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 down(&dev->cmd.poll_sem);
559
560 return 0;
561}
562
563/*
564 * Switch back to polling (used when shutting down the device)
565 */
566void mthca_cmd_use_polling(struct mthca_dev *dev)
567{
568 int i;
569
Eli Cohen14abdff2006-02-26 14:36:06 -0800570 dev->cmd.flags &= ~MTHCA_CMD_USE_EVENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 for (i = 0; i < dev->cmd.max_cmds; ++i)
573 down(&dev->cmd.event_sem);
574
575 kfree(dev->cmd.context);
576
577 up(&dev->cmd.poll_sem);
578}
579
Roland Dreiered878452005-06-27 14:36:45 -0700580struct mthca_mailbox *mthca_alloc_mailbox(struct mthca_dev *dev,
Al Viro87b750d2005-10-21 03:22:13 -0400581 gfp_t gfp_mask)
Roland Dreiered878452005-06-27 14:36:45 -0700582{
583 struct mthca_mailbox *mailbox;
584
585 mailbox = kmalloc(sizeof *mailbox, gfp_mask);
586 if (!mailbox)
587 return ERR_PTR(-ENOMEM);
588
589 mailbox->buf = pci_pool_alloc(dev->cmd.pool, gfp_mask, &mailbox->dma);
590 if (!mailbox->buf) {
591 kfree(mailbox);
592 return ERR_PTR(-ENOMEM);
593 }
594
595 return mailbox;
596}
597
598void mthca_free_mailbox(struct mthca_dev *dev, struct mthca_mailbox *mailbox)
599{
600 if (!mailbox)
601 return;
602
603 pci_pool_free(dev->cmd.pool, mailbox->buf, mailbox->dma);
604 kfree(mailbox);
605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607int mthca_SYS_EN(struct mthca_dev *dev, u8 *status)
608{
609 u64 out;
610 int ret;
611
612 ret = mthca_cmd_imm(dev, 0, &out, 0, 0, CMD_SYS_EN, HZ, status);
613
614 if (*status == MTHCA_CMD_STAT_DDR_MEM_ERR)
615 mthca_warn(dev, "SYS_EN DDR error: syn=%x, sock=%d, "
616 "sladdr=%d, SPD source=%s\n",
617 (int) (out >> 6) & 0xf, (int) (out >> 4) & 3,
618 (int) (out >> 1) & 7, (int) out & 1 ? "NVMEM" : "DIMM");
619
620 return ret;
621}
622
623int mthca_SYS_DIS(struct mthca_dev *dev, u8 *status)
624{
625 return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, HZ, status);
626}
627
628static int mthca_map_cmd(struct mthca_dev *dev, u16 op, struct mthca_icm *icm,
629 u64 virt, u8 *status)
630{
Roland Dreiered878452005-06-27 14:36:45 -0700631 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct mthca_icm_iter iter;
Roland Dreiered878452005-06-27 14:36:45 -0700633 __be64 *pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int lg;
635 int nent = 0;
636 int i;
637 int err = 0;
638 int ts = 0, tc = 0;
639
Roland Dreiered878452005-06-27 14:36:45 -0700640 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
641 if (IS_ERR(mailbox))
642 return PTR_ERR(mailbox);
643 memset(mailbox->buf, 0, MTHCA_MAILBOX_SIZE);
644 pages = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 for (mthca_icm_first(icm, &iter);
647 !mthca_icm_last(&iter);
648 mthca_icm_next(&iter)) {
649 /*
650 * We have to pass pages that are aligned to their
651 * size, so find the least significant 1 in the
652 * address or size and use that as our log2 size.
653 */
654 lg = ffs(mthca_icm_addr(&iter) | mthca_icm_size(&iter)) - 1;
655 if (lg < 12) {
656 mthca_warn(dev, "Got FW area not aligned to 4K (%llx/%lx).\n",
657 (unsigned long long) mthca_icm_addr(&iter),
658 mthca_icm_size(&iter));
659 err = -EINVAL;
660 goto out;
661 }
Ishai Rabinovitz59f174f2006-01-12 15:24:51 -0800662 for (i = 0; i < mthca_icm_size(&iter) >> lg; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (virt != -1) {
Roland Dreiered878452005-06-27 14:36:45 -0700664 pages[nent * 2] = cpu_to_be64(virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 virt += 1 << lg;
666 }
667
Roland Dreiered878452005-06-27 14:36:45 -0700668 pages[nent * 2 + 1] = cpu_to_be64((mthca_icm_addr(&iter) +
669 (i << lg)) | (lg - 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ts += 1 << (lg - 10);
671 ++tc;
672
Michael S. Tsirkin44dd8232005-09-26 09:42:09 -0700673 if (++nent == MTHCA_MAILBOX_SIZE / 16) {
Roland Dreiered878452005-06-27 14:36:45 -0700674 err = mthca_cmd(dev, mailbox->dma, nent, 0, op,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 CMD_TIME_CLASS_B, status);
676 if (err || *status)
677 goto out;
678 nent = 0;
679 }
680 }
681 }
682
683 if (nent)
Roland Dreiered878452005-06-27 14:36:45 -0700684 err = mthca_cmd(dev, mailbox->dma, nent, 0, op,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 CMD_TIME_CLASS_B, status);
686
687 switch (op) {
688 case CMD_MAP_FA:
689 mthca_dbg(dev, "Mapped %d chunks/%d KB for FW.\n", tc, ts);
690 break;
691 case CMD_MAP_ICM_AUX:
692 mthca_dbg(dev, "Mapped %d chunks/%d KB for ICM aux.\n", tc, ts);
693 break;
694 case CMD_MAP_ICM:
695 mthca_dbg(dev, "Mapped %d chunks/%d KB at %llx for ICM.\n",
696 tc, ts, (unsigned long long) virt - (ts << 10));
697 break;
698 }
699
700out:
Roland Dreiered878452005-06-27 14:36:45 -0700701 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return err;
703}
704
705int mthca_MAP_FA(struct mthca_dev *dev, struct mthca_icm *icm, u8 *status)
706{
707 return mthca_map_cmd(dev, CMD_MAP_FA, icm, -1, status);
708}
709
710int mthca_UNMAP_FA(struct mthca_dev *dev, u8 *status)
711{
712 return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_FA, CMD_TIME_CLASS_B, status);
713}
714
715int mthca_RUN_FW(struct mthca_dev *dev, u8 *status)
716{
717 return mthca_cmd(dev, 0, 0, 0, CMD_RUN_FW, CMD_TIME_CLASS_A, status);
718}
719
Eli Cohen14abdff2006-02-26 14:36:06 -0800720static void mthca_setup_cmd_doorbells(struct mthca_dev *dev, u64 base)
721{
722 unsigned long addr;
723 u16 max_off = 0;
724 int i;
725
726 for (i = 0; i < 8; ++i)
727 max_off = max(max_off, dev->cmd.dbell_offsets[i]);
728
729 if ((base & PAGE_MASK) != ((base + max_off) & PAGE_MASK)) {
730 mthca_warn(dev, "Firmware doorbell region at 0x%016llx, "
731 "length 0x%x crosses a page boundary\n",
732 (unsigned long long) base, max_off);
733 return;
734 }
735
736 addr = pci_resource_start(dev->pdev, 2) +
737 ((pci_resource_len(dev->pdev, 2) - 1) & base);
738 dev->cmd.dbell_map = ioremap(addr, max_off + sizeof(u32));
739 if (!dev->cmd.dbell_map)
740 return;
741
742 dev->cmd.flags |= MTHCA_CMD_POST_DOORBELLS;
743 mthca_dbg(dev, "Mapped doorbell page for posting FW commands\n");
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746int mthca_QUERY_FW(struct mthca_dev *dev, u8 *status)
747{
Roland Dreiered878452005-06-27 14:36:45 -0700748 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 u32 *outbox;
Eli Cohen14abdff2006-02-26 14:36:06 -0800750 u64 base;
751 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int err = 0;
753 u8 lg;
Eli Cohen14abdff2006-02-26 14:36:06 -0800754 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756#define QUERY_FW_OUT_SIZE 0x100
757#define QUERY_FW_VER_OFFSET 0x00
758#define QUERY_FW_MAX_CMD_OFFSET 0x0f
759#define QUERY_FW_ERR_START_OFFSET 0x30
760#define QUERY_FW_ERR_SIZE_OFFSET 0x38
761
Eli Cohen14abdff2006-02-26 14:36:06 -0800762#define QUERY_FW_CMD_DB_EN_OFFSET 0x10
763#define QUERY_FW_CMD_DB_OFFSET 0x50
764#define QUERY_FW_CMD_DB_BASE 0x60
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766#define QUERY_FW_START_OFFSET 0x20
767#define QUERY_FW_END_OFFSET 0x28
768
769#define QUERY_FW_SIZE_OFFSET 0x00
770#define QUERY_FW_CLR_INT_BASE_OFFSET 0x20
771#define QUERY_FW_EQ_ARM_BASE_OFFSET 0x40
772#define QUERY_FW_EQ_SET_CI_BASE_OFFSET 0x48
773
Roland Dreiered878452005-06-27 14:36:45 -0700774 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
775 if (IS_ERR(mailbox))
776 return PTR_ERR(mailbox);
777 outbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Roland Dreiered878452005-06-27 14:36:45 -0700779 err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_FW,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 CMD_TIME_CLASS_A, status);
781
782 if (err)
783 goto out;
784
785 MTHCA_GET(dev->fw_ver, outbox, QUERY_FW_VER_OFFSET);
786 /*
787 * FW subminor version is at more signifant bits than minor
788 * version, so swap here.
789 */
790 dev->fw_ver = (dev->fw_ver & 0xffff00000000ull) |
791 ((dev->fw_ver & 0xffff0000ull) >> 16) |
792 ((dev->fw_ver & 0x0000ffffull) << 16);
793
Eli Cohen14abdff2006-02-26 14:36:06 -0800794 mthca_dbg(dev, "FW version %012llx, max commands %d\n",
795 (unsigned long long) dev->fw_ver, dev->cmd.max_cmds);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 MTHCA_GET(lg, outbox, QUERY_FW_MAX_CMD_OFFSET);
798 dev->cmd.max_cmds = 1 << lg;
Roland Dreier3d155f82005-10-27 11:03:38 -0700799 MTHCA_GET(dev->catas_err.addr, outbox, QUERY_FW_ERR_START_OFFSET);
800 MTHCA_GET(dev->catas_err.size, outbox, QUERY_FW_ERR_SIZE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Roland Dreier3d155f82005-10-27 11:03:38 -0700802 mthca_dbg(dev, "Catastrophic error buffer at 0x%llx, size 0x%x\n",
803 (unsigned long long) dev->catas_err.addr, dev->catas_err.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Eli Cohen14abdff2006-02-26 14:36:06 -0800805 MTHCA_GET(tmp, outbox, QUERY_FW_CMD_DB_EN_OFFSET);
806 if (tmp & 0x1) {
807 mthca_dbg(dev, "FW supports commands through doorbells\n");
808
809 MTHCA_GET(base, outbox, QUERY_FW_CMD_DB_BASE);
810 for (i = 0; i < MTHCA_CMD_NUM_DBELL_DWORDS; ++i)
811 MTHCA_GET(dev->cmd.dbell_offsets[i], outbox,
812 QUERY_FW_CMD_DB_OFFSET + (i << 1));
813
814 mthca_setup_cmd_doorbells(dev, base);
815 }
816
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700817 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 MTHCA_GET(dev->fw.arbel.fw_pages, outbox, QUERY_FW_SIZE_OFFSET);
819 MTHCA_GET(dev->fw.arbel.clr_int_base, outbox, QUERY_FW_CLR_INT_BASE_OFFSET);
820 MTHCA_GET(dev->fw.arbel.eq_arm_base, outbox, QUERY_FW_EQ_ARM_BASE_OFFSET);
821 MTHCA_GET(dev->fw.arbel.eq_set_ci_base, outbox, QUERY_FW_EQ_SET_CI_BASE_OFFSET);
822 mthca_dbg(dev, "FW size %d KB\n", dev->fw.arbel.fw_pages << 2);
823
824 /*
825 * Arbel page size is always 4 KB; round up number of
826 * system pages needed.
827 */
828 dev->fw.arbel.fw_pages =
Michael S. Tsirkinc063a062006-01-12 15:43:58 -0800829 ALIGN(dev->fw.arbel.fw_pages, PAGE_SIZE >> 12) >>
830 (PAGE_SHIFT - 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 mthca_dbg(dev, "Clear int @ %llx, EQ arm @ %llx, EQ set CI @ %llx\n",
833 (unsigned long long) dev->fw.arbel.clr_int_base,
834 (unsigned long long) dev->fw.arbel.eq_arm_base,
835 (unsigned long long) dev->fw.arbel.eq_set_ci_base);
836 } else {
837 MTHCA_GET(dev->fw.tavor.fw_start, outbox, QUERY_FW_START_OFFSET);
838 MTHCA_GET(dev->fw.tavor.fw_end, outbox, QUERY_FW_END_OFFSET);
839
840 mthca_dbg(dev, "FW size %d KB (start %llx, end %llx)\n",
841 (int) ((dev->fw.tavor.fw_end - dev->fw.tavor.fw_start) >> 10),
842 (unsigned long long) dev->fw.tavor.fw_start,
843 (unsigned long long) dev->fw.tavor.fw_end);
844 }
845
846out:
Roland Dreiered878452005-06-27 14:36:45 -0700847 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return err;
849}
850
851int mthca_ENABLE_LAM(struct mthca_dev *dev, u8 *status)
852{
Roland Dreiered878452005-06-27 14:36:45 -0700853 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 u8 info;
855 u32 *outbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int err = 0;
857
858#define ENABLE_LAM_OUT_SIZE 0x100
859#define ENABLE_LAM_START_OFFSET 0x00
860#define ENABLE_LAM_END_OFFSET 0x08
861#define ENABLE_LAM_INFO_OFFSET 0x13
862
863#define ENABLE_LAM_INFO_HIDDEN_FLAG (1 << 4)
864#define ENABLE_LAM_INFO_ECC_MASK 0x3
865
Roland Dreiered878452005-06-27 14:36:45 -0700866 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
867 if (IS_ERR(mailbox))
868 return PTR_ERR(mailbox);
869 outbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Roland Dreiered878452005-06-27 14:36:45 -0700871 err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_ENABLE_LAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 CMD_TIME_CLASS_C, status);
873
874 if (err)
875 goto out;
876
877 if (*status == MTHCA_CMD_STAT_LAM_NOT_PRE)
878 goto out;
879
880 MTHCA_GET(dev->ddr_start, outbox, ENABLE_LAM_START_OFFSET);
881 MTHCA_GET(dev->ddr_end, outbox, ENABLE_LAM_END_OFFSET);
882 MTHCA_GET(info, outbox, ENABLE_LAM_INFO_OFFSET);
883
884 if (!!(info & ENABLE_LAM_INFO_HIDDEN_FLAG) !=
885 !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
886 mthca_info(dev, "FW reports that HCA-attached memory "
887 "is %s hidden; does not match PCI config\n",
888 (info & ENABLE_LAM_INFO_HIDDEN_FLAG) ?
889 "" : "not");
890 }
891 if (info & ENABLE_LAM_INFO_HIDDEN_FLAG)
892 mthca_dbg(dev, "HCA-attached memory is hidden.\n");
893
894 mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
895 (int) ((dev->ddr_end - dev->ddr_start) >> 10),
896 (unsigned long long) dev->ddr_start,
897 (unsigned long long) dev->ddr_end);
898
899out:
Roland Dreiered878452005-06-27 14:36:45 -0700900 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return err;
902}
903
904int mthca_DISABLE_LAM(struct mthca_dev *dev, u8 *status)
905{
906 return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, CMD_TIME_CLASS_C, status);
907}
908
909int mthca_QUERY_DDR(struct mthca_dev *dev, u8 *status)
910{
Roland Dreiered878452005-06-27 14:36:45 -0700911 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 u8 info;
913 u32 *outbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int err = 0;
915
916#define QUERY_DDR_OUT_SIZE 0x100
917#define QUERY_DDR_START_OFFSET 0x00
918#define QUERY_DDR_END_OFFSET 0x08
919#define QUERY_DDR_INFO_OFFSET 0x13
920
921#define QUERY_DDR_INFO_HIDDEN_FLAG (1 << 4)
922#define QUERY_DDR_INFO_ECC_MASK 0x3
923
Roland Dreiered878452005-06-27 14:36:45 -0700924 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
925 if (IS_ERR(mailbox))
926 return PTR_ERR(mailbox);
927 outbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Roland Dreiered878452005-06-27 14:36:45 -0700929 err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_DDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 CMD_TIME_CLASS_A, status);
931
932 if (err)
933 goto out;
934
935 MTHCA_GET(dev->ddr_start, outbox, QUERY_DDR_START_OFFSET);
936 MTHCA_GET(dev->ddr_end, outbox, QUERY_DDR_END_OFFSET);
937 MTHCA_GET(info, outbox, QUERY_DDR_INFO_OFFSET);
938
939 if (!!(info & QUERY_DDR_INFO_HIDDEN_FLAG) !=
940 !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
941 mthca_info(dev, "FW reports that HCA-attached memory "
942 "is %s hidden; does not match PCI config\n",
943 (info & QUERY_DDR_INFO_HIDDEN_FLAG) ?
944 "" : "not");
945 }
946 if (info & QUERY_DDR_INFO_HIDDEN_FLAG)
947 mthca_dbg(dev, "HCA-attached memory is hidden.\n");
948
949 mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
950 (int) ((dev->ddr_end - dev->ddr_start) >> 10),
951 (unsigned long long) dev->ddr_start,
952 (unsigned long long) dev->ddr_end);
953
954out:
Roland Dreiered878452005-06-27 14:36:45 -0700955 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return err;
957}
958
959int mthca_QUERY_DEV_LIM(struct mthca_dev *dev,
960 struct mthca_dev_lim *dev_lim, u8 *status)
961{
Roland Dreiered878452005-06-27 14:36:45 -0700962 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 u32 *outbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 u8 field;
965 u16 size;
966 int err;
967
968#define QUERY_DEV_LIM_OUT_SIZE 0x100
969#define QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET 0x10
970#define QUERY_DEV_LIM_MAX_QP_SZ_OFFSET 0x11
971#define QUERY_DEV_LIM_RSVD_QP_OFFSET 0x12
972#define QUERY_DEV_LIM_MAX_QP_OFFSET 0x13
973#define QUERY_DEV_LIM_RSVD_SRQ_OFFSET 0x14
974#define QUERY_DEV_LIM_MAX_SRQ_OFFSET 0x15
975#define QUERY_DEV_LIM_RSVD_EEC_OFFSET 0x16
976#define QUERY_DEV_LIM_MAX_EEC_OFFSET 0x17
977#define QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET 0x19
978#define QUERY_DEV_LIM_RSVD_CQ_OFFSET 0x1a
979#define QUERY_DEV_LIM_MAX_CQ_OFFSET 0x1b
980#define QUERY_DEV_LIM_MAX_MPT_OFFSET 0x1d
981#define QUERY_DEV_LIM_RSVD_EQ_OFFSET 0x1e
982#define QUERY_DEV_LIM_MAX_EQ_OFFSET 0x1f
983#define QUERY_DEV_LIM_RSVD_MTT_OFFSET 0x20
984#define QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET 0x21
985#define QUERY_DEV_LIM_RSVD_MRW_OFFSET 0x22
986#define QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET 0x23
987#define QUERY_DEV_LIM_MAX_AV_OFFSET 0x27
988#define QUERY_DEV_LIM_MAX_REQ_QP_OFFSET 0x29
989#define QUERY_DEV_LIM_MAX_RES_QP_OFFSET 0x2b
990#define QUERY_DEV_LIM_MAX_RDMA_OFFSET 0x2f
991#define QUERY_DEV_LIM_RSZ_SRQ_OFFSET 0x33
992#define QUERY_DEV_LIM_ACK_DELAY_OFFSET 0x35
993#define QUERY_DEV_LIM_MTU_WIDTH_OFFSET 0x36
994#define QUERY_DEV_LIM_VL_PORT_OFFSET 0x37
995#define QUERY_DEV_LIM_MAX_GID_OFFSET 0x3b
996#define QUERY_DEV_LIM_MAX_PKEY_OFFSET 0x3f
997#define QUERY_DEV_LIM_FLAGS_OFFSET 0x44
998#define QUERY_DEV_LIM_RSVD_UAR_OFFSET 0x48
999#define QUERY_DEV_LIM_UAR_SZ_OFFSET 0x49
1000#define QUERY_DEV_LIM_PAGE_SZ_OFFSET 0x4b
1001#define QUERY_DEV_LIM_MAX_SG_OFFSET 0x51
1002#define QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET 0x52
1003#define QUERY_DEV_LIM_MAX_SG_RQ_OFFSET 0x55
1004#define QUERY_DEV_LIM_MAX_DESC_SZ_RQ_OFFSET 0x56
1005#define QUERY_DEV_LIM_MAX_QP_MCG_OFFSET 0x61
1006#define QUERY_DEV_LIM_RSVD_MCG_OFFSET 0x62
1007#define QUERY_DEV_LIM_MAX_MCG_OFFSET 0x63
1008#define QUERY_DEV_LIM_RSVD_PD_OFFSET 0x64
1009#define QUERY_DEV_LIM_MAX_PD_OFFSET 0x65
1010#define QUERY_DEV_LIM_RSVD_RDD_OFFSET 0x66
1011#define QUERY_DEV_LIM_MAX_RDD_OFFSET 0x67
1012#define QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET 0x80
1013#define QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET 0x82
1014#define QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET 0x84
1015#define QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET 0x86
1016#define QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET 0x88
1017#define QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET 0x8a
1018#define QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET 0x8c
1019#define QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET 0x8e
1020#define QUERY_DEV_LIM_MTT_ENTRY_SZ_OFFSET 0x90
1021#define QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET 0x92
1022#define QUERY_DEV_LIM_PBL_SZ_OFFSET 0x96
1023#define QUERY_DEV_LIM_BMME_FLAGS_OFFSET 0x97
1024#define QUERY_DEV_LIM_RSVD_LKEY_OFFSET 0x98
1025#define QUERY_DEV_LIM_LAMR_OFFSET 0x9f
1026#define QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET 0xa0
1027
Roland Dreiered878452005-06-27 14:36:45 -07001028 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1029 if (IS_ERR(mailbox))
1030 return PTR_ERR(mailbox);
1031 outbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Roland Dreiered878452005-06-27 14:36:45 -07001033 err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_DEV_LIM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 CMD_TIME_CLASS_A, status);
1035
1036 if (err)
1037 goto out;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_QP_OFFSET);
1040 dev_lim->reserved_qps = 1 << (field & 0xf);
1041 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_OFFSET);
1042 dev_lim->max_qps = 1 << (field & 0x1f);
1043 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_SRQ_OFFSET);
1044 dev_lim->reserved_srqs = 1 << (field >> 4);
1045 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_OFFSET);
1046 dev_lim->max_srqs = 1 << (field & 0x1f);
1047 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EEC_OFFSET);
1048 dev_lim->reserved_eecs = 1 << (field & 0xf);
1049 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EEC_OFFSET);
1050 dev_lim->max_eecs = 1 << (field & 0x1f);
1051 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET);
1052 dev_lim->max_cq_sz = 1 << field;
1053 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_CQ_OFFSET);
1054 dev_lim->reserved_cqs = 1 << (field & 0xf);
1055 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_OFFSET);
1056 dev_lim->max_cqs = 1 << (field & 0x1f);
1057 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MPT_OFFSET);
1058 dev_lim->max_mpts = 1 << (field & 0x3f);
1059 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EQ_OFFSET);
1060 dev_lim->reserved_eqs = 1 << (field & 0xf);
1061 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EQ_OFFSET);
1062 dev_lim->max_eqs = 1 << (field & 0x7);
1063 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MTT_OFFSET);
1064 dev_lim->reserved_mtts = 1 << (field >> 4);
1065 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET);
1066 dev_lim->max_mrw_sz = 1 << field;
1067 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MRW_OFFSET);
1068 dev_lim->reserved_mrws = 1 << (field & 0xf);
1069 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET);
1070 dev_lim->max_mtt_seg = 1 << (field & 0x3f);
1071 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_REQ_QP_OFFSET);
1072 dev_lim->max_requester_per_qp = 1 << (field & 0x3f);
1073 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RES_QP_OFFSET);
1074 dev_lim->max_responder_per_qp = 1 << (field & 0x3f);
1075 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDMA_OFFSET);
1076 dev_lim->max_rdma_global = 1 << (field & 0x3f);
1077 MTHCA_GET(field, outbox, QUERY_DEV_LIM_ACK_DELAY_OFFSET);
1078 dev_lim->local_ca_ack_delay = field & 0x1f;
1079 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MTU_WIDTH_OFFSET);
1080 dev_lim->max_mtu = field >> 4;
1081 dev_lim->max_port_width = field & 0xf;
1082 MTHCA_GET(field, outbox, QUERY_DEV_LIM_VL_PORT_OFFSET);
1083 dev_lim->max_vl = field >> 4;
1084 dev_lim->num_ports = field & 0xf;
1085 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_GID_OFFSET);
1086 dev_lim->max_gids = 1 << (field & 0xf);
1087 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PKEY_OFFSET);
1088 dev_lim->max_pkeys = 1 << (field & 0xf);
1089 MTHCA_GET(dev_lim->flags, outbox, QUERY_DEV_LIM_FLAGS_OFFSET);
1090 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_UAR_OFFSET);
1091 dev_lim->reserved_uars = field >> 4;
1092 MTHCA_GET(field, outbox, QUERY_DEV_LIM_UAR_SZ_OFFSET);
1093 dev_lim->uar_size = 1 << ((field & 0x3f) + 20);
1094 MTHCA_GET(field, outbox, QUERY_DEV_LIM_PAGE_SZ_OFFSET);
1095 dev_lim->min_page_sz = 1 << field;
1096 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_OFFSET);
1097 dev_lim->max_sg = field;
1098
1099 MTHCA_GET(size, outbox, QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET);
1100 dev_lim->max_desc_sz = size;
1101
1102 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_MCG_OFFSET);
1103 dev_lim->max_qp_per_mcg = 1 << field;
1104 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MCG_OFFSET);
1105 dev_lim->reserved_mgms = field & 0xf;
1106 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MCG_OFFSET);
1107 dev_lim->max_mcgs = 1 << field;
1108 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_PD_OFFSET);
1109 dev_lim->reserved_pds = field >> 4;
1110 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PD_OFFSET);
1111 dev_lim->max_pds = 1 << (field & 0x3f);
1112 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_RDD_OFFSET);
1113 dev_lim->reserved_rdds = field >> 4;
1114 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDD_OFFSET);
1115 dev_lim->max_rdds = 1 << (field & 0x3f);
1116
1117 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET);
1118 dev_lim->eec_entry_sz = size;
1119 MTHCA_GET(size, outbox, QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET);
1120 dev_lim->qpc_entry_sz = size;
1121 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET);
1122 dev_lim->eeec_entry_sz = size;
1123 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET);
1124 dev_lim->eqpc_entry_sz = size;
1125 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET);
1126 dev_lim->eqc_entry_sz = size;
1127 MTHCA_GET(size, outbox, QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET);
1128 dev_lim->cqc_entry_sz = size;
1129 MTHCA_GET(size, outbox, QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET);
1130 dev_lim->srq_entry_sz = size;
1131 MTHCA_GET(size, outbox, QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET);
1132 dev_lim->uar_scratch_entry_sz = size;
1133
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001134 if (mthca_is_memfree(dev)) {
Jack Morgensteina3c8ab42005-11-30 09:55:22 -08001135 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET);
1136 dev_lim->max_srq_sz = 1 << field;
1137 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_SZ_OFFSET);
1138 dev_lim->max_qp_sz = 1 << field;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSZ_SRQ_OFFSET);
1140 dev_lim->hca.arbel.resize_srq = field & 1;
Roland Dreier8cf2daf2005-04-16 15:26:14 -07001141 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_RQ_OFFSET);
1142 dev_lim->max_sg = min_t(int, field, dev_lim->max_sg);
Jack Morgenstein77369ed2005-11-09 11:26:07 -08001143 MTHCA_GET(size, outbox, QUERY_DEV_LIM_MAX_DESC_SZ_RQ_OFFSET);
1144 dev_lim->max_desc_sz = min_t(int, size, dev_lim->max_desc_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 MTHCA_GET(size, outbox, QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET);
1146 dev_lim->mpt_entry_sz = size;
1147 MTHCA_GET(field, outbox, QUERY_DEV_LIM_PBL_SZ_OFFSET);
1148 dev_lim->hca.arbel.max_pbl_sz = 1 << (field & 0x3f);
1149 MTHCA_GET(dev_lim->hca.arbel.bmme_flags, outbox,
1150 QUERY_DEV_LIM_BMME_FLAGS_OFFSET);
1151 MTHCA_GET(dev_lim->hca.arbel.reserved_lkey, outbox,
1152 QUERY_DEV_LIM_RSVD_LKEY_OFFSET);
1153 MTHCA_GET(field, outbox, QUERY_DEV_LIM_LAMR_OFFSET);
1154 dev_lim->hca.arbel.lam_required = field & 1;
1155 MTHCA_GET(dev_lim->hca.arbel.max_icm_sz, outbox,
1156 QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET);
1157
1158 if (dev_lim->hca.arbel.bmme_flags & 1)
1159 mthca_dbg(dev, "Base MM extensions: yes "
1160 "(flags %d, max PBL %d, rsvd L_Key %08x)\n",
1161 dev_lim->hca.arbel.bmme_flags,
1162 dev_lim->hca.arbel.max_pbl_sz,
1163 dev_lim->hca.arbel.reserved_lkey);
1164 else
1165 mthca_dbg(dev, "Base MM extensions: no\n");
1166
1167 mthca_dbg(dev, "Max ICM size %lld MB\n",
1168 (unsigned long long) dev_lim->hca.arbel.max_icm_sz >> 20);
1169 } else {
Jack Morgensteina3c8ab42005-11-30 09:55:22 -08001170 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET);
1171 dev_lim->max_srq_sz = (1 << field) - 1;
1172 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_SZ_OFFSET);
1173 dev_lim->max_qp_sz = (1 << field) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_AV_OFFSET);
1175 dev_lim->hca.tavor.max_avs = 1 << (field & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dev_lim->mpt_entry_sz = MTHCA_MPT_ENTRY_SIZE;
1177 }
1178
Roland Dreierf295c792006-02-10 18:02:44 -08001179 mthca_dbg(dev, "Max QPs: %d, reserved QPs: %d, entry size: %d\n",
1180 dev_lim->max_qps, dev_lim->reserved_qps, dev_lim->qpc_entry_sz);
1181 mthca_dbg(dev, "Max SRQs: %d, reserved SRQs: %d, entry size: %d\n",
1182 dev_lim->max_srqs, dev_lim->reserved_srqs, dev_lim->srq_entry_sz);
1183 mthca_dbg(dev, "Max CQs: %d, reserved CQs: %d, entry size: %d\n",
1184 dev_lim->max_cqs, dev_lim->reserved_cqs, dev_lim->cqc_entry_sz);
1185 mthca_dbg(dev, "Max EQs: %d, reserved EQs: %d, entry size: %d\n",
1186 dev_lim->max_eqs, dev_lim->reserved_eqs, dev_lim->eqc_entry_sz);
1187 mthca_dbg(dev, "reserved MPTs: %d, reserved MTTs: %d\n",
1188 dev_lim->reserved_mrws, dev_lim->reserved_mtts);
1189 mthca_dbg(dev, "Max PDs: %d, reserved PDs: %d, reserved UARs: %d\n",
1190 dev_lim->max_pds, dev_lim->reserved_pds, dev_lim->reserved_uars);
1191 mthca_dbg(dev, "Max QP/MCG: %d, reserved MGMs: %d\n",
1192 dev_lim->max_pds, dev_lim->reserved_mgms);
1193 mthca_dbg(dev, "Max CQEs: %d, max WQEs: %d, max SRQ WQEs: %d\n",
1194 dev_lim->max_cq_sz, dev_lim->max_qp_sz, dev_lim->max_srq_sz);
1195
1196 mthca_dbg(dev, "Flags: %08x\n", dev_lim->flags);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198out:
Roland Dreiered878452005-06-27 14:36:45 -07001199 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return err;
1201}
1202
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -07001203static void get_board_id(void *vsd, char *board_id)
1204{
1205 int i;
1206
1207#define VSD_OFFSET_SIG1 0x00
1208#define VSD_OFFSET_SIG2 0xde
1209#define VSD_OFFSET_MLX_BOARD_ID 0xd0
1210#define VSD_OFFSET_TS_BOARD_ID 0x20
1211
1212#define VSD_SIGNATURE_TOPSPIN 0x5ad
1213
1214 memset(board_id, 0, MTHCA_BOARD_ID_LEN);
1215
1216 if (be16_to_cpup(vsd + VSD_OFFSET_SIG1) == VSD_SIGNATURE_TOPSPIN &&
1217 be16_to_cpup(vsd + VSD_OFFSET_SIG2) == VSD_SIGNATURE_TOPSPIN) {
1218 strlcpy(board_id, vsd + VSD_OFFSET_TS_BOARD_ID, MTHCA_BOARD_ID_LEN);
1219 } else {
1220 /*
1221 * The board ID is a string but the firmware byte
1222 * swaps each 4-byte word before passing it back to
1223 * us. Therefore we need to swab it before printing.
1224 */
1225 for (i = 0; i < 4; ++i)
1226 ((u32 *) board_id)[i] =
1227 swab32(*(u32 *) (vsd + VSD_OFFSET_MLX_BOARD_ID + i * 4));
1228 }
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231int mthca_QUERY_ADAPTER(struct mthca_dev *dev,
1232 struct mthca_adapter *adapter, u8 *status)
1233{
Roland Dreiered878452005-06-27 14:36:45 -07001234 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 u32 *outbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int err;
1237
1238#define QUERY_ADAPTER_OUT_SIZE 0x100
1239#define QUERY_ADAPTER_VENDOR_ID_OFFSET 0x00
1240#define QUERY_ADAPTER_DEVICE_ID_OFFSET 0x04
1241#define QUERY_ADAPTER_REVISION_ID_OFFSET 0x08
1242#define QUERY_ADAPTER_INTA_PIN_OFFSET 0x10
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -07001243#define QUERY_ADAPTER_VSD_OFFSET 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Roland Dreiered878452005-06-27 14:36:45 -07001245 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1246 if (IS_ERR(mailbox))
1247 return PTR_ERR(mailbox);
1248 outbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Roland Dreiered878452005-06-27 14:36:45 -07001250 err = mthca_cmd_box(dev, 0, mailbox->dma, 0, 0, CMD_QUERY_ADAPTER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 CMD_TIME_CLASS_A, status);
1252
1253 if (err)
1254 goto out;
1255
Roland Dreiered878452005-06-27 14:36:45 -07001256 MTHCA_GET(adapter->vendor_id, outbox, QUERY_ADAPTER_VENDOR_ID_OFFSET);
1257 MTHCA_GET(adapter->device_id, outbox, QUERY_ADAPTER_DEVICE_ID_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 MTHCA_GET(adapter->revision_id, outbox, QUERY_ADAPTER_REVISION_ID_OFFSET);
Roland Dreiered878452005-06-27 14:36:45 -07001259 MTHCA_GET(adapter->inta_pin, outbox, QUERY_ADAPTER_INTA_PIN_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -07001261 get_board_id(outbox + QUERY_ADAPTER_VSD_OFFSET / 4,
1262 adapter->board_id);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264out:
Roland Dreiered878452005-06-27 14:36:45 -07001265 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return err;
1267}
1268
1269int mthca_INIT_HCA(struct mthca_dev *dev,
1270 struct mthca_init_hca_param *param,
1271 u8 *status)
1272{
Roland Dreiered878452005-06-27 14:36:45 -07001273 struct mthca_mailbox *mailbox;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001274 __be32 *inbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int err;
1276
1277#define INIT_HCA_IN_SIZE 0x200
1278#define INIT_HCA_FLAGS_OFFSET 0x014
1279#define INIT_HCA_QPC_OFFSET 0x020
1280#define INIT_HCA_QPC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x10)
1281#define INIT_HCA_LOG_QP_OFFSET (INIT_HCA_QPC_OFFSET + 0x17)
1282#define INIT_HCA_EEC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x20)
1283#define INIT_HCA_LOG_EEC_OFFSET (INIT_HCA_QPC_OFFSET + 0x27)
1284#define INIT_HCA_SRQC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x28)
1285#define INIT_HCA_LOG_SRQ_OFFSET (INIT_HCA_QPC_OFFSET + 0x2f)
1286#define INIT_HCA_CQC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x30)
1287#define INIT_HCA_LOG_CQ_OFFSET (INIT_HCA_QPC_OFFSET + 0x37)
1288#define INIT_HCA_EQPC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x40)
1289#define INIT_HCA_EEEC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x50)
1290#define INIT_HCA_EQC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x60)
1291#define INIT_HCA_LOG_EQ_OFFSET (INIT_HCA_QPC_OFFSET + 0x67)
1292#define INIT_HCA_RDB_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x70)
1293#define INIT_HCA_UDAV_OFFSET 0x0b0
1294#define INIT_HCA_UDAV_LKEY_OFFSET (INIT_HCA_UDAV_OFFSET + 0x0)
1295#define INIT_HCA_UDAV_PD_OFFSET (INIT_HCA_UDAV_OFFSET + 0x4)
1296#define INIT_HCA_MCAST_OFFSET 0x0c0
1297#define INIT_HCA_MC_BASE_OFFSET (INIT_HCA_MCAST_OFFSET + 0x00)
1298#define INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x12)
1299#define INIT_HCA_MC_HASH_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x16)
1300#define INIT_HCA_LOG_MC_TABLE_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x1b)
1301#define INIT_HCA_TPT_OFFSET 0x0f0
1302#define INIT_HCA_MPT_BASE_OFFSET (INIT_HCA_TPT_OFFSET + 0x00)
1303#define INIT_HCA_MTT_SEG_SZ_OFFSET (INIT_HCA_TPT_OFFSET + 0x09)
1304#define INIT_HCA_LOG_MPT_SZ_OFFSET (INIT_HCA_TPT_OFFSET + 0x0b)
1305#define INIT_HCA_MTT_BASE_OFFSET (INIT_HCA_TPT_OFFSET + 0x10)
1306#define INIT_HCA_UAR_OFFSET 0x120
1307#define INIT_HCA_UAR_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x00)
1308#define INIT_HCA_UARC_SZ_OFFSET (INIT_HCA_UAR_OFFSET + 0x09)
1309#define INIT_HCA_LOG_UAR_SZ_OFFSET (INIT_HCA_UAR_OFFSET + 0x0a)
1310#define INIT_HCA_UAR_PAGE_SZ_OFFSET (INIT_HCA_UAR_OFFSET + 0x0b)
1311#define INIT_HCA_UAR_SCATCH_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x10)
1312#define INIT_HCA_UAR_CTX_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x18)
1313
Roland Dreiered878452005-06-27 14:36:45 -07001314 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1315 if (IS_ERR(mailbox))
1316 return PTR_ERR(mailbox);
1317 inbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 memset(inbox, 0, INIT_HCA_IN_SIZE);
1320
1321#if defined(__LITTLE_ENDIAN)
1322 *(inbox + INIT_HCA_FLAGS_OFFSET / 4) &= ~cpu_to_be32(1 << 1);
1323#elif defined(__BIG_ENDIAN)
1324 *(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 1);
1325#else
1326#error Host endianness not defined
1327#endif
1328 /* Check port for UD address vector: */
1329 *(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1);
1330
1331 /* We leave wqe_quota, responder_exu, etc as 0 (default) */
1332
1333 /* QPC/EEC/CQC/EQC/RDB attributes */
1334
1335 MTHCA_PUT(inbox, param->qpc_base, INIT_HCA_QPC_BASE_OFFSET);
1336 MTHCA_PUT(inbox, param->log_num_qps, INIT_HCA_LOG_QP_OFFSET);
1337 MTHCA_PUT(inbox, param->eec_base, INIT_HCA_EEC_BASE_OFFSET);
1338 MTHCA_PUT(inbox, param->log_num_eecs, INIT_HCA_LOG_EEC_OFFSET);
1339 MTHCA_PUT(inbox, param->srqc_base, INIT_HCA_SRQC_BASE_OFFSET);
1340 MTHCA_PUT(inbox, param->log_num_srqs, INIT_HCA_LOG_SRQ_OFFSET);
1341 MTHCA_PUT(inbox, param->cqc_base, INIT_HCA_CQC_BASE_OFFSET);
1342 MTHCA_PUT(inbox, param->log_num_cqs, INIT_HCA_LOG_CQ_OFFSET);
1343 MTHCA_PUT(inbox, param->eqpc_base, INIT_HCA_EQPC_BASE_OFFSET);
1344 MTHCA_PUT(inbox, param->eeec_base, INIT_HCA_EEEC_BASE_OFFSET);
1345 MTHCA_PUT(inbox, param->eqc_base, INIT_HCA_EQC_BASE_OFFSET);
1346 MTHCA_PUT(inbox, param->log_num_eqs, INIT_HCA_LOG_EQ_OFFSET);
1347 MTHCA_PUT(inbox, param->rdb_base, INIT_HCA_RDB_BASE_OFFSET);
1348
1349 /* UD AV attributes */
1350
1351 /* multicast attributes */
1352
1353 MTHCA_PUT(inbox, param->mc_base, INIT_HCA_MC_BASE_OFFSET);
1354 MTHCA_PUT(inbox, param->log_mc_entry_sz, INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET);
1355 MTHCA_PUT(inbox, param->mc_hash_sz, INIT_HCA_MC_HASH_SZ_OFFSET);
1356 MTHCA_PUT(inbox, param->log_mc_table_sz, INIT_HCA_LOG_MC_TABLE_SZ_OFFSET);
1357
1358 /* TPT attributes */
1359
1360 MTHCA_PUT(inbox, param->mpt_base, INIT_HCA_MPT_BASE_OFFSET);
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001361 if (!mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 MTHCA_PUT(inbox, param->mtt_seg_sz, INIT_HCA_MTT_SEG_SZ_OFFSET);
1363 MTHCA_PUT(inbox, param->log_mpt_sz, INIT_HCA_LOG_MPT_SZ_OFFSET);
1364 MTHCA_PUT(inbox, param->mtt_base, INIT_HCA_MTT_BASE_OFFSET);
1365
1366 /* UAR attributes */
1367 {
1368 u8 uar_page_sz = PAGE_SHIFT - 12;
1369 MTHCA_PUT(inbox, uar_page_sz, INIT_HCA_UAR_PAGE_SZ_OFFSET);
1370 }
1371
1372 MTHCA_PUT(inbox, param->uar_scratch_base, INIT_HCA_UAR_SCATCH_BASE_OFFSET);
1373
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001374 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 MTHCA_PUT(inbox, param->log_uarc_sz, INIT_HCA_UARC_SZ_OFFSET);
1376 MTHCA_PUT(inbox, param->log_uar_sz, INIT_HCA_LOG_UAR_SZ_OFFSET);
1377 MTHCA_PUT(inbox, param->uarc_base, INIT_HCA_UAR_CTX_BASE_OFFSET);
1378 }
1379
Roland Dreiered878452005-06-27 14:36:45 -07001380 err = mthca_cmd(dev, mailbox->dma, 0, 0, CMD_INIT_HCA, HZ, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Roland Dreiered878452005-06-27 14:36:45 -07001382 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return err;
1384}
1385
1386int mthca_INIT_IB(struct mthca_dev *dev,
1387 struct mthca_init_ib_param *param,
1388 int port, u8 *status)
1389{
Roland Dreiered878452005-06-27 14:36:45 -07001390 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 u32 *inbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int err;
1393 u32 flags;
1394
1395#define INIT_IB_IN_SIZE 56
1396#define INIT_IB_FLAGS_OFFSET 0x00
1397#define INIT_IB_FLAG_SIG (1 << 18)
1398#define INIT_IB_FLAG_NG (1 << 17)
1399#define INIT_IB_FLAG_G0 (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#define INIT_IB_VL_SHIFT 4
Roland Dreierda6561c2005-08-17 07:39:10 -07001401#define INIT_IB_PORT_WIDTH_SHIFT 8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402#define INIT_IB_MTU_SHIFT 12
1403#define INIT_IB_MAX_GID_OFFSET 0x06
1404#define INIT_IB_MAX_PKEY_OFFSET 0x0a
1405#define INIT_IB_GUID0_OFFSET 0x10
1406#define INIT_IB_NODE_GUID_OFFSET 0x18
1407#define INIT_IB_SI_GUID_OFFSET 0x20
1408
Roland Dreiered878452005-06-27 14:36:45 -07001409 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1410 if (IS_ERR(mailbox))
1411 return PTR_ERR(mailbox);
1412 inbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 memset(inbox, 0, INIT_IB_IN_SIZE);
1415
1416 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 flags |= param->set_guid0 ? INIT_IB_FLAG_G0 : 0;
1418 flags |= param->set_node_guid ? INIT_IB_FLAG_NG : 0;
1419 flags |= param->set_si_guid ? INIT_IB_FLAG_SIG : 0;
1420 flags |= param->vl_cap << INIT_IB_VL_SHIFT;
Roland Dreierda6561c2005-08-17 07:39:10 -07001421 flags |= param->port_width << INIT_IB_PORT_WIDTH_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 flags |= param->mtu_cap << INIT_IB_MTU_SHIFT;
1423 MTHCA_PUT(inbox, flags, INIT_IB_FLAGS_OFFSET);
1424
1425 MTHCA_PUT(inbox, param->gid_cap, INIT_IB_MAX_GID_OFFSET);
1426 MTHCA_PUT(inbox, param->pkey_cap, INIT_IB_MAX_PKEY_OFFSET);
1427 MTHCA_PUT(inbox, param->guid0, INIT_IB_GUID0_OFFSET);
1428 MTHCA_PUT(inbox, param->node_guid, INIT_IB_NODE_GUID_OFFSET);
1429 MTHCA_PUT(inbox, param->si_guid, INIT_IB_SI_GUID_OFFSET);
1430
Roland Dreiered878452005-06-27 14:36:45 -07001431 err = mthca_cmd(dev, mailbox->dma, port, 0, CMD_INIT_IB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 CMD_TIME_CLASS_A, status);
1433
Roland Dreiered878452005-06-27 14:36:45 -07001434 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return err;
1436}
1437
1438int mthca_CLOSE_IB(struct mthca_dev *dev, int port, u8 *status)
1439{
1440 return mthca_cmd(dev, 0, port, 0, CMD_CLOSE_IB, HZ, status);
1441}
1442
1443int mthca_CLOSE_HCA(struct mthca_dev *dev, int panic, u8 *status)
1444{
1445 return mthca_cmd(dev, 0, 0, panic, CMD_CLOSE_HCA, HZ, status);
1446}
1447
1448int mthca_SET_IB(struct mthca_dev *dev, struct mthca_set_ib_param *param,
1449 int port, u8 *status)
1450{
Roland Dreiered878452005-06-27 14:36:45 -07001451 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 u32 *inbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int err;
1454 u32 flags = 0;
1455
1456#define SET_IB_IN_SIZE 0x40
1457#define SET_IB_FLAGS_OFFSET 0x00
1458#define SET_IB_FLAG_SIG (1 << 18)
1459#define SET_IB_FLAG_RQK (1 << 0)
1460#define SET_IB_CAP_MASK_OFFSET 0x04
1461#define SET_IB_SI_GUID_OFFSET 0x08
1462
Roland Dreiered878452005-06-27 14:36:45 -07001463 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1464 if (IS_ERR(mailbox))
1465 return PTR_ERR(mailbox);
1466 inbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 memset(inbox, 0, SET_IB_IN_SIZE);
1469
1470 flags |= param->set_si_guid ? SET_IB_FLAG_SIG : 0;
1471 flags |= param->reset_qkey_viol ? SET_IB_FLAG_RQK : 0;
1472 MTHCA_PUT(inbox, flags, SET_IB_FLAGS_OFFSET);
1473
1474 MTHCA_PUT(inbox, param->cap_mask, SET_IB_CAP_MASK_OFFSET);
1475 MTHCA_PUT(inbox, param->si_guid, SET_IB_SI_GUID_OFFSET);
1476
Roland Dreiered878452005-06-27 14:36:45 -07001477 err = mthca_cmd(dev, mailbox->dma, port, 0, CMD_SET_IB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 CMD_TIME_CLASS_B, status);
1479
Roland Dreiered878452005-06-27 14:36:45 -07001480 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return err;
1482}
1483
1484int mthca_MAP_ICM(struct mthca_dev *dev, struct mthca_icm *icm, u64 virt, u8 *status)
1485{
1486 return mthca_map_cmd(dev, CMD_MAP_ICM, icm, virt, status);
1487}
1488
1489int mthca_MAP_ICM_page(struct mthca_dev *dev, u64 dma_addr, u64 virt, u8 *status)
1490{
Roland Dreiered878452005-06-27 14:36:45 -07001491 struct mthca_mailbox *mailbox;
Sean Hefty97f52eb2005-08-13 21:05:57 -07001492 __be64 *inbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int err;
1494
Roland Dreiered878452005-06-27 14:36:45 -07001495 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1496 if (IS_ERR(mailbox))
1497 return PTR_ERR(mailbox);
1498 inbox = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 inbox[0] = cpu_to_be64(virt);
1501 inbox[1] = cpu_to_be64(dma_addr);
1502
Roland Dreiered878452005-06-27 14:36:45 -07001503 err = mthca_cmd(dev, mailbox->dma, 1, 0, CMD_MAP_ICM,
1504 CMD_TIME_CLASS_B, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Roland Dreiered878452005-06-27 14:36:45 -07001506 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (!err)
Roland Dreier6bd62282005-04-16 15:26:31 -07001509 mthca_dbg(dev, "Mapped page at %llx to %llx for ICM.\n",
1510 (unsigned long long) dma_addr, (unsigned long long) virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 return err;
1513}
1514
1515int mthca_UNMAP_ICM(struct mthca_dev *dev, u64 virt, u32 page_count, u8 *status)
1516{
1517 mthca_dbg(dev, "Unmapping %d pages at %llx from ICM.\n",
1518 page_count, (unsigned long long) virt);
1519
1520 return mthca_cmd(dev, virt, page_count, 0, CMD_UNMAP_ICM, CMD_TIME_CLASS_B, status);
1521}
1522
1523int mthca_MAP_ICM_AUX(struct mthca_dev *dev, struct mthca_icm *icm, u8 *status)
1524{
1525 return mthca_map_cmd(dev, CMD_MAP_ICM_AUX, icm, -1, status);
1526}
1527
1528int mthca_UNMAP_ICM_AUX(struct mthca_dev *dev, u8 *status)
1529{
1530 return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_ICM_AUX, CMD_TIME_CLASS_B, status);
1531}
1532
1533int mthca_SET_ICM_SIZE(struct mthca_dev *dev, u64 icm_size, u64 *aux_pages,
1534 u8 *status)
1535{
1536 int ret = mthca_cmd_imm(dev, icm_size, aux_pages, 0, 0, CMD_SET_ICM_SIZE,
1537 CMD_TIME_CLASS_A, status);
1538
1539 if (ret || status)
1540 return ret;
1541
1542 /*
1543 * Arbel page size is always 4 KB; round up number of system
1544 * pages needed.
1545 */
1546 *aux_pages = (*aux_pages + (1 << (PAGE_SHIFT - 12)) - 1) >> (PAGE_SHIFT - 12);
Michael S. Tsirkinc063a062006-01-12 15:43:58 -08001547 *aux_pages = ALIGN(*aux_pages, PAGE_SIZE >> 12) >> (PAGE_SHIFT - 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 return 0;
1550}
1551
Roland Dreiered878452005-06-27 14:36:45 -07001552int mthca_SW2HW_MPT(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int mpt_index, u8 *status)
1554{
Roland Dreiered878452005-06-27 14:36:45 -07001555 return mthca_cmd(dev, mailbox->dma, mpt_index, 0, CMD_SW2HW_MPT,
1556 CMD_TIME_CLASS_B, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Roland Dreiered878452005-06-27 14:36:45 -07001559int mthca_HW2SW_MPT(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int mpt_index, u8 *status)
1561{
Roland Dreiered878452005-06-27 14:36:45 -07001562 return mthca_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
1563 !mailbox, CMD_HW2SW_MPT,
1564 CMD_TIME_CLASS_B, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Roland Dreiered878452005-06-27 14:36:45 -07001567int mthca_WRITE_MTT(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int num_mtt, u8 *status)
1569{
Roland Dreiered878452005-06-27 14:36:45 -07001570 return mthca_cmd(dev, mailbox->dma, num_mtt, 0, CMD_WRITE_MTT,
1571 CMD_TIME_CLASS_B, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Michael S. Tsirkinb8ca06f2005-04-16 15:26:28 -07001574int mthca_SYNC_TPT(struct mthca_dev *dev, u8 *status)
1575{
1576 return mthca_cmd(dev, 0, 0, 0, CMD_SYNC_TPT, CMD_TIME_CLASS_B, status);
1577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579int mthca_MAP_EQ(struct mthca_dev *dev, u64 event_mask, int unmap,
1580 int eq_num, u8 *status)
1581{
1582 mthca_dbg(dev, "%s mask %016llx for eqn %d\n",
1583 unmap ? "Clearing" : "Setting",
1584 (unsigned long long) event_mask, eq_num);
1585 return mthca_cmd(dev, event_mask, (unmap << 31) | eq_num,
1586 0, CMD_MAP_EQ, CMD_TIME_CLASS_B, status);
1587}
1588
Roland Dreiered878452005-06-27 14:36:45 -07001589int mthca_SW2HW_EQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int eq_num, u8 *status)
1591{
Roland Dreiered878452005-06-27 14:36:45 -07001592 return mthca_cmd(dev, mailbox->dma, eq_num, 0, CMD_SW2HW_EQ,
1593 CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Roland Dreiered878452005-06-27 14:36:45 -07001596int mthca_HW2SW_EQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int eq_num, u8 *status)
1598{
Roland Dreiered878452005-06-27 14:36:45 -07001599 return mthca_cmd_box(dev, 0, mailbox->dma, eq_num, 0,
1600 CMD_HW2SW_EQ,
1601 CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
Roland Dreiered878452005-06-27 14:36:45 -07001604int mthca_SW2HW_CQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int cq_num, u8 *status)
1606{
Roland Dreiered878452005-06-27 14:36:45 -07001607 return mthca_cmd(dev, mailbox->dma, cq_num, 0, CMD_SW2HW_CQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Roland Dreiered878452005-06-27 14:36:45 -07001611int mthca_HW2SW_CQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int cq_num, u8 *status)
1613{
Roland Dreiered878452005-06-27 14:36:45 -07001614 return mthca_cmd_box(dev, 0, mailbox->dma, cq_num, 0,
1615 CMD_HW2SW_CQ,
1616 CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Roland Dreier4885bf62006-01-30 14:31:33 -08001619int mthca_RESIZE_CQ(struct mthca_dev *dev, int cq_num, u32 lkey, u8 log_size,
1620 u8 *status)
1621{
1622 struct mthca_mailbox *mailbox;
1623 __be32 *inbox;
1624 int err;
1625
1626#define RESIZE_CQ_IN_SIZE 0x40
1627#define RESIZE_CQ_LOG_SIZE_OFFSET 0x0c
1628#define RESIZE_CQ_LKEY_OFFSET 0x1c
1629
1630 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1631 if (IS_ERR(mailbox))
1632 return PTR_ERR(mailbox);
1633 inbox = mailbox->buf;
1634
1635 memset(inbox, 0, RESIZE_CQ_IN_SIZE);
1636 /*
1637 * Leave start address fields zeroed out -- mthca assumes that
1638 * MRs for CQs always start at virtual address 0.
1639 */
1640 MTHCA_PUT(inbox, log_size, RESIZE_CQ_LOG_SIZE_OFFSET);
1641 MTHCA_PUT(inbox, lkey, RESIZE_CQ_LKEY_OFFSET);
1642
1643 err = mthca_cmd(dev, mailbox->dma, cq_num, 1, CMD_RESIZE_CQ,
1644 CMD_TIME_CLASS_B, status);
1645
1646 mthca_free_mailbox(dev, mailbox);
1647 return err;
1648}
1649
Roland Dreierec34a922005-08-19 10:59:31 -07001650int mthca_SW2HW_SRQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1651 int srq_num, u8 *status)
1652{
1653 return mthca_cmd(dev, mailbox->dma, srq_num, 0, CMD_SW2HW_SRQ,
1654 CMD_TIME_CLASS_A, status);
1655}
1656
1657int mthca_HW2SW_SRQ(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1658 int srq_num, u8 *status)
1659{
1660 return mthca_cmd_box(dev, 0, mailbox->dma, srq_num, 0,
1661 CMD_HW2SW_SRQ,
1662 CMD_TIME_CLASS_A, status);
1663}
1664
Eli Cohen8ebe5072006-02-13 16:40:21 -08001665int mthca_QUERY_SRQ(struct mthca_dev *dev, u32 num,
1666 struct mthca_mailbox *mailbox, u8 *status)
1667{
1668 return mthca_cmd_box(dev, 0, mailbox->dma, num, 0,
1669 CMD_QUERY_SRQ, CMD_TIME_CLASS_A, status);
1670}
1671
Roland Dreierec34a922005-08-19 10:59:31 -07001672int mthca_ARM_SRQ(struct mthca_dev *dev, int srq_num, int limit, u8 *status)
1673{
1674 return mthca_cmd(dev, limit, srq_num, 0, CMD_ARM_SRQ,
1675 CMD_TIME_CLASS_B, status);
1676}
1677
Roland Dreierd8441832006-02-13 16:30:18 -08001678int mthca_MODIFY_QP(struct mthca_dev *dev, enum ib_qp_state cur,
1679 enum ib_qp_state next, u32 num, int is_ee,
1680 struct mthca_mailbox *mailbox, u32 optmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 u8 *status)
1682{
Roland Dreierd8441832006-02-13 16:30:18 -08001683 static const u16 op[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
1684 [IB_QPS_RESET] = {
1685 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1686 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1687 [IB_QPS_INIT] = CMD_RST2INIT_QPEE,
1688 },
1689 [IB_QPS_INIT] = {
1690 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1691 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1692 [IB_QPS_INIT] = CMD_INIT2INIT_QPEE,
1693 [IB_QPS_RTR] = CMD_INIT2RTR_QPEE,
1694 },
1695 [IB_QPS_RTR] = {
1696 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1697 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1698 [IB_QPS_RTS] = CMD_RTR2RTS_QPEE,
1699 },
1700 [IB_QPS_RTS] = {
1701 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1702 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1703 [IB_QPS_RTS] = CMD_RTS2RTS_QPEE,
1704 [IB_QPS_SQD] = CMD_RTS2SQD_QPEE,
1705 },
1706 [IB_QPS_SQD] = {
1707 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1708 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1709 [IB_QPS_RTS] = CMD_SQD2RTS_QPEE,
1710 [IB_QPS_SQD] = CMD_SQD2SQD_QPEE,
1711 },
1712 [IB_QPS_SQE] = {
1713 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1714 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1715 [IB_QPS_RTS] = CMD_SQERR2RTS_QPEE,
1716 },
1717 [IB_QPS_ERR] = {
1718 [IB_QPS_RESET] = CMD_ERR2RST_QPEE,
1719 [IB_QPS_ERR] = CMD_2ERR_QPEE,
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 };
Roland Dreierd8441832006-02-13 16:30:18 -08001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 u8 op_mod = 0;
Roland Dreiered878452005-06-27 14:36:45 -07001724 int my_mailbox = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int err;
1726
Roland Dreierd8441832006-02-13 16:30:18 -08001727 if (op[cur][next] == CMD_ERR2RST_QPEE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 op_mod = 3; /* don't write outbox, any->reset */
1729
1730 /* For debugging */
Roland Dreiered878452005-06-27 14:36:45 -07001731 if (!mailbox) {
1732 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1733 if (!IS_ERR(mailbox)) {
1734 my_mailbox = 1;
1735 op_mod = 2; /* write outbox, any->reset */
1736 } else
1737 mailbox = NULL;
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Roland Dreiered878452005-06-27 14:36:45 -07001740 err = mthca_cmd_box(dev, 0, mailbox ? mailbox->dma : 0,
1741 (!!is_ee << 24) | num, op_mod,
Roland Dreierd8441832006-02-13 16:30:18 -08001742 op[cur][next], CMD_TIME_CLASS_C, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Roland Dreiered878452005-06-27 14:36:45 -07001744 if (0 && mailbox) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int i;
1746 mthca_dbg(dev, "Dumping QP context:\n");
Roland Dreiered878452005-06-27 14:36:45 -07001747 printk(" %08x\n", be32_to_cpup(mailbox->buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 for (i = 0; i < 0x100 / 4; ++i) {
1749 if (i % 8 == 0)
1750 printk("[%02x] ", i * 4);
Roland Dreiered878452005-06-27 14:36:45 -07001751 printk(" %08x",
Sean Hefty97f52eb2005-08-13 21:05:57 -07001752 be32_to_cpu(((__be32 *) mailbox->buf)[i + 2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if ((i + 1) % 8 == 0)
1754 printk("\n");
1755 }
1756 }
1757
Roland Dreierd8441832006-02-13 16:30:18 -08001758 if (my_mailbox)
1759 mthca_free_mailbox(dev, mailbox);
1760 } else {
1761 if (0) {
1762 int i;
1763 mthca_dbg(dev, "Dumping QP context:\n");
1764 printk(" opt param mask: %08x\n", be32_to_cpup(mailbox->buf));
1765 for (i = 0; i < 0x100 / 4; ++i) {
1766 if (i % 8 == 0)
1767 printk(" [%02x] ", i * 4);
1768 printk(" %08x",
1769 be32_to_cpu(((__be32 *) mailbox->buf)[i + 2]));
1770 if ((i + 1) % 8 == 0)
1771 printk("\n");
1772 }
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Roland Dreierd8441832006-02-13 16:30:18 -08001775 err = mthca_cmd(dev, mailbox->dma, optmask | (!!is_ee << 24) | num,
1776 op_mod, op[cur][next], CMD_TIME_CLASS_C, status);
1777 }
Roland Dreiered878452005-06-27 14:36:45 -07001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return err;
1780}
1781
1782int mthca_QUERY_QP(struct mthca_dev *dev, u32 num, int is_ee,
Roland Dreiered878452005-06-27 14:36:45 -07001783 struct mthca_mailbox *mailbox, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Roland Dreiered878452005-06-27 14:36:45 -07001785 return mthca_cmd_box(dev, 0, mailbox->dma, (!!is_ee << 24) | num, 0,
1786 CMD_QUERY_QPEE, CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789int mthca_CONF_SPECIAL_QP(struct mthca_dev *dev, int type, u32 qpn,
1790 u8 *status)
1791{
1792 u8 op_mod;
1793
1794 switch (type) {
1795 case IB_QPT_SMI:
1796 op_mod = 0;
1797 break;
1798 case IB_QPT_GSI:
1799 op_mod = 1;
1800 break;
1801 case IB_QPT_RAW_IPV6:
1802 op_mod = 2;
1803 break;
1804 case IB_QPT_RAW_ETY:
1805 op_mod = 3;
1806 break;
1807 default:
1808 return -EINVAL;
1809 }
1810
1811 return mthca_cmd(dev, 0, qpn, op_mod, CMD_CONF_SPECIAL_QP,
1812 CMD_TIME_CLASS_B, status);
1813}
1814
1815int mthca_MAD_IFC(struct mthca_dev *dev, int ignore_mkey, int ignore_bkey,
Roland Dreiered878452005-06-27 14:36:45 -07001816 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 void *in_mad, void *response_mad, u8 *status)
1818{
Roland Dreiered878452005-06-27 14:36:45 -07001819 struct mthca_mailbox *inmailbox, *outmailbox;
1820 void *inbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int err;
1822 u32 in_modifier = port;
1823 u8 op_modifier = 0;
1824
1825#define MAD_IFC_BOX_SIZE 0x400
1826#define MAD_IFC_MY_QPN_OFFSET 0x100
1827#define MAD_IFC_RQPN_OFFSET 0x104
1828#define MAD_IFC_SL_OFFSET 0x108
1829#define MAD_IFC_G_PATH_OFFSET 0x109
1830#define MAD_IFC_RLID_OFFSET 0x10a
1831#define MAD_IFC_PKEY_OFFSET 0x10e
1832#define MAD_IFC_GRH_OFFSET 0x140
1833
Roland Dreiered878452005-06-27 14:36:45 -07001834 inmailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1835 if (IS_ERR(inmailbox))
1836 return PTR_ERR(inmailbox);
1837 inbox = inmailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Roland Dreiered878452005-06-27 14:36:45 -07001839 outmailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
1840 if (IS_ERR(outmailbox)) {
1841 mthca_free_mailbox(dev, inmailbox);
1842 return PTR_ERR(outmailbox);
1843 }
1844
1845 memcpy(inbox, in_mad, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /*
1848 * Key check traps can't be generated unless we have in_wc to
1849 * tell us where to send the trap.
1850 */
1851 if (ignore_mkey || !in_wc)
1852 op_modifier |= 0x1;
1853 if (ignore_bkey || !in_wc)
1854 op_modifier |= 0x2;
1855
1856 if (in_wc) {
1857 u8 val;
1858
Roland Dreiered878452005-06-27 14:36:45 -07001859 memset(inbox + 256, 0, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Roland Dreiered878452005-06-27 14:36:45 -07001861 MTHCA_PUT(inbox, in_wc->qp_num, MAD_IFC_MY_QPN_OFFSET);
1862 MTHCA_PUT(inbox, in_wc->src_qp, MAD_IFC_RQPN_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 val = in_wc->sl << 4;
Roland Dreiered878452005-06-27 14:36:45 -07001865 MTHCA_PUT(inbox, val, MAD_IFC_SL_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 val = in_wc->dlid_path_bits |
1868 (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
Roland Dreiered878452005-06-27 14:36:45 -07001869 MTHCA_PUT(inbox, val, MAD_IFC_GRH_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Roland Dreiered878452005-06-27 14:36:45 -07001871 MTHCA_PUT(inbox, in_wc->slid, MAD_IFC_RLID_OFFSET);
1872 MTHCA_PUT(inbox, in_wc->pkey_index, MAD_IFC_PKEY_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 if (in_grh)
Roland Dreiered878452005-06-27 14:36:45 -07001875 memcpy(inbox + MAD_IFC_GRH_OFFSET, in_grh, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 op_modifier |= 0x10;
1878
1879 in_modifier |= in_wc->slid << 16;
1880 }
1881
Roland Dreiered878452005-06-27 14:36:45 -07001882 err = mthca_cmd_box(dev, inmailbox->dma, outmailbox->dma,
1883 in_modifier, op_modifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 CMD_MAD_IFC, CMD_TIME_CLASS_C, status);
1885
1886 if (!err && !*status)
Roland Dreiered878452005-06-27 14:36:45 -07001887 memcpy(response_mad, outmailbox->buf, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Roland Dreiered878452005-06-27 14:36:45 -07001889 mthca_free_mailbox(dev, inmailbox);
1890 mthca_free_mailbox(dev, outmailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return err;
1892}
1893
Roland Dreiered878452005-06-27 14:36:45 -07001894int mthca_READ_MGM(struct mthca_dev *dev, int index,
1895 struct mthca_mailbox *mailbox, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Roland Dreiered878452005-06-27 14:36:45 -07001897 return mthca_cmd_box(dev, 0, mailbox->dma, index, 0,
1898 CMD_READ_MGM, CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Roland Dreiered878452005-06-27 14:36:45 -07001901int mthca_WRITE_MGM(struct mthca_dev *dev, int index,
1902 struct mthca_mailbox *mailbox, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Roland Dreiered878452005-06-27 14:36:45 -07001904 return mthca_cmd(dev, mailbox->dma, index, 0, CMD_WRITE_MGM,
1905 CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Roland Dreiered878452005-06-27 14:36:45 -07001908int mthca_MGID_HASH(struct mthca_dev *dev, struct mthca_mailbox *mailbox,
1909 u16 *hash, u8 *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 u64 imm;
1912 int err;
1913
Roland Dreiered878452005-06-27 14:36:45 -07001914 err = mthca_cmd_imm(dev, mailbox->dma, &imm, 0, 0, CMD_MGID_HASH,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 CMD_TIME_CLASS_A, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Roland Dreiered878452005-06-27 14:36:45 -07001917 *hash = imm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return err;
1919}
1920
1921int mthca_NOP(struct mthca_dev *dev, u8 *status)
1922{
1923 return mthca_cmd(dev, 0, 0x1f, 0, CMD_NOP, msecs_to_jiffies(100), status);
1924}