mlx4_core: initial header-file changes for SRIOV support
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx4 / eq.c
1 /*
2  * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/interrupt.h>
35 #include <linux/slab.h>
36 #include <linux/export.h>
37 #include <linux/mm.h>
38 #include <linux/dma-mapping.h>
39
40 #include <linux/mlx4/cmd.h>
41
42 #include "mlx4.h"
43 #include "fw.h"
44
45 enum {
46         MLX4_IRQNAME_SIZE       = 32
47 };
48
49 enum {
50         MLX4_NUM_ASYNC_EQE      = 0x100,
51         MLX4_NUM_SPARE_EQE      = 0x80,
52         MLX4_EQ_ENTRY_SIZE      = 0x20
53 };
54
55 /*
56  * Must be packed because start is 64 bits but only aligned to 32 bits.
57  */
58 struct mlx4_eq_context {
59         __be32                  flags;
60         u16                     reserved1[3];
61         __be16                  page_offset;
62         u8                      log_eq_size;
63         u8                      reserved2[4];
64         u8                      eq_period;
65         u8                      reserved3;
66         u8                      eq_max_count;
67         u8                      reserved4[3];
68         u8                      intr;
69         u8                      log_page_size;
70         u8                      reserved5[2];
71         u8                      mtt_base_addr_h;
72         __be32                  mtt_base_addr_l;
73         u32                     reserved6[2];
74         __be32                  consumer_index;
75         __be32                  producer_index;
76         u32                     reserved7[4];
77 };
78
79 #define MLX4_EQ_STATUS_OK          ( 0 << 28)
80 #define MLX4_EQ_STATUS_WRITE_FAIL  (10 << 28)
81 #define MLX4_EQ_OWNER_SW           ( 0 << 24)
82 #define MLX4_EQ_OWNER_HW           ( 1 << 24)
83 #define MLX4_EQ_FLAG_EC            ( 1 << 18)
84 #define MLX4_EQ_FLAG_OI            ( 1 << 17)
85 #define MLX4_EQ_STATE_ARMED        ( 9 <<  8)
86 #define MLX4_EQ_STATE_FIRED        (10 <<  8)
87 #define MLX4_EQ_STATE_ALWAYS_ARMED (11 <<  8)
88
89 #define MLX4_ASYNC_EVENT_MASK ((1ull << MLX4_EVENT_TYPE_PATH_MIG)           | \
90                                (1ull << MLX4_EVENT_TYPE_COMM_EST)           | \
91                                (1ull << MLX4_EVENT_TYPE_SQ_DRAINED)         | \
92                                (1ull << MLX4_EVENT_TYPE_CQ_ERROR)           | \
93                                (1ull << MLX4_EVENT_TYPE_WQ_CATAS_ERROR)     | \
94                                (1ull << MLX4_EVENT_TYPE_EEC_CATAS_ERROR)    | \
95                                (1ull << MLX4_EVENT_TYPE_PATH_MIG_FAILED)    | \
96                                (1ull << MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
97                                (1ull << MLX4_EVENT_TYPE_WQ_ACCESS_ERROR)    | \
98                                (1ull << MLX4_EVENT_TYPE_PORT_CHANGE)        | \
99                                (1ull << MLX4_EVENT_TYPE_ECC_DETECT)         | \
100                                (1ull << MLX4_EVENT_TYPE_SRQ_CATAS_ERROR)    | \
101                                (1ull << MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE)    | \
102                                (1ull << MLX4_EVENT_TYPE_SRQ_LIMIT)          | \
103                                (1ull << MLX4_EVENT_TYPE_CMD))
104
105 static void eq_set_ci(struct mlx4_eq *eq, int req_not)
106 {
107         __raw_writel((__force u32) cpu_to_be32((eq->cons_index & 0xffffff) |
108                                                req_not << 31),
109                      eq->doorbell);
110         /* We still want ordering, just not swabbing, so add a barrier */
111         mb();
112 }
113
114 static struct mlx4_eqe *get_eqe(struct mlx4_eq *eq, u32 entry)
115 {
116         unsigned long off = (entry & (eq->nent - 1)) * MLX4_EQ_ENTRY_SIZE;
117         return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
118 }
119
120 static struct mlx4_eqe *next_eqe_sw(struct mlx4_eq *eq)
121 {
122         struct mlx4_eqe *eqe = get_eqe(eq, eq->cons_index);
123         return !!(eqe->owner & 0x80) ^ !!(eq->cons_index & eq->nent) ? NULL : eqe;
124 }
125
126 static int mlx4_eq_int(struct mlx4_dev *dev, struct mlx4_eq *eq)
127 {
128         struct mlx4_eqe *eqe;
129         int cqn;
130         int eqes_found = 0;
131         int set_ci = 0;
132         int port;
133
134         while ((eqe = next_eqe_sw(eq))) {
135                 /*
136                  * Make sure we read EQ entry contents after we've
137                  * checked the ownership bit.
138                  */
139                 rmb();
140
141                 switch (eqe->type) {
142                 case MLX4_EVENT_TYPE_COMP:
143                         cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
144                         mlx4_cq_completion(dev, cqn);
145                         break;
146
147                 case MLX4_EVENT_TYPE_PATH_MIG:
148                 case MLX4_EVENT_TYPE_COMM_EST:
149                 case MLX4_EVENT_TYPE_SQ_DRAINED:
150                 case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
151                 case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
152                 case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
153                 case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
154                 case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
155                         mlx4_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
156                                       eqe->type);
157                         break;
158
159                 case MLX4_EVENT_TYPE_SRQ_LIMIT:
160                 case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
161                         mlx4_srq_event(dev, be32_to_cpu(eqe->event.srq.srqn) & 0xffffff,
162                                       eqe->type);
163                         break;
164
165                 case MLX4_EVENT_TYPE_CMD:
166                         mlx4_cmd_event(dev,
167                                        be16_to_cpu(eqe->event.cmd.token),
168                                        eqe->event.cmd.status,
169                                        be64_to_cpu(eqe->event.cmd.out_param));
170                         break;
171
172                 case MLX4_EVENT_TYPE_PORT_CHANGE:
173                         port = be32_to_cpu(eqe->event.port_change.port) >> 28;
174                         if (eqe->subtype == MLX4_PORT_CHANGE_SUBTYPE_DOWN) {
175                                 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_PORT_DOWN,
176                                                     port);
177                                 mlx4_priv(dev)->sense.do_sense_port[port] = 1;
178                         } else {
179                                 mlx4_dispatch_event(dev, MLX4_DEV_EVENT_PORT_UP,
180                                                     port);
181                                 mlx4_priv(dev)->sense.do_sense_port[port] = 0;
182                         }
183                         break;
184
185                 case MLX4_EVENT_TYPE_CQ_ERROR:
186                         mlx4_warn(dev, "CQ %s on CQN %06x\n",
187                                   eqe->event.cq_err.syndrome == 1 ?
188                                   "overrun" : "access violation",
189                                   be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
190                         mlx4_cq_event(dev, be32_to_cpu(eqe->event.cq_err.cqn),
191                                       eqe->type);
192                         break;
193
194                 case MLX4_EVENT_TYPE_EQ_OVERFLOW:
195                         mlx4_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
196                         break;
197
198                 case MLX4_EVENT_TYPE_EEC_CATAS_ERROR:
199                 case MLX4_EVENT_TYPE_ECC_DETECT:
200                 default:
201                         mlx4_warn(dev, "Unhandled event %02x(%02x) on EQ %d at index %u\n",
202                                   eqe->type, eqe->subtype, eq->eqn, eq->cons_index);
203                         break;
204                 }
205
206                 ++eq->cons_index;
207                 eqes_found = 1;
208                 ++set_ci;
209
210                 /*
211                  * The HCA will think the queue has overflowed if we
212                  * don't tell it we've been processing events.  We
213                  * create our EQs with MLX4_NUM_SPARE_EQE extra
214                  * entries, so we must update our consumer index at
215                  * least that often.
216                  */
217                 if (unlikely(set_ci >= MLX4_NUM_SPARE_EQE)) {
218                         eq_set_ci(eq, 0);
219                         set_ci = 0;
220                 }
221         }
222
223         eq_set_ci(eq, 1);
224
225         return eqes_found;
226 }
227
228 static irqreturn_t mlx4_interrupt(int irq, void *dev_ptr)
229 {
230         struct mlx4_dev *dev = dev_ptr;
231         struct mlx4_priv *priv = mlx4_priv(dev);
232         int work = 0;
233         int i;
234
235         writel(priv->eq_table.clr_mask, priv->eq_table.clr_int);
236
237         for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
238                 work |= mlx4_eq_int(dev, &priv->eq_table.eq[i]);
239
240         return IRQ_RETVAL(work);
241 }
242
243 static irqreturn_t mlx4_msi_x_interrupt(int irq, void *eq_ptr)
244 {
245         struct mlx4_eq  *eq  = eq_ptr;
246         struct mlx4_dev *dev = eq->dev;
247
248         mlx4_eq_int(dev, eq);
249
250         /* MSI-X vectors always belong to us */
251         return IRQ_HANDLED;
252 }
253
254 static int mlx4_MAP_EQ(struct mlx4_dev *dev, u64 event_mask, int unmap,
255                         int eq_num)
256 {
257         return mlx4_cmd(dev, event_mask, (unmap << 31) | eq_num,
258                         0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B);
259 }
260
261 static int mlx4_SW2HW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
262                          int eq_num)
263 {
264         return mlx4_cmd(dev, mailbox->dma, eq_num, 0, MLX4_CMD_SW2HW_EQ,
265                         MLX4_CMD_TIME_CLASS_A);
266 }
267
268 static int mlx4_HW2SW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
269                          int eq_num)
270 {
271         return mlx4_cmd_box(dev, 0, mailbox->dma, eq_num, 0, MLX4_CMD_HW2SW_EQ,
272                             MLX4_CMD_TIME_CLASS_A);
273 }
274
275 static int mlx4_num_eq_uar(struct mlx4_dev *dev)
276 {
277         /*
278          * Each UAR holds 4 EQ doorbells.  To figure out how many UARs
279          * we need to map, take the difference of highest index and
280          * the lowest index we'll use and add 1.
281          */
282         return (dev->caps.num_comp_vectors + 1 + dev->caps.reserved_eqs +
283                  dev->caps.comp_pool)/4 - dev->caps.reserved_eqs/4 + 1;
284 }
285
286 static void __iomem *mlx4_get_eq_uar(struct mlx4_dev *dev, struct mlx4_eq *eq)
287 {
288         struct mlx4_priv *priv = mlx4_priv(dev);
289         int index;
290
291         index = eq->eqn / 4 - dev->caps.reserved_eqs / 4;
292
293         if (!priv->eq_table.uar_map[index]) {
294                 priv->eq_table.uar_map[index] =
295                         ioremap(pci_resource_start(dev->pdev, 2) +
296                                 ((eq->eqn / 4) << PAGE_SHIFT),
297                                 PAGE_SIZE);
298                 if (!priv->eq_table.uar_map[index]) {
299                         mlx4_err(dev, "Couldn't map EQ doorbell for EQN 0x%06x\n",
300                                  eq->eqn);
301                         return NULL;
302                 }
303         }
304
305         return priv->eq_table.uar_map[index] + 0x800 + 8 * (eq->eqn % 4);
306 }
307
308 static int mlx4_create_eq(struct mlx4_dev *dev, int nent,
309                           u8 intr, struct mlx4_eq *eq)
310 {
311         struct mlx4_priv *priv = mlx4_priv(dev);
312         struct mlx4_cmd_mailbox *mailbox;
313         struct mlx4_eq_context *eq_context;
314         int npages;
315         u64 *dma_list = NULL;
316         dma_addr_t t;
317         u64 mtt_addr;
318         int err = -ENOMEM;
319         int i;
320
321         eq->dev   = dev;
322         eq->nent  = roundup_pow_of_two(max(nent, 2));
323         npages = PAGE_ALIGN(eq->nent * MLX4_EQ_ENTRY_SIZE) / PAGE_SIZE;
324
325         eq->page_list = kmalloc(npages * sizeof *eq->page_list,
326                                 GFP_KERNEL);
327         if (!eq->page_list)
328                 goto err_out;
329
330         for (i = 0; i < npages; ++i)
331                 eq->page_list[i].buf = NULL;
332
333         dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
334         if (!dma_list)
335                 goto err_out_free;
336
337         mailbox = mlx4_alloc_cmd_mailbox(dev);
338         if (IS_ERR(mailbox))
339                 goto err_out_free;
340         eq_context = mailbox->buf;
341
342         for (i = 0; i < npages; ++i) {
343                 eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
344                                                           PAGE_SIZE, &t, GFP_KERNEL);
345                 if (!eq->page_list[i].buf)
346                         goto err_out_free_pages;
347
348                 dma_list[i] = t;
349                 eq->page_list[i].map = t;
350
351                 memset(eq->page_list[i].buf, 0, PAGE_SIZE);
352         }
353
354         eq->eqn = mlx4_bitmap_alloc(&priv->eq_table.bitmap);
355         if (eq->eqn == -1)
356                 goto err_out_free_pages;
357
358         eq->doorbell = mlx4_get_eq_uar(dev, eq);
359         if (!eq->doorbell) {
360                 err = -ENOMEM;
361                 goto err_out_free_eq;
362         }
363
364         err = mlx4_mtt_init(dev, npages, PAGE_SHIFT, &eq->mtt);
365         if (err)
366                 goto err_out_free_eq;
367
368         err = mlx4_write_mtt(dev, &eq->mtt, 0, npages, dma_list);
369         if (err)
370                 goto err_out_free_mtt;
371
372         memset(eq_context, 0, sizeof *eq_context);
373         eq_context->flags         = cpu_to_be32(MLX4_EQ_STATUS_OK   |
374                                                 MLX4_EQ_STATE_ARMED);
375         eq_context->log_eq_size   = ilog2(eq->nent);
376         eq_context->intr          = intr;
377         eq_context->log_page_size = PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT;
378
379         mtt_addr = mlx4_mtt_addr(dev, &eq->mtt);
380         eq_context->mtt_base_addr_h = mtt_addr >> 32;
381         eq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
382
383         err = mlx4_SW2HW_EQ(dev, mailbox, eq->eqn);
384         if (err) {
385                 mlx4_warn(dev, "SW2HW_EQ failed (%d)\n", err);
386                 goto err_out_free_mtt;
387         }
388
389         kfree(dma_list);
390         mlx4_free_cmd_mailbox(dev, mailbox);
391
392         eq->cons_index = 0;
393
394         return err;
395
396 err_out_free_mtt:
397         mlx4_mtt_cleanup(dev, &eq->mtt);
398
399 err_out_free_eq:
400         mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
401
402 err_out_free_pages:
403         for (i = 0; i < npages; ++i)
404                 if (eq->page_list[i].buf)
405                         dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
406                                           eq->page_list[i].buf,
407                                           eq->page_list[i].map);
408
409         mlx4_free_cmd_mailbox(dev, mailbox);
410
411 err_out_free:
412         kfree(eq->page_list);
413         kfree(dma_list);
414
415 err_out:
416         return err;
417 }
418
419 static void mlx4_free_eq(struct mlx4_dev *dev,
420                          struct mlx4_eq *eq)
421 {
422         struct mlx4_priv *priv = mlx4_priv(dev);
423         struct mlx4_cmd_mailbox *mailbox;
424         int err;
425         int npages = PAGE_ALIGN(MLX4_EQ_ENTRY_SIZE * eq->nent) / PAGE_SIZE;
426         int i;
427
428         mailbox = mlx4_alloc_cmd_mailbox(dev);
429         if (IS_ERR(mailbox))
430                 return;
431
432         err = mlx4_HW2SW_EQ(dev, mailbox, eq->eqn);
433         if (err)
434                 mlx4_warn(dev, "HW2SW_EQ failed (%d)\n", err);
435
436         if (0) {
437                 mlx4_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
438                 for (i = 0; i < sizeof (struct mlx4_eq_context) / 4; ++i) {
439                         if (i % 4 == 0)
440                                 pr_cont("[%02x] ", i * 4);
441                         pr_cont(" %08x", be32_to_cpup(mailbox->buf + i * 4));
442                         if ((i + 1) % 4 == 0)
443                                 pr_cont("\n");
444                 }
445         }
446
447         mlx4_mtt_cleanup(dev, &eq->mtt);
448         for (i = 0; i < npages; ++i)
449                 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
450                                     eq->page_list[i].buf,
451                                     eq->page_list[i].map);
452
453         kfree(eq->page_list);
454         mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
455         mlx4_free_cmd_mailbox(dev, mailbox);
456 }
457
458 static void mlx4_free_irqs(struct mlx4_dev *dev)
459 {
460         struct mlx4_eq_table *eq_table = &mlx4_priv(dev)->eq_table;
461         struct mlx4_priv *priv = mlx4_priv(dev);
462         int     i, vec;
463
464         if (eq_table->have_irq)
465                 free_irq(dev->pdev->irq, dev);
466
467         for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
468                 if (eq_table->eq[i].have_irq) {
469                         free_irq(eq_table->eq[i].irq, eq_table->eq + i);
470                         eq_table->eq[i].have_irq = 0;
471                 }
472
473         for (i = 0; i < dev->caps.comp_pool; i++) {
474                 /*
475                  * Freeing the assigned irq's
476                  * all bits should be 0, but we need to validate
477                  */
478                 if (priv->msix_ctl.pool_bm & 1ULL << i) {
479                         /* NO need protecting*/
480                         vec = dev->caps.num_comp_vectors + 1 + i;
481                         free_irq(priv->eq_table.eq[vec].irq,
482                                  &priv->eq_table.eq[vec]);
483                 }
484         }
485
486
487         kfree(eq_table->irq_names);
488 }
489
490 static int mlx4_map_clr_int(struct mlx4_dev *dev)
491 {
492         struct mlx4_priv *priv = mlx4_priv(dev);
493
494         priv->clr_base = ioremap(pci_resource_start(dev->pdev, priv->fw.clr_int_bar) +
495                                  priv->fw.clr_int_base, MLX4_CLR_INT_SIZE);
496         if (!priv->clr_base) {
497                 mlx4_err(dev, "Couldn't map interrupt clear register, aborting.\n");
498                 return -ENOMEM;
499         }
500
501         return 0;
502 }
503
504 static void mlx4_unmap_clr_int(struct mlx4_dev *dev)
505 {
506         struct mlx4_priv *priv = mlx4_priv(dev);
507
508         iounmap(priv->clr_base);
509 }
510
511 int mlx4_alloc_eq_table(struct mlx4_dev *dev)
512 {
513         struct mlx4_priv *priv = mlx4_priv(dev);
514
515         priv->eq_table.eq = kcalloc(dev->caps.num_eqs - dev->caps.reserved_eqs,
516                                     sizeof *priv->eq_table.eq, GFP_KERNEL);
517         if (!priv->eq_table.eq)
518                 return -ENOMEM;
519
520         return 0;
521 }
522
523 void mlx4_free_eq_table(struct mlx4_dev *dev)
524 {
525         kfree(mlx4_priv(dev)->eq_table.eq);
526 }
527
528 int mlx4_init_eq_table(struct mlx4_dev *dev)
529 {
530         struct mlx4_priv *priv = mlx4_priv(dev);
531         int err;
532         int i;
533
534         priv->eq_table.uar_map = kcalloc(sizeof *priv->eq_table.uar_map,
535                                          mlx4_num_eq_uar(dev), GFP_KERNEL);
536         if (!priv->eq_table.uar_map) {
537                 err = -ENOMEM;
538                 goto err_out_free;
539         }
540
541         err = mlx4_bitmap_init(&priv->eq_table.bitmap, dev->caps.num_eqs,
542                                dev->caps.num_eqs - 1, dev->caps.reserved_eqs, 0);
543         if (err)
544                 goto err_out_free;
545
546         for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
547                 priv->eq_table.uar_map[i] = NULL;
548
549         err = mlx4_map_clr_int(dev);
550         if (err)
551                 goto err_out_bitmap;
552
553         priv->eq_table.clr_mask =
554                 swab32(1 << (priv->eq_table.inta_pin & 31));
555         priv->eq_table.clr_int  = priv->clr_base +
556                 (priv->eq_table.inta_pin < 32 ? 4 : 0);
557
558         priv->eq_table.irq_names =
559                 kmalloc(MLX4_IRQNAME_SIZE * (dev->caps.num_comp_vectors + 1 +
560                                              dev->caps.comp_pool),
561                         GFP_KERNEL);
562         if (!priv->eq_table.irq_names) {
563                 err = -ENOMEM;
564                 goto err_out_bitmap;
565         }
566
567         for (i = 0; i < dev->caps.num_comp_vectors; ++i) {
568                 err = mlx4_create_eq(dev, dev->caps.num_cqs -
569                                           dev->caps.reserved_cqs +
570                                           MLX4_NUM_SPARE_EQE,
571                                      (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
572                                      &priv->eq_table.eq[i]);
573                 if (err) {
574                         --i;
575                         goto err_out_unmap;
576                 }
577         }
578
579         err = mlx4_create_eq(dev, MLX4_NUM_ASYNC_EQE + MLX4_NUM_SPARE_EQE,
580                              (dev->flags & MLX4_FLAG_MSI_X) ? dev->caps.num_comp_vectors : 0,
581                              &priv->eq_table.eq[dev->caps.num_comp_vectors]);
582         if (err)
583                 goto err_out_comp;
584
585         /*if additional completion vectors poolsize is 0 this loop will not run*/
586         for (i = dev->caps.num_comp_vectors + 1;
587               i < dev->caps.num_comp_vectors + dev->caps.comp_pool + 1; ++i) {
588
589                 err = mlx4_create_eq(dev, dev->caps.num_cqs -
590                                           dev->caps.reserved_cqs +
591                                           MLX4_NUM_SPARE_EQE,
592                                      (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
593                                      &priv->eq_table.eq[i]);
594                 if (err) {
595                         --i;
596                         goto err_out_unmap;
597                 }
598         }
599
600
601         if (dev->flags & MLX4_FLAG_MSI_X) {
602                 const char *eq_name;
603
604                 for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i) {
605                         if (i < dev->caps.num_comp_vectors) {
606                                 snprintf(priv->eq_table.irq_names +
607                                          i * MLX4_IRQNAME_SIZE,
608                                          MLX4_IRQNAME_SIZE,
609                                          "mlx4-comp-%d@pci:%s", i,
610                                          pci_name(dev->pdev));
611                         } else {
612                                 snprintf(priv->eq_table.irq_names +
613                                          i * MLX4_IRQNAME_SIZE,
614                                          MLX4_IRQNAME_SIZE,
615                                          "mlx4-async@pci:%s",
616                                          pci_name(dev->pdev));
617                         }
618
619                         eq_name = priv->eq_table.irq_names +
620                                   i * MLX4_IRQNAME_SIZE;
621                         err = request_irq(priv->eq_table.eq[i].irq,
622                                           mlx4_msi_x_interrupt, 0, eq_name,
623                                           priv->eq_table.eq + i);
624                         if (err)
625                                 goto err_out_async;
626
627                         priv->eq_table.eq[i].have_irq = 1;
628                 }
629         } else {
630                 snprintf(priv->eq_table.irq_names,
631                          MLX4_IRQNAME_SIZE,
632                          DRV_NAME "@pci:%s",
633                          pci_name(dev->pdev));
634                 err = request_irq(dev->pdev->irq, mlx4_interrupt,
635                                   IRQF_SHARED, priv->eq_table.irq_names, dev);
636                 if (err)
637                         goto err_out_async;
638
639                 priv->eq_table.have_irq = 1;
640         }
641
642         err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
643                           priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
644         if (err)
645                 mlx4_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
646                            priv->eq_table.eq[dev->caps.num_comp_vectors].eqn, err);
647
648         for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
649                 eq_set_ci(&priv->eq_table.eq[i], 1);
650
651         return 0;
652
653 err_out_async:
654         mlx4_free_eq(dev, &priv->eq_table.eq[dev->caps.num_comp_vectors]);
655
656 err_out_comp:
657         i = dev->caps.num_comp_vectors - 1;
658
659 err_out_unmap:
660         while (i >= 0) {
661                 mlx4_free_eq(dev, &priv->eq_table.eq[i]);
662                 --i;
663         }
664         mlx4_unmap_clr_int(dev);
665         mlx4_free_irqs(dev);
666
667 err_out_bitmap:
668         mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
669
670 err_out_free:
671         kfree(priv->eq_table.uar_map);
672
673         return err;
674 }
675
676 void mlx4_cleanup_eq_table(struct mlx4_dev *dev)
677 {
678         struct mlx4_priv *priv = mlx4_priv(dev);
679         int i;
680
681         mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 1,
682                     priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
683
684         mlx4_free_irqs(dev);
685
686         for (i = 0; i < dev->caps.num_comp_vectors + dev->caps.comp_pool + 1; ++i)
687                 mlx4_free_eq(dev, &priv->eq_table.eq[i]);
688
689         mlx4_unmap_clr_int(dev);
690
691         for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
692                 if (priv->eq_table.uar_map[i])
693                         iounmap(priv->eq_table.uar_map[i]);
694
695         mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
696
697         kfree(priv->eq_table.uar_map);
698 }
699
700 /* A test that verifies that we can accept interrupts on all
701  * the irq vectors of the device.
702  * Interrupts are checked using the NOP command.
703  */
704 int mlx4_test_interrupts(struct mlx4_dev *dev)
705 {
706         struct mlx4_priv *priv = mlx4_priv(dev);
707         int i;
708         int err;
709
710         err = mlx4_NOP(dev);
711         /* When not in MSI_X, there is only one irq to check */
712         if (!(dev->flags & MLX4_FLAG_MSI_X))
713                 return err;
714
715         /* A loop over all completion vectors, for each vector we will check
716          * whether it works by mapping command completions to that vector
717          * and performing a NOP command
718          */
719         for(i = 0; !err && (i < dev->caps.num_comp_vectors); ++i) {
720                 /* Temporary use polling for command completions */
721                 mlx4_cmd_use_polling(dev);
722
723                 /* Map the new eq to handle all asyncronous events */
724                 err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
725                                   priv->eq_table.eq[i].eqn);
726                 if (err) {
727                         mlx4_warn(dev, "Failed mapping eq for interrupt test\n");
728                         mlx4_cmd_use_events(dev);
729                         break;
730                 }
731
732                 /* Go back to using events */
733                 mlx4_cmd_use_events(dev);
734                 err = mlx4_NOP(dev);
735         }
736
737         /* Return to default */
738         mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
739                     priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
740         return err;
741 }
742 EXPORT_SYMBOL(mlx4_test_interrupts);
743
744 int mlx4_assign_eq(struct mlx4_dev *dev, char* name, int * vector)
745 {
746
747         struct mlx4_priv *priv = mlx4_priv(dev);
748         int vec = 0, err = 0, i;
749
750         spin_lock(&priv->msix_ctl.pool_lock);
751         for (i = 0; !vec && i < dev->caps.comp_pool; i++) {
752                 if (~priv->msix_ctl.pool_bm & 1ULL << i) {
753                         priv->msix_ctl.pool_bm |= 1ULL << i;
754                         vec = dev->caps.num_comp_vectors + 1 + i;
755                         snprintf(priv->eq_table.irq_names +
756                                         vec * MLX4_IRQNAME_SIZE,
757                                         MLX4_IRQNAME_SIZE, "%s", name);
758                         err = request_irq(priv->eq_table.eq[vec].irq,
759                                           mlx4_msi_x_interrupt, 0,
760                                           &priv->eq_table.irq_names[vec<<5],
761                                           priv->eq_table.eq + vec);
762                         if (err) {
763                                 /*zero out bit by fliping it*/
764                                 priv->msix_ctl.pool_bm ^= 1 << i;
765                                 vec = 0;
766                                 continue;
767                                 /*we dont want to break here*/
768                         }
769                         eq_set_ci(&priv->eq_table.eq[vec], 1);
770                 }
771         }
772         spin_unlock(&priv->msix_ctl.pool_lock);
773
774         if (vec) {
775                 *vector = vec;
776         } else {
777                 *vector = 0;
778                 err = (i == dev->caps.comp_pool) ? -ENOSPC : err;
779         }
780         return err;
781 }
782 EXPORT_SYMBOL(mlx4_assign_eq);
783
784 void mlx4_release_eq(struct mlx4_dev *dev, int vec)
785 {
786         struct mlx4_priv *priv = mlx4_priv(dev);
787         /*bm index*/
788         int i = vec - dev->caps.num_comp_vectors - 1;
789
790         if (likely(i >= 0)) {
791                 /*sanity check , making sure were not trying to free irq's
792                   Belonging to a legacy EQ*/
793                 spin_lock(&priv->msix_ctl.pool_lock);
794                 if (priv->msix_ctl.pool_bm & 1ULL << i) {
795                         free_irq(priv->eq_table.eq[vec].irq,
796                                  &priv->eq_table.eq[vec]);
797                         priv->msix_ctl.pool_bm &= ~(1ULL << i);
798                 }
799                 spin_unlock(&priv->msix_ctl.pool_lock);
800         }
801
802 }
803 EXPORT_SYMBOL(mlx4_release_eq);
804