iwcm.c 28 KB

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