iw_cm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/in.h>
  35. #include <linux/vmalloc.h>
  36. #include "rds.h"
  37. #include "iw.h"
  38. /*
  39. * Set the selected protocol version
  40. */
  41. static void rds_iw_set_protocol(struct rds_connection *conn, unsigned int version)
  42. {
  43. conn->c_version = version;
  44. }
  45. /*
  46. * Set up flow control
  47. */
  48. static void rds_iw_set_flow_control(struct rds_connection *conn, u32 credits)
  49. {
  50. struct rds_iw_connection *ic = conn->c_transport_data;
  51. if (rds_iw_sysctl_flow_control && credits != 0) {
  52. /* We're doing flow control */
  53. ic->i_flowctl = 1;
  54. rds_iw_send_add_credits(conn, credits);
  55. } else {
  56. ic->i_flowctl = 0;
  57. }
  58. }
  59. /*
  60. * Connection established.
  61. * We get here for both outgoing and incoming connection.
  62. */
  63. void rds_iw_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
  64. {
  65. const struct rds_iw_connect_private *dp = NULL;
  66. struct rds_iw_connection *ic = conn->c_transport_data;
  67. struct rds_iw_device *rds_iwdev;
  68. int err;
  69. if (event->param.conn.private_data_len) {
  70. dp = event->param.conn.private_data;
  71. rds_iw_set_protocol(conn,
  72. RDS_PROTOCOL(dp->dp_protocol_major,
  73. dp->dp_protocol_minor));
  74. rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  75. }
  76. /* update ib_device with this local ipaddr & conn */
  77. rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
  78. err = rds_iw_update_cm_id(rds_iwdev, ic->i_cm_id);
  79. if (err)
  80. printk(KERN_ERR "rds_iw_update_ipaddr failed (%d)\n", err);
  81. err = rds_iw_add_conn(rds_iwdev, conn);
  82. if (err)
  83. printk(KERN_ERR "rds_iw_add_conn failed (%d)\n", err);
  84. /* If the peer gave us the last packet it saw, process this as if
  85. * we had received a regular ACK. */
  86. if (dp && dp->dp_ack_seq)
  87. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  88. printk(KERN_NOTICE "RDS/IW: connected to %pI4<->%pI4 version %u.%u%s\n",
  89. &conn->c_laddr, &conn->c_faddr,
  90. RDS_PROTOCOL_MAJOR(conn->c_version),
  91. RDS_PROTOCOL_MINOR(conn->c_version),
  92. ic->i_flowctl ? ", flow control" : "");
  93. rds_connect_complete(conn);
  94. }
  95. static void rds_iw_cm_fill_conn_param(struct rds_connection *conn,
  96. struct rdma_conn_param *conn_param,
  97. struct rds_iw_connect_private *dp,
  98. u32 protocol_version)
  99. {
  100. struct rds_iw_connection *ic = conn->c_transport_data;
  101. memset(conn_param, 0, sizeof(struct rdma_conn_param));
  102. /* XXX tune these? */
  103. conn_param->responder_resources = 1;
  104. conn_param->initiator_depth = 1;
  105. if (dp) {
  106. memset(dp, 0, sizeof(*dp));
  107. dp->dp_saddr = conn->c_laddr;
  108. dp->dp_daddr = conn->c_faddr;
  109. dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
  110. dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
  111. dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IW_SUPPORTED_PROTOCOLS);
  112. dp->dp_ack_seq = rds_iw_piggyb_ack(ic);
  113. /* Advertise flow control */
  114. if (ic->i_flowctl) {
  115. unsigned int credits;
  116. credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
  117. dp->dp_credit = cpu_to_be32(credits);
  118. atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
  119. }
  120. conn_param->private_data = dp;
  121. conn_param->private_data_len = sizeof(*dp);
  122. }
  123. }
  124. static void rds_iw_cq_event_handler(struct ib_event *event, void *data)
  125. {
  126. rdsdebug("event %u data %p\n", event->event, data);
  127. }
  128. static void rds_iw_qp_event_handler(struct ib_event *event, void *data)
  129. {
  130. struct rds_connection *conn = data;
  131. struct rds_iw_connection *ic = conn->c_transport_data;
  132. rdsdebug("conn %p ic %p event %u\n", conn, ic, event->event);
  133. switch (event->event) {
  134. case IB_EVENT_COMM_EST:
  135. rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
  136. break;
  137. case IB_EVENT_QP_REQ_ERR:
  138. case IB_EVENT_QP_FATAL:
  139. default:
  140. rds_iw_conn_error(conn, "RDS/IW: Fatal QP Event %u - connection %pI4->%pI4...reconnecting\n",
  141. event->event, &conn->c_laddr,
  142. &conn->c_faddr);
  143. break;
  144. }
  145. }
  146. /*
  147. * Create a QP
  148. */
  149. static int rds_iw_init_qp_attrs(struct ib_qp_init_attr *attr,
  150. struct rds_iw_device *rds_iwdev,
  151. struct rds_iw_work_ring *send_ring,
  152. void (*send_cq_handler)(struct ib_cq *, void *),
  153. struct rds_iw_work_ring *recv_ring,
  154. void (*recv_cq_handler)(struct ib_cq *, void *),
  155. void *context)
  156. {
  157. struct ib_device *dev = rds_iwdev->dev;
  158. unsigned int send_size, recv_size;
  159. int ret;
  160. /* The offset of 1 is to accomodate the additional ACK WR. */
  161. send_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_send_wr + 1);
  162. recv_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_recv_wr + 1);
  163. rds_iw_ring_resize(send_ring, send_size - 1);
  164. rds_iw_ring_resize(recv_ring, recv_size - 1);
  165. memset(attr, 0, sizeof(*attr));
  166. attr->event_handler = rds_iw_qp_event_handler;
  167. attr->qp_context = context;
  168. attr->cap.max_send_wr = send_size;
  169. attr->cap.max_recv_wr = recv_size;
  170. attr->cap.max_send_sge = rds_iwdev->max_sge;
  171. attr->cap.max_recv_sge = RDS_IW_RECV_SGE;
  172. attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  173. attr->qp_type = IB_QPT_RC;
  174. attr->send_cq = ib_create_cq(dev, send_cq_handler,
  175. rds_iw_cq_event_handler,
  176. context, send_size, 0);
  177. if (IS_ERR(attr->send_cq)) {
  178. ret = PTR_ERR(attr->send_cq);
  179. attr->send_cq = NULL;
  180. rdsdebug("ib_create_cq send failed: %d\n", ret);
  181. goto out;
  182. }
  183. attr->recv_cq = ib_create_cq(dev, recv_cq_handler,
  184. rds_iw_cq_event_handler,
  185. context, recv_size, 0);
  186. if (IS_ERR(attr->recv_cq)) {
  187. ret = PTR_ERR(attr->recv_cq);
  188. attr->recv_cq = NULL;
  189. rdsdebug("ib_create_cq send failed: %d\n", ret);
  190. goto out;
  191. }
  192. ret = ib_req_notify_cq(attr->send_cq, IB_CQ_NEXT_COMP);
  193. if (ret) {
  194. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  195. goto out;
  196. }
  197. ret = ib_req_notify_cq(attr->recv_cq, IB_CQ_SOLICITED);
  198. if (ret) {
  199. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  200. goto out;
  201. }
  202. out:
  203. if (ret) {
  204. if (attr->send_cq)
  205. ib_destroy_cq(attr->send_cq);
  206. if (attr->recv_cq)
  207. ib_destroy_cq(attr->recv_cq);
  208. }
  209. return ret;
  210. }
  211. /*
  212. * This needs to be very careful to not leave IS_ERR pointers around for
  213. * cleanup to trip over.
  214. */
  215. static int rds_iw_setup_qp(struct rds_connection *conn)
  216. {
  217. struct rds_iw_connection *ic = conn->c_transport_data;
  218. struct ib_device *dev = ic->i_cm_id->device;
  219. struct ib_qp_init_attr attr;
  220. struct rds_iw_device *rds_iwdev;
  221. int ret;
  222. /* rds_iw_add_one creates a rds_iw_device object per IB device,
  223. * and allocates a protection domain, memory range and MR pool
  224. * for each. If that fails for any reason, it will not register
  225. * the rds_iwdev at all.
  226. */
  227. rds_iwdev = ib_get_client_data(dev, &rds_iw_client);
  228. if (rds_iwdev == NULL) {
  229. if (printk_ratelimit())
  230. printk(KERN_NOTICE "RDS/IW: No client_data for device %s\n",
  231. dev->name);
  232. return -EOPNOTSUPP;
  233. }
  234. /* Protection domain and memory range */
  235. ic->i_pd = rds_iwdev->pd;
  236. ic->i_mr = rds_iwdev->mr;
  237. ret = rds_iw_init_qp_attrs(&attr, rds_iwdev,
  238. &ic->i_send_ring, rds_iw_send_cq_comp_handler,
  239. &ic->i_recv_ring, rds_iw_recv_cq_comp_handler,
  240. conn);
  241. if (ret < 0)
  242. goto out;
  243. ic->i_send_cq = attr.send_cq;
  244. ic->i_recv_cq = attr.recv_cq;
  245. /*
  246. * XXX this can fail if max_*_wr is too large? Are we supposed
  247. * to back off until we get a value that the hardware can support?
  248. */
  249. ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
  250. if (ret) {
  251. rdsdebug("rdma_create_qp failed: %d\n", ret);
  252. goto out;
  253. }
  254. ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
  255. ic->i_send_ring.w_nr *
  256. sizeof(struct rds_header),
  257. &ic->i_send_hdrs_dma, GFP_KERNEL);
  258. if (ic->i_send_hdrs == NULL) {
  259. ret = -ENOMEM;
  260. rdsdebug("ib_dma_alloc_coherent send failed\n");
  261. goto out;
  262. }
  263. ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
  264. ic->i_recv_ring.w_nr *
  265. sizeof(struct rds_header),
  266. &ic->i_recv_hdrs_dma, GFP_KERNEL);
  267. if (ic->i_recv_hdrs == NULL) {
  268. ret = -ENOMEM;
  269. rdsdebug("ib_dma_alloc_coherent recv failed\n");
  270. goto out;
  271. }
  272. ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
  273. &ic->i_ack_dma, GFP_KERNEL);
  274. if (ic->i_ack == NULL) {
  275. ret = -ENOMEM;
  276. rdsdebug("ib_dma_alloc_coherent ack failed\n");
  277. goto out;
  278. }
  279. ic->i_sends = vmalloc(ic->i_send_ring.w_nr * sizeof(struct rds_iw_send_work));
  280. if (ic->i_sends == NULL) {
  281. ret = -ENOMEM;
  282. rdsdebug("send allocation failed\n");
  283. goto out;
  284. }
  285. rds_iw_send_init_ring(ic);
  286. ic->i_recvs = vmalloc(ic->i_recv_ring.w_nr * sizeof(struct rds_iw_recv_work));
  287. if (ic->i_recvs == NULL) {
  288. ret = -ENOMEM;
  289. rdsdebug("recv allocation failed\n");
  290. goto out;
  291. }
  292. rds_iw_recv_init_ring(ic);
  293. rds_iw_recv_init_ack(ic);
  294. /* Post receive buffers - as a side effect, this will update
  295. * the posted credit count. */
  296. rds_iw_recv_refill(conn, GFP_KERNEL, GFP_HIGHUSER, 1);
  297. rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr,
  298. ic->i_send_cq, ic->i_recv_cq);
  299. out:
  300. return ret;
  301. }
  302. static u32 rds_iw_protocol_compatible(const struct rds_iw_connect_private *dp)
  303. {
  304. u16 common;
  305. u32 version = 0;
  306. /* rdma_cm private data is odd - when there is any private data in the
  307. * request, we will be given a pretty large buffer without telling us the
  308. * original size. The only way to tell the difference is by looking at
  309. * the contents, which are initialized to zero.
  310. * If the protocol version fields aren't set, this is a connection attempt
  311. * from an older version. This could could be 3.0 or 2.0 - we can't tell.
  312. * We really should have changed this for OFED 1.3 :-( */
  313. if (dp->dp_protocol_major == 0)
  314. return RDS_PROTOCOL_3_0;
  315. common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IW_SUPPORTED_PROTOCOLS;
  316. if (dp->dp_protocol_major == 3 && common) {
  317. version = RDS_PROTOCOL_3_0;
  318. while ((common >>= 1) != 0)
  319. version++;
  320. } else if (printk_ratelimit()) {
  321. printk(KERN_NOTICE "RDS: Connection from %pI4 using "
  322. "incompatible protocol version %u.%u\n",
  323. &dp->dp_saddr,
  324. dp->dp_protocol_major,
  325. dp->dp_protocol_minor);
  326. }
  327. return version;
  328. }
  329. int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id,
  330. struct rdma_cm_event *event)
  331. {
  332. const struct rds_iw_connect_private *dp = event->param.conn.private_data;
  333. struct rds_iw_connect_private dp_rep;
  334. struct rds_connection *conn = NULL;
  335. struct rds_iw_connection *ic = NULL;
  336. struct rdma_conn_param conn_param;
  337. struct rds_iw_device *rds_iwdev;
  338. u32 version;
  339. int err, destroy = 1;
  340. /* Check whether the remote protocol version matches ours. */
  341. version = rds_iw_protocol_compatible(dp);
  342. if (!version)
  343. goto out;
  344. rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u\n",
  345. &dp->dp_saddr, &dp->dp_daddr,
  346. RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version));
  347. conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_iw_transport,
  348. GFP_KERNEL);
  349. if (IS_ERR(conn)) {
  350. rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
  351. conn = NULL;
  352. goto out;
  353. }
  354. /*
  355. * The connection request may occur while the
  356. * previous connection exist, e.g. in case of failover.
  357. * But as connections may be initiated simultaneously
  358. * by both hosts, we have a random backoff mechanism -
  359. * see the comment above rds_queue_reconnect()
  360. */
  361. mutex_lock(&conn->c_cm_lock);
  362. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  363. if (rds_conn_state(conn) == RDS_CONN_UP) {
  364. rdsdebug("incoming connect while connecting\n");
  365. rds_conn_drop(conn);
  366. rds_iw_stats_inc(s_iw_listen_closed_stale);
  367. } else
  368. if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
  369. /* Wait and see - our connect may still be succeeding */
  370. rds_iw_stats_inc(s_iw_connect_raced);
  371. }
  372. mutex_unlock(&conn->c_cm_lock);
  373. goto out;
  374. }
  375. ic = conn->c_transport_data;
  376. rds_iw_set_protocol(conn, version);
  377. rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  378. /* If the peer gave us the last packet it saw, process this as if
  379. * we had received a regular ACK. */
  380. if (dp->dp_ack_seq)
  381. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  382. BUG_ON(cm_id->context);
  383. BUG_ON(ic->i_cm_id);
  384. ic->i_cm_id = cm_id;
  385. cm_id->context = conn;
  386. rds_iwdev = ib_get_client_data(cm_id->device, &rds_iw_client);
  387. ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
  388. /* We got halfway through setting up the ib_connection, if we
  389. * fail now, we have to take the long route out of this mess. */
  390. destroy = 0;
  391. err = rds_iw_setup_qp(conn);
  392. if (err) {
  393. rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", err);
  394. goto out;
  395. }
  396. rds_iw_cm_fill_conn_param(conn, &conn_param, &dp_rep, version);
  397. /* rdma_accept() calls rdma_reject() internally if it fails */
  398. err = rdma_accept(cm_id, &conn_param);
  399. mutex_unlock(&conn->c_cm_lock);
  400. if (err) {
  401. rds_iw_conn_error(conn, "rdma_accept failed (%d)\n", err);
  402. goto out;
  403. }
  404. return 0;
  405. out:
  406. rdma_reject(cm_id, NULL, 0);
  407. return destroy;
  408. }
  409. int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id)
  410. {
  411. struct rds_connection *conn = cm_id->context;
  412. struct rds_iw_connection *ic = conn->c_transport_data;
  413. struct rdma_conn_param conn_param;
  414. struct rds_iw_connect_private dp;
  415. int ret;
  416. /* If the peer doesn't do protocol negotiation, we must
  417. * default to RDSv3.0 */
  418. rds_iw_set_protocol(conn, RDS_PROTOCOL_3_0);
  419. ic->i_flowctl = rds_iw_sysctl_flow_control; /* advertise flow control */
  420. ret = rds_iw_setup_qp(conn);
  421. if (ret) {
  422. rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", ret);
  423. goto out;
  424. }
  425. rds_iw_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION);
  426. ret = rdma_connect(cm_id, &conn_param);
  427. if (ret)
  428. rds_iw_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  429. out:
  430. /* Beware - returning non-zero tells the rdma_cm to destroy
  431. * the cm_id. We should certainly not do it as long as we still
  432. * "own" the cm_id. */
  433. if (ret) {
  434. struct rds_iw_connection *ic = conn->c_transport_data;
  435. if (ic->i_cm_id == cm_id)
  436. ret = 0;
  437. }
  438. return ret;
  439. }
  440. int rds_iw_conn_connect(struct rds_connection *conn)
  441. {
  442. struct rds_iw_connection *ic = conn->c_transport_data;
  443. struct rds_iw_device *rds_iwdev;
  444. struct sockaddr_in src, dest;
  445. int ret;
  446. /* XXX I wonder what affect the port space has */
  447. /* delegate cm event handler to rdma_transport */
  448. ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
  449. RDMA_PS_TCP);
  450. if (IS_ERR(ic->i_cm_id)) {
  451. ret = PTR_ERR(ic->i_cm_id);
  452. ic->i_cm_id = NULL;
  453. rdsdebug("rdma_create_id() failed: %d\n", ret);
  454. goto out;
  455. }
  456. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  457. src.sin_family = AF_INET;
  458. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  459. src.sin_port = (__force u16)htons(0);
  460. /* First, bind to the local address and device. */
  461. ret = rdma_bind_addr(ic->i_cm_id, (struct sockaddr *) &src);
  462. if (ret) {
  463. rdsdebug("rdma_bind_addr(%pI4) failed: %d\n",
  464. &conn->c_laddr, ret);
  465. rdma_destroy_id(ic->i_cm_id);
  466. ic->i_cm_id = NULL;
  467. goto out;
  468. }
  469. rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
  470. ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
  471. dest.sin_family = AF_INET;
  472. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  473. dest.sin_port = (__force u16)htons(RDS_PORT);
  474. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  475. (struct sockaddr *)&dest,
  476. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  477. if (ret) {
  478. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  479. ret);
  480. rdma_destroy_id(ic->i_cm_id);
  481. ic->i_cm_id = NULL;
  482. }
  483. out:
  484. return ret;
  485. }
  486. /*
  487. * This is so careful about only cleaning up resources that were built up
  488. * so that it can be called at any point during startup. In fact it
  489. * can be called multiple times for a given connection.
  490. */
  491. void rds_iw_conn_shutdown(struct rds_connection *conn)
  492. {
  493. struct rds_iw_connection *ic = conn->c_transport_data;
  494. int err = 0;
  495. struct ib_qp_attr qp_attr;
  496. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  497. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  498. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  499. if (ic->i_cm_id) {
  500. struct ib_device *dev = ic->i_cm_id->device;
  501. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  502. err = rdma_disconnect(ic->i_cm_id);
  503. if (err) {
  504. /* Actually this may happen quite frequently, when
  505. * an outgoing connect raced with an incoming connect.
  506. */
  507. rdsdebug("rds_iw_conn_shutdown: failed to disconnect,"
  508. " cm: %p err %d\n", ic->i_cm_id, err);
  509. }
  510. if (ic->i_cm_id->qp) {
  511. qp_attr.qp_state = IB_QPS_ERR;
  512. ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  513. }
  514. wait_event(rds_iw_ring_empty_wait,
  515. rds_iw_ring_empty(&ic->i_send_ring) &&
  516. rds_iw_ring_empty(&ic->i_recv_ring));
  517. if (ic->i_send_hdrs)
  518. ib_dma_free_coherent(dev,
  519. ic->i_send_ring.w_nr *
  520. sizeof(struct rds_header),
  521. ic->i_send_hdrs,
  522. ic->i_send_hdrs_dma);
  523. if (ic->i_recv_hdrs)
  524. ib_dma_free_coherent(dev,
  525. ic->i_recv_ring.w_nr *
  526. sizeof(struct rds_header),
  527. ic->i_recv_hdrs,
  528. ic->i_recv_hdrs_dma);
  529. if (ic->i_ack)
  530. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  531. ic->i_ack, ic->i_ack_dma);
  532. if (ic->i_sends)
  533. rds_iw_send_clear_ring(ic);
  534. if (ic->i_recvs)
  535. rds_iw_recv_clear_ring(ic);
  536. if (ic->i_cm_id->qp)
  537. rdma_destroy_qp(ic->i_cm_id);
  538. if (ic->i_send_cq)
  539. ib_destroy_cq(ic->i_send_cq);
  540. if (ic->i_recv_cq)
  541. ib_destroy_cq(ic->i_recv_cq);
  542. /*
  543. * If associated with an rds_iw_device:
  544. * Move connection back to the nodev list.
  545. * Remove cm_id from the device cm_id list.
  546. */
  547. if (ic->rds_iwdev) {
  548. spin_lock_irq(&ic->rds_iwdev->spinlock);
  549. BUG_ON(list_empty(&ic->iw_node));
  550. list_del(&ic->iw_node);
  551. spin_unlock_irq(&ic->rds_iwdev->spinlock);
  552. spin_lock_irq(&iw_nodev_conns_lock);
  553. list_add_tail(&ic->iw_node, &iw_nodev_conns);
  554. spin_unlock_irq(&iw_nodev_conns_lock);
  555. rds_iw_remove_cm_id(ic->rds_iwdev, ic->i_cm_id);
  556. ic->rds_iwdev = NULL;
  557. }
  558. rdma_destroy_id(ic->i_cm_id);
  559. ic->i_cm_id = NULL;
  560. ic->i_pd = NULL;
  561. ic->i_mr = NULL;
  562. ic->i_send_cq = NULL;
  563. ic->i_recv_cq = NULL;
  564. ic->i_send_hdrs = NULL;
  565. ic->i_recv_hdrs = NULL;
  566. ic->i_ack = NULL;
  567. }
  568. BUG_ON(ic->rds_iwdev);
  569. /* Clear pending transmit */
  570. if (ic->i_rm) {
  571. rds_message_put(ic->i_rm);
  572. ic->i_rm = NULL;
  573. }
  574. /* Clear the ACK state */
  575. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  576. rds_iw_set_64bit(&ic->i_ack_next, 0);
  577. ic->i_ack_recv = 0;
  578. /* Clear flow control state */
  579. ic->i_flowctl = 0;
  580. atomic_set(&ic->i_credits, 0);
  581. rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
  582. rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
  583. if (ic->i_iwinc) {
  584. rds_inc_put(&ic->i_iwinc->ii_inc);
  585. ic->i_iwinc = NULL;
  586. }
  587. vfree(ic->i_sends);
  588. ic->i_sends = NULL;
  589. vfree(ic->i_recvs);
  590. ic->i_recvs = NULL;
  591. rdsdebug("shutdown complete\n");
  592. }
  593. int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  594. {
  595. struct rds_iw_connection *ic;
  596. unsigned long flags;
  597. /* XXX too lazy? */
  598. ic = kzalloc(sizeof(struct rds_iw_connection), GFP_KERNEL);
  599. if (ic == NULL)
  600. return -ENOMEM;
  601. INIT_LIST_HEAD(&ic->iw_node);
  602. mutex_init(&ic->i_recv_mutex);
  603. /*
  604. * rds_iw_conn_shutdown() waits for these to be emptied so they
  605. * must be initialized before it can be called.
  606. */
  607. rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
  608. rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
  609. ic->conn = conn;
  610. conn->c_transport_data = ic;
  611. spin_lock_irqsave(&iw_nodev_conns_lock, flags);
  612. list_add_tail(&ic->iw_node, &iw_nodev_conns);
  613. spin_unlock_irqrestore(&iw_nodev_conns_lock, flags);
  614. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  615. return 0;
  616. }
  617. void rds_iw_conn_free(void *arg)
  618. {
  619. struct rds_iw_connection *ic = arg;
  620. rdsdebug("ic %p\n", ic);
  621. list_del(&ic->iw_node);
  622. kfree(ic);
  623. }
  624. /*
  625. * An error occurred on the connection
  626. */
  627. void
  628. __rds_iw_conn_error(struct rds_connection *conn, const char *fmt, ...)
  629. {
  630. va_list ap;
  631. rds_conn_drop(conn);
  632. va_start(ap, fmt);
  633. vprintk(fmt, ap);
  634. va_end(ap);
  635. }