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