ib_cm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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_single_path.h"
  39. #include "rds.h"
  40. #include "ib.h"
  41. /*
  42. * Set the selected protocol version
  43. */
  44. static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version)
  45. {
  46. conn->c_version = version;
  47. }
  48. /*
  49. * Set up flow control
  50. */
  51. static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits)
  52. {
  53. struct rds_ib_connection *ic = conn->c_transport_data;
  54. if (rds_ib_sysctl_flow_control && credits != 0) {
  55. /* We're doing flow control */
  56. ic->i_flowctl = 1;
  57. rds_ib_send_add_credits(conn, credits);
  58. } else {
  59. ic->i_flowctl = 0;
  60. }
  61. }
  62. /*
  63. * Tune RNR behavior. Without flow control, we use a rather
  64. * low timeout, but not the absolute minimum - this should
  65. * be tunable.
  66. *
  67. * We already set the RNR retry count to 7 (which is the
  68. * smallest infinite number :-) above.
  69. * If flow control is off, we want to change this back to 0
  70. * so that we learn quickly when our credit accounting is
  71. * buggy.
  72. *
  73. * Caller passes in a qp_attr pointer - don't waste stack spacv
  74. * by allocation this twice.
  75. */
  76. static void
  77. rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr)
  78. {
  79. int ret;
  80. attr->min_rnr_timer = IB_RNR_TIMER_000_32;
  81. ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER);
  82. if (ret)
  83. printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret);
  84. }
  85. /*
  86. * Connection established.
  87. * We get here for both outgoing and incoming connection.
  88. */
  89. void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
  90. {
  91. const struct rds_ib_connect_private *dp = NULL;
  92. struct rds_ib_connection *ic = conn->c_transport_data;
  93. struct ib_qp_attr qp_attr;
  94. int err;
  95. if (event->param.conn.private_data_len >= sizeof(*dp)) {
  96. dp = event->param.conn.private_data;
  97. /* make sure it isn't empty data */
  98. if (dp->dp_protocol_major) {
  99. rds_ib_set_protocol(conn,
  100. RDS_PROTOCOL(dp->dp_protocol_major,
  101. dp->dp_protocol_minor));
  102. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  103. }
  104. }
  105. if (conn->c_version < RDS_PROTOCOL(3, 1)) {
  106. printk(KERN_NOTICE "RDS/IB: Connection to %pI4 version %u.%u failed,"
  107. " no longer supported\n",
  108. &conn->c_faddr,
  109. RDS_PROTOCOL_MAJOR(conn->c_version),
  110. RDS_PROTOCOL_MINOR(conn->c_version));
  111. rds_conn_destroy(conn);
  112. return;
  113. } else {
  114. printk(KERN_NOTICE "RDS/IB: connected to %pI4 version %u.%u%s\n",
  115. &conn->c_faddr,
  116. RDS_PROTOCOL_MAJOR(conn->c_version),
  117. RDS_PROTOCOL_MINOR(conn->c_version),
  118. ic->i_flowctl ? ", flow control" : "");
  119. }
  120. /*
  121. * Init rings and fill recv. this needs to wait until protocol negotiation
  122. * is complete, since ring layout is different from 3.0 to 3.1.
  123. */
  124. rds_ib_send_init_ring(ic);
  125. rds_ib_recv_init_ring(ic);
  126. /* Post receive buffers - as a side effect, this will update
  127. * the posted credit count. */
  128. rds_ib_recv_refill(conn, 1, GFP_KERNEL);
  129. /* Tune RNR behavior */
  130. rds_ib_tune_rnr(ic, &qp_attr);
  131. qp_attr.qp_state = IB_QPS_RTS;
  132. err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  133. if (err)
  134. printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err);
  135. /* update ib_device with this local ipaddr */
  136. err = rds_ib_update_ipaddr(ic->rds_ibdev, conn->c_laddr);
  137. if (err)
  138. printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n",
  139. err);
  140. /* If the peer gave us the last packet it saw, process this as if
  141. * we had received a regular ACK. */
  142. if (dp) {
  143. /* dp structure start is not guaranteed to be 8 bytes aligned.
  144. * Since dp_ack_seq is 64-bit extended load operations can be
  145. * used so go through get_unaligned to avoid unaligned errors.
  146. */
  147. __be64 dp_ack_seq = get_unaligned(&dp->dp_ack_seq);
  148. if (dp_ack_seq)
  149. rds_send_drop_acked(conn, be64_to_cpu(dp_ack_seq),
  150. NULL);
  151. }
  152. rds_connect_complete(conn);
  153. }
  154. static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
  155. struct rdma_conn_param *conn_param,
  156. struct rds_ib_connect_private *dp,
  157. u32 protocol_version,
  158. u32 max_responder_resources,
  159. u32 max_initiator_depth)
  160. {
  161. struct rds_ib_connection *ic = conn->c_transport_data;
  162. struct rds_ib_device *rds_ibdev = ic->rds_ibdev;
  163. memset(conn_param, 0, sizeof(struct rdma_conn_param));
  164. conn_param->responder_resources =
  165. min_t(u32, rds_ibdev->max_responder_resources, max_responder_resources);
  166. conn_param->initiator_depth =
  167. min_t(u32, rds_ibdev->max_initiator_depth, max_initiator_depth);
  168. conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7);
  169. conn_param->rnr_retry_count = 7;
  170. if (dp) {
  171. memset(dp, 0, sizeof(*dp));
  172. dp->dp_saddr = conn->c_laddr;
  173. dp->dp_daddr = conn->c_faddr;
  174. dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
  175. dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
  176. dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS);
  177. dp->dp_ack_seq = cpu_to_be64(rds_ib_piggyb_ack(ic));
  178. /* Advertise flow control */
  179. if (ic->i_flowctl) {
  180. unsigned int credits;
  181. credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
  182. dp->dp_credit = cpu_to_be32(credits);
  183. atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
  184. }
  185. conn_param->private_data = dp;
  186. conn_param->private_data_len = sizeof(*dp);
  187. }
  188. }
  189. static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
  190. {
  191. rdsdebug("event %u (%s) data %p\n",
  192. event->event, ib_event_msg(event->event), data);
  193. }
  194. /* Plucking the oldest entry from the ring can be done concurrently with
  195. * the thread refilling the ring. Each ring operation is protected by
  196. * spinlocks and the transient state of refilling doesn't change the
  197. * recording of which entry is oldest.
  198. *
  199. * This relies on IB only calling one cq comp_handler for each cq so that
  200. * there will only be one caller of rds_recv_incoming() per RDS connection.
  201. */
  202. static void rds_ib_cq_comp_handler_recv(struct ib_cq *cq, void *context)
  203. {
  204. struct rds_connection *conn = context;
  205. struct rds_ib_connection *ic = conn->c_transport_data;
  206. rdsdebug("conn %p cq %p\n", conn, cq);
  207. rds_ib_stats_inc(s_ib_evt_handler_call);
  208. tasklet_schedule(&ic->i_recv_tasklet);
  209. }
  210. static void poll_scq(struct rds_ib_connection *ic, struct ib_cq *cq,
  211. struct ib_wc *wcs)
  212. {
  213. int nr, i;
  214. struct ib_wc *wc;
  215. while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) {
  216. for (i = 0; i < nr; i++) {
  217. wc = wcs + i;
  218. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  219. (unsigned long long)wc->wr_id, wc->status,
  220. wc->byte_len, be32_to_cpu(wc->ex.imm_data));
  221. if (wc->wr_id <= ic->i_send_ring.w_nr ||
  222. wc->wr_id == RDS_IB_ACK_WR_ID)
  223. rds_ib_send_cqe_handler(ic, wc);
  224. else
  225. rds_ib_mr_cqe_handler(ic, wc);
  226. }
  227. }
  228. }
  229. static void rds_ib_tasklet_fn_send(unsigned long data)
  230. {
  231. struct rds_ib_connection *ic = (struct rds_ib_connection *)data;
  232. struct rds_connection *conn = ic->conn;
  233. rds_ib_stats_inc(s_ib_tasklet_call);
  234. poll_scq(ic, ic->i_send_cq, ic->i_send_wc);
  235. ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
  236. poll_scq(ic, ic->i_send_cq, ic->i_send_wc);
  237. if (rds_conn_up(conn) &&
  238. (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags) ||
  239. test_bit(0, &conn->c_map_queued)))
  240. rds_send_xmit(&ic->conn->c_path[0]);
  241. }
  242. static void poll_rcq(struct rds_ib_connection *ic, struct ib_cq *cq,
  243. struct ib_wc *wcs,
  244. struct rds_ib_ack_state *ack_state)
  245. {
  246. int nr, i;
  247. struct ib_wc *wc;
  248. while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) {
  249. for (i = 0; i < nr; i++) {
  250. wc = wcs + i;
  251. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  252. (unsigned long long)wc->wr_id, wc->status,
  253. wc->byte_len, be32_to_cpu(wc->ex.imm_data));
  254. rds_ib_recv_cqe_handler(ic, wc, ack_state);
  255. }
  256. }
  257. }
  258. static void rds_ib_tasklet_fn_recv(unsigned long data)
  259. {
  260. struct rds_ib_connection *ic = (struct rds_ib_connection *)data;
  261. struct rds_connection *conn = ic->conn;
  262. struct rds_ib_device *rds_ibdev = ic->rds_ibdev;
  263. struct rds_ib_ack_state state;
  264. if (!rds_ibdev)
  265. rds_conn_drop(conn);
  266. rds_ib_stats_inc(s_ib_tasklet_call);
  267. memset(&state, 0, sizeof(state));
  268. poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state);
  269. ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  270. poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state);
  271. if (state.ack_next_valid)
  272. rds_ib_set_ack(ic, state.ack_next, state.ack_required);
  273. if (state.ack_recv_valid && state.ack_recv > ic->i_ack_recv) {
  274. rds_send_drop_acked(conn, state.ack_recv, NULL);
  275. ic->i_ack_recv = state.ack_recv;
  276. }
  277. if (rds_conn_up(conn))
  278. rds_ib_attempt_ack(ic);
  279. }
  280. static void rds_ib_qp_event_handler(struct ib_event *event, void *data)
  281. {
  282. struct rds_connection *conn = data;
  283. struct rds_ib_connection *ic = conn->c_transport_data;
  284. rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event,
  285. ib_event_msg(event->event));
  286. switch (event->event) {
  287. case IB_EVENT_COMM_EST:
  288. rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
  289. break;
  290. default:
  291. rdsdebug("Fatal QP Event %u (%s) "
  292. "- connection %pI4->%pI4, reconnecting\n",
  293. event->event, ib_event_msg(event->event),
  294. &conn->c_laddr, &conn->c_faddr);
  295. rds_conn_drop(conn);
  296. break;
  297. }
  298. }
  299. static void rds_ib_cq_comp_handler_send(struct ib_cq *cq, void *context)
  300. {
  301. struct rds_connection *conn = context;
  302. struct rds_ib_connection *ic = conn->c_transport_data;
  303. rdsdebug("conn %p cq %p\n", conn, cq);
  304. rds_ib_stats_inc(s_ib_evt_handler_call);
  305. tasklet_schedule(&ic->i_send_tasklet);
  306. }
  307. /*
  308. * This needs to be very careful to not leave IS_ERR pointers around for
  309. * cleanup to trip over.
  310. */
  311. static int rds_ib_setup_qp(struct rds_connection *conn)
  312. {
  313. struct rds_ib_connection *ic = conn->c_transport_data;
  314. struct ib_device *dev = ic->i_cm_id->device;
  315. struct ib_qp_init_attr attr;
  316. struct ib_cq_init_attr cq_attr = {};
  317. struct rds_ib_device *rds_ibdev;
  318. int ret, fr_queue_space;
  319. /*
  320. * It's normal to see a null device if an incoming connection races
  321. * with device removal, so we don't print a warning.
  322. */
  323. rds_ibdev = rds_ib_get_client_data(dev);
  324. if (!rds_ibdev)
  325. return -EOPNOTSUPP;
  326. /* The fr_queue_space is currently set to 512, to add extra space on
  327. * completion queue and send queue. This extra space is used for FRMR
  328. * registration and invalidation work requests
  329. */
  330. fr_queue_space = (rds_ibdev->use_fastreg ? RDS_IB_DEFAULT_FR_WR : 0);
  331. /* add the conn now so that connection establishment has the dev */
  332. rds_ib_add_conn(rds_ibdev, conn);
  333. if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1)
  334. rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1);
  335. if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1)
  336. rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1);
  337. /* Protection domain and memory range */
  338. ic->i_pd = rds_ibdev->pd;
  339. cq_attr.cqe = ic->i_send_ring.w_nr + fr_queue_space + 1;
  340. ic->i_send_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_send,
  341. rds_ib_cq_event_handler, conn,
  342. &cq_attr);
  343. if (IS_ERR(ic->i_send_cq)) {
  344. ret = PTR_ERR(ic->i_send_cq);
  345. ic->i_send_cq = NULL;
  346. rdsdebug("ib_create_cq send failed: %d\n", ret);
  347. goto rds_ibdev_out;
  348. }
  349. cq_attr.cqe = ic->i_recv_ring.w_nr;
  350. ic->i_recv_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_recv,
  351. rds_ib_cq_event_handler, conn,
  352. &cq_attr);
  353. if (IS_ERR(ic->i_recv_cq)) {
  354. ret = PTR_ERR(ic->i_recv_cq);
  355. ic->i_recv_cq = NULL;
  356. rdsdebug("ib_create_cq recv failed: %d\n", ret);
  357. goto send_cq_out;
  358. }
  359. ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
  360. if (ret) {
  361. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  362. goto recv_cq_out;
  363. }
  364. ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  365. if (ret) {
  366. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  367. goto recv_cq_out;
  368. }
  369. /* XXX negotiate max send/recv with remote? */
  370. memset(&attr, 0, sizeof(attr));
  371. attr.event_handler = rds_ib_qp_event_handler;
  372. attr.qp_context = conn;
  373. /* + 1 to allow for the single ack message */
  374. attr.cap.max_send_wr = ic->i_send_ring.w_nr + fr_queue_space + 1;
  375. attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1;
  376. attr.cap.max_send_sge = rds_ibdev->max_sge;
  377. attr.cap.max_recv_sge = RDS_IB_RECV_SGE;
  378. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  379. attr.qp_type = IB_QPT_RC;
  380. attr.send_cq = ic->i_send_cq;
  381. attr.recv_cq = ic->i_recv_cq;
  382. atomic_set(&ic->i_fastreg_wrs, RDS_IB_DEFAULT_FR_WR);
  383. /*
  384. * XXX this can fail if max_*_wr is too large? Are we supposed
  385. * to back off until we get a value that the hardware can support?
  386. */
  387. ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
  388. if (ret) {
  389. rdsdebug("rdma_create_qp failed: %d\n", ret);
  390. goto recv_cq_out;
  391. }
  392. ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
  393. ic->i_send_ring.w_nr *
  394. sizeof(struct rds_header),
  395. &ic->i_send_hdrs_dma, GFP_KERNEL);
  396. if (!ic->i_send_hdrs) {
  397. ret = -ENOMEM;
  398. rdsdebug("ib_dma_alloc_coherent send failed\n");
  399. goto qp_out;
  400. }
  401. ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
  402. ic->i_recv_ring.w_nr *
  403. sizeof(struct rds_header),
  404. &ic->i_recv_hdrs_dma, GFP_KERNEL);
  405. if (!ic->i_recv_hdrs) {
  406. ret = -ENOMEM;
  407. rdsdebug("ib_dma_alloc_coherent recv failed\n");
  408. goto send_hdrs_dma_out;
  409. }
  410. ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
  411. &ic->i_ack_dma, GFP_KERNEL);
  412. if (!ic->i_ack) {
  413. ret = -ENOMEM;
  414. rdsdebug("ib_dma_alloc_coherent ack failed\n");
  415. goto recv_hdrs_dma_out;
  416. }
  417. ic->i_sends = vzalloc_node(ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work),
  418. ibdev_to_node(dev));
  419. if (!ic->i_sends) {
  420. ret = -ENOMEM;
  421. rdsdebug("send allocation failed\n");
  422. goto ack_dma_out;
  423. }
  424. ic->i_recvs = vzalloc_node(ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work),
  425. ibdev_to_node(dev));
  426. if (!ic->i_recvs) {
  427. ret = -ENOMEM;
  428. rdsdebug("recv allocation failed\n");
  429. goto sends_out;
  430. }
  431. rds_ib_recv_init_ack(ic);
  432. rdsdebug("conn %p pd %p cq %p %p\n", conn, ic->i_pd,
  433. ic->i_send_cq, ic->i_recv_cq);
  434. return ret;
  435. sends_out:
  436. vfree(ic->i_sends);
  437. ack_dma_out:
  438. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  439. ic->i_ack, ic->i_ack_dma);
  440. recv_hdrs_dma_out:
  441. ib_dma_free_coherent(dev, ic->i_recv_ring.w_nr *
  442. sizeof(struct rds_header),
  443. ic->i_recv_hdrs, ic->i_recv_hdrs_dma);
  444. send_hdrs_dma_out:
  445. ib_dma_free_coherent(dev, ic->i_send_ring.w_nr *
  446. sizeof(struct rds_header),
  447. ic->i_send_hdrs, ic->i_send_hdrs_dma);
  448. qp_out:
  449. rdma_destroy_qp(ic->i_cm_id);
  450. recv_cq_out:
  451. if (!ib_destroy_cq(ic->i_recv_cq))
  452. ic->i_recv_cq = NULL;
  453. send_cq_out:
  454. if (!ib_destroy_cq(ic->i_send_cq))
  455. ic->i_send_cq = NULL;
  456. rds_ibdev_out:
  457. rds_ib_remove_conn(rds_ibdev, conn);
  458. rds_ib_dev_put(rds_ibdev);
  459. return ret;
  460. }
  461. static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event)
  462. {
  463. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  464. u16 common;
  465. u32 version = 0;
  466. /*
  467. * rdma_cm private data is odd - when there is any private data in the
  468. * request, we will be given a pretty large buffer without telling us the
  469. * original size. The only way to tell the difference is by looking at
  470. * the contents, which are initialized to zero.
  471. * If the protocol version fields aren't set, this is a connection attempt
  472. * from an older version. This could could be 3.0 or 2.0 - we can't tell.
  473. * We really should have changed this for OFED 1.3 :-(
  474. */
  475. /* Be paranoid. RDS always has privdata */
  476. if (!event->param.conn.private_data_len) {
  477. printk(KERN_NOTICE "RDS incoming connection has no private data, "
  478. "rejecting\n");
  479. return 0;
  480. }
  481. /* Even if len is crap *now* I still want to check it. -ASG */
  482. if (event->param.conn.private_data_len < sizeof (*dp) ||
  483. dp->dp_protocol_major == 0)
  484. return RDS_PROTOCOL_3_0;
  485. common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IB_SUPPORTED_PROTOCOLS;
  486. if (dp->dp_protocol_major == 3 && common) {
  487. version = RDS_PROTOCOL_3_0;
  488. while ((common >>= 1) != 0)
  489. version++;
  490. } else
  491. printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using incompatible protocol version %u.%u\n",
  492. &dp->dp_saddr,
  493. dp->dp_protocol_major,
  494. dp->dp_protocol_minor);
  495. return version;
  496. }
  497. int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
  498. struct rdma_cm_event *event)
  499. {
  500. __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id;
  501. __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id;
  502. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  503. struct rds_ib_connect_private dp_rep;
  504. struct rds_connection *conn = NULL;
  505. struct rds_ib_connection *ic = NULL;
  506. struct rdma_conn_param conn_param;
  507. u32 version;
  508. int err = 1, destroy = 1;
  509. /* Check whether the remote protocol version matches ours. */
  510. version = rds_ib_protocol_compatible(event);
  511. if (!version)
  512. goto out;
  513. rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u lguid 0x%llx fguid "
  514. "0x%llx\n", &dp->dp_saddr, &dp->dp_daddr,
  515. RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version),
  516. (unsigned long long)be64_to_cpu(lguid),
  517. (unsigned long long)be64_to_cpu(fguid));
  518. /* RDS/IB is not currently netns aware, thus init_net */
  519. conn = rds_conn_create(&init_net, dp->dp_daddr, dp->dp_saddr,
  520. &rds_ib_transport, GFP_KERNEL);
  521. if (IS_ERR(conn)) {
  522. rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
  523. conn = NULL;
  524. goto out;
  525. }
  526. /*
  527. * The connection request may occur while the
  528. * previous connection exist, e.g. in case of failover.
  529. * But as connections may be initiated simultaneously
  530. * by both hosts, we have a random backoff mechanism -
  531. * see the comment above rds_queue_reconnect()
  532. */
  533. mutex_lock(&conn->c_cm_lock);
  534. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  535. if (rds_conn_state(conn) == RDS_CONN_UP) {
  536. rdsdebug("incoming connect while connecting\n");
  537. rds_conn_drop(conn);
  538. rds_ib_stats_inc(s_ib_listen_closed_stale);
  539. } else
  540. if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
  541. /* Wait and see - our connect may still be succeeding */
  542. rds_ib_stats_inc(s_ib_connect_raced);
  543. }
  544. goto out;
  545. }
  546. ic = conn->c_transport_data;
  547. rds_ib_set_protocol(conn, version);
  548. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  549. /* If the peer gave us the last packet it saw, process this as if
  550. * we had received a regular ACK. */
  551. if (dp->dp_ack_seq)
  552. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  553. BUG_ON(cm_id->context);
  554. BUG_ON(ic->i_cm_id);
  555. ic->i_cm_id = cm_id;
  556. cm_id->context = conn;
  557. /* We got halfway through setting up the ib_connection, if we
  558. * fail now, we have to take the long route out of this mess. */
  559. destroy = 0;
  560. err = rds_ib_setup_qp(conn);
  561. if (err) {
  562. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err);
  563. goto out;
  564. }
  565. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version,
  566. event->param.conn.responder_resources,
  567. event->param.conn.initiator_depth);
  568. /* rdma_accept() calls rdma_reject() internally if it fails */
  569. err = rdma_accept(cm_id, &conn_param);
  570. if (err)
  571. rds_ib_conn_error(conn, "rdma_accept failed (%d)\n", err);
  572. out:
  573. if (conn)
  574. mutex_unlock(&conn->c_cm_lock);
  575. if (err)
  576. rdma_reject(cm_id, NULL, 0);
  577. return destroy;
  578. }
  579. int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id)
  580. {
  581. struct rds_connection *conn = cm_id->context;
  582. struct rds_ib_connection *ic = conn->c_transport_data;
  583. struct rdma_conn_param conn_param;
  584. struct rds_ib_connect_private dp;
  585. int ret;
  586. /* If the peer doesn't do protocol negotiation, we must
  587. * default to RDSv3.0 */
  588. rds_ib_set_protocol(conn, RDS_PROTOCOL_3_0);
  589. ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */
  590. ret = rds_ib_setup_qp(conn);
  591. if (ret) {
  592. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret);
  593. goto out;
  594. }
  595. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION,
  596. UINT_MAX, UINT_MAX);
  597. ret = rdma_connect(cm_id, &conn_param);
  598. if (ret)
  599. rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  600. out:
  601. /* Beware - returning non-zero tells the rdma_cm to destroy
  602. * the cm_id. We should certainly not do it as long as we still
  603. * "own" the cm_id. */
  604. if (ret) {
  605. if (ic->i_cm_id == cm_id)
  606. ret = 0;
  607. }
  608. return ret;
  609. }
  610. int rds_ib_conn_path_connect(struct rds_conn_path *cp)
  611. {
  612. struct rds_connection *conn = cp->cp_conn;
  613. struct rds_ib_connection *ic = conn->c_transport_data;
  614. struct sockaddr_in src, dest;
  615. int ret;
  616. /* XXX I wonder what affect the port space has */
  617. /* delegate cm event handler to rdma_transport */
  618. ic->i_cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler, conn,
  619. RDMA_PS_TCP, IB_QPT_RC);
  620. if (IS_ERR(ic->i_cm_id)) {
  621. ret = PTR_ERR(ic->i_cm_id);
  622. ic->i_cm_id = NULL;
  623. rdsdebug("rdma_create_id() failed: %d\n", ret);
  624. goto out;
  625. }
  626. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  627. src.sin_family = AF_INET;
  628. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  629. src.sin_port = (__force u16)htons(0);
  630. dest.sin_family = AF_INET;
  631. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  632. dest.sin_port = (__force u16)htons(RDS_PORT);
  633. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  634. (struct sockaddr *)&dest,
  635. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  636. if (ret) {
  637. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  638. ret);
  639. rdma_destroy_id(ic->i_cm_id);
  640. ic->i_cm_id = NULL;
  641. }
  642. out:
  643. return ret;
  644. }
  645. /*
  646. * This is so careful about only cleaning up resources that were built up
  647. * so that it can be called at any point during startup. In fact it
  648. * can be called multiple times for a given connection.
  649. */
  650. void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
  651. {
  652. struct rds_connection *conn = cp->cp_conn;
  653. struct rds_ib_connection *ic = conn->c_transport_data;
  654. int err = 0;
  655. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  656. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  657. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  658. if (ic->i_cm_id) {
  659. struct ib_device *dev = ic->i_cm_id->device;
  660. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  661. err = rdma_disconnect(ic->i_cm_id);
  662. if (err) {
  663. /* Actually this may happen quite frequently, when
  664. * an outgoing connect raced with an incoming connect.
  665. */
  666. rdsdebug("failed to disconnect, cm: %p err %d\n",
  667. ic->i_cm_id, err);
  668. }
  669. /*
  670. * We want to wait for tx and rx completion to finish
  671. * before we tear down the connection, but we have to be
  672. * careful not to get stuck waiting on a send ring that
  673. * only has unsignaled sends in it. We've shutdown new
  674. * sends before getting here so by waiting for signaled
  675. * sends to complete we're ensured that there will be no
  676. * more tx processing.
  677. */
  678. wait_event(rds_ib_ring_empty_wait,
  679. rds_ib_ring_empty(&ic->i_recv_ring) &&
  680. (atomic_read(&ic->i_signaled_sends) == 0) &&
  681. (atomic_read(&ic->i_fastreg_wrs) == RDS_IB_DEFAULT_FR_WR));
  682. tasklet_kill(&ic->i_send_tasklet);
  683. tasklet_kill(&ic->i_recv_tasklet);
  684. /* first destroy the ib state that generates callbacks */
  685. if (ic->i_cm_id->qp)
  686. rdma_destroy_qp(ic->i_cm_id);
  687. if (ic->i_send_cq)
  688. ib_destroy_cq(ic->i_send_cq);
  689. if (ic->i_recv_cq)
  690. ib_destroy_cq(ic->i_recv_cq);
  691. /* then free the resources that ib callbacks use */
  692. if (ic->i_send_hdrs)
  693. ib_dma_free_coherent(dev,
  694. ic->i_send_ring.w_nr *
  695. sizeof(struct rds_header),
  696. ic->i_send_hdrs,
  697. ic->i_send_hdrs_dma);
  698. if (ic->i_recv_hdrs)
  699. ib_dma_free_coherent(dev,
  700. ic->i_recv_ring.w_nr *
  701. sizeof(struct rds_header),
  702. ic->i_recv_hdrs,
  703. ic->i_recv_hdrs_dma);
  704. if (ic->i_ack)
  705. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  706. ic->i_ack, ic->i_ack_dma);
  707. if (ic->i_sends)
  708. rds_ib_send_clear_ring(ic);
  709. if (ic->i_recvs)
  710. rds_ib_recv_clear_ring(ic);
  711. rdma_destroy_id(ic->i_cm_id);
  712. /*
  713. * Move connection back to the nodev list.
  714. */
  715. if (ic->rds_ibdev)
  716. rds_ib_remove_conn(ic->rds_ibdev, conn);
  717. ic->i_cm_id = NULL;
  718. ic->i_pd = NULL;
  719. ic->i_send_cq = NULL;
  720. ic->i_recv_cq = NULL;
  721. ic->i_send_hdrs = NULL;
  722. ic->i_recv_hdrs = NULL;
  723. ic->i_ack = NULL;
  724. }
  725. BUG_ON(ic->rds_ibdev);
  726. /* Clear pending transmit */
  727. if (ic->i_data_op) {
  728. struct rds_message *rm;
  729. rm = container_of(ic->i_data_op, struct rds_message, data);
  730. rds_message_put(rm);
  731. ic->i_data_op = NULL;
  732. }
  733. /* Clear the ACK state */
  734. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  735. #ifdef KERNEL_HAS_ATOMIC64
  736. atomic64_set(&ic->i_ack_next, 0);
  737. #else
  738. ic->i_ack_next = 0;
  739. #endif
  740. ic->i_ack_recv = 0;
  741. /* Clear flow control state */
  742. ic->i_flowctl = 0;
  743. atomic_set(&ic->i_credits, 0);
  744. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  745. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  746. if (ic->i_ibinc) {
  747. rds_inc_put(&ic->i_ibinc->ii_inc);
  748. ic->i_ibinc = NULL;
  749. }
  750. vfree(ic->i_sends);
  751. ic->i_sends = NULL;
  752. vfree(ic->i_recvs);
  753. ic->i_recvs = NULL;
  754. }
  755. int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  756. {
  757. struct rds_ib_connection *ic;
  758. unsigned long flags;
  759. int ret;
  760. /* XXX too lazy? */
  761. ic = kzalloc(sizeof(struct rds_ib_connection), gfp);
  762. if (!ic)
  763. return -ENOMEM;
  764. ret = rds_ib_recv_alloc_caches(ic);
  765. if (ret) {
  766. kfree(ic);
  767. return ret;
  768. }
  769. INIT_LIST_HEAD(&ic->ib_node);
  770. tasklet_init(&ic->i_send_tasklet, rds_ib_tasklet_fn_send,
  771. (unsigned long)ic);
  772. tasklet_init(&ic->i_recv_tasklet, rds_ib_tasklet_fn_recv,
  773. (unsigned long)ic);
  774. mutex_init(&ic->i_recv_mutex);
  775. #ifndef KERNEL_HAS_ATOMIC64
  776. spin_lock_init(&ic->i_ack_lock);
  777. #endif
  778. atomic_set(&ic->i_signaled_sends, 0);
  779. /*
  780. * rds_ib_conn_shutdown() waits for these to be emptied so they
  781. * must be initialized before it can be called.
  782. */
  783. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  784. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  785. ic->conn = conn;
  786. conn->c_transport_data = ic;
  787. spin_lock_irqsave(&ib_nodev_conns_lock, flags);
  788. list_add_tail(&ic->ib_node, &ib_nodev_conns);
  789. spin_unlock_irqrestore(&ib_nodev_conns_lock, flags);
  790. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  791. return 0;
  792. }
  793. /*
  794. * Free a connection. Connection must be shut down and not set for reconnect.
  795. */
  796. void rds_ib_conn_free(void *arg)
  797. {
  798. struct rds_ib_connection *ic = arg;
  799. spinlock_t *lock_ptr;
  800. rdsdebug("ic %p\n", ic);
  801. /*
  802. * Conn is either on a dev's list or on the nodev list.
  803. * A race with shutdown() or connect() would cause problems
  804. * (since rds_ibdev would change) but that should never happen.
  805. */
  806. lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock;
  807. spin_lock_irq(lock_ptr);
  808. list_del(&ic->ib_node);
  809. spin_unlock_irq(lock_ptr);
  810. rds_ib_recv_free_caches(ic);
  811. kfree(ic);
  812. }
  813. /*
  814. * An error occurred on the connection
  815. */
  816. void
  817. __rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...)
  818. {
  819. va_list ap;
  820. rds_conn_drop(conn);
  821. va_start(ap, fmt);
  822. vprintk(fmt, ap);
  823. va_end(ap);
  824. }