iwcm.c 29 KB

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