Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw/async_tx
[pandora-kernel.git] / drivers / infiniband / hw / cxgb4 / provider.c
1 /*
2  * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/device.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/delay.h>
38 #include <linux/errno.h>
39 #include <linux/list.h>
40 #include <linux/spinlock.h>
41 #include <linux/ethtool.h>
42 #include <linux/rtnetlink.h>
43 #include <linux/inetdevice.h>
44 #include <linux/io.h>
45
46 #include <asm/irq.h>
47 #include <asm/byteorder.h>
48
49 #include <rdma/iw_cm.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/ib_smi.h>
52 #include <rdma/ib_umem.h>
53 #include <rdma/ib_user_verbs.h>
54
55 #include "iw_cxgb4.h"
56
57 static int fastreg_support;
58 module_param(fastreg_support, int, 0644);
59 MODULE_PARM_DESC(fastreg_support, "Advertise fastreg support (default=0)");
60
61 static int c4iw_modify_port(struct ib_device *ibdev,
62                             u8 port, int port_modify_mask,
63                             struct ib_port_modify *props)
64 {
65         return -ENOSYS;
66 }
67
68 static struct ib_ah *c4iw_ah_create(struct ib_pd *pd,
69                                     struct ib_ah_attr *ah_attr)
70 {
71         return ERR_PTR(-ENOSYS);
72 }
73
74 static int c4iw_ah_destroy(struct ib_ah *ah)
75 {
76         return -ENOSYS;
77 }
78
79 static int c4iw_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
80 {
81         return -ENOSYS;
82 }
83
84 static int c4iw_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
85 {
86         return -ENOSYS;
87 }
88
89 static int c4iw_process_mad(struct ib_device *ibdev, int mad_flags,
90                             u8 port_num, struct ib_wc *in_wc,
91                             struct ib_grh *in_grh, struct ib_mad *in_mad,
92                             struct ib_mad *out_mad)
93 {
94         return -ENOSYS;
95 }
96
97 static int c4iw_dealloc_ucontext(struct ib_ucontext *context)
98 {
99         struct c4iw_dev *rhp = to_c4iw_dev(context->device);
100         struct c4iw_ucontext *ucontext = to_c4iw_ucontext(context);
101         struct c4iw_mm_entry *mm, *tmp;
102
103         PDBG("%s context %p\n", __func__, context);
104         list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)
105                 kfree(mm);
106         c4iw_release_dev_ucontext(&rhp->rdev, &ucontext->uctx);
107         kfree(ucontext);
108         return 0;
109 }
110
111 static struct ib_ucontext *c4iw_alloc_ucontext(struct ib_device *ibdev,
112                                                struct ib_udata *udata)
113 {
114         struct c4iw_ucontext *context;
115         struct c4iw_dev *rhp = to_c4iw_dev(ibdev);
116
117         PDBG("%s ibdev %p\n", __func__, ibdev);
118         context = kzalloc(sizeof(*context), GFP_KERNEL);
119         if (!context)
120                 return ERR_PTR(-ENOMEM);
121         c4iw_init_dev_ucontext(&rhp->rdev, &context->uctx);
122         INIT_LIST_HEAD(&context->mmaps);
123         spin_lock_init(&context->mmap_lock);
124         return &context->ibucontext;
125 }
126
127 static int c4iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
128 {
129         int len = vma->vm_end - vma->vm_start;
130         u32 key = vma->vm_pgoff << PAGE_SHIFT;
131         struct c4iw_rdev *rdev;
132         int ret = 0;
133         struct c4iw_mm_entry *mm;
134         struct c4iw_ucontext *ucontext;
135         u64 addr;
136
137         PDBG("%s pgoff 0x%lx key 0x%x len %d\n", __func__, vma->vm_pgoff,
138              key, len);
139
140         if (vma->vm_start & (PAGE_SIZE-1))
141                 return -EINVAL;
142
143         rdev = &(to_c4iw_dev(context->device)->rdev);
144         ucontext = to_c4iw_ucontext(context);
145
146         mm = remove_mmap(ucontext, key, len);
147         if (!mm)
148                 return -EINVAL;
149         addr = mm->addr;
150         kfree(mm);
151
152         if ((addr >= pci_resource_start(rdev->lldi.pdev, 2)) &&
153             (addr < (pci_resource_start(rdev->lldi.pdev, 2) +
154                        pci_resource_len(rdev->lldi.pdev, 2)))) {
155
156                 /*
157                  * Map T4 DB register.
158                  */
159                 if (vma->vm_flags & VM_READ)
160                         return -EPERM;
161
162                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
163                 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
164                 vma->vm_flags &= ~VM_MAYREAD;
165                 ret = io_remap_pfn_range(vma, vma->vm_start,
166                                          addr >> PAGE_SHIFT,
167                                          len, vma->vm_page_prot);
168         } else {
169
170                 /*
171                  * Map WQ or CQ contig dma memory...
172                  */
173                 ret = remap_pfn_range(vma, vma->vm_start,
174                                       addr >> PAGE_SHIFT,
175                                       len, vma->vm_page_prot);
176         }
177
178         return ret;
179 }
180
181 static int c4iw_deallocate_pd(struct ib_pd *pd)
182 {
183         struct c4iw_dev *rhp;
184         struct c4iw_pd *php;
185
186         php = to_c4iw_pd(pd);
187         rhp = php->rhp;
188         PDBG("%s ibpd %p pdid 0x%x\n", __func__, pd, php->pdid);
189         c4iw_put_resource(&rhp->rdev.resource.pdid_fifo, php->pdid,
190                           &rhp->rdev.resource.pdid_fifo_lock);
191         kfree(php);
192         return 0;
193 }
194
195 static struct ib_pd *c4iw_allocate_pd(struct ib_device *ibdev,
196                                       struct ib_ucontext *context,
197                                       struct ib_udata *udata)
198 {
199         struct c4iw_pd *php;
200         u32 pdid;
201         struct c4iw_dev *rhp;
202
203         PDBG("%s ibdev %p\n", __func__, ibdev);
204         rhp = (struct c4iw_dev *) ibdev;
205         pdid =  c4iw_get_resource(&rhp->rdev.resource.pdid_fifo,
206                                   &rhp->rdev.resource.pdid_fifo_lock);
207         if (!pdid)
208                 return ERR_PTR(-EINVAL);
209         php = kzalloc(sizeof(*php), GFP_KERNEL);
210         if (!php) {
211                 c4iw_put_resource(&rhp->rdev.resource.pdid_fifo, pdid,
212                                   &rhp->rdev.resource.pdid_fifo_lock);
213                 return ERR_PTR(-ENOMEM);
214         }
215         php->pdid = pdid;
216         php->rhp = rhp;
217         if (context) {
218                 if (ib_copy_to_udata(udata, &php->pdid, sizeof(u32))) {
219                         c4iw_deallocate_pd(&php->ibpd);
220                         return ERR_PTR(-EFAULT);
221                 }
222         }
223         PDBG("%s pdid 0x%0x ptr 0x%p\n", __func__, pdid, php);
224         return &php->ibpd;
225 }
226
227 static int c4iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
228                            u16 *pkey)
229 {
230         PDBG("%s ibdev %p\n", __func__, ibdev);
231         *pkey = 0;
232         return 0;
233 }
234
235 static int c4iw_query_gid(struct ib_device *ibdev, u8 port, int index,
236                           union ib_gid *gid)
237 {
238         struct c4iw_dev *dev;
239
240         PDBG("%s ibdev %p, port %d, index %d, gid %p\n",
241                __func__, ibdev, port, index, gid);
242         dev = to_c4iw_dev(ibdev);
243         BUG_ON(port == 0);
244         memset(&(gid->raw[0]), 0, sizeof(gid->raw));
245         memcpy(&(gid->raw[0]), dev->rdev.lldi.ports[port-1]->dev_addr, 6);
246         return 0;
247 }
248
249 static int c4iw_query_device(struct ib_device *ibdev,
250                              struct ib_device_attr *props)
251 {
252
253         struct c4iw_dev *dev;
254         PDBG("%s ibdev %p\n", __func__, ibdev);
255
256         dev = to_c4iw_dev(ibdev);
257         memset(props, 0, sizeof *props);
258         memcpy(&props->sys_image_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
259         props->hw_ver = dev->rdev.lldi.adapter_type;
260         props->fw_ver = dev->rdev.lldi.fw_vers;
261         props->device_cap_flags = dev->device_cap_flags;
262         props->page_size_cap = T4_PAGESIZE_MASK;
263         props->vendor_id = (u32)dev->rdev.lldi.pdev->vendor;
264         props->vendor_part_id = (u32)dev->rdev.lldi.pdev->device;
265         props->max_mr_size = T4_MAX_MR_SIZE;
266         props->max_qp = T4_MAX_NUM_QP;
267         props->max_qp_wr = T4_MAX_QP_DEPTH;
268         props->max_sge = T4_MAX_RECV_SGE;
269         props->max_sge_rd = 1;
270         props->max_qp_rd_atom = c4iw_max_read_depth;
271         props->max_qp_init_rd_atom = c4iw_max_read_depth;
272         props->max_cq = T4_MAX_NUM_CQ;
273         props->max_cqe = T4_MAX_CQ_DEPTH;
274         props->max_mr = c4iw_num_stags(&dev->rdev);
275         props->max_pd = T4_MAX_NUM_PD;
276         props->local_ca_ack_delay = 0;
277         props->max_fast_reg_page_list_len = T4_MAX_FR_DEPTH;
278
279         return 0;
280 }
281
282 static int c4iw_query_port(struct ib_device *ibdev, u8 port,
283                            struct ib_port_attr *props)
284 {
285         struct c4iw_dev *dev;
286         struct net_device *netdev;
287         struct in_device *inetdev;
288
289         PDBG("%s ibdev %p\n", __func__, ibdev);
290
291         dev = to_c4iw_dev(ibdev);
292         netdev = dev->rdev.lldi.ports[port-1];
293
294         memset(props, 0, sizeof(struct ib_port_attr));
295         props->max_mtu = IB_MTU_4096;
296         if (netdev->mtu >= 4096)
297                 props->active_mtu = IB_MTU_4096;
298         else if (netdev->mtu >= 2048)
299                 props->active_mtu = IB_MTU_2048;
300         else if (netdev->mtu >= 1024)
301                 props->active_mtu = IB_MTU_1024;
302         else if (netdev->mtu >= 512)
303                 props->active_mtu = IB_MTU_512;
304         else
305                 props->active_mtu = IB_MTU_256;
306
307         if (!netif_carrier_ok(netdev))
308                 props->state = IB_PORT_DOWN;
309         else {
310                 inetdev = in_dev_get(netdev);
311                 if (inetdev) {
312                         if (inetdev->ifa_list)
313                                 props->state = IB_PORT_ACTIVE;
314                         else
315                                 props->state = IB_PORT_INIT;
316                         in_dev_put(inetdev);
317                 } else
318                         props->state = IB_PORT_INIT;
319         }
320
321         props->port_cap_flags =
322             IB_PORT_CM_SUP |
323             IB_PORT_SNMP_TUNNEL_SUP |
324             IB_PORT_REINIT_SUP |
325             IB_PORT_DEVICE_MGMT_SUP |
326             IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
327         props->gid_tbl_len = 1;
328         props->pkey_tbl_len = 1;
329         props->active_width = 2;
330         props->active_speed = 2;
331         props->max_msg_sz = -1;
332
333         return 0;
334 }
335
336 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
337                         char *buf)
338 {
339         struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
340                                                  ibdev.dev);
341         PDBG("%s dev 0x%p\n", __func__, dev);
342         return sprintf(buf, "%d\n", c4iw_dev->rdev.lldi.adapter_type);
343 }
344
345 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
346                            char *buf)
347 {
348         struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
349                                                  ibdev.dev);
350         PDBG("%s dev 0x%p\n", __func__, dev);
351
352         return sprintf(buf, "%u.%u.%u.%u\n",
353                         FW_HDR_FW_VER_MAJOR_GET(c4iw_dev->rdev.lldi.fw_vers),
354                         FW_HDR_FW_VER_MINOR_GET(c4iw_dev->rdev.lldi.fw_vers),
355                         FW_HDR_FW_VER_MICRO_GET(c4iw_dev->rdev.lldi.fw_vers),
356                         FW_HDR_FW_VER_BUILD_GET(c4iw_dev->rdev.lldi.fw_vers));
357 }
358
359 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
360                         char *buf)
361 {
362         struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
363                                                  ibdev.dev);
364         struct ethtool_drvinfo info;
365         struct net_device *lldev = c4iw_dev->rdev.lldi.ports[0];
366
367         PDBG("%s dev 0x%p\n", __func__, dev);
368         lldev->ethtool_ops->get_drvinfo(lldev, &info);
369         return sprintf(buf, "%s\n", info.driver);
370 }
371
372 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
373                           char *buf)
374 {
375         struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
376                                                  ibdev.dev);
377         PDBG("%s dev 0x%p\n", __func__, dev);
378         return sprintf(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
379                        c4iw_dev->rdev.lldi.pdev->device);
380 }
381
382 static int c4iw_get_mib(struct ib_device *ibdev,
383                         union rdma_protocol_stats *stats)
384 {
385         return -ENOSYS;
386 }
387
388 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
389 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
390 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
391 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
392
393 static struct device_attribute *c4iw_class_attributes[] = {
394         &dev_attr_hw_rev,
395         &dev_attr_fw_ver,
396         &dev_attr_hca_type,
397         &dev_attr_board_id,
398 };
399
400 int c4iw_register_device(struct c4iw_dev *dev)
401 {
402         int ret;
403         int i;
404
405         PDBG("%s c4iw_dev %p\n", __func__, dev);
406         BUG_ON(!dev->rdev.lldi.ports[0]);
407         strlcpy(dev->ibdev.name, "cxgb4_%d", IB_DEVICE_NAME_MAX);
408         memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
409         memcpy(&dev->ibdev.node_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
410         dev->ibdev.owner = THIS_MODULE;
411         dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY | IB_DEVICE_MEM_WINDOW;
412         if (fastreg_support)
413                 dev->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
414         dev->ibdev.local_dma_lkey = 0;
415         dev->ibdev.uverbs_cmd_mask =
416             (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
417             (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
418             (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
419             (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
420             (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
421             (1ull << IB_USER_VERBS_CMD_REG_MR) |
422             (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
423             (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
424             (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
425             (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
426             (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
427             (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
428             (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
429             (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
430             (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
431             (1ull << IB_USER_VERBS_CMD_POST_SEND) |
432             (1ull << IB_USER_VERBS_CMD_POST_RECV);
433         dev->ibdev.node_type = RDMA_NODE_RNIC;
434         memcpy(dev->ibdev.node_desc, C4IW_NODE_DESC, sizeof(C4IW_NODE_DESC));
435         dev->ibdev.phys_port_cnt = dev->rdev.lldi.nports;
436         dev->ibdev.num_comp_vectors = 1;
437         dev->ibdev.dma_device = &(dev->rdev.lldi.pdev->dev);
438         dev->ibdev.query_device = c4iw_query_device;
439         dev->ibdev.query_port = c4iw_query_port;
440         dev->ibdev.modify_port = c4iw_modify_port;
441         dev->ibdev.query_pkey = c4iw_query_pkey;
442         dev->ibdev.query_gid = c4iw_query_gid;
443         dev->ibdev.alloc_ucontext = c4iw_alloc_ucontext;
444         dev->ibdev.dealloc_ucontext = c4iw_dealloc_ucontext;
445         dev->ibdev.mmap = c4iw_mmap;
446         dev->ibdev.alloc_pd = c4iw_allocate_pd;
447         dev->ibdev.dealloc_pd = c4iw_deallocate_pd;
448         dev->ibdev.create_ah = c4iw_ah_create;
449         dev->ibdev.destroy_ah = c4iw_ah_destroy;
450         dev->ibdev.create_qp = c4iw_create_qp;
451         dev->ibdev.modify_qp = c4iw_ib_modify_qp;
452         dev->ibdev.destroy_qp = c4iw_destroy_qp;
453         dev->ibdev.create_cq = c4iw_create_cq;
454         dev->ibdev.destroy_cq = c4iw_destroy_cq;
455         dev->ibdev.resize_cq = c4iw_resize_cq;
456         dev->ibdev.poll_cq = c4iw_poll_cq;
457         dev->ibdev.get_dma_mr = c4iw_get_dma_mr;
458         dev->ibdev.reg_phys_mr = c4iw_register_phys_mem;
459         dev->ibdev.rereg_phys_mr = c4iw_reregister_phys_mem;
460         dev->ibdev.reg_user_mr = c4iw_reg_user_mr;
461         dev->ibdev.dereg_mr = c4iw_dereg_mr;
462         dev->ibdev.alloc_mw = c4iw_alloc_mw;
463         dev->ibdev.bind_mw = c4iw_bind_mw;
464         dev->ibdev.dealloc_mw = c4iw_dealloc_mw;
465         dev->ibdev.alloc_fast_reg_mr = c4iw_alloc_fast_reg_mr;
466         dev->ibdev.alloc_fast_reg_page_list = c4iw_alloc_fastreg_pbl;
467         dev->ibdev.free_fast_reg_page_list = c4iw_free_fastreg_pbl;
468         dev->ibdev.attach_mcast = c4iw_multicast_attach;
469         dev->ibdev.detach_mcast = c4iw_multicast_detach;
470         dev->ibdev.process_mad = c4iw_process_mad;
471         dev->ibdev.req_notify_cq = c4iw_arm_cq;
472         dev->ibdev.post_send = c4iw_post_send;
473         dev->ibdev.post_recv = c4iw_post_receive;
474         dev->ibdev.get_protocol_stats = c4iw_get_mib;
475
476         dev->ibdev.iwcm = kmalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);
477         if (!dev->ibdev.iwcm)
478                 return -ENOMEM;
479
480         dev->ibdev.iwcm->connect = c4iw_connect;
481         dev->ibdev.iwcm->accept = c4iw_accept_cr;
482         dev->ibdev.iwcm->reject = c4iw_reject_cr;
483         dev->ibdev.iwcm->create_listen = c4iw_create_listen;
484         dev->ibdev.iwcm->destroy_listen = c4iw_destroy_listen;
485         dev->ibdev.iwcm->add_ref = c4iw_qp_add_ref;
486         dev->ibdev.iwcm->rem_ref = c4iw_qp_rem_ref;
487         dev->ibdev.iwcm->get_qp = c4iw_get_qp;
488
489         ret = ib_register_device(&dev->ibdev, NULL);
490         if (ret)
491                 goto bail1;
492
493         for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i) {
494                 ret = device_create_file(&dev->ibdev.dev,
495                                          c4iw_class_attributes[i]);
496                 if (ret)
497                         goto bail2;
498         }
499         dev->registered = 1;
500         return 0;
501 bail2:
502         ib_unregister_device(&dev->ibdev);
503 bail1:
504         kfree(dev->ibdev.iwcm);
505         return ret;
506 }
507
508 void c4iw_unregister_device(struct c4iw_dev *dev)
509 {
510         int i;
511
512         PDBG("%s c4iw_dev %p\n", __func__, dev);
513         for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i)
514                 device_remove_file(&dev->ibdev.dev,
515                                    c4iw_class_attributes[i]);
516         ib_unregister_device(&dev->ibdev);
517         kfree(dev->ibdev.iwcm);
518         dev->registered = 0;
519         return;
520 }