ib_cm.c 36 KB

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