iwcm.c 33 KB

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