iwcm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. /*
  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_entry(cm_id_priv->work_free_list.next, 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(sizeof(struct iwcm_work), GFP_KERNEL);
  156. if (!work) {
  157. dealloc_work_entries(cm_id_priv);
  158. return -ENOMEM;
  159. }
  160. work->cm_id = cm_id_priv;
  161. INIT_LIST_HEAD(&work->list);
  162. put_work(work);
  163. }
  164. return 0;
  165. }
  166. /*
  167. * Save private data from incoming connection requests to
  168. * iw_cm_event, so the low level driver doesn't have to. Adjust
  169. * the event ptr to point to the local copy.
  170. */
  171. static int copy_private_data(struct iw_cm_event *event)
  172. {
  173. void *p;
  174. p = kmemdup(event->private_data, event->private_data_len, GFP_ATOMIC);
  175. if (!p)
  176. return -ENOMEM;
  177. event->private_data = p;
  178. return 0;
  179. }
  180. static void free_cm_id(struct iwcm_id_private *cm_id_priv)
  181. {
  182. dealloc_work_entries(cm_id_priv);
  183. kfree(cm_id_priv);
  184. }
  185. /*
  186. * Release a reference on cm_id. If the last reference is being
  187. * released, free the cm_id and return 1.
  188. */
  189. static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
  190. {
  191. BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
  192. if (atomic_dec_and_test(&cm_id_priv->refcount)) {
  193. BUG_ON(!list_empty(&cm_id_priv->work_list));
  194. free_cm_id(cm_id_priv);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. static void add_ref(struct iw_cm_id *cm_id)
  200. {
  201. struct iwcm_id_private *cm_id_priv;
  202. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  203. atomic_inc(&cm_id_priv->refcount);
  204. }
  205. static void rem_ref(struct iw_cm_id *cm_id)
  206. {
  207. struct iwcm_id_private *cm_id_priv;
  208. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  209. (void)iwcm_deref_id(cm_id_priv);
  210. }
  211. static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
  212. struct iw_cm_id *iw_create_cm_id(struct ib_device *device,
  213. iw_cm_handler cm_handler,
  214. void *context)
  215. {
  216. struct iwcm_id_private *cm_id_priv;
  217. cm_id_priv = kzalloc(sizeof(*cm_id_priv), GFP_KERNEL);
  218. if (!cm_id_priv)
  219. return ERR_PTR(-ENOMEM);
  220. cm_id_priv->state = IW_CM_STATE_IDLE;
  221. cm_id_priv->id.device = device;
  222. cm_id_priv->id.cm_handler = cm_handler;
  223. cm_id_priv->id.context = context;
  224. cm_id_priv->id.event_handler = cm_event_handler;
  225. cm_id_priv->id.add_ref = add_ref;
  226. cm_id_priv->id.rem_ref = rem_ref;
  227. spin_lock_init(&cm_id_priv->lock);
  228. atomic_set(&cm_id_priv->refcount, 1);
  229. init_waitqueue_head(&cm_id_priv->connect_wait);
  230. init_completion(&cm_id_priv->destroy_comp);
  231. INIT_LIST_HEAD(&cm_id_priv->work_list);
  232. INIT_LIST_HEAD(&cm_id_priv->work_free_list);
  233. return &cm_id_priv->id;
  234. }
  235. EXPORT_SYMBOL(iw_create_cm_id);
  236. static int iwcm_modify_qp_err(struct ib_qp *qp)
  237. {
  238. struct ib_qp_attr qp_attr;
  239. if (!qp)
  240. return -EINVAL;
  241. qp_attr.qp_state = IB_QPS_ERR;
  242. return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  243. }
  244. /*
  245. * This is really the RDMAC CLOSING state. It is most similar to the
  246. * IB SQD QP state.
  247. */
  248. static int iwcm_modify_qp_sqd(struct ib_qp *qp)
  249. {
  250. struct ib_qp_attr qp_attr;
  251. BUG_ON(qp == NULL);
  252. qp_attr.qp_state = IB_QPS_SQD;
  253. return ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  254. }
  255. /*
  256. * CM_ID <-- CLOSING
  257. *
  258. * Block if a passive or active connection is currently being processed. Then
  259. * process the event as follows:
  260. * - If we are ESTABLISHED, move to CLOSING and modify the QP state
  261. * based on the abrupt flag
  262. * - If the connection is already in the CLOSING or IDLE state, the peer is
  263. * disconnecting concurrently with us and we've already seen the
  264. * DISCONNECT event -- ignore the request and return 0
  265. * - Disconnect on a listening endpoint returns -EINVAL
  266. */
  267. int iw_cm_disconnect(struct iw_cm_id *cm_id, int abrupt)
  268. {
  269. struct iwcm_id_private *cm_id_priv;
  270. unsigned long flags;
  271. int ret = 0;
  272. struct ib_qp *qp = NULL;
  273. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  274. /* Wait if we're currently in a connect or accept downcall */
  275. wait_event(cm_id_priv->connect_wait,
  276. !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
  277. spin_lock_irqsave(&cm_id_priv->lock, flags);
  278. switch (cm_id_priv->state) {
  279. case IW_CM_STATE_ESTABLISHED:
  280. cm_id_priv->state = IW_CM_STATE_CLOSING;
  281. /* QP could be <nul> for user-mode client */
  282. if (cm_id_priv->qp)
  283. qp = cm_id_priv->qp;
  284. else
  285. ret = -EINVAL;
  286. break;
  287. case IW_CM_STATE_LISTEN:
  288. ret = -EINVAL;
  289. break;
  290. case IW_CM_STATE_CLOSING:
  291. /* remote peer closed first */
  292. case IW_CM_STATE_IDLE:
  293. /* accept or connect returned !0 */
  294. break;
  295. case IW_CM_STATE_CONN_RECV:
  296. /*
  297. * App called disconnect before/without calling accept after
  298. * connect_request event delivered.
  299. */
  300. break;
  301. case IW_CM_STATE_CONN_SENT:
  302. /* Can only get here if wait above fails */
  303. default:
  304. BUG();
  305. }
  306. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  307. if (qp) {
  308. if (abrupt)
  309. ret = iwcm_modify_qp_err(qp);
  310. else
  311. ret = iwcm_modify_qp_sqd(qp);
  312. /*
  313. * If both sides are disconnecting the QP could
  314. * already be in ERR or SQD states
  315. */
  316. ret = 0;
  317. }
  318. return ret;
  319. }
  320. EXPORT_SYMBOL(iw_cm_disconnect);
  321. /*
  322. * CM_ID <-- DESTROYING
  323. *
  324. * Clean up all resources associated with the connection and release
  325. * the initial reference taken by iw_create_cm_id.
  326. */
  327. static void destroy_cm_id(struct iw_cm_id *cm_id)
  328. {
  329. struct iwcm_id_private *cm_id_priv;
  330. struct ib_qp *qp;
  331. unsigned long flags;
  332. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  333. /*
  334. * Wait if we're currently in a connect or accept downcall. A
  335. * listening endpoint should never block here.
  336. */
  337. wait_event(cm_id_priv->connect_wait,
  338. !test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
  339. /*
  340. * Since we're deleting the cm_id, drop any events that
  341. * might arrive before the last dereference.
  342. */
  343. set_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags);
  344. spin_lock_irqsave(&cm_id_priv->lock, flags);
  345. qp = cm_id_priv->qp;
  346. cm_id_priv->qp = NULL;
  347. switch (cm_id_priv->state) {
  348. case IW_CM_STATE_LISTEN:
  349. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  350. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  351. /* destroy the listening endpoint */
  352. cm_id->device->ops.iw_destroy_listen(cm_id);
  353. spin_lock_irqsave(&cm_id_priv->lock, flags);
  354. break;
  355. case IW_CM_STATE_ESTABLISHED:
  356. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  357. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  358. /* Abrupt close of the connection */
  359. (void)iwcm_modify_qp_err(qp);
  360. spin_lock_irqsave(&cm_id_priv->lock, flags);
  361. break;
  362. case IW_CM_STATE_IDLE:
  363. case IW_CM_STATE_CLOSING:
  364. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  365. break;
  366. case IW_CM_STATE_CONN_RECV:
  367. /*
  368. * App called destroy before/without calling accept after
  369. * receiving connection request event notification or
  370. * returned non zero from the event callback function.
  371. * In either case, must tell the provider to reject.
  372. */
  373. cm_id_priv->state = IW_CM_STATE_DESTROYING;
  374. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  375. cm_id->device->ops.iw_reject(cm_id, NULL, 0);
  376. spin_lock_irqsave(&cm_id_priv->lock, flags);
  377. break;
  378. case IW_CM_STATE_CONN_SENT:
  379. case IW_CM_STATE_DESTROYING:
  380. default:
  381. BUG();
  382. break;
  383. }
  384. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  385. if (qp)
  386. cm_id_priv->id.device->ops.iw_rem_ref(qp);
  387. if (cm_id->mapped) {
  388. iwpm_remove_mapinfo(&cm_id->local_addr, &cm_id->m_local_addr);
  389. iwpm_remove_mapping(&cm_id->local_addr, RDMA_NL_IWCM);
  390. }
  391. (void)iwcm_deref_id(cm_id_priv);
  392. }
  393. /*
  394. * This function is only called by the application thread and cannot
  395. * be called by the event thread. The function will wait for all
  396. * references to be released on the cm_id and then kfree the cm_id
  397. * object.
  398. */
  399. void iw_destroy_cm_id(struct iw_cm_id *cm_id)
  400. {
  401. destroy_cm_id(cm_id);
  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 empty;
  903. int ret = 0;
  904. spin_lock_irqsave(&cm_id_priv->lock, flags);
  905. empty = list_empty(&cm_id_priv->work_list);
  906. while (!empty) {
  907. work = list_entry(cm_id_priv->work_list.next,
  908. struct iwcm_work, list);
  909. list_del_init(&work->list);
  910. empty = list_empty(&cm_id_priv->work_list);
  911. levent = work->event;
  912. put_work(work);
  913. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  914. if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
  915. ret = process_event(cm_id_priv, &levent);
  916. if (ret)
  917. destroy_cm_id(&cm_id_priv->id);
  918. } else
  919. pr_debug("dropping event %d\n", levent.event);
  920. if (iwcm_deref_id(cm_id_priv))
  921. return;
  922. if (empty)
  923. return;
  924. spin_lock_irqsave(&cm_id_priv->lock, flags);
  925. }
  926. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  927. }
  928. /*
  929. * This function is called on interrupt context. Schedule events on
  930. * the iwcm_wq thread to allow callback functions to downcall into
  931. * the CM and/or block. Events are queued to a per-CM_ID
  932. * work_list. If this is the first event on the work_list, the work
  933. * element is also queued on the iwcm_wq thread.
  934. *
  935. * Each event holds a reference on the cm_id. Until the last posted
  936. * event has been delivered and processed, the cm_id cannot be
  937. * deleted.
  938. *
  939. * Returns:
  940. * 0 - the event was handled.
  941. * -ENOMEM - the event was not handled due to lack of resources.
  942. */
  943. static int cm_event_handler(struct iw_cm_id *cm_id,
  944. struct iw_cm_event *iw_event)
  945. {
  946. struct iwcm_work *work;
  947. struct iwcm_id_private *cm_id_priv;
  948. unsigned long flags;
  949. int ret = 0;
  950. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  951. spin_lock_irqsave(&cm_id_priv->lock, flags);
  952. work = get_work(cm_id_priv);
  953. if (!work) {
  954. ret = -ENOMEM;
  955. goto out;
  956. }
  957. INIT_WORK(&work->work, cm_work_handler);
  958. work->cm_id = cm_id_priv;
  959. work->event = *iw_event;
  960. if ((work->event.event == IW_CM_EVENT_CONNECT_REQUEST ||
  961. work->event.event == IW_CM_EVENT_CONNECT_REPLY) &&
  962. work->event.private_data_len) {
  963. ret = copy_private_data(&work->event);
  964. if (ret) {
  965. put_work(work);
  966. goto out;
  967. }
  968. }
  969. atomic_inc(&cm_id_priv->refcount);
  970. if (list_empty(&cm_id_priv->work_list)) {
  971. list_add_tail(&work->list, &cm_id_priv->work_list);
  972. queue_work(iwcm_wq, &work->work);
  973. } else
  974. list_add_tail(&work->list, &cm_id_priv->work_list);
  975. out:
  976. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  977. return ret;
  978. }
  979. static int iwcm_init_qp_init_attr(struct iwcm_id_private *cm_id_priv,
  980. struct ib_qp_attr *qp_attr,
  981. int *qp_attr_mask)
  982. {
  983. unsigned long flags;
  984. int ret;
  985. spin_lock_irqsave(&cm_id_priv->lock, flags);
  986. switch (cm_id_priv->state) {
  987. case IW_CM_STATE_IDLE:
  988. case IW_CM_STATE_CONN_SENT:
  989. case IW_CM_STATE_CONN_RECV:
  990. case IW_CM_STATE_ESTABLISHED:
  991. *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
  992. qp_attr->qp_access_flags = IB_ACCESS_REMOTE_WRITE|
  993. IB_ACCESS_REMOTE_READ;
  994. ret = 0;
  995. break;
  996. default:
  997. ret = -EINVAL;
  998. break;
  999. }
  1000. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1001. return ret;
  1002. }
  1003. static int iwcm_init_qp_rts_attr(struct iwcm_id_private *cm_id_priv,
  1004. struct ib_qp_attr *qp_attr,
  1005. int *qp_attr_mask)
  1006. {
  1007. unsigned long flags;
  1008. int ret;
  1009. spin_lock_irqsave(&cm_id_priv->lock, flags);
  1010. switch (cm_id_priv->state) {
  1011. case IW_CM_STATE_IDLE:
  1012. case IW_CM_STATE_CONN_SENT:
  1013. case IW_CM_STATE_CONN_RECV:
  1014. case IW_CM_STATE_ESTABLISHED:
  1015. *qp_attr_mask = 0;
  1016. ret = 0;
  1017. break;
  1018. default:
  1019. ret = -EINVAL;
  1020. break;
  1021. }
  1022. spin_unlock_irqrestore(&cm_id_priv->lock, flags);
  1023. return ret;
  1024. }
  1025. int iw_cm_init_qp_attr(struct iw_cm_id *cm_id,
  1026. struct ib_qp_attr *qp_attr,
  1027. int *qp_attr_mask)
  1028. {
  1029. struct iwcm_id_private *cm_id_priv;
  1030. int ret;
  1031. cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
  1032. switch (qp_attr->qp_state) {
  1033. case IB_QPS_INIT:
  1034. case IB_QPS_RTR:
  1035. ret = iwcm_init_qp_init_attr(cm_id_priv,
  1036. qp_attr, qp_attr_mask);
  1037. break;
  1038. case IB_QPS_RTS:
  1039. ret = iwcm_init_qp_rts_attr(cm_id_priv,
  1040. qp_attr, qp_attr_mask);
  1041. break;
  1042. default:
  1043. ret = -EINVAL;
  1044. break;
  1045. }
  1046. return ret;
  1047. }
  1048. EXPORT_SYMBOL(iw_cm_init_qp_attr);
  1049. static int __init iw_cm_init(void)
  1050. {
  1051. int ret;
  1052. ret = iwpm_init(RDMA_NL_IWCM);
  1053. if (ret)
  1054. return ret;
  1055. iwcm_wq = alloc_ordered_workqueue("iw_cm_wq", 0);
  1056. if (!iwcm_wq)
  1057. goto err_alloc;
  1058. iwcm_ctl_table_hdr = register_net_sysctl(&init_net, "net/iw_cm",
  1059. iwcm_ctl_table);
  1060. if (!iwcm_ctl_table_hdr) {
  1061. pr_err("iw_cm: couldn't register sysctl paths\n");
  1062. goto err_sysctl;
  1063. }
  1064. rdma_nl_register(RDMA_NL_IWCM, iwcm_nl_cb_table);
  1065. return 0;
  1066. err_sysctl:
  1067. destroy_workqueue(iwcm_wq);
  1068. err_alloc:
  1069. iwpm_exit(RDMA_NL_IWCM);
  1070. return -ENOMEM;
  1071. }
  1072. static void __exit iw_cm_cleanup(void)
  1073. {
  1074. rdma_nl_unregister(RDMA_NL_IWCM);
  1075. unregister_net_sysctl_table(iwcm_ctl_table_hdr);
  1076. destroy_workqueue(iwcm_wq);
  1077. iwpm_exit(RDMA_NL_IWCM);
  1078. }
  1079. MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_IWCM, 2);
  1080. module_init(iw_cm_init);
  1081. module_exit(iw_cm_cleanup);