iwcm.c 28 KB

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