iwcm.c 28 KB

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