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