blob: 76321f0ef0198f5c14513793dbd46fa8977b1034 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Tom Duffycd4e8fb2005-06-27 14:36:37 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Roland Dreier4885bf62006-01-30 14:31:33 -08004 * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07005 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer.
21 *
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
35 *
Sean Heftycf311cd2006-01-10 07:39:34 -080036 * $Id: mthca_provider.c 4859 2006-01-09 21:55:10Z roland $
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Roland Dreiera4d61e82005-08-25 13:40:04 -070039#include <rdma/ib_smi.h>
Roland Dreier883a99c2005-10-14 14:00:58 -070040#include <rdma/ib_user_verbs.h>
Roland Dreier53b8b3f2005-07-07 17:57:17 -070041#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "mthca_dev.h"
44#include "mthca_cmd.h"
Roland Dreier5e0b5372005-07-07 17:57:16 -070045#include "mthca_user.h"
46#include "mthca_memfree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Roland Dreier87635b72006-01-09 15:29:53 -080048static void init_query_mad(struct ib_smp *mad)
49{
50 mad->base_version = 1;
51 mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
52 mad->class_version = 1;
53 mad->method = IB_MGMT_METHOD_GET;
54}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int mthca_query_device(struct ib_device *ibdev,
57 struct ib_device_attr *props)
58{
59 struct ib_smp *in_mad = NULL;
60 struct ib_smp *out_mad = NULL;
61 int err = -ENOMEM;
62 struct mthca_dev* mdev = to_mdev(ibdev);
63
64 u8 status;
65
Roland Dreier105e50a2006-01-09 15:21:21 -080066 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
68 if (!in_mad || !out_mad)
69 goto out;
70
Roland Dreiera8520922005-06-27 14:36:42 -070071 memset(props, 0, sizeof *props);
Roland Dreier8cf2daf2005-04-16 15:26:14 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 props->fw_ver = mdev->fw_ver;
74
Roland Dreier87635b72006-01-09 15:29:53 -080075 init_query_mad(in_mad);
76 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 err = mthca_MAD_IFC(mdev, 1, 1,
79 1, NULL, NULL, in_mad, out_mad,
80 &status);
81 if (err)
82 goto out;
83 if (status) {
84 err = -EINVAL;
85 goto out;
86 }
87
Roland Dreier8cf2daf2005-04-16 15:26:14 -070088 props->device_cap_flags = mdev->device_cap_flags;
Sean Hefty97f52eb2005-08-13 21:05:57 -070089 props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 0xffffff;
Sean Hefty97f52eb2005-08-13 21:05:57 -070091 props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
Jack Morgensteina1c337a2005-09-27 13:54:44 -070092 props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Roland Dreier8cf2daf2005-04-16 15:26:14 -070095 props->max_mr_size = ~0ull;
Jack Morgenstein0f69ce12005-11-04 16:03:32 -080096 props->page_size_cap = mdev->limits.page_size_cap;
Roland Dreier8cf2daf2005-04-16 15:26:14 -070097 props->max_qp = mdev->limits.num_qps - mdev->limits.reserved_qps;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -070098 props->max_qp_wr = mdev->limits.max_wqes;
Roland Dreier8cf2daf2005-04-16 15:26:14 -070099 props->max_sge = mdev->limits.max_sg;
100 props->max_cq = mdev->limits.num_cqs - mdev->limits.reserved_cqs;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700101 props->max_cqe = mdev->limits.max_cqes;
Roland Dreier8cf2daf2005-04-16 15:26:14 -0700102 props->max_mr = mdev->limits.num_mpts - mdev->limits.reserved_mrws;
103 props->max_pd = mdev->limits.num_pds - mdev->limits.reserved_pds;
104 props->max_qp_rd_atom = 1 << mdev->qp_table.rdb_shift;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700105 props->max_qp_init_rd_atom = mdev->limits.max_qp_init_rdma;
106 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
107 props->max_srq = mdev->limits.num_srqs - mdev->limits.reserved_srqs;
108 props->max_srq_wr = mdev->limits.max_srq_wqes;
109 props->max_srq_sge = mdev->limits.max_sg;
Roland Dreier8cf2daf2005-04-16 15:26:14 -0700110 props->local_ca_ack_delay = mdev->limits.local_ca_ack_delay;
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800111 props->atomic_cap = mdev->limits.flags & DEV_LIM_FLAG_ATOMIC ?
Jack Morgenstein33033b72005-09-26 12:30:02 -0700112 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700113 props->max_pkeys = mdev->limits.pkey_table_len;
114 props->max_mcast_grp = mdev->limits.num_mgms + mdev->limits.num_amgms;
115 props->max_mcast_qp_attach = MTHCA_QP_PER_MGM;
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800116 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700117 props->max_mcast_grp;
Roland Dreier8cf2daf2005-04-16 15:26:14 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 err = 0;
120 out:
121 kfree(in_mad);
122 kfree(out_mad);
123 return err;
124}
125
126static int mthca_query_port(struct ib_device *ibdev,
127 u8 port, struct ib_port_attr *props)
128{
129 struct ib_smp *in_mad = NULL;
130 struct ib_smp *out_mad = NULL;
131 int err = -ENOMEM;
132 u8 status;
133
Roland Dreier105e50a2006-01-09 15:21:21 -0800134 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
136 if (!in_mad || !out_mad)
137 goto out;
138
Roland Dreierd1887ec2005-08-18 12:14:11 -0700139 memset(props, 0, sizeof *props);
140
Roland Dreier87635b72006-01-09 15:29:53 -0800141 init_query_mad(in_mad);
142 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
143 in_mad->attr_mod = cpu_to_be32(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
146 port, NULL, NULL, in_mad, out_mad,
147 &status);
148 if (err)
149 goto out;
150 if (status) {
151 err = -EINVAL;
152 goto out;
153 }
154
Sean Hefty97f52eb2005-08-13 21:05:57 -0700155 props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 props->lmc = out_mad->data[34] & 0x7;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700157 props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 props->sm_sl = out_mad->data[36] & 0xf;
159 props->state = out_mad->data[32] & 0xf;
160 props->phys_state = out_mad->data[33] >> 4;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700161 props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 props->gid_tbl_len = to_mdev(ibdev)->limits.gid_table_len;
Roland Dreierd1887ec2005-08-18 12:14:11 -0700163 props->max_msg_sz = 0x80000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 props->pkey_tbl_len = to_mdev(ibdev)->limits.pkey_table_len;
Jack Morgenstein98250512005-10-01 13:09:44 -0700165 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
Sean Hefty97f52eb2005-08-13 21:05:57 -0700166 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 props->active_width = out_mad->data[31] & 0xf;
168 props->active_speed = out_mad->data[35] >> 4;
Jack Morgenstein98250512005-10-01 13:09:44 -0700169 props->max_mtu = out_mad->data[41] & 0xf;
170 props->active_mtu = out_mad->data[36] >> 4;
171 props->subnet_timeout = out_mad->data[51] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 out:
174 kfree(in_mad);
175 kfree(out_mad);
176 return err;
177}
178
179static int mthca_modify_port(struct ib_device *ibdev,
180 u8 port, int port_modify_mask,
181 struct ib_port_modify *props)
182{
183 struct mthca_set_ib_param set_ib;
184 struct ib_port_attr attr;
185 int err;
186 u8 status;
187
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800188 if (mutex_lock_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -ERESTARTSYS;
190
191 err = mthca_query_port(ibdev, port, &attr);
192 if (err)
193 goto out;
194
195 set_ib.set_si_guid = 0;
196 set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
197
198 set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
199 ~props->clr_port_cap_mask;
200
201 err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
202 if (err)
203 goto out;
204 if (status) {
205 err = -EINVAL;
206 goto out;
207 }
208
209out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800210 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return err;
212}
213
214static int mthca_query_pkey(struct ib_device *ibdev,
215 u8 port, u16 index, u16 *pkey)
216{
217 struct ib_smp *in_mad = NULL;
218 struct ib_smp *out_mad = NULL;
219 int err = -ENOMEM;
220 u8 status;
221
Roland Dreier105e50a2006-01-09 15:21:21 -0800222 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
224 if (!in_mad || !out_mad)
225 goto out;
226
Roland Dreier87635b72006-01-09 15:29:53 -0800227 init_query_mad(in_mad);
228 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
229 in_mad->attr_mod = cpu_to_be32(index / 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
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
Sean Hefty97f52eb2005-08-13 21:05:57 -0700241 *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 out:
244 kfree(in_mad);
245 kfree(out_mad);
246 return err;
247}
248
249static int mthca_query_gid(struct ib_device *ibdev, u8 port,
250 int index, union ib_gid *gid)
251{
252 struct ib_smp *in_mad = NULL;
253 struct ib_smp *out_mad = NULL;
254 int err = -ENOMEM;
255 u8 status;
256
Roland Dreier105e50a2006-01-09 15:21:21 -0800257 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
259 if (!in_mad || !out_mad)
260 goto out;
261
Roland Dreier87635b72006-01-09 15:29:53 -0800262 init_query_mad(in_mad);
263 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
264 in_mad->attr_mod = cpu_to_be32(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
267 port, NULL, NULL, in_mad, out_mad,
268 &status);
269 if (err)
270 goto out;
271 if (status) {
272 err = -EINVAL;
273 goto out;
274 }
275
276 memcpy(gid->raw, out_mad->data + 8, 8);
277
Roland Dreier87635b72006-01-09 15:29:53 -0800278 init_query_mad(in_mad);
279 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
280 in_mad->attr_mod = cpu_to_be32(index / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
283 port, NULL, NULL, in_mad, out_mad,
284 &status);
285 if (err)
286 goto out;
287 if (status) {
288 err = -EINVAL;
289 goto out;
290 }
291
292 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
293
294 out:
295 kfree(in_mad);
296 kfree(out_mad);
297 return err;
298}
299
Roland Dreier5e0b5372005-07-07 17:57:16 -0700300static struct ib_ucontext *mthca_alloc_ucontext(struct ib_device *ibdev,
301 struct ib_udata *udata)
302{
303 struct mthca_alloc_ucontext_resp uresp;
304 struct mthca_ucontext *context;
305 int err;
306
307 memset(&uresp, 0, sizeof uresp);
308
309 uresp.qp_tab_size = to_mdev(ibdev)->limits.num_qps;
310 if (mthca_is_memfree(to_mdev(ibdev)))
311 uresp.uarc_size = to_mdev(ibdev)->uar_table.uarc_size;
312 else
313 uresp.uarc_size = 0;
314
315 context = kmalloc(sizeof *context, GFP_KERNEL);
316 if (!context)
317 return ERR_PTR(-ENOMEM);
318
319 err = mthca_uar_alloc(to_mdev(ibdev), &context->uar);
320 if (err) {
321 kfree(context);
322 return ERR_PTR(err);
323 }
324
325 context->db_tab = mthca_init_user_db_tab(to_mdev(ibdev));
326 if (IS_ERR(context->db_tab)) {
327 err = PTR_ERR(context->db_tab);
328 mthca_uar_free(to_mdev(ibdev), &context->uar);
329 kfree(context);
330 return ERR_PTR(err);
331 }
332
333 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
334 mthca_cleanup_user_db_tab(to_mdev(ibdev), &context->uar, context->db_tab);
335 mthca_uar_free(to_mdev(ibdev), &context->uar);
336 kfree(context);
337 return ERR_PTR(-EFAULT);
338 }
339
340 return &context->ibucontext;
341}
342
343static int mthca_dealloc_ucontext(struct ib_ucontext *context)
344{
345 mthca_cleanup_user_db_tab(to_mdev(context->device), &to_mucontext(context)->uar,
346 to_mucontext(context)->db_tab);
347 mthca_uar_free(to_mdev(context->device), &to_mucontext(context)->uar);
348 kfree(to_mucontext(context));
349
350 return 0;
351}
352
Roland Dreier53b8b3f2005-07-07 17:57:17 -0700353static int mthca_mmap_uar(struct ib_ucontext *context,
354 struct vm_area_struct *vma)
355{
356 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
357 return -EINVAL;
358
359 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
360
Michael S. Tsirkin6d376752005-07-27 14:42:45 -0700361 if (io_remap_pfn_range(vma, vma->vm_start,
362 to_mucontext(context)->uar.pfn,
363 PAGE_SIZE, vma->vm_page_prot))
Roland Dreier53b8b3f2005-07-07 17:57:17 -0700364 return -EAGAIN;
365
366 return 0;
367}
368
Roland Dreier1cf296b2005-07-07 17:57:11 -0700369static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
370 struct ib_ucontext *context,
371 struct ib_udata *udata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct mthca_pd *pd;
374 int err;
375
376 pd = kmalloc(sizeof *pd, GFP_KERNEL);
377 if (!pd)
378 return ERR_PTR(-ENOMEM);
379
Roland Dreier99264c12005-07-07 17:57:18 -0700380 err = mthca_pd_alloc(to_mdev(ibdev), !context, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (err) {
382 kfree(pd);
383 return ERR_PTR(err);
384 }
385
Roland Dreier99264c12005-07-07 17:57:18 -0700386 if (context) {
387 if (ib_copy_to_udata(udata, &pd->pd_num, sizeof (__u32))) {
388 mthca_pd_free(to_mdev(ibdev), pd);
389 kfree(pd);
390 return ERR_PTR(-EFAULT);
391 }
392 }
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return &pd->ibpd;
395}
396
397static int mthca_dealloc_pd(struct ib_pd *pd)
398{
399 mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
400 kfree(pd);
401
402 return 0;
403}
404
405static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
406 struct ib_ah_attr *ah_attr)
407{
408 int err;
409 struct mthca_ah *ah;
410
Roland Dreier8df8a342005-04-16 15:26:26 -0700411 ah = kmalloc(sizeof *ah, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!ah)
413 return ERR_PTR(-ENOMEM);
414
415 err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
416 if (err) {
417 kfree(ah);
418 return ERR_PTR(err);
419 }
420
421 return &ah->ibah;
422}
423
424static int mthca_ah_destroy(struct ib_ah *ah)
425{
426 mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
427 kfree(ah);
428
429 return 0;
430}
431
Roland Dreierec34a922005-08-19 10:59:31 -0700432static struct ib_srq *mthca_create_srq(struct ib_pd *pd,
433 struct ib_srq_init_attr *init_attr,
434 struct ib_udata *udata)
435{
436 struct mthca_create_srq ucmd;
437 struct mthca_ucontext *context = NULL;
438 struct mthca_srq *srq;
439 int err;
440
441 srq = kmalloc(sizeof *srq, GFP_KERNEL);
442 if (!srq)
443 return ERR_PTR(-ENOMEM);
444
445 if (pd->uobject) {
446 context = to_mucontext(pd->uobject->context);
447
Jack Morgenstein17e2e812006-01-12 15:35:15 -0800448 if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
449 err = -EFAULT;
450 goto err_free;
451 }
Roland Dreierec34a922005-08-19 10:59:31 -0700452
453 err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
454 context->db_tab, ucmd.db_index,
455 ucmd.db_page);
456
457 if (err)
458 goto err_free;
459
460 srq->mr.ibmr.lkey = ucmd.lkey;
461 srq->db_index = ucmd.db_index;
462 }
463
464 err = mthca_alloc_srq(to_mdev(pd->device), to_mpd(pd),
465 &init_attr->attr, srq);
466
467 if (err && pd->uobject)
468 mthca_unmap_user_db(to_mdev(pd->device), &context->uar,
469 context->db_tab, ucmd.db_index);
470
471 if (err)
472 goto err_free;
473
474 if (context && ib_copy_to_udata(udata, &srq->srqn, sizeof (__u32))) {
475 mthca_free_srq(to_mdev(pd->device), srq);
476 err = -EFAULT;
477 goto err_free;
478 }
479
480 return &srq->ibsrq;
481
482err_free:
483 kfree(srq);
484
485 return ERR_PTR(err);
486}
487
488static int mthca_destroy_srq(struct ib_srq *srq)
489{
490 struct mthca_ucontext *context;
491
492 if (srq->uobject) {
493 context = to_mucontext(srq->uobject->context);
494
495 mthca_unmap_user_db(to_mdev(srq->device), &context->uar,
496 context->db_tab, to_msrq(srq)->db_index);
497 }
498
499 mthca_free_srq(to_mdev(srq->device), to_msrq(srq));
500 kfree(srq);
501
502 return 0;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
Roland Dreier1cf296b2005-07-07 17:57:11 -0700506 struct ib_qp_init_attr *init_attr,
507 struct ib_udata *udata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Roland Dreier80c8ec22005-07-07 17:57:20 -0700509 struct mthca_create_qp ucmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct mthca_qp *qp;
511 int err;
512
513 switch (init_attr->qp_type) {
514 case IB_QPT_RC:
515 case IB_QPT_UC:
516 case IB_QPT_UD:
517 {
Roland Dreier80c8ec22005-07-07 17:57:20 -0700518 struct mthca_ucontext *context;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 qp = kmalloc(sizeof *qp, GFP_KERNEL);
521 if (!qp)
522 return ERR_PTR(-ENOMEM);
523
Roland Dreier80c8ec22005-07-07 17:57:20 -0700524 if (pd->uobject) {
525 context = to_mucontext(pd->uobject->context);
526
Jack Morgenstein17e2e812006-01-12 15:35:15 -0800527 if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
528 kfree(qp);
Roland Dreier80c8ec22005-07-07 17:57:20 -0700529 return ERR_PTR(-EFAULT);
Jack Morgenstein17e2e812006-01-12 15:35:15 -0800530 }
Roland Dreier80c8ec22005-07-07 17:57:20 -0700531
532 err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
533 context->db_tab,
534 ucmd.sq_db_index, ucmd.sq_db_page);
535 if (err) {
536 kfree(qp);
537 return ERR_PTR(err);
538 }
539
540 err = mthca_map_user_db(to_mdev(pd->device), &context->uar,
541 context->db_tab,
542 ucmd.rq_db_index, ucmd.rq_db_page);
543 if (err) {
544 mthca_unmap_user_db(to_mdev(pd->device),
545 &context->uar,
546 context->db_tab,
547 ucmd.sq_db_index);
548 kfree(qp);
549 return ERR_PTR(err);
550 }
551
552 qp->mr.ibmr.lkey = ucmd.lkey;
553 qp->sq.db_index = ucmd.sq_db_index;
554 qp->rq.db_index = ucmd.rq_db_index;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 err = mthca_alloc_qp(to_mdev(pd->device), to_mpd(pd),
558 to_mcq(init_attr->send_cq),
559 to_mcq(init_attr->recv_cq),
560 init_attr->qp_type, init_attr->sq_sig_type,
Roland Dreier80c8ec22005-07-07 17:57:20 -0700561 &init_attr->cap, qp);
562
563 if (err && pd->uobject) {
564 context = to_mucontext(pd->uobject->context);
565
566 mthca_unmap_user_db(to_mdev(pd->device),
567 &context->uar,
568 context->db_tab,
569 ucmd.sq_db_index);
570 mthca_unmap_user_db(to_mdev(pd->device),
571 &context->uar,
572 context->db_tab,
573 ucmd.rq_db_index);
574 }
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 qp->ibqp.qp_num = qp->qpn;
577 break;
578 }
579 case IB_QPT_SMI:
580 case IB_QPT_GSI:
581 {
Roland Dreier80c8ec22005-07-07 17:57:20 -0700582 /* Don't allow userspace to create special QPs */
583 if (pd->uobject)
584 return ERR_PTR(-EINVAL);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
587 if (!qp)
588 return ERR_PTR(-ENOMEM);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
591
592 err = mthca_alloc_sqp(to_mdev(pd->device), to_mpd(pd),
593 to_mcq(init_attr->send_cq),
594 to_mcq(init_attr->recv_cq),
Roland Dreier80c8ec22005-07-07 17:57:20 -0700595 init_attr->sq_sig_type, &init_attr->cap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 qp->ibqp.qp_num, init_attr->port_num,
597 to_msqp(qp));
598 break;
599 }
600 default:
601 /* Don't support raw QPs */
602 return ERR_PTR(-ENOSYS);
603 }
604
605 if (err) {
606 kfree(qp);
607 return ERR_PTR(err);
608 }
609
Roland Dreier80c8ec22005-07-07 17:57:20 -0700610 init_attr->cap.max_send_wr = qp->sq.max;
611 init_attr->cap.max_recv_wr = qp->rq.max;
612 init_attr->cap.max_send_sge = qp->sq.max_gs;
613 init_attr->cap.max_recv_sge = qp->rq.max_gs;
Jack Morgenstein77369ed2005-11-09 11:26:07 -0800614 init_attr->cap.max_inline_data = qp->max_inline_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return &qp->ibqp;
617}
618
619static int mthca_destroy_qp(struct ib_qp *qp)
620{
Roland Dreier80c8ec22005-07-07 17:57:20 -0700621 if (qp->uobject) {
622 mthca_unmap_user_db(to_mdev(qp->device),
623 &to_mucontext(qp->uobject->context)->uar,
624 to_mucontext(qp->uobject->context)->db_tab,
625 to_mqp(qp)->sq.db_index);
626 mthca_unmap_user_db(to_mdev(qp->device),
627 &to_mucontext(qp->uobject->context)->uar,
628 to_mucontext(qp->uobject->context)->db_tab,
629 to_mqp(qp)->rq.db_index);
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
632 kfree(qp);
633 return 0;
634}
635
Roland Dreier1cf296b2005-07-07 17:57:11 -0700636static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
637 struct ib_ucontext *context,
638 struct ib_udata *udata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Roland Dreier74c21742005-07-07 17:57:19 -0700640 struct mthca_create_cq ucmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct mthca_cq *cq;
642 int nent;
643 int err;
644
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700645 if (entries < 1 || entries > to_mdev(ibdev)->limits.max_cqes)
646 return ERR_PTR(-EINVAL);
647
Roland Dreier74c21742005-07-07 17:57:19 -0700648 if (context) {
649 if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
650 return ERR_PTR(-EFAULT);
651
652 err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
653 to_mucontext(context)->db_tab,
654 ucmd.set_db_index, ucmd.set_db_page);
655 if (err)
656 return ERR_PTR(err);
657
658 err = mthca_map_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
659 to_mucontext(context)->db_tab,
660 ucmd.arm_db_index, ucmd.arm_db_page);
661 if (err)
662 goto err_unmap_set;
663 }
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 cq = kmalloc(sizeof *cq, GFP_KERNEL);
Roland Dreier74c21742005-07-07 17:57:19 -0700666 if (!cq) {
667 err = -ENOMEM;
668 goto err_unmap_arm;
669 }
670
671 if (context) {
Roland Dreier4885bf62006-01-30 14:31:33 -0800672 cq->buf.mr.ibmr.lkey = ucmd.lkey;
673 cq->set_ci_db_index = ucmd.set_db_index;
674 cq->arm_db_index = ucmd.arm_db_index;
Roland Dreier74c21742005-07-07 17:57:19 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 for (nent = 1; nent <= entries; nent <<= 1)
678 ; /* nothing */
679
Roland Dreier74c21742005-07-07 17:57:19 -0700680 err = mthca_init_cq(to_mdev(ibdev), nent,
681 context ? to_mucontext(context) : NULL,
682 context ? ucmd.pdn : to_mdev(ibdev)->driver_pd.pd_num,
683 cq);
684 if (err)
685 goto err_free;
686
687 if (context && ib_copy_to_udata(udata, &cq->cqn, sizeof (__u32))) {
688 mthca_free_cq(to_mdev(ibdev), cq);
689 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Roland Dreier4885bf62006-01-30 14:31:33 -0800692 cq->resize_buf = NULL;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return &cq->ibcq;
Roland Dreier74c21742005-07-07 17:57:19 -0700695
696err_free:
697 kfree(cq);
698
699err_unmap_arm:
700 if (context)
701 mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
702 to_mucontext(context)->db_tab, ucmd.arm_db_index);
703
704err_unmap_set:
705 if (context)
706 mthca_unmap_user_db(to_mdev(ibdev), &to_mucontext(context)->uar,
707 to_mucontext(context)->db_tab, ucmd.set_db_index);
708
709 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Roland Dreier4885bf62006-01-30 14:31:33 -0800712static int mthca_alloc_resize_buf(struct mthca_dev *dev, struct mthca_cq *cq,
713 int entries)
714{
715 int ret;
716
717 spin_lock_irq(&cq->lock);
718 if (cq->resize_buf) {
719 ret = -EBUSY;
720 goto unlock;
721 }
722
723 cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_ATOMIC);
724 if (!cq->resize_buf) {
725 ret = -ENOMEM;
726 goto unlock;
727 }
728
729 cq->resize_buf->state = CQ_RESIZE_ALLOC;
730
731 ret = 0;
732
733unlock:
734 spin_unlock_irq(&cq->lock);
735
736 if (ret)
737 return ret;
738
739 ret = mthca_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
740 if (ret) {
741 spin_lock_irq(&cq->lock);
742 kfree(cq->resize_buf);
743 cq->resize_buf = NULL;
744 spin_unlock_irq(&cq->lock);
745 return ret;
746 }
747
748 cq->resize_buf->cqe = entries - 1;
749
750 spin_lock_irq(&cq->lock);
751 cq->resize_buf->state = CQ_RESIZE_READY;
752 spin_unlock_irq(&cq->lock);
753
754 return 0;
755}
756
757static int mthca_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
758{
759 struct mthca_dev *dev = to_mdev(ibcq->device);
760 struct mthca_cq *cq = to_mcq(ibcq);
761 struct mthca_resize_cq ucmd;
762 u32 lkey;
763 u8 status;
764 int ret;
765
766 if (entries < 1 || entries > dev->limits.max_cqes)
767 return -EINVAL;
768
769 entries = roundup_pow_of_two(entries + 1);
770 if (entries == ibcq->cqe + 1)
771 return 0;
772
773 if (cq->is_kernel) {
774 ret = mthca_alloc_resize_buf(dev, cq, entries);
775 if (ret)
776 return ret;
777 lkey = cq->resize_buf->buf.mr.ibmr.lkey;
778 } else {
779 if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
780 return -EFAULT;
781 lkey = ucmd.lkey;
782 }
783
784 ret = mthca_RESIZE_CQ(dev, cq->cqn, lkey, long_log2(entries), &status);
785 if (status)
786 ret = -EINVAL;
787
788 if (ret) {
789 if (cq->resize_buf) {
790 mthca_free_cq_buf(dev, &cq->resize_buf->buf,
791 cq->resize_buf->cqe);
792 kfree(cq->resize_buf);
793 spin_lock_irq(&cq->lock);
794 cq->resize_buf = NULL;
795 spin_unlock_irq(&cq->lock);
796 }
797 return ret;
798 }
799
800 if (cq->is_kernel) {
801 struct mthca_cq_buf tbuf;
802 int tcqe;
803
804 spin_lock_irq(&cq->lock);
805 if (cq->resize_buf->state == CQ_RESIZE_READY) {
806 mthca_cq_resize_copy_cqes(cq);
807 tbuf = cq->buf;
808 tcqe = cq->ibcq.cqe;
809 cq->buf = cq->resize_buf->buf;
810 cq->ibcq.cqe = cq->resize_buf->cqe;
811 } else {
812 tbuf = cq->resize_buf->buf;
813 tcqe = cq->resize_buf->cqe;
814 }
815
816 kfree(cq->resize_buf);
817 cq->resize_buf = NULL;
818 spin_unlock_irq(&cq->lock);
819
820 mthca_free_cq_buf(dev, &tbuf, tcqe);
821 } else
822 ibcq->cqe = entries - 1;
823
824 return 0;
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int mthca_destroy_cq(struct ib_cq *cq)
828{
Roland Dreier74c21742005-07-07 17:57:19 -0700829 if (cq->uobject) {
830 mthca_unmap_user_db(to_mdev(cq->device),
831 &to_mucontext(cq->uobject->context)->uar,
832 to_mucontext(cq->uobject->context)->db_tab,
833 to_mcq(cq)->arm_db_index);
834 mthca_unmap_user_db(to_mdev(cq->device),
835 &to_mucontext(cq->uobject->context)->uar,
836 to_mucontext(cq->uobject->context)->db_tab,
837 to_mcq(cq)->set_ci_db_index);
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
840 kfree(cq);
841
842 return 0;
843}
844
845static inline u32 convert_access(int acc)
846{
847 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MTHCA_MPT_FLAG_ATOMIC : 0) |
848 (acc & IB_ACCESS_REMOTE_WRITE ? MTHCA_MPT_FLAG_REMOTE_WRITE : 0) |
849 (acc & IB_ACCESS_REMOTE_READ ? MTHCA_MPT_FLAG_REMOTE_READ : 0) |
850 (acc & IB_ACCESS_LOCAL_WRITE ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0) |
851 MTHCA_MPT_FLAG_LOCAL_READ;
852}
853
854static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
855{
856 struct mthca_mr *mr;
857 int err;
858
859 mr = kmalloc(sizeof *mr, GFP_KERNEL);
860 if (!mr)
861 return ERR_PTR(-ENOMEM);
862
863 err = mthca_mr_alloc_notrans(to_mdev(pd->device),
864 to_mpd(pd)->pd_num,
865 convert_access(acc), mr);
866
867 if (err) {
868 kfree(mr);
869 return ERR_PTR(err);
870 }
871
872 return &mr->ibmr;
873}
874
875static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
876 struct ib_phys_buf *buffer_list,
877 int num_phys_buf,
878 int acc,
879 u64 *iova_start)
880{
881 struct mthca_mr *mr;
882 u64 *page_list;
883 u64 total_size;
884 u64 mask;
885 int shift;
886 int npages;
887 int err;
888 int i, j, n;
889
890 /* First check that we have enough alignment */
891 if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK))
892 return ERR_PTR(-EINVAL);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 mask = 0;
895 total_size = 0;
896 for (i = 0; i < num_phys_buf; ++i) {
Michael S. Tsirkin6627fa62006-01-09 13:50:57 -0800897 if (i != 0)
898 mask |= buffer_list[i].addr;
899 if (i != num_phys_buf - 1)
900 mask |= buffer_list[i].addr + buffer_list[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 total_size += buffer_list[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
Michael S. Tsirkin6627fa62006-01-09 13:50:57 -0800905 if (mask & ~PAGE_MASK)
906 return ERR_PTR(-EINVAL);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* Find largest page shift we can use to cover buffers */
909 for (shift = PAGE_SHIFT; shift < 31; ++shift)
910 if (num_phys_buf > 1) {
911 if ((1ULL << shift) & mask)
912 break;
913 } else {
914 if (1ULL << shift >=
915 buffer_list[0].size +
916 (buffer_list[0].addr & ((1ULL << shift) - 1)))
917 break;
918 }
919
920 buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
921 buffer_list[0].addr &= ~0ull << shift;
922
923 mr = kmalloc(sizeof *mr, GFP_KERNEL);
924 if (!mr)
925 return ERR_PTR(-ENOMEM);
926
927 npages = 0;
928 for (i = 0; i < num_phys_buf; ++i)
929 npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
930
931 if (!npages)
932 return &mr->ibmr;
933
934 page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
935 if (!page_list) {
936 kfree(mr);
937 return ERR_PTR(-ENOMEM);
938 }
939
940 n = 0;
941 for (i = 0; i < num_phys_buf; ++i)
942 for (j = 0;
943 j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
944 ++j)
945 page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
946
947 mthca_dbg(to_mdev(pd->device), "Registering memory at %llx (iova %llx) "
948 "in PD %x; shift %d, npages %d.\n",
949 (unsigned long long) buffer_list[0].addr,
950 (unsigned long long) *iova_start,
951 to_mpd(pd)->pd_num,
952 shift, npages);
953
954 err = mthca_mr_alloc_phys(to_mdev(pd->device),
955 to_mpd(pd)->pd_num,
956 page_list, shift, npages,
957 *iova_start, total_size,
958 convert_access(acc), mr);
959
960 if (err) {
Roland Dreier761f9eb2005-06-27 14:36:44 -0700961 kfree(page_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 kfree(mr);
963 return ERR_PTR(err);
964 }
965
966 kfree(page_list);
967 return &mr->ibmr;
968}
969
Roland Dreier24d42812005-07-07 17:57:19 -0700970static struct ib_mr *mthca_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
971 int acc, struct ib_udata *udata)
972{
973 struct mthca_dev *dev = to_mdev(pd->device);
974 struct ib_umem_chunk *chunk;
975 struct mthca_mr *mr;
976 u64 *pages;
977 int shift, n, len;
978 int i, j, k;
979 int err = 0;
980
981 shift = ffs(region->page_size) - 1;
982
983 mr = kmalloc(sizeof *mr, GFP_KERNEL);
984 if (!mr)
985 return ERR_PTR(-ENOMEM);
986
987 n = 0;
988 list_for_each_entry(chunk, &region->chunk_list, list)
989 n += chunk->nents;
990
991 mr->mtt = mthca_alloc_mtt(dev, n);
992 if (IS_ERR(mr->mtt)) {
993 err = PTR_ERR(mr->mtt);
994 goto err;
995 }
996
997 pages = (u64 *) __get_free_page(GFP_KERNEL);
998 if (!pages) {
999 err = -ENOMEM;
1000 goto err_mtt;
1001 }
1002
1003 i = n = 0;
1004
1005 list_for_each_entry(chunk, &region->chunk_list, list)
1006 for (j = 0; j < chunk->nmap; ++j) {
1007 len = sg_dma_len(&chunk->page_list[j]) >> shift;
1008 for (k = 0; k < len; ++k) {
1009 pages[i++] = sg_dma_address(&chunk->page_list[j]) +
1010 region->page_size * k;
1011 /*
1012 * Be friendly to WRITE_MTT command
1013 * and leave two empty slots for the
1014 * index and reserved fields of the
1015 * mailbox.
1016 */
1017 if (i == PAGE_SIZE / sizeof (u64) - 2) {
1018 err = mthca_write_mtt(dev, mr->mtt,
1019 n, pages, i);
1020 if (err)
1021 goto mtt_done;
1022 n += i;
1023 i = 0;
1024 }
1025 }
1026 }
1027
1028 if (i)
1029 err = mthca_write_mtt(dev, mr->mtt, n, pages, i);
1030mtt_done:
1031 free_page((unsigned long) pages);
1032 if (err)
1033 goto err_mtt;
1034
1035 err = mthca_mr_alloc(dev, to_mpd(pd)->pd_num, shift, region->virt_base,
1036 region->length, convert_access(acc), mr);
1037
1038 if (err)
1039 goto err_mtt;
1040
1041 return &mr->ibmr;
1042
1043err_mtt:
1044 mthca_free_mtt(dev, mr->mtt);
1045
1046err:
1047 kfree(mr);
1048 return ERR_PTR(err);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051static int mthca_dereg_mr(struct ib_mr *mr)
1052{
Roland Dreiere464b2a2005-04-16 15:26:17 -07001053 struct mthca_mr *mmr = to_mmr(mr);
1054 mthca_free_mr(to_mdev(mr->device), mmr);
1055 kfree(mmr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return 0;
1057}
1058
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -07001059static struct ib_fmr *mthca_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
1060 struct ib_fmr_attr *fmr_attr)
1061{
1062 struct mthca_fmr *fmr;
1063 int err;
1064
1065 fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
1066 if (!fmr)
1067 return ERR_PTR(-ENOMEM);
1068
1069 memcpy(&fmr->attr, fmr_attr, sizeof *fmr_attr);
1070 err = mthca_fmr_alloc(to_mdev(pd->device), to_mpd(pd)->pd_num,
1071 convert_access(mr_access_flags), fmr);
1072
1073 if (err) {
1074 kfree(fmr);
1075 return ERR_PTR(err);
1076 }
1077
1078 return &fmr->ibmr;
1079}
1080
1081static int mthca_dealloc_fmr(struct ib_fmr *fmr)
1082{
1083 struct mthca_fmr *mfmr = to_mfmr(fmr);
1084 int err;
1085
1086 err = mthca_free_fmr(to_mdev(fmr->device), mfmr);
1087 if (err)
1088 return err;
1089
1090 kfree(mfmr);
1091 return 0;
1092}
1093
1094static int mthca_unmap_fmr(struct list_head *fmr_list)
1095{
1096 struct ib_fmr *fmr;
1097 int err;
1098 u8 status;
1099 struct mthca_dev *mdev = NULL;
1100
1101 list_for_each_entry(fmr, fmr_list, list) {
1102 if (mdev && to_mdev(fmr->device) != mdev)
1103 return -EINVAL;
1104 mdev = to_mdev(fmr->device);
1105 }
1106
1107 if (!mdev)
1108 return 0;
1109
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001110 if (mthca_is_memfree(mdev)) {
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -07001111 list_for_each_entry(fmr, fmr_list, list)
1112 mthca_arbel_fmr_unmap(mdev, to_mfmr(fmr));
1113
1114 wmb();
1115 } else
1116 list_for_each_entry(fmr, fmr_list, list)
1117 mthca_tavor_fmr_unmap(mdev, to_mfmr(fmr));
1118
1119 err = mthca_SYNC_TPT(mdev, &status);
1120 if (err)
1121 return err;
1122 if (status)
1123 return -EINVAL;
1124 return 0;
1125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127static ssize_t show_rev(struct class_device *cdev, char *buf)
1128{
1129 struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
1130 return sprintf(buf, "%x\n", dev->rev_id);
1131}
1132
1133static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
1134{
1135 struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
Roland Dreier87cfe322005-11-02 22:59:37 -08001136 return sprintf(buf, "%d.%d.%d\n", (int) (dev->fw_ver >> 32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 (int) (dev->fw_ver >> 16) & 0xffff,
1138 (int) dev->fw_ver & 0xffff);
1139}
1140
1141static ssize_t show_hca(struct class_device *cdev, char *buf)
1142{
1143 struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
Roland Dreier68a3c212005-04-16 15:26:34 -07001144 switch (dev->pdev->device) {
1145 case PCI_DEVICE_ID_MELLANOX_TAVOR:
1146 return sprintf(buf, "MT23108\n");
1147 case PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT:
1148 return sprintf(buf, "MT25208 (MT23108 compat mode)\n");
1149 case PCI_DEVICE_ID_MELLANOX_ARBEL:
1150 return sprintf(buf, "MT25208\n");
1151 case PCI_DEVICE_ID_MELLANOX_SINAI:
1152 case PCI_DEVICE_ID_MELLANOX_SINAI_OLD:
1153 return sprintf(buf, "MT25204\n");
1154 default:
1155 return sprintf(buf, "unknown\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157}
1158
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -07001159static ssize_t show_board(struct class_device *cdev, char *buf)
1160{
1161 struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
1162 return sprintf(buf, "%.*s\n", MTHCA_BOARD_ID_LEN, dev->board_id);
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1166static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
1167static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -07001168static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170static struct class_device_attribute *mthca_class_attributes[] = {
1171 &class_device_attr_hw_rev,
1172 &class_device_attr_fw_ver,
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -07001173 &class_device_attr_hca_type,
1174 &class_device_attr_board_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175};
1176
Sean Heftycf311cd2006-01-10 07:39:34 -08001177static int mthca_init_node_data(struct mthca_dev *dev)
1178{
1179 struct ib_smp *in_mad = NULL;
1180 struct ib_smp *out_mad = NULL;
1181 int err = -ENOMEM;
1182 u8 status;
1183
1184 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
1185 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
1186 if (!in_mad || !out_mad)
1187 goto out;
1188
1189 init_query_mad(in_mad);
1190 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
1191
1192 err = mthca_MAD_IFC(dev, 1, 1,
1193 1, NULL, NULL, in_mad, out_mad,
1194 &status);
1195 if (err)
1196 goto out;
1197 if (status) {
1198 err = -EINVAL;
1199 goto out;
1200 }
1201
1202 memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
1203
1204out:
1205 kfree(in_mad);
1206 kfree(out_mad);
1207 return err;
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210int mthca_register_device(struct mthca_dev *dev)
1211{
1212 int ret;
1213 int i;
1214
Sean Heftycf311cd2006-01-10 07:39:34 -08001215 ret = mthca_init_node_data(dev);
1216 if (ret)
1217 return ret;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 strlcpy(dev->ib_dev.name, "mthca%d", IB_DEVICE_NAME_MAX);
Roland Dreier1cf296b2005-07-07 17:57:11 -07001220 dev->ib_dev.owner = THIS_MODULE;
1221
Roland Dreier274c0892005-09-29 14:17:48 -07001222 dev->ib_dev.uverbs_abi_ver = MTHCA_UVERBS_ABI_VERSION;
Roland Dreier883a99c2005-10-14 14:00:58 -07001223 dev->ib_dev.uverbs_cmd_mask =
1224 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1225 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1226 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1227 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1228 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1229 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1230 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1231 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1232 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
Roland Dreier4885bf62006-01-30 14:31:33 -08001233 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
Roland Dreier883a99c2005-10-14 14:00:58 -07001234 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1235 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1236 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
1237 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1238 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1239 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1240 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1241 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
1242 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 dev->ib_dev.node_type = IB_NODE_CA;
1244 dev->ib_dev.phys_port_cnt = dev->limits.num_ports;
1245 dev->ib_dev.dma_device = &dev->pdev->dev;
1246 dev->ib_dev.class_dev.dev = &dev->pdev->dev;
1247 dev->ib_dev.query_device = mthca_query_device;
1248 dev->ib_dev.query_port = mthca_query_port;
1249 dev->ib_dev.modify_port = mthca_modify_port;
1250 dev->ib_dev.query_pkey = mthca_query_pkey;
1251 dev->ib_dev.query_gid = mthca_query_gid;
Roland Dreier5e0b5372005-07-07 17:57:16 -07001252 dev->ib_dev.alloc_ucontext = mthca_alloc_ucontext;
1253 dev->ib_dev.dealloc_ucontext = mthca_dealloc_ucontext;
Roland Dreier53b8b3f2005-07-07 17:57:17 -07001254 dev->ib_dev.mmap = mthca_mmap_uar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 dev->ib_dev.alloc_pd = mthca_alloc_pd;
1256 dev->ib_dev.dealloc_pd = mthca_dealloc_pd;
1257 dev->ib_dev.create_ah = mthca_ah_create;
1258 dev->ib_dev.destroy_ah = mthca_ah_destroy;
Roland Dreierec34a922005-08-19 10:59:31 -07001259
1260 if (dev->mthca_flags & MTHCA_FLAG_SRQ) {
1261 dev->ib_dev.create_srq = mthca_create_srq;
Roland Dreier90f104d2005-10-06 13:15:56 -07001262 dev->ib_dev.modify_srq = mthca_modify_srq;
Roland Dreierec34a922005-08-19 10:59:31 -07001263 dev->ib_dev.destroy_srq = mthca_destroy_srq;
1264
1265 if (mthca_is_memfree(dev))
1266 dev->ib_dev.post_srq_recv = mthca_arbel_post_srq_recv;
1267 else
1268 dev->ib_dev.post_srq_recv = mthca_tavor_post_srq_recv;
1269 }
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 dev->ib_dev.create_qp = mthca_create_qp;
1272 dev->ib_dev.modify_qp = mthca_modify_qp;
1273 dev->ib_dev.destroy_qp = mthca_destroy_qp;
1274 dev->ib_dev.create_cq = mthca_create_cq;
Roland Dreier4885bf62006-01-30 14:31:33 -08001275 dev->ib_dev.resize_cq = mthca_resize_cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 dev->ib_dev.destroy_cq = mthca_destroy_cq;
1277 dev->ib_dev.poll_cq = mthca_poll_cq;
1278 dev->ib_dev.get_dma_mr = mthca_get_dma_mr;
1279 dev->ib_dev.reg_phys_mr = mthca_reg_phys_mr;
Roland Dreier24d42812005-07-07 17:57:19 -07001280 dev->ib_dev.reg_user_mr = mthca_reg_user_mr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 dev->ib_dev.dereg_mr = mthca_dereg_mr;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -07001282
1283 if (dev->mthca_flags & MTHCA_FLAG_FMR) {
1284 dev->ib_dev.alloc_fmr = mthca_alloc_fmr;
1285 dev->ib_dev.unmap_fmr = mthca_unmap_fmr;
1286 dev->ib_dev.dealloc_fmr = mthca_dealloc_fmr;
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001287 if (mthca_is_memfree(dev))
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -07001288 dev->ib_dev.map_phys_fmr = mthca_arbel_map_phys_fmr;
1289 else
1290 dev->ib_dev.map_phys_fmr = mthca_tavor_map_phys_fmr;
1291 }
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 dev->ib_dev.attach_mcast = mthca_multicast_attach;
1294 dev->ib_dev.detach_mcast = mthca_multicast_detach;
1295 dev->ib_dev.process_mad = mthca_process_mad;
1296
Roland Dreierd10ddbf2005-04-16 15:26:32 -07001297 if (mthca_is_memfree(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 dev->ib_dev.req_notify_cq = mthca_arbel_arm_cq;
1299 dev->ib_dev.post_send = mthca_arbel_post_send;
1300 dev->ib_dev.post_recv = mthca_arbel_post_receive;
1301 } else {
1302 dev->ib_dev.req_notify_cq = mthca_tavor_arm_cq;
1303 dev->ib_dev.post_send = mthca_tavor_post_send;
1304 dev->ib_dev.post_recv = mthca_tavor_post_receive;
1305 }
1306
Roland Dreierfd9cfdd2006-01-30 16:45:11 -08001307 mutex_init(&dev->cap_mask_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 ret = ib_register_device(&dev->ib_dev);
1310 if (ret)
1311 return ret;
1312
1313 for (i = 0; i < ARRAY_SIZE(mthca_class_attributes); ++i) {
1314 ret = class_device_create_file(&dev->ib_dev.class_dev,
1315 mthca_class_attributes[i]);
1316 if (ret) {
1317 ib_unregister_device(&dev->ib_dev);
1318 return ret;
1319 }
1320 }
1321
Roland Dreier3d155f82005-10-27 11:03:38 -07001322 mthca_start_catas_poll(dev);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
1327void mthca_unregister_device(struct mthca_dev *dev)
1328{
Roland Dreier3d155f82005-10-27 11:03:38 -07001329 mthca_stop_catas_poll(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ib_unregister_device(&dev->ib_dev);
1331}