iw_cm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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/slab.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/ratelimit.h>
  38. #include "rds.h"
  39. #include "iw.h"
  40. /*
  41. * Set the selected protocol version
  42. */
  43. static void rds_iw_set_protocol(struct rds_connection *conn, unsigned int version)
  44. {
  45. conn->c_version = version;
  46. }
  47. /*
  48. * Set up flow control
  49. */
  50. static void rds_iw_set_flow_control(struct rds_connection *conn, u32 credits)
  51. {
  52. struct rds_iw_connection *ic = conn->c_transport_data;
  53. if (rds_iw_sysctl_flow_control && credits != 0) {
  54. /* We're doing flow control */
  55. ic->i_flowctl = 1;
  56. rds_iw_send_add_credits(conn, credits);
  57. } else {
  58. ic->i_flowctl = 0;
  59. }
  60. }
  61. /*
  62. * Connection established.
  63. * We get here for both outgoing and incoming connection.
  64. */
  65. void rds_iw_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
  66. {
  67. const struct rds_iw_connect_private *dp = NULL;
  68. struct rds_iw_connection *ic = conn->c_transport_data;
  69. struct rds_iw_device *rds_iwdev;
  70. int err;
  71. if (event->param.conn.private_data_len) {
  72. dp = event->param.conn.private_data;
  73. rds_iw_set_protocol(conn,
  74. RDS_PROTOCOL(dp->dp_protocol_major,
  75. dp->dp_protocol_minor));
  76. rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  77. }
  78. /* update ib_device with this local ipaddr & conn */
  79. rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
  80. err = rds_iw_update_cm_id(rds_iwdev, ic->i_cm_id);
  81. if (err)
  82. printk(KERN_ERR "rds_iw_update_ipaddr failed (%d)\n", err);
  83. rds_iw_add_conn(rds_iwdev, conn);
  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. rdsdebug("Fatal QP Event %u "
  141. "- connection %pI4->%pI4, reconnecting\n",
  142. event->event, &conn->c_laddr,
  143. &conn->c_faddr);
  144. rds_conn_drop(conn);
  145. break;
  146. }
  147. }
  148. /*
  149. * Create a QP
  150. */
  151. static int rds_iw_init_qp_attrs(struct ib_qp_init_attr *attr,
  152. struct rds_iw_device *rds_iwdev,
  153. struct rds_iw_work_ring *send_ring,
  154. void (*send_cq_handler)(struct ib_cq *, void *),
  155. struct rds_iw_work_ring *recv_ring,
  156. void (*recv_cq_handler)(struct ib_cq *, void *),
  157. void *context)
  158. {
  159. struct ib_device *dev = rds_iwdev->dev;
  160. struct ib_cq_init_attr cq_attr = {};
  161. unsigned int send_size, recv_size;
  162. int ret;
  163. /* The offset of 1 is to accommodate the additional ACK WR. */
  164. send_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_send_wr + 1);
  165. recv_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_recv_wr + 1);
  166. rds_iw_ring_resize(send_ring, send_size - 1);
  167. rds_iw_ring_resize(recv_ring, recv_size - 1);
  168. memset(attr, 0, sizeof(*attr));
  169. attr->event_handler = rds_iw_qp_event_handler;
  170. attr->qp_context = context;
  171. attr->cap.max_send_wr = send_size;
  172. attr->cap.max_recv_wr = recv_size;
  173. attr->cap.max_send_sge = rds_iwdev->max_sge;
  174. attr->cap.max_recv_sge = RDS_IW_RECV_SGE;
  175. attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  176. attr->qp_type = IB_QPT_RC;
  177. cq_attr.cqe = send_size;
  178. attr->send_cq = ib_create_cq(dev, send_cq_handler,
  179. rds_iw_cq_event_handler,
  180. context, &cq_attr);
  181. if (IS_ERR(attr->send_cq)) {
  182. ret = PTR_ERR(attr->send_cq);
  183. attr->send_cq = NULL;
  184. rdsdebug("ib_create_cq send failed: %d\n", ret);
  185. goto out;
  186. }
  187. cq_attr.cqe = recv_size;
  188. attr->recv_cq = ib_create_cq(dev, recv_cq_handler,
  189. rds_iw_cq_event_handler,
  190. context, &cq_attr);
  191. if (IS_ERR(attr->recv_cq)) {
  192. ret = PTR_ERR(attr->recv_cq);
  193. attr->recv_cq = NULL;
  194. rdsdebug("ib_create_cq send failed: %d\n", ret);
  195. goto out;
  196. }
  197. ret = ib_req_notify_cq(attr->send_cq, IB_CQ_NEXT_COMP);
  198. if (ret) {
  199. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  200. goto out;
  201. }
  202. ret = ib_req_notify_cq(attr->recv_cq, IB_CQ_SOLICITED);
  203. if (ret) {
  204. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  205. goto out;
  206. }
  207. out:
  208. if (ret) {
  209. if (attr->send_cq)
  210. ib_destroy_cq(attr->send_cq);
  211. if (attr->recv_cq)
  212. ib_destroy_cq(attr->recv_cq);
  213. }
  214. return ret;
  215. }
  216. /*
  217. * This needs to be very careful to not leave IS_ERR pointers around for
  218. * cleanup to trip over.
  219. */
  220. static int rds_iw_setup_qp(struct rds_connection *conn)
  221. {
  222. struct rds_iw_connection *ic = conn->c_transport_data;
  223. struct ib_device *dev = ic->i_cm_id->device;
  224. struct ib_qp_init_attr attr;
  225. struct rds_iw_device *rds_iwdev;
  226. int ret;
  227. /* rds_iw_add_one creates a rds_iw_device object per IB device,
  228. * and allocates a protection domain, memory range and MR pool
  229. * for each. If that fails for any reason, it will not register
  230. * the rds_iwdev at all.
  231. */
  232. rds_iwdev = ib_get_client_data(dev, &rds_iw_client);
  233. if (!rds_iwdev) {
  234. printk_ratelimited(KERN_NOTICE "RDS/IW: No client_data for device %s\n",
  235. dev->name);
  236. return -EOPNOTSUPP;
  237. }
  238. /* Protection domain and memory range */
  239. ic->i_pd = rds_iwdev->pd;
  240. ic->i_mr = rds_iwdev->mr;
  241. ret = rds_iw_init_qp_attrs(&attr, rds_iwdev,
  242. &ic->i_send_ring, rds_iw_send_cq_comp_handler,
  243. &ic->i_recv_ring, rds_iw_recv_cq_comp_handler,
  244. conn);
  245. if (ret < 0)
  246. goto out;
  247. ic->i_send_cq = attr.send_cq;
  248. ic->i_recv_cq = attr.recv_cq;
  249. /*
  250. * XXX this can fail if max_*_wr is too large? Are we supposed
  251. * to back off until we get a value that the hardware can support?
  252. */
  253. ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
  254. if (ret) {
  255. rdsdebug("rdma_create_qp failed: %d\n", ret);
  256. goto out;
  257. }
  258. ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
  259. ic->i_send_ring.w_nr *
  260. sizeof(struct rds_header),
  261. &ic->i_send_hdrs_dma, GFP_KERNEL);
  262. if (!ic->i_send_hdrs) {
  263. ret = -ENOMEM;
  264. rdsdebug("ib_dma_alloc_coherent send failed\n");
  265. goto out;
  266. }
  267. ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
  268. ic->i_recv_ring.w_nr *
  269. sizeof(struct rds_header),
  270. &ic->i_recv_hdrs_dma, GFP_KERNEL);
  271. if (!ic->i_recv_hdrs) {
  272. ret = -ENOMEM;
  273. rdsdebug("ib_dma_alloc_coherent recv failed\n");
  274. goto out;
  275. }
  276. ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
  277. &ic->i_ack_dma, GFP_KERNEL);
  278. if (!ic->i_ack) {
  279. ret = -ENOMEM;
  280. rdsdebug("ib_dma_alloc_coherent ack failed\n");
  281. goto out;
  282. }
  283. ic->i_sends = vmalloc(ic->i_send_ring.w_nr * sizeof(struct rds_iw_send_work));
  284. if (!ic->i_sends) {
  285. ret = -ENOMEM;
  286. rdsdebug("send allocation failed\n");
  287. goto out;
  288. }
  289. rds_iw_send_init_ring(ic);
  290. ic->i_recvs = vmalloc(ic->i_recv_ring.w_nr * sizeof(struct rds_iw_recv_work));
  291. if (!ic->i_recvs) {
  292. ret = -ENOMEM;
  293. rdsdebug("recv allocation failed\n");
  294. goto out;
  295. }
  296. rds_iw_recv_init_ring(ic);
  297. rds_iw_recv_init_ack(ic);
  298. /* Post receive buffers - as a side effect, this will update
  299. * the posted credit count. */
  300. rds_iw_recv_refill(conn, GFP_KERNEL, GFP_HIGHUSER, 1);
  301. rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr,
  302. ic->i_send_cq, ic->i_recv_cq);
  303. out:
  304. return ret;
  305. }
  306. static u32 rds_iw_protocol_compatible(const struct rds_iw_connect_private *dp)
  307. {
  308. u16 common;
  309. u32 version = 0;
  310. /* rdma_cm private data is odd - when there is any private data in the
  311. * request, we will be given a pretty large buffer without telling us the
  312. * original size. The only way to tell the difference is by looking at
  313. * the contents, which are initialized to zero.
  314. * If the protocol version fields aren't set, this is a connection attempt
  315. * from an older version. This could could be 3.0 or 2.0 - we can't tell.
  316. * We really should have changed this for OFED 1.3 :-( */
  317. if (dp->dp_protocol_major == 0)
  318. return RDS_PROTOCOL_3_0;
  319. common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IW_SUPPORTED_PROTOCOLS;
  320. if (dp->dp_protocol_major == 3 && common) {
  321. version = RDS_PROTOCOL_3_0;
  322. while ((common >>= 1) != 0)
  323. version++;
  324. }
  325. printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using "
  326. "incompatible protocol version %u.%u\n",
  327. &dp->dp_saddr,
  328. dp->dp_protocol_major,
  329. dp->dp_protocol_minor);
  330. return version;
  331. }
  332. int rds_iw_cm_handle_connect(struct rdma_cm_id *cm_id,
  333. struct rdma_cm_event *event)
  334. {
  335. const struct rds_iw_connect_private *dp = event->param.conn.private_data;
  336. struct rds_iw_connect_private dp_rep;
  337. struct rds_connection *conn = NULL;
  338. struct rds_iw_connection *ic = NULL;
  339. struct rdma_conn_param conn_param;
  340. struct rds_iw_device *rds_iwdev;
  341. u32 version;
  342. int err, destroy = 1;
  343. /* Check whether the remote protocol version matches ours. */
  344. version = rds_iw_protocol_compatible(dp);
  345. if (!version)
  346. goto out;
  347. rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u\n",
  348. &dp->dp_saddr, &dp->dp_daddr,
  349. RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version));
  350. /* RDS/IW is not currently netns aware, thus init_net */
  351. conn = rds_conn_create(&init_net, dp->dp_daddr, dp->dp_saddr,
  352. &rds_iw_transport, GFP_KERNEL);
  353. if (IS_ERR(conn)) {
  354. rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
  355. conn = NULL;
  356. goto out;
  357. }
  358. /*
  359. * The connection request may occur while the
  360. * previous connection exist, e.g. in case of failover.
  361. * But as connections may be initiated simultaneously
  362. * by both hosts, we have a random backoff mechanism -
  363. * see the comment above rds_queue_reconnect()
  364. */
  365. mutex_lock(&conn->c_cm_lock);
  366. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  367. if (rds_conn_state(conn) == RDS_CONN_UP) {
  368. rdsdebug("incoming connect while connecting\n");
  369. rds_conn_drop(conn);
  370. rds_iw_stats_inc(s_iw_listen_closed_stale);
  371. } else
  372. if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
  373. /* Wait and see - our connect may still be succeeding */
  374. rds_iw_stats_inc(s_iw_connect_raced);
  375. }
  376. mutex_unlock(&conn->c_cm_lock);
  377. goto out;
  378. }
  379. ic = conn->c_transport_data;
  380. rds_iw_set_protocol(conn, version);
  381. rds_iw_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  382. /* If the peer gave us the last packet it saw, process this as if
  383. * we had received a regular ACK. */
  384. if (dp->dp_ack_seq)
  385. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  386. BUG_ON(cm_id->context);
  387. BUG_ON(ic->i_cm_id);
  388. ic->i_cm_id = cm_id;
  389. cm_id->context = conn;
  390. rds_iwdev = ib_get_client_data(cm_id->device, &rds_iw_client);
  391. ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
  392. /* We got halfway through setting up the ib_connection, if we
  393. * fail now, we have to take the long route out of this mess. */
  394. destroy = 0;
  395. err = rds_iw_setup_qp(conn);
  396. if (err) {
  397. rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", err);
  398. mutex_unlock(&conn->c_cm_lock);
  399. goto out;
  400. }
  401. rds_iw_cm_fill_conn_param(conn, &conn_param, &dp_rep, version);
  402. /* rdma_accept() calls rdma_reject() internally if it fails */
  403. err = rdma_accept(cm_id, &conn_param);
  404. mutex_unlock(&conn->c_cm_lock);
  405. if (err) {
  406. rds_iw_conn_error(conn, "rdma_accept failed (%d)\n", err);
  407. goto out;
  408. }
  409. return 0;
  410. out:
  411. rdma_reject(cm_id, NULL, 0);
  412. return destroy;
  413. }
  414. int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id)
  415. {
  416. struct rds_connection *conn = cm_id->context;
  417. struct rds_iw_connection *ic = conn->c_transport_data;
  418. struct rdma_conn_param conn_param;
  419. struct rds_iw_connect_private dp;
  420. int ret;
  421. /* If the peer doesn't do protocol negotiation, we must
  422. * default to RDSv3.0 */
  423. rds_iw_set_protocol(conn, RDS_PROTOCOL_3_0);
  424. ic->i_flowctl = rds_iw_sysctl_flow_control; /* advertise flow control */
  425. ret = rds_iw_setup_qp(conn);
  426. if (ret) {
  427. rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", ret);
  428. goto out;
  429. }
  430. rds_iw_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION);
  431. ret = rdma_connect(cm_id, &conn_param);
  432. if (ret)
  433. rds_iw_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  434. out:
  435. /* Beware - returning non-zero tells the rdma_cm to destroy
  436. * the cm_id. We should certainly not do it as long as we still
  437. * "own" the cm_id. */
  438. if (ret) {
  439. struct rds_iw_connection *ic = conn->c_transport_data;
  440. if (ic->i_cm_id == cm_id)
  441. ret = 0;
  442. }
  443. return ret;
  444. }
  445. int rds_iw_conn_connect(struct rds_connection *conn)
  446. {
  447. struct rds_iw_connection *ic = conn->c_transport_data;
  448. struct rds_iw_device *rds_iwdev;
  449. struct sockaddr_in src, dest;
  450. int ret;
  451. /* XXX I wonder what affect the port space has */
  452. /* delegate cm event handler to rdma_transport */
  453. ic->i_cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler, conn,
  454. RDMA_PS_TCP, IB_QPT_RC);
  455. if (IS_ERR(ic->i_cm_id)) {
  456. ret = PTR_ERR(ic->i_cm_id);
  457. ic->i_cm_id = NULL;
  458. rdsdebug("rdma_create_id() failed: %d\n", ret);
  459. goto out;
  460. }
  461. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  462. src.sin_family = AF_INET;
  463. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  464. src.sin_port = (__force u16)htons(0);
  465. /* First, bind to the local address and device. */
  466. ret = rdma_bind_addr(ic->i_cm_id, (struct sockaddr *) &src);
  467. if (ret) {
  468. rdsdebug("rdma_bind_addr(%pI4) failed: %d\n",
  469. &conn->c_laddr, ret);
  470. rdma_destroy_id(ic->i_cm_id);
  471. ic->i_cm_id = NULL;
  472. goto out;
  473. }
  474. rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
  475. ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
  476. dest.sin_family = AF_INET;
  477. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  478. dest.sin_port = (__force u16)htons(RDS_PORT);
  479. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  480. (struct sockaddr *)&dest,
  481. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  482. if (ret) {
  483. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  484. ret);
  485. rdma_destroy_id(ic->i_cm_id);
  486. ic->i_cm_id = NULL;
  487. }
  488. out:
  489. return ret;
  490. }
  491. /*
  492. * This is so careful about only cleaning up resources that were built up
  493. * so that it can be called at any point during startup. In fact it
  494. * can be called multiple times for a given connection.
  495. */
  496. void rds_iw_conn_shutdown(struct rds_connection *conn)
  497. {
  498. struct rds_iw_connection *ic = conn->c_transport_data;
  499. int err = 0;
  500. struct ib_qp_attr qp_attr;
  501. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  502. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  503. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  504. if (ic->i_cm_id) {
  505. struct ib_device *dev = ic->i_cm_id->device;
  506. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  507. err = rdma_disconnect(ic->i_cm_id);
  508. if (err) {
  509. /* Actually this may happen quite frequently, when
  510. * an outgoing connect raced with an incoming connect.
  511. */
  512. rdsdebug("failed to disconnect, cm: %p err %d\n",
  513. ic->i_cm_id, err);
  514. }
  515. if (ic->i_cm_id->qp) {
  516. qp_attr.qp_state = IB_QPS_ERR;
  517. ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  518. }
  519. wait_event(rds_iw_ring_empty_wait,
  520. rds_iw_ring_empty(&ic->i_send_ring) &&
  521. rds_iw_ring_empty(&ic->i_recv_ring));
  522. if (ic->i_send_hdrs)
  523. ib_dma_free_coherent(dev,
  524. ic->i_send_ring.w_nr *
  525. sizeof(struct rds_header),
  526. ic->i_send_hdrs,
  527. ic->i_send_hdrs_dma);
  528. if (ic->i_recv_hdrs)
  529. ib_dma_free_coherent(dev,
  530. ic->i_recv_ring.w_nr *
  531. sizeof(struct rds_header),
  532. ic->i_recv_hdrs,
  533. ic->i_recv_hdrs_dma);
  534. if (ic->i_ack)
  535. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  536. ic->i_ack, ic->i_ack_dma);
  537. if (ic->i_sends)
  538. rds_iw_send_clear_ring(ic);
  539. if (ic->i_recvs)
  540. rds_iw_recv_clear_ring(ic);
  541. if (ic->i_cm_id->qp)
  542. rdma_destroy_qp(ic->i_cm_id);
  543. if (ic->i_send_cq)
  544. ib_destroy_cq(ic->i_send_cq);
  545. if (ic->i_recv_cq)
  546. ib_destroy_cq(ic->i_recv_cq);
  547. /*
  548. * If associated with an rds_iw_device:
  549. * Move connection back to the nodev list.
  550. * Remove cm_id from the device cm_id list.
  551. */
  552. if (ic->rds_iwdev)
  553. rds_iw_remove_conn(ic->rds_iwdev, conn);
  554. rdma_destroy_id(ic->i_cm_id);
  555. ic->i_cm_id = NULL;
  556. ic->i_pd = NULL;
  557. ic->i_mr = NULL;
  558. ic->i_send_cq = NULL;
  559. ic->i_recv_cq = NULL;
  560. ic->i_send_hdrs = NULL;
  561. ic->i_recv_hdrs = NULL;
  562. ic->i_ack = NULL;
  563. }
  564. BUG_ON(ic->rds_iwdev);
  565. /* Clear pending transmit */
  566. if (ic->i_rm) {
  567. rds_message_put(ic->i_rm);
  568. ic->i_rm = NULL;
  569. }
  570. /* Clear the ACK state */
  571. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  572. #ifdef KERNEL_HAS_ATOMIC64
  573. atomic64_set(&ic->i_ack_next, 0);
  574. #else
  575. ic->i_ack_next = 0;
  576. #endif
  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);
  599. if (!ic)
  600. return -ENOMEM;
  601. INIT_LIST_HEAD(&ic->iw_node);
  602. tasklet_init(&ic->i_recv_tasklet, rds_iw_recv_tasklet_fn,
  603. (unsigned long) ic);
  604. mutex_init(&ic->i_recv_mutex);
  605. #ifndef KERNEL_HAS_ATOMIC64
  606. spin_lock_init(&ic->i_ack_lock);
  607. #endif
  608. /*
  609. * rds_iw_conn_shutdown() waits for these to be emptied so they
  610. * must be initialized before it can be called.
  611. */
  612. rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
  613. rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
  614. ic->conn = conn;
  615. conn->c_transport_data = ic;
  616. spin_lock_irqsave(&iw_nodev_conns_lock, flags);
  617. list_add_tail(&ic->iw_node, &iw_nodev_conns);
  618. spin_unlock_irqrestore(&iw_nodev_conns_lock, flags);
  619. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  620. return 0;
  621. }
  622. /*
  623. * Free a connection. Connection must be shut down and not set for reconnect.
  624. */
  625. void rds_iw_conn_free(void *arg)
  626. {
  627. struct rds_iw_connection *ic = arg;
  628. spinlock_t *lock_ptr;
  629. rdsdebug("ic %p\n", ic);
  630. /*
  631. * Conn is either on a dev's list or on the nodev list.
  632. * A race with shutdown() or connect() would cause problems
  633. * (since rds_iwdev would change) but that should never happen.
  634. */
  635. lock_ptr = ic->rds_iwdev ? &ic->rds_iwdev->spinlock : &iw_nodev_conns_lock;
  636. spin_lock_irq(lock_ptr);
  637. list_del(&ic->iw_node);
  638. spin_unlock_irq(lock_ptr);
  639. kfree(ic);
  640. }
  641. /*
  642. * An error occurred on the connection
  643. */
  644. void
  645. __rds_iw_conn_error(struct rds_connection *conn, const char *fmt, ...)
  646. {
  647. va_list ap;
  648. rds_conn_drop(conn);
  649. va_start(ap, fmt);
  650. vprintk(fmt, ap);
  651. va_end(ap);
  652. }