Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[linux-2.6] / drivers / infiniband / hw / mlx4 / main.c
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc. 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
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/errno.h>
36
37 #include <rdma/ib_smi.h>
38 #include <rdma/ib_user_verbs.h>
39
40 #include <linux/mlx4/driver.h>
41 #include <linux/mlx4/cmd.h>
42
43 #include "mlx4_ib.h"
44 #include "user.h"
45
46 #define DRV_NAME        "mlx4_ib"
47 #define DRV_VERSION     "1.0"
48 #define DRV_RELDATE     "April 4, 2008"
49
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
52 MODULE_LICENSE("Dual BSD/GPL");
53 MODULE_VERSION(DRV_VERSION);
54
55 static const char mlx4_ib_version[] =
56         DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
57         DRV_VERSION " (" DRV_RELDATE ")\n";
58
59 static void init_query_mad(struct ib_smp *mad)
60 {
61         mad->base_version  = 1;
62         mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
63         mad->class_version = 1;
64         mad->method        = IB_MGMT_METHOD_GET;
65 }
66
67 static int mlx4_ib_query_device(struct ib_device *ibdev,
68                                 struct ib_device_attr *props)
69 {
70         struct mlx4_ib_dev *dev = to_mdev(ibdev);
71         struct ib_smp *in_mad  = NULL;
72         struct ib_smp *out_mad = NULL;
73         int err = -ENOMEM;
74
75         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
76         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
77         if (!in_mad || !out_mad)
78                 goto out;
79
80         init_query_mad(in_mad);
81         in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
82
83         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
84         if (err)
85                 goto out;
86
87         memset(props, 0, sizeof *props);
88
89         props->fw_ver = dev->dev->caps.fw_ver;
90         props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
91                 IB_DEVICE_PORT_ACTIVE_EVENT             |
92                 IB_DEVICE_SYS_IMAGE_GUID                |
93                 IB_DEVICE_RC_RNR_NAK_GEN;
94         if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
95                 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
96         if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
97                 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
98         if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
99                 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
100         if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
101                 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
102         if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
103                 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
104         if (dev->dev->caps.max_gso_sz)
105                 props->device_cap_flags |= IB_DEVICE_UD_TSO;
106
107         props->vendor_id           = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
108                 0xffffff;
109         props->vendor_part_id      = be16_to_cpup((__be16 *) (out_mad->data + 30));
110         props->hw_ver              = be32_to_cpup((__be32 *) (out_mad->data + 32));
111         memcpy(&props->sys_image_guid, out_mad->data +  4, 8);
112
113         props->max_mr_size         = ~0ull;
114         props->page_size_cap       = dev->dev->caps.page_size_cap;
115         props->max_qp              = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
116         props->max_qp_wr           = dev->dev->caps.max_wqes;
117         props->max_sge             = min(dev->dev->caps.max_sq_sg,
118                                          dev->dev->caps.max_rq_sg);
119         props->max_cq              = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
120         props->max_cqe             = dev->dev->caps.max_cqes;
121         props->max_mr              = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
122         props->max_pd              = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
123         props->max_qp_rd_atom      = dev->dev->caps.max_qp_dest_rdma;
124         props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
125         props->max_res_rd_atom     = props->max_qp_rd_atom * props->max_qp;
126         props->max_srq             = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
127         props->max_srq_wr          = dev->dev->caps.max_srq_wqes - 1;
128         props->max_srq_sge         = dev->dev->caps.max_srq_sge;
129         props->local_ca_ack_delay  = dev->dev->caps.local_ca_ack_delay;
130         props->atomic_cap          = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
131                 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
132         props->max_pkeys           = dev->dev->caps.pkey_table_len[1];
133         props->max_mcast_grp       = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
134         props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
135         props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
136                                            props->max_mcast_grp;
137         props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;
138
139 out:
140         kfree(in_mad);
141         kfree(out_mad);
142
143         return err;
144 }
145
146 static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
147                               struct ib_port_attr *props)
148 {
149         struct ib_smp *in_mad  = NULL;
150         struct ib_smp *out_mad = NULL;
151         int err = -ENOMEM;
152
153         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
154         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
155         if (!in_mad || !out_mad)
156                 goto out;
157
158         memset(props, 0, sizeof *props);
159
160         init_query_mad(in_mad);
161         in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
162         in_mad->attr_mod = cpu_to_be32(port);
163
164         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
165         if (err)
166                 goto out;
167
168         props->lid              = be16_to_cpup((__be16 *) (out_mad->data + 16));
169         props->lmc              = out_mad->data[34] & 0x7;
170         props->sm_lid           = be16_to_cpup((__be16 *) (out_mad->data + 18));
171         props->sm_sl            = out_mad->data[36] & 0xf;
172         props->state            = out_mad->data[32] & 0xf;
173         props->phys_state       = out_mad->data[33] >> 4;
174         props->port_cap_flags   = be32_to_cpup((__be32 *) (out_mad->data + 20));
175         props->gid_tbl_len      = to_mdev(ibdev)->dev->caps.gid_table_len[port];
176         props->max_msg_sz       = to_mdev(ibdev)->dev->caps.max_msg_sz;
177         props->pkey_tbl_len     = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
178         props->bad_pkey_cntr    = be16_to_cpup((__be16 *) (out_mad->data + 46));
179         props->qkey_viol_cntr   = be16_to_cpup((__be16 *) (out_mad->data + 48));
180         props->active_width     = out_mad->data[31] & 0xf;
181         props->active_speed     = out_mad->data[35] >> 4;
182         props->max_mtu          = out_mad->data[41] & 0xf;
183         props->active_mtu       = out_mad->data[36] >> 4;
184         props->subnet_timeout   = out_mad->data[51] & 0x1f;
185         props->max_vl_num       = out_mad->data[37] >> 4;
186         props->init_type_reply  = out_mad->data[41] >> 4;
187
188 out:
189         kfree(in_mad);
190         kfree(out_mad);
191
192         return err;
193 }
194
195 static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
196                              union ib_gid *gid)
197 {
198         struct ib_smp *in_mad  = NULL;
199         struct ib_smp *out_mad = NULL;
200         int err = -ENOMEM;
201
202         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
203         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
204         if (!in_mad || !out_mad)
205                 goto out;
206
207         init_query_mad(in_mad);
208         in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
209         in_mad->attr_mod = cpu_to_be32(port);
210
211         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
212         if (err)
213                 goto out;
214
215         memcpy(gid->raw, out_mad->data + 8, 8);
216
217         init_query_mad(in_mad);
218         in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
219         in_mad->attr_mod = cpu_to_be32(index / 8);
220
221         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
222         if (err)
223                 goto out;
224
225         memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
226
227 out:
228         kfree(in_mad);
229         kfree(out_mad);
230         return err;
231 }
232
233 static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
234                               u16 *pkey)
235 {
236         struct ib_smp *in_mad  = NULL;
237         struct ib_smp *out_mad = NULL;
238         int err = -ENOMEM;
239
240         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
241         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
242         if (!in_mad || !out_mad)
243                 goto out;
244
245         init_query_mad(in_mad);
246         in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
247         in_mad->attr_mod = cpu_to_be32(index / 32);
248
249         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
250         if (err)
251                 goto out;
252
253         *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
254
255 out:
256         kfree(in_mad);
257         kfree(out_mad);
258         return err;
259 }
260
261 static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
262                                  struct ib_device_modify *props)
263 {
264         if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
265                 return -EOPNOTSUPP;
266
267         if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
268                 spin_lock(&to_mdev(ibdev)->sm_lock);
269                 memcpy(ibdev->node_desc, props->node_desc, 64);
270                 spin_unlock(&to_mdev(ibdev)->sm_lock);
271         }
272
273         return 0;
274 }
275
276 static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
277                          u32 cap_mask)
278 {
279         struct mlx4_cmd_mailbox *mailbox;
280         int err;
281
282         mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
283         if (IS_ERR(mailbox))
284                 return PTR_ERR(mailbox);
285
286         memset(mailbox->buf, 0, 256);
287
288         if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
289                 *(u8 *) mailbox->buf         = !!reset_qkey_viols << 6;
290                 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
291         } else {
292                 ((u8 *) mailbox->buf)[3]     = !!reset_qkey_viols;
293                 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
294         }
295
296         err = mlx4_cmd(dev->dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
297                        MLX4_CMD_TIME_CLASS_B);
298
299         mlx4_free_cmd_mailbox(dev->dev, mailbox);
300         return err;
301 }
302
303 static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
304                                struct ib_port_modify *props)
305 {
306         struct ib_port_attr attr;
307         u32 cap_mask;
308         int err;
309
310         mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
311
312         err = mlx4_ib_query_port(ibdev, port, &attr);
313         if (err)
314                 goto out;
315
316         cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
317                 ~props->clr_port_cap_mask;
318
319         err = mlx4_SET_PORT(to_mdev(ibdev), port,
320                             !!(mask & IB_PORT_RESET_QKEY_CNTR),
321                             cap_mask);
322
323 out:
324         mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
325         return err;
326 }
327
328 static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
329                                                   struct ib_udata *udata)
330 {
331         struct mlx4_ib_dev *dev = to_mdev(ibdev);
332         struct mlx4_ib_ucontext *context;
333         struct mlx4_ib_alloc_ucontext_resp resp;
334         int err;
335
336         resp.qp_tab_size      = dev->dev->caps.num_qps;
337         resp.bf_reg_size      = dev->dev->caps.bf_reg_size;
338         resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
339
340         context = kmalloc(sizeof *context, GFP_KERNEL);
341         if (!context)
342                 return ERR_PTR(-ENOMEM);
343
344         err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
345         if (err) {
346                 kfree(context);
347                 return ERR_PTR(err);
348         }
349
350         INIT_LIST_HEAD(&context->db_page_list);
351         mutex_init(&context->db_page_mutex);
352
353         err = ib_copy_to_udata(udata, &resp, sizeof resp);
354         if (err) {
355                 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
356                 kfree(context);
357                 return ERR_PTR(-EFAULT);
358         }
359
360         return &context->ibucontext;
361 }
362
363 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
364 {
365         struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
366
367         mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
368         kfree(context);
369
370         return 0;
371 }
372
373 static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
374 {
375         struct mlx4_ib_dev *dev = to_mdev(context->device);
376
377         if (vma->vm_end - vma->vm_start != PAGE_SIZE)
378                 return -EINVAL;
379
380         if (vma->vm_pgoff == 0) {
381                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
382
383                 if (io_remap_pfn_range(vma, vma->vm_start,
384                                        to_mucontext(context)->uar.pfn,
385                                        PAGE_SIZE, vma->vm_page_prot))
386                         return -EAGAIN;
387         } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
388                 /* FIXME want pgprot_writecombine() for BlueFlame pages */
389                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
390
391                 if (io_remap_pfn_range(vma, vma->vm_start,
392                                        to_mucontext(context)->uar.pfn +
393                                        dev->dev->caps.num_uars,
394                                        PAGE_SIZE, vma->vm_page_prot))
395                         return -EAGAIN;
396         } else
397                 return -EINVAL;
398
399         return 0;
400 }
401
402 static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
403                                       struct ib_ucontext *context,
404                                       struct ib_udata *udata)
405 {
406         struct mlx4_ib_pd *pd;
407         int err;
408
409         pd = kmalloc(sizeof *pd, GFP_KERNEL);
410         if (!pd)
411                 return ERR_PTR(-ENOMEM);
412
413         err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
414         if (err) {
415                 kfree(pd);
416                 return ERR_PTR(err);
417         }
418
419         if (context)
420                 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
421                         mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
422                         kfree(pd);
423                         return ERR_PTR(-EFAULT);
424                 }
425
426         return &pd->ibpd;
427 }
428
429 static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
430 {
431         mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
432         kfree(pd);
433
434         return 0;
435 }
436
437 static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
438 {
439         return mlx4_multicast_attach(to_mdev(ibqp->device)->dev,
440                                      &to_mqp(ibqp)->mqp, gid->raw);
441 }
442
443 static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
444 {
445         return mlx4_multicast_detach(to_mdev(ibqp->device)->dev,
446                                      &to_mqp(ibqp)->mqp, gid->raw);
447 }
448
449 static int init_node_data(struct mlx4_ib_dev *dev)
450 {
451         struct ib_smp *in_mad  = NULL;
452         struct ib_smp *out_mad = NULL;
453         int err = -ENOMEM;
454
455         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
456         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
457         if (!in_mad || !out_mad)
458                 goto out;
459
460         init_query_mad(in_mad);
461         in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
462
463         err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
464         if (err)
465                 goto out;
466
467         memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
468
469         in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
470
471         err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
472         if (err)
473                 goto out;
474
475         dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
476         memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
477
478 out:
479         kfree(in_mad);
480         kfree(out_mad);
481         return err;
482 }
483
484 static ssize_t show_hca(struct class_device *cdev, char *buf)
485 {
486         struct mlx4_ib_dev *dev = container_of(cdev, struct mlx4_ib_dev, ib_dev.class_dev);
487         return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
488 }
489
490 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
491 {
492         struct mlx4_ib_dev *dev = container_of(cdev, struct mlx4_ib_dev, ib_dev.class_dev);
493         return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
494                        (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
495                        (int) dev->dev->caps.fw_ver & 0xffff);
496 }
497
498 static ssize_t show_rev(struct class_device *cdev, char *buf)
499 {
500         struct mlx4_ib_dev *dev = container_of(cdev, struct mlx4_ib_dev, ib_dev.class_dev);
501         return sprintf(buf, "%x\n", dev->dev->rev_id);
502 }
503
504 static ssize_t show_board(struct class_device *cdev, char *buf)
505 {
506         struct mlx4_ib_dev *dev = container_of(cdev, struct mlx4_ib_dev, ib_dev.class_dev);
507         return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN, dev->dev->board_id);
508 }
509
510 static CLASS_DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
511 static CLASS_DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
512 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
513 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
514
515 static struct class_device_attribute *mlx4_class_attributes[] = {
516         &class_device_attr_hw_rev,
517         &class_device_attr_fw_ver,
518         &class_device_attr_hca_type,
519         &class_device_attr_board_id
520 };
521
522 static void *mlx4_ib_add(struct mlx4_dev *dev)
523 {
524         static int mlx4_ib_version_printed;
525         struct mlx4_ib_dev *ibdev;
526         int i;
527
528
529         if (!mlx4_ib_version_printed) {
530                 printk(KERN_INFO "%s", mlx4_ib_version);
531                 ++mlx4_ib_version_printed;
532         }
533
534         ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
535         if (!ibdev) {
536                 dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
537                 return NULL;
538         }
539
540         if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
541                 goto err_dealloc;
542
543         if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
544                 goto err_pd;
545
546         ibdev->uar_map = ioremap(ibdev->priv_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
547         if (!ibdev->uar_map)
548                 goto err_uar;
549         MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
550
551         INIT_LIST_HEAD(&ibdev->pgdir_list);
552         mutex_init(&ibdev->pgdir_mutex);
553
554         ibdev->dev = dev;
555
556         strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
557         ibdev->ib_dev.owner             = THIS_MODULE;
558         ibdev->ib_dev.node_type         = RDMA_NODE_IB_CA;
559         ibdev->ib_dev.phys_port_cnt     = dev->caps.num_ports;
560         ibdev->ib_dev.num_comp_vectors  = 1;
561         ibdev->ib_dev.dma_device        = &dev->pdev->dev;
562
563         ibdev->ib_dev.uverbs_abi_ver    = MLX4_IB_UVERBS_ABI_VERSION;
564         ibdev->ib_dev.uverbs_cmd_mask   =
565                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
566                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
567                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
568                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
569                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
570                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
571                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
572                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
573                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
574                 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
575                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
576                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
577                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
578                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
579                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
580                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
581                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
582                 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
583                 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
584                 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
585                 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
586
587         ibdev->ib_dev.query_device      = mlx4_ib_query_device;
588         ibdev->ib_dev.query_port        = mlx4_ib_query_port;
589         ibdev->ib_dev.query_gid         = mlx4_ib_query_gid;
590         ibdev->ib_dev.query_pkey        = mlx4_ib_query_pkey;
591         ibdev->ib_dev.modify_device     = mlx4_ib_modify_device;
592         ibdev->ib_dev.modify_port       = mlx4_ib_modify_port;
593         ibdev->ib_dev.alloc_ucontext    = mlx4_ib_alloc_ucontext;
594         ibdev->ib_dev.dealloc_ucontext  = mlx4_ib_dealloc_ucontext;
595         ibdev->ib_dev.mmap              = mlx4_ib_mmap;
596         ibdev->ib_dev.alloc_pd          = mlx4_ib_alloc_pd;
597         ibdev->ib_dev.dealloc_pd        = mlx4_ib_dealloc_pd;
598         ibdev->ib_dev.create_ah         = mlx4_ib_create_ah;
599         ibdev->ib_dev.query_ah          = mlx4_ib_query_ah;
600         ibdev->ib_dev.destroy_ah        = mlx4_ib_destroy_ah;
601         ibdev->ib_dev.create_srq        = mlx4_ib_create_srq;
602         ibdev->ib_dev.modify_srq        = mlx4_ib_modify_srq;
603         ibdev->ib_dev.query_srq         = mlx4_ib_query_srq;
604         ibdev->ib_dev.destroy_srq       = mlx4_ib_destroy_srq;
605         ibdev->ib_dev.post_srq_recv     = mlx4_ib_post_srq_recv;
606         ibdev->ib_dev.create_qp         = mlx4_ib_create_qp;
607         ibdev->ib_dev.modify_qp         = mlx4_ib_modify_qp;
608         ibdev->ib_dev.query_qp          = mlx4_ib_query_qp;
609         ibdev->ib_dev.destroy_qp        = mlx4_ib_destroy_qp;
610         ibdev->ib_dev.post_send         = mlx4_ib_post_send;
611         ibdev->ib_dev.post_recv         = mlx4_ib_post_recv;
612         ibdev->ib_dev.create_cq         = mlx4_ib_create_cq;
613         ibdev->ib_dev.modify_cq         = mlx4_ib_modify_cq;
614         ibdev->ib_dev.resize_cq         = mlx4_ib_resize_cq;
615         ibdev->ib_dev.destroy_cq        = mlx4_ib_destroy_cq;
616         ibdev->ib_dev.poll_cq           = mlx4_ib_poll_cq;
617         ibdev->ib_dev.req_notify_cq     = mlx4_ib_arm_cq;
618         ibdev->ib_dev.get_dma_mr        = mlx4_ib_get_dma_mr;
619         ibdev->ib_dev.reg_user_mr       = mlx4_ib_reg_user_mr;
620         ibdev->ib_dev.dereg_mr          = mlx4_ib_dereg_mr;
621         ibdev->ib_dev.attach_mcast      = mlx4_ib_mcg_attach;
622         ibdev->ib_dev.detach_mcast      = mlx4_ib_mcg_detach;
623         ibdev->ib_dev.process_mad       = mlx4_ib_process_mad;
624
625         ibdev->ib_dev.alloc_fmr         = mlx4_ib_fmr_alloc;
626         ibdev->ib_dev.map_phys_fmr      = mlx4_ib_map_phys_fmr;
627         ibdev->ib_dev.unmap_fmr         = mlx4_ib_unmap_fmr;
628         ibdev->ib_dev.dealloc_fmr       = mlx4_ib_fmr_dealloc;
629
630         if (init_node_data(ibdev))
631                 goto err_map;
632
633         spin_lock_init(&ibdev->sm_lock);
634         mutex_init(&ibdev->cap_mask_mutex);
635
636         if (ib_register_device(&ibdev->ib_dev))
637                 goto err_map;
638
639         if (mlx4_ib_mad_init(ibdev))
640                 goto err_reg;
641
642         for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
643                 if (class_device_create_file(&ibdev->ib_dev.class_dev,
644                                                mlx4_class_attributes[i]))
645                         goto err_reg;
646         }
647
648         return ibdev;
649
650 err_reg:
651         ib_unregister_device(&ibdev->ib_dev);
652
653 err_map:
654         iounmap(ibdev->uar_map);
655
656 err_uar:
657         mlx4_uar_free(dev, &ibdev->priv_uar);
658
659 err_pd:
660         mlx4_pd_free(dev, ibdev->priv_pdn);
661
662 err_dealloc:
663         ib_dealloc_device(&ibdev->ib_dev);
664
665         return NULL;
666 }
667
668 static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
669 {
670         struct mlx4_ib_dev *ibdev = ibdev_ptr;
671         int p;
672
673         for (p = 1; p <= dev->caps.num_ports; ++p)
674                 mlx4_CLOSE_PORT(dev, p);
675
676         mlx4_ib_mad_cleanup(ibdev);
677         ib_unregister_device(&ibdev->ib_dev);
678         iounmap(ibdev->uar_map);
679         mlx4_uar_free(dev, &ibdev->priv_uar);
680         mlx4_pd_free(dev, ibdev->priv_pdn);
681         ib_dealloc_device(&ibdev->ib_dev);
682 }
683
684 static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
685                           enum mlx4_dev_event event, int port)
686 {
687         struct ib_event ibev;
688
689         switch (event) {
690         case MLX4_DEV_EVENT_PORT_UP:
691                 ibev.event = IB_EVENT_PORT_ACTIVE;
692                 break;
693
694         case MLX4_DEV_EVENT_PORT_DOWN:
695                 ibev.event = IB_EVENT_PORT_ERR;
696                 break;
697
698         case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
699                 ibev.event = IB_EVENT_DEVICE_FATAL;
700                 break;
701
702         default:
703                 return;
704         }
705
706         ibev.device           = ibdev_ptr;
707         ibev.element.port_num = port;
708
709         ib_dispatch_event(&ibev);
710 }
711
712 static struct mlx4_interface mlx4_ib_interface = {
713         .add    = mlx4_ib_add,
714         .remove = mlx4_ib_remove,
715         .event  = mlx4_ib_event
716 };
717
718 static int __init mlx4_ib_init(void)
719 {
720         return mlx4_register_interface(&mlx4_ib_interface);
721 }
722
723 static void __exit mlx4_ib_cleanup(void)
724 {
725         mlx4_unregister_interface(&mlx4_ib_interface);
726 }
727
728 module_init(mlx4_ib_init);
729 module_exit(mlx4_ib_cleanup);