iwcm.c 29 KB

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