iwcm.c 29 KB

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