iwcm.c 33 KB

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