ib_cm.c 34 KB

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