2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
32 * $Id: mthca_provider.c 1397 2004-12-28 05:09:00Z roland $
37 #include "mthca_dev.h"
38 #include "mthca_cmd.h"
40 static int mthca_query_device(struct ib_device *ibdev,
41 struct ib_device_attr *props)
43 struct ib_smp *in_mad = NULL;
44 struct ib_smp *out_mad = NULL;
48 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
49 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
50 if (!in_mad || !out_mad)
53 props->fw_ver = to_mdev(ibdev)->fw_ver;
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;
62 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
63 1, NULL, NULL, in_mad, out_mad,
72 props->vendor_id = be32_to_cpup((u32 *) (out_mad->data + 36)) &
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);
86 static int mthca_query_port(struct ib_device *ibdev,
87 u8 port, struct ib_port_attr *props)
89 struct ib_smp *in_mad = NULL;
90 struct ib_smp *out_mad = NULL;
94 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
95 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
96 if (!in_mad || !out_mad)
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);
107 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
108 port, NULL, NULL, in_mad, out_mad,
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;
136 static int mthca_modify_port(struct ib_device *ibdev,
137 u8 port, int port_modify_mask,
138 struct ib_port_modify *props)
140 struct mthca_set_ib_param set_ib;
141 struct ib_port_attr attr;
145 if (down_interruptible(&to_mdev(ibdev)->cap_mask_mutex))
148 err = mthca_query_port(ibdev, port, &attr);
152 set_ib.set_si_guid = 0;
153 set_ib.reset_qkey_viol = !!(port_modify_mask & IB_PORT_RESET_QKEY_CNTR);
155 set_ib.cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
156 ~props->clr_port_cap_mask;
158 err = mthca_SET_IB(to_mdev(ibdev), &set_ib, port, &status);
167 up(&to_mdev(ibdev)->cap_mask_mutex);
171 static int mthca_query_pkey(struct ib_device *ibdev,
172 u8 port, u16 index, u16 *pkey)
174 struct ib_smp *in_mad = NULL;
175 struct ib_smp *out_mad = NULL;
179 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
180 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
181 if (!in_mad || !out_mad)
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);
192 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
193 port, NULL, NULL, in_mad, out_mad,
202 *pkey = be16_to_cpu(((u16 *) out_mad->data)[index % 32]);
210 static int mthca_query_gid(struct ib_device *ibdev, u8 port,
211 int index, union ib_gid *gid)
213 struct ib_smp *in_mad = NULL;
214 struct ib_smp *out_mad = NULL;
218 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
219 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
220 if (!in_mad || !out_mad)
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);
231 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
232 port, NULL, NULL, in_mad, out_mad,
241 memcpy(gid->raw, out_mad->data + 8, 8);
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);
251 err = mthca_MAD_IFC(to_mdev(ibdev), 1, 1,
252 port, NULL, NULL, in_mad, out_mad,
261 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 16, 8);
269 static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev)
274 pd = kmalloc(sizeof *pd, GFP_KERNEL);
276 return ERR_PTR(-ENOMEM);
278 err = mthca_pd_alloc(to_mdev(ibdev), pd);
287 static int mthca_dealloc_pd(struct ib_pd *pd)
289 mthca_pd_free(to_mdev(pd->device), to_mpd(pd));
295 static struct ib_ah *mthca_ah_create(struct ib_pd *pd,
296 struct ib_ah_attr *ah_attr)
301 ah = kmalloc(sizeof *ah, GFP_KERNEL);
303 return ERR_PTR(-ENOMEM);
305 err = mthca_create_ah(to_mdev(pd->device), to_mpd(pd), ah_attr, ah);
314 static int mthca_ah_destroy(struct ib_ah *ah)
316 mthca_destroy_ah(to_mdev(ah->device), to_mah(ah));
322 static struct ib_qp *mthca_create_qp(struct ib_pd *pd,
323 struct ib_qp_init_attr *init_attr)
328 switch (init_attr->qp_type) {
333 qp = kmalloc(sizeof *qp, GFP_KERNEL);
335 return ERR_PTR(-ENOMEM);
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;
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;
353 qp = kmalloc(sizeof (struct mthca_sqp), GFP_KERNEL);
355 return ERR_PTR(-ENOMEM);
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;
362 qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
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,
373 /* Don't support raw QPs */
374 return ERR_PTR(-ENOSYS);
382 init_attr->cap.max_inline_data = 0;
387 static int mthca_destroy_qp(struct ib_qp *qp)
389 mthca_free_qp(to_mdev(qp->device), to_mqp(qp));
394 static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries)
400 cq = kmalloc(sizeof *cq, GFP_KERNEL);
402 return ERR_PTR(-ENOMEM);
404 for (nent = 1; nent <= entries; nent <<= 1)
407 err = mthca_init_cq(to_mdev(ibdev), nent, cq);
412 cq->ibcq.cqe = nent - 1;
417 static int mthca_destroy_cq(struct ib_cq *cq)
419 mthca_free_cq(to_mdev(cq->device), to_mcq(cq));
425 static int mthca_req_notify_cq(struct ib_cq *cq, enum ib_cq_notify notify)
427 mthca_arm_cq(to_mdev(cq->device), to_mcq(cq),
428 notify == IB_CQ_SOLICITED);
432 static inline u32 convert_access(int acc)
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;
441 static struct ib_mr *mthca_get_dma_mr(struct ib_pd *pd, int acc)
446 mr = kmalloc(sizeof *mr, GFP_KERNEL);
448 return ERR_PTR(-ENOMEM);
450 err = mthca_mr_alloc_notrans(to_mdev(pd->device),
452 convert_access(acc), mr);
462 static struct ib_mr *mthca_reg_phys_mr(struct ib_pd *pd,
463 struct ib_phys_buf *buffer_list,
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);
481 if (num_phys_buf > 1 &&
482 ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK))
483 return ERR_PTR(-EINVAL);
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);
494 total_size += buffer_list[i].size;
496 mask |= buffer_list[i].addr;
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)
506 buffer_list[0].size +
507 (buffer_list[0].addr & ((1ULL << shift) - 1)))
511 buffer_list[0].size += buffer_list[0].addr & ((1ULL << shift) - 1);
512 buffer_list[0].addr &= ~0ull << shift;
514 mr = kmalloc(sizeof *mr, GFP_KERNEL);
516 return ERR_PTR(-ENOMEM);
519 for (i = 0; i < num_phys_buf; ++i)
520 npages += (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
525 page_list = kmalloc(npages * sizeof *page_list, GFP_KERNEL);
528 return ERR_PTR(-ENOMEM);
532 for (i = 0; i < num_phys_buf; ++i)
534 j < (buffer_list[i].size + (1ULL << shift) - 1) >> shift;
536 page_list[n++] = buffer_list[i].addr + ((u64) j << shift);
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,
545 err = mthca_mr_alloc_phys(to_mdev(pd->device),
547 page_list, shift, npages,
548 *iova_start, total_size,
549 convert_access(acc), mr);
560 static int mthca_dereg_mr(struct ib_mr *mr)
562 mthca_free_mr(to_mdev(mr->device), to_mmr(mr));
567 static ssize_t show_rev(struct class_device *cdev, char *buf)
569 struct mthca_dev *dev = container_of(cdev, struct mthca_dev, ib_dev.class_dev);
570 return sprintf(buf, "%x\n", dev->rev_id);
573 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
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);
581 static ssize_t show_hca(struct class_device *cdev, char *buf)
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");
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);
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
602 int mthca_register_device(struct mthca_dev *dev)
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;
637 init_MUTEX(&dev->cap_mask_mutex);
639 ret = ib_register_device(&dev->ib_dev);
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]);
647 ib_unregister_device(&dev->ib_dev);
655 void mthca_unregister_device(struct mthca_dev *dev)
657 ib_unregister_device(&dev->ib_dev);