]> err.no Git - linux-2.6/blob - drivers/infiniband/hw/mthca/mthca_provider.c
[PATCH] IB uverbs: add mthca user context support
[linux-2.6] / drivers / infiniband / hw / mthca / mthca_provider.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2005 Cisco Systems. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  * $Id: mthca_provider.c 1397 2004-12-28 05:09:00Z roland $
35  */
36
37 #include <ib_smi.h>
38
39 #include "mthca_dev.h"
40 #include "mthca_cmd.h"
41 #include "mthca_user.h"
42 #include "mthca_memfree.h"
43
44 static int mthca_query_device(struct ib_device *ibdev,
45                               struct ib_device_attr *props)
46 {
47         struct ib_smp *in_mad  = NULL;
48         struct ib_smp *out_mad = NULL;
49         int err = -ENOMEM;
50         struct mthca_dev* mdev = to_mdev(ibdev);
51
52         u8 status;
53
54         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
55         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
56         if (!in_mad || !out_mad)
57                 goto out;
58
59         memset(props, 0, sizeof *props);
60
61         props->fw_ver              = mdev->fw_ver;
62
63         memset(in_mad, 0, sizeof *in_mad);
64         in_mad->base_version       = 1;
65         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
66         in_mad->class_version      = 1;
67         in_mad->method             = IB_MGMT_METHOD_GET;
68         in_mad->attr_id            = IB_SMP_ATTR_NODE_INFO;
69
70         err = mthca_MAD_IFC(mdev, 1, 1,
71                             1, NULL, NULL, in_mad, out_mad,
72                             &status);
73         if (err)
74                 goto out;
75         if (status) {
76                 err = -EINVAL;
77                 goto out;
78         }
79
80         props->device_cap_flags    = mdev->device_cap_flags;
81         props->vendor_id           = be32_to_cpup((u32 *) (out_mad->data + 36)) &
82                 0xffffff;
83         props->vendor_part_id      = be16_to_cpup((u16 *) (out_mad->data + 30));
84         props->hw_ver              = be16_to_cpup((u16 *) (out_mad->data + 32));
85         memcpy(&props->sys_image_guid, out_mad->data +  4, 8);
86         memcpy(&props->node_guid,      out_mad->data + 12, 8);
87
88         props->max_mr_size         = ~0ull;
89         props->max_qp              = mdev->limits.num_qps - mdev->limits.reserved_qps;
90         props->max_qp_wr           = 0xffff;
91         props->max_sge             = mdev->limits.max_sg;
92         props->max_cq              = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
93         props->max_cqe             = 0xffff;
94         props->max_mr              = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
95         props->max_pd              = mdev->limits.num_pds - mdev->limits.reserved_pds;
96         props->max_qp_rd_atom      = 1 << mdev->qp_table.rdb_shift;
97         props->max_qp_init_rd_atom = 1 << mdev->qp_table.rdb_shift;
98         props->local_ca_ack_delay  = mdev->limits.local_ca_ack_delay;
99
100         err = 0;
101  out:
102         kfree(in_mad);
103         kfree(out_mad);
104         return err;
105 }
106
107 static int mthca_query_port(struct ib_device *ibdev,
108                             u8 port, struct ib_port_attr *props)
109 {
110         struct ib_smp *in_mad  = NULL;
111         struct ib_smp *out_mad = NULL;
112         int err = -ENOMEM;
113         u8 status;
114
115         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
116         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
117         if (!in_mad || !out_mad)
118                 goto out;
119
120         memset(in_mad, 0, sizeof *in_mad);
121         in_mad->base_version       = 1;
122         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
123         in_mad->class_version      = 1;
124         in_mad->method             = IB_MGMT_METHOD_GET;
125         in_mad->attr_id            = IB_SMP_ATTR_PORT_INFO;
126         in_mad->attr_mod           = cpu_to_be32(port);
127
128         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
129                             port, NULL, NULL, in_mad, out_mad,
130                             &status);
131         if (err)
132                 goto out;
133         if (status) {
134                 err = -EINVAL;
135                 goto out;
136         }
137
138         props->lid               = be16_to_cpup((u16 *) (out_mad->data + 16));
139         props->lmc               = out_mad->data[34] & 0x7;
140         props->sm_lid            = be16_to_cpup((u16 *) (out_mad->data + 18));
141         props->sm_sl             = out_mad->data[36] & 0xf;
142         props->state             = out_mad->data[32] & 0xf;
143         props->phys_state        = out_mad->data[33] >> 4;
144         props->port_cap_flags    = be32_to_cpup((u32 *) (out_mad->data + 20));
145         props->gid_tbl_len       = to_mdev(ibdev)->limits.gid_table_len;
146         props->pkey_tbl_len      = to_mdev(ibdev)->limits.pkey_table_len;
147         props->qkey_viol_cntr    = be16_to_cpup((u16 *) (out_mad->data + 48));
148         props->active_width      = out_mad->data[31] & 0xf;
149         props->active_speed      = out_mad->data[35] >> 4;
150
151  out:
152         kfree(in_mad);
153         kfree(out_mad);
154         return err;
155 }
156
157 static int mthca_modify_port(struct ib_device *ibdev,
158                              u8 port, int port_modify_mask,
159                              struct ib_port_modify *props)
160 {
161         struct mthca_set_ib_param set_ib;
162         struct ib_port_attr attr;
163         int err;
164         u8 status;
165
166         if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
167                 return -ERESTARTSYS;
168
169         err = mthca_query_port(ibdev, port, &attr);
170         if (err)
171                 goto out;
172
173         set_ib.set_si_guid     = 0;
174         set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
175
176         set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
177                 ~props->clr_port_cap_mask;
178
179         err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
180         if (err)
181                 goto out;
182         if (status) {
183                 err = -EINVAL;
184                 goto out;
185         }
186
187 out:
188         up(&to_mdev(ibdev)->cap_mask_mutex);
189         return err;
190 }
191
192 static int mthca_query_pkey(struct ib_device *ibdev,
193                             u8 port, u16 index, u16 *pkey)
194 {
195         struct ib_smp *in_mad  = NULL;
196         struct ib_smp *out_mad = NULL;
197         int err = -ENOMEM;
198         u8 status;
199
200         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
201         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
202         if (!in_mad || !out_mad)
203                 goto out;
204
205         memset(in_mad, 0, sizeof *in_mad);
206         in_mad->base_version       = 1;
207         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
208         in_mad->class_version      = 1;
209         in_mad->method             = IB_MGMT_METHOD_GET;
210         in_mad->attr_id            = IB_SMP_ATTR_PKEY_TABLE;
211         in_mad->attr_mod           = cpu_to_be32(index / 32);
212
213         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
214                             port, NULL, NULL, in_mad, out_mad,
215                             &status);
216         if (err)
217                 goto out;
218         if (status) {
219                 err = -EINVAL;
220                 goto out;
221         }
222
223         *pkey = be16_to_cpu(((u16 *) out_mad->data)[index % 32]);
224
225  out:
226         kfree(in_mad);
227         kfree(out_mad);
228         return err;
229 }
230
231 static int mthca_query_gid(struct ib_device *ibdev, u8 port,
232                            int index, union ib_gid *gid)
233 {
234         struct ib_smp *in_mad  = NULL;
235         struct ib_smp *out_mad = NULL;
236         int err = -ENOMEM;
237         u8 status;
238
239         in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
240         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
241         if (!in_mad || !out_mad)
242                 goto out;
243
244         memset(in_mad, 0, sizeof *in_mad);
245         in_mad->base_version       = 1;
246         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
247         in_mad->class_version      = 1;
248         in_mad->method             = IB_MGMT_METHOD_GET;
249         in_mad->attr_id            = IB_SMP_ATTR_PORT_INFO;
250         in_mad->attr_mod           = cpu_to_be32(port);
251
252         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
253                             port, NULL, NULL, in_mad, out_mad,
254                             &status);
255         if (err)
256                 goto out;
257         if (status) {
258                 err = -EINVAL;
259                 goto out;
260         }
261
262         memcpy(gid->raw, out_mad->data + 8, 8);
263
264         memset(in_mad, 0, sizeof *in_mad);
265         in_mad->base_version       = 1;
266         in_mad->mgmt_class         = IB_MGMT_CLASS_SUBN_LID_ROUTED;
267         in_mad->class_version      = 1;
268         in_mad->method             = IB_MGMT_METHOD_GET;
269         in_mad->attr_id            = IB_SMP_ATTR_GUID_INFO;
270         in_mad->attr_mod           = cpu_to_be32(index / 8);
271
272         err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
273                             port, NULL, NULL, in_mad, out_mad,
274                             &status);
275         if (err)
276                 goto out;
277         if (status) {
278                 err = -EINVAL;
279                 goto out;
280         }
281
282         memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
283
284  out:
285         kfree(in_mad);
286         kfree(out_mad);
287         return err;
288 }
289
290 static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
291                                                 struct ib_udata *udata)
292 {
293         struct mthca_alloc_ucontext_resp uresp;
294         struct mthca_ucontext           *context;
295         int                              err;
296
297         memset(&uresp, 0, sizeof uresp);
298
299         uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
300         if (mthca_is_memfree(to_mdev(ibdev)))
301                 uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
302         else
303                 uresp.uarc_size = 0;
304
305         context = kmalloc(sizeof *context, GFP_KERNEL);
306         if (!context)
307                 return ERR_PTR(-ENOMEM);
308
309         err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
310         if (err) {
311                 kfree(context);
312                 return ERR_PTR(err);
313         }
314
315         context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
316         if (IS_ERR(context->db_tab)) {
317                 err = PTR_ERR(context->db_tab);
318                 mthca_uar_free(to_mdev(ibdev), &context->uar);
319                 kfree(context);
320                 return ERR_PTR(err);
321         }
322
323         if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
324                 mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
325                 mthca_uar_free(to_mdev(ibdev), &context->uar);
326                 kfree(context);
327                 return ERR_PTR(-EFAULT);
328         }
329
330         return &context->ibucontext;
331 }
332
333 static int mthca_dealloc_ucontext(struct ib_ucontext *context)
334 {
335         mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
336                                   to_mucontext(context)->db_tab);
337         mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
338         kfree(to_mucontext(context));
339
340         return 0;
341 }
342
343 static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
344                                     struct ib_ucontext *context,
345                                     struct ib_udata *udata)
346 {
347         struct mthca_pd *pd;
348         int err;
349
350         pd = kmalloc(sizeof *pd, GFP_KERNEL);
351         if (!pd)
352                 return ERR_PTR(-ENOMEM);
353
354         err = mthca_pd_alloc(to_mdev(ibdev), pd);
355         if (err) {
356                 kfree(pd);
357                 return ERR_PTR(err);
358         }
359
360         return &pd->ibpd;
361 }
362
363 static int mthca_dealloc_pd(struct ib_pd *pd)
364 {
365         mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
366         kfree(pd);
367
368         return 0;
369 }
370
371 static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
372                                      struct ib_ah_attr *ah_attr)
373 {
374         int err;
375         struct mthca_ah *ah;
376
377         ah = kmalloc(sizeof *ah, GFP_ATOMIC);
378         if (!ah)
379                 return ERR_PTR(-ENOMEM);
380
381         err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
382         if (err) {
383                 kfree(ah);
384                 return ERR_PTR(err);
385         }
386
387         return &ah->ibah;
388 }
389
390 static int mthca_ah_destroy(struct ib_ah *ah)
391 {
392         mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
393         kfree(ah);
394
395         return 0;
396 }
397
398 static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
399                                      struct ib_qp_init_attr *init_attr,
400                                      struct ib_udata *udata)
401 {
402         struct mthca_qp *qp;
403         int err;
404
405         switch (init_attr->qp_type) {
406         case IB_QPT_RC:
407         case IB_QPT_UC:
408         case IB_QPT_UD:
409         {
410                 qp = kmalloc(sizeof *qp, GFP_KERNEL);
411                 if (!qp)
412                         return ERR_PTR(-ENOMEM);
413
414                 qp->sq.max    = init_attr->cap.max_send_wr;
415                 qp->rq.max    = init_attr->cap.max_recv_wr;
416                 qp->sq.max_gs = init_attr->cap.max_send_sge;
417                 qp->rq.max_gs = init_attr->cap.max_recv_sge;
418
419                 err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
420                                      to_mcq(init_attr->send_cq),
421                                      to_mcq(init_attr->recv_cq),
422                                      init_attr->qp_type, init_attr->sq_sig_type,
423                                      qp);
424                 qp->ibqp.qp_num = qp->qpn;
425                 break;
426         }
427         case IB_QPT_SMI:
428         case IB_QPT_GSI:
429         {
430                 qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
431                 if (!qp)
432                         return ERR_PTR(-ENOMEM);
433
434                 qp->sq.max    = init_attr->cap.max_send_wr;
435                 qp->rq.max    = init_attr->cap.max_recv_wr;
436                 qp->sq.max_gs = init_attr->cap.max_send_sge;
437                 qp->rq.max_gs = init_attr->cap.max_recv_sge;
438
439                 qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
440
441                 err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
442                                       to_mcq(init_attr->send_cq),
443                                       to_mcq(init_attr->recv_cq),
444                                       init_attr->sq_sig_type,
445                                       qp->ibqp.qp_num, init_attr->port_num,
446                                       to_msqp(qp));
447                 break;
448         }
449         default:
450                 /* Don't support raw QPs */
451                 return ERR_PTR(-ENOSYS);
452         }
453
454         if (err) {
455                 kfree(qp);
456                 return ERR_PTR(err);
457         }
458
459         init_attr->cap.max_inline_data = 0;
460
461         return &qp->ibqp;
462 }
463
464 static int mthca_destroy_qp(struct ib_qp *qp)
465 {
466         mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
467         kfree(qp);
468         return 0;
469 }
470
471 static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
472                                      struct ib_ucontext *context,
473                                      struct ib_udata *udata)
474 {
475         struct mthca_cq *cq;
476         int nent;
477         int err;
478
479         cq = kmalloc(sizeof *cq, GFP_KERNEL);
480         if (!cq)
481                 return ERR_PTR(-ENOMEM);
482
483         for (nent = 1; nent <= entries; nent <<= 1)
484                 ; /* nothing */
485
486         err = mthca_init_cq(to_mdev(ibdev), nent, cq);
487         if (err) {
488                 kfree(cq);
489                 cq = ERR_PTR(err);
490         }
491
492         return &cq->ibcq;
493 }
494
495 static int mthca_destroy_cq(struct ib_cq *cq)
496 {
497         mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
498         kfree(cq);
499
500         return 0;
501 }
502
503 static inline u32 convert_access(int acc)
504 {
505         return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC       : 0) |
506                (acc & IB_ACCESS_REMOTE_WRITE  ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
507                (acc & IB_ACCESS_REMOTE_READ   ? MTHCA_MPT_FLAG_REMOTE_READ  : 0) |
508                (acc & IB_ACCESS_LOCAL_WRITE   ? MTHCA_MPT_FLAG_LOCAL_WRITE  : 0) |
509                MTHCA_MPT_FLAG_LOCAL_READ;
510 }
511
512 static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
513 {
514         struct mthca_mr *mr;
515         int err;
516
517         mr = kmalloc(sizeof *mr, GFP_KERNEL);
518         if (!mr)
519                 return ERR_PTR(-ENOMEM);
520
521         err = mthca_mr_alloc_notrans(to_mdev(pd->device),
522                                      to_mpd(pd)->pd_num,
523                                      convert_access(acc), mr);
524
525         if (err) {
526                 kfree(mr);
527                 return ERR_PTR(err);
528         }
529
530         return &mr->ibmr;
531 }
532
533 static struct ib_mr *mthca_reg_phys_mr(struct ib_pd       *pd,
534                                        struct ib_phys_buf *buffer_list,
535                                        int                 num_phys_buf,
536                                        int                 acc,
537                                        u64                *iova_start)
538 {
539         struct mthca_mr *mr;
540         u64 *page_list;
541         u64 total_size;
542         u64 mask;
543         int shift;
544         int npages;
545         int err;
546         int i, j, n;
547
548         /* First check that we have enough alignment */
549         if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
550                 return ERR_PTR(-EINVAL);
551
552         if (num_phys_buf > 1 &&
553             ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
554                 return ERR_PTR(-EINVAL);
555
556         mask = 0;
557         total_size = 0;
558         for (i = 0; i < num_phys_buf; ++i) {
559                 if (i != 0 && buffer_list[i].addr & ~PAGE_MASK)
560                         return ERR_PTR(-EINVAL);
561                 if (i != 0 && i != num_phys_buf - 1 &&
562                     (buffer_list[i].size & ~PAGE_MASK))
563                         return ERR_PTR(-EINVAL);
564
565                 total_size += buffer_list[i].size;
566                 if (i > 0)
567                         mask |= buffer_list[i].addr;
568         }
569
570         /* Find largest page shift we can use to cover buffers */
571         for (shift = PAGE_SHIFT; shift < 31; ++shift)
572                 if (num_phys_buf > 1) {
573                         if ((1ULL << shift) & mask)
574                                 break;
575                 } else {
576                         if (1ULL << shift >=
577                             buffer_list[0].size +
578                             (buffer_list[0].addr & ((1ULL << shift) - 1)))
579                                 break;
580                 }
581
582         buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
583         buffer_list[0].addr &= ~0ull << shift;
584
585         mr = kmalloc(sizeof *mr, GFP_KERNEL);
586         if (!mr)
587                 return ERR_PTR(-ENOMEM);
588
589         npages = 0;
590         for (i = 0; i < num_phys_buf; ++i)
591                 npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
592
593         if (!npages)
594                 return &mr->ibmr;
595
596         page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
597         if (!page_list) {
598                 kfree(mr);
599                 return ERR_PTR(-ENOMEM);
600         }
601
602         n = 0;
603         for (i = 0; i < num_phys_buf; ++i)
604                 for (j = 0;
605                      j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
606                      ++j)
607                         page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
608
609         mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
610                   "in PD %x; shift %d, npages %d.\n",
611                   (unsigned long long) buffer_list[0].addr,
612                   (unsigned long long) *iova_start,
613                   to_mpd(pd)->pd_num,
614                   shift, npages);
615
616         err = mthca_mr_alloc_phys(to_mdev(pd->device),
617                                   to_mpd(pd)->pd_num,
618                                   page_list, shift, npages,
619                                   *iova_start, total_size,
620                                   convert_access(acc), mr);
621
622         if (err) {
623                 kfree(page_list);
624                 kfree(mr);
625                 return ERR_PTR(err);
626         }
627
628         kfree(page_list);
629         return &mr->ibmr;
630 }
631
632 static int mthca_dereg_mr(struct ib_mr *mr)
633 {
634         struct mthca_mr *mmr = to_mmr(mr);
635         mthca_free_mr(to_mdev(mr->device), mmr);
636         kfree(mmr);
637         return 0;
638 }
639
640 static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
641                                       struct ib_fmr_attr *fmr_attr)
642 {
643         struct mthca_fmr *fmr;
644         int err;
645
646         fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
647         if (!fmr)
648                 return ERR_PTR(-ENOMEM);
649
650         memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
651         err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
652                              convert_access(mr_access_flags), fmr);
653
654         if (err) {
655                 kfree(fmr);
656                 return ERR_PTR(err);
657         }
658
659         return &fmr->ibmr;
660 }
661
662 static int mthca_dealloc_fmr(struct ib_fmr *fmr)
663 {
664         struct mthca_fmr *mfmr = to_mfmr(fmr);
665         int err;
666
667         err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
668         if (err)
669                 return err;
670
671         kfree(mfmr);
672         return 0;
673 }
674
675 static int mthca_unmap_fmr(struct list_head *fmr_list)
676 {
677         struct ib_fmr *fmr;
678         int err;
679         u8 status;
680         struct mthca_dev *mdev = NULL;
681
682         list_for_each_entry(fmr, fmr_list, list) {
683                 if (mdev && to_mdev(fmr->device) != mdev)
684                         return -EINVAL;
685                 mdev = to_mdev(fmr->device);
686         }
687
688         if (!mdev)
689                 return 0;
690
691         if (mthca_is_memfree(mdev)) {
692                 list_for_each_entry(fmr, fmr_list, list)
693                         mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
694
695                 wmb();
696         } else
697                 list_for_each_entry(fmr, fmr_list, list)
698                         mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
699
700         err = mthca_SYNC_TPT(mdev, &status);
701         if (err)
702                 return err;
703         if (status)
704                 return -EINVAL;
705         return 0;
706 }
707
708 static ssize_t show_rev(struct class_device *cdev, char *buf)
709 {
710         struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
711         return sprintf(buf, "%x\n", dev->rev_id);
712 }
713
714 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
715 {
716         struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
717         return sprintf(buf, "%x.%x.%x\n", (int) (dev->fw_ver >> 32),
718                        (int) (dev->fw_ver >> 16) & 0xffff,
719                        (int) dev->fw_ver & 0xffff);
720 }
721
722 static ssize_t show_hca(struct class_device *cdev, char *buf)
723 {
724         struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
725         switch (dev->pdev->device) {
726         case PCI_DEVICE_ID_MELLANOX_TAVOR:
727                 return sprintf(buf, "MT23108\n");
728         case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
729                 return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
730         case PCI_DEVICE_ID_MELLANOX_ARBEL:
731                 return sprintf(buf, "MT25208\n");
732         case PCI_DEVICE_ID_MELLANOX_SINAI:
733         case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
734                 return sprintf(buf, "MT25204\n");
735         default:
736                 return sprintf(buf, "unknown\n");
737         }
738 }
739
740 static CLASS_DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
741 static CLASS_DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
742 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
743
744 static struct class_device_attribute *mthca_class_attributes[] = {
745         &class_device_attr_hw_rev,
746         &class_device_attr_fw_ver,
747         &class_device_attr_hca_type
748 };
749
750 int mthca_register_device(struct mthca_dev *dev)
751 {
752         int ret;
753         int i;
754
755         strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
756         dev->ib_dev.owner                = THIS_MODULE;
757
758         dev->ib_dev.node_type            = IB_NODE_CA;
759         dev->ib_dev.phys_port_cnt        = dev->limits.num_ports;
760         dev->ib_dev.dma_device           = &dev->pdev->dev;
761         dev->ib_dev.class_dev.dev        = &dev->pdev->dev;
762         dev->ib_dev.query_device         = mthca_query_device;
763         dev->ib_dev.query_port           = mthca_query_port;
764         dev->ib_dev.modify_port          = mthca_modify_port;
765         dev->ib_dev.query_pkey           = mthca_query_pkey;
766         dev->ib_dev.query_gid            = mthca_query_gid;
767         dev->ib_dev.alloc_ucontext       = mthca_alloc_ucontext;
768         dev->ib_dev.dealloc_ucontext     = mthca_dealloc_ucontext;
769         dev->ib_dev.alloc_pd             = mthca_alloc_pd;
770         dev->ib_dev.dealloc_pd           = mthca_dealloc_pd;
771         dev->ib_dev.create_ah            = mthca_ah_create;
772         dev->ib_dev.destroy_ah           = mthca_ah_destroy;
773         dev->ib_dev.create_qp            = mthca_create_qp;
774         dev->ib_dev.modify_qp            = mthca_modify_qp;
775         dev->ib_dev.destroy_qp           = mthca_destroy_qp;
776         dev->ib_dev.create_cq            = mthca_create_cq;
777         dev->ib_dev.destroy_cq           = mthca_destroy_cq;
778         dev->ib_dev.poll_cq              = mthca_poll_cq;
779         dev->ib_dev.get_dma_mr           = mthca_get_dma_mr;
780         dev->ib_dev.reg_phys_mr          = mthca_reg_phys_mr;
781         dev->ib_dev.dereg_mr             = mthca_dereg_mr;
782
783         if (dev->mthca_flags & MTHCA_FLAG_FMR) {
784                 dev->ib_dev.alloc_fmr            = mthca_alloc_fmr;
785                 dev->ib_dev.unmap_fmr            = mthca_unmap_fmr;
786                 dev->ib_dev.dealloc_fmr          = mthca_dealloc_fmr;
787                 if (mthca_is_memfree(dev))
788                         dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
789                 else
790                         dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
791         }
792
793         dev->ib_dev.attach_mcast         = mthca_multicast_attach;
794         dev->ib_dev.detach_mcast         = mthca_multicast_detach;
795         dev->ib_dev.process_mad          = mthca_process_mad;
796
797         if (mthca_is_memfree(dev)) {
798                 dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
799                 dev->ib_dev.post_send     = mthca_arbel_post_send;
800                 dev->ib_dev.post_recv     = mthca_arbel_post_receive;
801         } else {
802                 dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
803                 dev->ib_dev.post_send     = mthca_tavor_post_send;
804                 dev->ib_dev.post_recv     = mthca_tavor_post_receive;
805         }
806
807         init_MUTEX(&dev->cap_mask_mutex);
808
809         ret = ib_register_device(&dev->ib_dev);
810         if (ret)
811                 return ret;
812
813         for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
814                 ret = class_device_create_file(&dev->ib_dev.class_dev,
815                                                mthca_class_attributes[i]);
816                 if (ret) {
817                         ib_unregister_device(&dev->ib_dev);
818                         return ret;
819                 }
820         }
821
822         return 0;
823 }
824
825 void mthca_unregister_device(struct mthca_dev *dev)
826 {
827         ib_unregister_device(&dev->ib_dev);
828 }