provider.c revision 331769
1/*
2 * Copyright (c) 2009-2013, 2016 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 <sys/cdefs.h>
33__FBSDID("$FreeBSD: stable/11/sys/dev/cxgbe/iw_cxgbe/provider.c 331769 2018-03-30 18:06:29Z hselasky $");
34
35#define	LINUXKPI_PARAM_PREFIX iw_cxgbe_
36
37#include "opt_inet.h"
38
39#ifdef TCP_OFFLOAD
40#include <asm/pgtable.h>
41#include <linux/page.h>
42#include <rdma/ib_verbs.h>
43#include <rdma/ib_user_verbs.h>
44
45#include "iw_cxgbe.h"
46#include "user.h"
47extern int use_dsgl;
48static int fastreg_support = 1;
49module_param(fastreg_support, int, 0644);
50MODULE_PARM_DESC(fastreg_support, "Advertise fastreg support (default = 1)");
51
52static int c4iw_modify_port(struct ib_device *ibdev,
53			    u8 port, int port_modify_mask,
54			    struct ib_port_modify *props)
55{
56	return -ENOSYS;
57}
58
59static struct ib_ah *c4iw_ah_create(struct ib_pd *pd,
60				    struct ib_ah_attr *ah_attr)
61{
62	return ERR_PTR(-ENOSYS);
63}
64
65static int c4iw_ah_destroy(struct ib_ah *ah)
66{
67	return -ENOSYS;
68}
69
70static int c4iw_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
71{
72	return -ENOSYS;
73}
74
75static int c4iw_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
76{
77	return -ENOSYS;
78}
79
80static int c4iw_process_mad(struct ib_device *ibdev, int mad_flags,
81		u8 port_num, const struct ib_wc *in_wc,
82		const struct ib_grh *in_grh,
83		const struct ib_mad_hdr *in_mad,
84		size_t in_mad_size,
85		struct ib_mad_hdr *out_mad,
86		size_t *out_mad_size,
87		u16 *out_mad_pkey_index)
88
89{
90	return -ENOSYS;
91}
92
93void _c4iw_free_ucontext(struct kref *kref)
94{
95	struct c4iw_ucontext *ucontext;
96	struct c4iw_dev *rhp;
97	struct c4iw_mm_entry *mm, *tmp;
98
99	ucontext = container_of(kref, struct c4iw_ucontext, kref);
100	rhp = to_c4iw_dev(ucontext->ibucontext.device);
101
102	CTR2(KTR_IW_CXGBE, "%s ucontext %p", __func__, ucontext);
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}
108
109static int c4iw_dealloc_ucontext(struct ib_ucontext *context)
110{
111	struct c4iw_ucontext *ucontext = to_c4iw_ucontext(context);
112
113	CTR2(KTR_IW_CXGBE, "%s context %p", __func__, context);
114	c4iw_put_ucontext(ucontext);
115	return 0;
116}
117
118static struct ib_ucontext *c4iw_alloc_ucontext(struct ib_device *ibdev,
119					       struct ib_udata *udata)
120{
121	struct c4iw_ucontext *context;
122	struct c4iw_dev *rhp = to_c4iw_dev(ibdev);
123	static int warned;
124	struct c4iw_alloc_ucontext_resp uresp;
125	int ret = 0;
126	struct c4iw_mm_entry *mm = NULL;
127
128	PDBG("%s ibdev %p\n", __func__, ibdev);
129	context = kzalloc(sizeof(*context), GFP_KERNEL);
130	if (!context) {
131		ret = -ENOMEM;
132		goto err;
133	}
134
135	c4iw_init_dev_ucontext(&rhp->rdev, &context->uctx);
136	INIT_LIST_HEAD(&context->mmaps);
137	spin_lock_init(&context->mmap_lock);
138	kref_init(&context->kref);
139
140	if (udata->outlen < sizeof(uresp) - sizeof(uresp.reserved)) {
141		if (!warned++)
142			log(LOG_ERR, "%s Warning - downlevel libcxgb4 "
143			       "(non-fatal), device status page disabled.\n",
144			       __func__);
145		rhp->rdev.flags |= T4_STATUS_PAGE_DISABLED;
146	} else {
147
148		mm = kmalloc(sizeof *mm, GFP_KERNEL);
149		if (!mm)
150			goto err_free;
151
152		uresp.status_page_size = PAGE_SIZE;
153
154		spin_lock(&context->mmap_lock);
155		uresp.status_page_key = context->key;
156		context->key += PAGE_SIZE;
157		spin_unlock(&context->mmap_lock);
158
159		ret = ib_copy_to_udata(udata, &uresp,
160				       sizeof(uresp) - sizeof(uresp.reserved));
161		if (ret)
162			goto err_mm;
163
164		mm->key = uresp.status_page_key;
165		mm->addr = vtophys(rhp->rdev.status_page);
166		mm->len = PAGE_SIZE;
167		insert_mmap(context, mm);
168	}
169	return &context->ibucontext;
170err_mm:
171	kfree(mm);
172err_free:
173	kfree(context);
174err:
175	return ERR_PTR(ret);
176}
177
178static int c4iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
179{
180	int len = vma->vm_end - vma->vm_start;
181	u32 key = vma->vm_pgoff << PAGE_SHIFT;
182	struct c4iw_rdev *rdev;
183	int ret = 0;
184	struct c4iw_mm_entry *mm;
185	struct c4iw_ucontext *ucontext;
186	u64 addr = 0;
187
188	CTR4(KTR_IW_CXGBE, "%s:1 ctx %p vma %p, vm_start %u", __func__,
189			context, vma, vma->vm_start);
190
191	CTR4(KTR_IW_CXGBE, "%s:1a pgoff 0x%lx key 0x%x len %d", __func__,
192	    vma->vm_pgoff, key, len);
193
194	if (vma->vm_start & (PAGE_SIZE-1)) {
195		CTR3(KTR_IW_CXGBE, "%s:2 unaligned vm_start %u vma %p",
196		    __func__, vma->vm_start, vma);
197		return -EINVAL;
198	}
199
200	rdev = &(to_c4iw_dev(context->device)->rdev);
201	ucontext = to_c4iw_ucontext(context);
202
203	mm = remove_mmap(ucontext, key, len);
204	if (!mm) {
205		CTR4(KTR_IW_CXGBE, "%s:3 ucontext %p key %u len %u", __func__,
206		    ucontext, key, len);
207		return -EINVAL;
208	}
209	addr = mm->addr;
210	kfree(mm);
211
212	/* user DB-GTS registers if addr in udbs_res range,
213	 * else WQ or CQ memory.
214	 * */
215	if (rdev->adap->iwt.wc_en && addr >= rdev->bar2_pa &&
216			addr < rdev->bar2_pa + rdev->bar2_len)
217		vma->vm_page_prot = t4_pgprot_wc(vma->vm_page_prot);
218
219	ret = io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
220			len, vma->vm_page_prot);
221	CTR4(KTR_IW_CXGBE, "%s:4 ctx %p vma %p ret %u", __func__, context, vma,
222	    ret);
223	return ret;
224}
225
226static int
227c4iw_deallocate_pd(struct ib_pd *pd)
228{
229	struct c4iw_pd *php = to_c4iw_pd(pd);
230	struct c4iw_dev *rhp = php->rhp;
231
232	CTR3(KTR_IW_CXGBE, "%s: pd %p, pdid 0x%x", __func__, pd, php->pdid);
233
234	c4iw_put_resource(&rhp->rdev.resource.pdid_table, php->pdid);
235	mutex_lock(&rhp->rdev.stats.lock);
236	rhp->rdev.stats.pd.cur--;
237	mutex_unlock(&rhp->rdev.stats.lock);
238	kfree(php);
239
240	return (0);
241}
242
243static struct ib_pd *
244c4iw_allocate_pd(struct ib_device *ibdev, struct ib_ucontext *context,
245    struct ib_udata *udata)
246{
247	struct c4iw_pd *php;
248	u32 pdid;
249	struct c4iw_dev *rhp;
250
251	CTR4(KTR_IW_CXGBE, "%s: ibdev %p, context %p, data %p", __func__, ibdev,
252	    context, udata);
253	rhp = (struct c4iw_dev *) ibdev;
254	pdid =  c4iw_get_resource(&rhp->rdev.resource.pdid_table);
255	if (!pdid)
256		return ERR_PTR(-EINVAL);
257	php = kzalloc(sizeof(*php), GFP_KERNEL);
258	if (!php) {
259		c4iw_put_resource(&rhp->rdev.resource.pdid_table, pdid);
260		return ERR_PTR(-ENOMEM);
261	}
262	php->pdid = pdid;
263	php->rhp = rhp;
264	if (context) {
265		if (ib_copy_to_udata(udata, &php->pdid, sizeof(u32))) {
266			c4iw_deallocate_pd(&php->ibpd);
267			return ERR_PTR(-EFAULT);
268		}
269	}
270	mutex_lock(&rhp->rdev.stats.lock);
271	rhp->rdev.stats.pd.cur++;
272	if (rhp->rdev.stats.pd.cur > rhp->rdev.stats.pd.max)
273		rhp->rdev.stats.pd.max = rhp->rdev.stats.pd.cur;
274	mutex_unlock(&rhp->rdev.stats.lock);
275
276	CTR6(KTR_IW_CXGBE,
277	    "%s: ibdev %p, context %p, data %p, pddid 0x%x, pd %p", __func__,
278	    ibdev, context, udata, pdid, php);
279	return (&php->ibpd);
280}
281
282static int
283c4iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
284{
285
286	CTR5(KTR_IW_CXGBE, "%s ibdev %p, port %d, index %d, pkey %p", __func__,
287	    ibdev, port, index, pkey);
288
289	*pkey = 0;
290	return (0);
291}
292
293static int
294c4iw_query_gid(struct ib_device *ibdev, u8 port, int index, union ib_gid *gid)
295{
296	struct c4iw_dev *dev;
297	struct port_info *pi;
298	struct adapter *sc;
299
300	CTR5(KTR_IW_CXGBE, "%s ibdev %p, port %d, index %d, gid %p", __func__,
301	    ibdev, port, index, gid);
302
303	memset(&gid->raw[0], 0, sizeof(gid->raw));
304	dev = to_c4iw_dev(ibdev);
305	sc = dev->rdev.adap;
306	if (port == 0 || port > sc->params.nports)
307		return (-EINVAL);
308	pi = sc->port[port - 1];
309	memcpy(&gid->raw[0], pi->vi[0].hw_addr, ETHER_ADDR_LEN);
310	return (0);
311}
312
313static int
314c4iw_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
315		struct ib_udata *uhw)
316{
317	struct c4iw_dev *dev = to_c4iw_dev(ibdev);
318	struct adapter *sc = dev->rdev.adap;
319
320	CTR3(KTR_IW_CXGBE, "%s ibdev %p, props %p", __func__, ibdev, props);
321
322	if (uhw->inlen || uhw->outlen)
323		return -EINVAL;
324
325	memset(props, 0, sizeof *props);
326	memcpy(&props->sys_image_guid, sc->port[0]->vi[0].hw_addr,
327	    ETHER_ADDR_LEN);
328	props->hw_ver = sc->params.chipid;
329	props->fw_ver = sc->params.fw_vers;
330	props->device_cap_flags = dev->device_cap_flags;
331	props->page_size_cap = T4_PAGESIZE_MASK;
332	props->vendor_id = pci_get_vendor(sc->dev);
333	props->vendor_part_id = pci_get_device(sc->dev);
334	props->max_mr_size = T4_MAX_MR_SIZE;
335	props->max_qp = sc->vres.qp.size / 2;
336	props->max_qp_wr = dev->rdev.hw_queue.t4_max_qp_depth;
337	props->max_sge = T4_MAX_RECV_SGE;
338	props->max_sge_rd = 1;
339	props->max_res_rd_atom = sc->params.max_ird_adapter;
340	props->max_qp_rd_atom = min(sc->params.max_ordird_qp,
341	    c4iw_max_read_depth);
342	props->max_qp_init_rd_atom = props->max_qp_rd_atom;
343	props->max_cq = sc->vres.qp.size;
344	props->max_cqe = dev->rdev.hw_queue.t4_max_cq_depth;
345	props->max_mr = c4iw_num_stags(&dev->rdev);
346	props->max_pd = T4_MAX_NUM_PD;
347	props->local_ca_ack_delay = 0;
348	props->max_fast_reg_page_list_len =
349		t4_max_fr_depth(sc->params.ulptx_memwrite_dsgl && use_dsgl);
350
351	return (0);
352}
353
354/*
355 * Returns -errno on failure.
356 */
357static int
358c4iw_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
359{
360	struct c4iw_dev *dev;
361	struct adapter *sc;
362	struct port_info *pi;
363	struct ifnet *ifp;
364
365	CTR4(KTR_IW_CXGBE, "%s ibdev %p, port %d, props %p", __func__, ibdev,
366	    port, props);
367
368	dev = to_c4iw_dev(ibdev);
369	sc = dev->rdev.adap;
370	if (port > sc->params.nports)
371		return (-EINVAL);
372	pi = sc->port[port - 1];
373	ifp = pi->vi[0].ifp;
374
375	memset(props, 0, sizeof(struct ib_port_attr));
376	props->max_mtu = IB_MTU_4096;
377	if (ifp->if_mtu >= 4096)
378		props->active_mtu = IB_MTU_4096;
379	else if (ifp->if_mtu >= 2048)
380		props->active_mtu = IB_MTU_2048;
381	else if (ifp->if_mtu >= 1024)
382		props->active_mtu = IB_MTU_1024;
383	else if (ifp->if_mtu >= 512)
384		props->active_mtu = IB_MTU_512;
385	else
386		props->active_mtu = IB_MTU_256;
387	props->state = pi->link_cfg.link_ok ? IB_PORT_ACTIVE : IB_PORT_DOWN;
388	props->port_cap_flags =
389	    IB_PORT_CM_SUP |
390	    IB_PORT_SNMP_TUNNEL_SUP |
391	    IB_PORT_REINIT_SUP |
392	    IB_PORT_DEVICE_MGMT_SUP |
393	    IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
394	props->gid_tbl_len = 1;
395	props->pkey_tbl_len = 1;
396	props->active_width = 2;
397	props->active_speed = 2;
398	props->max_msg_sz = -1;
399
400	return 0;
401}
402
403static int c4iw_port_immutable(struct ib_device *ibdev, u8 port_num,
404			       struct ib_port_immutable *immutable)
405{
406	struct ib_port_attr attr;
407	int err;
408
409	immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
410
411	err = ib_query_port(ibdev, port_num, &attr);
412	if (err)
413		return err;
414
415	immutable->pkey_tbl_len = attr.pkey_tbl_len;
416	immutable->gid_tbl_len = attr.gid_tbl_len;
417
418	return 0;
419}
420
421/*
422 * Returns -errno on error.
423 */
424int
425c4iw_register_device(struct c4iw_dev *dev)
426{
427	struct adapter *sc = dev->rdev.adap;
428	struct ib_device *ibdev = &dev->ibdev;
429	struct iw_cm_verbs *iwcm;
430	int ret;
431
432	CTR3(KTR_IW_CXGBE, "%s c4iw_dev %p, adapter %p", __func__, dev, sc);
433	BUG_ON(!sc->port[0]);
434	strlcpy(ibdev->name, device_get_nameunit(sc->dev), sizeof(ibdev->name));
435	memset(&ibdev->node_guid, 0, sizeof(ibdev->node_guid));
436	memcpy(&ibdev->node_guid, sc->port[0]->vi[0].hw_addr, ETHER_ADDR_LEN);
437	ibdev->owner = THIS_MODULE;
438	dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY | IB_DEVICE_MEM_WINDOW;
439	if (fastreg_support)
440		dev->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
441	ibdev->local_dma_lkey = 0;
442	ibdev->uverbs_cmd_mask =
443	    (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
444	    (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
445	    (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
446	    (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
447	    (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
448	    (1ull << IB_USER_VERBS_CMD_REG_MR) |
449	    (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
450	    (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
451	    (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
452	    (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
453	    (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
454	    (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
455	    (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
456	    (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
457	    (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
458	    (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
459	    (1ull << IB_USER_VERBS_CMD_POST_SEND) |
460	    (1ull << IB_USER_VERBS_CMD_POST_RECV);
461	ibdev->node_type = RDMA_NODE_RNIC;
462	strlcpy(ibdev->node_desc, C4IW_NODE_DESC, sizeof(ibdev->node_desc));
463	ibdev->phys_port_cnt = sc->params.nports;
464	ibdev->num_comp_vectors = 1;
465	ibdev->dma_device = NULL;
466	ibdev->query_device = c4iw_query_device;
467	ibdev->query_port = c4iw_query_port;
468	ibdev->modify_port = c4iw_modify_port;
469	ibdev->query_pkey = c4iw_query_pkey;
470	ibdev->query_gid = c4iw_query_gid;
471	ibdev->alloc_ucontext = c4iw_alloc_ucontext;
472	ibdev->dealloc_ucontext = c4iw_dealloc_ucontext;
473	ibdev->mmap = c4iw_mmap;
474	ibdev->alloc_pd = c4iw_allocate_pd;
475	ibdev->dealloc_pd = c4iw_deallocate_pd;
476	ibdev->create_ah = c4iw_ah_create;
477	ibdev->destroy_ah = c4iw_ah_destroy;
478	ibdev->create_qp = c4iw_create_qp;
479	ibdev->modify_qp = c4iw_ib_modify_qp;
480	ibdev->query_qp = c4iw_ib_query_qp;
481	ibdev->destroy_qp = c4iw_destroy_qp;
482	ibdev->create_cq = c4iw_create_cq;
483	ibdev->destroy_cq = c4iw_destroy_cq;
484	ibdev->resize_cq = c4iw_resize_cq;
485	ibdev->poll_cq = c4iw_poll_cq;
486	ibdev->get_dma_mr = c4iw_get_dma_mr;
487	ibdev->reg_user_mr = c4iw_reg_user_mr;
488	ibdev->dereg_mr = c4iw_dereg_mr;
489	ibdev->alloc_mw = c4iw_alloc_mw;
490	ibdev->dealloc_mw = c4iw_dealloc_mw;
491	ibdev->alloc_mr = c4iw_alloc_mr;
492	ibdev->map_mr_sg = c4iw_map_mr_sg;
493	ibdev->attach_mcast = c4iw_multicast_attach;
494	ibdev->detach_mcast = c4iw_multicast_detach;
495	ibdev->process_mad = c4iw_process_mad;
496	ibdev->req_notify_cq = c4iw_arm_cq;
497	ibdev->post_send = c4iw_post_send;
498	ibdev->post_recv = c4iw_post_receive;
499	ibdev->uverbs_abi_ver = C4IW_UVERBS_ABI_VERSION;
500	ibdev->get_port_immutable = c4iw_port_immutable;
501
502	iwcm = kmalloc(sizeof(*iwcm), GFP_KERNEL);
503	if (iwcm == NULL)
504		return (-ENOMEM);
505
506	iwcm->connect = c4iw_connect;
507	iwcm->accept = c4iw_accept_cr;
508	iwcm->reject = c4iw_reject_cr;
509	iwcm->create_listen = c4iw_create_listen;
510	iwcm->destroy_listen = c4iw_destroy_listen;
511	iwcm->add_ref = c4iw_qp_add_ref;
512	iwcm->rem_ref = c4iw_qp_rem_ref;
513	iwcm->get_qp = c4iw_get_qp;
514	ibdev->iwcm = iwcm;
515
516	ret = ib_register_device(&dev->ibdev, NULL);
517	if (ret)
518		kfree(iwcm);
519
520	return (ret);
521}
522
523void
524c4iw_unregister_device(struct c4iw_dev *dev)
525{
526
527	CTR3(KTR_IW_CXGBE, "%s c4iw_dev %p, adapter %p", __func__, dev,
528	    dev->rdev.adap);
529	ib_unregister_device(&dev->ibdev);
530	kfree(dev->ibdev.iwcm);
531	return;
532}
533#endif
534