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 static struct ib_ah *c4iw_ah_create(struct ib_pd *pd,
62 				    struct ib_ah_attr *ah_attr,
63 				    struct ib_udata *udata)
64 
65 {
66 	return ERR_PTR(-ENOSYS);
67 }
68 
69 static int c4iw_ah_destroy(struct ib_ah *ah)
70 {
71 	return -ENOSYS;
72 }
73 
74 static int c4iw_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
75 {
76 	return -ENOSYS;
77 }
78 
79 static int c4iw_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
80 {
81 	return -ENOSYS;
82 }
83 
84 static int c4iw_process_mad(struct ib_device *ibdev, int mad_flags,
85 			    u8 port_num, const struct ib_wc *in_wc,
86 			    const struct ib_grh *in_grh,
87 			    const struct ib_mad_hdr *in_mad,
88 			    size_t in_mad_size,
89 			    struct ib_mad_hdr *out_mad,
90 			    size_t *out_mad_size,
91 			    u16 *out_mad_pkey_index)
92 {
93 	return -ENOSYS;
94 }
95 
96 static int c4iw_dealloc_ucontext(struct ib_ucontext *context)
97 {
98 	struct c4iw_dev *rhp = to_c4iw_dev(context->device);
99 	struct c4iw_ucontext *ucontext = to_c4iw_ucontext(context);
100 	struct c4iw_mm_entry *mm, *tmp;
101 
102 	PDBG("%s context %p\n", __func__, context);
103 	list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)
104 		kfree(mm);
105 	c4iw_release_dev_ucontext(&rhp->rdev, &ucontext->uctx);
106 	kfree(ucontext);
107 	return 0;
108 }
109 
110 static struct ib_ucontext *c4iw_alloc_ucontext(struct ib_device *ibdev,
111 					       struct ib_udata *udata)
112 {
113 	struct c4iw_ucontext *context;
114 	struct c4iw_dev *rhp = to_c4iw_dev(ibdev);
115 	static int warned;
116 	struct c4iw_alloc_ucontext_resp uresp;
117 	int ret = 0;
118 	struct c4iw_mm_entry *mm = NULL;
119 
120 	PDBG("%s ibdev %p\n", __func__, ibdev);
121 	context = kzalloc(sizeof(*context), GFP_KERNEL);
122 	if (!context) {
123 		ret = -ENOMEM;
124 		goto err;
125 	}
126 
127 	c4iw_init_dev_ucontext(&rhp->rdev, &context->uctx);
128 	INIT_LIST_HEAD(&context->mmaps);
129 	spin_lock_init(&context->mmap_lock);
130 
131 	if (udata->outlen < sizeof(uresp) - sizeof(uresp.reserved)) {
132 		if (!warned++)
133 			pr_err(MOD "Warning - downlevel libcxgb4 (non-fatal), device status page disabled.");
134 		rhp->rdev.flags |= T4_STATUS_PAGE_DISABLED;
135 	} else {
136 		mm = kmalloc(sizeof(*mm), GFP_KERNEL);
137 		if (!mm) {
138 			ret = -ENOMEM;
139 			goto err_free;
140 		}
141 
142 		uresp.status_page_size = PAGE_SIZE;
143 
144 		spin_lock(&context->mmap_lock);
145 		uresp.status_page_key = context->key;
146 		context->key += PAGE_SIZE;
147 		spin_unlock(&context->mmap_lock);
148 
149 		ret = ib_copy_to_udata(udata, &uresp,
150 				       sizeof(uresp) - sizeof(uresp.reserved));
151 		if (ret)
152 			goto err_mm;
153 
154 		mm->key = uresp.status_page_key;
155 		mm->addr = virt_to_phys(rhp->rdev.status_page);
156 		mm->len = PAGE_SIZE;
157 		insert_mmap(context, mm);
158 	}
159 	return &context->ibucontext;
160 err_mm:
161 	kfree(mm);
162 err_free:
163 	kfree(context);
164 err:
165 	return ERR_PTR(ret);
166 }
167 
168 static int c4iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
169 {
170 	int len = vma->vm_end - vma->vm_start;
171 	u32 key = vma->vm_pgoff << PAGE_SHIFT;
172 	struct c4iw_rdev *rdev;
173 	int ret = 0;
174 	struct c4iw_mm_entry *mm;
175 	struct c4iw_ucontext *ucontext;
176 	u64 addr;
177 
178 	PDBG("%s pgoff 0x%lx key 0x%x len %d\n", __func__, vma->vm_pgoff,
179 	     key, len);
180 
181 	if (vma->vm_start & (PAGE_SIZE-1))
182 		return -EINVAL;
183 
184 	rdev = &(to_c4iw_dev(context->device)->rdev);
185 	ucontext = to_c4iw_ucontext(context);
186 
187 	mm = remove_mmap(ucontext, key, len);
188 	if (!mm)
189 		return -EINVAL;
190 	addr = mm->addr;
191 	kfree(mm);
192 
193 	if ((addr >= pci_resource_start(rdev->lldi.pdev, 0)) &&
194 	    (addr < (pci_resource_start(rdev->lldi.pdev, 0) +
195 		    pci_resource_len(rdev->lldi.pdev, 0)))) {
196 
197 		/*
198 		 * MA_SYNC register...
199 		 */
200 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
201 		ret = io_remap_pfn_range(vma, vma->vm_start,
202 					 addr >> PAGE_SHIFT,
203 					 len, vma->vm_page_prot);
204 	} else if ((addr >= pci_resource_start(rdev->lldi.pdev, 2)) &&
205 		   (addr < (pci_resource_start(rdev->lldi.pdev, 2) +
206 		    pci_resource_len(rdev->lldi.pdev, 2)))) {
207 
208 		/*
209 		 * Map user DB or OCQP memory...
210 		 */
211 		if (addr >= rdev->oc_mw_pa)
212 			vma->vm_page_prot = t4_pgprot_wc(vma->vm_page_prot);
213 		else {
214 			if (!is_t4(rdev->lldi.adapter_type))
215 				vma->vm_page_prot =
216 					t4_pgprot_wc(vma->vm_page_prot);
217 			else
218 				vma->vm_page_prot =
219 					pgprot_noncached(vma->vm_page_prot);
220 		}
221 		ret = io_remap_pfn_range(vma, vma->vm_start,
222 					 addr >> PAGE_SHIFT,
223 					 len, vma->vm_page_prot);
224 	} else {
225 
226 		/*
227 		 * Map WQ or CQ contig dma memory...
228 		 */
229 		ret = remap_pfn_range(vma, vma->vm_start,
230 				      addr >> PAGE_SHIFT,
231 				      len, vma->vm_page_prot);
232 	}
233 
234 	return ret;
235 }
236 
237 static int c4iw_deallocate_pd(struct ib_pd *pd)
238 {
239 	struct c4iw_dev *rhp;
240 	struct c4iw_pd *php;
241 
242 	php = to_c4iw_pd(pd);
243 	rhp = php->rhp;
244 	PDBG("%s ibpd %p pdid 0x%x\n", __func__, pd, php->pdid);
245 	c4iw_put_resource(&rhp->rdev.resource.pdid_table, php->pdid);
246 	mutex_lock(&rhp->rdev.stats.lock);
247 	rhp->rdev.stats.pd.cur--;
248 	mutex_unlock(&rhp->rdev.stats.lock);
249 	kfree(php);
250 	return 0;
251 }
252 
253 static struct ib_pd *c4iw_allocate_pd(struct ib_device *ibdev,
254 				      struct ib_ucontext *context,
255 				      struct ib_udata *udata)
256 {
257 	struct c4iw_pd *php;
258 	u32 pdid;
259 	struct c4iw_dev *rhp;
260 
261 	PDBG("%s ibdev %p\n", __func__, ibdev);
262 	rhp = (struct c4iw_dev *) ibdev;
263 	pdid =  c4iw_get_resource(&rhp->rdev.resource.pdid_table);
264 	if (!pdid)
265 		return ERR_PTR(-EINVAL);
266 	php = kzalloc(sizeof(*php), GFP_KERNEL);
267 	if (!php) {
268 		c4iw_put_resource(&rhp->rdev.resource.pdid_table, pdid);
269 		return ERR_PTR(-ENOMEM);
270 	}
271 	php->pdid = pdid;
272 	php->rhp = rhp;
273 	if (context) {
274 		if (ib_copy_to_udata(udata, &php->pdid, sizeof(u32))) {
275 			c4iw_deallocate_pd(&php->ibpd);
276 			return ERR_PTR(-EFAULT);
277 		}
278 	}
279 	mutex_lock(&rhp->rdev.stats.lock);
280 	rhp->rdev.stats.pd.cur++;
281 	if (rhp->rdev.stats.pd.cur > rhp->rdev.stats.pd.max)
282 		rhp->rdev.stats.pd.max = rhp->rdev.stats.pd.cur;
283 	mutex_unlock(&rhp->rdev.stats.lock);
284 	PDBG("%s pdid 0x%0x ptr 0x%p\n", __func__, pdid, php);
285 	return &php->ibpd;
286 }
287 
288 static int c4iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
289 			   u16 *pkey)
290 {
291 	PDBG("%s ibdev %p\n", __func__, ibdev);
292 	*pkey = 0;
293 	return 0;
294 }
295 
296 static int c4iw_query_gid(struct ib_device *ibdev, u8 port, int index,
297 			  union ib_gid *gid)
298 {
299 	struct c4iw_dev *dev;
300 
301 	PDBG("%s ibdev %p, port %d, index %d, gid %p\n",
302 	       __func__, ibdev, port, index, gid);
303 	dev = to_c4iw_dev(ibdev);
304 	BUG_ON(port == 0);
305 	memset(&(gid->raw[0]), 0, sizeof(gid->raw));
306 	memcpy(&(gid->raw[0]), dev->rdev.lldi.ports[port-1]->dev_addr, 6);
307 	return 0;
308 }
309 
310 static int c4iw_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
311 			     struct ib_udata *uhw)
312 {
313 
314 	struct c4iw_dev *dev;
315 
316 	PDBG("%s ibdev %p\n", __func__, ibdev);
317 
318 	if (uhw->inlen || uhw->outlen)
319 		return -EINVAL;
320 
321 	dev = to_c4iw_dev(ibdev);
322 	memset(props, 0, sizeof *props);
323 	memcpy(&props->sys_image_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
324 	props->hw_ver = CHELSIO_CHIP_RELEASE(dev->rdev.lldi.adapter_type);
325 	props->fw_ver = dev->rdev.lldi.fw_vers;
326 	props->device_cap_flags = dev->device_cap_flags;
327 	props->page_size_cap = T4_PAGESIZE_MASK;
328 	props->vendor_id = (u32)dev->rdev.lldi.pdev->vendor;
329 	props->vendor_part_id = (u32)dev->rdev.lldi.pdev->device;
330 	props->max_mr_size = T4_MAX_MR_SIZE;
331 	props->max_qp = dev->rdev.lldi.vr->qp.size / 2;
332 	props->max_qp_wr = dev->rdev.hw_queue.t4_max_qp_depth;
333 	props->max_sge = T4_MAX_RECV_SGE;
334 	props->max_sge_rd = 1;
335 	props->max_res_rd_atom = dev->rdev.lldi.max_ird_adapter;
336 	props->max_qp_rd_atom = min(dev->rdev.lldi.max_ordird_qp,
337 				    c4iw_max_read_depth);
338 	props->max_qp_init_rd_atom = props->max_qp_rd_atom;
339 	props->max_cq = dev->rdev.lldi.vr->qp.size;
340 	props->max_cqe = dev->rdev.hw_queue.t4_max_cq_depth;
341 	props->max_mr = c4iw_num_stags(&dev->rdev);
342 	props->max_pd = T4_MAX_NUM_PD;
343 	props->local_ca_ack_delay = 0;
344 	props->max_fast_reg_page_list_len =
345 		t4_max_fr_depth(dev->rdev.lldi.ulptx_memwrite_dsgl && use_dsgl);
346 
347 	return 0;
348 }
349 
350 static int c4iw_query_port(struct ib_device *ibdev, u8 port,
351 			   struct ib_port_attr *props)
352 {
353 	struct c4iw_dev *dev;
354 	struct net_device *netdev;
355 	struct in_device *inetdev;
356 
357 	PDBG("%s ibdev %p\n", __func__, ibdev);
358 
359 	dev = to_c4iw_dev(ibdev);
360 	netdev = dev->rdev.lldi.ports[port-1];
361 
362 	memset(props, 0, sizeof(struct ib_port_attr));
363 	props->max_mtu = IB_MTU_4096;
364 	if (netdev->mtu >= 4096)
365 		props->active_mtu = IB_MTU_4096;
366 	else if (netdev->mtu >= 2048)
367 		props->active_mtu = IB_MTU_2048;
368 	else if (netdev->mtu >= 1024)
369 		props->active_mtu = IB_MTU_1024;
370 	else if (netdev->mtu >= 512)
371 		props->active_mtu = IB_MTU_512;
372 	else
373 		props->active_mtu = IB_MTU_256;
374 
375 	if (!netif_carrier_ok(netdev))
376 		props->state = IB_PORT_DOWN;
377 	else {
378 		inetdev = in_dev_get(netdev);
379 		if (inetdev) {
380 			if (inetdev->ifa_list)
381 				props->state = IB_PORT_ACTIVE;
382 			else
383 				props->state = IB_PORT_INIT;
384 			in_dev_put(inetdev);
385 		} else
386 			props->state = IB_PORT_INIT;
387 	}
388 
389 	props->port_cap_flags =
390 	    IB_PORT_CM_SUP |
391 	    IB_PORT_SNMP_TUNNEL_SUP |
392 	    IB_PORT_REINIT_SUP |
393 	    IB_PORT_DEVICE_MGMT_SUP |
394 	    IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
395 	props->gid_tbl_len = 1;
396 	props->pkey_tbl_len = 1;
397 	props->active_width = 2;
398 	props->active_speed = IB_SPEED_DDR;
399 	props->max_msg_sz = -1;
400 
401 	return 0;
402 }
403 
404 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
405 			char *buf)
406 {
407 	struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
408 						 ibdev.dev);
409 	PDBG("%s dev 0x%p\n", __func__, dev);
410 	return sprintf(buf, "%d\n",
411 		       CHELSIO_CHIP_RELEASE(c4iw_dev->rdev.lldi.adapter_type));
412 }
413 
414 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
415 			char *buf)
416 {
417 	struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
418 						 ibdev.dev);
419 	struct ethtool_drvinfo info;
420 	struct net_device *lldev = c4iw_dev->rdev.lldi.ports[0];
421 
422 	PDBG("%s dev 0x%p\n", __func__, dev);
423 	lldev->ethtool_ops->get_drvinfo(lldev, &info);
424 	return sprintf(buf, "%s\n", info.driver);
425 }
426 
427 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
428 			  char *buf)
429 {
430 	struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
431 						 ibdev.dev);
432 	PDBG("%s dev 0x%p\n", __func__, dev);
433 	return sprintf(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
434 		       c4iw_dev->rdev.lldi.pdev->device);
435 }
436 
437 enum counters {
438 	IP4INSEGS,
439 	IP4OUTSEGS,
440 	IP4RETRANSSEGS,
441 	IP4OUTRSTS,
442 	IP6INSEGS,
443 	IP6OUTSEGS,
444 	IP6RETRANSSEGS,
445 	IP6OUTRSTS,
446 	NR_COUNTERS
447 };
448 
449 static const char * const names[] = {
450 	[IP4INSEGS] = "ip4InSegs",
451 	[IP4OUTSEGS] = "ip4OutSegs",
452 	[IP4RETRANSSEGS] = "ip4RetransSegs",
453 	[IP4OUTRSTS] = "ip4OutRsts",
454 	[IP6INSEGS] = "ip6InSegs",
455 	[IP6OUTSEGS] = "ip6OutSegs",
456 	[IP6RETRANSSEGS] = "ip6RetransSegs",
457 	[IP6OUTRSTS] = "ip6OutRsts"
458 };
459 
460 static struct rdma_hw_stats *c4iw_alloc_stats(struct ib_device *ibdev,
461 					      u8 port_num)
462 {
463 	BUILD_BUG_ON(ARRAY_SIZE(names) != NR_COUNTERS);
464 
465 	if (port_num != 0)
466 		return NULL;
467 
468 	return rdma_alloc_hw_stats_struct(names, NR_COUNTERS,
469 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
470 }
471 
472 static int c4iw_get_mib(struct ib_device *ibdev,
473 			struct rdma_hw_stats *stats,
474 			u8 port, int index)
475 {
476 	struct tp_tcp_stats v4, v6;
477 	struct c4iw_dev *c4iw_dev = to_c4iw_dev(ibdev);
478 
479 	cxgb4_get_tcp_stats(c4iw_dev->rdev.lldi.pdev, &v4, &v6);
480 	stats->value[IP4INSEGS] = v4.tcp_in_segs;
481 	stats->value[IP4OUTSEGS] = v4.tcp_out_segs;
482 	stats->value[IP4RETRANSSEGS] = v4.tcp_retrans_segs;
483 	stats->value[IP4OUTRSTS] = v4.tcp_out_rsts;
484 	stats->value[IP6INSEGS] = v6.tcp_in_segs;
485 	stats->value[IP6OUTSEGS] = v6.tcp_out_segs;
486 	stats->value[IP6RETRANSSEGS] = v6.tcp_retrans_segs;
487 	stats->value[IP6OUTRSTS] = v6.tcp_out_rsts;
488 
489 	return stats->num_counters;
490 }
491 
492 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
493 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
494 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
495 
496 static struct device_attribute *c4iw_class_attributes[] = {
497 	&dev_attr_hw_rev,
498 	&dev_attr_hca_type,
499 	&dev_attr_board_id,
500 };
501 
502 static int c4iw_port_immutable(struct ib_device *ibdev, u8 port_num,
503 			       struct ib_port_immutable *immutable)
504 {
505 	struct ib_port_attr attr;
506 	int err;
507 
508 	err = c4iw_query_port(ibdev, port_num, &attr);
509 	if (err)
510 		return err;
511 
512 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
513 	immutable->gid_tbl_len = attr.gid_tbl_len;
514 	immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
515 
516 	return 0;
517 }
518 
519 static void get_dev_fw_str(struct ib_device *dev, char *str,
520 			   size_t str_len)
521 {
522 	struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
523 						 ibdev);
524 	PDBG("%s dev 0x%p\n", __func__, dev);
525 
526 	snprintf(str, str_len, "%u.%u.%u.%u",
527 		 FW_HDR_FW_VER_MAJOR_G(c4iw_dev->rdev.lldi.fw_vers),
528 		 FW_HDR_FW_VER_MINOR_G(c4iw_dev->rdev.lldi.fw_vers),
529 		 FW_HDR_FW_VER_MICRO_G(c4iw_dev->rdev.lldi.fw_vers),
530 		 FW_HDR_FW_VER_BUILD_G(c4iw_dev->rdev.lldi.fw_vers));
531 }
532 
533 int c4iw_register_device(struct c4iw_dev *dev)
534 {
535 	int ret;
536 	int i;
537 
538 	PDBG("%s c4iw_dev %p\n", __func__, dev);
539 	BUG_ON(!dev->rdev.lldi.ports[0]);
540 	strlcpy(dev->ibdev.name, "cxgb4_%d", IB_DEVICE_NAME_MAX);
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 	dev->ibdev.node_type = RDMA_NODE_RNIC;
568 	BUILD_BUG_ON(sizeof(C4IW_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX);
569 	memcpy(dev->ibdev.node_desc, C4IW_NODE_DESC, sizeof(C4IW_NODE_DESC));
570 	dev->ibdev.phys_port_cnt = dev->rdev.lldi.nports;
571 	dev->ibdev.num_comp_vectors =  dev->rdev.lldi.nciq;
572 	dev->ibdev.dma_device = &(dev->rdev.lldi.pdev->dev);
573 	dev->ibdev.query_device = c4iw_query_device;
574 	dev->ibdev.query_port = c4iw_query_port;
575 	dev->ibdev.query_pkey = c4iw_query_pkey;
576 	dev->ibdev.query_gid = c4iw_query_gid;
577 	dev->ibdev.alloc_ucontext = c4iw_alloc_ucontext;
578 	dev->ibdev.dealloc_ucontext = c4iw_dealloc_ucontext;
579 	dev->ibdev.mmap = c4iw_mmap;
580 	dev->ibdev.alloc_pd = c4iw_allocate_pd;
581 	dev->ibdev.dealloc_pd = c4iw_deallocate_pd;
582 	dev->ibdev.create_ah = c4iw_ah_create;
583 	dev->ibdev.destroy_ah = c4iw_ah_destroy;
584 	dev->ibdev.create_qp = c4iw_create_qp;
585 	dev->ibdev.modify_qp = c4iw_ib_modify_qp;
586 	dev->ibdev.query_qp = c4iw_ib_query_qp;
587 	dev->ibdev.destroy_qp = c4iw_destroy_qp;
588 	dev->ibdev.create_cq = c4iw_create_cq;
589 	dev->ibdev.destroy_cq = c4iw_destroy_cq;
590 	dev->ibdev.resize_cq = c4iw_resize_cq;
591 	dev->ibdev.poll_cq = c4iw_poll_cq;
592 	dev->ibdev.get_dma_mr = c4iw_get_dma_mr;
593 	dev->ibdev.reg_user_mr = c4iw_reg_user_mr;
594 	dev->ibdev.dereg_mr = c4iw_dereg_mr;
595 	dev->ibdev.alloc_mw = c4iw_alloc_mw;
596 	dev->ibdev.dealloc_mw = c4iw_dealloc_mw;
597 	dev->ibdev.alloc_mr = c4iw_alloc_mr;
598 	dev->ibdev.map_mr_sg = c4iw_map_mr_sg;
599 	dev->ibdev.attach_mcast = c4iw_multicast_attach;
600 	dev->ibdev.detach_mcast = c4iw_multicast_detach;
601 	dev->ibdev.process_mad = c4iw_process_mad;
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.alloc_hw_stats = c4iw_alloc_stats;
606 	dev->ibdev.get_hw_stats = c4iw_get_mib;
607 	dev->ibdev.uverbs_abi_ver = C4IW_UVERBS_ABI_VERSION;
608 	dev->ibdev.get_port_immutable = c4iw_port_immutable;
609 	dev->ibdev.get_dev_fw_str = get_dev_fw_str;
610 	dev->ibdev.drain_sq = c4iw_drain_sq;
611 	dev->ibdev.drain_rq = c4iw_drain_rq;
612 
613 	dev->ibdev.iwcm = kmalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);
614 	if (!dev->ibdev.iwcm)
615 		return -ENOMEM;
616 
617 	dev->ibdev.iwcm->connect = c4iw_connect;
618 	dev->ibdev.iwcm->accept = c4iw_accept_cr;
619 	dev->ibdev.iwcm->reject = c4iw_reject_cr;
620 	dev->ibdev.iwcm->create_listen = c4iw_create_listen;
621 	dev->ibdev.iwcm->destroy_listen = c4iw_destroy_listen;
622 	dev->ibdev.iwcm->add_ref = c4iw_qp_add_ref;
623 	dev->ibdev.iwcm->rem_ref = c4iw_qp_rem_ref;
624 	dev->ibdev.iwcm->get_qp = c4iw_get_qp;
625 	memcpy(dev->ibdev.iwcm->ifname, dev->rdev.lldi.ports[0]->name,
626 	       sizeof(dev->ibdev.iwcm->ifname));
627 
628 	ret = ib_register_device(&dev->ibdev, NULL);
629 	if (ret)
630 		goto bail1;
631 
632 	for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i) {
633 		ret = device_create_file(&dev->ibdev.dev,
634 					 c4iw_class_attributes[i]);
635 		if (ret)
636 			goto bail2;
637 	}
638 	return 0;
639 bail2:
640 	ib_unregister_device(&dev->ibdev);
641 bail1:
642 	kfree(dev->ibdev.iwcm);
643 	return ret;
644 }
645 
646 void c4iw_unregister_device(struct c4iw_dev *dev)
647 {
648 	int i;
649 
650 	PDBG("%s c4iw_dev %p\n", __func__, dev);
651 	for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i)
652 		device_remove_file(&dev->ibdev.dev,
653 				   c4iw_class_attributes[i]);
654 	ib_unregister_device(&dev->ibdev);
655 	kfree(dev->ibdev.iwcm);
656 	return;
657 }
658