iwcm.c 33 KB

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