blob: 78d4891720e9a92ee03ddc6ab0e6eb81ca3f8796 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * $Id: mthca_cmd.c 1349 2004-12-16 21:09:43Z roland $
33 */
34
35#include <linux/sched.h>
36#include <linux/pci.h>
37#include <linux/errno.h>
38#include <asm/io.h>
39#include <ib_mad.h>
40
41#include "mthca_dev.h"
42#include "mthca_config_reg.h"
43#include "mthca_cmd.h"
44#include "mthca_memfree.h"
45
46#define CMD_POLL_TOKEN 0xffff
47
48enum {
49 HCR_IN_PARAM_OFFSET = 0x00,
50 HCR_IN_MODIFIER_OFFSET = 0x08,
51 HCR_OUT_PARAM_OFFSET = 0x0c,
52 HCR_TOKEN_OFFSET = 0x14,
53 HCR_STATUS_OFFSET = 0x18,
54
55 HCR_OPMOD_SHIFT = 12,
56 HCA_E_BIT = 22,
57 HCR_GO_BIT = 23
58};
59
60enum {
61 /* initialization and general commands */
62 CMD_SYS_EN = 0x1,
63 CMD_SYS_DIS = 0x2,
64 CMD_MAP_FA = 0xfff,
65 CMD_UNMAP_FA = 0xffe,
66 CMD_RUN_FW = 0xff6,
67 CMD_MOD_STAT_CFG = 0x34,
68 CMD_QUERY_DEV_LIM = 0x3,
69 CMD_QUERY_FW = 0x4,
70 CMD_ENABLE_LAM = 0xff8,
71 CMD_DISABLE_LAM = 0xff7,
72 CMD_QUERY_DDR = 0x5,
73 CMD_QUERY_ADAPTER = 0x6,
74 CMD_INIT_HCA = 0x7,
75 CMD_CLOSE_HCA = 0x8,
76 CMD_INIT_IB = 0x9,
77 CMD_CLOSE_IB = 0xa,
78 CMD_QUERY_HCA = 0xb,
79 CMD_SET_IB = 0xc,
80 CMD_ACCESS_DDR = 0x2e,
81 CMD_MAP_ICM = 0xffa,
82 CMD_UNMAP_ICM = 0xff9,
83 CMD_MAP_ICM_AUX = 0xffc,
84 CMD_UNMAP_ICM_AUX = 0xffb,
85 CMD_SET_ICM_SIZE = 0xffd,
86
87 /* TPT commands */
88 CMD_SW2HW_MPT = 0xd,
89 CMD_QUERY_MPT = 0xe,
90 CMD_HW2SW_MPT = 0xf,
91 CMD_READ_MTT = 0x10,
92 CMD_WRITE_MTT = 0x11,
93 CMD_SYNC_TPT = 0x2f,
94
95 /* EQ commands */
96 CMD_MAP_EQ = 0x12,
97 CMD_SW2HW_EQ = 0x13,
98 CMD_HW2SW_EQ = 0x14,
99 CMD_QUERY_EQ = 0x15,
100
101 /* CQ commands */
102 CMD_SW2HW_CQ = 0x16,
103 CMD_HW2SW_CQ = 0x17,
104 CMD_QUERY_CQ = 0x18,
105 CMD_RESIZE_CQ = 0x2c,
106
107 /* SRQ commands */
108 CMD_SW2HW_SRQ = 0x35,
109 CMD_HW2SW_SRQ = 0x36,
110 CMD_QUERY_SRQ = 0x37,
111
112 /* QP/EE commands */
113 CMD_RST2INIT_QPEE = 0x19,
114 CMD_INIT2RTR_QPEE = 0x1a,
115 CMD_RTR2RTS_QPEE = 0x1b,
116 CMD_RTS2RTS_QPEE = 0x1c,
117 CMD_SQERR2RTS_QPEE = 0x1d,
118 CMD_2ERR_QPEE = 0x1e,
119 CMD_RTS2SQD_QPEE = 0x1f,
120 CMD_SQD2SQD_QPEE = 0x38,
121 CMD_SQD2RTS_QPEE = 0x20,
122 CMD_ERR2RST_QPEE = 0x21,
123 CMD_QUERY_QPEE = 0x22,
124 CMD_INIT2INIT_QPEE = 0x2d,
125 CMD_SUSPEND_QPEE = 0x32,
126 CMD_UNSUSPEND_QPEE = 0x33,
127 /* special QPs and management commands */
128 CMD_CONF_SPECIAL_QP = 0x23,
129 CMD_MAD_IFC = 0x24,
130
131 /* multicast commands */
132 CMD_READ_MGM = 0x25,
133 CMD_WRITE_MGM = 0x26,
134 CMD_MGID_HASH = 0x27,
135
136 /* miscellaneous commands */
137 CMD_DIAG_RPRT = 0x30,
138 CMD_NOP = 0x31,
139
140 /* debug commands */
141 CMD_QUERY_DEBUG_MSG = 0x2a,
142 CMD_SET_DEBUG_MSG = 0x2b,
143};
144
145/*
146 * According to Mellanox code, FW may be starved and never complete
147 * commands. So we can't use strict timeouts described in PRM -- we
148 * just arbitrarily select 60 seconds for now.
149 */
150#if 0
151/*
152 * Round up and add 1 to make sure we get the full wait time (since we
153 * will be starting in the middle of a jiffy)
154 */
155enum {
156 CMD_TIME_CLASS_A = (HZ + 999) / 1000 + 1,
157 CMD_TIME_CLASS_B = (HZ + 99) / 100 + 1,
158 CMD_TIME_CLASS_C = (HZ + 9) / 10 + 1
159};
160#else
161enum {
162 CMD_TIME_CLASS_A = 60 * HZ,
163 CMD_TIME_CLASS_B = 60 * HZ,
164 CMD_TIME_CLASS_C = 60 * HZ
165};
166#endif
167
168enum {
169 GO_BIT_TIMEOUT = HZ * 10
170};
171
172struct mthca_cmd_context {
173 struct completion done;
174 struct timer_list timer;
175 int result;
176 int next;
177 u64 out_param;
178 u16 token;
179 u8 status;
180};
181
182static inline int go_bit(struct mthca_dev *dev)
183{
184 return readl(dev->hcr + HCR_STATUS_OFFSET) &
185 swab32(1 << HCR_GO_BIT);
186}
187
188static int mthca_cmd_post(struct mthca_dev *dev,
189 u64 in_param,
190 u64 out_param,
191 u32 in_modifier,
192 u8 op_modifier,
193 u16 op,
194 u16 token,
195 int event)
196{
197 int err = 0;
198
199 if (down_interruptible(&dev->cmd.hcr_sem))
200 return -EINTR;
201
202 if (event) {
203 unsigned long end = jiffies + GO_BIT_TIMEOUT;
204
205 while (go_bit(dev) && time_before(jiffies, end)) {
206 set_current_state(TASK_RUNNING);
207 schedule();
208 }
209 }
210
211 if (go_bit(dev)) {
212 err = -EAGAIN;
213 goto out;
214 }
215
216 /*
217 * We use writel (instead of something like memcpy_toio)
218 * because writes of less than 32 bits to the HCR don't work
219 * (and some architectures such as ia64 implement memcpy_toio
220 * in terms of writeb).
221 */
222 __raw_writel(cpu_to_be32(in_param >> 32), dev->hcr + 0 * 4);
223 __raw_writel(cpu_to_be32(in_param & 0xfffffffful), dev->hcr + 1 * 4);
224 __raw_writel(cpu_to_be32(in_modifier), dev->hcr + 2 * 4);
225 __raw_writel(cpu_to_be32(out_param >> 32), dev->hcr + 3 * 4);
226 __raw_writel(cpu_to_be32(out_param & 0xfffffffful), dev->hcr + 4 * 4);
227 __raw_writel(cpu_to_be32(token << 16), dev->hcr + 5 * 4);
228
229 /* __raw_writel may not order writes. */
230 wmb();
231
232 __raw_writel(cpu_to_be32((1 << HCR_GO_BIT) |
233 (event ? (1 << HCA_E_BIT) : 0) |
234 (op_modifier << HCR_OPMOD_SHIFT) |
235 op), dev->hcr + 6 * 4);
236
237out:
238 up(&dev->cmd.hcr_sem);
239 return err;
240}
241
242static int mthca_cmd_poll(struct mthca_dev *dev,
243 u64 in_param,
244 u64 *out_param,
245 int out_is_imm,
246 u32 in_modifier,
247 u8 op_modifier,
248 u16 op,
249 unsigned long timeout,
250 u8 *status)
251{
252 int err = 0;
253 unsigned long end;
254
255 if (down_interruptible(&dev->cmd.poll_sem))
256 return -EINTR;
257
258 err = mthca_cmd_post(dev, in_param,
259 out_param ? *out_param : 0,
260 in_modifier, op_modifier,
261 op, CMD_POLL_TOKEN, 0);
262 if (err)
263 goto out;
264
265 end = timeout + jiffies;
266 while (go_bit(dev) && time_before(jiffies, end)) {
267 set_current_state(TASK_RUNNING);
268 schedule();
269 }
270
271 if (go_bit(dev)) {
272 err = -EBUSY;
273 goto out;
274 }
275
276 if (out_is_imm) {
277 memcpy_fromio(out_param, dev->hcr + HCR_OUT_PARAM_OFFSET, sizeof (u64));
278 be64_to_cpus(out_param);
279 }
280
281 *status = be32_to_cpu(__raw_readl(dev->hcr + HCR_STATUS_OFFSET)) >> 24;
282
283out:
284 up(&dev->cmd.poll_sem);
285 return err;
286}
287
288void mthca_cmd_event(struct mthca_dev *dev,
289 u16 token,
290 u8 status,
291 u64 out_param)
292{
293 struct mthca_cmd_context *context =
294 &dev->cmd.context[token & dev->cmd.token_mask];
295
296 /* previously timed out command completing at long last */
297 if (token != context->token)
298 return;
299
300 context->result = 0;
301 context->status = status;
302 context->out_param = out_param;
303
304 context->token += dev->cmd.token_mask + 1;
305
306 complete(&context->done);
307}
308
309static void event_timeout(unsigned long context_ptr)
310{
311 struct mthca_cmd_context *context =
312 (struct mthca_cmd_context *) context_ptr;
313
314 context->result = -EBUSY;
315 complete(&context->done);
316}
317
318static int mthca_cmd_wait(struct mthca_dev *dev,
319 u64 in_param,
320 u64 *out_param,
321 int out_is_imm,
322 u32 in_modifier,
323 u8 op_modifier,
324 u16 op,
325 unsigned long timeout,
326 u8 *status)
327{
328 int err = 0;
329 struct mthca_cmd_context *context;
330
331 if (down_interruptible(&dev->cmd.event_sem))
332 return -EINTR;
333
334 spin_lock(&dev->cmd.context_lock);
335 BUG_ON(dev->cmd.free_head < 0);
336 context = &dev->cmd.context[dev->cmd.free_head];
337 dev->cmd.free_head = context->next;
338 spin_unlock(&dev->cmd.context_lock);
339
340 init_completion(&context->done);
341
342 err = mthca_cmd_post(dev, in_param,
343 out_param ? *out_param : 0,
344 in_modifier, op_modifier,
345 op, context->token, 1);
346 if (err)
347 goto out;
348
349 context->timer.expires = jiffies + timeout;
350 add_timer(&context->timer);
351
352 wait_for_completion(&context->done);
353 del_timer_sync(&context->timer);
354
355 err = context->result;
356 if (err)
357 goto out;
358
359 *status = context->status;
360 if (*status)
361 mthca_dbg(dev, "Command %02x completed with status %02x\n",
362 op, *status);
363
364 if (out_is_imm)
365 *out_param = context->out_param;
366
367out:
368 spin_lock(&dev->cmd.context_lock);
369 context->next = dev->cmd.free_head;
370 dev->cmd.free_head = context - dev->cmd.context;
371 spin_unlock(&dev->cmd.context_lock);
372
373 up(&dev->cmd.event_sem);
374 return err;
375}
376
377/* Invoke a command with an output mailbox */
378static int mthca_cmd_box(struct mthca_dev *dev,
379 u64 in_param,
380 u64 out_param,
381 u32 in_modifier,
382 u8 op_modifier,
383 u16 op,
384 unsigned long timeout,
385 u8 *status)
386{
387 if (dev->cmd.use_events)
388 return mthca_cmd_wait(dev, in_param, &out_param, 0,
389 in_modifier, op_modifier, op,
390 timeout, status);
391 else
392 return mthca_cmd_poll(dev, in_param, &out_param, 0,
393 in_modifier, op_modifier, op,
394 timeout, status);
395}
396
397/* Invoke a command with no output parameter */
398static int mthca_cmd(struct mthca_dev *dev,
399 u64 in_param,
400 u32 in_modifier,
401 u8 op_modifier,
402 u16 op,
403 unsigned long timeout,
404 u8 *status)
405{
406 return mthca_cmd_box(dev, in_param, 0, in_modifier,
407 op_modifier, op, timeout, status);
408}
409
410/*
411 * Invoke a command with an immediate output parameter (and copy the
412 * output into the caller's out_param pointer after the command
413 * executes).
414 */
415static int mthca_cmd_imm(struct mthca_dev *dev,
416 u64 in_param,
417 u64 *out_param,
418 u32 in_modifier,
419 u8 op_modifier,
420 u16 op,
421 unsigned long timeout,
422 u8 *status)
423{
424 if (dev->cmd.use_events)
425 return mthca_cmd_wait(dev, in_param, out_param, 1,
426 in_modifier, op_modifier, op,
427 timeout, status);
428 else
429 return mthca_cmd_poll(dev, in_param, out_param, 1,
430 in_modifier, op_modifier, op,
431 timeout, status);
432}
433
Roland Dreier80fd8232005-06-27 14:36:45 -0700434int mthca_cmd_init(struct mthca_dev *dev)
435{
436 sema_init(&dev->cmd.hcr_sem, 1);
437 sema_init(&dev->cmd.poll_sem, 1);
438 dev->cmd.use_events = 0;
439
440 dev->hcr = ioremap(pci_resource_start(dev->pdev, 0) + MTHCA_HCR_BASE,
441 MTHCA_HCR_SIZE);
442 if (!dev->hcr) {
443 mthca_err(dev, "Couldn't map command register.");
444 return -ENOMEM;
445 }
446
447 return 0;
448}
449
450void mthca_cmd_cleanup(struct mthca_dev *dev)
451{
452 iounmap(dev->hcr);
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
456 * Switch to using events to issue FW commands (should be called after
457 * event queue to command events has been initialized).
458 */
459int mthca_cmd_use_events(struct mthca_dev *dev)
460{
461 int i;
462
463 dev->cmd.context = kmalloc(dev->cmd.max_cmds *
464 sizeof (struct mthca_cmd_context),
465 GFP_KERNEL);
466 if (!dev->cmd.context)
467 return -ENOMEM;
468
469 for (i = 0; i < dev->cmd.max_cmds; ++i) {
470 dev->cmd.context[i].token = i;
471 dev->cmd.context[i].next = i + 1;
472 init_timer(&dev->cmd.context[i].timer);
473 dev->cmd.context[i].timer.data =
474 (unsigned long) &dev->cmd.context[i];
475 dev->cmd.context[i].timer.function = event_timeout;
476 }
477
478 dev->cmd.context[dev->cmd.max_cmds - 1].next = -1;
479 dev->cmd.free_head = 0;
480
481 sema_init(&dev->cmd.event_sem, dev->cmd.max_cmds);
482 spin_lock_init(&dev->cmd.context_lock);
483
484 for (dev->cmd.token_mask = 1;
485 dev->cmd.token_mask < dev->cmd.max_cmds;
486 dev->cmd.token_mask <<= 1)
487 ; /* nothing */
488 --dev->cmd.token_mask;
489
490 dev->cmd.use_events = 1;
491 down(&dev->cmd.poll_sem);
492
493 return 0;
494}
495
496/*
497 * Switch back to polling (used when shutting down the device)
498 */
499void mthca_cmd_use_polling(struct mthca_dev *dev)
500{
501 int i;
502
503 dev->cmd.use_events = 0;
504
505 for (i = 0; i < dev->cmd.max_cmds; ++i)
506 down(&dev->cmd.event_sem);
507
508 kfree(dev->cmd.context);
509
510 up(&dev->cmd.poll_sem);
511}
512
513int mthca_SYS_EN(struct mthca_dev *dev, u8 *status)
514{
515 u64 out;
516 int ret;
517
518 ret = mthca_cmd_imm(dev, 0, &out, 0, 0, CMD_SYS_EN, HZ, status);
519
520 if (*status == MTHCA_CMD_STAT_DDR_MEM_ERR)
521 mthca_warn(dev, "SYS_EN DDR error: syn=%x, sock=%d, "
522 "sladdr=%d, SPD source=%s\n",
523 (int) (out >> 6) & 0xf, (int) (out >> 4) & 3,
524 (int) (out >> 1) & 7, (int) out & 1 ? "NVMEM" : "DIMM");
525
526 return ret;
527}
528
529int mthca_SYS_DIS(struct mthca_dev *dev, u8 *status)
530{
531 return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, HZ, status);
532}
533
534static int mthca_map_cmd(struct mthca_dev *dev, u16 op, struct mthca_icm *icm,
535 u64 virt, u8 *status)
536{
537 u32 *inbox;
538 dma_addr_t indma;
539 struct mthca_icm_iter iter;
540 int lg;
541 int nent = 0;
542 int i;
543 int err = 0;
544 int ts = 0, tc = 0;
545
546 inbox = pci_alloc_consistent(dev->pdev, PAGE_SIZE, &indma);
547 if (!inbox)
548 return -ENOMEM;
549
550 memset(inbox, 0, PAGE_SIZE);
551
552 for (mthca_icm_first(icm, &iter);
553 !mthca_icm_last(&iter);
554 mthca_icm_next(&iter)) {
555 /*
556 * We have to pass pages that are aligned to their
557 * size, so find the least significant 1 in the
558 * address or size and use that as our log2 size.
559 */
560 lg = ffs(mthca_icm_addr(&iter) | mthca_icm_size(&iter)) - 1;
561 if (lg < 12) {
562 mthca_warn(dev, "Got FW area not aligned to 4K (%llx/%lx).\n",
563 (unsigned long long) mthca_icm_addr(&iter),
564 mthca_icm_size(&iter));
565 err = -EINVAL;
566 goto out;
567 }
568 for (i = 0; i < mthca_icm_size(&iter) / (1 << lg); ++i, ++nent) {
569 if (virt != -1) {
570 *((__be64 *) (inbox + nent * 4)) =
571 cpu_to_be64(virt);
572 virt += 1 << lg;
573 }
574
575 *((__be64 *) (inbox + nent * 4 + 2)) =
576 cpu_to_be64((mthca_icm_addr(&iter) +
577 (i << lg)) | (lg - 12));
578 ts += 1 << (lg - 10);
579 ++tc;
580
581 if (nent == PAGE_SIZE / 16) {
582 err = mthca_cmd(dev, indma, nent, 0, op,
583 CMD_TIME_CLASS_B, status);
584 if (err || *status)
585 goto out;
586 nent = 0;
587 }
588 }
589 }
590
591 if (nent)
592 err = mthca_cmd(dev, indma, nent, 0, op,
593 CMD_TIME_CLASS_B, status);
594
595 switch (op) {
596 case CMD_MAP_FA:
597 mthca_dbg(dev, "Mapped %d chunks/%d KB for FW.\n", tc, ts);
598 break;
599 case CMD_MAP_ICM_AUX:
600 mthca_dbg(dev, "Mapped %d chunks/%d KB for ICM aux.\n", tc, ts);
601 break;
602 case CMD_MAP_ICM:
603 mthca_dbg(dev, "Mapped %d chunks/%d KB at %llx for ICM.\n",
604 tc, ts, (unsigned long long) virt - (ts << 10));
605 break;
606 }
607
608out:
609 pci_free_consistent(dev->pdev, PAGE_SIZE, inbox, indma);
610 return err;
611}
612
613int mthca_MAP_FA(struct mthca_dev *dev, struct mthca_icm *icm, u8 *status)
614{
615 return mthca_map_cmd(dev, CMD_MAP_FA, icm, -1, status);
616}
617
618int mthca_UNMAP_FA(struct mthca_dev *dev, u8 *status)
619{
620 return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_FA, CMD_TIME_CLASS_B, status);
621}
622
623int mthca_RUN_FW(struct mthca_dev *dev, u8 *status)
624{
625 return mthca_cmd(dev, 0, 0, 0, CMD_RUN_FW, CMD_TIME_CLASS_A, status);
626}
627
628int mthca_QUERY_FW(struct mthca_dev *dev, u8 *status)
629{
630 u32 *outbox;
631 dma_addr_t outdma;
632 int err = 0;
633 u8 lg;
634
635#define QUERY_FW_OUT_SIZE 0x100
636#define QUERY_FW_VER_OFFSET 0x00
637#define QUERY_FW_MAX_CMD_OFFSET 0x0f
638#define QUERY_FW_ERR_START_OFFSET 0x30
639#define QUERY_FW_ERR_SIZE_OFFSET 0x38
640
641#define QUERY_FW_START_OFFSET 0x20
642#define QUERY_FW_END_OFFSET 0x28
643
644#define QUERY_FW_SIZE_OFFSET 0x00
645#define QUERY_FW_CLR_INT_BASE_OFFSET 0x20
646#define QUERY_FW_EQ_ARM_BASE_OFFSET 0x40
647#define QUERY_FW_EQ_SET_CI_BASE_OFFSET 0x48
648
649 outbox = pci_alloc_consistent(dev->pdev, QUERY_FW_OUT_SIZE, &outdma);
650 if (!outbox) {
651 return -ENOMEM;
652 }
653
654 err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_FW,
655 CMD_TIME_CLASS_A, status);
656
657 if (err)
658 goto out;
659
660 MTHCA_GET(dev->fw_ver, outbox, QUERY_FW_VER_OFFSET);
661 /*
662 * FW subminor version is at more signifant bits than minor
663 * version, so swap here.
664 */
665 dev->fw_ver = (dev->fw_ver & 0xffff00000000ull) |
666 ((dev->fw_ver & 0xffff0000ull) >> 16) |
667 ((dev->fw_ver & 0x0000ffffull) << 16);
668
669 MTHCA_GET(lg, outbox, QUERY_FW_MAX_CMD_OFFSET);
670 dev->cmd.max_cmds = 1 << lg;
671
672 mthca_dbg(dev, "FW version %012llx, max commands %d\n",
673 (unsigned long long) dev->fw_ver, dev->cmd.max_cmds);
674
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700675 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 MTHCA_GET(dev->fw.arbel.fw_pages, outbox, QUERY_FW_SIZE_OFFSET);
677 MTHCA_GET(dev->fw.arbel.clr_int_base, outbox, QUERY_FW_CLR_INT_BASE_OFFSET);
678 MTHCA_GET(dev->fw.arbel.eq_arm_base, outbox, QUERY_FW_EQ_ARM_BASE_OFFSET);
679 MTHCA_GET(dev->fw.arbel.eq_set_ci_base, outbox, QUERY_FW_EQ_SET_CI_BASE_OFFSET);
680 mthca_dbg(dev, "FW size %d KB\n", dev->fw.arbel.fw_pages << 2);
681
682 /*
683 * Arbel page size is always 4 KB; round up number of
684 * system pages needed.
685 */
686 dev->fw.arbel.fw_pages =
687 (dev->fw.arbel.fw_pages + (1 << (PAGE_SHIFT - 12)) - 1) >>
688 (PAGE_SHIFT - 12);
689
690 mthca_dbg(dev, "Clear int @ %llx, EQ arm @ %llx, EQ set CI @ %llx\n",
691 (unsigned long long) dev->fw.arbel.clr_int_base,
692 (unsigned long long) dev->fw.arbel.eq_arm_base,
693 (unsigned long long) dev->fw.arbel.eq_set_ci_base);
694 } else {
695 MTHCA_GET(dev->fw.tavor.fw_start, outbox, QUERY_FW_START_OFFSET);
696 MTHCA_GET(dev->fw.tavor.fw_end, outbox, QUERY_FW_END_OFFSET);
697
698 mthca_dbg(dev, "FW size %d KB (start %llx, end %llx)\n",
699 (int) ((dev->fw.tavor.fw_end - dev->fw.tavor.fw_start) >> 10),
700 (unsigned long long) dev->fw.tavor.fw_start,
701 (unsigned long long) dev->fw.tavor.fw_end);
702 }
703
704out:
705 pci_free_consistent(dev->pdev, QUERY_FW_OUT_SIZE, outbox, outdma);
706 return err;
707}
708
709int mthca_ENABLE_LAM(struct mthca_dev *dev, u8 *status)
710{
711 u8 info;
712 u32 *outbox;
713 dma_addr_t outdma;
714 int err = 0;
715
716#define ENABLE_LAM_OUT_SIZE 0x100
717#define ENABLE_LAM_START_OFFSET 0x00
718#define ENABLE_LAM_END_OFFSET 0x08
719#define ENABLE_LAM_INFO_OFFSET 0x13
720
721#define ENABLE_LAM_INFO_HIDDEN_FLAG (1 << 4)
722#define ENABLE_LAM_INFO_ECC_MASK 0x3
723
724 outbox = pci_alloc_consistent(dev->pdev, ENABLE_LAM_OUT_SIZE, &outdma);
725 if (!outbox)
726 return -ENOMEM;
727
728 err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_ENABLE_LAM,
729 CMD_TIME_CLASS_C, status);
730
731 if (err)
732 goto out;
733
734 if (*status == MTHCA_CMD_STAT_LAM_NOT_PRE)
735 goto out;
736
737 MTHCA_GET(dev->ddr_start, outbox, ENABLE_LAM_START_OFFSET);
738 MTHCA_GET(dev->ddr_end, outbox, ENABLE_LAM_END_OFFSET);
739 MTHCA_GET(info, outbox, ENABLE_LAM_INFO_OFFSET);
740
741 if (!!(info & ENABLE_LAM_INFO_HIDDEN_FLAG) !=
742 !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
743 mthca_info(dev, "FW reports that HCA-attached memory "
744 "is %s hidden; does not match PCI config\n",
745 (info & ENABLE_LAM_INFO_HIDDEN_FLAG) ?
746 "" : "not");
747 }
748 if (info & ENABLE_LAM_INFO_HIDDEN_FLAG)
749 mthca_dbg(dev, "HCA-attached memory is hidden.\n");
750
751 mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
752 (int) ((dev->ddr_end - dev->ddr_start) >> 10),
753 (unsigned long long) dev->ddr_start,
754 (unsigned long long) dev->ddr_end);
755
756out:
757 pci_free_consistent(dev->pdev, ENABLE_LAM_OUT_SIZE, outbox, outdma);
758 return err;
759}
760
761int mthca_DISABLE_LAM(struct mthca_dev *dev, u8 *status)
762{
763 return mthca_cmd(dev, 0, 0, 0, CMD_SYS_DIS, CMD_TIME_CLASS_C, status);
764}
765
766int mthca_QUERY_DDR(struct mthca_dev *dev, u8 *status)
767{
768 u8 info;
769 u32 *outbox;
770 dma_addr_t outdma;
771 int err = 0;
772
773#define QUERY_DDR_OUT_SIZE 0x100
774#define QUERY_DDR_START_OFFSET 0x00
775#define QUERY_DDR_END_OFFSET 0x08
776#define QUERY_DDR_INFO_OFFSET 0x13
777
778#define QUERY_DDR_INFO_HIDDEN_FLAG (1 << 4)
779#define QUERY_DDR_INFO_ECC_MASK 0x3
780
781 outbox = pci_alloc_consistent(dev->pdev, QUERY_DDR_OUT_SIZE, &outdma);
782 if (!outbox)
783 return -ENOMEM;
784
785 err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_DDR,
786 CMD_TIME_CLASS_A, status);
787
788 if (err)
789 goto out;
790
791 MTHCA_GET(dev->ddr_start, outbox, QUERY_DDR_START_OFFSET);
792 MTHCA_GET(dev->ddr_end, outbox, QUERY_DDR_END_OFFSET);
793 MTHCA_GET(info, outbox, QUERY_DDR_INFO_OFFSET);
794
795 if (!!(info & QUERY_DDR_INFO_HIDDEN_FLAG) !=
796 !!(dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) {
797 mthca_info(dev, "FW reports that HCA-attached memory "
798 "is %s hidden; does not match PCI config\n",
799 (info & QUERY_DDR_INFO_HIDDEN_FLAG) ?
800 "" : "not");
801 }
802 if (info & QUERY_DDR_INFO_HIDDEN_FLAG)
803 mthca_dbg(dev, "HCA-attached memory is hidden.\n");
804
805 mthca_dbg(dev, "HCA memory size %d KB (start %llx, end %llx)\n",
806 (int) ((dev->ddr_end - dev->ddr_start) >> 10),
807 (unsigned long long) dev->ddr_start,
808 (unsigned long long) dev->ddr_end);
809
810out:
811 pci_free_consistent(dev->pdev, QUERY_DDR_OUT_SIZE, outbox, outdma);
812 return err;
813}
814
815int mthca_QUERY_DEV_LIM(struct mthca_dev *dev,
816 struct mthca_dev_lim *dev_lim, u8 *status)
817{
818 u32 *outbox;
819 dma_addr_t outdma;
820 u8 field;
821 u16 size;
822 int err;
823
824#define QUERY_DEV_LIM_OUT_SIZE 0x100
825#define QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET 0x10
826#define QUERY_DEV_LIM_MAX_QP_SZ_OFFSET 0x11
827#define QUERY_DEV_LIM_RSVD_QP_OFFSET 0x12
828#define QUERY_DEV_LIM_MAX_QP_OFFSET 0x13
829#define QUERY_DEV_LIM_RSVD_SRQ_OFFSET 0x14
830#define QUERY_DEV_LIM_MAX_SRQ_OFFSET 0x15
831#define QUERY_DEV_LIM_RSVD_EEC_OFFSET 0x16
832#define QUERY_DEV_LIM_MAX_EEC_OFFSET 0x17
833#define QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET 0x19
834#define QUERY_DEV_LIM_RSVD_CQ_OFFSET 0x1a
835#define QUERY_DEV_LIM_MAX_CQ_OFFSET 0x1b
836#define QUERY_DEV_LIM_MAX_MPT_OFFSET 0x1d
837#define QUERY_DEV_LIM_RSVD_EQ_OFFSET 0x1e
838#define QUERY_DEV_LIM_MAX_EQ_OFFSET 0x1f
839#define QUERY_DEV_LIM_RSVD_MTT_OFFSET 0x20
840#define QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET 0x21
841#define QUERY_DEV_LIM_RSVD_MRW_OFFSET 0x22
842#define QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET 0x23
843#define QUERY_DEV_LIM_MAX_AV_OFFSET 0x27
844#define QUERY_DEV_LIM_MAX_REQ_QP_OFFSET 0x29
845#define QUERY_DEV_LIM_MAX_RES_QP_OFFSET 0x2b
846#define QUERY_DEV_LIM_MAX_RDMA_OFFSET 0x2f
847#define QUERY_DEV_LIM_RSZ_SRQ_OFFSET 0x33
848#define QUERY_DEV_LIM_ACK_DELAY_OFFSET 0x35
849#define QUERY_DEV_LIM_MTU_WIDTH_OFFSET 0x36
850#define QUERY_DEV_LIM_VL_PORT_OFFSET 0x37
851#define QUERY_DEV_LIM_MAX_GID_OFFSET 0x3b
852#define QUERY_DEV_LIM_MAX_PKEY_OFFSET 0x3f
853#define QUERY_DEV_LIM_FLAGS_OFFSET 0x44
854#define QUERY_DEV_LIM_RSVD_UAR_OFFSET 0x48
855#define QUERY_DEV_LIM_UAR_SZ_OFFSET 0x49
856#define QUERY_DEV_LIM_PAGE_SZ_OFFSET 0x4b
857#define QUERY_DEV_LIM_MAX_SG_OFFSET 0x51
858#define QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET 0x52
859#define QUERY_DEV_LIM_MAX_SG_RQ_OFFSET 0x55
860#define QUERY_DEV_LIM_MAX_DESC_SZ_RQ_OFFSET 0x56
861#define QUERY_DEV_LIM_MAX_QP_MCG_OFFSET 0x61
862#define QUERY_DEV_LIM_RSVD_MCG_OFFSET 0x62
863#define QUERY_DEV_LIM_MAX_MCG_OFFSET 0x63
864#define QUERY_DEV_LIM_RSVD_PD_OFFSET 0x64
865#define QUERY_DEV_LIM_MAX_PD_OFFSET 0x65
866#define QUERY_DEV_LIM_RSVD_RDD_OFFSET 0x66
867#define QUERY_DEV_LIM_MAX_RDD_OFFSET 0x67
868#define QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET 0x80
869#define QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET 0x82
870#define QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET 0x84
871#define QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET 0x86
872#define QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET 0x88
873#define QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET 0x8a
874#define QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET 0x8c
875#define QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET 0x8e
876#define QUERY_DEV_LIM_MTT_ENTRY_SZ_OFFSET 0x90
877#define QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET 0x92
878#define QUERY_DEV_LIM_PBL_SZ_OFFSET 0x96
879#define QUERY_DEV_LIM_BMME_FLAGS_OFFSET 0x97
880#define QUERY_DEV_LIM_RSVD_LKEY_OFFSET 0x98
881#define QUERY_DEV_LIM_LAMR_OFFSET 0x9f
882#define QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET 0xa0
883
884 outbox = pci_alloc_consistent(dev->pdev, QUERY_DEV_LIM_OUT_SIZE, &outdma);
885 if (!outbox)
886 return -ENOMEM;
887
888 err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_DEV_LIM,
889 CMD_TIME_CLASS_A, status);
890
891 if (err)
892 goto out;
893
894 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_SZ_OFFSET);
895 dev_lim->max_srq_sz = 1 << field;
896 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_SZ_OFFSET);
897 dev_lim->max_qp_sz = 1 << field;
898 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_QP_OFFSET);
899 dev_lim->reserved_qps = 1 << (field & 0xf);
900 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_OFFSET);
901 dev_lim->max_qps = 1 << (field & 0x1f);
902 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_SRQ_OFFSET);
903 dev_lim->reserved_srqs = 1 << (field >> 4);
904 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SRQ_OFFSET);
905 dev_lim->max_srqs = 1 << (field & 0x1f);
906 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EEC_OFFSET);
907 dev_lim->reserved_eecs = 1 << (field & 0xf);
908 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EEC_OFFSET);
909 dev_lim->max_eecs = 1 << (field & 0x1f);
910 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_SZ_OFFSET);
911 dev_lim->max_cq_sz = 1 << field;
912 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_CQ_OFFSET);
913 dev_lim->reserved_cqs = 1 << (field & 0xf);
914 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_CQ_OFFSET);
915 dev_lim->max_cqs = 1 << (field & 0x1f);
916 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MPT_OFFSET);
917 dev_lim->max_mpts = 1 << (field & 0x3f);
918 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_EQ_OFFSET);
919 dev_lim->reserved_eqs = 1 << (field & 0xf);
920 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_EQ_OFFSET);
921 dev_lim->max_eqs = 1 << (field & 0x7);
922 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MTT_OFFSET);
923 dev_lim->reserved_mtts = 1 << (field >> 4);
924 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MRW_SZ_OFFSET);
925 dev_lim->max_mrw_sz = 1 << field;
926 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MRW_OFFSET);
927 dev_lim->reserved_mrws = 1 << (field & 0xf);
928 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MTT_SEG_OFFSET);
929 dev_lim->max_mtt_seg = 1 << (field & 0x3f);
930 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_REQ_QP_OFFSET);
931 dev_lim->max_requester_per_qp = 1 << (field & 0x3f);
932 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RES_QP_OFFSET);
933 dev_lim->max_responder_per_qp = 1 << (field & 0x3f);
934 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDMA_OFFSET);
935 dev_lim->max_rdma_global = 1 << (field & 0x3f);
936 MTHCA_GET(field, outbox, QUERY_DEV_LIM_ACK_DELAY_OFFSET);
937 dev_lim->local_ca_ack_delay = field & 0x1f;
938 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MTU_WIDTH_OFFSET);
939 dev_lim->max_mtu = field >> 4;
940 dev_lim->max_port_width = field & 0xf;
941 MTHCA_GET(field, outbox, QUERY_DEV_LIM_VL_PORT_OFFSET);
942 dev_lim->max_vl = field >> 4;
943 dev_lim->num_ports = field & 0xf;
944 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_GID_OFFSET);
945 dev_lim->max_gids = 1 << (field & 0xf);
946 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PKEY_OFFSET);
947 dev_lim->max_pkeys = 1 << (field & 0xf);
948 MTHCA_GET(dev_lim->flags, outbox, QUERY_DEV_LIM_FLAGS_OFFSET);
949 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_UAR_OFFSET);
950 dev_lim->reserved_uars = field >> 4;
951 MTHCA_GET(field, outbox, QUERY_DEV_LIM_UAR_SZ_OFFSET);
952 dev_lim->uar_size = 1 << ((field & 0x3f) + 20);
953 MTHCA_GET(field, outbox, QUERY_DEV_LIM_PAGE_SZ_OFFSET);
954 dev_lim->min_page_sz = 1 << field;
955 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_OFFSET);
956 dev_lim->max_sg = field;
957
958 MTHCA_GET(size, outbox, QUERY_DEV_LIM_MAX_DESC_SZ_OFFSET);
959 dev_lim->max_desc_sz = size;
960
961 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_QP_MCG_OFFSET);
962 dev_lim->max_qp_per_mcg = 1 << field;
963 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_MCG_OFFSET);
964 dev_lim->reserved_mgms = field & 0xf;
965 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_MCG_OFFSET);
966 dev_lim->max_mcgs = 1 << field;
967 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_PD_OFFSET);
968 dev_lim->reserved_pds = field >> 4;
969 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_PD_OFFSET);
970 dev_lim->max_pds = 1 << (field & 0x3f);
971 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSVD_RDD_OFFSET);
972 dev_lim->reserved_rdds = field >> 4;
973 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_RDD_OFFSET);
974 dev_lim->max_rdds = 1 << (field & 0x3f);
975
976 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEC_ENTRY_SZ_OFFSET);
977 dev_lim->eec_entry_sz = size;
978 MTHCA_GET(size, outbox, QUERY_DEV_LIM_QPC_ENTRY_SZ_OFFSET);
979 dev_lim->qpc_entry_sz = size;
980 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EEEC_ENTRY_SZ_OFFSET);
981 dev_lim->eeec_entry_sz = size;
982 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQPC_ENTRY_SZ_OFFSET);
983 dev_lim->eqpc_entry_sz = size;
984 MTHCA_GET(size, outbox, QUERY_DEV_LIM_EQC_ENTRY_SZ_OFFSET);
985 dev_lim->eqc_entry_sz = size;
986 MTHCA_GET(size, outbox, QUERY_DEV_LIM_CQC_ENTRY_SZ_OFFSET);
987 dev_lim->cqc_entry_sz = size;
988 MTHCA_GET(size, outbox, QUERY_DEV_LIM_SRQ_ENTRY_SZ_OFFSET);
989 dev_lim->srq_entry_sz = size;
990 MTHCA_GET(size, outbox, QUERY_DEV_LIM_UAR_ENTRY_SZ_OFFSET);
991 dev_lim->uar_scratch_entry_sz = size;
992
993 mthca_dbg(dev, "Max QPs: %d, reserved QPs: %d, entry size: %d\n",
994 dev_lim->max_qps, dev_lim->reserved_qps, dev_lim->qpc_entry_sz);
995 mthca_dbg(dev, "Max CQs: %d, reserved CQs: %d, entry size: %d\n",
996 dev_lim->max_cqs, dev_lim->reserved_cqs, dev_lim->cqc_entry_sz);
997 mthca_dbg(dev, "Max EQs: %d, reserved EQs: %d, entry size: %d\n",
998 dev_lim->max_eqs, dev_lim->reserved_eqs, dev_lim->eqc_entry_sz);
999 mthca_dbg(dev, "reserved MPTs: %d, reserved MTTs: %d\n",
1000 dev_lim->reserved_mrws, dev_lim->reserved_mtts);
1001 mthca_dbg(dev, "Max PDs: %d, reserved PDs: %d, reserved UARs: %d\n",
1002 dev_lim->max_pds, dev_lim->reserved_pds, dev_lim->reserved_uars);
1003 mthca_dbg(dev, "Max QP/MCG: %d, reserved MGMs: %d\n",
1004 dev_lim->max_pds, dev_lim->reserved_mgms);
1005
1006 mthca_dbg(dev, "Flags: %08x\n", dev_lim->flags);
1007
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001008 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 MTHCA_GET(field, outbox, QUERY_DEV_LIM_RSZ_SRQ_OFFSET);
1010 dev_lim->hca.arbel.resize_srq = field & 1;
Roland Dreier8cf2daf2005-04-16 15:26:14 -07001011 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_SG_RQ_OFFSET);
1012 dev_lim->max_sg = min_t(int, field, dev_lim->max_sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 MTHCA_GET(size, outbox, QUERY_DEV_LIM_MPT_ENTRY_SZ_OFFSET);
1014 dev_lim->mpt_entry_sz = size;
1015 MTHCA_GET(field, outbox, QUERY_DEV_LIM_PBL_SZ_OFFSET);
1016 dev_lim->hca.arbel.max_pbl_sz = 1 << (field & 0x3f);
1017 MTHCA_GET(dev_lim->hca.arbel.bmme_flags, outbox,
1018 QUERY_DEV_LIM_BMME_FLAGS_OFFSET);
1019 MTHCA_GET(dev_lim->hca.arbel.reserved_lkey, outbox,
1020 QUERY_DEV_LIM_RSVD_LKEY_OFFSET);
1021 MTHCA_GET(field, outbox, QUERY_DEV_LIM_LAMR_OFFSET);
1022 dev_lim->hca.arbel.lam_required = field & 1;
1023 MTHCA_GET(dev_lim->hca.arbel.max_icm_sz, outbox,
1024 QUERY_DEV_LIM_MAX_ICM_SZ_OFFSET);
1025
1026 if (dev_lim->hca.arbel.bmme_flags & 1)
1027 mthca_dbg(dev, "Base MM extensions: yes "
1028 "(flags %d, max PBL %d, rsvd L_Key %08x)\n",
1029 dev_lim->hca.arbel.bmme_flags,
1030 dev_lim->hca.arbel.max_pbl_sz,
1031 dev_lim->hca.arbel.reserved_lkey);
1032 else
1033 mthca_dbg(dev, "Base MM extensions: no\n");
1034
1035 mthca_dbg(dev, "Max ICM size %lld MB\n",
1036 (unsigned long long) dev_lim->hca.arbel.max_icm_sz >> 20);
1037 } else {
1038 MTHCA_GET(field, outbox, QUERY_DEV_LIM_MAX_AV_OFFSET);
1039 dev_lim->hca.tavor.max_avs = 1 << (field & 0x3f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 dev_lim->mpt_entry_sz = MTHCA_MPT_ENTRY_SIZE;
1041 }
1042
1043out:
1044 pci_free_consistent(dev->pdev, QUERY_DEV_LIM_OUT_SIZE, outbox, outdma);
1045 return err;
1046}
1047
1048int mthca_QUERY_ADAPTER(struct mthca_dev *dev,
1049 struct mthca_adapter *adapter, u8 *status)
1050{
1051 u32 *outbox;
1052 dma_addr_t outdma;
1053 int err;
1054
1055#define QUERY_ADAPTER_OUT_SIZE 0x100
1056#define QUERY_ADAPTER_VENDOR_ID_OFFSET 0x00
1057#define QUERY_ADAPTER_DEVICE_ID_OFFSET 0x04
1058#define QUERY_ADAPTER_REVISION_ID_OFFSET 0x08
1059#define QUERY_ADAPTER_INTA_PIN_OFFSET 0x10
1060
1061 outbox = pci_alloc_consistent(dev->pdev, QUERY_ADAPTER_OUT_SIZE, &outdma);
1062 if (!outbox)
1063 return -ENOMEM;
1064
1065 err = mthca_cmd_box(dev, 0, outdma, 0, 0, CMD_QUERY_ADAPTER,
1066 CMD_TIME_CLASS_A, status);
1067
1068 if (err)
1069 goto out;
1070
1071 MTHCA_GET(adapter->vendor_id, outbox, QUERY_ADAPTER_VENDOR_ID_OFFSET);
1072 MTHCA_GET(adapter->device_id, outbox, QUERY_ADAPTER_DEVICE_ID_OFFSET);
1073 MTHCA_GET(adapter->revision_id, outbox, QUERY_ADAPTER_REVISION_ID_OFFSET);
1074 MTHCA_GET(adapter->inta_pin, outbox, QUERY_ADAPTER_INTA_PIN_OFFSET);
1075
1076out:
1077 pci_free_consistent(dev->pdev, QUERY_DEV_LIM_OUT_SIZE, outbox, outdma);
1078 return err;
1079}
1080
1081int mthca_INIT_HCA(struct mthca_dev *dev,
1082 struct mthca_init_hca_param *param,
1083 u8 *status)
1084{
1085 u32 *inbox;
1086 dma_addr_t indma;
1087 int err;
1088
1089#define INIT_HCA_IN_SIZE 0x200
1090#define INIT_HCA_FLAGS_OFFSET 0x014
1091#define INIT_HCA_QPC_OFFSET 0x020
1092#define INIT_HCA_QPC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x10)
1093#define INIT_HCA_LOG_QP_OFFSET (INIT_HCA_QPC_OFFSET + 0x17)
1094#define INIT_HCA_EEC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x20)
1095#define INIT_HCA_LOG_EEC_OFFSET (INIT_HCA_QPC_OFFSET + 0x27)
1096#define INIT_HCA_SRQC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x28)
1097#define INIT_HCA_LOG_SRQ_OFFSET (INIT_HCA_QPC_OFFSET + 0x2f)
1098#define INIT_HCA_CQC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x30)
1099#define INIT_HCA_LOG_CQ_OFFSET (INIT_HCA_QPC_OFFSET + 0x37)
1100#define INIT_HCA_EQPC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x40)
1101#define INIT_HCA_EEEC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x50)
1102#define INIT_HCA_EQC_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x60)
1103#define INIT_HCA_LOG_EQ_OFFSET (INIT_HCA_QPC_OFFSET + 0x67)
1104#define INIT_HCA_RDB_BASE_OFFSET (INIT_HCA_QPC_OFFSET + 0x70)
1105#define INIT_HCA_UDAV_OFFSET 0x0b0
1106#define INIT_HCA_UDAV_LKEY_OFFSET (INIT_HCA_UDAV_OFFSET + 0x0)
1107#define INIT_HCA_UDAV_PD_OFFSET (INIT_HCA_UDAV_OFFSET + 0x4)
1108#define INIT_HCA_MCAST_OFFSET 0x0c0
1109#define INIT_HCA_MC_BASE_OFFSET (INIT_HCA_MCAST_OFFSET + 0x00)
1110#define INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x12)
1111#define INIT_HCA_MC_HASH_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x16)
1112#define INIT_HCA_LOG_MC_TABLE_SZ_OFFSET (INIT_HCA_MCAST_OFFSET + 0x1b)
1113#define INIT_HCA_TPT_OFFSET 0x0f0
1114#define INIT_HCA_MPT_BASE_OFFSET (INIT_HCA_TPT_OFFSET + 0x00)
1115#define INIT_HCA_MTT_SEG_SZ_OFFSET (INIT_HCA_TPT_OFFSET + 0x09)
1116#define INIT_HCA_LOG_MPT_SZ_OFFSET (INIT_HCA_TPT_OFFSET + 0x0b)
1117#define INIT_HCA_MTT_BASE_OFFSET (INIT_HCA_TPT_OFFSET + 0x10)
1118#define INIT_HCA_UAR_OFFSET 0x120
1119#define INIT_HCA_UAR_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x00)
1120#define INIT_HCA_UARC_SZ_OFFSET (INIT_HCA_UAR_OFFSET + 0x09)
1121#define INIT_HCA_LOG_UAR_SZ_OFFSET (INIT_HCA_UAR_OFFSET + 0x0a)
1122#define INIT_HCA_UAR_PAGE_SZ_OFFSET (INIT_HCA_UAR_OFFSET + 0x0b)
1123#define INIT_HCA_UAR_SCATCH_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x10)
1124#define INIT_HCA_UAR_CTX_BASE_OFFSET (INIT_HCA_UAR_OFFSET + 0x18)
1125
1126 inbox = pci_alloc_consistent(dev->pdev, INIT_HCA_IN_SIZE, &indma);
1127 if (!inbox)
1128 return -ENOMEM;
1129
1130 memset(inbox, 0, INIT_HCA_IN_SIZE);
1131
1132#if defined(__LITTLE_ENDIAN)
1133 *(inbox + INIT_HCA_FLAGS_OFFSET / 4) &= ~cpu_to_be32(1 << 1);
1134#elif defined(__BIG_ENDIAN)
1135 *(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1 << 1);
1136#else
1137#error Host endianness not defined
1138#endif
1139 /* Check port for UD address vector: */
1140 *(inbox + INIT_HCA_FLAGS_OFFSET / 4) |= cpu_to_be32(1);
1141
1142 /* We leave wqe_quota, responder_exu, etc as 0 (default) */
1143
1144 /* QPC/EEC/CQC/EQC/RDB attributes */
1145
1146 MTHCA_PUT(inbox, param->qpc_base, INIT_HCA_QPC_BASE_OFFSET);
1147 MTHCA_PUT(inbox, param->log_num_qps, INIT_HCA_LOG_QP_OFFSET);
1148 MTHCA_PUT(inbox, param->eec_base, INIT_HCA_EEC_BASE_OFFSET);
1149 MTHCA_PUT(inbox, param->log_num_eecs, INIT_HCA_LOG_EEC_OFFSET);
1150 MTHCA_PUT(inbox, param->srqc_base, INIT_HCA_SRQC_BASE_OFFSET);
1151 MTHCA_PUT(inbox, param->log_num_srqs, INIT_HCA_LOG_SRQ_OFFSET);
1152 MTHCA_PUT(inbox, param->cqc_base, INIT_HCA_CQC_BASE_OFFSET);
1153 MTHCA_PUT(inbox, param->log_num_cqs, INIT_HCA_LOG_CQ_OFFSET);
1154 MTHCA_PUT(inbox, param->eqpc_base, INIT_HCA_EQPC_BASE_OFFSET);
1155 MTHCA_PUT(inbox, param->eeec_base, INIT_HCA_EEEC_BASE_OFFSET);
1156 MTHCA_PUT(inbox, param->eqc_base, INIT_HCA_EQC_BASE_OFFSET);
1157 MTHCA_PUT(inbox, param->log_num_eqs, INIT_HCA_LOG_EQ_OFFSET);
1158 MTHCA_PUT(inbox, param->rdb_base, INIT_HCA_RDB_BASE_OFFSET);
1159
1160 /* UD AV attributes */
1161
1162 /* multicast attributes */
1163
1164 MTHCA_PUT(inbox, param->mc_base, INIT_HCA_MC_BASE_OFFSET);
1165 MTHCA_PUT(inbox, param->log_mc_entry_sz, INIT_HCA_LOG_MC_ENTRY_SZ_OFFSET);
1166 MTHCA_PUT(inbox, param->mc_hash_sz, INIT_HCA_MC_HASH_SZ_OFFSET);
1167 MTHCA_PUT(inbox, param->log_mc_table_sz, INIT_HCA_LOG_MC_TABLE_SZ_OFFSET);
1168
1169 /* TPT attributes */
1170
1171 MTHCA_PUT(inbox, param->mpt_base, INIT_HCA_MPT_BASE_OFFSET);
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001172 if (!mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 MTHCA_PUT(inbox, param->mtt_seg_sz, INIT_HCA_MTT_SEG_SZ_OFFSET);
1174 MTHCA_PUT(inbox, param->log_mpt_sz, INIT_HCA_LOG_MPT_SZ_OFFSET);
1175 MTHCA_PUT(inbox, param->mtt_base, INIT_HCA_MTT_BASE_OFFSET);
1176
1177 /* UAR attributes */
1178 {
1179 u8 uar_page_sz = PAGE_SHIFT - 12;
1180 MTHCA_PUT(inbox, uar_page_sz, INIT_HCA_UAR_PAGE_SZ_OFFSET);
1181 }
1182
1183 MTHCA_PUT(inbox, param->uar_scratch_base, INIT_HCA_UAR_SCATCH_BASE_OFFSET);
1184
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001185 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 MTHCA_PUT(inbox, param->log_uarc_sz, INIT_HCA_UARC_SZ_OFFSET);
1187 MTHCA_PUT(inbox, param->log_uar_sz, INIT_HCA_LOG_UAR_SZ_OFFSET);
1188 MTHCA_PUT(inbox, param->uarc_base, INIT_HCA_UAR_CTX_BASE_OFFSET);
1189 }
1190
1191 err = mthca_cmd(dev, indma, 0, 0, CMD_INIT_HCA,
1192 HZ, status);
1193
1194 pci_free_consistent(dev->pdev, INIT_HCA_IN_SIZE, inbox, indma);
1195 return err;
1196}
1197
1198int mthca_INIT_IB(struct mthca_dev *dev,
1199 struct mthca_init_ib_param *param,
1200 int port, u8 *status)
1201{
1202 u32 *inbox;
1203 dma_addr_t indma;
1204 int err;
1205 u32 flags;
1206
1207#define INIT_IB_IN_SIZE 56
1208#define INIT_IB_FLAGS_OFFSET 0x00
1209#define INIT_IB_FLAG_SIG (1 << 18)
1210#define INIT_IB_FLAG_NG (1 << 17)
1211#define INIT_IB_FLAG_G0 (1 << 16)
1212#define INIT_IB_FLAG_1X (1 << 8)
1213#define INIT_IB_FLAG_4X (1 << 9)
1214#define INIT_IB_FLAG_12X (1 << 11)
1215#define INIT_IB_VL_SHIFT 4
1216#define INIT_IB_MTU_SHIFT 12
1217#define INIT_IB_MAX_GID_OFFSET 0x06
1218#define INIT_IB_MAX_PKEY_OFFSET 0x0a
1219#define INIT_IB_GUID0_OFFSET 0x10
1220#define INIT_IB_NODE_GUID_OFFSET 0x18
1221#define INIT_IB_SI_GUID_OFFSET 0x20
1222
1223 inbox = pci_alloc_consistent(dev->pdev, INIT_IB_IN_SIZE, &indma);
1224 if (!inbox)
1225 return -ENOMEM;
1226
1227 memset(inbox, 0, INIT_IB_IN_SIZE);
1228
1229 flags = 0;
1230 flags |= param->enable_1x ? INIT_IB_FLAG_1X : 0;
1231 flags |= param->enable_4x ? INIT_IB_FLAG_4X : 0;
1232 flags |= param->set_guid0 ? INIT_IB_FLAG_G0 : 0;
1233 flags |= param->set_node_guid ? INIT_IB_FLAG_NG : 0;
1234 flags |= param->set_si_guid ? INIT_IB_FLAG_SIG : 0;
1235 flags |= param->vl_cap << INIT_IB_VL_SHIFT;
1236 flags |= param->mtu_cap << INIT_IB_MTU_SHIFT;
1237 MTHCA_PUT(inbox, flags, INIT_IB_FLAGS_OFFSET);
1238
1239 MTHCA_PUT(inbox, param->gid_cap, INIT_IB_MAX_GID_OFFSET);
1240 MTHCA_PUT(inbox, param->pkey_cap, INIT_IB_MAX_PKEY_OFFSET);
1241 MTHCA_PUT(inbox, param->guid0, INIT_IB_GUID0_OFFSET);
1242 MTHCA_PUT(inbox, param->node_guid, INIT_IB_NODE_GUID_OFFSET);
1243 MTHCA_PUT(inbox, param->si_guid, INIT_IB_SI_GUID_OFFSET);
1244
1245 err = mthca_cmd(dev, indma, port, 0, CMD_INIT_IB,
1246 CMD_TIME_CLASS_A, status);
1247
1248 pci_free_consistent(dev->pdev, INIT_HCA_IN_SIZE, inbox, indma);
1249 return err;
1250}
1251
1252int mthca_CLOSE_IB(struct mthca_dev *dev, int port, u8 *status)
1253{
1254 return mthca_cmd(dev, 0, port, 0, CMD_CLOSE_IB, HZ, status);
1255}
1256
1257int mthca_CLOSE_HCA(struct mthca_dev *dev, int panic, u8 *status)
1258{
1259 return mthca_cmd(dev, 0, 0, panic, CMD_CLOSE_HCA, HZ, status);
1260}
1261
1262int mthca_SET_IB(struct mthca_dev *dev, struct mthca_set_ib_param *param,
1263 int port, u8 *status)
1264{
1265 u32 *inbox;
1266 dma_addr_t indma;
1267 int err;
1268 u32 flags = 0;
1269
1270#define SET_IB_IN_SIZE 0x40
1271#define SET_IB_FLAGS_OFFSET 0x00
1272#define SET_IB_FLAG_SIG (1 << 18)
1273#define SET_IB_FLAG_RQK (1 << 0)
1274#define SET_IB_CAP_MASK_OFFSET 0x04
1275#define SET_IB_SI_GUID_OFFSET 0x08
1276
1277 inbox = pci_alloc_consistent(dev->pdev, SET_IB_IN_SIZE, &indma);
1278 if (!inbox)
1279 return -ENOMEM;
1280
1281 memset(inbox, 0, SET_IB_IN_SIZE);
1282
1283 flags |= param->set_si_guid ? SET_IB_FLAG_SIG : 0;
1284 flags |= param->reset_qkey_viol ? SET_IB_FLAG_RQK : 0;
1285 MTHCA_PUT(inbox, flags, SET_IB_FLAGS_OFFSET);
1286
1287 MTHCA_PUT(inbox, param->cap_mask, SET_IB_CAP_MASK_OFFSET);
1288 MTHCA_PUT(inbox, param->si_guid, SET_IB_SI_GUID_OFFSET);
1289
1290 err = mthca_cmd(dev, indma, port, 0, CMD_SET_IB,
1291 CMD_TIME_CLASS_B, status);
1292
1293 pci_free_consistent(dev->pdev, INIT_HCA_IN_SIZE, inbox, indma);
1294 return err;
1295}
1296
1297int mthca_MAP_ICM(struct mthca_dev *dev, struct mthca_icm *icm, u64 virt, u8 *status)
1298{
1299 return mthca_map_cmd(dev, CMD_MAP_ICM, icm, virt, status);
1300}
1301
1302int mthca_MAP_ICM_page(struct mthca_dev *dev, u64 dma_addr, u64 virt, u8 *status)
1303{
1304 u64 *inbox;
1305 dma_addr_t indma;
1306 int err;
1307
1308 inbox = pci_alloc_consistent(dev->pdev, 16, &indma);
1309 if (!inbox)
1310 return -ENOMEM;
1311
1312 inbox[0] = cpu_to_be64(virt);
1313 inbox[1] = cpu_to_be64(dma_addr);
1314
1315 err = mthca_cmd(dev, indma, 1, 0, CMD_MAP_ICM, CMD_TIME_CLASS_B, status);
1316
1317 pci_free_consistent(dev->pdev, 16, inbox, indma);
1318
1319 if (!err)
Roland Dreier6bd62282005-04-16 15:26:31 -07001320 mthca_dbg(dev, "Mapped page at %llx to %llx for ICM.\n",
1321 (unsigned long long) dma_addr, (unsigned long long) virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 return err;
1324}
1325
1326int mthca_UNMAP_ICM(struct mthca_dev *dev, u64 virt, u32 page_count, u8 *status)
1327{
1328 mthca_dbg(dev, "Unmapping %d pages at %llx from ICM.\n",
1329 page_count, (unsigned long long) virt);
1330
1331 return mthca_cmd(dev, virt, page_count, 0, CMD_UNMAP_ICM, CMD_TIME_CLASS_B, status);
1332}
1333
1334int mthca_MAP_ICM_AUX(struct mthca_dev *dev, struct mthca_icm *icm, u8 *status)
1335{
1336 return mthca_map_cmd(dev, CMD_MAP_ICM_AUX, icm, -1, status);
1337}
1338
1339int mthca_UNMAP_ICM_AUX(struct mthca_dev *dev, u8 *status)
1340{
1341 return mthca_cmd(dev, 0, 0, 0, CMD_UNMAP_ICM_AUX, CMD_TIME_CLASS_B, status);
1342}
1343
1344int mthca_SET_ICM_SIZE(struct mthca_dev *dev, u64 icm_size, u64 *aux_pages,
1345 u8 *status)
1346{
1347 int ret = mthca_cmd_imm(dev, icm_size, aux_pages, 0, 0, CMD_SET_ICM_SIZE,
1348 CMD_TIME_CLASS_A, status);
1349
1350 if (ret || status)
1351 return ret;
1352
1353 /*
1354 * Arbel page size is always 4 KB; round up number of system
1355 * pages needed.
1356 */
1357 *aux_pages = (*aux_pages + (1 << (PAGE_SHIFT - 12)) - 1) >> (PAGE_SHIFT - 12);
1358
1359 return 0;
1360}
1361
1362int mthca_SW2HW_MPT(struct mthca_dev *dev, void *mpt_entry,
1363 int mpt_index, u8 *status)
1364{
1365 dma_addr_t indma;
1366 int err;
1367
1368 indma = pci_map_single(dev->pdev, mpt_entry,
1369 MTHCA_MPT_ENTRY_SIZE,
1370 PCI_DMA_TODEVICE);
1371 if (pci_dma_mapping_error(indma))
1372 return -ENOMEM;
1373
1374 err = mthca_cmd(dev, indma, mpt_index, 0, CMD_SW2HW_MPT,
1375 CMD_TIME_CLASS_B, status);
1376
1377 pci_unmap_single(dev->pdev, indma,
1378 MTHCA_MPT_ENTRY_SIZE, PCI_DMA_TODEVICE);
1379 return err;
1380}
1381
1382int mthca_HW2SW_MPT(struct mthca_dev *dev, void *mpt_entry,
1383 int mpt_index, u8 *status)
1384{
1385 dma_addr_t outdma = 0;
1386 int err;
1387
1388 if (mpt_entry) {
1389 outdma = pci_map_single(dev->pdev, mpt_entry,
1390 MTHCA_MPT_ENTRY_SIZE,
1391 PCI_DMA_FROMDEVICE);
1392 if (pci_dma_mapping_error(outdma))
1393 return -ENOMEM;
1394 }
1395
1396 err = mthca_cmd_box(dev, 0, outdma, mpt_index, !mpt_entry,
1397 CMD_HW2SW_MPT,
1398 CMD_TIME_CLASS_B, status);
1399
1400 if (mpt_entry)
1401 pci_unmap_single(dev->pdev, outdma,
1402 MTHCA_MPT_ENTRY_SIZE,
1403 PCI_DMA_FROMDEVICE);
1404 return err;
1405}
1406
1407int mthca_WRITE_MTT(struct mthca_dev *dev, u64 *mtt_entry,
1408 int num_mtt, u8 *status)
1409{
1410 dma_addr_t indma;
1411 int err;
1412
1413 indma = pci_map_single(dev->pdev, mtt_entry,
1414 (num_mtt + 2) * 8,
1415 PCI_DMA_TODEVICE);
1416 if (pci_dma_mapping_error(indma))
1417 return -ENOMEM;
1418
1419 err = mthca_cmd(dev, indma, num_mtt, 0, CMD_WRITE_MTT,
1420 CMD_TIME_CLASS_B, status);
1421
1422 pci_unmap_single(dev->pdev, indma,
1423 (num_mtt + 2) * 8, PCI_DMA_TODEVICE);
1424 return err;
1425}
1426
Michael S. Tsirkinb8ca06f2005-04-16 15:26:28 -07001427int mthca_SYNC_TPT(struct mthca_dev *dev, u8 *status)
1428{
1429 return mthca_cmd(dev, 0, 0, 0, CMD_SYNC_TPT, CMD_TIME_CLASS_B, status);
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432int mthca_MAP_EQ(struct mthca_dev *dev, u64 event_mask, int unmap,
1433 int eq_num, u8 *status)
1434{
1435 mthca_dbg(dev, "%s mask %016llx for eqn %d\n",
1436 unmap ? "Clearing" : "Setting",
1437 (unsigned long long) event_mask, eq_num);
1438 return mthca_cmd(dev, event_mask, (unmap << 31) | eq_num,
1439 0, CMD_MAP_EQ, CMD_TIME_CLASS_B, status);
1440}
1441
1442int mthca_SW2HW_EQ(struct mthca_dev *dev, void *eq_context,
1443 int eq_num, u8 *status)
1444{
1445 dma_addr_t indma;
1446 int err;
1447
1448 indma = pci_map_single(dev->pdev, eq_context,
1449 MTHCA_EQ_CONTEXT_SIZE,
1450 PCI_DMA_TODEVICE);
1451 if (pci_dma_mapping_error(indma))
1452 return -ENOMEM;
1453
1454 err = mthca_cmd(dev, indma, eq_num, 0, CMD_SW2HW_EQ,
1455 CMD_TIME_CLASS_A, status);
1456
1457 pci_unmap_single(dev->pdev, indma,
1458 MTHCA_EQ_CONTEXT_SIZE, PCI_DMA_TODEVICE);
1459 return err;
1460}
1461
1462int mthca_HW2SW_EQ(struct mthca_dev *dev, void *eq_context,
1463 int eq_num, u8 *status)
1464{
1465 dma_addr_t outdma = 0;
1466 int err;
1467
1468 outdma = pci_map_single(dev->pdev, eq_context,
1469 MTHCA_EQ_CONTEXT_SIZE,
1470 PCI_DMA_FROMDEVICE);
1471 if (pci_dma_mapping_error(outdma))
1472 return -ENOMEM;
1473
1474 err = mthca_cmd_box(dev, 0, outdma, eq_num, 0,
1475 CMD_HW2SW_EQ,
1476 CMD_TIME_CLASS_A, status);
1477
1478 pci_unmap_single(dev->pdev, outdma,
1479 MTHCA_EQ_CONTEXT_SIZE,
1480 PCI_DMA_FROMDEVICE);
1481 return err;
1482}
1483
1484int mthca_SW2HW_CQ(struct mthca_dev *dev, void *cq_context,
1485 int cq_num, u8 *status)
1486{
1487 dma_addr_t indma;
1488 int err;
1489
1490 indma = pci_map_single(dev->pdev, cq_context,
1491 MTHCA_CQ_CONTEXT_SIZE,
1492 PCI_DMA_TODEVICE);
1493 if (pci_dma_mapping_error(indma))
1494 return -ENOMEM;
1495
1496 err = mthca_cmd(dev, indma, cq_num, 0, CMD_SW2HW_CQ,
1497 CMD_TIME_CLASS_A, status);
1498
1499 pci_unmap_single(dev->pdev, indma,
1500 MTHCA_CQ_CONTEXT_SIZE, PCI_DMA_TODEVICE);
1501 return err;
1502}
1503
1504int mthca_HW2SW_CQ(struct mthca_dev *dev, void *cq_context,
1505 int cq_num, u8 *status)
1506{
1507 dma_addr_t outdma = 0;
1508 int err;
1509
1510 outdma = pci_map_single(dev->pdev, cq_context,
1511 MTHCA_CQ_CONTEXT_SIZE,
1512 PCI_DMA_FROMDEVICE);
1513 if (pci_dma_mapping_error(outdma))
1514 return -ENOMEM;
1515
1516 err = mthca_cmd_box(dev, 0, outdma, cq_num, 0,
1517 CMD_HW2SW_CQ,
1518 CMD_TIME_CLASS_A, status);
1519
1520 pci_unmap_single(dev->pdev, outdma,
1521 MTHCA_CQ_CONTEXT_SIZE,
1522 PCI_DMA_FROMDEVICE);
1523 return err;
1524}
1525
1526int mthca_MODIFY_QP(struct mthca_dev *dev, int trans, u32 num,
1527 int is_ee, void *qp_context, u32 optmask,
1528 u8 *status)
1529{
1530 static const u16 op[] = {
1531 [MTHCA_TRANS_RST2INIT] = CMD_RST2INIT_QPEE,
1532 [MTHCA_TRANS_INIT2INIT] = CMD_INIT2INIT_QPEE,
1533 [MTHCA_TRANS_INIT2RTR] = CMD_INIT2RTR_QPEE,
1534 [MTHCA_TRANS_RTR2RTS] = CMD_RTR2RTS_QPEE,
1535 [MTHCA_TRANS_RTS2RTS] = CMD_RTS2RTS_QPEE,
1536 [MTHCA_TRANS_SQERR2RTS] = CMD_SQERR2RTS_QPEE,
1537 [MTHCA_TRANS_ANY2ERR] = CMD_2ERR_QPEE,
1538 [MTHCA_TRANS_RTS2SQD] = CMD_RTS2SQD_QPEE,
1539 [MTHCA_TRANS_SQD2SQD] = CMD_SQD2SQD_QPEE,
1540 [MTHCA_TRANS_SQD2RTS] = CMD_SQD2RTS_QPEE,
1541 [MTHCA_TRANS_ANY2RST] = CMD_ERR2RST_QPEE
1542 };
1543 u8 op_mod = 0;
1544
1545 dma_addr_t indma;
1546 int err;
1547
1548 if (trans < 0 || trans >= ARRAY_SIZE(op))
1549 return -EINVAL;
1550
1551 if (trans == MTHCA_TRANS_ANY2RST) {
1552 indma = 0;
1553 op_mod = 3; /* don't write outbox, any->reset */
1554
1555 /* For debugging */
1556 qp_context = pci_alloc_consistent(dev->pdev, MTHCA_QP_CONTEXT_SIZE,
1557 &indma);
1558 op_mod = 2; /* write outbox, any->reset */
1559 } else {
1560 indma = pci_map_single(dev->pdev, qp_context,
1561 MTHCA_QP_CONTEXT_SIZE,
1562 PCI_DMA_TODEVICE);
1563 if (pci_dma_mapping_error(indma))
1564 return -ENOMEM;
1565
1566 if (0) {
1567 int i;
1568 mthca_dbg(dev, "Dumping QP context:\n");
1569 printk(" opt param mask: %08x\n", be32_to_cpup(qp_context));
1570 for (i = 0; i < 0x100 / 4; ++i) {
1571 if (i % 8 == 0)
1572 printk(" [%02x] ", i * 4);
1573 printk(" %08x", be32_to_cpu(((u32 *) qp_context)[i + 2]));
1574 if ((i + 1) % 8 == 0)
1575 printk("\n");
1576 }
1577 }
1578 }
1579
1580 if (trans == MTHCA_TRANS_ANY2RST) {
1581 err = mthca_cmd_box(dev, 0, indma, (!!is_ee << 24) | num,
1582 op_mod, op[trans], CMD_TIME_CLASS_C, status);
1583
1584 if (0) {
1585 int i;
1586 mthca_dbg(dev, "Dumping QP context:\n");
1587 printk(" %08x\n", be32_to_cpup(qp_context));
1588 for (i = 0; i < 0x100 / 4; ++i) {
1589 if (i % 8 == 0)
1590 printk("[%02x] ", i * 4);
1591 printk(" %08x", be32_to_cpu(((u32 *) qp_context)[i + 2]));
1592 if ((i + 1) % 8 == 0)
1593 printk("\n");
1594 }
1595 }
1596
1597 } else
1598 err = mthca_cmd(dev, indma, (!!is_ee << 24) | num,
1599 op_mod, op[trans], CMD_TIME_CLASS_C, status);
1600
1601 if (trans != MTHCA_TRANS_ANY2RST)
1602 pci_unmap_single(dev->pdev, indma,
1603 MTHCA_QP_CONTEXT_SIZE, PCI_DMA_TODEVICE);
1604 else
1605 pci_free_consistent(dev->pdev, MTHCA_QP_CONTEXT_SIZE,
1606 qp_context, indma);
1607 return err;
1608}
1609
1610int mthca_QUERY_QP(struct mthca_dev *dev, u32 num, int is_ee,
1611 void *qp_context, u8 *status)
1612{
1613 dma_addr_t outdma = 0;
1614 int err;
1615
1616 outdma = pci_map_single(dev->pdev, qp_context,
1617 MTHCA_QP_CONTEXT_SIZE,
1618 PCI_DMA_FROMDEVICE);
1619 if (pci_dma_mapping_error(outdma))
1620 return -ENOMEM;
1621
1622 err = mthca_cmd_box(dev, 0, outdma, (!!is_ee << 24) | num, 0,
1623 CMD_QUERY_QPEE,
1624 CMD_TIME_CLASS_A, status);
1625
1626 pci_unmap_single(dev->pdev, outdma,
1627 MTHCA_QP_CONTEXT_SIZE,
1628 PCI_DMA_FROMDEVICE);
1629 return err;
1630}
1631
1632int mthca_CONF_SPECIAL_QP(struct mthca_dev *dev, int type, u32 qpn,
1633 u8 *status)
1634{
1635 u8 op_mod;
1636
1637 switch (type) {
1638 case IB_QPT_SMI:
1639 op_mod = 0;
1640 break;
1641 case IB_QPT_GSI:
1642 op_mod = 1;
1643 break;
1644 case IB_QPT_RAW_IPV6:
1645 op_mod = 2;
1646 break;
1647 case IB_QPT_RAW_ETY:
1648 op_mod = 3;
1649 break;
1650 default:
1651 return -EINVAL;
1652 }
1653
1654 return mthca_cmd(dev, 0, qpn, op_mod, CMD_CONF_SPECIAL_QP,
1655 CMD_TIME_CLASS_B, status);
1656}
1657
1658int mthca_MAD_IFC(struct mthca_dev *dev, int ignore_mkey, int ignore_bkey,
1659 int port, struct ib_wc* in_wc, struct ib_grh* in_grh,
1660 void *in_mad, void *response_mad, u8 *status)
1661{
1662 void *box;
1663 dma_addr_t dma;
1664 int err;
1665 u32 in_modifier = port;
1666 u8 op_modifier = 0;
1667
1668#define MAD_IFC_BOX_SIZE 0x400
1669#define MAD_IFC_MY_QPN_OFFSET 0x100
1670#define MAD_IFC_RQPN_OFFSET 0x104
1671#define MAD_IFC_SL_OFFSET 0x108
1672#define MAD_IFC_G_PATH_OFFSET 0x109
1673#define MAD_IFC_RLID_OFFSET 0x10a
1674#define MAD_IFC_PKEY_OFFSET 0x10e
1675#define MAD_IFC_GRH_OFFSET 0x140
1676
1677 box = pci_alloc_consistent(dev->pdev, MAD_IFC_BOX_SIZE, &dma);
1678 if (!box)
1679 return -ENOMEM;
1680
1681 memcpy(box, in_mad, 256);
1682
1683 /*
1684 * Key check traps can't be generated unless we have in_wc to
1685 * tell us where to send the trap.
1686 */
1687 if (ignore_mkey || !in_wc)
1688 op_modifier |= 0x1;
1689 if (ignore_bkey || !in_wc)
1690 op_modifier |= 0x2;
1691
1692 if (in_wc) {
1693 u8 val;
1694
1695 memset(box + 256, 0, 256);
1696
1697 MTHCA_PUT(box, in_wc->qp_num, MAD_IFC_MY_QPN_OFFSET);
1698 MTHCA_PUT(box, in_wc->src_qp, MAD_IFC_RQPN_OFFSET);
1699
1700 val = in_wc->sl << 4;
1701 MTHCA_PUT(box, val, MAD_IFC_SL_OFFSET);
1702
1703 val = in_wc->dlid_path_bits |
1704 (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
1705 MTHCA_PUT(box, val, MAD_IFC_GRH_OFFSET);
1706
1707 MTHCA_PUT(box, in_wc->slid, MAD_IFC_RLID_OFFSET);
1708 MTHCA_PUT(box, in_wc->pkey_index, MAD_IFC_PKEY_OFFSET);
1709
1710 if (in_grh)
1711 memcpy((u8 *) box + MAD_IFC_GRH_OFFSET, in_grh, 40);
1712
1713 op_modifier |= 0x10;
1714
1715 in_modifier |= in_wc->slid << 16;
1716 }
1717
1718 err = mthca_cmd_box(dev, dma, dma + 512, in_modifier, op_modifier,
1719 CMD_MAD_IFC, CMD_TIME_CLASS_C, status);
1720
1721 if (!err && !*status)
1722 memcpy(response_mad, box + 512, 256);
1723
1724 pci_free_consistent(dev->pdev, MAD_IFC_BOX_SIZE, box, dma);
1725 return err;
1726}
1727
1728int mthca_READ_MGM(struct mthca_dev *dev, int index, void *mgm,
1729 u8 *status)
1730{
1731 dma_addr_t outdma = 0;
1732 int err;
1733
1734 outdma = pci_map_single(dev->pdev, mgm,
1735 MTHCA_MGM_ENTRY_SIZE,
1736 PCI_DMA_FROMDEVICE);
1737 if (pci_dma_mapping_error(outdma))
1738 return -ENOMEM;
1739
1740 err = mthca_cmd_box(dev, 0, outdma, index, 0,
1741 CMD_READ_MGM,
1742 CMD_TIME_CLASS_A, status);
1743
1744 pci_unmap_single(dev->pdev, outdma,
1745 MTHCA_MGM_ENTRY_SIZE,
1746 PCI_DMA_FROMDEVICE);
1747 return err;
1748}
1749
1750int mthca_WRITE_MGM(struct mthca_dev *dev, int index, void *mgm,
1751 u8 *status)
1752{
1753 dma_addr_t indma;
1754 int err;
1755
1756 indma = pci_map_single(dev->pdev, mgm,
1757 MTHCA_MGM_ENTRY_SIZE,
1758 PCI_DMA_TODEVICE);
1759 if (pci_dma_mapping_error(indma))
1760 return -ENOMEM;
1761
1762 err = mthca_cmd(dev, indma, index, 0, CMD_WRITE_MGM,
1763 CMD_TIME_CLASS_A, status);
1764
1765 pci_unmap_single(dev->pdev, indma,
1766 MTHCA_MGM_ENTRY_SIZE, PCI_DMA_TODEVICE);
1767 return err;
1768}
1769
1770int mthca_MGID_HASH(struct mthca_dev *dev, void *gid, u16 *hash,
1771 u8 *status)
1772{
1773 dma_addr_t indma;
1774 u64 imm;
1775 int err;
1776
1777 indma = pci_map_single(dev->pdev, gid, 16, PCI_DMA_TODEVICE);
1778 if (pci_dma_mapping_error(indma))
1779 return -ENOMEM;
1780
1781 err = mthca_cmd_imm(dev, indma, &imm, 0, 0, CMD_MGID_HASH,
1782 CMD_TIME_CLASS_A, status);
1783 *hash = imm;
1784
1785 pci_unmap_single(dev->pdev, indma, 16, PCI_DMA_TODEVICE);
1786 return err;
1787}
1788
1789int mthca_NOP(struct mthca_dev *dev, u8 *status)
1790{
1791 return mthca_cmd(dev, 0, 0x1f, 0, CMD_NOP, msecs_to_jiffies(100), status);
1792}