iwcm.c 28 KB

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