1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/slab.h>
35 
36 #include "ipoib.h"
37 
38 int ipoib_mcast_attach(struct net_device *dev, u16 mlid, union ib_gid *mgid, int set_qkey)
39 {
40 	struct ipoib_dev_priv *priv = netdev_priv(dev);
41 	struct ib_qp_attr *qp_attr = NULL;
42 	int ret;
43 	u16 pkey_index;
44 
45 	if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) {
46 		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
47 		ret = -ENXIO;
48 		goto out;
49 	}
50 	set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
51 
52 	if (set_qkey) {
53 		ret = -ENOMEM;
54 		qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
55 		if (!qp_attr)
56 			goto out;
57 
58 		/* set correct QKey for QP */
59 		qp_attr->qkey = priv->qkey;
60 		ret = ib_modify_qp(priv->qp, qp_attr, IB_QP_QKEY);
61 		if (ret) {
62 			ipoib_warn(priv, "failed to modify QP, ret = %d\n", ret);
63 			goto out;
64 		}
65 	}
66 
67 	/* attach QP to multicast group */
68 	ret = ib_attach_mcast(priv->qp, mgid, mlid);
69 	if (ret)
70 		ipoib_warn(priv, "failed to attach to multicast group, ret = %d\n", ret);
71 
72 out:
73 	kfree(qp_attr);
74 	return ret;
75 }
76 
77 int ipoib_init_qp(struct net_device *dev)
78 {
79 	struct ipoib_dev_priv *priv = netdev_priv(dev);
80 	int ret;
81 	struct ib_qp_attr qp_attr;
82 	int attr_mask;
83 
84 	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
85 		return -1;
86 
87 	qp_attr.qp_state = IB_QPS_INIT;
88 	qp_attr.qkey = 0;
89 	qp_attr.port_num = priv->port;
90 	qp_attr.pkey_index = priv->pkey_index;
91 	attr_mask =
92 	    IB_QP_QKEY |
93 	    IB_QP_PORT |
94 	    IB_QP_PKEY_INDEX |
95 	    IB_QP_STATE;
96 	ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);
97 	if (ret) {
98 		ipoib_warn(priv, "failed to modify QP to init, ret = %d\n", ret);
99 		goto out_fail;
100 	}
101 
102 	qp_attr.qp_state = IB_QPS_RTR;
103 	/* Can't set this in a INIT->RTR transition */
104 	attr_mask &= ~IB_QP_PORT;
105 	ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);
106 	if (ret) {
107 		ipoib_warn(priv, "failed to modify QP to RTR, ret = %d\n", ret);
108 		goto out_fail;
109 	}
110 
111 	qp_attr.qp_state = IB_QPS_RTS;
112 	qp_attr.sq_psn = 0;
113 	attr_mask |= IB_QP_SQ_PSN;
114 	attr_mask &= ~IB_QP_PKEY_INDEX;
115 	ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);
116 	if (ret) {
117 		ipoib_warn(priv, "failed to modify QP to RTS, ret = %d\n", ret);
118 		goto out_fail;
119 	}
120 
121 	return 0;
122 
123 out_fail:
124 	qp_attr.qp_state = IB_QPS_RESET;
125 	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
126 		ipoib_warn(priv, "Failed to modify QP to RESET state\n");
127 
128 	return ret;
129 }
130 
131 int ipoib_transport_dev_init(struct net_device *dev, struct ib_device *ca)
132 {
133 	struct ipoib_dev_priv *priv = netdev_priv(dev);
134 	struct ib_qp_init_attr init_attr = {
135 		.cap = {
136 			.max_send_wr  = ipoib_sendq_size,
137 			.max_recv_wr  = ipoib_recvq_size,
138 			.max_send_sge = 1,
139 			.max_recv_sge = IPOIB_UD_RX_SG
140 		},
141 		.sq_sig_type = IB_SIGNAL_ALL_WR,
142 		.qp_type     = IB_QPT_UD
143 	};
144 	struct ib_cq_init_attr cq_attr = {};
145 
146 	int ret, size;
147 	int i;
148 
149 	priv->pd = ib_alloc_pd(priv->ca);
150 	if (IS_ERR(priv->pd)) {
151 		printk(KERN_WARNING "%s: failed to allocate PD\n", ca->name);
152 		return -ENODEV;
153 	}
154 
155 	priv->mr = ib_get_dma_mr(priv->pd, IB_ACCESS_LOCAL_WRITE);
156 	if (IS_ERR(priv->mr)) {
157 		printk(KERN_WARNING "%s: ib_get_dma_mr failed\n", ca->name);
158 		goto out_free_pd;
159 	}
160 
161 	/*
162 	 * the various IPoIB tasks assume they will never race against
163 	 * themselves, so always use a single thread workqueue
164 	 */
165 	priv->wq = create_singlethread_workqueue("ipoib_wq");
166 	if (!priv->wq) {
167 		printk(KERN_WARNING "ipoib: failed to allocate device WQ\n");
168 		goto out_free_mr;
169 	}
170 
171 	size = ipoib_recvq_size + 1;
172 	ret = ipoib_cm_dev_init(dev);
173 	if (!ret) {
174 		size += ipoib_sendq_size;
175 		if (ipoib_cm_has_srq(dev))
176 			size += ipoib_recvq_size + 1; /* 1 extra for rx_drain_qp */
177 		else
178 			size += ipoib_recvq_size * ipoib_max_conn_qp;
179 	} else
180 		if (ret != -ENOSYS)
181 			goto out_free_wq;
182 
183 	cq_attr.cqe = size;
184 	priv->recv_cq = ib_create_cq(priv->ca, ipoib_ib_completion, NULL,
185 				     dev, &cq_attr);
186 	if (IS_ERR(priv->recv_cq)) {
187 		printk(KERN_WARNING "%s: failed to create receive CQ\n", ca->name);
188 		goto out_cm_dev_cleanup;
189 	}
190 
191 	cq_attr.cqe = ipoib_sendq_size;
192 	priv->send_cq = ib_create_cq(priv->ca, ipoib_send_comp_handler, NULL,
193 				     dev, &cq_attr);
194 	if (IS_ERR(priv->send_cq)) {
195 		printk(KERN_WARNING "%s: failed to create send CQ\n", ca->name);
196 		goto out_free_recv_cq;
197 	}
198 
199 	if (ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP))
200 		goto out_free_send_cq;
201 
202 	init_attr.send_cq = priv->send_cq;
203 	init_attr.recv_cq = priv->recv_cq;
204 
205 	if (priv->hca_caps & IB_DEVICE_UD_TSO)
206 		init_attr.create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
207 
208 	if (priv->hca_caps & IB_DEVICE_BLOCK_MULTICAST_LOOPBACK)
209 		init_attr.create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
210 
211 	if (priv->hca_caps & IB_DEVICE_MANAGED_FLOW_STEERING)
212 		init_attr.create_flags |= IB_QP_CREATE_NETIF_QP;
213 
214 	if (dev->features & NETIF_F_SG)
215 		init_attr.cap.max_send_sge = MAX_SKB_FRAGS + 1;
216 
217 	priv->qp = ib_create_qp(priv->pd, &init_attr);
218 	if (IS_ERR(priv->qp)) {
219 		printk(KERN_WARNING "%s: failed to create QP\n", ca->name);
220 		goto out_free_send_cq;
221 	}
222 
223 	priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff;
224 	priv->dev->dev_addr[2] = (priv->qp->qp_num >>  8) & 0xff;
225 	priv->dev->dev_addr[3] = (priv->qp->qp_num      ) & 0xff;
226 
227 	for (i = 0; i < MAX_SKB_FRAGS + 1; ++i)
228 		priv->tx_sge[i].lkey = priv->mr->lkey;
229 
230 	priv->tx_wr.opcode	= IB_WR_SEND;
231 	priv->tx_wr.sg_list	= priv->tx_sge;
232 	priv->tx_wr.send_flags	= IB_SEND_SIGNALED;
233 
234 	priv->rx_sge[0].lkey = priv->mr->lkey;
235 
236 	priv->rx_sge[0].length = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
237 	priv->rx_wr.num_sge = 1;
238 
239 	priv->rx_wr.next = NULL;
240 	priv->rx_wr.sg_list = priv->rx_sge;
241 
242 	return 0;
243 
244 out_free_send_cq:
245 	ib_destroy_cq(priv->send_cq);
246 
247 out_free_recv_cq:
248 	ib_destroy_cq(priv->recv_cq);
249 
250 out_cm_dev_cleanup:
251 	ipoib_cm_dev_cleanup(dev);
252 
253 out_free_wq:
254 	destroy_workqueue(priv->wq);
255 	priv->wq = NULL;
256 
257 out_free_mr:
258 	ib_dereg_mr(priv->mr);
259 
260 out_free_pd:
261 	ib_dealloc_pd(priv->pd);
262 
263 	return -ENODEV;
264 }
265 
266 void ipoib_transport_dev_cleanup(struct net_device *dev)
267 {
268 	struct ipoib_dev_priv *priv = netdev_priv(dev);
269 
270 	if (priv->qp) {
271 		if (ib_destroy_qp(priv->qp))
272 			ipoib_warn(priv, "ib_qp_destroy failed\n");
273 
274 		priv->qp = NULL;
275 		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
276 	}
277 
278 	if (ib_destroy_cq(priv->send_cq))
279 		ipoib_warn(priv, "ib_cq_destroy (send) failed\n");
280 
281 	if (ib_destroy_cq(priv->recv_cq))
282 		ipoib_warn(priv, "ib_cq_destroy (recv) failed\n");
283 
284 	ipoib_cm_dev_cleanup(dev);
285 
286 	if (priv->wq) {
287 		flush_workqueue(priv->wq);
288 		destroy_workqueue(priv->wq);
289 		priv->wq = NULL;
290 	}
291 
292 	if (ib_dereg_mr(priv->mr))
293 		ipoib_warn(priv, "ib_dereg_mr failed\n");
294 
295 	if (ib_dealloc_pd(priv->pd))
296 		ipoib_warn(priv, "ib_dealloc_pd failed\n");
297 
298 }
299 
300 void ipoib_event(struct ib_event_handler *handler,
301 		 struct ib_event *record)
302 {
303 	struct ipoib_dev_priv *priv =
304 		container_of(handler, struct ipoib_dev_priv, event_handler);
305 
306 	if (record->element.port_num != priv->port)
307 		return;
308 
309 	ipoib_dbg(priv, "Event %d on device %s port %d\n", record->event,
310 		  record->device->name, record->element.port_num);
311 
312 	if (record->event == IB_EVENT_SM_CHANGE ||
313 	    record->event == IB_EVENT_CLIENT_REREGISTER) {
314 		queue_work(ipoib_workqueue, &priv->flush_light);
315 	} else if (record->event == IB_EVENT_PORT_ERR ||
316 		   record->event == IB_EVENT_PORT_ACTIVE ||
317 		   record->event == IB_EVENT_LID_CHANGE) {
318 		queue_work(ipoib_workqueue, &priv->flush_normal);
319 	} else if (record->event == IB_EVENT_PKEY_CHANGE) {
320 		queue_work(ipoib_workqueue, &priv->flush_heavy);
321 	}
322 }
323