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 <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. unsigned int send_size, recv_size;
  161. int ret;
  162. /* The offset of 1 is to accommodate the additional ACK WR. */
  163. send_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_send_wr + 1);
  164. recv_size = min_t(unsigned int, rds_iwdev->max_wrs, rds_iw_sysctl_max_recv_wr + 1);
  165. rds_iw_ring_resize(send_ring, send_size - 1);
  166. rds_iw_ring_resize(recv_ring, recv_size - 1);
  167. memset(attr, 0, sizeof(*attr));
  168. attr->event_handler = rds_iw_qp_event_handler;
  169. attr->qp_context = context;
  170. attr->cap.max_send_wr = send_size;
  171. attr->cap.max_recv_wr = recv_size;
  172. attr->cap.max_send_sge = rds_iwdev->max_sge;
  173. attr->cap.max_recv_sge = RDS_IW_RECV_SGE;
  174. attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  175. attr->qp_type = IB_QPT_RC;
  176. attr->send_cq = ib_create_cq(dev, send_cq_handler,
  177. rds_iw_cq_event_handler,
  178. context, send_size, 0);
  179. if (IS_ERR(attr->send_cq)) {
  180. ret = PTR_ERR(attr->send_cq);
  181. attr->send_cq = NULL;
  182. rdsdebug("ib_create_cq send failed: %d\n", ret);
  183. goto out;
  184. }
  185. attr->recv_cq = ib_create_cq(dev, recv_cq_handler,
  186. rds_iw_cq_event_handler,
  187. context, recv_size, 0);
  188. if (IS_ERR(attr->recv_cq)) {
  189. ret = PTR_ERR(attr->recv_cq);
  190. attr->recv_cq = NULL;
  191. rdsdebug("ib_create_cq send failed: %d\n", ret);
  192. goto out;
  193. }
  194. ret = ib_req_notify_cq(attr->send_cq, IB_CQ_NEXT_COMP);
  195. if (ret) {
  196. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  197. goto out;
  198. }
  199. ret = ib_req_notify_cq(attr->recv_cq, IB_CQ_SOLICITED);
  200. if (ret) {
  201. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  202. goto out;
  203. }
  204. out:
  205. if (ret) {
  206. if (attr->send_cq)
  207. ib_destroy_cq(attr->send_cq);
  208. if (attr->recv_cq)
  209. ib_destroy_cq(attr->recv_cq);
  210. }
  211. return ret;
  212. }
  213. /*
  214. * This needs to be very careful to not leave IS_ERR pointers around for
  215. * cleanup to trip over.
  216. */
  217. static int rds_iw_setup_qp(struct rds_connection *conn)
  218. {
  219. struct rds_iw_connection *ic = conn->c_transport_data;
  220. struct ib_device *dev = ic->i_cm_id->device;
  221. struct ib_qp_init_attr attr;
  222. struct rds_iw_device *rds_iwdev;
  223. int ret;
  224. /* rds_iw_add_one creates a rds_iw_device object per IB device,
  225. * and allocates a protection domain, memory range and MR pool
  226. * for each. If that fails for any reason, it will not register
  227. * the rds_iwdev at all.
  228. */
  229. rds_iwdev = ib_get_client_data(dev, &rds_iw_client);
  230. if (!rds_iwdev) {
  231. printk_ratelimited(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) {
  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) {
  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) {
  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) {
  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) {
  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. }
  322. printk_ratelimited(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. 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. mutex_unlock(&conn->c_cm_lock);
  395. goto out;
  396. }
  397. rds_iw_cm_fill_conn_param(conn, &conn_param, &dp_rep, version);
  398. /* rdma_accept() calls rdma_reject() internally if it fails */
  399. err = rdma_accept(cm_id, &conn_param);
  400. mutex_unlock(&conn->c_cm_lock);
  401. if (err) {
  402. rds_iw_conn_error(conn, "rdma_accept failed (%d)\n", err);
  403. goto out;
  404. }
  405. return 0;
  406. out:
  407. rdma_reject(cm_id, NULL, 0);
  408. return destroy;
  409. }
  410. int rds_iw_cm_initiate_connect(struct rdma_cm_id *cm_id)
  411. {
  412. struct rds_connection *conn = cm_id->context;
  413. struct rds_iw_connection *ic = conn->c_transport_data;
  414. struct rdma_conn_param conn_param;
  415. struct rds_iw_connect_private dp;
  416. int ret;
  417. /* If the peer doesn't do protocol negotiation, we must
  418. * default to RDSv3.0 */
  419. rds_iw_set_protocol(conn, RDS_PROTOCOL_3_0);
  420. ic->i_flowctl = rds_iw_sysctl_flow_control; /* advertise flow control */
  421. ret = rds_iw_setup_qp(conn);
  422. if (ret) {
  423. rds_iw_conn_error(conn, "rds_iw_setup_qp failed (%d)\n", ret);
  424. goto out;
  425. }
  426. rds_iw_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION);
  427. ret = rdma_connect(cm_id, &conn_param);
  428. if (ret)
  429. rds_iw_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  430. out:
  431. /* Beware - returning non-zero tells the rdma_cm to destroy
  432. * the cm_id. We should certainly not do it as long as we still
  433. * "own" the cm_id. */
  434. if (ret) {
  435. struct rds_iw_connection *ic = conn->c_transport_data;
  436. if (ic->i_cm_id == cm_id)
  437. ret = 0;
  438. }
  439. return ret;
  440. }
  441. int rds_iw_conn_connect(struct rds_connection *conn)
  442. {
  443. struct rds_iw_connection *ic = conn->c_transport_data;
  444. struct rds_iw_device *rds_iwdev;
  445. struct sockaddr_in src, dest;
  446. int ret;
  447. /* XXX I wonder what affect the port space has */
  448. /* delegate cm event handler to rdma_transport */
  449. ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
  450. RDMA_PS_TCP, IB_QPT_RC);
  451. if (IS_ERR(ic->i_cm_id)) {
  452. ret = PTR_ERR(ic->i_cm_id);
  453. ic->i_cm_id = NULL;
  454. rdsdebug("rdma_create_id() failed: %d\n", ret);
  455. goto out;
  456. }
  457. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  458. src.sin_family = AF_INET;
  459. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  460. src.sin_port = (__force u16)htons(0);
  461. /* First, bind to the local address and device. */
  462. ret = rdma_bind_addr(ic->i_cm_id, (struct sockaddr *) &src);
  463. if (ret) {
  464. rdsdebug("rdma_bind_addr(%pI4) failed: %d\n",
  465. &conn->c_laddr, ret);
  466. rdma_destroy_id(ic->i_cm_id);
  467. ic->i_cm_id = NULL;
  468. goto out;
  469. }
  470. rds_iwdev = ib_get_client_data(ic->i_cm_id->device, &rds_iw_client);
  471. ic->i_dma_local_lkey = rds_iwdev->dma_local_lkey;
  472. dest.sin_family = AF_INET;
  473. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  474. dest.sin_port = (__force u16)htons(RDS_PORT);
  475. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  476. (struct sockaddr *)&dest,
  477. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  478. if (ret) {
  479. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  480. ret);
  481. rdma_destroy_id(ic->i_cm_id);
  482. ic->i_cm_id = NULL;
  483. }
  484. out:
  485. return ret;
  486. }
  487. /*
  488. * This is so careful about only cleaning up resources that were built up
  489. * so that it can be called at any point during startup. In fact it
  490. * can be called multiple times for a given connection.
  491. */
  492. void rds_iw_conn_shutdown(struct rds_connection *conn)
  493. {
  494. struct rds_iw_connection *ic = conn->c_transport_data;
  495. int err = 0;
  496. struct ib_qp_attr qp_attr;
  497. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  498. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  499. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  500. if (ic->i_cm_id) {
  501. struct ib_device *dev = ic->i_cm_id->device;
  502. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  503. err = rdma_disconnect(ic->i_cm_id);
  504. if (err) {
  505. /* Actually this may happen quite frequently, when
  506. * an outgoing connect raced with an incoming connect.
  507. */
  508. rdsdebug("rds_iw_conn_shutdown: failed to disconnect,"
  509. " cm: %p err %d\n", ic->i_cm_id, err);
  510. }
  511. if (ic->i_cm_id->qp) {
  512. qp_attr.qp_state = IB_QPS_ERR;
  513. ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  514. }
  515. wait_event(rds_iw_ring_empty_wait,
  516. rds_iw_ring_empty(&ic->i_send_ring) &&
  517. rds_iw_ring_empty(&ic->i_recv_ring));
  518. if (ic->i_send_hdrs)
  519. ib_dma_free_coherent(dev,
  520. ic->i_send_ring.w_nr *
  521. sizeof(struct rds_header),
  522. ic->i_send_hdrs,
  523. ic->i_send_hdrs_dma);
  524. if (ic->i_recv_hdrs)
  525. ib_dma_free_coherent(dev,
  526. ic->i_recv_ring.w_nr *
  527. sizeof(struct rds_header),
  528. ic->i_recv_hdrs,
  529. ic->i_recv_hdrs_dma);
  530. if (ic->i_ack)
  531. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  532. ic->i_ack, ic->i_ack_dma);
  533. if (ic->i_sends)
  534. rds_iw_send_clear_ring(ic);
  535. if (ic->i_recvs)
  536. rds_iw_recv_clear_ring(ic);
  537. if (ic->i_cm_id->qp)
  538. rdma_destroy_qp(ic->i_cm_id);
  539. if (ic->i_send_cq)
  540. ib_destroy_cq(ic->i_send_cq);
  541. if (ic->i_recv_cq)
  542. ib_destroy_cq(ic->i_recv_cq);
  543. /*
  544. * If associated with an rds_iw_device:
  545. * Move connection back to the nodev list.
  546. * Remove cm_id from the device cm_id list.
  547. */
  548. if (ic->rds_iwdev)
  549. rds_iw_remove_conn(ic->rds_iwdev, conn);
  550. rdma_destroy_id(ic->i_cm_id);
  551. ic->i_cm_id = NULL;
  552. ic->i_pd = NULL;
  553. ic->i_mr = NULL;
  554. ic->i_send_cq = NULL;
  555. ic->i_recv_cq = NULL;
  556. ic->i_send_hdrs = NULL;
  557. ic->i_recv_hdrs = NULL;
  558. ic->i_ack = NULL;
  559. }
  560. BUG_ON(ic->rds_iwdev);
  561. /* Clear pending transmit */
  562. if (ic->i_rm) {
  563. rds_message_put(ic->i_rm);
  564. ic->i_rm = NULL;
  565. }
  566. /* Clear the ACK state */
  567. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  568. #ifdef KERNEL_HAS_ATOMIC64
  569. atomic64_set(&ic->i_ack_next, 0);
  570. #else
  571. ic->i_ack_next = 0;
  572. #endif
  573. ic->i_ack_recv = 0;
  574. /* Clear flow control state */
  575. ic->i_flowctl = 0;
  576. atomic_set(&ic->i_credits, 0);
  577. rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
  578. rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
  579. if (ic->i_iwinc) {
  580. rds_inc_put(&ic->i_iwinc->ii_inc);
  581. ic->i_iwinc = NULL;
  582. }
  583. vfree(ic->i_sends);
  584. ic->i_sends = NULL;
  585. vfree(ic->i_recvs);
  586. ic->i_recvs = NULL;
  587. rdsdebug("shutdown complete\n");
  588. }
  589. int rds_iw_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  590. {
  591. struct rds_iw_connection *ic;
  592. unsigned long flags;
  593. /* XXX too lazy? */
  594. ic = kzalloc(sizeof(struct rds_iw_connection), gfp);
  595. if (!ic)
  596. return -ENOMEM;
  597. INIT_LIST_HEAD(&ic->iw_node);
  598. tasklet_init(&ic->i_recv_tasklet, rds_iw_recv_tasklet_fn,
  599. (unsigned long) ic);
  600. mutex_init(&ic->i_recv_mutex);
  601. #ifndef KERNEL_HAS_ATOMIC64
  602. spin_lock_init(&ic->i_ack_lock);
  603. #endif
  604. /*
  605. * rds_iw_conn_shutdown() waits for these to be emptied so they
  606. * must be initialized before it can be called.
  607. */
  608. rds_iw_ring_init(&ic->i_send_ring, rds_iw_sysctl_max_send_wr);
  609. rds_iw_ring_init(&ic->i_recv_ring, rds_iw_sysctl_max_recv_wr);
  610. ic->conn = conn;
  611. conn->c_transport_data = ic;
  612. spin_lock_irqsave(&iw_nodev_conns_lock, flags);
  613. list_add_tail(&ic->iw_node, &iw_nodev_conns);
  614. spin_unlock_irqrestore(&iw_nodev_conns_lock, flags);
  615. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  616. return 0;
  617. }
  618. /*
  619. * Free a connection. Connection must be shut down and not set for reconnect.
  620. */
  621. void rds_iw_conn_free(void *arg)
  622. {
  623. struct rds_iw_connection *ic = arg;
  624. spinlock_t *lock_ptr;
  625. rdsdebug("ic %p\n", ic);
  626. /*
  627. * Conn is either on a dev's list or on the nodev list.
  628. * A race with shutdown() or connect() would cause problems
  629. * (since rds_iwdev would change) but that should never happen.
  630. */
  631. lock_ptr = ic->rds_iwdev ? &ic->rds_iwdev->spinlock : &iw_nodev_conns_lock;
  632. spin_lock_irq(lock_ptr);
  633. list_del(&ic->iw_node);
  634. spin_unlock_irq(lock_ptr);
  635. kfree(ic);
  636. }
  637. /*
  638. * An error occurred on the connection
  639. */
  640. void
  641. __rds_iw_conn_error(struct rds_connection *conn, const char *fmt, ...)
  642. {
  643. va_list ap;
  644. rds_conn_drop(conn);
  645. va_start(ap, fmt);
  646. vprintk(fmt, ap);
  647. va_end(ap);
  648. }