iwcm.c 28 KB

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