]>
Commit | Line | Data |
---|---|---|
922a8e9f TT |
1 | /* |
2 | * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved. | |
3 | * Copyright (c) 2004 Topspin Corporation. All rights reserved. | |
4 | * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved. | |
5 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. | |
6 | * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved. | |
7 | * Copyright (c) 2005 Network Appliance, Inc. All rights reserved. | |
8 | * | |
9 | * This software is available to you under a choice of one of two | |
10 | * licenses. You may choose to be licensed under the terms of the GNU | |
11 | * General Public License (GPL) Version 2, available from the file | |
12 | * COPYING in the main directory of this source tree, or the | |
13 | * OpenIB.org BSD license below: | |
14 | * | |
15 | * Redistribution and use in source and binary forms, with or | |
16 | * without modification, are permitted provided that the following | |
17 | * conditions are met: | |
18 | * | |
19 | * - Redistributions of source code must retain the above | |
20 | * copyright notice, this list of conditions and the following | |
21 | * disclaimer. | |
22 | * | |
23 | * - Redistributions in binary form must reproduce the above | |
24 | * copyright notice, this list of conditions and the following | |
25 | * disclaimer in the documentation and/or other materials | |
26 | * provided with the distribution. | |
27 | * | |
28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
29 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
31 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
32 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
33 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
34 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
35 | * SOFTWARE. | |
36 | * | |
37 | */ | |
38 | #include <linux/dma-mapping.h> | |
39 | #include <linux/err.h> | |
40 | #include <linux/idr.h> | |
41 | #include <linux/interrupt.h> | |
922a8e9f TT |
42 | #include <linux/rbtree.h> |
43 | #include <linux/spinlock.h> | |
44 | #include <linux/workqueue.h> | |
45 | #include <linux/completion.h> | |
46 | ||
47 | #include <rdma/iw_cm.h> | |
48 | #include <rdma/ib_addr.h> | |
49 | ||
50 | #include "iwcm.h" | |
51 | ||
52 | MODULE_AUTHOR("Tom Tucker"); | |
53 | MODULE_DESCRIPTION("iWARP CM"); | |
54 | MODULE_LICENSE("Dual BSD/GPL"); | |
55 | ||
56 | static struct workqueue_struct *iwcm_wq; | |
57 | struct iwcm_work { | |
58 | struct work_struct work; | |
59 | struct iwcm_id_private *cm_id; | |
60 | struct list_head list; | |
61 | struct iw_cm_event event; | |
62 | struct list_head free_list; | |
63 | }; | |
64 | ||
65 | /* | |
66 | * The following services provide a mechanism for pre-allocating iwcm_work | |
67 | * elements. The design pre-allocates them based on the cm_id type: | |
68 | * LISTENING IDS: Get enough elements preallocated to handle the | |
69 | * listen backlog. | |
70 | * ACTIVE IDS: 4: CONNECT_REPLY, ESTABLISHED, DISCONNECT, CLOSE | |
71 | * PASSIVE IDS: 3: ESTABLISHED, DISCONNECT, CLOSE | |
72 | * | |
73 | * Allocating them in connect and listen avoids having to deal | |
74 | * with allocation failures on the event upcall from the provider (which | |
75 | * is called in the interrupt context). | |
76 | * | |
77 | * One exception is when creating the cm_id for incoming connection requests. | |
78 | * There are two cases: | |
79 | * 1) in the event upcall, cm_event_handler(), for a listening cm_id. If | |
80 | * the backlog is exceeded, then no more connection request events will | |
81 | * be processed. cm_event_handler() returns -ENOMEM in this case. Its up | |
715a588f | 82 | * to the provider to reject the connection request. |
922a8e9f TT |
83 | * 2) in the connection request workqueue handler, cm_conn_req_handler(). |
84 | * If work elements cannot be allocated for the new connect request cm_id, | |
85 | * then IWCM will call the provider reject method. This is ok since | |
86 | * cm_conn_req_handler() runs in the workqueue thread context. | |
87 | */ | |
88 | ||
89 | static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv) | |
90 | { | |
91 | struct iwcm_work *work; | |
92 | ||
93 | if (list_empty(&cm_id_priv->work_free_list)) | |
94 | return NULL; | |
95 | work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work, | |
96 | free_list); | |
97 | list_del_init(&work->free_list); | |
98 | return work; | |
99 | } | |
100 | ||
101 | static void put_work(struct iwcm_work *work) | |
102 | { | |
103 | list_add(&work->free_list, &work->cm_id->work_free_list); | |
104 | } | |
105 | ||
106 | static void dealloc_work_entries(struct iwcm_id_private *cm_id_priv) | |
107 | { | |
108 | struct list_head *e, *tmp; | |
109 | ||
110 | list_for_each_safe(e, tmp, &cm_id_priv->work_free_list) | |
111 | kfree(list_entry(e, struct iwcm_work, free_list)); | |
112 | } | |
113 | ||
114 | static int alloc_work_entries(struct iwcm_id_private *cm_id_priv, int count) | |
115 | { | |
116 | struct iwcm_work *work; | |
117 | ||
118 | BUG_ON(!list_empty(&cm_id_priv->work_free_list)); | |
119 | while (count--) { | |
120 | work = kmalloc(sizeof(struct iwcm_work), GFP_KERNEL); | |
121 | if (!work) { | |
122 | dealloc_work_entries(cm_id_priv); | |
123 | return -ENOMEM; | |
124 | } | |
125 | work->cm_id = cm_id_priv; | |
126 | INIT_LIST_HEAD(&work->list); | |
127 | put_work(work); | |
128 | } | |
129 | return 0; | |
130 | } | |
131 | ||
132 | /* | |
715a588f KK |
133 | * Save private data from incoming connection requests to |
134 | * iw_cm_event, so the low level driver doesn't have to. Adjust | |
922a8e9f TT |
135 | * the event ptr to point to the local copy. |
136 | */ | |
715a588f | 137 | static int copy_private_data(struct iw_cm_event *event) |
922a8e9f TT |
138 | { |
139 | void *p; | |
140 | ||
bed8bdfd | 141 | p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC); |
922a8e9f TT |
142 | if (!p) |
143 | return -ENOMEM; | |
922a8e9f TT |
144 | event->private_data = p; |
145 | return 0; | |
146 | } | |
147 | ||
ebb90986 SW |
148 | static void free_cm_id(struct iwcm_id_private *cm_id_priv) |
149 | { | |
150 | dealloc_work_entries(cm_id_priv); | |
151 | kfree(cm_id_priv); | |
152 | } | |
153 | ||
922a8e9f | 154 | /* |
9ab1ffa8 KK |
155 | * Release a reference on cm_id. If the last reference is being |
156 | * released, enable the waiting thread (in iw_destroy_cm_id) to | |
157 | * get woken up, and return 1 if a thread is already waiting. | |
922a8e9f TT |
158 | */ |
159 | static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv) | |
160 | { | |
922a8e9f TT |
161 | BUG_ON(atomic_read(&cm_id_priv->refcount)==0); |
162 | if (atomic_dec_and_test(&cm_id_priv->refcount)) { | |
163 | BUG_ON(!list_empty(&cm_id_priv->work_list)); | |
922a8e9f | 164 | complete(&cm_id_priv->destroy_comp); |
ebb90986 | 165 | return 1; |
922a8e9f TT |
166 | } |
167 | ||
ebb90986 | 168 | return 0; |
922a8e9f TT |
169 | } |
170 | ||
171 | static void add_ref(struct iw_cm_id *cm_id) | |
172 | { | |
173 | struct iwcm_id_private *cm_id_priv; | |
174 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
175 | atomic_inc(&cm_id_priv->refcount); | |
176 | } | |
177 | ||
178 | static void rem_ref(struct iw_cm_id *cm_id) | |
179 | { | |
180 | struct iwcm_id_private *cm_id_priv; | |
181 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
ebb90986 SW |
182 | if (iwcm_deref_id(cm_id_priv) && |
183 | test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) { | |
184 | BUG_ON(!list_empty(&cm_id_priv->work_list)); | |
185 | free_cm_id(cm_id_priv); | |
186 | } | |
922a8e9f TT |
187 | } |
188 | ||
189 | static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event); | |
190 | ||
191 | struct iw_cm_id *iw_create_cm_id(struct ib_device *device, | |
192 | iw_cm_handler cm_handler, | |
193 | void *context) | |
194 | { | |
195 | struct iwcm_id_private *cm_id_priv; | |
196 | ||
197 | cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL); | |
198 | if (!cm_id_priv) | |
199 | return ERR_PTR(-ENOMEM); | |
200 | ||
201 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
202 | cm_id_priv->id.device = device; | |
203 | cm_id_priv->id.cm_handler = cm_handler; | |
204 | cm_id_priv->id.context = context; | |
205 | cm_id_priv->id.event_handler = cm_event_handler; | |
206 | cm_id_priv->id.add_ref = add_ref; | |
207 | cm_id_priv->id.rem_ref = rem_ref; | |
208 | spin_lock_init(&cm_id_priv->lock); | |
209 | atomic_set(&cm_id_priv->refcount, 1); | |
210 | init_waitqueue_head(&cm_id_priv->connect_wait); | |
211 | init_completion(&cm_id_priv->destroy_comp); | |
212 | INIT_LIST_HEAD(&cm_id_priv->work_list); | |
213 | INIT_LIST_HEAD(&cm_id_priv->work_free_list); | |
214 | ||
215 | return &cm_id_priv->id; | |
216 | } | |
217 | EXPORT_SYMBOL(iw_create_cm_id); | |
218 | ||
219 | ||
220 | static int iwcm_modify_qp_err(struct ib_qp *qp) | |
221 | { | |
222 | struct ib_qp_attr qp_attr; | |
223 | ||
224 | if (!qp) | |
225 | return -EINVAL; | |
226 | ||
227 | qp_attr.qp_state = IB_QPS_ERR; | |
228 | return ib_modify_qp(qp, &qp_attr, IB_QP_STATE); | |
229 | } | |
230 | ||
231 | /* | |
232 | * This is really the RDMAC CLOSING state. It is most similar to the | |
233 | * IB SQD QP state. | |
234 | */ | |
235 | static int iwcm_modify_qp_sqd(struct ib_qp *qp) | |
236 | { | |
237 | struct ib_qp_attr qp_attr; | |
238 | ||
239 | BUG_ON(qp == NULL); | |
240 | qp_attr.qp_state = IB_QPS_SQD; | |
241 | return ib_modify_qp(qp, &qp_attr, IB_QP_STATE); | |
242 | } | |
243 | ||
244 | /* | |
245 | * CM_ID <-- CLOSING | |
246 | * | |
715a588f | 247 | * Block if a passive or active connection is currently being processed. Then |
922a8e9f TT |
248 | * process the event as follows: |
249 | * - If we are ESTABLISHED, move to CLOSING and modify the QP state | |
250 | * based on the abrupt flag | |
251 | * - If the connection is already in the CLOSING or IDLE state, the peer is | |
252 | * disconnecting concurrently with us and we've already seen the | |
253 | * DISCONNECT event -- ignore the request and return 0 | |
254 | * - Disconnect on a listening endpoint returns -EINVAL | |
255 | */ | |
256 | int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt) | |
257 | { | |
258 | struct iwcm_id_private *cm_id_priv; | |
259 | unsigned long flags; | |
260 | int ret = 0; | |
261 | struct ib_qp *qp = NULL; | |
262 | ||
263 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
264 | /* Wait if we're currently in a connect or accept downcall */ | |
265 | wait_event(cm_id_priv->connect_wait, | |
266 | !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags)); | |
267 | ||
268 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
269 | switch (cm_id_priv->state) { | |
270 | case IW_CM_STATE_ESTABLISHED: | |
271 | cm_id_priv->state = IW_CM_STATE_CLOSING; | |
272 | ||
273 | /* QP could be <nul> for user-mode client */ | |
274 | if (cm_id_priv->qp) | |
275 | qp = cm_id_priv->qp; | |
276 | else | |
277 | ret = -EINVAL; | |
278 | break; | |
279 | case IW_CM_STATE_LISTEN: | |
280 | ret = -EINVAL; | |
281 | break; | |
282 | case IW_CM_STATE_CLOSING: | |
283 | /* remote peer closed first */ | |
284 | case IW_CM_STATE_IDLE: | |
285 | /* accept or connect returned !0 */ | |
286 | break; | |
287 | case IW_CM_STATE_CONN_RECV: | |
288 | /* | |
289 | * App called disconnect before/without calling accept after | |
290 | * connect_request event delivered. | |
291 | */ | |
292 | break; | |
293 | case IW_CM_STATE_CONN_SENT: | |
294 | /* Can only get here if wait above fails */ | |
295 | default: | |
296 | BUG(); | |
297 | } | |
298 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
299 | ||
300 | if (qp) { | |
301 | if (abrupt) | |
302 | ret = iwcm_modify_qp_err(qp); | |
303 | else | |
304 | ret = iwcm_modify_qp_sqd(qp); | |
305 | ||
306 | /* | |
307 | * If both sides are disconnecting the QP could | |
308 | * already be in ERR or SQD states | |
309 | */ | |
310 | ret = 0; | |
311 | } | |
312 | ||
313 | return ret; | |
314 | } | |
315 | EXPORT_SYMBOL(iw_cm_disconnect); | |
316 | ||
317 | /* | |
318 | * CM_ID <-- DESTROYING | |
319 | * | |
320 | * Clean up all resources associated with the connection and release | |
321 | * the initial reference taken by iw_create_cm_id. | |
322 | */ | |
323 | static void destroy_cm_id(struct iw_cm_id *cm_id) | |
324 | { | |
325 | struct iwcm_id_private *cm_id_priv; | |
326 | unsigned long flags; | |
327 | int ret; | |
328 | ||
329 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
330 | /* | |
331 | * Wait if we're currently in a connect or accept downcall. A | |
332 | * listening endpoint should never block here. | |
333 | */ | |
334 | wait_event(cm_id_priv->connect_wait, | |
335 | !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags)); | |
336 | ||
337 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
338 | switch (cm_id_priv->state) { | |
339 | case IW_CM_STATE_LISTEN: | |
340 | cm_id_priv->state = IW_CM_STATE_DESTROYING; | |
341 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
342 | /* destroy the listening endpoint */ | |
343 | ret = cm_id->device->iwcm->destroy_listen(cm_id); | |
344 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
345 | break; | |
346 | case IW_CM_STATE_ESTABLISHED: | |
347 | cm_id_priv->state = IW_CM_STATE_DESTROYING; | |
348 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
349 | /* Abrupt close of the connection */ | |
350 | (void)iwcm_modify_qp_err(cm_id_priv->qp); | |
351 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
352 | break; | |
353 | case IW_CM_STATE_IDLE: | |
354 | case IW_CM_STATE_CLOSING: | |
355 | cm_id_priv->state = IW_CM_STATE_DESTROYING; | |
356 | break; | |
357 | case IW_CM_STATE_CONN_RECV: | |
358 | /* | |
359 | * App called destroy before/without calling accept after | |
ebb90986 SW |
360 | * receiving connection request event notification or |
361 | * returned non zero from the event callback function. | |
362 | * In either case, must tell the provider to reject. | |
922a8e9f TT |
363 | */ |
364 | cm_id_priv->state = IW_CM_STATE_DESTROYING; | |
54e05f15 | 365 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); |
cb58160e | 366 | cm_id->device->iwcm->reject(cm_id, NULL, 0); |
54e05f15 | 367 | spin_lock_irqsave(&cm_id_priv->lock, flags); |
922a8e9f TT |
368 | break; |
369 | case IW_CM_STATE_CONN_SENT: | |
370 | case IW_CM_STATE_DESTROYING: | |
371 | default: | |
372 | BUG(); | |
373 | break; | |
374 | } | |
375 | if (cm_id_priv->qp) { | |
376 | cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp); | |
377 | cm_id_priv->qp = NULL; | |
378 | } | |
379 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
380 | ||
381 | (void)iwcm_deref_id(cm_id_priv); | |
382 | } | |
383 | ||
384 | /* | |
385 | * This function is only called by the application thread and cannot | |
386 | * be called by the event thread. The function will wait for all | |
387 | * references to be released on the cm_id and then kfree the cm_id | |
388 | * object. | |
389 | */ | |
390 | void iw_destroy_cm_id(struct iw_cm_id *cm_id) | |
391 | { | |
392 | struct iwcm_id_private *cm_id_priv; | |
393 | ||
394 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
395 | BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)); | |
396 | ||
397 | destroy_cm_id(cm_id); | |
398 | ||
399 | wait_for_completion(&cm_id_priv->destroy_comp); | |
400 | ||
ebb90986 | 401 | free_cm_id(cm_id_priv); |
922a8e9f TT |
402 | } |
403 | EXPORT_SYMBOL(iw_destroy_cm_id); | |
404 | ||
405 | /* | |
406 | * CM_ID <-- LISTEN | |
407 | * | |
408 | * Start listening for connect requests. Generates one CONNECT_REQUEST | |
409 | * event for each inbound connect request. | |
410 | */ | |
411 | int iw_cm_listen(struct iw_cm_id *cm_id, int backlog) | |
412 | { | |
413 | struct iwcm_id_private *cm_id_priv; | |
414 | unsigned long flags; | |
13fccdb3 | 415 | int ret; |
922a8e9f TT |
416 | |
417 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
418 | ||
419 | ret = alloc_work_entries(cm_id_priv, backlog); | |
420 | if (ret) | |
421 | return ret; | |
422 | ||
423 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
424 | switch (cm_id_priv->state) { | |
425 | case IW_CM_STATE_IDLE: | |
426 | cm_id_priv->state = IW_CM_STATE_LISTEN; | |
427 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
428 | ret = cm_id->device->iwcm->create_listen(cm_id, backlog); | |
429 | if (ret) | |
430 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
431 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
432 | break; | |
433 | default: | |
434 | ret = -EINVAL; | |
435 | } | |
436 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
437 | ||
438 | return ret; | |
439 | } | |
440 | EXPORT_SYMBOL(iw_cm_listen); | |
441 | ||
442 | /* | |
443 | * CM_ID <-- IDLE | |
444 | * | |
445 | * Rejects an inbound connection request. No events are generated. | |
446 | */ | |
447 | int iw_cm_reject(struct iw_cm_id *cm_id, | |
448 | const void *private_data, | |
449 | u8 private_data_len) | |
450 | { | |
451 | struct iwcm_id_private *cm_id_priv; | |
452 | unsigned long flags; | |
453 | int ret; | |
454 | ||
455 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
456 | set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
457 | ||
458 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
459 | if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) { | |
460 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
461 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
462 | wake_up_all(&cm_id_priv->connect_wait); | |
463 | return -EINVAL; | |
464 | } | |
465 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
466 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
467 | ||
468 | ret = cm_id->device->iwcm->reject(cm_id, private_data, | |
469 | private_data_len); | |
470 | ||
471 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
472 | wake_up_all(&cm_id_priv->connect_wait); | |
473 | ||
474 | return ret; | |
475 | } | |
476 | EXPORT_SYMBOL(iw_cm_reject); | |
477 | ||
478 | /* | |
479 | * CM_ID <-- ESTABLISHED | |
480 | * | |
481 | * Accepts an inbound connection request and generates an ESTABLISHED | |
482 | * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block | |
483 | * until the ESTABLISHED event is received from the provider. | |
484 | */ | |
485 | int iw_cm_accept(struct iw_cm_id *cm_id, | |
486 | struct iw_cm_conn_param *iw_param) | |
487 | { | |
488 | struct iwcm_id_private *cm_id_priv; | |
489 | struct ib_qp *qp; | |
490 | unsigned long flags; | |
491 | int ret; | |
492 | ||
493 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
494 | set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
495 | ||
496 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
497 | if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) { | |
498 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
499 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
500 | wake_up_all(&cm_id_priv->connect_wait); | |
501 | return -EINVAL; | |
502 | } | |
503 | /* Get the ib_qp given the QPN */ | |
504 | qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn); | |
505 | if (!qp) { | |
506 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
507 | return -EINVAL; | |
508 | } | |
509 | cm_id->device->iwcm->add_ref(qp); | |
510 | cm_id_priv->qp = qp; | |
511 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
512 | ||
513 | ret = cm_id->device->iwcm->accept(cm_id, iw_param); | |
514 | if (ret) { | |
515 | /* An error on accept precludes provider events */ | |
516 | BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV); | |
517 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
518 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
519 | if (cm_id_priv->qp) { | |
520 | cm_id->device->iwcm->rem_ref(qp); | |
521 | cm_id_priv->qp = NULL; | |
522 | } | |
523 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
524 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
525 | wake_up_all(&cm_id_priv->connect_wait); | |
526 | } | |
527 | ||
528 | return ret; | |
529 | } | |
530 | EXPORT_SYMBOL(iw_cm_accept); | |
531 | ||
532 | /* | |
533 | * Active Side: CM_ID <-- CONN_SENT | |
534 | * | |
535 | * If successful, results in the generation of a CONNECT_REPLY | |
536 | * event. iw_cm_disconnect and iw_cm_destroy will block until the | |
537 | * CONNECT_REPLY event is received from the provider. | |
538 | */ | |
539 | int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param) | |
540 | { | |
541 | struct iwcm_id_private *cm_id_priv; | |
13fccdb3 | 542 | int ret; |
922a8e9f TT |
543 | unsigned long flags; |
544 | struct ib_qp *qp; | |
545 | ||
546 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
547 | ||
548 | ret = alloc_work_entries(cm_id_priv, 4); | |
549 | if (ret) | |
550 | return ret; | |
551 | ||
552 | set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
553 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
554 | ||
555 | if (cm_id_priv->state != IW_CM_STATE_IDLE) { | |
556 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
557 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
558 | wake_up_all(&cm_id_priv->connect_wait); | |
559 | return -EINVAL; | |
560 | } | |
561 | ||
562 | /* Get the ib_qp given the QPN */ | |
563 | qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn); | |
564 | if (!qp) { | |
565 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
566 | return -EINVAL; | |
567 | } | |
568 | cm_id->device->iwcm->add_ref(qp); | |
569 | cm_id_priv->qp = qp; | |
570 | cm_id_priv->state = IW_CM_STATE_CONN_SENT; | |
571 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
572 | ||
573 | ret = cm_id->device->iwcm->connect(cm_id, iw_param); | |
574 | if (ret) { | |
575 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
576 | if (cm_id_priv->qp) { | |
577 | cm_id->device->iwcm->rem_ref(qp); | |
578 | cm_id_priv->qp = NULL; | |
579 | } | |
580 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
581 | BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT); | |
582 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
583 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
584 | wake_up_all(&cm_id_priv->connect_wait); | |
585 | } | |
586 | ||
587 | return ret; | |
588 | } | |
589 | EXPORT_SYMBOL(iw_cm_connect); | |
590 | ||
591 | /* | |
592 | * Passive Side: new CM_ID <-- CONN_RECV | |
593 | * | |
594 | * Handles an inbound connect request. The function creates a new | |
595 | * iw_cm_id to represent the new connection and inherits the client | |
596 | * callback function and other attributes from the listening parent. | |
597 | * | |
598 | * The work item contains a pointer to the listen_cm_id and the event. The | |
599 | * listen_cm_id contains the client cm_handler, context and | |
600 | * device. These are copied when the device is cloned. The event | |
601 | * contains the new four tuple. | |
602 | * | |
603 | * An error on the child should not affect the parent, so this | |
604 | * function does not return a value. | |
605 | */ | |
606 | static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv, | |
607 | struct iw_cm_event *iw_event) | |
608 | { | |
609 | unsigned long flags; | |
610 | struct iw_cm_id *cm_id; | |
611 | struct iwcm_id_private *cm_id_priv; | |
612 | int ret; | |
613 | ||
614 | /* | |
615 | * The provider should never generate a connection request | |
616 | * event with a bad status. | |
617 | */ | |
618 | BUG_ON(iw_event->status); | |
619 | ||
620 | /* | |
621 | * We could be destroying the listening id. If so, ignore this | |
622 | * upcall. | |
623 | */ | |
624 | spin_lock_irqsave(&listen_id_priv->lock, flags); | |
625 | if (listen_id_priv->state != IW_CM_STATE_LISTEN) { | |
626 | spin_unlock_irqrestore(&listen_id_priv->lock, flags); | |
83b96586 | 627 | goto out; |
922a8e9f TT |
628 | } |
629 | spin_unlock_irqrestore(&listen_id_priv->lock, flags); | |
630 | ||
631 | cm_id = iw_create_cm_id(listen_id_priv->id.device, | |
632 | listen_id_priv->id.cm_handler, | |
633 | listen_id_priv->id.context); | |
634 | /* If the cm_id could not be created, ignore the request */ | |
635 | if (IS_ERR(cm_id)) | |
83b96586 | 636 | goto out; |
922a8e9f TT |
637 | |
638 | cm_id->provider_data = iw_event->provider_data; | |
639 | cm_id->local_addr = iw_event->local_addr; | |
640 | cm_id->remote_addr = iw_event->remote_addr; | |
641 | ||
642 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
643 | cm_id_priv->state = IW_CM_STATE_CONN_RECV; | |
644 | ||
645 | ret = alloc_work_entries(cm_id_priv, 3); | |
646 | if (ret) { | |
647 | iw_cm_reject(cm_id, NULL, 0); | |
648 | iw_destroy_cm_id(cm_id); | |
83b96586 | 649 | goto out; |
922a8e9f TT |
650 | } |
651 | ||
652 | /* Call the client CM handler */ | |
653 | ret = cm_id->cm_handler(cm_id, iw_event); | |
654 | if (ret) { | |
ebb90986 | 655 | iw_cm_reject(cm_id, NULL, 0); |
922a8e9f TT |
656 | set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags); |
657 | destroy_cm_id(cm_id); | |
658 | if (atomic_read(&cm_id_priv->refcount)==0) | |
ebb90986 | 659 | free_cm_id(cm_id_priv); |
922a8e9f TT |
660 | } |
661 | ||
83b96586 | 662 | out: |
922a8e9f TT |
663 | if (iw_event->private_data_len) |
664 | kfree(iw_event->private_data); | |
665 | } | |
666 | ||
667 | /* | |
668 | * Passive Side: CM_ID <-- ESTABLISHED | |
669 | * | |
670 | * The provider generated an ESTABLISHED event which means that | |
671 | * the MPA negotion has completed successfully and we are now in MPA | |
672 | * FPDU mode. | |
673 | * | |
674 | * This event can only be received in the CONN_RECV state. If the | |
675 | * remote peer closed, the ESTABLISHED event would be received followed | |
676 | * by the CLOSE event. If the app closes, it will block until we wake | |
677 | * it up after processing this event. | |
678 | */ | |
679 | static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv, | |
680 | struct iw_cm_event *iw_event) | |
681 | { | |
682 | unsigned long flags; | |
13fccdb3 | 683 | int ret; |
922a8e9f TT |
684 | |
685 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
686 | ||
687 | /* | |
688 | * We clear the CONNECT_WAIT bit here to allow the callback | |
689 | * function to call iw_cm_disconnect. Calling iw_destroy_cm_id | |
690 | * from a callback handler is not allowed. | |
691 | */ | |
692 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
693 | BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV); | |
694 | cm_id_priv->state = IW_CM_STATE_ESTABLISHED; | |
695 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
696 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event); | |
697 | wake_up_all(&cm_id_priv->connect_wait); | |
698 | ||
699 | return ret; | |
700 | } | |
701 | ||
702 | /* | |
703 | * Active Side: CM_ID <-- ESTABLISHED | |
704 | * | |
705 | * The app has called connect and is waiting for the established event to | |
706 | * post it's requests to the server. This event will wake up anyone | |
707 | * blocked in iw_cm_disconnect or iw_destroy_id. | |
708 | */ | |
709 | static int cm_conn_rep_handler(struct iwcm_id_private *cm_id_priv, | |
710 | struct iw_cm_event *iw_event) | |
711 | { | |
712 | unsigned long flags; | |
13fccdb3 | 713 | int ret; |
922a8e9f TT |
714 | |
715 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
716 | /* | |
717 | * Clear the connect wait bit so a callback function calling | |
718 | * iw_cm_disconnect will not wait and deadlock this thread | |
719 | */ | |
720 | clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); | |
721 | BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT); | |
722 | if (iw_event->status == IW_CM_EVENT_STATUS_ACCEPTED) { | |
723 | cm_id_priv->id.local_addr = iw_event->local_addr; | |
724 | cm_id_priv->id.remote_addr = iw_event->remote_addr; | |
725 | cm_id_priv->state = IW_CM_STATE_ESTABLISHED; | |
726 | } else { | |
727 | /* REJECTED or RESET */ | |
728 | cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp); | |
729 | cm_id_priv->qp = NULL; | |
730 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
731 | } | |
732 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
733 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event); | |
734 | ||
735 | if (iw_event->private_data_len) | |
736 | kfree(iw_event->private_data); | |
737 | ||
738 | /* Wake up waiters on connect complete */ | |
739 | wake_up_all(&cm_id_priv->connect_wait); | |
740 | ||
741 | return ret; | |
742 | } | |
743 | ||
744 | /* | |
745 | * CM_ID <-- CLOSING | |
746 | * | |
747 | * If in the ESTABLISHED state, move to CLOSING. | |
748 | */ | |
749 | static void cm_disconnect_handler(struct iwcm_id_private *cm_id_priv, | |
750 | struct iw_cm_event *iw_event) | |
751 | { | |
752 | unsigned long flags; | |
753 | ||
754 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
755 | if (cm_id_priv->state == IW_CM_STATE_ESTABLISHED) | |
756 | cm_id_priv->state = IW_CM_STATE_CLOSING; | |
757 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
758 | } | |
759 | ||
760 | /* | |
761 | * CM_ID <-- IDLE | |
762 | * | |
763 | * If in the ESTBLISHED or CLOSING states, the QP will have have been | |
764 | * moved by the provider to the ERR state. Disassociate the CM_ID from | |
765 | * the QP, move to IDLE, and remove the 'connected' reference. | |
766 | * | |
767 | * If in some other state, the cm_id was destroyed asynchronously. | |
768 | * This is the last reference that will result in waking up | |
769 | * the app thread blocked in iw_destroy_cm_id. | |
770 | */ | |
771 | static int cm_close_handler(struct iwcm_id_private *cm_id_priv, | |
772 | struct iw_cm_event *iw_event) | |
773 | { | |
774 | unsigned long flags; | |
775 | int ret = 0; | |
776 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
777 | ||
778 | if (cm_id_priv->qp) { | |
779 | cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp); | |
780 | cm_id_priv->qp = NULL; | |
781 | } | |
782 | switch (cm_id_priv->state) { | |
783 | case IW_CM_STATE_ESTABLISHED: | |
784 | case IW_CM_STATE_CLOSING: | |
785 | cm_id_priv->state = IW_CM_STATE_IDLE; | |
786 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
787 | ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event); | |
788 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
789 | break; | |
790 | case IW_CM_STATE_DESTROYING: | |
791 | break; | |
792 | default: | |
793 | BUG(); | |
794 | } | |
795 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
796 | ||
797 | return ret; | |
798 | } | |
799 | ||
800 | static int process_event(struct iwcm_id_private *cm_id_priv, | |
801 | struct iw_cm_event *iw_event) | |
802 | { | |
803 | int ret = 0; | |
804 | ||
805 | switch (iw_event->event) { | |
806 | case IW_CM_EVENT_CONNECT_REQUEST: | |
807 | cm_conn_req_handler(cm_id_priv, iw_event); | |
808 | break; | |
809 | case IW_CM_EVENT_CONNECT_REPLY: | |
810 | ret = cm_conn_rep_handler(cm_id_priv, iw_event); | |
811 | break; | |
812 | case IW_CM_EVENT_ESTABLISHED: | |
813 | ret = cm_conn_est_handler(cm_id_priv, iw_event); | |
814 | break; | |
815 | case IW_CM_EVENT_DISCONNECT: | |
816 | cm_disconnect_handler(cm_id_priv, iw_event); | |
817 | break; | |
818 | case IW_CM_EVENT_CLOSE: | |
819 | ret = cm_close_handler(cm_id_priv, iw_event); | |
820 | break; | |
821 | default: | |
822 | BUG(); | |
823 | } | |
824 | ||
825 | return ret; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Process events on the work_list for the cm_id. If the callback | |
830 | * function requests that the cm_id be deleted, a flag is set in the | |
831 | * cm_id flags to indicate that when the last reference is | |
832 | * removed, the cm_id is to be destroyed. This is necessary to | |
833 | * distinguish between an object that will be destroyed by the app | |
834 | * thread asleep on the destroy_comp list vs. an object destroyed | |
835 | * here synchronously when the last reference is removed. | |
836 | */ | |
c4028958 | 837 | static void cm_work_handler(struct work_struct *_work) |
922a8e9f | 838 | { |
4c1ac1b4 | 839 | struct iwcm_work *work = container_of(_work, struct iwcm_work, work); |
33ba0fa9 | 840 | struct iw_cm_event levent; |
922a8e9f TT |
841 | struct iwcm_id_private *cm_id_priv = work->cm_id; |
842 | unsigned long flags; | |
843 | int empty; | |
844 | int ret = 0; | |
d7c1fbd6 | 845 | int destroy_id; |
922a8e9f TT |
846 | |
847 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
848 | empty = list_empty(&cm_id_priv->work_list); | |
849 | while (!empty) { | |
850 | work = list_entry(cm_id_priv->work_list.next, | |
851 | struct iwcm_work, list); | |
852 | list_del_init(&work->list); | |
853 | empty = list_empty(&cm_id_priv->work_list); | |
33ba0fa9 | 854 | levent = work->event; |
922a8e9f TT |
855 | put_work(work); |
856 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
857 | ||
33ba0fa9 | 858 | ret = process_event(cm_id_priv, &levent); |
922a8e9f TT |
859 | if (ret) { |
860 | set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags); | |
861 | destroy_cm_id(&cm_id_priv->id); | |
862 | } | |
863 | BUG_ON(atomic_read(&cm_id_priv->refcount)==0); | |
d7c1fbd6 | 864 | destroy_id = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags); |
ebb90986 | 865 | if (iwcm_deref_id(cm_id_priv)) { |
d7c1fbd6 | 866 | if (destroy_id) { |
ebb90986 SW |
867 | BUG_ON(!list_empty(&cm_id_priv->work_list)); |
868 | free_cm_id(cm_id_priv); | |
869 | } | |
922a8e9f TT |
870 | return; |
871 | } | |
872 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
873 | } | |
874 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
875 | } | |
876 | ||
877 | /* | |
878 | * This function is called on interrupt context. Schedule events on | |
879 | * the iwcm_wq thread to allow callback functions to downcall into | |
880 | * the CM and/or block. Events are queued to a per-CM_ID | |
881 | * work_list. If this is the first event on the work_list, the work | |
882 | * element is also queued on the iwcm_wq thread. | |
883 | * | |
884 | * Each event holds a reference on the cm_id. Until the last posted | |
885 | * event has been delivered and processed, the cm_id cannot be | |
886 | * deleted. | |
887 | * | |
888 | * Returns: | |
889 | * 0 - the event was handled. | |
890 | * -ENOMEM - the event was not handled due to lack of resources. | |
891 | */ | |
892 | static int cm_event_handler(struct iw_cm_id *cm_id, | |
893 | struct iw_cm_event *iw_event) | |
894 | { | |
895 | struct iwcm_work *work; | |
896 | struct iwcm_id_private *cm_id_priv; | |
897 | unsigned long flags; | |
898 | int ret = 0; | |
899 | ||
900 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
901 | ||
902 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
903 | work = get_work(cm_id_priv); | |
904 | if (!work) { | |
905 | ret = -ENOMEM; | |
906 | goto out; | |
907 | } | |
908 | ||
c4028958 | 909 | INIT_WORK(&work->work, cm_work_handler); |
922a8e9f TT |
910 | work->cm_id = cm_id_priv; |
911 | work->event = *iw_event; | |
912 | ||
913 | if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST || | |
914 | work->event.event == IW_CM_EVENT_CONNECT_REPLY) && | |
915 | work->event.private_data_len) { | |
715a588f | 916 | ret = copy_private_data(&work->event); |
922a8e9f TT |
917 | if (ret) { |
918 | put_work(work); | |
919 | goto out; | |
920 | } | |
921 | } | |
922 | ||
923 | atomic_inc(&cm_id_priv->refcount); | |
924 | if (list_empty(&cm_id_priv->work_list)) { | |
925 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
926 | queue_work(iwcm_wq, &work->work); | |
927 | } else | |
928 | list_add_tail(&work->list, &cm_id_priv->work_list); | |
929 | out: | |
930 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
931 | return ret; | |
932 | } | |
933 | ||
934 | static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv, | |
935 | struct ib_qp_attr *qp_attr, | |
936 | int *qp_attr_mask) | |
937 | { | |
938 | unsigned long flags; | |
939 | int ret; | |
940 | ||
941 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
942 | switch (cm_id_priv->state) { | |
943 | case IW_CM_STATE_IDLE: | |
944 | case IW_CM_STATE_CONN_SENT: | |
945 | case IW_CM_STATE_CONN_RECV: | |
946 | case IW_CM_STATE_ESTABLISHED: | |
947 | *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS; | |
1ca8d156 | 948 | qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE| |
922a8e9f TT |
949 | IB_ACCESS_REMOTE_READ; |
950 | ret = 0; | |
951 | break; | |
952 | default: | |
953 | ret = -EINVAL; | |
954 | break; | |
955 | } | |
956 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
957 | return ret; | |
958 | } | |
959 | ||
960 | static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv, | |
961 | struct ib_qp_attr *qp_attr, | |
962 | int *qp_attr_mask) | |
963 | { | |
964 | unsigned long flags; | |
965 | int ret; | |
966 | ||
967 | spin_lock_irqsave(&cm_id_priv->lock, flags); | |
968 | switch (cm_id_priv->state) { | |
969 | case IW_CM_STATE_IDLE: | |
970 | case IW_CM_STATE_CONN_SENT: | |
971 | case IW_CM_STATE_CONN_RECV: | |
972 | case IW_CM_STATE_ESTABLISHED: | |
973 | *qp_attr_mask = 0; | |
974 | ret = 0; | |
975 | break; | |
976 | default: | |
977 | ret = -EINVAL; | |
978 | break; | |
979 | } | |
980 | spin_unlock_irqrestore(&cm_id_priv->lock, flags); | |
981 | return ret; | |
982 | } | |
983 | ||
984 | int iw_cm_init_qp_attr(struct iw_cm_id *cm_id, | |
985 | struct ib_qp_attr *qp_attr, | |
986 | int *qp_attr_mask) | |
987 | { | |
988 | struct iwcm_id_private *cm_id_priv; | |
989 | int ret; | |
990 | ||
991 | cm_id_priv = container_of(cm_id, struct iwcm_id_private, id); | |
992 | switch (qp_attr->qp_state) { | |
993 | case IB_QPS_INIT: | |
994 | case IB_QPS_RTR: | |
995 | ret = iwcm_init_qp_init_attr(cm_id_priv, | |
996 | qp_attr, qp_attr_mask); | |
997 | break; | |
998 | case IB_QPS_RTS: | |
999 | ret = iwcm_init_qp_rts_attr(cm_id_priv, | |
1000 | qp_attr, qp_attr_mask); | |
1001 | break; | |
1002 | default: | |
1003 | ret = -EINVAL; | |
1004 | break; | |
1005 | } | |
1006 | return ret; | |
1007 | } | |
1008 | EXPORT_SYMBOL(iw_cm_init_qp_attr); | |
1009 | ||
1010 | static int __init iw_cm_init(void) | |
1011 | { | |
1012 | iwcm_wq = create_singlethread_workqueue("iw_cm_wq"); | |
1013 | if (!iwcm_wq) | |
1014 | return -ENOMEM; | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static void __exit iw_cm_cleanup(void) | |
1020 | { | |
1021 | destroy_workqueue(iwcm_wq); | |
1022 | } | |
1023 | ||
1024 | module_init(iw_cm_init); | |
1025 | module_exit(iw_cm_cleanup); |