iwcm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 connectino 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 in the
  122. * cm_id_priv 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 iwcm_id_private *cm_id_priv,
  126. struct iw_cm_event *event)
  127. {
  128. void *p;
  129. p = kmalloc(event->private_data_len, GFP_ATOMIC);
  130. if (!p)
  131. return -ENOMEM;
  132. memcpy(p, event->private_data, event->private_data_len);
  133. event->private_data = p;
  134. return 0;
  135. }
  136. /*
  137. * Release a reference on cm_id. If the last reference is being removed
  138. * and iw_destroy_cm_id is waiting, wake up the waiting thread.
  139. */
  140. static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
  141. {
  142. int ret = 0;
  143. BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
  144. if (atomic_dec_and_test(&cm_id_priv->refcount)) {
  145. BUG_ON(!list_empty(&cm_id_priv->work_list));
  146. if (waitqueue_active(&cm_id_priv->destroy_comp.wait)) {
  147. BUG_ON(cm_id_priv->state != IW_CM_STATE_DESTROYING);
  148. BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY,
  149. &cm_id_priv->flags));
  150. ret = 1;
  151. }
  152. complete(&cm_id_priv->destroy_comp);
  153. }
  154. return ret;
  155. }
  156. static void add_ref(struct iw_cm_id *cm_id)
  157. {
  158. struct iwcm_id_private *cm_id_priv;
  159. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  160. atomic_inc(&cm_id_priv->refcount);
  161. }
  162. static void rem_ref(struct iw_cm_id *cm_id)
  163. {
  164. struct iwcm_id_private *cm_id_priv;
  165. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  166. iwcm_deref_id(cm_id_priv);
  167. }
  168. static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
  169. struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
  170. iw_cm_handler cm_handler,
  171. void *context)
  172. {
  173. struct iwcm_id_private *cm_id_priv;
  174. cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
  175. if (!cm_id_priv)
  176. return ERR_PTR(-ENOMEM);
  177. cm_id_priv->state = IW_CM_STATE_IDLE;
  178. cm_id_priv->id.device = device;
  179. cm_id_priv->id.cm_handler = cm_handler;
  180. cm_id_priv->id.context = context;
  181. cm_id_priv->id.event_handler = cm_event_handler;
  182. cm_id_priv->id.add_ref = add_ref;
  183. cm_id_priv->id.rem_ref = rem_ref;
  184. spin_lock_init(&cm_id_priv->lock);
  185. atomic_set(&cm_id_priv->refcount, 1);
  186. init_waitqueue_head(&cm_id_priv->connect_wait);
  187. init_completion(&cm_id_priv->destroy_comp);
  188. INIT_LIST_HEAD(&cm_id_priv->work_list);
  189. INIT_LIST_HEAD(&cm_id_priv->work_free_list);
  190. return &cm_id_priv->id;
  191. }
  192. EXPORT_SYMBOL(iw_create_cm_id);
  193. static int iwcm_modify_qp_err(struct ib_qp *qp)
  194. {
  195. struct ib_qp_attr qp_attr;
  196. if (!qp)
  197. return -EINVAL;
  198. qp_attr.qp_state = IB_QPS_ERR;
  199. return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  200. }
  201. /*
  202. * This is really the RDMAC CLOSING state. It is most similar to the
  203. * IB SQD QP state.
  204. */
  205. static int iwcm_modify_qp_sqd(struct ib_qp *qp)
  206. {
  207. struct ib_qp_attr qp_attr;
  208. BUG_ON(qp == NULL);
  209. qp_attr.qp_state = IB_QPS_SQD;
  210. return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  211. }
  212. /*
  213. * CM_ID <-- CLOSING
  214. *
  215. * Block if a passive or active connection is currenlty being processed. Then
  216. * process the event as follows:
  217. * - If we are ESTABLISHED, move to CLOSING and modify the QP state
  218. * based on the abrupt flag
  219. * - If the connection is already in the CLOSING or IDLE state, the peer is
  220. * disconnecting concurrently with us and we've already seen the
  221. * DISCONNECT event -- ignore the request and return 0
  222. * - Disconnect on a listening endpoint returns -EINVAL
  223. */
  224. int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
  225. {
  226. struct iwcm_id_private *cm_id_priv;
  227. unsigned long flags;
  228. int ret = 0;
  229. struct ib_qp *qp = NULL;
  230. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  231. /* Wait if we're currently in a connect or accept downcall */
  232. wait_event(cm_id_priv->connect_wait,
  233. !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
  234. spin_lock_irqsave(&cm_id_priv->lock, flags);
  235. switch (cm_id_priv->state) {
  236. case IW_CM_STATE_ESTABLISHED:
  237. cm_id_priv->state = IW_CM_STATE_CLOSING;
  238. /* QP could be <nul> for user-mode client */
  239. if (cm_id_priv->qp)
  240. qp = cm_id_priv->qp;
  241. else
  242. ret = -EINVAL;
  243. break;
  244. case IW_CM_STATE_LISTEN:
  245. ret = -EINVAL;
  246. break;
  247. case IW_CM_STATE_CLOSING:
  248. /* remote peer closed first */
  249. case IW_CM_STATE_IDLE:
  250. /* accept or connect returned !0 */
  251. break;
  252. case IW_CM_STATE_CONN_RECV:
  253. /*
  254. * App called disconnect before/without calling accept after
  255. * connect_request event delivered.
  256. */
  257. break;
  258. case IW_CM_STATE_CONN_SENT:
  259. /* Can only get here if wait above fails */
  260. default:
  261. BUG();
  262. }
  263. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  264. if (qp) {
  265. if (abrupt)
  266. ret = iwcm_modify_qp_err(qp);
  267. else
  268. ret = iwcm_modify_qp_sqd(qp);
  269. /*
  270. * If both sides are disconnecting the QP could
  271. * already be in ERR or SQD states
  272. */
  273. ret = 0;
  274. }
  275. return ret;
  276. }
  277. EXPORT_SYMBOL(iw_cm_disconnect);
  278. /*
  279. * CM_ID <-- DESTROYING
  280. *
  281. * Clean up all resources associated with the connection and release
  282. * the initial reference taken by iw_create_cm_id.
  283. */
  284. static void destroy_cm_id(struct iw_cm_id *cm_id)
  285. {
  286. struct iwcm_id_private *cm_id_priv;
  287. unsigned long flags;
  288. int ret;
  289. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  290. /*
  291. * Wait if we're currently in a connect or accept downcall. A
  292. * listening endpoint should never block here.
  293. */
  294. wait_event(cm_id_priv->connect_wait,
  295. !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
  296. spin_lock_irqsave(&cm_id_priv->lock, flags);
  297. switch (cm_id_priv->state) {
  298. case IW_CM_STATE_LISTEN:
  299. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  300. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  301. /* destroy the listening endpoint */
  302. ret = cm_id->device->iwcm->destroy_listen(cm_id);
  303. spin_lock_irqsave(&cm_id_priv->lock, flags);
  304. break;
  305. case IW_CM_STATE_ESTABLISHED:
  306. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  307. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  308. /* Abrupt close of the connection */
  309. (void)iwcm_modify_qp_err(cm_id_priv->qp);
  310. spin_lock_irqsave(&cm_id_priv->lock, flags);
  311. break;
  312. case IW_CM_STATE_IDLE:
  313. case IW_CM_STATE_CLOSING:
  314. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  315. break;
  316. case IW_CM_STATE_CONN_RECV:
  317. /*
  318. * App called destroy before/without calling accept after
  319. * receiving connection request event notification.
  320. */
  321. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  322. break;
  323. case IW_CM_STATE_CONN_SENT:
  324. case IW_CM_STATE_DESTROYING:
  325. default:
  326. BUG();
  327. break;
  328. }
  329. if (cm_id_priv->qp) {
  330. cm_id_priv->id.device->iwcm->rem_ref(cm_id_priv->qp);
  331. cm_id_priv->qp = NULL;
  332. }
  333. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  334. (void)iwcm_deref_id(cm_id_priv);
  335. }
  336. /*
  337. * This function is only called by the application thread and cannot
  338. * be called by the event thread. The function will wait for all
  339. * references to be released on the cm_id and then kfree the cm_id
  340. * object.
  341. */
  342. void iw_destroy_cm_id(struct iw_cm_id *cm_id)
  343. {
  344. struct iwcm_id_private *cm_id_priv;
  345. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  346. BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
  347. destroy_cm_id(cm_id);
  348. wait_for_completion(&cm_id_priv->destroy_comp);
  349. dealloc_work_entries(cm_id_priv);
  350. kfree(cm_id_priv);
  351. }
  352. EXPORT_SYMBOL(iw_destroy_cm_id);
  353. /*
  354. * CM_ID <-- LISTEN
  355. *
  356. * Start listening for connect requests. Generates one CONNECT_REQUEST
  357. * event for each inbound connect request.
  358. */
  359. int iw_cm_listen(struct iw_cm_id *cm_id, int backlog)
  360. {
  361. struct iwcm_id_private *cm_id_priv;
  362. unsigned long flags;
  363. int ret = 0;
  364. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  365. ret = alloc_work_entries(cm_id_priv, backlog);
  366. if (ret)
  367. return ret;
  368. spin_lock_irqsave(&cm_id_priv->lock, flags);
  369. switch (cm_id_priv->state) {
  370. case IW_CM_STATE_IDLE:
  371. cm_id_priv->state = IW_CM_STATE_LISTEN;
  372. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  373. ret = cm_id->device->iwcm->create_listen(cm_id, backlog);
  374. if (ret)
  375. cm_id_priv->state = IW_CM_STATE_IDLE;
  376. spin_lock_irqsave(&cm_id_priv->lock, flags);
  377. break;
  378. default:
  379. ret = -EINVAL;
  380. }
  381. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL(iw_cm_listen);
  385. /*
  386. * CM_ID <-- IDLE
  387. *
  388. * Rejects an inbound connection request. No events are generated.
  389. */
  390. int iw_cm_reject(struct iw_cm_id *cm_id,
  391. const void *private_data,
  392. u8 private_data_len)
  393. {
  394. struct iwcm_id_private *cm_id_priv;
  395. unsigned long flags;
  396. int ret;
  397. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  398. set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  399. spin_lock_irqsave(&cm_id_priv->lock, flags);
  400. if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
  401. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  402. clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  403. wake_up_all(&cm_id_priv->connect_wait);
  404. return -EINVAL;
  405. }
  406. cm_id_priv->state = IW_CM_STATE_IDLE;
  407. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  408. ret = cm_id->device->iwcm->reject(cm_id, private_data,
  409. private_data_len);
  410. clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  411. wake_up_all(&cm_id_priv->connect_wait);
  412. return ret;
  413. }
  414. EXPORT_SYMBOL(iw_cm_reject);
  415. /*
  416. * CM_ID <-- ESTABLISHED
  417. *
  418. * Accepts an inbound connection request and generates an ESTABLISHED
  419. * event. Callers of iw_cm_disconnect and iw_destroy_cm_id will block
  420. * until the ESTABLISHED event is received from the provider.
  421. */
  422. int iw_cm_accept(struct iw_cm_id *cm_id,
  423. struct iw_cm_conn_param *iw_param)
  424. {
  425. struct iwcm_id_private *cm_id_priv;
  426. struct ib_qp *qp;
  427. unsigned long flags;
  428. int ret;
  429. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  430. set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  431. spin_lock_irqsave(&cm_id_priv->lock, flags);
  432. if (cm_id_priv->state != IW_CM_STATE_CONN_RECV) {
  433. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  434. clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  435. wake_up_all(&cm_id_priv->connect_wait);
  436. return -EINVAL;
  437. }
  438. /* Get the ib_qp given the QPN */
  439. qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
  440. if (!qp) {
  441. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  442. return -EINVAL;
  443. }
  444. cm_id->device->iwcm->add_ref(qp);
  445. cm_id_priv->qp = qp;
  446. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  447. ret = cm_id->device->iwcm->accept(cm_id, iw_param);
  448. if (ret) {
  449. /* An error on accept precludes provider events */
  450. BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV);
  451. cm_id_priv->state = IW_CM_STATE_IDLE;
  452. spin_lock_irqsave(&cm_id_priv->lock, flags);
  453. if (cm_id_priv->qp) {
  454. cm_id->device->iwcm->rem_ref(qp);
  455. cm_id_priv->qp = NULL;
  456. }
  457. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  458. clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  459. wake_up_all(&cm_id_priv->connect_wait);
  460. }
  461. return ret;
  462. }
  463. EXPORT_SYMBOL(iw_cm_accept);
  464. /*
  465. * Active Side: CM_ID <-- CONN_SENT
  466. *
  467. * If successful, results in the generation of a CONNECT_REPLY
  468. * event. iw_cm_disconnect and iw_cm_destroy will block until the
  469. * CONNECT_REPLY event is received from the provider.
  470. */
  471. int iw_cm_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
  472. {
  473. struct iwcm_id_private *cm_id_priv;
  474. int ret = 0;
  475. unsigned long flags;
  476. struct ib_qp *qp;
  477. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  478. ret = alloc_work_entries(cm_id_priv, 4);
  479. if (ret)
  480. return ret;
  481. set_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  482. spin_lock_irqsave(&cm_id_priv->lock, flags);
  483. if (cm_id_priv->state != IW_CM_STATE_IDLE) {
  484. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  485. clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  486. wake_up_all(&cm_id_priv->connect_wait);
  487. return -EINVAL;
  488. }
  489. /* Get the ib_qp given the QPN */
  490. qp = cm_id->device->iwcm->get_qp(cm_id->device, iw_param->qpn);
  491. if (!qp) {
  492. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  493. return -EINVAL;
  494. }
  495. cm_id->device->iwcm->add_ref(qp);
  496. cm_id_priv->qp = qp;
  497. cm_id_priv->state = IW_CM_STATE_CONN_SENT;
  498. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  499. ret = cm_id->device->iwcm->connect(cm_id, iw_param);
  500. if (ret) {
  501. spin_lock_irqsave(&cm_id_priv->lock, flags);
  502. if (cm_id_priv->qp) {
  503. cm_id->device->iwcm->rem_ref(qp);
  504. cm_id_priv->qp = NULL;
  505. }
  506. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  507. BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_SENT);
  508. cm_id_priv->state = IW_CM_STATE_IDLE;
  509. clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags);
  510. wake_up_all(&cm_id_priv->connect_wait);
  511. }
  512. return ret;
  513. }
  514. EXPORT_SYMBOL(iw_cm_connect);
  515. /*
  516. * Passive Side: new CM_ID <-- CONN_RECV
  517. *
  518. * Handles an inbound connect request. The function creates a new
  519. * iw_cm_id to represent the new connection and inherits the client
  520. * callback function and other attributes from the listening parent.
  521. *
  522. * The work item contains a pointer to the listen_cm_id and the event. The
  523. * listen_cm_id contains the client cm_handler, context and
  524. * device. These are copied when the device is cloned. The event
  525. * contains the new four tuple.
  526. *
  527. * An error on the child should not affect the parent, so this
  528. * function does not return a value.
  529. */
  530. static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
  531. struct iw_cm_event *iw_event)
  532. {
  533. unsigned long flags;
  534. struct iw_cm_id *cm_id;
  535. struct iwcm_id_private *cm_id_priv;
  536. int ret;
  537. /*
  538. * The provider should never generate a connection request
  539. * event with a bad status.
  540. */
  541. BUG_ON(iw_event->status);
  542. /*
  543. * We could be destroying the listening id. If so, ignore this
  544. * upcall.
  545. */
  546. spin_lock_irqsave(&listen_id_priv->lock, flags);
  547. if (listen_id_priv->state != IW_CM_STATE_LISTEN) {
  548. spin_unlock_irqrestore(&listen_id_priv->lock, flags);
  549. return;
  550. }
  551. spin_unlock_irqrestore(&listen_id_priv->lock, flags);
  552. cm_id = iw_create_cm_id(listen_id_priv->id.device,
  553. listen_id_priv->id.cm_handler,
  554. listen_id_priv->id.context);
  555. /* If the cm_id could not be created, ignore the request */
  556. if (IS_ERR(cm_id))
  557. return;
  558. cm_id->provider_data = iw_event->provider_data;
  559. cm_id->local_addr = iw_event->local_addr;
  560. cm_id->remote_addr = iw_event->remote_addr;
  561. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  562. cm_id_priv->state = IW_CM_STATE_CONN_RECV;
  563. ret = alloc_work_entries(cm_id_priv, 3);
  564. if (ret) {
  565. iw_cm_reject(cm_id, NULL, 0);
  566. iw_destroy_cm_id(cm_id);
  567. return;
  568. }
  569. /* Call the client CM handler */
  570. ret = cm_id->cm_handler(cm_id, iw_event);
  571. if (ret) {
  572. set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
  573. destroy_cm_id(cm_id);
  574. if (atomic_read(&cm_id_priv->refcount)==0)
  575. kfree(cm_id);
  576. }
  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 = 0;
  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 = 0;
  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(void *arg)
  734. {
  735. struct iwcm_work *work = arg, lwork;
  736. struct iwcm_id_private *cm_id_priv = work->cm_id;
  737. unsigned long flags;
  738. int empty;
  739. int ret = 0;
  740. spin_lock_irqsave(&cm_id_priv->lock, flags);
  741. empty = list_empty(&cm_id_priv->work_list);
  742. while (!empty) {
  743. work = list_entry(cm_id_priv->work_list.next,
  744. struct iwcm_work, list);
  745. list_del_init(&work->list);
  746. empty = list_empty(&cm_id_priv->work_list);
  747. lwork = *work;
  748. put_work(work);
  749. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  750. ret = process_event(cm_id_priv, &work->event);
  751. if (ret) {
  752. set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
  753. destroy_cm_id(&cm_id_priv->id);
  754. }
  755. BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
  756. if (iwcm_deref_id(cm_id_priv))
  757. return;
  758. if (atomic_read(&cm_id_priv->refcount)==0 &&
  759. test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags)) {
  760. dealloc_work_entries(cm_id_priv);
  761. kfree(cm_id_priv);
  762. return;
  763. }
  764. spin_lock_irqsave(&cm_id_priv->lock, flags);
  765. }
  766. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  767. }
  768. /*
  769. * This function is called on interrupt context. Schedule events on
  770. * the iwcm_wq thread to allow callback functions to downcall into
  771. * the CM and/or block. Events are queued to a per-CM_ID
  772. * work_list. If this is the first event on the work_list, the work
  773. * element is also queued on the iwcm_wq thread.
  774. *
  775. * Each event holds a reference on the cm_id. Until the last posted
  776. * event has been delivered and processed, the cm_id cannot be
  777. * deleted.
  778. *
  779. * Returns:
  780. * 0 - the event was handled.
  781. * -ENOMEM - the event was not handled due to lack of resources.
  782. */
  783. static int cm_event_handler(struct iw_cm_id *cm_id,
  784. struct iw_cm_event *iw_event)
  785. {
  786. struct iwcm_work *work;
  787. struct iwcm_id_private *cm_id_priv;
  788. unsigned long flags;
  789. int ret = 0;
  790. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  791. spin_lock_irqsave(&cm_id_priv->lock, flags);
  792. work = get_work(cm_id_priv);
  793. if (!work) {
  794. ret = -ENOMEM;
  795. goto out;
  796. }
  797. INIT_WORK(&work->work, cm_work_handler, work);
  798. work->cm_id = cm_id_priv;
  799. work->event = *iw_event;
  800. if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
  801. work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
  802. work->event.private_data_len) {
  803. ret = copy_private_data(cm_id_priv, &work->event);
  804. if (ret) {
  805. put_work(work);
  806. goto out;
  807. }
  808. }
  809. atomic_inc(&cm_id_priv->refcount);
  810. if (list_empty(&cm_id_priv->work_list)) {
  811. list_add_tail(&work->list, &cm_id_priv->work_list);
  812. queue_work(iwcm_wq, &work->work);
  813. } else
  814. list_add_tail(&work->list, &cm_id_priv->work_list);
  815. out:
  816. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  817. return ret;
  818. }
  819. static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
  820. struct ib_qp_attr *qp_attr,
  821. int *qp_attr_mask)
  822. {
  823. unsigned long flags;
  824. int ret;
  825. spin_lock_irqsave(&cm_id_priv->lock, flags);
  826. switch (cm_id_priv->state) {
  827. case IW_CM_STATE_IDLE:
  828. case IW_CM_STATE_CONN_SENT:
  829. case IW_CM_STATE_CONN_RECV:
  830. case IW_CM_STATE_ESTABLISHED:
  831. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
  832. qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
  833. IB_ACCESS_REMOTE_WRITE|
  834. IB_ACCESS_REMOTE_READ;
  835. ret = 0;
  836. break;
  837. default:
  838. ret = -EINVAL;
  839. break;
  840. }
  841. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  842. return ret;
  843. }
  844. static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
  845. struct ib_qp_attr *qp_attr,
  846. int *qp_attr_mask)
  847. {
  848. unsigned long flags;
  849. int ret;
  850. spin_lock_irqsave(&cm_id_priv->lock, flags);
  851. switch (cm_id_priv->state) {
  852. case IW_CM_STATE_IDLE:
  853. case IW_CM_STATE_CONN_SENT:
  854. case IW_CM_STATE_CONN_RECV:
  855. case IW_CM_STATE_ESTABLISHED:
  856. *qp_attr_mask = 0;
  857. ret = 0;
  858. break;
  859. default:
  860. ret = -EINVAL;
  861. break;
  862. }
  863. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  864. return ret;
  865. }
  866. int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
  867. struct ib_qp_attr *qp_attr,
  868. int *qp_attr_mask)
  869. {
  870. struct iwcm_id_private *cm_id_priv;
  871. int ret;
  872. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  873. switch (qp_attr->qp_state) {
  874. case IB_QPS_INIT:
  875. case IB_QPS_RTR:
  876. ret = iwcm_init_qp_init_attr(cm_id_priv,
  877. qp_attr, qp_attr_mask);
  878. break;
  879. case IB_QPS_RTS:
  880. ret = iwcm_init_qp_rts_attr(cm_id_priv,
  881. qp_attr, qp_attr_mask);
  882. break;
  883. default:
  884. ret = -EINVAL;
  885. break;
  886. }
  887. return ret;
  888. }
  889. EXPORT_SYMBOL(iw_cm_init_qp_attr);
  890. static int __init iw_cm_init(void)
  891. {
  892. iwcm_wq = create_singlethread_workqueue("iw_cm_wq");
  893. if (!iwcm_wq)
  894. return -ENOMEM;
  895. return 0;
  896. }
  897. static void __exit iw_cm_cleanup(void)
  898. {
  899. destroy_workqueue(iwcm_wq);
  900. }
  901. module_init(iw_cm_init);
  902. module_exit(iw_cm_cleanup);