RDMA/nes: Remove useless usecnt variable and redundant memset
[linux-2.6-block.git] / drivers / infiniband / hw / cxgb4 / provider.c
CommitLineData
cfdda9d7
SW
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
40dbf6ee 57static int fastreg_support = 1;
cfdda9d7 58module_param(fastreg_support, int, 0644);
40dbf6ee 59MODULE_PARM_DESC(fastreg_support, "Advertise fastreg support (default=1)");
cfdda9d7 60
c12a67fe 61void _c4iw_free_ucontext(struct kref *kref)
cfdda9d7 62{
c12a67fe
SW
63 struct c4iw_ucontext *ucontext;
64 struct c4iw_dev *rhp;
cfdda9d7
SW
65 struct c4iw_mm_entry *mm, *tmp;
66
c12a67fe
SW
67 ucontext = container_of(kref, struct c4iw_ucontext, kref);
68 rhp = to_c4iw_dev(ucontext->ibucontext.device);
69
548ddb19 70 pr_debug("ucontext %p\n", ucontext);
cfdda9d7
SW
71 list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)
72 kfree(mm);
73 c4iw_release_dev_ucontext(&rhp->rdev, &ucontext->uctx);
74 kfree(ucontext);
c12a67fe
SW
75}
76
77static int c4iw_dealloc_ucontext(struct ib_ucontext *context)
78{
79 struct c4iw_ucontext *ucontext = to_c4iw_ucontext(context);
80
548ddb19 81 pr_debug("context %p\n", context);
c12a67fe 82 c4iw_put_ucontext(ucontext);
cfdda9d7
SW
83 return 0;
84}
85
86static struct ib_ucontext *c4iw_alloc_ucontext(struct ib_device *ibdev,
87 struct ib_udata *udata)
88{
89 struct c4iw_ucontext *context;
90 struct c4iw_dev *rhp = to_c4iw_dev(ibdev);
05eb2389
SW
91 struct c4iw_alloc_ucontext_resp uresp;
92 int ret = 0;
93 struct c4iw_mm_entry *mm = NULL;
cfdda9d7 94
548ddb19 95 pr_debug("ibdev %p\n", ibdev);
cfdda9d7 96 context = kzalloc(sizeof(*context), GFP_KERNEL);
05eb2389
SW
97 if (!context) {
98 ret = -ENOMEM;
99 goto err;
100 }
101
cfdda9d7
SW
102 c4iw_init_dev_ucontext(&rhp->rdev, &context->uctx);
103 INIT_LIST_HEAD(&context->mmaps);
104 spin_lock_init(&context->mmap_lock);
c12a67fe 105 kref_init(&context->kref);
05eb2389 106
b7dfa889 107 if (udata->outlen < sizeof(uresp) - sizeof(uresp.reserved)) {
700456bd 108 pr_err_once("Warning - downlevel libcxgb4 (non-fatal), device status page disabled\n");
05eb2389
SW
109 rhp->rdev.flags |= T4_STATUS_PAGE_DISABLED;
110 } else {
111 mm = kmalloc(sizeof(*mm), GFP_KERNEL);
bfd2793c
YD
112 if (!mm) {
113 ret = -ENOMEM;
05eb2389 114 goto err_free;
bfd2793c 115 }
05eb2389
SW
116
117 uresp.status_page_size = PAGE_SIZE;
118
119 spin_lock(&context->mmap_lock);
120 uresp.status_page_key = context->key;
121 context->key += PAGE_SIZE;
122 spin_unlock(&context->mmap_lock);
123
b7dfa889
YD
124 ret = ib_copy_to_udata(udata, &uresp,
125 sizeof(uresp) - sizeof(uresp.reserved));
05eb2389
SW
126 if (ret)
127 goto err_mm;
128
129 mm->key = uresp.status_page_key;
130 mm->addr = virt_to_phys(rhp->rdev.status_page);
131 mm->len = PAGE_SIZE;
132 insert_mmap(context, mm);
133 }
cfdda9d7 134 return &context->ibucontext;
05eb2389
SW
135err_mm:
136 kfree(mm);
137err_free:
138 kfree(context);
139err:
140 return ERR_PTR(ret);
cfdda9d7
SW
141}
142
143static int c4iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
144{
145 int len = vma->vm_end - vma->vm_start;
146 u32 key = vma->vm_pgoff << PAGE_SHIFT;
147 struct c4iw_rdev *rdev;
148 int ret = 0;
149 struct c4iw_mm_entry *mm;
150 struct c4iw_ucontext *ucontext;
151 u64 addr;
152
548ddb19 153 pr_debug("pgoff 0x%lx key 0x%x len %d\n", vma->vm_pgoff,
a9a42886 154 key, len);
cfdda9d7
SW
155
156 if (vma->vm_start & (PAGE_SIZE-1))
157 return -EINVAL;
158
159 rdev = &(to_c4iw_dev(context->device)->rdev);
160 ucontext = to_c4iw_ucontext(context);
161
162 mm = remove_mmap(ucontext, key, len);
163 if (!mm)
164 return -EINVAL;
165 addr = mm->addr;
166 kfree(mm);
167
c6d7b267
SW
168 if ((addr >= pci_resource_start(rdev->lldi.pdev, 0)) &&
169 (addr < (pci_resource_start(rdev->lldi.pdev, 0) +
170 pci_resource_len(rdev->lldi.pdev, 0)))) {
cfdda9d7
SW
171
172 /*
c6d7b267 173 * MA_SYNC register...
cfdda9d7 174 */
cfdda9d7 175 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
c6d7b267
SW
176 ret = io_remap_pfn_range(vma, vma->vm_start,
177 addr >> PAGE_SHIFT,
178 len, vma->vm_page_prot);
179 } else if ((addr >= pci_resource_start(rdev->lldi.pdev, 2)) &&
180 (addr < (pci_resource_start(rdev->lldi.pdev, 2) +
181 pci_resource_len(rdev->lldi.pdev, 2)))) {
182
183 /*
184 * Map user DB or OCQP memory...
185 */
186 if (addr >= rdev->oc_mw_pa)
187 vma->vm_page_prot = t4_pgprot_wc(vma->vm_page_prot);
f079af7a 188 else {
963cab50 189 if (!is_t4(rdev->lldi.adapter_type))
f079af7a
VP
190 vma->vm_page_prot =
191 t4_pgprot_wc(vma->vm_page_prot);
192 else
193 vma->vm_page_prot =
194 pgprot_noncached(vma->vm_page_prot);
195 }
cfdda9d7
SW
196 ret = io_remap_pfn_range(vma, vma->vm_start,
197 addr >> PAGE_SHIFT,
198 len, vma->vm_page_prot);
199 } else {
200
201 /*
202 * Map WQ or CQ contig dma memory...
203 */
204 ret = remap_pfn_range(vma, vma->vm_start,
205 addr >> PAGE_SHIFT,
206 len, vma->vm_page_prot);
207 }
208
209 return ret;
210}
211
21a428a0 212static void c4iw_deallocate_pd(struct ib_pd *pd)
cfdda9d7
SW
213{
214 struct c4iw_dev *rhp;
215 struct c4iw_pd *php;
216
217 php = to_c4iw_pd(pd);
218 rhp = php->rhp;
548ddb19 219 pr_debug("ibpd %p pdid 0x%x\n", pd, php->pdid);
ec3eead2 220 c4iw_put_resource(&rhp->rdev.resource.pdid_table, php->pdid);
8d81ef34
VP
221 mutex_lock(&rhp->rdev.stats.lock);
222 rhp->rdev.stats.pd.cur--;
223 mutex_unlock(&rhp->rdev.stats.lock);
cfdda9d7
SW
224}
225
21a428a0
LR
226static int c4iw_allocate_pd(struct ib_pd *pd, struct ib_ucontext *context,
227 struct ib_udata *udata)
cfdda9d7 228{
21a428a0
LR
229 struct c4iw_pd *php = to_c4iw_pd(pd);
230 struct ib_device *ibdev = pd->device;
cfdda9d7
SW
231 u32 pdid;
232 struct c4iw_dev *rhp;
233
548ddb19 234 pr_debug("ibdev %p\n", ibdev);
cfdda9d7 235 rhp = (struct c4iw_dev *) ibdev;
ec3eead2 236 pdid = c4iw_get_resource(&rhp->rdev.resource.pdid_table);
cfdda9d7 237 if (!pdid)
21a428a0
LR
238 return -EINVAL;
239
cfdda9d7
SW
240 php->pdid = pdid;
241 php->rhp = rhp;
242 if (context) {
7f86260b
JG
243 struct c4iw_alloc_pd_resp uresp = {.pdid = php->pdid};
244
245 if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
cfdda9d7 246 c4iw_deallocate_pd(&php->ibpd);
21a428a0 247 return -EFAULT;
cfdda9d7
SW
248 }
249 }
8d81ef34
VP
250 mutex_lock(&rhp->rdev.stats.lock);
251 rhp->rdev.stats.pd.cur++;
252 if (rhp->rdev.stats.pd.cur > rhp->rdev.stats.pd.max)
253 rhp->rdev.stats.pd.max = rhp->rdev.stats.pd.cur;
254 mutex_unlock(&rhp->rdev.stats.lock);
548ddb19 255 pr_debug("pdid 0x%0x ptr 0x%p\n", pdid, php);
21a428a0 256 return 0;
cfdda9d7
SW
257}
258
259static int c4iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
260 u16 *pkey)
261{
548ddb19 262 pr_debug("ibdev %p\n", ibdev);
cfdda9d7
SW
263 *pkey = 0;
264 return 0;
265}
266
267static int c4iw_query_gid(struct ib_device *ibdev, u8 port, int index,
268 union ib_gid *gid)
269{
270 struct c4iw_dev *dev;
271
548ddb19
BP
272 pr_debug("ibdev %p, port %d, index %d, gid %p\n",
273 ibdev, port, index, gid);
ba97b749
SW
274 if (!port)
275 return -EINVAL;
cfdda9d7 276 dev = to_c4iw_dev(ibdev);
cfdda9d7
SW
277 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
278 memcpy(&(gid->raw[0]), dev->rdev.lldi.ports[port-1]->dev_addr, 6);
279 return 0;
280}
281
2528e33e
MB
282static int c4iw_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
283 struct ib_udata *uhw)
cfdda9d7
SW
284{
285
286 struct c4iw_dev *dev;
2528e33e 287
548ddb19 288 pr_debug("ibdev %p\n", ibdev);
cfdda9d7 289
2528e33e
MB
290 if (uhw->inlen || uhw->outlen)
291 return -EINVAL;
292
cfdda9d7
SW
293 dev = to_c4iw_dev(ibdev);
294 memset(props, 0, sizeof *props);
295 memcpy(&props->sys_image_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
f079af7a 296 props->hw_ver = CHELSIO_CHIP_RELEASE(dev->rdev.lldi.adapter_type);
cfdda9d7
SW
297 props->fw_ver = dev->rdev.lldi.fw_vers;
298 props->device_cap_flags = dev->device_cap_flags;
299 props->page_size_cap = T4_PAGESIZE_MASK;
300 props->vendor_id = (u32)dev->rdev.lldi.pdev->vendor;
301 props->vendor_part_id = (u32)dev->rdev.lldi.pdev->device;
302 props->max_mr_size = T4_MAX_MR_SIZE;
66eb19af 303 props->max_qp = dev->rdev.lldi.vr->qp.size / 2;
6a0b6174 304 props->max_srq = dev->rdev.lldi.vr->srq.size;
04e10e21 305 props->max_qp_wr = dev->rdev.hw_queue.t4_max_qp_depth;
6a0b6174 306 props->max_srq_wr = dev->rdev.hw_queue.t4_max_qp_depth;
33023fb8
SW
307 props->max_send_sge = min(T4_MAX_SEND_SGE, T4_MAX_WRITE_SGE);
308 props->max_recv_sge = T4_MAX_RECV_SGE;
6a0b6174 309 props->max_srq_sge = T4_MAX_RECV_SGE;
cfdda9d7 310 props->max_sge_rd = 1;
4c2c5763
HS
311 props->max_res_rd_atom = dev->rdev.lldi.max_ird_adapter;
312 props->max_qp_rd_atom = min(dev->rdev.lldi.max_ordird_qp,
313 c4iw_max_read_depth);
314 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
66eb19af 315 props->max_cq = dev->rdev.lldi.vr->qp.size;
04e10e21 316 props->max_cqe = dev->rdev.hw_queue.t4_max_cq_depth;
cfdda9d7
SW
317 props->max_mr = c4iw_num_stags(&dev->rdev);
318 props->max_pd = T4_MAX_NUM_PD;
319 props->local_ca_ack_delay = 0;
ee30f7d5
H
320 props->max_fast_reg_page_list_len =
321 t4_max_fr_depth(dev->rdev.lldi.ulptx_memwrite_dsgl && use_dsgl);
cfdda9d7
SW
322
323 return 0;
324}
325
326static int c4iw_query_port(struct ib_device *ibdev, u8 port,
327 struct ib_port_attr *props)
328{
329 struct c4iw_dev *dev;
330 struct net_device *netdev;
331 struct in_device *inetdev;
332
548ddb19 333 pr_debug("ibdev %p\n", ibdev);
cfdda9d7
SW
334
335 dev = to_c4iw_dev(ibdev);
336 netdev = dev->rdev.lldi.ports[port-1];
c4550c63 337 /* props being zeroed by the caller, avoid zeroing it here */
cfdda9d7 338 props->max_mtu = IB_MTU_4096;
d3f4aadd 339 props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
cfdda9d7
SW
340
341 if (!netif_carrier_ok(netdev))
342 props->state = IB_PORT_DOWN;
343 else {
344 inetdev = in_dev_get(netdev);
345 if (inetdev) {
346 if (inetdev->ifa_list)
347 props->state = IB_PORT_ACTIVE;
348 else
349 props->state = IB_PORT_INIT;
350 in_dev_put(inetdev);
351 } else
352 props->state = IB_PORT_INIT;
353 }
354
355 props->port_cap_flags =
356 IB_PORT_CM_SUP |
357 IB_PORT_SNMP_TUNNEL_SUP |
358 IB_PORT_REINIT_SUP |
359 IB_PORT_DEVICE_MGMT_SUP |
360 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
361 props->gid_tbl_len = 1;
362 props->pkey_tbl_len = 1;
363 props->active_width = 2;
2e96691c 364 props->active_speed = IB_SPEED_DDR;
cfdda9d7
SW
365 props->max_msg_sz = -1;
366
367 return 0;
368}
369
508a523f
PP
370static ssize_t hw_rev_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
cfdda9d7 372{
54747231
PP
373 struct c4iw_dev *c4iw_dev =
374 rdma_device_to_drv_device(dev, struct c4iw_dev, ibdev);
375
548ddb19 376 pr_debug("dev 0x%p\n", dev);
f079af7a
VP
377 return sprintf(buf, "%d\n",
378 CHELSIO_CHIP_RELEASE(c4iw_dev->rdev.lldi.adapter_type));
cfdda9d7 379}
508a523f 380static DEVICE_ATTR_RO(hw_rev);
cfdda9d7 381
508a523f
PP
382static ssize_t hca_type_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
cfdda9d7 384{
54747231
PP
385 struct c4iw_dev *c4iw_dev =
386 rdma_device_to_drv_device(dev, struct c4iw_dev, ibdev);
cfdda9d7
SW
387 struct ethtool_drvinfo info;
388 struct net_device *lldev = c4iw_dev->rdev.lldi.ports[0];
389
548ddb19 390 pr_debug("dev 0x%p\n", dev);
cfdda9d7
SW
391 lldev->ethtool_ops->get_drvinfo(lldev, &info);
392 return sprintf(buf, "%s\n", info.driver);
393}
508a523f 394static DEVICE_ATTR_RO(hca_type);
cfdda9d7 395
508a523f
PP
396static ssize_t board_id_show(struct device *dev, struct device_attribute *attr,
397 char *buf)
cfdda9d7 398{
54747231
PP
399 struct c4iw_dev *c4iw_dev =
400 rdma_device_to_drv_device(dev, struct c4iw_dev, ibdev);
401
548ddb19 402 pr_debug("dev 0x%p\n", dev);
cfdda9d7
SW
403 return sprintf(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
404 c4iw_dev->rdev.lldi.pdev->device);
405}
508a523f 406static DEVICE_ATTR_RO(board_id);
cfdda9d7 407
b40f4757
CL
408enum counters {
409 IP4INSEGS,
410 IP4OUTSEGS,
411 IP4RETRANSSEGS,
412 IP4OUTRSTS,
413 IP6INSEGS,
414 IP6OUTSEGS,
415 IP6RETRANSSEGS,
416 IP6OUTRSTS,
417 NR_COUNTERS
418};
419
420static const char * const names[] = {
421 [IP4INSEGS] = "ip4InSegs",
422 [IP4OUTSEGS] = "ip4OutSegs",
423 [IP4RETRANSSEGS] = "ip4RetransSegs",
424 [IP4OUTRSTS] = "ip4OutRsts",
425 [IP6INSEGS] = "ip6InSegs",
426 [IP6OUTSEGS] = "ip6OutSegs",
427 [IP6RETRANSSEGS] = "ip6RetransSegs",
428 [IP6OUTRSTS] = "ip6OutRsts"
429};
430
431static struct rdma_hw_stats *c4iw_alloc_stats(struct ib_device *ibdev,
432 u8 port_num)
433{
434 BUILD_BUG_ON(ARRAY_SIZE(names) != NR_COUNTERS);
435
436 if (port_num != 0)
437 return NULL;
438
439 return rdma_alloc_hw_stats_struct(names, NR_COUNTERS,
440 RDMA_HW_STATS_DEFAULT_LIFESPAN);
441}
442
cfdda9d7 443static int c4iw_get_mib(struct ib_device *ibdev,
b40f4757
CL
444 struct rdma_hw_stats *stats,
445 u8 port, int index)
cfdda9d7 446{
de5dd81b
SW
447 struct tp_tcp_stats v4, v6;
448 struct c4iw_dev *c4iw_dev = to_c4iw_dev(ibdev);
449
450 cxgb4_get_tcp_stats(c4iw_dev->rdev.lldi.pdev, &v4, &v6);
b40f4757
CL
451 stats->value[IP4INSEGS] = v4.tcp_in_segs;
452 stats->value[IP4OUTSEGS] = v4.tcp_out_segs;
453 stats->value[IP4RETRANSSEGS] = v4.tcp_retrans_segs;
454 stats->value[IP4OUTRSTS] = v4.tcp_out_rsts;
455 stats->value[IP6INSEGS] = v6.tcp_in_segs;
456 stats->value[IP6OUTSEGS] = v6.tcp_out_segs;
457 stats->value[IP6RETRANSSEGS] = v6.tcp_retrans_segs;
458 stats->value[IP6OUTRSTS] = v6.tcp_out_rsts;
459
460 return stats->num_counters;
cfdda9d7
SW
461}
462
508a523f
PP
463static struct attribute *c4iw_class_attributes[] = {
464 &dev_attr_hw_rev.attr,
465 &dev_attr_hca_type.attr,
466 &dev_attr_board_id.attr,
467 NULL
468};
cfdda9d7 469
508a523f
PP
470static const struct attribute_group c4iw_attr_group = {
471 .attrs = c4iw_class_attributes,
cfdda9d7
SW
472};
473
7738613e
IW
474static int c4iw_port_immutable(struct ib_device *ibdev, u8 port_num,
475 struct ib_port_immutable *immutable)
476{
477 struct ib_port_attr attr;
478 int err;
479
c4550c63
OG
480 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
481
482 err = ib_query_port(ibdev, port_num, &attr);
7738613e
IW
483 if (err)
484 return err;
485
486 immutable->pkey_tbl_len = attr.pkey_tbl_len;
487 immutable->gid_tbl_len = attr.gid_tbl_len;
488
489 return 0;
490}
491
9abb0d1b 492static void get_dev_fw_str(struct ib_device *dev, char *str)
ce192243
IW
493{
494 struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
495 ibdev);
548ddb19 496 pr_debug("dev 0x%p\n", dev);
ce192243 497
9abb0d1b 498 snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u.%u.%u",
ce192243
IW
499 FW_HDR_FW_VER_MAJOR_G(c4iw_dev->rdev.lldi.fw_vers),
500 FW_HDR_FW_VER_MINOR_G(c4iw_dev->rdev.lldi.fw_vers),
501 FW_HDR_FW_VER_MICRO_G(c4iw_dev->rdev.lldi.fw_vers),
502 FW_HDR_FW_VER_BUILD_G(c4iw_dev->rdev.lldi.fw_vers));
503}
504
f215a3d2
SW
505static struct net_device *get_netdev(struct ib_device *dev, u8 port)
506{
507 struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev, ibdev);
508 struct c4iw_rdev *rdev = &c4iw_dev->rdev;
509 struct net_device *ndev;
510
511 if (!port || port > rdev->lldi.nports)
512 return NULL;
513
514 rcu_read_lock();
515 ndev = rdev->lldi.ports[port - 1];
516 if (ndev)
517 dev_hold(ndev);
518 rcu_read_unlock();
519
520 return ndev;
521}
522
056f9c7f
SW
523static int fill_res_entry(struct sk_buff *msg, struct rdma_restrack_entry *res)
524{
525 return (res->type < ARRAY_SIZE(c4iw_restrack_funcs) &&
526 c4iw_restrack_funcs[res->type]) ?
527 c4iw_restrack_funcs[res->type](msg, res) : 0;
528}
529
dad3b05d
KH
530static const struct ib_device_ops c4iw_dev_ops = {
531 .alloc_hw_stats = c4iw_alloc_stats,
532 .alloc_mr = c4iw_alloc_mr,
533 .alloc_mw = c4iw_alloc_mw,
534 .alloc_pd = c4iw_allocate_pd,
535 .alloc_ucontext = c4iw_alloc_ucontext,
536 .create_cq = c4iw_create_cq,
537 .create_qp = c4iw_create_qp,
538 .create_srq = c4iw_create_srq,
539 .dealloc_mw = c4iw_dealloc_mw,
540 .dealloc_pd = c4iw_deallocate_pd,
541 .dealloc_ucontext = c4iw_dealloc_ucontext,
542 .dereg_mr = c4iw_dereg_mr,
543 .destroy_cq = c4iw_destroy_cq,
544 .destroy_qp = c4iw_destroy_qp,
545 .destroy_srq = c4iw_destroy_srq,
02da3750 546 .fill_res_entry = fill_res_entry,
dad3b05d
KH
547 .get_dev_fw_str = get_dev_fw_str,
548 .get_dma_mr = c4iw_get_dma_mr,
549 .get_hw_stats = c4iw_get_mib,
550 .get_netdev = get_netdev,
551 .get_port_immutable = c4iw_port_immutable,
552 .map_mr_sg = c4iw_map_mr_sg,
553 .mmap = c4iw_mmap,
554 .modify_qp = c4iw_ib_modify_qp,
555 .modify_srq = c4iw_modify_srq,
556 .poll_cq = c4iw_poll_cq,
557 .post_recv = c4iw_post_receive,
558 .post_send = c4iw_post_send,
559 .post_srq_recv = c4iw_post_srq_recv,
560 .query_device = c4iw_query_device,
561 .query_gid = c4iw_query_gid,
562 .query_pkey = c4iw_query_pkey,
563 .query_port = c4iw_query_port,
564 .query_qp = c4iw_ib_query_qp,
565 .reg_user_mr = c4iw_reg_user_mr,
566 .req_notify_cq = c4iw_arm_cq,
21a428a0 567 INIT_RDMA_OBJ_SIZE(ib_pd, c4iw_pd, ibpd),
dad3b05d
KH
568};
569
1c8f1da5 570void c4iw_register_device(struct work_struct *work)
cfdda9d7
SW
571{
572 int ret;
1c8f1da5
BP
573 struct uld_ctx *ctx = container_of(work, struct uld_ctx, reg_work);
574 struct c4iw_dev *dev = ctx->dev;
cfdda9d7 575
548ddb19 576 pr_debug("c4iw_dev %p\n", dev);
cfdda9d7
SW
577 memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
578 memcpy(&dev->ibdev.node_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
579 dev->ibdev.owner = THIS_MODULE;
580 dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY | IB_DEVICE_MEM_WINDOW;
581 if (fastreg_support)
582 dev->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
583 dev->ibdev.local_dma_lkey = 0;
584 dev->ibdev.uverbs_cmd_mask =
585 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
586 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
587 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
588 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
589 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
590 (1ull << IB_USER_VERBS_CMD_REG_MR) |
591 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
592 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
593 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
594 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
595 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
596 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
597 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
67bbc055 598 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
cfdda9d7
SW
599 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
600 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
601 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
6a0b6174
RR
602 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
603 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
604 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
605 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
cfdda9d7 606 dev->ibdev.node_type = RDMA_NODE_RNIC;
bd99fdea 607 BUILD_BUG_ON(sizeof(C4IW_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX);
cfdda9d7
SW
608 memcpy(dev->ibdev.node_desc, C4IW_NODE_DESC, sizeof(C4IW_NODE_DESC));
609 dev->ibdev.phys_port_cnt = dev->rdev.lldi.nports;
cf38be6d 610 dev->ibdev.num_comp_vectors = dev->rdev.lldi.nciq;
d08868a1 611 dev->ibdev.dev.parent = &dev->rdev.lldi.pdev->dev;
c6d7b267 612 dev->ibdev.uverbs_abi_ver = C4IW_UVERBS_ABI_VERSION;
cfdda9d7 613
95b8e384 614 dev->ibdev.iwcm = kzalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);
1c8f1da5
BP
615 if (!dev->ibdev.iwcm) {
616 ret = -ENOMEM;
617 goto err_dealloc_ctx;
618 }
cfdda9d7
SW
619
620 dev->ibdev.iwcm->connect = c4iw_connect;
621 dev->ibdev.iwcm->accept = c4iw_accept_cr;
622 dev->ibdev.iwcm->reject = c4iw_reject_cr;
623 dev->ibdev.iwcm->create_listen = c4iw_create_listen;
624 dev->ibdev.iwcm->destroy_listen = c4iw_destroy_listen;
625 dev->ibdev.iwcm->add_ref = c4iw_qp_add_ref;
626 dev->ibdev.iwcm->rem_ref = c4iw_qp_rem_ref;
627 dev->ibdev.iwcm->get_qp = c4iw_get_qp;
851d7b6b
SW
628 memcpy(dev->ibdev.iwcm->ifname, dev->rdev.lldi.ports[0]->name,
629 sizeof(dev->ibdev.iwcm->ifname));
cfdda9d7 630
508a523f 631 rdma_set_device_sysfs_group(&dev->ibdev, &c4iw_attr_group);
0ede73bc 632 dev->ibdev.driver_id = RDMA_DRIVER_CXGB4;
dad3b05d 633 ib_set_device_ops(&dev->ibdev, &c4iw_dev_ops);
ea4baf7f 634 ret = ib_register_device(&dev->ibdev, "cxgb4_%d");
cfdda9d7 635 if (ret)
1c8f1da5 636 goto err_kfree_iwcm;
1c8f1da5 637 return;
508a523f 638
1c8f1da5 639err_kfree_iwcm:
cfdda9d7 640 kfree(dev->ibdev.iwcm);
1c8f1da5
BP
641err_dealloc_ctx:
642 pr_err("%s - Failed registering iwarp device: %d\n",
643 pci_name(ctx->lldi.pdev), ret);
644 c4iw_dealloc(ctx);
645 return;
cfdda9d7
SW
646}
647
648void c4iw_unregister_device(struct c4iw_dev *dev)
649{
548ddb19 650 pr_debug("c4iw_dev %p\n", dev);
cfdda9d7
SW
651 ib_unregister_device(&dev->ibdev);
652 kfree(dev->ibdev.iwcm);
653 return;
654}