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