ib_cm.c 29 KB

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