iwcm.c 29 KB

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