This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / infiniband / hw / mthca / mthca_eq.c
1 /*
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_eq.c 1382 2004-12-24 02:21:02Z roland $
33  */
34
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
38 #include <linux/pci.h>
39
40 #include "mthca_dev.h"
41 #include "mthca_cmd.h"
42 #include "mthca_config_reg.h"
43
44 enum {
45         MTHCA_NUM_ASYNC_EQE = 0x80,
46         MTHCA_NUM_CMD_EQE   = 0x80,
47         MTHCA_EQ_ENTRY_SIZE = 0x20
48 };
49
50 /*
51  * Must be packed because start is 64 bits but only aligned to 32 bits.
52  */
53 struct mthca_eq_context {
54         u32 flags;
55         u64 start;
56         u32 logsize_usrpage;
57         u32 pd;
58         u8  reserved1[3];
59         u8  intr;
60         u32 lost_count;
61         u32 lkey;
62         u32 reserved2[2];
63         u32 consumer_index;
64         u32 producer_index;
65         u32 reserved3[4];
66 } __attribute__((packed));
67
68 #define MTHCA_EQ_STATUS_OK          ( 0 << 28)
69 #define MTHCA_EQ_STATUS_OVERFLOW    ( 9 << 28)
70 #define MTHCA_EQ_STATUS_WRITE_FAIL  (10 << 28)
71 #define MTHCA_EQ_OWNER_SW           ( 0 << 24)
72 #define MTHCA_EQ_OWNER_HW           ( 1 << 24)
73 #define MTHCA_EQ_FLAG_TR            ( 1 << 18)
74 #define MTHCA_EQ_FLAG_OI            ( 1 << 17)
75 #define MTHCA_EQ_STATE_ARMED        ( 1 <<  8)
76 #define MTHCA_EQ_STATE_FIRED        ( 2 <<  8)
77 #define MTHCA_EQ_STATE_ALWAYS_ARMED ( 3 <<  8)
78
79 enum {
80         MTHCA_EVENT_TYPE_COMP               = 0x00,
81         MTHCA_EVENT_TYPE_PATH_MIG           = 0x01,
82         MTHCA_EVENT_TYPE_COMM_EST           = 0x02,
83         MTHCA_EVENT_TYPE_SQ_DRAINED         = 0x03,
84         MTHCA_EVENT_TYPE_SRQ_LAST_WQE       = 0x13,
85         MTHCA_EVENT_TYPE_CQ_ERROR           = 0x04,
86         MTHCA_EVENT_TYPE_WQ_CATAS_ERROR     = 0x05,
87         MTHCA_EVENT_TYPE_EEC_CATAS_ERROR    = 0x06,
88         MTHCA_EVENT_TYPE_PATH_MIG_FAILED    = 0x07,
89         MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR = 0x10,
90         MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR    = 0x11,
91         MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR    = 0x12,
92         MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR  = 0x08,
93         MTHCA_EVENT_TYPE_PORT_CHANGE        = 0x09,
94         MTHCA_EVENT_TYPE_EQ_OVERFLOW        = 0x0f,
95         MTHCA_EVENT_TYPE_ECC_DETECT         = 0x0e,
96         MTHCA_EVENT_TYPE_CMD                = 0x0a
97 };
98
99 #define MTHCA_ASYNC_EVENT_MASK ((1ULL << MTHCA_EVENT_TYPE_PATH_MIG)           | \
100                                 (1ULL << MTHCA_EVENT_TYPE_COMM_EST)           | \
101                                 (1ULL << MTHCA_EVENT_TYPE_SQ_DRAINED)         | \
102                                 (1ULL << MTHCA_EVENT_TYPE_CQ_ERROR)           | \
103                                 (1ULL << MTHCA_EVENT_TYPE_WQ_CATAS_ERROR)     | \
104                                 (1ULL << MTHCA_EVENT_TYPE_EEC_CATAS_ERROR)    | \
105                                 (1ULL << MTHCA_EVENT_TYPE_PATH_MIG_FAILED)    | \
106                                 (1ULL << MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
107                                 (1ULL << MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR)    | \
108                                 (1ULL << MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR)  | \
109                                 (1ULL << MTHCA_EVENT_TYPE_PORT_CHANGE)        | \
110                                 (1ULL << MTHCA_EVENT_TYPE_ECC_DETECT))
111 #define MTHCA_SRQ_EVENT_MASK    (1ULL << MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR)    | \
112                                 (1ULL << MTHCA_EVENT_TYPE_SRQ_LAST_WQE)
113 #define MTHCA_CMD_EVENT_MASK    (1ULL << MTHCA_EVENT_TYPE_CMD)
114
115 #define MTHCA_EQ_DB_INC_CI     (1 << 24)
116 #define MTHCA_EQ_DB_REQ_NOT    (2 << 24)
117 #define MTHCA_EQ_DB_DISARM_CQ  (3 << 24)
118 #define MTHCA_EQ_DB_SET_CI     (4 << 24)
119 #define MTHCA_EQ_DB_ALWAYS_ARM (5 << 24)
120
121 struct mthca_eqe {
122         u8 reserved1;
123         u8 type;
124         u8 reserved2;
125         u8 subtype;
126         union {
127                 u32 raw[6];
128                 struct {
129                         u32 cqn;
130                 } __attribute__((packed)) comp;
131                 struct {
132                         u16 reserved1;
133                         u16 token;
134                         u32 reserved2;
135                         u8  reserved3[3];
136                         u8  status;
137                         u64 out_param;
138                 } __attribute__((packed)) cmd;
139                 struct {
140                         u32 qpn;
141                 } __attribute__((packed)) qp;
142                 struct {
143                         u32 cqn;
144                         u32 reserved1;
145                         u8  reserved2[3];
146                         u8  syndrome;
147                 } __attribute__((packed)) cq_err;
148                 struct {
149                         u32 reserved1[2];
150                         u32 port;
151                 } __attribute__((packed)) port_change;
152         } event;
153         u8 reserved3[3];
154         u8 owner;
155 } __attribute__((packed));
156
157 #define  MTHCA_EQ_ENTRY_OWNER_SW      (0 << 7)
158 #define  MTHCA_EQ_ENTRY_OWNER_HW      (1 << 7)
159
160 static inline u64 async_mask(struct mthca_dev *dev)
161 {
162         return dev->mthca_flags & MTHCA_FLAG_SRQ ?
163                 MTHCA_ASYNC_EVENT_MASK | MTHCA_SRQ_EVENT_MASK :
164                 MTHCA_ASYNC_EVENT_MASK;
165 }
166
167 static inline void set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
168 {
169         u32 doorbell[2];
170
171         doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_SET_CI | eq->eqn);
172         doorbell[1] = cpu_to_be32(ci & (eq->nent - 1));
173
174         mthca_write64(doorbell,
175                       dev->kar + MTHCA_EQ_DOORBELL,
176                       MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
177 }
178
179 static inline void eq_req_not(struct mthca_dev *dev, int eqn)
180 {
181         u32 doorbell[2];
182
183         doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_REQ_NOT | eqn);
184         doorbell[1] = 0;
185
186         mthca_write64(doorbell,
187                       dev->kar + MTHCA_EQ_DOORBELL,
188                       MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
189 }
190
191 static inline void disarm_cq(struct mthca_dev *dev, int eqn, int cqn)
192 {
193         u32 doorbell[2];
194
195         doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_DISARM_CQ | eqn);
196         doorbell[1] = cpu_to_be32(cqn);
197
198         mthca_write64(doorbell,
199                       dev->kar + MTHCA_EQ_DOORBELL,
200                       MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
201 }
202
203 static inline struct mthca_eqe *get_eqe(struct mthca_eq *eq, u32 entry)
204 {
205         unsigned long off = (entry & (eq->nent - 1)) * MTHCA_EQ_ENTRY_SIZE;
206         return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
207 }
208
209 static inline struct mthca_eqe* next_eqe_sw(struct mthca_eq *eq)
210 {
211         struct mthca_eqe* eqe;
212         eqe = get_eqe(eq, eq->cons_index);
213         return (MTHCA_EQ_ENTRY_OWNER_HW & eqe->owner) ? NULL : eqe;
214 }
215
216 static inline void set_eqe_hw(struct mthca_eqe *eqe)
217 {
218         eqe->owner =  MTHCA_EQ_ENTRY_OWNER_HW;
219 }
220
221 static void port_change(struct mthca_dev *dev, int port, int active)
222 {
223         struct ib_event record;
224
225         mthca_dbg(dev, "Port change to %s for port %d\n",
226                   active ? "active" : "down", port);
227
228         record.device = &dev->ib_dev;
229         record.event  = active ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
230         record.element.port_num = port;
231
232         ib_dispatch_event(&record);
233 }
234
235 static void mthca_eq_int(struct mthca_dev *dev, struct mthca_eq *eq)
236 {
237         struct mthca_eqe *eqe;
238         int disarm_cqn;
239         int  eqes_found = 0;
240
241         while ((eqe = next_eqe_sw(eq))) {
242                 int set_ci = 0;
243
244                 /*
245                  * Make sure we read EQ entry contents after we've
246                  * checked the ownership bit.
247                  */
248                 rmb();
249
250                 switch (eqe->type) {
251                 case MTHCA_EVENT_TYPE_COMP:
252                         disarm_cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
253                         disarm_cq(dev, eq->eqn, disarm_cqn);
254                         mthca_cq_event(dev, disarm_cqn);
255                         break;
256
257                 case MTHCA_EVENT_TYPE_PATH_MIG:
258                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
259                                        IB_EVENT_PATH_MIG);
260                         break;
261
262                 case MTHCA_EVENT_TYPE_COMM_EST:
263                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
264                                        IB_EVENT_COMM_EST);
265                         break;
266
267                 case MTHCA_EVENT_TYPE_SQ_DRAINED:
268                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
269                                        IB_EVENT_SQ_DRAINED);
270                         break;
271
272                 case MTHCA_EVENT_TYPE_WQ_CATAS_ERROR:
273                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
274                                        IB_EVENT_QP_FATAL);
275                         break;
276
277                 case MTHCA_EVENT_TYPE_PATH_MIG_FAILED:
278                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
279                                        IB_EVENT_PATH_MIG_ERR);
280                         break;
281
282                 case MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
283                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
284                                        IB_EVENT_QP_REQ_ERR);
285                         break;
286
287                 case MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR:
288                         mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
289                                        IB_EVENT_QP_ACCESS_ERR);
290                         break;
291
292                 case MTHCA_EVENT_TYPE_CMD:
293                         mthca_cmd_event(dev,
294                                         be16_to_cpu(eqe->event.cmd.token),
295                                         eqe->event.cmd.status,
296                                         be64_to_cpu(eqe->event.cmd.out_param));
297                         /*
298                          * cmd_event() may add more commands.
299                          * The card will think the queue has overflowed if
300                          * we don't tell it we've been processing events.
301                          */
302                         set_ci = 1;
303                         break;
304
305                 case MTHCA_EVENT_TYPE_PORT_CHANGE:
306                         port_change(dev,
307                                     (be32_to_cpu(eqe->event.port_change.port) >> 28) & 3,
308                                     eqe->subtype == 0x4);
309                         break;
310
311                 case MTHCA_EVENT_TYPE_CQ_ERROR:
312                         mthca_warn(dev, "CQ %s on CQN %08x\n",
313                                    eqe->event.cq_err.syndrome == 1 ?
314                                    "overrun" : "access violation",
315                                    be32_to_cpu(eqe->event.cq_err.cqn));
316                         break;
317
318                 case MTHCA_EVENT_TYPE_EQ_OVERFLOW:
319                         mthca_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
320                         break;
321
322                 case MTHCA_EVENT_TYPE_EEC_CATAS_ERROR:
323                 case MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR:
324                 case MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR:
325                 case MTHCA_EVENT_TYPE_ECC_DETECT:
326                 default:
327                         mthca_warn(dev, "Unhandled event %02x(%02x) on EQ %d\n",
328                                    eqe->type, eqe->subtype, eq->eqn);
329                         break;
330                 };
331
332                 set_eqe_hw(eqe);
333                 ++eq->cons_index;
334                 eqes_found = 1;
335
336                 if (set_ci) {
337                         wmb(); /* see comment below */
338                         set_eq_ci(dev, eq, eq->cons_index);
339                         set_ci = 0;
340                 }
341         }
342
343         /*
344          * This barrier makes sure that all updates to
345          * ownership bits done by set_eqe_hw() hit memory
346          * before the consumer index is updated.  set_eq_ci()
347          * allows the HCA to possibly write more EQ entries,
348          * and we want to avoid the exceedingly unlikely
349          * possibility of the HCA writing an entry and then
350          * having set_eqe_hw() overwrite the owner field.
351          */
352         if (likely(eqes_found)) {
353                 wmb();
354                 set_eq_ci(dev, eq, eq->cons_index);
355         }
356         eq_req_not(dev, eq->eqn);
357 }
358
359 static irqreturn_t mthca_interrupt(int irq, void *dev_ptr, struct pt_regs *regs)
360 {
361         struct mthca_dev *dev = dev_ptr;
362         u32 ecr;
363         int work = 0;
364         int i;
365
366         if (dev->eq_table.clr_mask)
367                 writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
368
369         if ((ecr = readl(dev->ecr_base + 4)) != 0) {
370                 work = 1;
371
372                 writel(ecr, dev->ecr_base +
373                        MTHCA_ECR_CLR_BASE - MTHCA_ECR_BASE + 4);
374
375                 for (i = 0; i < MTHCA_NUM_EQ; ++i)
376                         if (ecr & dev->eq_table.eq[i].ecr_mask)
377                                 mthca_eq_int(dev, &dev->eq_table.eq[i]);
378         }
379
380         return IRQ_RETVAL(work);
381 }
382
383 static irqreturn_t mthca_msi_x_interrupt(int irq, void *eq_ptr,
384                                          struct pt_regs *regs)
385 {
386         struct mthca_eq  *eq  = eq_ptr;
387         struct mthca_dev *dev = eq->dev;
388
389         mthca_eq_int(dev, eq);
390
391         /* MSI-X vectors always belong to us */
392         return IRQ_HANDLED;
393 }
394
395 static int __devinit mthca_create_eq(struct mthca_dev *dev,
396                                      int nent,
397                                      u8 intr,
398                                      struct mthca_eq *eq)
399 {
400         int npages = (nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
401                 PAGE_SIZE;
402         u64 *dma_list = NULL;
403         dma_addr_t t;
404         void *mailbox = NULL;
405         struct mthca_eq_context *eq_context;
406         int err = -ENOMEM;
407         int i;
408         u8 status;
409
410         /* Make sure EQ size is aligned to a power of 2 size. */
411         for (i = 1; i < nent; i <<= 1)
412                 ; /* nothing */
413         nent = i;
414
415         eq->dev = dev;
416
417         eq->page_list = kmalloc(npages * sizeof *eq->page_list,
418                                 GFP_KERNEL);
419         if (!eq->page_list)
420                 goto err_out;
421
422         for (i = 0; i < npages; ++i)
423                 eq->page_list[i].buf = NULL;
424
425         dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
426         if (!dma_list)
427                 goto err_out_free;
428
429         mailbox = kmalloc(sizeof *eq_context + MTHCA_CMD_MAILBOX_EXTRA,
430                           GFP_KERNEL);
431         if (!mailbox)
432                 goto err_out_free;
433         eq_context = MAILBOX_ALIGN(mailbox);
434
435         for (i = 0; i < npages; ++i) {
436                 eq->page_list[i].buf = pci_alloc_consistent(dev->pdev,
437                                                             PAGE_SIZE, &t);
438                 if (!eq->page_list[i].buf)
439                         goto err_out_free;
440
441                 dma_list[i] = t;
442                 pci_unmap_addr_set(&eq->page_list[i], mapping, t);
443
444                 memset(eq->page_list[i].buf, 0, PAGE_SIZE);
445         }
446
447         for (i = 0; i < nent; ++i)
448                 set_eqe_hw(get_eqe(eq, i));
449
450         eq->eqn = mthca_alloc(&dev->eq_table.alloc);
451         if (eq->eqn == -1)
452                 goto err_out_free;
453
454         err = mthca_mr_alloc_phys(dev, dev->driver_pd.pd_num,
455                                   dma_list, PAGE_SHIFT, npages,
456                                   0, npages * PAGE_SIZE,
457                                   MTHCA_MPT_FLAG_LOCAL_WRITE |
458                                   MTHCA_MPT_FLAG_LOCAL_READ,
459                                   &eq->mr);
460         if (err)
461                 goto err_out_free_eq;
462
463         eq->nent = nent;
464
465         memset(eq_context, 0, sizeof *eq_context);
466         eq_context->flags           = cpu_to_be32(MTHCA_EQ_STATUS_OK   |
467                                                   MTHCA_EQ_OWNER_HW    |
468                                                   MTHCA_EQ_STATE_ARMED |
469                                                   MTHCA_EQ_FLAG_TR);
470         eq_context->start           = cpu_to_be64(0);
471         eq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24 |
472                                                   MTHCA_KAR_PAGE);
473         eq_context->pd              = cpu_to_be32(dev->driver_pd.pd_num);
474         eq_context->intr            = intr;
475         eq_context->lkey            = cpu_to_be32(eq->mr.ibmr.lkey);
476
477         err = mthca_SW2HW_EQ(dev, eq_context, eq->eqn, &status);
478         if (err) {
479                 mthca_warn(dev, "SW2HW_EQ failed (%d)\n", err);
480                 goto err_out_free_mr;
481         }
482         if (status) {
483                 mthca_warn(dev, "SW2HW_EQ returned status 0x%02x\n",
484                            status);
485                 err = -EINVAL;
486                 goto err_out_free_mr;
487         }
488
489         kfree(dma_list);
490         kfree(mailbox);
491
492         eq->ecr_mask   = swab32(1 << eq->eqn);
493         eq->cons_index = 0;
494
495         eq_req_not(dev, eq->eqn);
496
497         mthca_dbg(dev, "Allocated EQ %d with %d entries\n",
498                   eq->eqn, nent);
499
500         return err;
501
502  err_out_free_mr:
503         mthca_free_mr(dev, &eq->mr);
504
505  err_out_free_eq:
506         mthca_free(&dev->eq_table.alloc, eq->eqn);
507
508  err_out_free:
509         for (i = 0; i < npages; ++i)
510                 if (eq->page_list[i].buf)
511                         pci_free_consistent(dev->pdev, PAGE_SIZE,
512                                             eq->page_list[i].buf,
513                                             pci_unmap_addr(&eq->page_list[i],
514                                                            mapping));
515
516         kfree(eq->page_list);
517         kfree(dma_list);
518         kfree(mailbox);
519
520  err_out:
521         return err;
522 }
523
524 static void mthca_free_eq(struct mthca_dev *dev,
525                           struct mthca_eq *eq)
526 {
527         void *mailbox = NULL;
528         int err;
529         u8 status;
530         int npages = (eq->nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
531                 PAGE_SIZE;
532         int i;
533
534         mailbox = kmalloc(sizeof (struct mthca_eq_context) + MTHCA_CMD_MAILBOX_EXTRA,
535                           GFP_KERNEL);
536         if (!mailbox)
537                 return;
538
539         err = mthca_HW2SW_EQ(dev, MAILBOX_ALIGN(mailbox),
540                              eq->eqn, &status);
541         if (err)
542                 mthca_warn(dev, "HW2SW_EQ failed (%d)\n", err);
543         if (status)
544                 mthca_warn(dev, "HW2SW_EQ returned status 0x%02x\n",
545                            status);
546
547         if (0) {
548                 mthca_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
549                 for (i = 0; i < sizeof (struct mthca_eq_context) / 4; ++i) {
550                         if (i % 4 == 0)
551                                 printk("[%02x] ", i * 4);
552                         printk(" %08x", be32_to_cpup(MAILBOX_ALIGN(mailbox) + i * 4));
553                         if ((i + 1) % 4 == 0)
554                                 printk("\n");
555                 }
556         }
557
558
559         mthca_free_mr(dev, &eq->mr);
560         for (i = 0; i < npages; ++i)
561                 pci_free_consistent(dev->pdev, PAGE_SIZE,
562                                     eq->page_list[i].buf,
563                                     pci_unmap_addr(&eq->page_list[i], mapping));
564
565         kfree(eq->page_list);
566         kfree(mailbox);
567 }
568
569 static void mthca_free_irqs(struct mthca_dev *dev)
570 {
571         int i;
572
573         if (dev->eq_table.have_irq)
574                 free_irq(dev->pdev->irq, dev);
575         for (i = 0; i < MTHCA_NUM_EQ; ++i)
576                 if (dev->eq_table.eq[i].have_irq)
577                         free_irq(dev->eq_table.eq[i].msi_x_vector,
578                                  dev->eq_table.eq + i);
579 }
580
581 int __devinit mthca_map_eq_icm(struct mthca_dev *dev, u64 icm_virt)
582 {
583         int ret;
584         u8 status;
585
586         /*
587          * We assume that mapping one page is enough for the whole EQ
588          * context table.  This is fine with all current HCAs, because
589          * we only use 32 EQs and each EQ uses 32 bytes of context
590          * memory, or 1 KB total.
591          */
592         dev->eq_table.icm_virt = icm_virt;
593         dev->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
594         if (!dev->eq_table.icm_page)
595                 return -ENOMEM;
596         dev->eq_table.icm_dma  = pci_map_page(dev->pdev, dev->eq_table.icm_page, 0,
597                                               PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
598         if (pci_dma_mapping_error(dev->eq_table.icm_dma)) {
599                 __free_page(dev->eq_table.icm_page);
600                 return -ENOMEM;
601         }
602
603         ret = mthca_MAP_ICM_page(dev, dev->eq_table.icm_dma, icm_virt, &status);
604         if (!ret && status)
605                 ret = -EINVAL;
606         if (ret) {
607                 pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
608                                PCI_DMA_BIDIRECTIONAL);
609                 __free_page(dev->eq_table.icm_page);
610         }
611
612         return ret;
613 }
614
615 void __devexit mthca_unmap_eq_icm(struct mthca_dev *dev)
616 {
617         u8 status;
618
619         mthca_UNMAP_ICM(dev, dev->eq_table.icm_virt, PAGE_SIZE / 4096, &status);
620         pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
621                        PCI_DMA_BIDIRECTIONAL);
622         __free_page(dev->eq_table.icm_page);
623 }
624
625 int __devinit mthca_init_eq_table(struct mthca_dev *dev)
626 {
627         int err;
628         u8 status;
629         u8 intr;
630         int i;
631
632         err = mthca_alloc_init(&dev->eq_table.alloc,
633                                dev->limits.num_eqs,
634                                dev->limits.num_eqs - 1,
635                                dev->limits.reserved_eqs);
636         if (err)
637                 return err;
638
639         if (dev->mthca_flags & MTHCA_FLAG_MSI ||
640             dev->mthca_flags & MTHCA_FLAG_MSI_X) {
641                 dev->eq_table.clr_mask = 0;
642         } else {
643                 dev->eq_table.clr_mask =
644                         swab32(1 << (dev->eq_table.inta_pin & 31));
645                 dev->eq_table.clr_int  = dev->clr_base +
646                         (dev->eq_table.inta_pin < 31 ? 4 : 0);
647         }
648
649         intr = (dev->mthca_flags & MTHCA_FLAG_MSI) ?
650                 128 : dev->eq_table.inta_pin;
651
652         err = mthca_create_eq(dev, dev->limits.num_cqs,
653                               (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr,
654                               &dev->eq_table.eq[MTHCA_EQ_COMP]);
655         if (err)
656                 goto err_out_free;
657
658         err = mthca_create_eq(dev, MTHCA_NUM_ASYNC_EQE,
659                               (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 129 : intr,
660                               &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
661         if (err)
662                 goto err_out_comp;
663
664         err = mthca_create_eq(dev, MTHCA_NUM_CMD_EQE,
665                               (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 130 : intr,
666                               &dev->eq_table.eq[MTHCA_EQ_CMD]);
667         if (err)
668                 goto err_out_async;
669
670         if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
671                 static const char *eq_name[] = {
672                         [MTHCA_EQ_COMP]  = DRV_NAME " (comp)",
673                         [MTHCA_EQ_ASYNC] = DRV_NAME " (async)",
674                         [MTHCA_EQ_CMD]   = DRV_NAME " (cmd)"
675                 };
676
677                 for (i = 0; i < MTHCA_NUM_EQ; ++i) {
678                         err = request_irq(dev->eq_table.eq[i].msi_x_vector,
679                                           mthca_msi_x_interrupt, 0,
680                                           eq_name[i], dev->eq_table.eq + i);
681                         if (err)
682                                 goto err_out_cmd;
683                         dev->eq_table.eq[i].have_irq = 1;
684                 }
685         } else {
686                 err = request_irq(dev->pdev->irq, mthca_interrupt, SA_SHIRQ,
687                                   DRV_NAME, dev);
688                 if (err)
689                         goto err_out_cmd;
690                 dev->eq_table.have_irq = 1;
691         }
692
693         err = mthca_MAP_EQ(dev, async_mask(dev),
694                            0, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
695         if (err)
696                 mthca_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
697                            dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, err);
698         if (status)
699                 mthca_warn(dev, "MAP_EQ for async EQ %d returned status 0x%02x\n",
700                            dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, status);
701
702         err = mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
703                            0, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
704         if (err)
705                 mthca_warn(dev, "MAP_EQ for cmd EQ %d failed (%d)\n",
706                            dev->eq_table.eq[MTHCA_EQ_CMD].eqn, err);
707         if (status)
708                 mthca_warn(dev, "MAP_EQ for cmd EQ %d returned status 0x%02x\n",
709                            dev->eq_table.eq[MTHCA_EQ_CMD].eqn, status);
710
711         return 0;
712
713 err_out_cmd:
714         mthca_free_irqs(dev);
715         mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_CMD]);
716
717 err_out_async:
718         mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
719
720 err_out_comp:
721         mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_COMP]);
722
723 err_out_free:
724         mthca_alloc_cleanup(&dev->eq_table.alloc);
725         return err;
726 }
727
728 void __devexit mthca_cleanup_eq_table(struct mthca_dev *dev)
729 {
730         u8 status;
731         int i;
732
733         mthca_free_irqs(dev);
734
735         mthca_MAP_EQ(dev, async_mask(dev),
736                      1, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
737         mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
738                      1, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
739
740         for (i = 0; i < MTHCA_NUM_EQ; ++i)
741                 mthca_free_eq(dev, &dev->eq_table.eq[i]);
742
743         mthca_alloc_cleanup(&dev->eq_table.alloc);
744 }