iw_cm.c 21 KB

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