iwcm.c 33 KB

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