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