iw_cm.c 21 KB

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