iwcm.c 33 KB

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