This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / infiniband / hw / mthca / mthca_provider.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  * $Id: mthca_provider.c 1397 2004-12-28 05:09:00Z roland $
33  */
34
35 #include <ib_smi.h>
36
37 #include "mthca_dev.h"
38 #include "mthca_cmd.h"
39
40 static int mthca_query_device(struct ib_device *ibdev,
41                               struct ib_device_attr *props)
42 {
43         struct ib_smp *in_mad  = NULL;
44         struct ib_smp *out_mad = NULL;
45         int err = -ENOMEM;
46         u8 status;
47
48         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
49         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
50         if (!in_mad || !out_mad)
51                 goto out;
52
53         props->fw_ver        = to_mdev(ibdev)->fw_ver;
54
55         memset(in_mad, 0, sizeof *in_mad);
56         in_mad->base_version       = 1;
57         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
58         in_mad->class_version      = 1;
59         in_mad->method             = IB_MGMT_METHOD_GET;
60         in_mad->attr_id            = IB_SMP_ATTR_NODE_INFO;
61
62         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
63                             1, NULL, NULL, in_mad, out_mad,
64                             &status);
65         if (err)
66                 goto out;
67         if (status) {
68                 err = -EINVAL;
69                 goto out;
70         }
71
72         props->vendor_id      = be32_to_cpup((u32 *) (out_mad->data + 36)) &
73                 0xffffff;
74         props->vendor_part_id = be16_to_cpup((u16 *) (out_mad->data + 30));
75         props->hw_ver         = be16_to_cpup((u16 *) (out_mad->data + 32));
76         memcpy(&props->sys_image_guid, out_mad->data +  4, 8);
77         memcpy(&props->node_guid,      out_mad->data + 12, 8);
78
79         err = 0;
80  out:
81         kfree(in_mad);
82         kfree(out_mad);
83         return err;
84 }
85
86 static int mthca_query_port(struct ib_device *ibdev,
87                             u8 port, struct ib_port_attr *props)
88 {
89         struct ib_smp *in_mad  = NULL;
90         struct ib_smp *out_mad = NULL;
91         int err = -ENOMEM;
92         u8 status;
93
94         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
95         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
96         if (!in_mad || !out_mad)
97                 goto out;
98
99         memset(in_mad, 0, sizeof *in_mad);
100         in_mad->base_version       = 1;
101         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
102         in_mad->class_version      = 1;
103         in_mad->method             = IB_MGMT_METHOD_GET;
104         in_mad->attr_id            = IB_SMP_ATTR_PORT_INFO;
105         in_mad->attr_mod           = cpu_to_be32(port);
106
107         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
108                             port, NULL, NULL, in_mad, out_mad,
109                             &status);
110         if (err)
111                 goto out;
112         if (status) {
113                 err = -EINVAL;
114                 goto out;
115         }
116
117         props->lid               = be16_to_cpup((u16 *) (out_mad->data + 16));
118         props->lmc               = out_mad->data[34] & 0x7;
119         props->sm_lid            = be16_to_cpup((u16 *) (out_mad->data + 18));
120         props->sm_sl             = out_mad->data[36] & 0xf;
121         props->state             = out_mad->data[32] & 0xf;
122         props->phys_state        = out_mad->data[33] >> 4;
123         props->port_cap_flags    = be32_to_cpup((u32 *) (out_mad->data + 20));
124         props->gid_tbl_len       = to_mdev(ibdev)->limits.gid_table_len;
125         props->pkey_tbl_len      = to_mdev(ibdev)->limits.pkey_table_len;
126         props->qkey_viol_cntr    = be16_to_cpup((u16 *) (out_mad->data + 48));
127         props->active_width      = out_mad->data[31] & 0xf;
128         props->active_speed      = out_mad->data[35] >> 4;
129
130  out:
131         kfree(in_mad);
132         kfree(out_mad);
133         return err;
134 }
135
136 static int mthca_modify_port(struct ib_device *ibdev,
137                              u8 port, int port_modify_mask,
138                              struct ib_port_modify *props)
139 {
140         struct mthca_set_ib_param set_ib;
141         struct ib_port_attr attr;
142         int err;
143         u8 status;
144
145         if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
146                 return -ERESTARTSYS;
147
148         err = mthca_query_port(ibdev, port, &attr);
149         if (err)
150                 goto out;
151
152         set_ib.set_si_guid     = 0;
153         set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
154
155         set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
156                 ~props->clr_port_cap_mask;
157
158         err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
159         if (err)
160                 goto out;
161         if (status) {
162                 err = -EINVAL;
163                 goto out;
164         }
165
166 out:
167         up(&to_mdev(ibdev)->cap_mask_mutex);
168         return err;
169 }
170
171 static int mthca_query_pkey(struct ib_device *ibdev,
172                             u8 port, u16 index, u16 *pkey)
173 {
174         struct ib_smp *in_mad  = NULL;
175         struct ib_smp *out_mad = NULL;
176         int err = -ENOMEM;
177         u8 status;
178
179         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
180         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
181         if (!in_mad || !out_mad)
182                 goto out;
183
184         memset(in_mad, 0, sizeof *in_mad);
185         in_mad->base_version       = 1;
186         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
187         in_mad->class_version      = 1;
188         in_mad->method             = IB_MGMT_METHOD_GET;
189         in_mad->attr_id            = IB_SMP_ATTR_PKEY_TABLE;
190         in_mad->attr_mod           = cpu_to_be32(index / 32);
191
192         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
193                             port, NULL, NULL, in_mad, out_mad,
194                             &status);
195         if (err)
196                 goto out;
197         if (status) {
198                 err = -EINVAL;
199                 goto out;
200         }
201
202         *pkey = be16_to_cpu(((u16 *) out_mad->data)[index % 32]);
203
204  out:
205         kfree(in_mad);
206         kfree(out_mad);
207         return err;
208 }
209
210 static int mthca_query_gid(struct ib_device *ibdev, u8 port,
211                            int index, union ib_gid *gid)
212 {
213         struct ib_smp *in_mad  = NULL;
214         struct ib_smp *out_mad = NULL;
215         int err = -ENOMEM;
216         u8 status;
217
218         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
219         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
220         if (!in_mad || !out_mad)
221                 goto out;
222
223         memset(in_mad, 0, sizeof *in_mad);
224         in_mad->base_version       = 1;
225         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
226         in_mad->class_version      = 1;
227         in_mad->method             = IB_MGMT_METHOD_GET;
228         in_mad->attr_id            = IB_SMP_ATTR_PORT_INFO;
229         in_mad->attr_mod           = cpu_to_be32(port);
230
231         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
232                             port, NULL, NULL, in_mad, out_mad,
233                             &status);
234         if (err)
235                 goto out;
236         if (status) {
237                 err = -EINVAL;
238                 goto out;
239         }
240
241         memcpy(gid->raw, out_mad->data + 8, 8);
242
243         memset(in_mad, 0, sizeof *in_mad);
244         in_mad->base_version       = 1;
245         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
246         in_mad->class_version      = 1;
247         in_mad->method             = IB_MGMT_METHOD_GET;
248         in_mad->attr_id            = IB_SMP_ATTR_GUID_INFO;
249         in_mad->attr_mod           = cpu_to_be32(index / 8);
250
251         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
252                             port, NULL, NULL, in_mad, out_mad,
253                             &status);
254         if (err)
255                 goto out;
256         if (status) {
257                 err = -EINVAL;
258                 goto out;
259         }
260
261         memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
262
263  out:
264         kfree(in_mad);
265         kfree(out_mad);
266         return err;
267 }
268
269 static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev)
270 {
271         struct mthca_pd *pd;
272         int err;
273
274         pd = kmalloc(sizeof *pd, GFP_KERNEL);
275         if (!pd)
276                 return ERR_PTR(-ENOMEM);
277
278         err = mthca_pd_alloc(to_mdev(ibdev), pd);
279         if (err) {
280                 kfree(pd);
281                 return ERR_PTR(err);
282         }
283
284         return &pd->ibpd;
285 }
286
287 static int mthca_dealloc_pd(struct ib_pd *pd)
288 {
289         mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
290         kfree(pd);
291
292         return 0;
293 }
294
295 static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
296                                      struct ib_ah_attr *ah_attr)
297 {
298         int err;
299         struct mthca_ah *ah;
300
301         ah = kmalloc(sizeof *ah, GFP_KERNEL);
302         if (!ah)
303                 return ERR_PTR(-ENOMEM);
304
305         err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
306         if (err) {
307                 kfree(ah);
308                 return ERR_PTR(err);
309         }
310
311         return &ah->ibah;
312 }
313
314 static int mthca_ah_destroy(struct ib_ah *ah)
315 {
316         mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
317         kfree(ah);
318
319         return 0;
320 }
321
322 static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
323                                      struct ib_qp_init_attr *init_attr)
324 {
325         struct mthca_qp *qp;
326         int err;
327
328         switch (init_attr->qp_type) {
329         case IB_QPT_RC:
330         case IB_QPT_UC:
331         case IB_QPT_UD:
332         {
333                 qp = kmalloc(sizeof *qp, GFP_KERNEL);
334                 if (!qp)
335                         return ERR_PTR(-ENOMEM);
336
337                 qp->sq.max    = init_attr->cap.max_send_wr;
338                 qp->rq.max    = init_attr->cap.max_recv_wr;
339                 qp->sq.max_gs = init_attr->cap.max_send_sge;
340                 qp->rq.max_gs = init_attr->cap.max_recv_sge;
341
342                 err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
343                                      to_mcq(init_attr->send_cq),
344                                      to_mcq(init_attr->recv_cq),
345                                      init_attr->qp_type, init_attr->sq_sig_type,
346                                      init_attr->rq_sig_type, qp);
347                 qp->ibqp.qp_num = qp->qpn;
348                 break;
349         }
350         case IB_QPT_SMI:
351         case IB_QPT_GSI:
352         {
353                 qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
354                 if (!qp)
355                         return ERR_PTR(-ENOMEM);
356
357                 qp->sq.max    = init_attr->cap.max_send_wr;
358                 qp->rq.max    = init_attr->cap.max_recv_wr;
359                 qp->sq.max_gs = init_attr->cap.max_send_sge;
360                 qp->rq.max_gs = init_attr->cap.max_recv_sge;
361
362                 qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
363
364                 err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
365                                       to_mcq(init_attr->send_cq),
366                                       to_mcq(init_attr->recv_cq),
367                                       init_attr->sq_sig_type, init_attr->rq_sig_type,
368                                       qp->ibqp.qp_num, init_attr->port_num,
369                                       to_msqp(qp));
370                 break;
371         }
372         default:
373                 /* Don't support raw QPs */
374                 return ERR_PTR(-ENOSYS);
375         }
376
377         if (err) {
378                 kfree(qp);
379                 return ERR_PTR(err);
380         }
381
382         init_attr->cap.max_inline_data = 0;
383
384         return &qp->ibqp;
385 }
386
387 static int mthca_destroy_qp(struct ib_qp *qp)
388 {
389         mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
390         kfree(qp);
391         return 0;
392 }
393
394 static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries)
395 {
396         struct mthca_cq *cq;
397         int nent;
398         int err;
399
400         cq = kmalloc(sizeof *cq, GFP_KERNEL);
401         if (!cq)
402                 return ERR_PTR(-ENOMEM);
403
404         for (nent = 1; nent <= entries; nent <<= 1)
405                 ; /* nothing */
406
407         err = mthca_init_cq(to_mdev(ibdev), nent, cq);
408         if (err) {
409                 kfree(cq);
410                 cq = ERR_PTR(err);
411         } else
412                 cq->ibcq.cqe = nent - 1;
413
414         return &cq->ibcq;
415 }
416
417 static int mthca_destroy_cq(struct ib_cq *cq)
418 {
419         mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
420         kfree(cq);
421
422         return 0;
423 }
424
425 static int mthca_req_notify_cq(struct ib_cq *cq, enum ib_cq_notify notify)
426 {
427         mthca_arm_cq(to_mdev(cq->device), to_mcq(cq),
428                      notify == IB_CQ_SOLICITED);
429         return 0;
430 }
431
432 static inline u32 convert_access(int acc)
433 {
434         return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC       : 0) |
435                (acc & IB_ACCESS_REMOTE_WRITE  ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
436                (acc & IB_ACCESS_REMOTE_READ   ? MTHCA_MPT_FLAG_REMOTE_READ  : 0) |
437                (acc & IB_ACCESS_LOCAL_WRITE   ? MTHCA_MPT_FLAG_LOCAL_WRITE  : 0) |
438                MTHCA_MPT_FLAG_LOCAL_READ;
439 }
440
441 static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
442 {
443         struct mthca_mr *mr;
444         int err;
445
446         mr = kmalloc(sizeof *mr, GFP_KERNEL);
447         if (!mr)
448                 return ERR_PTR(-ENOMEM);
449
450         err = mthca_mr_alloc_notrans(to_mdev(pd->device),
451                                      to_mpd(pd)->pd_num,
452                                      convert_access(acc), mr);
453
454         if (err) {
455                 kfree(mr);
456                 return ERR_PTR(err);
457         }
458
459         return &mr->ibmr;
460 }
461
462 static struct ib_mr *mthca_reg_phys_mr(struct ib_pd       *pd,
463                                        struct ib_phys_buf *buffer_list,
464                                        int                 num_phys_buf,
465                                        int                 acc,
466                                        u64                *iova_start)
467 {
468         struct mthca_mr *mr;
469         u64 *page_list;
470         u64 total_size;
471         u64 mask;
472         int shift;
473         int npages;
474         int err;
475         int i, j, n;
476
477         /* First check that we have enough alignment */
478         if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
479                 return ERR_PTR(-EINVAL);
480
481         if (num_phys_buf > 1 &&
482             ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
483                 return ERR_PTR(-EINVAL);
484
485         mask = 0;
486         total_size = 0;
487         for (i = 0; i < num_phys_buf; ++i) {
488                 if (buffer_list[i].addr & ~PAGE_MASK)
489                         return ERR_PTR(-EINVAL);
490                 if (i != 0 && i != num_phys_buf - 1 &&
491                     (buffer_list[i].size & ~PAGE_MASK))
492                         return ERR_PTR(-EINVAL);
493
494                 total_size += buffer_list[i].size;
495                 if (i > 0)
496                         mask |= buffer_list[i].addr;
497         }
498
499         /* Find largest page shift we can use to cover buffers */
500         for (shift = PAGE_SHIFT; shift < 31; ++shift)
501                 if (num_phys_buf > 1) {
502                         if ((1ULL << shift) & mask)
503                                 break;
504                 } else {
505                         if (1ULL << shift >=
506                             buffer_list[0].size +
507                             (buffer_list[0].addr & ((1ULL << shift) - 1)))
508                                 break;
509                 }
510
511         buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
512         buffer_list[0].addr &= ~0ull << shift;
513
514         mr = kmalloc(sizeof *mr, GFP_KERNEL);
515         if (!mr)
516                 return ERR_PTR(-ENOMEM);
517
518         npages = 0;
519         for (i = 0; i < num_phys_buf; ++i)
520                 npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
521
522         if (!npages)
523                 return &mr->ibmr;
524
525         page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
526         if (!page_list) {
527                 kfree(mr);
528                 return ERR_PTR(-ENOMEM);
529         }
530
531         n = 0;
532         for (i = 0; i < num_phys_buf; ++i)
533                 for (j = 0;
534                      j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
535                      ++j)
536                         page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
537
538         mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
539                   "in PD %x; shift %d, npages %d.\n",
540                   (unsigned long long) buffer_list[0].addr,
541                   (unsigned long long) *iova_start,
542                   to_mpd(pd)->pd_num,
543                   shift, npages);
544
545         err = mthca_mr_alloc_phys(to_mdev(pd->device),
546                                   to_mpd(pd)->pd_num,
547                                   page_list, shift, npages,
548                                   *iova_start, total_size,
549                                   convert_access(acc), mr);
550
551         if (err) {
552                 kfree(mr);
553                 return ERR_PTR(err);
554         }
555
556         kfree(page_list);
557         return &mr->ibmr;
558 }
559
560 static int mthca_dereg_mr(struct ib_mr *mr)
561 {
562         mthca_free_mr(to_mdev(mr->device), to_mmr(mr));
563         kfree(mr);
564         return 0;
565 }
566
567 static ssize_t show_rev(struct class_device *cdev, char *buf)
568 {
569         struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
570         return sprintf(buf, "%x\n", dev->rev_id);
571 }
572
573 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
574 {
575         struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
576         return sprintf(buf, "%x.%x.%x\n", (int) (dev->fw_ver >> 32),
577                        (int) (dev->fw_ver >> 16) & 0xffff,
578                        (int) dev->fw_ver & 0xffff);
579 }
580
581 static ssize_t show_hca(struct class_device *cdev, char *buf)
582 {
583         struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
584         switch (dev->hca_type) {
585         case TAVOR:        return sprintf(buf, "MT23108\n");
586         case ARBEL_COMPAT: return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
587         case ARBEL_NATIVE: return sprintf(buf, "MT25208\n");
588         default:           return sprintf(buf, "unknown\n");
589         }
590 }
591
592 static CLASS_DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
593 static CLASS_DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
594 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
595
596 static struct class_device_attribute *mthca_class_attributes[] = {
597         &class_device_attr_hw_rev,
598         &class_device_attr_fw_ver,
599         &class_device_attr_hca_type
600 };
601
602 int mthca_register_device(struct mthca_dev *dev)
603 {
604         int ret;
605         int i;
606
607         strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
608         dev->ib_dev.node_type            = IB_NODE_CA;
609         dev->ib_dev.phys_port_cnt        = dev->limits.num_ports;
610         dev->ib_dev.dma_device           = &dev->pdev->dev;
611         dev->ib_dev.class_dev.dev        = &dev->pdev->dev;
612         dev->ib_dev.query_device         = mthca_query_device;
613         dev->ib_dev.query_port           = mthca_query_port;
614         dev->ib_dev.modify_port          = mthca_modify_port;
615         dev->ib_dev.query_pkey           = mthca_query_pkey;
616         dev->ib_dev.query_gid            = mthca_query_gid;
617         dev->ib_dev.alloc_pd             = mthca_alloc_pd;
618         dev->ib_dev.dealloc_pd           = mthca_dealloc_pd;
619         dev->ib_dev.create_ah            = mthca_ah_create;
620         dev->ib_dev.destroy_ah           = mthca_ah_destroy;
621         dev->ib_dev.create_qp            = mthca_create_qp;
622         dev->ib_dev.modify_qp            = mthca_modify_qp;
623         dev->ib_dev.destroy_qp           = mthca_destroy_qp;
624         dev->ib_dev.post_send            = mthca_post_send;
625         dev->ib_dev.post_recv            = mthca_post_receive;
626         dev->ib_dev.create_cq            = mthca_create_cq;
627         dev->ib_dev.destroy_cq           = mthca_destroy_cq;
628         dev->ib_dev.poll_cq              = mthca_poll_cq;
629         dev->ib_dev.req_notify_cq        = mthca_req_notify_cq;
630         dev->ib_dev.get_dma_mr           = mthca_get_dma_mr;
631         dev->ib_dev.reg_phys_mr          = mthca_reg_phys_mr;
632         dev->ib_dev.dereg_mr             = mthca_dereg_mr;
633         dev->ib_dev.attach_mcast         = mthca_multicast_attach;
634         dev->ib_dev.detach_mcast         = mthca_multicast_detach;
635         dev->ib_dev.process_mad          = mthca_process_mad;
636
637         init_MUTEX(&dev->cap_mask_mutex);
638
639         ret = ib_register_device(&dev->ib_dev);
640         if (ret)
641                 return ret;
642
643         for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
644                 ret = class_device_create_file(&dev->ib_dev.class_dev,
645                                                mthca_class_attributes[i]);
646                 if (ret) {
647                         ib_unregister_device(&dev->ib_dev);
648                         return ret;
649                 }
650         }
651
652         return 0;
653 }
654
655 void mthca_unregister_device(struct mthca_dev *dev)
656 {
657         ib_unregister_device(&dev->ib_dev);
658 }